Blame


1 0a0a3048 2018-01-10 stsp /*
2 0a0a3048 2018-01-10 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 0a0a3048 2018-01-10 stsp *
4 0a0a3048 2018-01-10 stsp * Permission to use, copy, modify, and distribute this software for any
5 0a0a3048 2018-01-10 stsp * purpose with or without fee is hereby granted, provided that the above
6 0a0a3048 2018-01-10 stsp * copyright notice and this permission notice appear in all copies.
7 0a0a3048 2018-01-10 stsp *
8 0a0a3048 2018-01-10 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 0a0a3048 2018-01-10 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 0a0a3048 2018-01-10 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 0a0a3048 2018-01-10 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 0a0a3048 2018-01-10 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 0a0a3048 2018-01-10 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 0a0a3048 2018-01-10 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 0a0a3048 2018-01-10 stsp */
16 0a0a3048 2018-01-10 stsp
17 0a0a3048 2018-01-10 stsp #include <sys/stat.h>
18 a1fd68d8 2018-01-12 stsp #include <sys/queue.h>
19 0a0a3048 2018-01-10 stsp
20 0a0a3048 2018-01-10 stsp #include <stdio.h>
21 0a0a3048 2018-01-10 stsp #include <stdlib.h>
22 0a0a3048 2018-01-10 stsp #include <sha1.h>
23 0a0a3048 2018-01-10 stsp
24 0a0a3048 2018-01-10 stsp #include "got_error.h"
25 a1fd68d8 2018-01-12 stsp #include "got_object.h"
26 0a0a3048 2018-01-10 stsp #include "pack.h"
27 0a0a3048 2018-01-10 stsp
28 0a0a3048 2018-01-10 stsp #define GOT_REPO_PATH "../../../"
29 0a0a3048 2018-01-10 stsp
30 0a0a3048 2018-01-10 stsp static int
31 0a0a3048 2018-01-10 stsp packfile_read_idx(const char *repo_path)
32 0a0a3048 2018-01-10 stsp {
33 0a0a3048 2018-01-10 stsp const struct got_error *err;
34 0a0a3048 2018-01-10 stsp struct got_packidx_v2_hdr *packidx;
35 0a0a3048 2018-01-10 stsp const char *pack_checksum = "5414c35e56c54294d2515863832bf46ad0e321d7";
36 0a0a3048 2018-01-10 stsp const char *pack_prefix = ".git/objects/pack/pack";
37 0a0a3048 2018-01-10 stsp char *fullpath;
38 0a0a3048 2018-01-10 stsp int ret = 1;
39 0a0a3048 2018-01-10 stsp
40 0a0a3048 2018-01-10 stsp if (asprintf(&fullpath, "%s/%s-%s.idx", repo_path, pack_prefix,
41 0a0a3048 2018-01-10 stsp pack_checksum) == -1)
42 0a0a3048 2018-01-10 stsp return 0;
43 0a0a3048 2018-01-10 stsp
44 0a0a3048 2018-01-10 stsp err = got_packidx_open(&packidx, fullpath);
45 0a0a3048 2018-01-10 stsp if (err) {
46 0a0a3048 2018-01-10 stsp printf("got_packidx_open: %s\n", err->msg);
47 0a0a3048 2018-01-10 stsp ret = 0;
48 0a0a3048 2018-01-10 stsp }
49 0a0a3048 2018-01-10 stsp
50 0a0a3048 2018-01-10 stsp got_packidx_close(packidx);
51 0a0a3048 2018-01-10 stsp free(fullpath);
52 0a0a3048 2018-01-10 stsp return ret;
53 0a0a3048 2018-01-10 stsp }
54 0a0a3048 2018-01-10 stsp
55 b08fe7be 2018-01-26 stsp #define RUN_TEST(expr, name) \
56 b08fe7be 2018-01-26 stsp { test_ok = (expr); \
57 b08fe7be 2018-01-26 stsp printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
58 b08fe7be 2018-01-26 stsp failure = (failure || !test_ok); }
59 b08fe7be 2018-01-26 stsp
60 0a0a3048 2018-01-10 stsp int
61 0a0a3048 2018-01-10 stsp main(int argc, const char *argv[])
62 0a0a3048 2018-01-10 stsp {
63 b08fe7be 2018-01-26 stsp int test_ok = 0, failure = 0;
64 0a0a3048 2018-01-10 stsp const char *repo_path;
65 0a0a3048 2018-01-10 stsp
66 0a0a3048 2018-01-10 stsp if (argc == 1)
67 0a0a3048 2018-01-10 stsp repo_path = GOT_REPO_PATH;
68 0a0a3048 2018-01-10 stsp else if (argc == 2)
69 0a0a3048 2018-01-10 stsp repo_path = argv[1];
70 0a0a3048 2018-01-10 stsp else {
71 0a0a3048 2018-01-10 stsp fprintf(stderr, "usage: repository_test [REPO_PATH]\n");
72 0a0a3048 2018-01-10 stsp return 1;
73 0a0a3048 2018-01-10 stsp }
74 0a0a3048 2018-01-10 stsp
75 0a0a3048 2018-01-10 stsp RUN_TEST(packfile_read_idx(repo_path), "packfile_read_idx");
76 0a0a3048 2018-01-10 stsp
77 0a0a3048 2018-01-10 stsp return failure ? 1 : 0;
78 0a0a3048 2018-01-10 stsp }