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 <errno.h>
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <util.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <err.h>
28 #include <unistd.h>
30 #include "got_error.h"
31 #include "got_object.h"
32 #include "got_reference.h"
33 #include "got_repository.h"
34 #include "got_diff.h"
35 #include "got_opentemp.h"
36 #include "got_privsep.h"
37 #include "got_path.h"
40 #ifndef nitems
41 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
42 #endif
44 #define GOT_REPO_PATH "../../../"
46 static int verbose;
48 void
49 test_printf(char *fmt, ...)
50 {
51 va_list ap;
53 if (!verbose)
54 return;
56 va_start(ap, fmt);
57 vprintf(fmt, ap);
58 va_end(ap);
59 }
61 static const struct got_error *
62 print_commit_object(struct got_object_id *, struct got_repository *);
64 static const struct got_error *
65 print_parent_commits(struct got_commit_object *commit,
66 struct got_repository *repo)
67 {
68 const struct got_object_id_queue *parent_ids;
69 struct got_object_qid *qid;
70 const struct got_error *err = NULL;
72 parent_ids = got_object_commit_get_parent_ids(commit);
73 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
74 err = print_commit_object(qid->id, repo);
75 if (err)
76 break;
77 }
79 return err;
80 }
82 static const struct got_error *
83 print_tree_object(struct got_object_id *id, char *parent,
84 struct got_repository *repo)
85 {
86 struct got_tree_object *tree;
87 const struct got_tree_entries *entries;
88 struct got_tree_entry *te;
89 const struct got_error *err;
91 err = got_object_open_as_tree(&tree, repo, id);
92 if (err != NULL)
93 return err;
95 entries = got_object_tree_get_entries(tree);
96 SIMPLEQ_FOREACH(te, &entries->head, entry) {
97 char *next_parent;
98 char *hex;
100 err = got_object_id_str(&hex, te->id);
101 if (err)
102 break;
104 if (!S_ISDIR(te->mode)) {
105 test_printf("%s %s/%s\n", hex, parent, te->name);
106 free(hex);
107 continue;
109 test_printf("%s %s/%s\n", hex, parent, te->name);
110 free(hex);
112 if (asprintf(&next_parent, "%s/%s", parent, te->name) == -1) {
113 err = got_error_from_errno("asprintf");
114 break;
117 err = print_tree_object(te->id, next_parent, repo);
118 free(next_parent);
119 if (err)
120 break;
123 got_object_tree_close(tree);
124 return err;
127 static const struct got_error *
128 print_commit_object(struct got_object_id *id, struct got_repository *repo)
130 struct got_commit_object *commit;
131 const struct got_object_id_queue *parent_ids;
132 struct got_object_qid *qid;
133 char *buf;
134 const struct got_error *err;
135 int obj_type;
137 err = got_object_open_as_commit(&commit, repo, id);
138 if (err)
139 return err;
141 err = got_object_id_str(&buf, id);
142 if (err) {
143 got_object_commit_close(commit);
144 return err;
146 test_printf("tree: %s\n", buf);
147 free(buf);
148 test_printf("parent%s: ",
149 (got_object_commit_get_nparents(commit) == 1) ? "" : "s");
150 parent_ids = got_object_commit_get_parent_ids(commit);
151 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
152 err = got_object_id_str(&buf, qid->id);
153 if (err) {
154 got_object_commit_close(commit);
155 return err;
157 test_printf("%s\n", buf);
158 free(buf);
160 test_printf("author: %s\n", got_object_commit_get_author(commit));
161 test_printf("committer: %s\n", got_object_commit_get_committer(commit));
162 test_printf("log: %s\n", got_object_commit_get_logmsg(commit));
164 err = got_object_get_type(&obj_type, repo,
165 got_object_commit_get_tree_id(commit));
166 if (err != NULL) {
167 got_object_commit_close(commit);
168 return err;
170 if (obj_type == GOT_OBJ_TYPE_TREE)
171 test_printf("\n");
173 err = print_parent_commits(commit, repo);
174 got_object_commit_close(commit);
176 return err;
179 static int
180 repo_read_log(const char *repo_path)
182 const struct got_error *err;
183 struct got_repository *repo;
184 struct got_reference *head_ref;
185 struct got_object_id *id;
186 char *buf;
188 err = got_repo_open(&repo, repo_path);
189 if (err != NULL || repo == NULL)
190 return 0;
191 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
192 if (err != NULL || head_ref == NULL)
193 return 0;
194 err = got_ref_resolve(&id, repo, head_ref);
195 if (err != NULL || head_ref == NULL)
196 return 0;
197 err = got_object_id_str(&buf, id);
198 if (err != NULL)
199 return 0;
200 test_printf("HEAD is at %s\n", buf);
201 free(buf);
202 err = print_commit_object(id, repo);
203 if (err)
204 return 0;
205 free(id);
206 got_ref_close(head_ref);
207 got_repo_close(repo);
208 return 1;
211 static int
212 repo_read_tree(const char *repo_path)
214 const char *tree_sha1 = "6cc96e0e093fb30630ba7f199d0a008b24c6a690";
215 const struct got_error *err;
216 struct got_repository *repo;
217 struct got_object_id *id;
219 err = got_repo_open(&repo, repo_path);
220 if (err != NULL || repo == NULL)
221 return 0;
222 err = got_object_resolve_id_str(&id, repo, tree_sha1);
223 if (err != NULL)
224 return 0;
226 print_tree_object(id, "", repo);
227 test_printf("\n");
229 got_repo_close(repo);
230 return (err == NULL);
233 static int
234 repo_read_blob(const char *repo_path)
236 const char *blob_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
237 const struct got_error *err;
238 struct got_repository *repo;
239 struct got_object_id *id;
240 struct got_blob_object *blob;
241 int i;
242 size_t len;
244 err = got_repo_open(&repo, repo_path);
245 if (err != NULL || repo == NULL)
246 return 0;
247 err = got_object_resolve_id_str(&id, repo, blob_sha1);
248 if (err != NULL)
249 return 0;
250 err = got_object_open_as_blob(&blob, repo, id, 64);
251 if (err != NULL)
252 return 0;
254 test_printf("\n");
255 do {
256 const uint8_t *buf = got_object_blob_get_read_buf(blob);
257 err = got_object_blob_read_block(&len, blob);
258 if (err)
259 break;
260 for (i = 0; i < len; i++)
261 test_printf("%c", buf[i]);
262 } while (len != 0);
263 test_printf("\n");
265 got_object_blob_close(blob);
266 got_repo_close(repo);
267 return (err == NULL);
270 static int
271 repo_diff_blob(const char *repo_path)
273 const char *blob1_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
274 const char *blob2_sha1 = "de7eb21b21c7823a753261aadf7cba35c9580fbf";
275 const struct got_error *err;
276 struct got_repository *repo;
277 struct got_object_id *id1, *id2;
278 struct got_blob_object *blob1;
279 struct got_blob_object *blob2;
280 FILE *outfile;
281 int i;
282 char *line;
283 size_t len;
284 const char delim[3] = {'\0', '\0', '\0'};
285 const char *expected_output[] = {
286 "blob - 141f5fdc96126c1f4195558560a3c915e3d9b4c3",
287 "blob + de7eb21b21c7823a753261aadf7cba35c9580fbf",
288 "--- 141f5fdc96126c1f4195558560a3c915e3d9b4c3",
289 "+++ de7eb21b21c7823a753261aadf7cba35c9580fbf",
290 "@@ -1,10 +1,10 @@",
291 " .PATH:${.CURDIR}/../../lib",
292 " ",
293 " PROG = repository_test",
294 "-SRCS = path.c repository.c error.c refs.c repository_test.c",
295 "+SRCS = path.c repository.c error.c refs.c object.c sha1.c repository_test.c",
296 " ",
297 " CPPFLAGS = -I${.CURDIR}/../../include",
298 "-LDADD = -lutil",
299 "+LDADD = -lutil -lz",
300 " ",
301 " NOMAN = yes"
302 };
304 err = got_repo_open(&repo, repo_path);
305 if (err != NULL || repo == NULL)
306 return 0;
308 err = got_object_resolve_id_str(&id1, repo, blob1_sha1);
309 if (err != NULL)
310 return 0;
312 err = got_object_resolve_id_str(&id2, repo, blob2_sha1);
313 if (err != NULL)
314 return 0;
316 err = got_object_open_as_blob(&blob1, repo, id1, 512);
317 if (err != NULL)
318 return 0;
320 err = got_object_open_as_blob(&blob2, repo, id2, 512);
321 if (err != NULL)
322 return 0;
324 test_printf("\n");
325 outfile = got_opentemp();
326 if (outfile == NULL)
327 return 0;
328 got_diff_blob(blob1, blob2, NULL, NULL, 3, outfile);
329 rewind(outfile);
330 i = 0;
331 while ((line = fparseln(outfile, &len, NULL, delim, 0)) != NULL) {
332 test_printf(line);
333 test_printf("\n");
334 if (i < nitems(expected_output) &&
335 strcmp(line, expected_output[i]) != 0) {
336 test_printf("diff output mismatch; expected: '%s'\n",
337 expected_output[i]);
338 return 0;
340 i++;
342 if (fclose(outfile) != 0 && err == NULL)
343 err = got_error_from_errno("fclose");
344 test_printf("\n");
345 if (i != nitems(expected_output) + 1) {
346 test_printf("number of lines expected: %d; actual: %d\n",
347 nitems(expected_output), i - 1);
348 return 0;
351 got_object_blob_close(blob1);
352 got_object_blob_close(blob2);
353 got_repo_close(repo);
354 return (err == NULL);
357 static int
358 repo_diff_tree(const char *repo_path)
360 const char *tree1_sha1 = "1efc41caf761a0a1f119d0c5121eedcb2e7a88c3";
361 const char *tree2_sha1 = "4aa8f2933839ff8a8fb3f905a4c232d22c6ff5f3";
362 const struct got_error *err;
363 struct got_repository *repo;
364 struct got_object_id *id1;
365 struct got_object_id *id2;
366 struct got_tree_object *tree1;
367 struct got_tree_object *tree2;
368 FILE *outfile;
369 struct got_diff_blob_output_unidiff_arg arg;
371 err = got_repo_open(&repo, repo_path);
372 if (err != NULL || repo == NULL)
373 return 0;
375 err = got_object_resolve_id_str(&id1, repo, tree1_sha1);
376 if (err != NULL)
377 return 0;
378 err = got_object_resolve_id_str(&id2, repo, tree2_sha1);
379 if (err != NULL)
380 return 0;
382 err = got_object_open_as_tree(&tree1, repo, id1);
383 if (err != NULL)
384 return 0;
386 err = got_object_open_as_tree(&tree2, repo, id2);
387 if (err != NULL)
388 return 0;
390 if (!verbose) {
391 outfile = fopen("/dev/null", "w+");
392 if (outfile == NULL)
393 return 0;
394 } else
395 outfile = stdout;
396 test_printf("\n");
397 arg.diff_context = 3;
398 arg.outfile = outfile;
399 got_diff_tree(tree1, tree2, "", "", repo,
400 got_diff_blob_output_unidiff, &arg, 1);
401 test_printf("\n");
403 got_object_tree_close(tree1);
404 got_object_tree_close(tree2);
405 got_repo_close(repo);
406 return (err == NULL);
409 #define RUN_TEST(expr, name) \
410 { test_ok = (expr); \
411 printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
412 failure = (failure || !test_ok); }
415 void
416 usage(void)
418 fprintf(stderr, "usage: repository_test [-v] [REPO_PATH]\n");
421 static const struct got_error *
422 apply_unveil(const char *repo_path)
424 const struct got_error *error;
426 if (repo_path) {
427 if (unveil(repo_path, "r") != 0)
428 return got_error_from_errno2("unveil", repo_path);
431 if (unveil("/tmp", "rwc") != 0)
432 return got_error_from_errno2("unveil", "/tmp");
434 if (unveil("/dev/null", "rwc") != 0)
435 return got_error_from_errno2("unveil", "/dev/null");
437 error = got_privsep_unveil_exec_helpers();
438 if (error != NULL)
439 return error;
441 if (unveil(NULL, NULL) != 0)
442 return got_error_from_errno("unveil");
444 return NULL;
447 int
448 main(int argc, char *argv[])
450 int test_ok = 0, failure = 0;
451 char *repo_path;
452 int ch;
453 const struct got_error *error;
455 #ifndef PROFILE
456 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil", NULL)
457 == -1)
458 err(1, "pledge");
459 #endif
461 while ((ch = getopt(argc, argv, "v")) != -1) {
462 switch (ch) {
463 case 'v':
464 verbose = 1;
465 break;
466 default:
467 usage();
468 return 1;
471 argc -= optind;
472 argv += optind;
474 switch (argc) {
475 case 0:
476 repo_path = realpath(GOT_REPO_PATH, NULL);
477 break;
478 case 1:
479 repo_path = realpath(argv[0], NULL);
480 break;
481 default:
482 usage();
483 return 1;
485 if (repo_path == NULL) {
486 fprintf(stderr, "realpath: %s\n", strerror(errno));
487 return 1;
490 error = apply_unveil(repo_path);
491 if (error) {
492 fprintf(stderr, "unveil: %s\n", error->msg);
493 free(repo_path);
494 return 1;
497 RUN_TEST(repo_read_tree(repo_path), "read_tree");
498 RUN_TEST(repo_read_log(repo_path), "read_log");
499 RUN_TEST(repo_read_blob(repo_path), "read_blob");
500 RUN_TEST(repo_diff_blob(repo_path), "diff_blob");
501 RUN_TEST(repo_diff_tree(repo_path), "diff_tree");
503 free(repo_path);
504 return failure ? 1 : 0;