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"
36 #include "got_lib_path.h"
39 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
42 #define GOT_REPO_PATH "../../../"
47 test_printf(char *fmt, ...)
59 static const struct got_error *
60 print_commit_object(struct got_object *, struct got_repository *);
62 static const struct got_error *
63 print_parent_commits(struct got_commit_object *commit,
64 struct got_repository *repo)
66 const struct got_object_id_queue *parent_ids;
67 struct got_object_qid *qid;
68 const struct got_error *err = NULL;
69 struct got_object *obj;
71 parent_ids = got_object_commit_get_parent_ids(commit);
72 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
73 err = got_object_open(&obj, repo, qid->id);
76 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT)
77 err = got_error(GOT_ERR_OBJ_TYPE);
79 err = print_commit_object(obj, repo);
80 got_object_close(obj);
88 static const struct got_error *
89 print_tree_object(struct got_object *obj, char *parent,
90 struct got_repository *repo)
92 struct got_tree_object *tree;
93 const struct got_tree_entries *entries;
94 struct got_tree_entry *te;
95 const struct got_error *err;
97 err = got_object_tree_open(&tree, repo, obj);
101 entries = got_object_tree_get_entries(tree);
102 SIMPLEQ_FOREACH(te, &entries->head, entry) {
103 struct got_object *treeobj;
107 err = got_object_id_str(&hex, te->id);
111 if (!S_ISDIR(te->mode)) {
112 test_printf("%s %s/%s\n", hex, parent, te->name);
116 test_printf("%s %s/%s\n", hex, parent, te->name);
119 err = got_object_open(&treeobj, repo, te->id);
123 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
124 err = got_error(GOT_ERR_OBJ_TYPE);
125 got_object_close(treeobj);
129 if (asprintf(&next_parent, "%s/%s", parent, te->name) == -1) {
130 err = got_error_from_errno();
131 got_object_close(treeobj);
135 err = print_tree_object(treeobj, next_parent, repo);
138 got_object_close(treeobj);
142 got_object_close(treeobj);
145 got_object_tree_close(tree);
149 static const struct got_error *
150 print_commit_object(struct got_object *obj, struct got_repository *repo)
152 struct got_commit_object *commit;
153 const struct got_object_id_queue *parent_ids;
154 struct got_object_qid *qid;
156 const struct got_error *err;
157 struct got_object* treeobj;
159 err = got_object_commit_open(&commit, repo, obj);
163 err = got_object_id_str(&buf, got_object_commit_get_tree_id(commit));
166 test_printf("tree: %s\n", buf);
168 test_printf("parent%s: ",
169 (got_object_commit_get_nparents(commit) == 1) ? "" : "s");
170 parent_ids = got_object_commit_get_parent_ids(commit);
171 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
172 err = got_object_id_str(&buf, qid->id);
175 test_printf("%s\n", buf);
178 test_printf("author: %s\n", got_object_commit_get_author(commit));
179 test_printf("committer: %s\n", got_object_commit_get_committer(commit));
180 test_printf("log: %s\n", got_object_commit_get_logmsg(commit));
182 err = got_object_open(&treeobj, repo,
183 got_object_commit_get_tree_id(commit));
186 if (got_object_get_type(treeobj) == GOT_OBJ_TYPE_TREE) {
187 print_tree_object(treeobj, "", repo);
190 got_object_close(treeobj);
192 err = print_parent_commits(commit, repo);
193 got_object_commit_close(commit);
199 repo_read_log(const char *repo_path)
201 const struct got_error *err;
202 struct got_repository *repo;
203 struct got_reference *head_ref;
204 struct got_object_id *id;
205 struct got_object *obj;
208 err = got_repo_open(&repo, repo_path);
209 if (err != NULL || repo == NULL)
211 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
212 if (err != NULL || head_ref == NULL)
214 err = got_ref_resolve(&id, repo, head_ref);
215 if (err != NULL || head_ref == NULL)
217 err = got_object_id_str(&buf, id);
220 test_printf("HEAD is at %s\n", buf);
222 err = got_object_open(&obj, repo, id);
223 if (err != NULL || obj == NULL)
225 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) {
226 err = print_commit_object(obj, repo);
231 got_object_close(obj);
233 got_ref_close(head_ref);
234 got_repo_close(repo);
239 repo_read_tree(const char *repo_path)
241 const char *tree_sha1 = "6cc96e0e093fb30630ba7f199d0a008b24c6a690";
242 const struct got_error *err;
243 struct got_repository *repo;
244 struct got_object *obj;
246 err = got_repo_open(&repo, repo_path);
247 if (err != NULL || repo == NULL)
249 err = got_object_open_by_id_str(&obj, repo, tree_sha1);
250 if (err != NULL || obj == NULL)
252 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE)
255 print_tree_object(obj, "", repo);
258 got_object_close(obj);
259 got_repo_close(repo);
260 return (err == NULL);
264 repo_read_blob(const char *repo_path)
266 const char *blob_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
267 const struct got_error *err;
268 struct got_repository *repo;
269 struct got_object *obj;
270 struct got_blob_object *blob;
274 err = got_repo_open(&repo, repo_path);
275 if (err != NULL || repo == NULL)
277 err = got_object_open_by_id_str(&obj, repo, blob_sha1);
278 if (err != NULL || obj == NULL)
280 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB)
283 err = got_object_blob_open(&blob, repo, obj, 64);
289 const uint8_t *buf = got_object_blob_get_read_buf(blob);
290 err = got_object_blob_read_block(&len, blob);
293 for (i = 0; i < len; i++)
294 test_printf("%c", buf[i]);
298 got_object_blob_close(blob);
299 got_object_close(obj);
300 got_repo_close(repo);
301 return (err == NULL);
305 repo_diff_blob(const char *repo_path)
307 const char *blob1_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
308 const char *blob2_sha1 = "de7eb21b21c7823a753261aadf7cba35c9580fbf";
309 const struct got_error *err;
310 struct got_repository *repo;
311 struct got_object *obj1;
312 struct got_object *obj2;
313 struct got_blob_object *blob1;
314 struct got_blob_object *blob2;
319 const char delim[3] = {'\0', '\0', '\0'};
320 const char *expected_output[] = {
321 "--- 141f5fdc96126c1f4195558560a3c915e3d9b4c3",
322 "+++ de7eb21b21c7823a753261aadf7cba35c9580fbf",
324 " .PATH:${.CURDIR}/../../lib",
326 " PROG = repository_test",
327 "-SRCS = path.c repository.c error.c refs.c repository_test.c",
328 "+SRCS = path.c repository.c error.c refs.c object.c sha1.c repository_test.c",
330 " CPPFLAGS = -I${.CURDIR}/../../include",
332 "+LDADD = -lutil -lz",
337 err = got_repo_open(&repo, repo_path);
338 if (err != NULL || repo == NULL)
341 err = got_object_open_by_id_str(&obj1, repo, blob1_sha1);
342 if (err != NULL || obj1 == NULL)
344 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB)
346 err = got_object_open_by_id_str(&obj2, repo, blob2_sha1);
347 if (err != NULL || obj2 == NULL)
349 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB)
352 err = got_object_blob_open(&blob1, repo, obj1, 512);
356 err = got_object_blob_open(&blob2, repo, obj2, 512);
361 outfile = got_opentemp();
364 got_diff_blob(blob1, blob2, NULL, NULL, 3, outfile);
367 while ((line = fparseln(outfile, &len, NULL, delim, 0)) != NULL) {
370 if (i < nitems(expected_output) &&
371 strcmp(line, expected_output[i]) != 0) {
372 test_printf("diff output mismatch; expected: '%s'\n",
380 if (i != nitems(expected_output) + 1) {
381 test_printf("number of lines expected: %d; actual: %d\n",
382 nitems(expected_output), i - 1);
386 got_object_blob_close(blob1);
387 got_object_blob_close(blob2);
388 got_object_close(obj1);
389 got_object_close(obj2);
390 got_repo_close(repo);
391 return (err == NULL);
395 repo_diff_tree(const char *repo_path)
397 const char *tree1_sha1 = "1efc41caf761a0a1f119d0c5121eedcb2e7a88c3";
398 const char *tree2_sha1 = "4aa8f2933839ff8a8fb3f905a4c232d22c6ff5f3";
399 const struct got_error *err;
400 struct got_repository *repo;
401 struct got_object *obj1;
402 struct got_object *obj2;
403 struct got_tree_object *tree1;
404 struct got_tree_object *tree2;
407 err = got_repo_open(&repo, repo_path);
408 if (err != NULL || repo == NULL)
411 err = got_object_open_by_id_str(&obj1, repo, tree1_sha1);
412 if (err != NULL || obj1 == NULL)
414 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_TREE)
416 err = got_object_open_by_id_str(&obj2, repo, tree2_sha1);
417 if (err != NULL || obj2 == NULL)
419 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_TREE)
422 err = got_object_tree_open(&tree1, repo, obj1);
426 err = got_object_tree_open(&tree2, repo, obj2);
431 outfile = fopen("/dev/null", "w+");
437 got_diff_tree(tree1, tree2, "", "", 3, repo, outfile);
440 got_object_tree_close(tree1);
441 got_object_tree_close(tree2);
442 got_object_close(obj1);
443 got_object_close(obj2);
444 got_repo_close(repo);
445 return (err == NULL);
448 #define RUN_TEST(expr, name) \
449 { test_ok = (expr); \
450 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
451 failure = (failure || !test_ok); }
457 fprintf(stderr, "usage: repository_test [-v] [REPO_PATH]\n");
461 main(int argc, char *argv[])
463 int test_ok = 0, failure = 0;
464 const char *repo_path;
468 if (pledge("stdio rpath wpath cpath proc exec sendfd", NULL) == -1)
472 while ((ch = getopt(argc, argv, "v")) != -1) {
486 repo_path = GOT_REPO_PATH;
494 RUN_TEST(repo_read_tree(repo_path), "read_tree");
495 RUN_TEST(repo_read_log(repo_path), "read_log");
496 RUN_TEST(repo_read_blob(repo_path), "read_blob");
497 RUN_TEST(repo_diff_blob(repo_path), "diff_blob");
498 RUN_TEST(repo_diff_tree(repo_path), "diff_tree");
500 return failure ? 1 : 0;