2 * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
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.
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.
18 #include <sys/queue.h>
30 #include "got_error.h"
31 #include "got_object.h"
32 #include "got_reference.h"
33 #include "got_repository.h"
35 #include "got_opentemp.h"
36 #include "got_privsep.h"
41 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
44 #define GOT_REPO_PATH "../../../"
49 test_printf(char *fmt, ...)
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)
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);
82 static const struct got_error *
83 print_tree_object(struct got_object_id *id, char *parent,
84 struct got_repository *repo)
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);
95 entries = got_object_tree_get_entries(tree);
96 SIMPLEQ_FOREACH(te, &entries->head, entry) {
100 err = got_object_id_str(&hex, te->id);
104 if (!S_ISDIR(te->mode)) {
105 test_printf("%s %s/%s\n", hex, parent, te->name);
109 test_printf("%s %s/%s\n", hex, parent, te->name);
112 if (asprintf(&next_parent, "%s/%s", parent, te->name) == -1) {
113 err = got_error_from_errno("asprintf");
117 err = print_tree_object(te->id, next_parent, repo);
123 got_object_tree_close(tree);
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;
134 const struct got_error *err;
137 err = got_object_open_as_commit(&commit, repo, id);
141 err = got_object_id_str(&buf, id);
143 got_object_commit_close(commit);
146 test_printf("tree: %s\n", 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);
154 got_object_commit_close(commit);
157 test_printf("%s\n", 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));
167 got_object_commit_close(commit);
170 if (obj_type == GOT_OBJ_TYPE_TREE)
173 err = print_parent_commits(commit, repo);
174 got_object_commit_close(commit);
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;
188 err = got_repo_open(&repo, repo_path);
189 if (err != NULL || repo == NULL)
191 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
192 if (err != NULL || head_ref == NULL)
194 err = got_ref_resolve(&id, repo, head_ref);
195 if (err != NULL || head_ref == NULL)
197 err = got_object_id_str(&buf, id);
200 test_printf("HEAD is at %s\n", buf);
202 err = print_commit_object(id, repo);
206 got_ref_close(head_ref);
207 got_repo_close(repo);
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)
222 err = got_object_resolve_id_str(&id, repo, tree_sha1);
226 print_tree_object(id, "", repo);
229 got_repo_close(repo);
230 return (err == NULL);
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;
244 err = got_repo_open(&repo, repo_path);
245 if (err != NULL || repo == NULL)
247 err = got_object_resolve_id_str(&id, repo, blob_sha1);
250 err = got_object_open_as_blob(&blob, repo, id, 64);
256 const uint8_t *buf = got_object_blob_get_read_buf(blob);
257 err = got_object_blob_read_block(&len, blob);
260 for (i = 0; i < len; i++)
261 test_printf("%c", buf[i]);
265 got_object_blob_close(blob);
266 got_repo_close(repo);
267 return (err == NULL);
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;
284 const char delim[3] = {'\0', '\0', '\0'};
285 const char *expected_output[] = {
286 "blob - 141f5fdc96126c1f4195558560a3c915e3d9b4c3",
287 "blob + de7eb21b21c7823a753261aadf7cba35c9580fbf",
288 "--- 141f5fdc96126c1f4195558560a3c915e3d9b4c3",
289 "+++ de7eb21b21c7823a753261aadf7cba35c9580fbf",
291 " .PATH:${.CURDIR}/../../lib",
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",
297 " CPPFLAGS = -I${.CURDIR}/../../include",
299 "+LDADD = -lutil -lz",
304 err = got_repo_open(&repo, repo_path);
305 if (err != NULL || repo == NULL)
308 err = got_object_resolve_id_str(&id1, repo, blob1_sha1);
312 err = got_object_resolve_id_str(&id2, repo, blob2_sha1);
316 err = got_object_open_as_blob(&blob1, repo, id1, 512);
320 err = got_object_open_as_blob(&blob2, repo, id2, 512);
325 outfile = got_opentemp();
328 got_diff_blob(blob1, blob2, NULL, NULL, 3, outfile);
331 while ((line = fparseln(outfile, &len, NULL, delim, 0)) != NULL) {
334 if (i < nitems(expected_output) &&
335 strcmp(line, expected_output[i]) != 0) {
336 test_printf("diff output mismatch; expected: '%s'\n",
342 if (fclose(outfile) != 0 && err == NULL)
343 err = got_error_from_errno("fclose");
345 if (i != nitems(expected_output) + 1) {
346 test_printf("number of lines expected: %d; actual: %d\n",
347 nitems(expected_output), i - 1);
351 got_object_blob_close(blob1);
352 got_object_blob_close(blob2);
353 got_repo_close(repo);
354 return (err == NULL);
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;
369 struct got_diff_blob_output_unidiff_arg arg;
371 err = got_repo_open(&repo, repo_path);
372 if (err != NULL || repo == NULL)
375 err = got_object_resolve_id_str(&id1, repo, tree1_sha1);
378 err = got_object_resolve_id_str(&id2, repo, tree2_sha1);
382 err = got_object_open_as_tree(&tree1, repo, id1);
386 err = got_object_open_as_tree(&tree2, repo, id2);
391 outfile = fopen("/dev/null", "w+");
397 arg.diff_context = 3;
398 arg.outfile = outfile;
399 got_diff_tree(tree1, tree2, "", "", repo,
400 got_diff_blob_output_unidiff, &arg);
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); }
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;
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();
441 if (unveil(NULL, NULL) != 0)
442 return got_error_from_errno("unveil");
448 main(int argc, char *argv[])
450 int test_ok = 0, failure = 0;
453 const struct got_error *error;
456 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil", NULL)
461 while ((ch = getopt(argc, argv, "v")) != -1) {
476 repo_path = realpath(GOT_REPO_PATH, NULL);
479 repo_path = realpath(argv[0], NULL);
485 if (repo_path == NULL) {
486 fprintf(stderr, "realpath: %s\n", strerror(errno));
490 error = apply_unveil(repo_path);
492 fprintf(stderr, "unveil: %s\n", error->msg);
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");
504 return failure ? 1 : 0;