Blob


1 /*
2 * Copyright (c) 2018 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 <stdio.h>
21 #include <stdlib.h>
22 #include <sha1.h>
24 #include "got_error.h"
25 #include "got_object.h"
26 #include "pack.h"
28 #define GOT_REPO_PATH "../../../"
30 static int
31 packfile_read_idx(const char *repo_path)
32 {
33 const struct got_error *err;
34 struct got_packidx_v2_hdr *packidx;
35 const char *pack_checksum = "5414c35e56c54294d2515863832bf46ad0e321d7";
36 const char *pack_prefix = ".git/objects/pack/pack";
37 char *fullpath;
38 int ret = 1;
40 if (asprintf(&fullpath, "%s/%s-%s.idx", repo_path, pack_prefix,
41 pack_checksum) == -1)
42 return 0;
44 err = got_packidx_open(&packidx, fullpath);
45 if (err) {
46 printf("got_packidx_open: %s\n", err->msg);
47 ret = 0;
48 }
50 got_packidx_close(packidx);
51 free(fullpath);
52 return ret;
53 }
55 #define RUN_TEST(expr, name) \
56 { test_ok = (expr); \
57 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
58 failure = (failure || !test_ok); }
60 int
61 main(int argc, const char *argv[])
62 {
63 int test_ok = 0, failure = 0;
64 const char *repo_path;
66 if (argc == 1)
67 repo_path = GOT_REPO_PATH;
68 else if (argc == 2)
69 repo_path = argv[1];
70 else {
71 fprintf(stderr, "usage: repository_test [REPO_PATH]\n");
72 return 1;
73 }
75 RUN_TEST(packfile_read_idx(repo_path), "packfile_read_idx");
77 return failure ? 1 : 0;
78 }