]> git.proxmox.com Git - libgit2.git/blob - tests/t02-objread.c
Merge pull request #405 from carlosmn/http-ls
[libgit2.git] / tests / t02-objread.c
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 */
25 #include "test_lib.h"
26 #include "test_helpers.h"
27 #include "odb.h"
28
29 #include "t02-data.h"
30 #include "t02-oids.h"
31
32
33 BEGIN_TEST(existsloose0, "check if a loose object exists on the odb")
34 git_odb *db;
35 git_oid id, id2;
36
37 must_pass(write_object_files(odb_dir, &one));
38 must_pass(git_odb_open(&db, odb_dir));
39 must_pass(git_oid_fromstr(&id, one.id));
40
41 must_be_true(git_odb_exists(db, &id));
42
43 /* Test for a non-existant object */
44 must_pass(git_oid_fromstr(&id2, "8b137891791fe96927ad78e64b0aad7bded08baa"));
45 must_be_true(0 == git_odb_exists(db, &id2));
46
47 git_odb_close(db);
48 must_pass(remove_object_files(odb_dir, &one));
49 END_TEST
50
51 BEGIN_TEST(readloose0, "read a loose commit")
52 git_odb *db;
53 git_oid id;
54 git_odb_object *obj;
55
56 must_pass(write_object_files(odb_dir, &commit));
57 must_pass(git_odb_open(&db, odb_dir));
58 must_pass(git_oid_fromstr(&id, commit.id));
59
60 must_pass(git_odb_read(&obj, db, &id));
61 must_pass(cmp_objects((git_rawobj *)&obj->raw, &commit));
62
63 git_odb_object_close(obj);
64 git_odb_close(db);
65 must_pass(remove_object_files(odb_dir, &commit));
66 END_TEST
67
68 BEGIN_TEST(readloose1, "read a loose tree")
69 git_odb *db;
70 git_oid id;
71 git_odb_object *obj;
72
73 must_pass(write_object_files(odb_dir, &tree));
74 must_pass(git_odb_open(&db, odb_dir));
75 must_pass(git_oid_fromstr(&id, tree.id));
76
77 must_pass(git_odb_read(&obj, db, &id));
78 must_pass(cmp_objects((git_rawobj *)&obj->raw, &tree));
79
80 git_odb_object_close(obj);
81 git_odb_close(db);
82 must_pass(remove_object_files(odb_dir, &tree));
83 END_TEST
84
85 BEGIN_TEST(readloose2, "read a loose tag")
86 git_odb *db;
87 git_oid id;
88 git_odb_object *obj;
89
90 must_pass(write_object_files(odb_dir, &tag));
91 must_pass(git_odb_open(&db, odb_dir));
92 must_pass(git_oid_fromstr(&id, tag.id));
93
94 must_pass(git_odb_read(&obj, db, &id));
95 must_pass(cmp_objects((git_rawobj *)&obj->raw, &tag));
96
97 git_odb_object_close(obj);
98 git_odb_close(db);
99 must_pass(remove_object_files(odb_dir, &tag));
100 END_TEST
101
102 BEGIN_TEST(readloose3, "read a loose zero-bytes object")
103 git_odb *db;
104 git_oid id;
105 git_odb_object *obj;
106
107 must_pass(write_object_files(odb_dir, &zero));
108 must_pass(git_odb_open(&db, odb_dir));
109 must_pass(git_oid_fromstr(&id, zero.id));
110
111 must_pass(git_odb_read(&obj, db, &id));
112 must_pass(cmp_objects((git_rawobj *)&obj->raw, &zero));
113
114 git_odb_object_close(obj);
115 git_odb_close(db);
116 must_pass(remove_object_files(odb_dir, &zero));
117 END_TEST
118
119 BEGIN_TEST(readloose4, "read a one-byte long loose object")
120 git_odb *db;
121 git_oid id;
122 git_odb_object *obj;
123
124 must_pass(write_object_files(odb_dir, &one));
125 must_pass(git_odb_open(&db, odb_dir));
126 must_pass(git_oid_fromstr(&id, one.id));
127
128 must_pass(git_odb_read(&obj, db, &id));
129 must_pass(cmp_objects(&obj->raw, &one));
130
131 git_odb_object_close(obj);
132 git_odb_close(db);
133 must_pass(remove_object_files(odb_dir, &one));
134 END_TEST
135
136 BEGIN_TEST(readloose5, "read a two-bytes long loose object")
137 git_odb *db;
138 git_oid id;
139 git_odb_object *obj;
140
141 must_pass(write_object_files(odb_dir, &two));
142 must_pass(git_odb_open(&db, odb_dir));
143 must_pass(git_oid_fromstr(&id, two.id));
144
145 must_pass(git_odb_read(&obj, db, &id));
146 must_pass(cmp_objects(&obj->raw, &two));
147
148 git_odb_object_close(obj);
149 git_odb_close(db);
150 must_pass(remove_object_files(odb_dir, &two));
151 END_TEST
152
153 BEGIN_TEST(readloose6, "read a loose object which is several bytes long")
154 git_odb *db;
155 git_oid id;
156 git_odb_object *obj;
157
158 must_pass(write_object_files(odb_dir, &some));
159 must_pass(git_odb_open(&db, odb_dir));
160 must_pass(git_oid_fromstr(&id, some.id));
161
162 must_pass(git_odb_read(&obj, db, &id));
163 must_pass(cmp_objects(&obj->raw, &some));
164
165 git_odb_object_close(obj);
166 git_odb_close(db);
167 must_pass(remove_object_files(odb_dir, &some));
168 END_TEST
169
170 BEGIN_TEST(readpack0, "read several packed objects")
171 unsigned int i;
172 git_odb *db;
173
174 must_pass(git_odb_open(&db, ODB_FOLDER));
175
176 for (i = 0; i < ARRAY_SIZE(packed_objects); ++i) {
177 git_oid id;
178 git_odb_object *obj;
179
180 must_pass(git_oid_fromstr(&id, packed_objects[i]));
181 must_be_true(git_odb_exists(db, &id) == 1);
182 must_pass(git_odb_read(&obj, db, &id));
183
184 git_odb_object_close(obj);
185 }
186
187 git_odb_close(db);
188 END_TEST
189
190 BEGIN_TEST(readheader0, "read only the header of several packed objects")
191 unsigned int i;
192 git_odb *db;
193
194 must_pass(git_odb_open(&db, ODB_FOLDER));
195
196 for (i = 0; i < ARRAY_SIZE(packed_objects); ++i) {
197 git_oid id;
198 git_odb_object *obj;
199 size_t len;
200 git_otype type;
201
202 must_pass(git_oid_fromstr(&id, packed_objects[i]));
203
204 must_pass(git_odb_read(&obj, db, &id));
205 must_pass(git_odb_read_header(&len, &type, db, &id));
206
207 must_be_true(obj->raw.len == len);
208 must_be_true(obj->raw.type == type);
209
210 git_odb_object_close(obj);
211 }
212
213 git_odb_close(db);
214 END_TEST
215
216 BEGIN_TEST(readheader1, "read only the header of several loose objects")
217 unsigned int i;
218 git_odb *db;
219
220 must_pass(git_odb_open(&db, ODB_FOLDER));
221
222 for (i = 0; i < ARRAY_SIZE(loose_objects); ++i) {
223 git_oid id;
224 git_odb_object *obj;
225 size_t len;
226 git_otype type;
227
228 must_pass(git_oid_fromstr(&id, loose_objects[i]));
229
230 must_be_true(git_odb_exists(db, &id) == 1);
231
232 must_pass(git_odb_read(&obj, db, &id));
233 must_pass(git_odb_read_header(&len, &type, db, &id));
234
235 must_be_true(obj->raw.len == len);
236 must_be_true(obj->raw.type == type);
237
238 git_odb_object_close(obj);
239 }
240
241 git_odb_close(db);
242 END_TEST
243
244 BEGIN_SUITE(objread)
245 ADD_TEST(existsloose0);
246
247 ADD_TEST(readloose0);
248 ADD_TEST(readloose1);
249 ADD_TEST(readloose2);
250 ADD_TEST(readloose3);
251 ADD_TEST(readloose4);
252 ADD_TEST(readloose5);
253 ADD_TEST(readloose6);
254
255 /*
256 ADD_TEST(readloose_enc0);
257 ADD_TEST(readloose_enc1);
258 ADD_TEST(readloose_enc2);
259 ADD_TEST(readloose_enc3);
260 ADD_TEST(readloose_enc4);
261 ADD_TEST(readloose_enc5);
262 ADD_TEST(readloose_enc6);
263 */
264
265 ADD_TEST(readpack0);
266
267 ADD_TEST(readheader0);
268 ADD_TEST(readheader1);
269 END_SUITE