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>
29 #include "got_error.h"
30 #include "got_object.h"
31 #include "got_reference.h"
32 #include "got_repository.h"
34 #include "got_opentemp.h"
35 #include "got_privsep.h"
37 #include "got_lib_path.h"
40 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
43 #define GOT_REPO_PATH "../../../"
48 test_printf(char *fmt, ...)
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)
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);
81 static const struct got_error *
82 print_tree_object(struct got_object_id *id, char *parent,
83 struct got_repository *repo)
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);
94 entries = got_object_tree_get_entries(tree);
95 SIMPLEQ_FOREACH(te, &entries->head, entry) {
99 err = got_object_id_str(&hex, te->id);
103 if (!S_ISDIR(te->mode)) {
104 test_printf("%s %s/%s\n", hex, parent, te->name);
108 test_printf("%s %s/%s\n", hex, parent, te->name);
111 if (asprintf(&next_parent, "%s/%s", parent, te->name) == -1) {
112 err = got_error_from_errno();
116 err = print_tree_object(te->id, next_parent, repo);
122 got_object_tree_close(tree);
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;
133 const struct got_error *err;
136 err = got_object_open_as_commit(&commit, repo, id);
140 err = got_object_id_str(&buf, id);
142 got_object_commit_close(commit);
145 test_printf("tree: %s\n", 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);
153 got_object_commit_close(commit);
156 test_printf("%s\n", 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));
166 got_object_commit_close(commit);
169 if (obj_type == GOT_OBJ_TYPE_TREE)
172 err = print_parent_commits(commit, repo);
173 got_object_commit_close(commit);
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;
187 err = got_repo_open(&repo, repo_path);
188 if (err != NULL || repo == NULL)
190 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
191 if (err != NULL || head_ref == NULL)
193 err = got_ref_resolve(&id, repo, head_ref);
194 if (err != NULL || head_ref == NULL)
196 err = got_object_id_str(&buf, id);
199 test_printf("HEAD is at %s\n", buf);
201 err = print_commit_object(id, repo);
205 got_ref_close(head_ref);
206 got_repo_close(repo);
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)
221 err = got_object_resolve_id_str(&id, repo, tree_sha1);
225 print_tree_object(id, "", repo);
228 got_repo_close(repo);
229 return (err == NULL);
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;
243 err = got_repo_open(&repo, repo_path);
244 if (err != NULL || repo == NULL)
246 err = got_object_resolve_id_str(&id, repo, blob_sha1);
249 err = got_object_open_as_blob(&blob, repo, id, 64);
255 const uint8_t *buf = got_object_blob_get_read_buf(blob);
256 err = got_object_blob_read_block(&len, blob);
259 for (i = 0; i < len; i++)
260 test_printf("%c", buf[i]);
264 got_object_blob_close(blob);
265 got_repo_close(repo);
266 return (err == NULL);
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;
283 const char delim[3] = {'\0', '\0', '\0'};
284 const char *expected_output[] = {
285 "blob - 141f5fdc96126c1f4195558560a3c915e3d9b4c3",
286 "blob + de7eb21b21c7823a753261aadf7cba35c9580fbf",
287 "--- 141f5fdc96126c1f4195558560a3c915e3d9b4c3",
288 "+++ de7eb21b21c7823a753261aadf7cba35c9580fbf",
290 " .PATH:${.CURDIR}/../../lib",
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",
296 " CPPFLAGS = -I${.CURDIR}/../../include",
298 "+LDADD = -lutil -lz",
303 err = got_repo_open(&repo, repo_path);
304 if (err != NULL || repo == NULL)
307 err = got_object_resolve_id_str(&id1, repo, blob1_sha1);
311 err = got_object_resolve_id_str(&id2, repo, blob2_sha1);
315 err = got_object_open_as_blob(&blob1, repo, id1, 512);
319 err = got_object_open_as_blob(&blob2, repo, id2, 512);
324 outfile = got_opentemp();
327 got_diff_blob(blob1, blob2, NULL, NULL, 3, outfile);
330 while ((line = fparseln(outfile, &len, NULL, delim, 0)) != NULL) {
333 if (i < nitems(expected_output) &&
334 strcmp(line, expected_output[i]) != 0) {
335 test_printf("diff output mismatch; expected: '%s'\n",
341 if (fclose(outfile) != 0 && err == NULL)
342 err = got_error_from_errno();
344 if (i != nitems(expected_output) + 1) {
345 test_printf("number of lines expected: %d; actual: %d\n",
346 nitems(expected_output), i - 1);
350 got_object_blob_close(blob1);
351 got_object_blob_close(blob2);
352 got_repo_close(repo);
353 return (err == NULL);
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;
369 err = got_repo_open(&repo, repo_path);
370 if (err != NULL || repo == NULL)
373 err = got_object_resolve_id_str(&id1, repo, tree1_sha1);
376 err = got_object_resolve_id_str(&id2, repo, tree2_sha1);
380 err = got_object_open_as_tree(&tree1, repo, id1);
384 err = got_object_open_as_tree(&tree2, repo, id2);
389 outfile = fopen("/dev/null", "w+");
395 got_diff_tree(tree1, tree2, "", "", 3, repo, outfile);
398 got_object_tree_close(tree1);
399 got_object_tree_close(tree2);
400 got_repo_close(repo);
401 return (err == NULL);
404 #define RUN_TEST(expr, name) \
405 { test_ok = (expr); \
406 printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
407 failure = (failure || !test_ok); }
413 fprintf(stderr, "usage: repository_test [-v] [REPO_PATH]\n");
416 static const struct got_error *
417 apply_unveil(const char *repo_path)
419 const struct got_error *error;
420 char *normpath = NULL;
423 normpath = got_path_normalize(repo_path);
424 if (normpath == NULL)
425 return got_error_from_errno();
426 if (unveil(normpath, "r") != 0) {
428 return got_error_from_errno();
433 if (unveil("/tmp", "rwc") != 0)
434 return got_error_from_errno();
436 if (unveil("/dev/null", "rwc") != 0)
437 return got_error_from_errno();
439 error = got_privsep_unveil_exec_helpers();
443 if (unveil(NULL, NULL) != 0)
444 return got_error_from_errno();
450 main(int argc, char *argv[])
452 int test_ok = 0, failure = 0;
453 const char *repo_path;
455 const struct got_error *error;
458 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil", NULL)
463 while ((ch = getopt(argc, argv, "v")) != -1) {
477 repo_path = GOT_REPO_PATH;
485 error = apply_unveil(repo_path);
487 fprintf(stderr, "unveil: %s", error->msg);
491 RUN_TEST(repo_read_tree(repo_path), "read_tree");
492 RUN_TEST(repo_read_log(repo_path), "read_log");
493 RUN_TEST(repo_read_blob(repo_path), "read_blob");
494 RUN_TEST(repo_diff_blob(repo_path), "diff_blob");
495 RUN_TEST(repo_diff_tree(repo_path), "diff_tree");
497 return failure ? 1 : 0;