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"
36 #include "got_lib_path.h"
38 #ifndef nitems
39 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
40 #endif
42 #define GOT_REPO_PATH "../../../"
44 static int verbose;
46 void
47 test_printf(char *fmt, ...)
48 {
49 va_list ap;
51 if (!verbose)
52 return;
54 va_start(ap, fmt);
55 vprintf(fmt, ap);
56 va_end(ap);
57 }
59 static const struct got_error *
60 print_commit_object(struct got_object_id *, struct got_repository *);
62 static const struct got_error *
63 print_parent_commits(struct got_commit_object *commit,
64 struct got_repository *repo)
65 {
66 const struct got_object_id_queue *parent_ids;
67 struct got_object_qid *qid;
68 const struct got_error *err = NULL;
70 parent_ids = got_object_commit_get_parent_ids(commit);
71 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
72 err = print_commit_object(qid->id, repo);
73 if (err)
74 break;
75 }
77 return err;
78 }
80 static const struct got_error *
81 print_tree_object(struct got_object_id *id, char *parent,
82 struct got_repository *repo)
83 {
84 struct got_tree_object *tree;
85 const struct got_tree_entries *entries;
86 struct got_tree_entry *te;
87 const struct got_error *err;
89 err = got_object_open_as_tree(&tree, repo, id);
90 if (err != NULL)
91 return err;
93 entries = got_object_tree_get_entries(tree);
94 SIMPLEQ_FOREACH(te, &entries->head, entry) {
95 char *next_parent;
96 char *hex;
98 err = got_object_id_str(&hex, te->id);
99 if (err)
100 break;
102 if (!S_ISDIR(te->mode)) {
103 test_printf("%s %s/%s\n", hex, parent, te->name);
104 free(hex);
105 continue;
107 test_printf("%s %s/%s\n", hex, parent, te->name);
108 free(hex);
110 if (asprintf(&next_parent, "%s/%s", parent, te->name) == -1) {
111 err = got_error_from_errno();
112 break;
115 err = print_tree_object(te->id, next_parent, repo);
116 free(next_parent);
117 if (err)
118 break;
121 got_object_tree_close(tree);
122 return err;
125 static const struct got_error *
126 print_commit_object(struct got_object_id *id, struct got_repository *repo)
128 struct got_commit_object *commit;
129 const struct got_object_id_queue *parent_ids;
130 struct got_object_qid *qid;
131 char *buf;
132 const struct got_error *err;
133 int obj_type;
135 err = got_object_open_as_commit(&commit, repo, id);
136 if (err)
137 return err;
139 err = got_object_id_str(&buf, id);
140 if (err) {
141 got_object_commit_close(commit);
142 return err;
144 test_printf("tree: %s\n", buf);
145 free(buf);
146 test_printf("parent%s: ",
147 (got_object_commit_get_nparents(commit) == 1) ? "" : "s");
148 parent_ids = got_object_commit_get_parent_ids(commit);
149 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
150 err = got_object_id_str(&buf, qid->id);
151 if (err) {
152 got_object_commit_close(commit);
153 return err;
155 test_printf("%s\n", buf);
156 free(buf);
158 test_printf("author: %s\n", got_object_commit_get_author(commit));
159 test_printf("committer: %s\n", got_object_commit_get_committer(commit));
160 test_printf("log: %s\n", got_object_commit_get_logmsg(commit));
162 err = got_object_get_type(&obj_type, repo,
163 got_object_commit_get_tree_id(commit));
164 if (err != NULL) {
165 got_object_commit_close(commit);
166 return err;
168 if (obj_type == GOT_OBJ_TYPE_TREE)
169 test_printf("\n");
171 err = print_parent_commits(commit, repo);
172 got_object_commit_close(commit);
174 return err;
177 static int
178 repo_read_log(const char *repo_path)
180 const struct got_error *err;
181 struct got_repository *repo;
182 struct got_reference *head_ref;
183 struct got_object_id *id;
184 char *buf;
186 err = got_repo_open(&repo, repo_path);
187 if (err != NULL || repo == NULL)
188 return 0;
189 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
190 if (err != NULL || head_ref == NULL)
191 return 0;
192 err = got_ref_resolve(&id, repo, head_ref);
193 if (err != NULL || head_ref == NULL)
194 return 0;
195 err = got_object_id_str(&buf, id);
196 if (err != NULL)
197 return 0;
198 test_printf("HEAD is at %s\n", buf);
199 free(buf);
200 err = print_commit_object(id, repo);
201 if (err)
202 return 0;
203 free(id);
204 got_ref_close(head_ref);
205 got_repo_close(repo);
206 return 1;
209 static int
210 repo_read_tree(const char *repo_path)
212 const char *tree_sha1 = "6cc96e0e093fb30630ba7f199d0a008b24c6a690";
213 const struct got_error *err;
214 struct got_repository *repo;
215 struct got_object_id *id;
217 err = got_repo_open(&repo, repo_path);
218 if (err != NULL || repo == NULL)
219 return 0;
220 err = got_object_resolve_id_str(&id, repo, tree_sha1);
221 if (err != NULL)
222 return 0;
224 print_tree_object(id, "", repo);
225 test_printf("\n");
227 got_repo_close(repo);
228 return (err == NULL);
231 static int
232 repo_read_blob(const char *repo_path)
234 const char *blob_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
235 const struct got_error *err;
236 struct got_repository *repo;
237 struct got_object_id *id;
238 struct got_blob_object *blob;
239 int i;
240 size_t len;
242 err = got_repo_open(&repo, repo_path);
243 if (err != NULL || repo == NULL)
244 return 0;
245 err = got_object_resolve_id_str(&id, repo, blob_sha1);
246 if (err != NULL)
247 return 0;
248 err = got_object_open_as_blob(&blob, repo, id, 64);
249 if (err != NULL)
250 return 0;
252 test_printf("\n");
253 do {
254 const uint8_t *buf = got_object_blob_get_read_buf(blob);
255 err = got_object_blob_read_block(&len, blob);
256 if (err)
257 break;
258 for (i = 0; i < len; i++)
259 test_printf("%c", buf[i]);
260 } while (len != 0);
261 test_printf("\n");
263 got_object_blob_close(blob);
264 got_repo_close(repo);
265 return (err == NULL);
268 static int
269 repo_diff_blob(const char *repo_path)
271 const char *blob1_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
272 const char *blob2_sha1 = "de7eb21b21c7823a753261aadf7cba35c9580fbf";
273 const struct got_error *err;
274 struct got_repository *repo;
275 struct got_object_id *id1, *id2;
276 struct got_blob_object *blob1;
277 struct got_blob_object *blob2;
278 FILE *outfile;
279 int i;
280 char *line;
281 size_t len;
282 const char delim[3] = {'\0', '\0', '\0'};
283 const char *expected_output[] = {
284 "--- 141f5fdc96126c1f4195558560a3c915e3d9b4c3",
285 "+++ de7eb21b21c7823a753261aadf7cba35c9580fbf",
286 "@@ -1,10 +1,10 @@",
287 " .PATH:${.CURDIR}/../../lib",
288 " ",
289 " PROG = repository_test",
290 "-SRCS = path.c repository.c error.c refs.c repository_test.c",
291 "+SRCS = path.c repository.c error.c refs.c object.c sha1.c repository_test.c",
292 " ",
293 " CPPFLAGS = -I${.CURDIR}/../../include",
294 "-LDADD = -lutil",
295 "+LDADD = -lutil -lz",
296 " ",
297 " NOMAN = yes"
298 };
300 err = got_repo_open(&repo, repo_path);
301 if (err != NULL || repo == NULL)
302 return 0;
304 err = got_object_resolve_id_str(&id1, repo, blob1_sha1);
305 if (err != NULL)
306 return 0;
308 err = got_object_resolve_id_str(&id2, repo, blob2_sha1);
309 if (err != NULL)
310 return 0;
312 err = got_object_open_as_blob(&blob1, repo, id1, 512);
313 if (err != NULL)
314 return 0;
316 err = got_object_open_as_blob(&blob2, repo, id2, 512);
317 if (err != NULL)
318 return 0;
320 test_printf("\n");
321 outfile = got_opentemp();
322 if (outfile == NULL)
323 return 0;
324 got_diff_blob(blob1, blob2, NULL, NULL, 3, outfile);
325 rewind(outfile);
326 i = 0;
327 while ((line = fparseln(outfile, &len, NULL, delim, 0)) != NULL) {
328 test_printf(line);
329 test_printf("\n");
330 if (i < nitems(expected_output) &&
331 strcmp(line, expected_output[i]) != 0) {
332 test_printf("diff output mismatch; expected: '%s'\n",
333 expected_output[i]);
334 return 0;
336 i++;
338 fclose(outfile);
339 test_printf("\n");
340 if (i != nitems(expected_output) + 1) {
341 test_printf("number of lines expected: %d; actual: %d\n",
342 nitems(expected_output), i - 1);
343 return 0;
346 got_object_blob_close(blob1);
347 got_object_blob_close(blob2);
348 got_repo_close(repo);
349 return (err == NULL);
352 static int
353 repo_diff_tree(const char *repo_path)
355 const char *tree1_sha1 = "1efc41caf761a0a1f119d0c5121eedcb2e7a88c3";
356 const char *tree2_sha1 = "4aa8f2933839ff8a8fb3f905a4c232d22c6ff5f3";
357 const struct got_error *err;
358 struct got_repository *repo;
359 struct got_object_id *id1;
360 struct got_object_id *id2;
361 struct got_tree_object *tree1;
362 struct got_tree_object *tree2;
363 FILE *outfile;
365 err = got_repo_open(&repo, repo_path);
366 if (err != NULL || repo == NULL)
367 return 0;
369 err = got_object_resolve_id_str(&id1, repo, tree1_sha1);
370 if (err != NULL)
371 return 0;
372 err = got_object_resolve_id_str(&id2, repo, tree2_sha1);
373 if (err != NULL)
374 return 0;
376 err = got_object_open_as_tree(&tree1, repo, id1);
377 if (err != NULL)
378 return 0;
380 err = got_object_open_as_tree(&tree2, repo, id2);
381 if (err != NULL)
382 return 0;
384 if (!verbose) {
385 outfile = fopen("/dev/null", "w+");
386 if (outfile == NULL)
387 return 0;
388 } else
389 outfile = stdout;
390 test_printf("\n");
391 got_diff_tree(tree1, tree2, "", "", 3, repo, outfile);
392 test_printf("\n");
394 got_object_tree_close(tree1);
395 got_object_tree_close(tree2);
396 got_repo_close(repo);
397 return (err == NULL);
400 #define RUN_TEST(expr, name) \
401 { test_ok = (expr); \
402 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
403 failure = (failure || !test_ok); }
406 void
407 usage(void)
409 fprintf(stderr, "usage: repository_test [-v] [REPO_PATH]\n");
412 int
413 main(int argc, char *argv[])
415 int test_ok = 0, failure = 0;
416 const char *repo_path;
417 int ch;
419 #ifndef PROFILE
420 if (pledge("stdio rpath wpath cpath proc exec sendfd", NULL) == -1)
421 err(1, "pledge");
422 #endif
424 while ((ch = getopt(argc, argv, "v")) != -1) {
425 switch (ch) {
426 case 'v':
427 verbose = 1;
428 break;
429 default:
430 usage();
431 return 1;
434 argc -= optind;
435 argv += optind;
437 if (argc == 0)
438 repo_path = GOT_REPO_PATH;
439 else if (argc == 1)
440 repo_path = argv[0];
441 else {
442 usage();
443 return 1;
446 RUN_TEST(repo_read_tree(repo_path), "read_tree");
447 RUN_TEST(repo_read_log(repo_path), "read_log");
448 RUN_TEST(repo_read_blob(repo_path), "read_blob");
449 RUN_TEST(repo_diff_blob(repo_path), "diff_blob");
450 RUN_TEST(repo_diff_tree(repo_path), "diff_tree");
452 return failure ? 1 : 0;