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"
35 #include "got_privsep.h"
36 #include "got_path.h"
39 #ifndef nitems
40 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
41 #endif
43 #define GOT_REPO_PATH "../../../"
45 static int verbose;
47 void
48 test_printf(char *fmt, ...)
49 {
50 va_list ap;
52 if (!verbose)
53 return;
55 va_start(ap, fmt);
56 vprintf(fmt, ap);
57 va_end(ap);
58 }
60 static const struct got_error *
61 print_commit_object(struct got_object_id *, struct got_repository *);
63 static const struct got_error *
64 print_parent_commits(struct got_commit_object *commit,
65 struct got_repository *repo)
66 {
67 const struct got_object_id_queue *parent_ids;
68 struct got_object_qid *qid;
69 const struct got_error *err = NULL;
71 parent_ids = got_object_commit_get_parent_ids(commit);
72 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
73 err = print_commit_object(qid->id, repo);
74 if (err)
75 break;
76 }
78 return err;
79 }
81 static const struct got_error *
82 print_tree_object(struct got_object_id *id, char *parent,
83 struct got_repository *repo)
84 {
85 struct got_tree_object *tree;
86 const struct got_tree_entries *entries;
87 struct got_tree_entry *te;
88 const struct got_error *err;
90 err = got_object_open_as_tree(&tree, repo, id);
91 if (err != NULL)
92 return err;
94 entries = got_object_tree_get_entries(tree);
95 SIMPLEQ_FOREACH(te, &entries->head, entry) {
96 char *next_parent;
97 char *hex;
99 err = got_object_id_str(&hex, te->id);
100 if (err)
101 break;
103 if (!S_ISDIR(te->mode)) {
104 test_printf("%s %s/%s\n", hex, parent, te->name);
105 free(hex);
106 continue;
108 test_printf("%s %s/%s\n", hex, parent, te->name);
109 free(hex);
111 if (asprintf(&next_parent, "%s/%s", parent, te->name) == -1) {
112 err = got_error_from_errno("asprintf");
113 break;
116 err = print_tree_object(te->id, next_parent, repo);
117 free(next_parent);
118 if (err)
119 break;
122 got_object_tree_close(tree);
123 return err;
126 static const struct got_error *
127 print_commit_object(struct got_object_id *id, struct got_repository *repo)
129 struct got_commit_object *commit;
130 const struct got_object_id_queue *parent_ids;
131 struct got_object_qid *qid;
132 char *buf;
133 const struct got_error *err;
134 int obj_type;
136 err = got_object_open_as_commit(&commit, repo, id);
137 if (err)
138 return err;
140 err = got_object_id_str(&buf, id);
141 if (err) {
142 got_object_commit_close(commit);
143 return err;
145 test_printf("tree: %s\n", buf);
146 free(buf);
147 test_printf("parent%s: ",
148 (got_object_commit_get_nparents(commit) == 1) ? "" : "s");
149 parent_ids = got_object_commit_get_parent_ids(commit);
150 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
151 err = got_object_id_str(&buf, qid->id);
152 if (err) {
153 got_object_commit_close(commit);
154 return err;
156 test_printf("%s\n", buf);
157 free(buf);
159 test_printf("author: %s\n", got_object_commit_get_author(commit));
160 test_printf("committer: %s\n", got_object_commit_get_committer(commit));
161 test_printf("log: %s\n", got_object_commit_get_logmsg(commit));
163 err = got_object_get_type(&obj_type, repo,
164 got_object_commit_get_tree_id(commit));
165 if (err != NULL) {
166 got_object_commit_close(commit);
167 return err;
169 if (obj_type == GOT_OBJ_TYPE_TREE)
170 test_printf("\n");
172 err = print_parent_commits(commit, repo);
173 got_object_commit_close(commit);
175 return err;
178 static int
179 repo_read_log(const char *repo_path)
181 const struct got_error *err;
182 struct got_repository *repo;
183 struct got_reference *head_ref;
184 struct got_object_id *id;
185 char *buf;
187 err = got_repo_open(&repo, repo_path);
188 if (err != NULL || repo == NULL)
189 return 0;
190 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
191 if (err != NULL || head_ref == NULL)
192 return 0;
193 err = got_ref_resolve(&id, repo, head_ref);
194 if (err != NULL || head_ref == NULL)
195 return 0;
196 err = got_object_id_str(&buf, id);
197 if (err != NULL)
198 return 0;
199 test_printf("HEAD is at %s\n", buf);
200 free(buf);
201 err = print_commit_object(id, repo);
202 if (err)
203 return 0;
204 free(id);
205 got_ref_close(head_ref);
206 got_repo_close(repo);
207 return 1;
210 static int
211 repo_read_tree(const char *repo_path)
213 const char *tree_sha1 = "6cc96e0e093fb30630ba7f199d0a008b24c6a690";
214 const struct got_error *err;
215 struct got_repository *repo;
216 struct got_object_id *id;
218 err = got_repo_open(&repo, repo_path);
219 if (err != NULL || repo == NULL)
220 return 0;
221 err = got_object_resolve_id_str(&id, repo, tree_sha1);
222 if (err != NULL)
223 return 0;
225 print_tree_object(id, "", repo);
226 test_printf("\n");
228 got_repo_close(repo);
229 return (err == NULL);
232 static int
233 repo_read_blob(const char *repo_path)
235 const char *blob_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
236 const struct got_error *err;
237 struct got_repository *repo;
238 struct got_object_id *id;
239 struct got_blob_object *blob;
240 int i;
241 size_t len;
243 err = got_repo_open(&repo, repo_path);
244 if (err != NULL || repo == NULL)
245 return 0;
246 err = got_object_resolve_id_str(&id, repo, blob_sha1);
247 if (err != NULL)
248 return 0;
249 err = got_object_open_as_blob(&blob, repo, id, 64);
250 if (err != NULL)
251 return 0;
253 test_printf("\n");
254 do {
255 const uint8_t *buf = got_object_blob_get_read_buf(blob);
256 err = got_object_blob_read_block(&len, blob);
257 if (err)
258 break;
259 for (i = 0; i < len; i++)
260 test_printf("%c", buf[i]);
261 } while (len != 0);
262 test_printf("\n");
264 got_object_blob_close(blob);
265 got_repo_close(repo);
266 return (err == NULL);
269 static int
270 repo_diff_blob(const char *repo_path)
272 const char *blob1_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
273 const char *blob2_sha1 = "de7eb21b21c7823a753261aadf7cba35c9580fbf";
274 const struct got_error *err;
275 struct got_repository *repo;
276 struct got_object_id *id1, *id2;
277 struct got_blob_object *blob1;
278 struct got_blob_object *blob2;
279 FILE *outfile;
280 int i;
281 char *line;
282 size_t len;
283 const char delim[3] = {'\0', '\0', '\0'};
284 const char *expected_output[] = {
285 "blob - 141f5fdc96126c1f4195558560a3c915e3d9b4c3",
286 "blob + de7eb21b21c7823a753261aadf7cba35c9580fbf",
287 "--- 141f5fdc96126c1f4195558560a3c915e3d9b4c3",
288 "+++ de7eb21b21c7823a753261aadf7cba35c9580fbf",
289 "@@ -1,10 +1,10 @@",
290 " .PATH:${.CURDIR}/../../lib",
291 " ",
292 " PROG = repository_test",
293 "-SRCS = path.c repository.c error.c refs.c repository_test.c",
294 "+SRCS = path.c repository.c error.c refs.c object.c sha1.c repository_test.c",
295 " ",
296 " CPPFLAGS = -I${.CURDIR}/../../include",
297 "-LDADD = -lutil",
298 "+LDADD = -lutil -lz",
299 " ",
300 " NOMAN = yes"
301 };
303 err = got_repo_open(&repo, repo_path);
304 if (err != NULL || repo == NULL)
305 return 0;
307 err = got_object_resolve_id_str(&id1, repo, blob1_sha1);
308 if (err != NULL)
309 return 0;
311 err = got_object_resolve_id_str(&id2, repo, blob2_sha1);
312 if (err != NULL)
313 return 0;
315 err = got_object_open_as_blob(&blob1, repo, id1, 512);
316 if (err != NULL)
317 return 0;
319 err = got_object_open_as_blob(&blob2, repo, id2, 512);
320 if (err != NULL)
321 return 0;
323 test_printf("\n");
324 outfile = got_opentemp();
325 if (outfile == NULL)
326 return 0;
327 got_diff_blob(blob1, blob2, NULL, NULL, 3, outfile);
328 rewind(outfile);
329 i = 0;
330 while ((line = fparseln(outfile, &len, NULL, delim, 0)) != NULL) {
331 test_printf(line);
332 test_printf("\n");
333 if (i < nitems(expected_output) &&
334 strcmp(line, expected_output[i]) != 0) {
335 test_printf("diff output mismatch; expected: '%s'\n",
336 expected_output[i]);
337 return 0;
339 i++;
341 if (fclose(outfile) != 0 && err == NULL)
342 err = got_error_from_errno("fclose");
343 test_printf("\n");
344 if (i != nitems(expected_output) + 1) {
345 test_printf("number of lines expected: %d; actual: %d\n",
346 nitems(expected_output), i - 1);
347 return 0;
350 got_object_blob_close(blob1);
351 got_object_blob_close(blob2);
352 got_repo_close(repo);
353 return (err == NULL);
356 static int
357 repo_diff_tree(const char *repo_path)
359 const char *tree1_sha1 = "1efc41caf761a0a1f119d0c5121eedcb2e7a88c3";
360 const char *tree2_sha1 = "4aa8f2933839ff8a8fb3f905a4c232d22c6ff5f3";
361 const struct got_error *err;
362 struct got_repository *repo;
363 struct got_object_id *id1;
364 struct got_object_id *id2;
365 struct got_tree_object *tree1;
366 struct got_tree_object *tree2;
367 FILE *outfile;
368 struct got_diff_blob_output_unidiff_arg arg;
370 err = got_repo_open(&repo, repo_path);
371 if (err != NULL || repo == NULL)
372 return 0;
374 err = got_object_resolve_id_str(&id1, repo, tree1_sha1);
375 if (err != NULL)
376 return 0;
377 err = got_object_resolve_id_str(&id2, repo, tree2_sha1);
378 if (err != NULL)
379 return 0;
381 err = got_object_open_as_tree(&tree1, repo, id1);
382 if (err != NULL)
383 return 0;
385 err = got_object_open_as_tree(&tree2, repo, id2);
386 if (err != NULL)
387 return 0;
389 if (!verbose) {
390 outfile = fopen("/dev/null", "w+");
391 if (outfile == NULL)
392 return 0;
393 } else
394 outfile = stdout;
395 test_printf("\n");
396 arg.diff_context = 3;
397 arg.outfile = outfile;
398 got_diff_tree(tree1, tree2, "", "", repo,
399 got_diff_blob_output_unidiff, &arg);
400 test_printf("\n");
402 got_object_tree_close(tree1);
403 got_object_tree_close(tree2);
404 got_repo_close(repo);
405 return (err == NULL);
408 #define RUN_TEST(expr, name) \
409 { test_ok = (expr); \
410 printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
411 failure = (failure || !test_ok); }
414 void
415 usage(void)
417 fprintf(stderr, "usage: repository_test [-v] [REPO_PATH]\n");
420 static const struct got_error *
421 apply_unveil(const char *repo_path)
423 const struct got_error *error;
424 char *normpath = NULL;
426 if (repo_path) {
427 normpath = got_path_normalize(repo_path);
428 if (normpath == NULL)
429 return got_error_from_errno("got_path_normalize");
430 if (unveil(normpath, "r") != 0) {
431 free(normpath);
432 return got_error_from_errno2("unveil", normpath);
434 free(normpath);
437 if (unveil("/tmp", "rwc") != 0)
438 return got_error_from_errno2("unveil", "/tmp");
440 if (unveil("/dev/null", "rwc") != 0)
441 return got_error_from_errno2("unveil", "/dev/null");
443 error = got_privsep_unveil_exec_helpers();
444 if (error != NULL)
445 return error;
447 if (unveil(NULL, NULL) != 0)
448 return got_error_from_errno("unveil");
450 return NULL;
453 int
454 main(int argc, char *argv[])
456 int test_ok = 0, failure = 0;
457 const char *repo_path;
458 int ch;
459 const struct got_error *error;
461 #ifndef PROFILE
462 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil", NULL)
463 == -1)
464 err(1, "pledge");
465 #endif
467 while ((ch = getopt(argc, argv, "v")) != -1) {
468 switch (ch) {
469 case 'v':
470 verbose = 1;
471 break;
472 default:
473 usage();
474 return 1;
477 argc -= optind;
478 argv += optind;
480 if (argc == 0)
481 repo_path = GOT_REPO_PATH;
482 else if (argc == 1)
483 repo_path = argv[0];
484 else {
485 usage();
486 return 1;
489 error = apply_unveil(repo_path);
490 if (error) {
491 fprintf(stderr, "unveil: %s", error->msg);
492 return 1;
495 RUN_TEST(repo_read_tree(repo_path), "read_tree");
496 RUN_TEST(repo_read_log(repo_path), "read_log");
497 RUN_TEST(repo_read_blob(repo_path), "read_blob");
498 RUN_TEST(repo_diff_blob(repo_path), "diff_blob");
499 RUN_TEST(repo_diff_tree(repo_path), "diff_tree");
501 return failure ? 1 : 0;