Blob


1 /*
2 * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
20 #include <stdarg.h>
21 #include <stdio.h>
22 #include <util.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <err.h>
27 #include <unistd.h>
29 #include "got_error.h"
30 #include "got_object.h"
31 #include "got_reference.h"
32 #include "got_repository.h"
33 #include "got_diff.h"
34 #include "got_opentemp.h"
36 #include "got_lib_path.h"
38 #ifndef nitems
39 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
40 #endif
42 #define GOT_REPO_PATH "../../../"
44 static int verbose;
46 void
47 test_printf(char *fmt, ...)
48 {
49 va_list ap;
51 if (!verbose)
52 return;
54 va_start(ap, fmt);
55 vprintf(fmt, ap);
56 va_end(ap);
57 }
59 static const struct got_error *
60 print_commit_object(struct got_object *, struct got_repository *);
62 static const struct got_error *
63 print_parent_commits(struct got_commit_object *commit,
64 struct got_repository *repo)
65 {
66 const struct got_object_id_queue *parent_ids;
67 struct got_object_qid *qid;
68 const struct got_error *err = NULL;
69 struct got_object *obj;
71 parent_ids = got_object_commit_get_parent_ids(commit);
72 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
73 err = got_object_open(&obj, repo, qid->id);
74 if (err != NULL)
75 return err;
76 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT)
77 err = got_error(GOT_ERR_OBJ_TYPE);
78 else
79 err = print_commit_object(obj, repo);
80 got_object_close(obj);
81 if (err)
82 break;
83 }
85 return err;
86 }
88 static const struct got_error *
89 print_tree_object(struct got_object *obj, char *parent,
90 struct got_repository *repo)
91 {
92 struct got_tree_object *tree;
93 const struct got_tree_entries *entries;
94 struct got_tree_entry *te;
95 const struct got_error *err;
97 err = got_object_tree_open(&tree, repo, obj);
98 if (err != NULL)
99 return err;
101 entries = got_object_tree_get_entries(tree);
102 SIMPLEQ_FOREACH(te, &entries->head, entry) {
103 struct got_object *treeobj;
104 char *next_parent;
105 char *hex;
107 err = got_object_id_str(&hex, te->id);
108 if (err)
109 break;
111 if (!S_ISDIR(te->mode)) {
112 test_printf("%s %s/%s\n", hex, parent, te->name);
113 free(hex);
114 continue;
116 test_printf("%s %s/%s\n", hex, parent, te->name);
117 free(hex);
119 err = got_object_open(&treeobj, repo, te->id);
120 if (err != NULL)
121 break;
123 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
124 err = got_error(GOT_ERR_OBJ_TYPE);
125 got_object_close(treeobj);
126 break;
129 if (asprintf(&next_parent, "%s/%s", parent, te->name) == -1) {
130 err = got_error_from_errno();
131 got_object_close(treeobj);
132 break;
135 err = print_tree_object(treeobj, next_parent, repo);
136 free(next_parent);
137 if (err) {
138 got_object_close(treeobj);
139 break;
142 got_object_close(treeobj);
145 got_object_tree_close(tree);
146 return err;
149 static const struct got_error *
150 print_commit_object(struct got_object *obj, struct got_repository *repo)
152 struct got_commit_object *commit;
153 const struct got_object_id_queue *parent_ids;
154 struct got_object_qid *qid;
155 char *buf;
156 const struct got_error *err;
157 struct got_object* treeobj;
159 err = got_object_commit_open(&commit, repo, obj);
160 if (err)
161 return err;
163 err = got_object_id_str(&buf, got_object_commit_get_tree_id(commit));
164 if (err)
165 return err;
166 test_printf("tree: %s\n", buf);
167 free(buf);
168 test_printf("parent%s: ",
169 (got_object_commit_get_nparents(commit) == 1) ? "" : "s");
170 parent_ids = got_object_commit_get_parent_ids(commit);
171 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
172 err = got_object_id_str(&buf, qid->id);
173 if (err)
174 return err;
175 test_printf("%s\n", buf);
176 free(buf);
178 test_printf("author: %s\n", got_object_commit_get_author(commit));
179 test_printf("committer: %s\n", got_object_commit_get_committer(commit));
180 test_printf("log: %s\n", got_object_commit_get_logmsg(commit));
182 err = got_object_open(&treeobj, repo,
183 got_object_commit_get_tree_id(commit));
184 if (err != NULL)
185 return err;
186 if (got_object_get_type(treeobj) == GOT_OBJ_TYPE_TREE) {
187 print_tree_object(treeobj, "", repo);
188 test_printf("\n");
190 got_object_close(treeobj);
192 err = print_parent_commits(commit, repo);
193 got_object_commit_close(commit);
195 return err;
198 static int
199 repo_read_log(const char *repo_path)
201 const struct got_error *err;
202 struct got_repository *repo;
203 struct got_reference *head_ref;
204 struct got_object_id *id;
205 struct got_object *obj;
206 char *buf;
208 err = got_repo_open(&repo, repo_path);
209 if (err != NULL || repo == NULL)
210 return 0;
211 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
212 if (err != NULL || head_ref == NULL)
213 return 0;
214 err = got_ref_resolve(&id, repo, head_ref);
215 if (err != NULL || head_ref == NULL)
216 return 0;
217 err = got_object_id_str(&buf, id);
218 if (err != NULL)
219 return 0;
220 test_printf("HEAD is at %s\n", buf);
221 free(buf);
222 err = got_object_open(&obj, repo, id);
223 if (err != NULL || obj == NULL)
224 return 0;
225 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) {
226 err = print_commit_object(obj, repo);
227 if (err)
228 return 0;
229 } else
230 return 0;
231 got_object_close(obj);
232 free(id);
233 got_ref_close(head_ref);
234 got_repo_close(repo);
235 return 1;
238 static int
239 repo_read_tree(const char *repo_path)
241 const char *tree_sha1 = "6cc96e0e093fb30630ba7f199d0a008b24c6a690";
242 const struct got_error *err;
243 struct got_repository *repo;
244 struct got_object *obj;
246 err = got_repo_open(&repo, repo_path);
247 if (err != NULL || repo == NULL)
248 return 0;
249 err = got_object_open_by_id_str(&obj, repo, tree_sha1);
250 if (err != NULL || obj == NULL)
251 return 0;
252 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE)
253 return 0;
255 print_tree_object(obj, "", repo);
256 test_printf("\n");
258 got_object_close(obj);
259 got_repo_close(repo);
260 return (err == NULL);
263 static int
264 repo_read_blob(const char *repo_path)
266 const char *blob_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
267 const struct got_error *err;
268 struct got_repository *repo;
269 struct got_object *obj;
270 struct got_blob_object *blob;
271 int i;
272 size_t len;
274 err = got_repo_open(&repo, repo_path);
275 if (err != NULL || repo == NULL)
276 return 0;
277 err = got_object_open_by_id_str(&obj, repo, blob_sha1);
278 if (err != NULL || obj == NULL)
279 return 0;
280 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB)
281 return 0;
283 err = got_object_blob_open(&blob, repo, obj, 64);
284 if (err != NULL)
285 return 0;
287 test_printf("\n");
288 do {
289 const uint8_t *buf = got_object_blob_get_read_buf(blob);
290 err = got_object_blob_read_block(&len, blob);
291 if (err)
292 break;
293 for (i = 0; i < len; i++)
294 test_printf("%c", buf[i]);
295 } while (len != 0);
296 test_printf("\n");
298 got_object_blob_close(blob);
299 got_object_close(obj);
300 got_repo_close(repo);
301 return (err == NULL);
304 static int
305 repo_diff_blob(const char *repo_path)
307 const char *blob1_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
308 const char *blob2_sha1 = "de7eb21b21c7823a753261aadf7cba35c9580fbf";
309 const struct got_error *err;
310 struct got_repository *repo;
311 struct got_object *obj1;
312 struct got_object *obj2;
313 struct got_blob_object *blob1;
314 struct got_blob_object *blob2;
315 FILE *outfile;
316 int i;
317 char *line;
318 size_t len;
319 const char delim[3] = {'\0', '\0', '\0'};
320 const char *expected_output[] = {
321 "--- 141f5fdc96126c1f4195558560a3c915e3d9b4c3",
322 "+++ de7eb21b21c7823a753261aadf7cba35c9580fbf",
323 "@@ -1,10 +1,10 @@",
324 " .PATH:${.CURDIR}/../../lib",
325 " ",
326 " PROG = repository_test",
327 "-SRCS = path.c repository.c error.c refs.c repository_test.c",
328 "+SRCS = path.c repository.c error.c refs.c object.c sha1.c repository_test.c",
329 " ",
330 " CPPFLAGS = -I${.CURDIR}/../../include",
331 "-LDADD = -lutil",
332 "+LDADD = -lutil -lz",
333 " ",
334 " NOMAN = yes"
335 };
337 err = got_repo_open(&repo, repo_path);
338 if (err != NULL || repo == NULL)
339 return 0;
341 err = got_object_open_by_id_str(&obj1, repo, blob1_sha1);
342 if (err != NULL || obj1 == NULL)
343 return 0;
344 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB)
345 return 0;
346 err = got_object_open_by_id_str(&obj2, repo, blob2_sha1);
347 if (err != NULL || obj2 == NULL)
348 return 0;
349 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB)
350 return 0;
352 err = got_object_blob_open(&blob1, repo, obj1, 512);
353 if (err != NULL)
354 return 0;
356 err = got_object_blob_open(&blob2, repo, obj2, 512);
357 if (err != NULL)
358 return 0;
360 test_printf("\n");
361 outfile = got_opentemp();
362 if (outfile == NULL)
363 return 0;
364 got_diff_blob(blob1, blob2, NULL, NULL, 3, outfile);
365 rewind(outfile);
366 i = 0;
367 while ((line = fparseln(outfile, &len, NULL, delim, 0)) != NULL) {
368 test_printf(line);
369 test_printf("\n");
370 if (i < nitems(expected_output) &&
371 strcmp(line, expected_output[i]) != 0) {
372 test_printf("diff output mismatch; expected: '%s'\n",
373 expected_output[i]);
374 return 0;
376 i++;
378 fclose(outfile);
379 test_printf("\n");
380 if (i != nitems(expected_output) + 1) {
381 test_printf("number of lines expected: %d; actual: %d\n",
382 nitems(expected_output), i - 1);
383 return 0;
386 got_object_blob_close(blob1);
387 got_object_blob_close(blob2);
388 got_object_close(obj1);
389 got_object_close(obj2);
390 got_repo_close(repo);
391 return (err == NULL);
394 static int
395 repo_diff_tree(const char *repo_path)
397 const char *tree1_sha1 = "1efc41caf761a0a1f119d0c5121eedcb2e7a88c3";
398 const char *tree2_sha1 = "4aa8f2933839ff8a8fb3f905a4c232d22c6ff5f3";
399 const struct got_error *err;
400 struct got_repository *repo;
401 struct got_object *obj1;
402 struct got_object *obj2;
403 struct got_tree_object *tree1;
404 struct got_tree_object *tree2;
405 FILE *outfile;
407 err = got_repo_open(&repo, repo_path);
408 if (err != NULL || repo == NULL)
409 return 0;
411 err = got_object_open_by_id_str(&obj1, repo, tree1_sha1);
412 if (err != NULL || obj1 == NULL)
413 return 0;
414 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_TREE)
415 return 0;
416 err = got_object_open_by_id_str(&obj2, repo, tree2_sha1);
417 if (err != NULL || obj2 == NULL)
418 return 0;
419 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_TREE)
420 return 0;
422 err = got_object_tree_open(&tree1, repo, obj1);
423 if (err != NULL)
424 return 0;
426 err = got_object_tree_open(&tree2, repo, obj2);
427 if (err != NULL)
428 return 0;
430 if (!verbose) {
431 outfile = fopen("/dev/null", "w+");
432 if (outfile == NULL)
433 return 0;
434 } else
435 outfile = stdout;
436 test_printf("\n");
437 got_diff_tree(tree1, tree2, "", "", 3, repo, outfile);
438 test_printf("\n");
440 got_object_tree_close(tree1);
441 got_object_tree_close(tree2);
442 got_object_close(obj1);
443 got_object_close(obj2);
444 got_repo_close(repo);
445 return (err == NULL);
448 #define RUN_TEST(expr, name) \
449 { test_ok = (expr); \
450 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
451 failure = (failure || !test_ok); }
454 void
455 usage(void)
457 fprintf(stderr, "usage: repository_test [-v] [REPO_PATH]\n");
460 int
461 main(int argc, char *argv[])
463 int test_ok = 0, failure = 0;
464 const char *repo_path;
465 int ch;
467 #ifndef PROFILE
468 if (pledge("stdio rpath wpath cpath proc exec sendfd", NULL) == -1)
469 err(1, "pledge");
470 #endif
472 while ((ch = getopt(argc, argv, "v")) != -1) {
473 switch (ch) {
474 case 'v':
475 verbose = 1;
476 break;
477 default:
478 usage();
479 return 1;
482 argc -= optind;
483 argv += optind;
485 if (argc == 0)
486 repo_path = GOT_REPO_PATH;
487 else if (argc == 1)
488 repo_path = argv[0];
489 else {
490 usage();
491 return 1;
494 RUN_TEST(repo_read_tree(repo_path), "read_tree");
495 RUN_TEST(repo_read_log(repo_path), "read_log");
496 RUN_TEST(repo_read_blob(repo_path), "read_blob");
497 RUN_TEST(repo_diff_blob(repo_path), "diff_blob");
498 RUN_TEST(repo_diff_tree(repo_path), "diff_tree");
500 return failure ? 1 : 0;