]>
Commit | Line | Data |
---|---|---|
2a1732b4 VM |
1 | /* |
2 | * This file is free software; you can redistribute it and/or modify | |
3 | * it under the terms of the GNU General Public License, version 2, | |
4 | * as published by the Free Software Foundation. | |
5 | * | |
6 | * In addition to the permissions in the GNU General Public License, | |
7 | * the authors give you unlimited permission to link the compiled | |
8 | * version of this file into combinations with other programs, | |
9 | * and to distribute those combinations without any restriction | |
10 | * coming from the use of this file. (The General Public License | |
11 | * restrictions do apply in other respects; for example, they cover | |
12 | * modification of the file, and distribution when not linked into | |
13 | * a combined executable.) | |
14 | * | |
15 | * This file is distributed in the hope that it will be useful, but | |
16 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
18 | * General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program; see the file COPYING. If not, write to | |
22 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, | |
23 | * Boston, MA 02110-1301, USA. | |
24 | */ | |
e17a3f56 | 25 | #include "test_lib.h" |
e17a3f56 | 26 | #include "fileops.h" |
72a3fe42 | 27 | #include "odb.h" |
e17a3f56 | 28 | |
c041af95 VM |
29 | static char *odb_dir = "test-objects"; |
30 | #include "t03-data.h" | |
31 | ||
e17a3f56 RJ |
32 | static int make_odb_dir(void) |
33 | { | |
f79026b4 | 34 | if (p_mkdir(odb_dir, 0755) < 0) { |
1e5dd572 RJ |
35 | int err = errno; |
36 | fprintf(stderr, "can't make directory \"%s\"", odb_dir); | |
37 | if (err == EEXIST) | |
38 | fprintf(stderr, " (already exists)"); | |
39 | fprintf(stderr, "\n"); | |
40 | return -1; | |
41 | } | |
42 | return 0; | |
e17a3f56 RJ |
43 | } |
44 | ||
e17a3f56 RJ |
45 | static int check_object_files(object_data *d) |
46 | { | |
f79026b4 | 47 | if (git_futils_exists(d->dir) < 0) |
1e5dd572 | 48 | return -1; |
f79026b4 | 49 | if (git_futils_exists(d->file) < 0) |
1e5dd572 RJ |
50 | return -1; |
51 | return 0; | |
e17a3f56 RJ |
52 | } |
53 | ||
f49a2e49 | 54 | static int cmp_objects(git_rawobj *o1, git_rawobj *o2) |
e17a3f56 | 55 | { |
1e5dd572 RJ |
56 | if (o1->type != o2->type) |
57 | return -1; | |
58 | if (o1->len != o2->len) | |
59 | return -1; | |
60 | if ((o1->len > 0) && (memcmp(o1->data, o2->data, o1->len) != 0)) | |
61 | return -1; | |
62 | return 0; | |
e17a3f56 RJ |
63 | } |
64 | ||
2a1732b4 VM |
65 | static int remove_object_files(object_data *d) |
66 | { | |
f79026b4 | 67 | if (p_unlink(d->file) < 0) { |
2a1732b4 VM |
68 | fprintf(stderr, "can't delete object file \"%s\"\n", d->file); |
69 | return -1; | |
70 | } | |
f79026b4 | 71 | if ((p_rmdir(d->dir) < 0) && (errno != ENOTEMPTY)) { |
2a1732b4 VM |
72 | fprintf(stderr, "can't remove directory \"%s\"\n", d->dir); |
73 | return -1; | |
74 | } | |
75 | ||
f79026b4 | 76 | if (p_rmdir(odb_dir) < 0) { |
2a1732b4 VM |
77 | fprintf(stderr, "can't remove directory \"%s\"\n", odb_dir); |
78 | return -1; | |
79 | } | |
80 | ||
81 | return 0; | |
82 | } | |
83 | ||
72a3fe42 VM |
84 | static int streaming_write(git_oid *oid, git_odb *odb, git_rawobj *raw) |
85 | { | |
86 | git_odb_stream *stream; | |
87 | int error; | |
88 | ||
89 | if ((error = git_odb_open_wstream(&stream, odb, raw->len, raw->type)) < GIT_SUCCESS) | |
90 | return error; | |
91 | ||
92 | stream->write(stream, raw->data, raw->len); | |
93 | ||
94 | error = stream->finalize_write(oid, stream); | |
95 | stream->free(stream); | |
96 | ||
97 | return error; | |
98 | } | |
99 | ||
3dccfed1 | 100 | BEGIN_TEST(write0, "write loose commit object") |
e17a3f56 RJ |
101 | git_odb *db; |
102 | git_oid id1, id2; | |
72a3fe42 | 103 | git_odb_object *obj; |
e17a3f56 RJ |
104 | |
105 | must_pass(make_odb_dir()); | |
106 | must_pass(git_odb_open(&db, odb_dir)); | |
fa48608e | 107 | must_pass(git_oid_fromstr(&id1, commit.id)); |
e17a3f56 | 108 | |
72a3fe42 | 109 | must_pass(streaming_write(&id2, db, &commit_obj)); |
e17a3f56 RJ |
110 | must_be_true(git_oid_cmp(&id1, &id2) == 0); |
111 | must_pass(check_object_files(&commit)); | |
112 | ||
7d7cd885 | 113 | must_pass(git_odb_read(&obj, db, &id1)); |
72a3fe42 | 114 | must_pass(cmp_objects(&obj->raw, &commit_obj)); |
e17a3f56 | 115 | |
72a3fe42 | 116 | git_odb_object_close(obj); |
e17a3f56 RJ |
117 | git_odb_close(db); |
118 | must_pass(remove_object_files(&commit)); | |
119 | END_TEST | |
120 | ||
3dccfed1 | 121 | BEGIN_TEST(write1, "write loose tree object") |
e17a3f56 RJ |
122 | git_odb *db; |
123 | git_oid id1, id2; | |
72a3fe42 | 124 | git_odb_object *obj; |
e17a3f56 RJ |
125 | |
126 | must_pass(make_odb_dir()); | |
127 | must_pass(git_odb_open(&db, odb_dir)); | |
fa48608e | 128 | must_pass(git_oid_fromstr(&id1, tree.id)); |
e17a3f56 | 129 | |
72a3fe42 | 130 | must_pass(streaming_write(&id2, db, &tree_obj)); |
e17a3f56 RJ |
131 | must_be_true(git_oid_cmp(&id1, &id2) == 0); |
132 | must_pass(check_object_files(&tree)); | |
133 | ||
7d7cd885 | 134 | must_pass(git_odb_read(&obj, db, &id1)); |
72a3fe42 | 135 | must_pass(cmp_objects(&obj->raw, &tree_obj)); |
e17a3f56 | 136 | |
72a3fe42 | 137 | git_odb_object_close(obj); |
e17a3f56 RJ |
138 | git_odb_close(db); |
139 | must_pass(remove_object_files(&tree)); | |
140 | END_TEST | |
141 | ||
3dccfed1 | 142 | BEGIN_TEST(write2, "write loose tag object") |
e17a3f56 RJ |
143 | git_odb *db; |
144 | git_oid id1, id2; | |
72a3fe42 | 145 | git_odb_object *obj; |
e17a3f56 RJ |
146 | |
147 | must_pass(make_odb_dir()); | |
148 | must_pass(git_odb_open(&db, odb_dir)); | |
fa48608e | 149 | must_pass(git_oid_fromstr(&id1, tag.id)); |
e17a3f56 | 150 | |
72a3fe42 | 151 | must_pass(streaming_write(&id2, db, &tag_obj)); |
e17a3f56 RJ |
152 | must_be_true(git_oid_cmp(&id1, &id2) == 0); |
153 | must_pass(check_object_files(&tag)); | |
154 | ||
7d7cd885 | 155 | must_pass(git_odb_read(&obj, db, &id1)); |
72a3fe42 | 156 | must_pass(cmp_objects(&obj->raw, &tag_obj)); |
e17a3f56 | 157 | |
72a3fe42 | 158 | git_odb_object_close(obj); |
e17a3f56 RJ |
159 | git_odb_close(db); |
160 | must_pass(remove_object_files(&tag)); | |
161 | END_TEST | |
162 | ||
3dccfed1 | 163 | BEGIN_TEST(write3, "write zero-length object") |
e17a3f56 RJ |
164 | git_odb *db; |
165 | git_oid id1, id2; | |
72a3fe42 | 166 | git_odb_object *obj; |
e17a3f56 RJ |
167 | |
168 | must_pass(make_odb_dir()); | |
169 | must_pass(git_odb_open(&db, odb_dir)); | |
fa48608e | 170 | must_pass(git_oid_fromstr(&id1, zero.id)); |
e17a3f56 | 171 | |
72a3fe42 | 172 | must_pass(streaming_write(&id2, db, &zero_obj)); |
e17a3f56 RJ |
173 | must_be_true(git_oid_cmp(&id1, &id2) == 0); |
174 | must_pass(check_object_files(&zero)); | |
175 | ||
7d7cd885 | 176 | must_pass(git_odb_read(&obj, db, &id1)); |
72a3fe42 | 177 | must_pass(cmp_objects(&obj->raw, &zero_obj)); |
e17a3f56 | 178 | |
72a3fe42 | 179 | git_odb_object_close(obj); |
e17a3f56 RJ |
180 | git_odb_close(db); |
181 | must_pass(remove_object_files(&zero)); | |
182 | END_TEST | |
183 | ||
3dccfed1 | 184 | BEGIN_TEST(write4, "write one-byte long object") |
e17a3f56 RJ |
185 | git_odb *db; |
186 | git_oid id1, id2; | |
72a3fe42 | 187 | git_odb_object *obj; |
e17a3f56 RJ |
188 | |
189 | must_pass(make_odb_dir()); | |
190 | must_pass(git_odb_open(&db, odb_dir)); | |
fa48608e | 191 | must_pass(git_oid_fromstr(&id1, one.id)); |
e17a3f56 | 192 | |
72a3fe42 | 193 | must_pass(streaming_write(&id2, db, &one_obj)); |
e17a3f56 RJ |
194 | must_be_true(git_oid_cmp(&id1, &id2) == 0); |
195 | must_pass(check_object_files(&one)); | |
196 | ||
7d7cd885 | 197 | must_pass(git_odb_read(&obj, db, &id1)); |
72a3fe42 | 198 | must_pass(cmp_objects(&obj->raw, &one_obj)); |
e17a3f56 | 199 | |
72a3fe42 | 200 | git_odb_object_close(obj); |
e17a3f56 RJ |
201 | git_odb_close(db); |
202 | must_pass(remove_object_files(&one)); | |
203 | END_TEST | |
204 | ||
3dccfed1 | 205 | BEGIN_TEST(write5, "write two-byte long object") |
e17a3f56 RJ |
206 | git_odb *db; |
207 | git_oid id1, id2; | |
72a3fe42 | 208 | git_odb_object *obj; |
e17a3f56 RJ |
209 | |
210 | must_pass(make_odb_dir()); | |
211 | must_pass(git_odb_open(&db, odb_dir)); | |
fa48608e | 212 | must_pass(git_oid_fromstr(&id1, two.id)); |
e17a3f56 | 213 | |
72a3fe42 | 214 | must_pass(streaming_write(&id2, db, &two_obj)); |
e17a3f56 RJ |
215 | must_be_true(git_oid_cmp(&id1, &id2) == 0); |
216 | must_pass(check_object_files(&two)); | |
217 | ||
7d7cd885 | 218 | must_pass(git_odb_read(&obj, db, &id1)); |
72a3fe42 | 219 | must_pass(cmp_objects(&obj->raw, &two_obj)); |
e17a3f56 | 220 | |
72a3fe42 | 221 | git_odb_object_close(obj); |
e17a3f56 RJ |
222 | git_odb_close(db); |
223 | must_pass(remove_object_files(&two)); | |
224 | END_TEST | |
225 | ||
3dccfed1 | 226 | BEGIN_TEST(write6, "write an object which is several bytes long") |
e17a3f56 RJ |
227 | git_odb *db; |
228 | git_oid id1, id2; | |
72a3fe42 | 229 | git_odb_object *obj; |
e17a3f56 RJ |
230 | |
231 | must_pass(make_odb_dir()); | |
232 | must_pass(git_odb_open(&db, odb_dir)); | |
fa48608e | 233 | must_pass(git_oid_fromstr(&id1, some.id)); |
e17a3f56 | 234 | |
72a3fe42 | 235 | must_pass(streaming_write(&id2, db, &some_obj)); |
e17a3f56 RJ |
236 | must_be_true(git_oid_cmp(&id1, &id2) == 0); |
237 | must_pass(check_object_files(&some)); | |
238 | ||
7d7cd885 | 239 | must_pass(git_odb_read(&obj, db, &id1)); |
72a3fe42 | 240 | must_pass(cmp_objects(&obj->raw, &some_obj)); |
e17a3f56 | 241 | |
72a3fe42 | 242 | git_odb_object_close(obj); |
e17a3f56 RJ |
243 | git_odb_close(db); |
244 | must_pass(remove_object_files(&some)); | |
245 | END_TEST | |
246 | ||
3dccfed1 VM |
247 | BEGIN_SUITE(objwrite) |
248 | ADD_TEST(write0); | |
249 | ADD_TEST(write1); | |
250 | ADD_TEST(write2); | |
251 | ADD_TEST(write3); | |
252 | ADD_TEST(write4); | |
253 | ADD_TEST(write5); | |
254 | ADD_TEST(write6); | |
255 | END_SUITE |