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"
37 #include "got_lib_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();
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);
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 fclose(outfile);
342 test_printf("\n");
343 if (i != nitems(expected_output) + 1) {
344 test_printf("number of lines expected: %d; actual: %d\n",
345 nitems(expected_output), i - 1);
346 return 0;
349 got_object_blob_close(blob1);
350 got_object_blob_close(blob2);
351 got_repo_close(repo);
352 return (err == NULL);
355 static int
356 repo_diff_tree(const char *repo_path)
358 const char *tree1_sha1 = "1efc41caf761a0a1f119d0c5121eedcb2e7a88c3";
359 const char *tree2_sha1 = "4aa8f2933839ff8a8fb3f905a4c232d22c6ff5f3";
360 const struct got_error *err;
361 struct got_repository *repo;
362 struct got_object_id *id1;
363 struct got_object_id *id2;
364 struct got_tree_object *tree1;
365 struct got_tree_object *tree2;
366 FILE *outfile;
368 err = got_repo_open(&repo, repo_path);
369 if (err != NULL || repo == NULL)
370 return 0;
372 err = got_object_resolve_id_str(&id1, repo, tree1_sha1);
373 if (err != NULL)
374 return 0;
375 err = got_object_resolve_id_str(&id2, repo, tree2_sha1);
376 if (err != NULL)
377 return 0;
379 err = got_object_open_as_tree(&tree1, repo, id1);
380 if (err != NULL)
381 return 0;
383 err = got_object_open_as_tree(&tree2, repo, id2);
384 if (err != NULL)
385 return 0;
387 if (!verbose) {
388 outfile = fopen("/dev/null", "w+");
389 if (outfile == NULL)
390 return 0;
391 } else
392 outfile = stdout;
393 test_printf("\n");
394 got_diff_tree(tree1, tree2, "", "", 3, repo, outfile);
395 test_printf("\n");
397 got_object_tree_close(tree1);
398 got_object_tree_close(tree2);
399 got_repo_close(repo);
400 return (err == NULL);
403 #define RUN_TEST(expr, name) \
404 { test_ok = (expr); \
405 printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
406 failure = (failure || !test_ok); }
409 void
410 usage(void)
412 fprintf(stderr, "usage: repository_test [-v] [REPO_PATH]\n");
415 static const struct got_error *
416 apply_unveil(const char *repo_path)
418 const struct got_error *error;
419 char *normpath = NULL;
421 if (repo_path) {
422 normpath = got_path_normalize(repo_path);
423 if (normpath == NULL)
424 return got_error_from_errno();
425 if (unveil(normpath, "r") != 0) {
426 free(normpath);
427 return got_error_from_errno();
429 free(normpath);
432 if (unveil("/tmp", "rwc") != 0)
433 return got_error_from_errno();
435 if (unveil("/dev/null", "rwc") != 0)
436 return got_error_from_errno();
438 error = got_privsep_unveil_exec_helpers();
439 if (error != NULL)
440 return error;
442 if (unveil(NULL, NULL) != 0)
443 return got_error_from_errno();
445 return NULL;
448 int
449 main(int argc, char *argv[])
451 int test_ok = 0, failure = 0;
452 const char *repo_path;
453 int ch;
454 const struct got_error *error;
456 #ifndef PROFILE
457 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil", NULL)
458 == -1)
459 err(1, "pledge");
460 #endif
462 while ((ch = getopt(argc, argv, "v")) != -1) {
463 switch (ch) {
464 case 'v':
465 verbose = 1;
466 break;
467 default:
468 usage();
469 return 1;
472 argc -= optind;
473 argv += optind;
475 if (argc == 0)
476 repo_path = GOT_REPO_PATH;
477 else if (argc == 1)
478 repo_path = argv[0];
479 else {
480 usage();
481 return 1;
484 error = apply_unveil(repo_path);
485 if (error) {
486 fprintf(stderr, "unveil: %s", error->msg);
487 return 1;
490 RUN_TEST(repo_read_tree(repo_path), "read_tree");
491 RUN_TEST(repo_read_log(repo_path), "read_log");
492 RUN_TEST(repo_read_blob(repo_path), "read_blob");
493 RUN_TEST(repo_diff_blob(repo_path), "diff_blob");
494 RUN_TEST(repo_diff_tree(repo_path), "diff_tree");
496 return failure ? 1 : 0;