Blame


1 7b19e0f1 2017-11-05 stsp /*
2 7b19e0f1 2017-11-05 stsp * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
3 7b19e0f1 2017-11-05 stsp *
4 7b19e0f1 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
5 7b19e0f1 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
6 7b19e0f1 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
7 7b19e0f1 2017-11-05 stsp *
8 7b19e0f1 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 7b19e0f1 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 7b19e0f1 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 7b19e0f1 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 7b19e0f1 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 7b19e0f1 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 7b19e0f1 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 7b19e0f1 2017-11-05 stsp */
16 7b19e0f1 2017-11-05 stsp
17 0ffeb3c2 2017-11-26 stsp #include <sys/stat.h>
18 d1cda826 2017-11-06 stsp #include <sys/queue.h>
19 d1cda826 2017-11-06 stsp
20 6e6049be 2019-07-23 stsp #include <errno.h>
21 82f2fb69 2018-01-26 stsp #include <stdarg.h>
22 4027f31a 2017-11-04 stsp #include <stdio.h>
23 5a83d54e 2018-04-01 stsp #include <util.h>
24 4027f31a 2017-11-04 stsp #include <stdlib.h>
25 82f2fb69 2018-01-26 stsp #include <string.h>
26 f8352b2a 2018-03-12 stsp #include <unistd.h>
27 f8352b2a 2018-03-12 stsp #include <err.h>
28 59ece79d 2018-02-12 stsp #include <unistd.h>
29 4027f31a 2017-11-04 stsp
30 4027f31a 2017-11-04 stsp #include "got_error.h"
31 11995603 2017-11-05 stsp #include "got_object.h"
32 5261c201 2018-04-01 stsp #include "got_reference.h"
33 4027f31a 2017-11-04 stsp #include "got_repository.h"
34 f78b0693 2017-11-29 stsp #include "got_diff.h"
35 511a516b 2018-05-19 stsp #include "got_opentemp.h"
36 cb44a3e5 2019-01-04 stsp #include "got_privsep.h"
37 324d37e7 2019-05-11 stsp #include "got_path.h"
38 4027f31a 2017-11-04 stsp
39 5a83d54e 2018-04-01 stsp
40 5a83d54e 2018-04-01 stsp #ifndef nitems
41 5a83d54e 2018-04-01 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
42 5a83d54e 2018-04-01 stsp #endif
43 5a83d54e 2018-04-01 stsp
44 4027f31a 2017-11-04 stsp #define GOT_REPO_PATH "../../../"
45 4027f31a 2017-11-04 stsp
46 14545512 2018-01-26 stsp static int verbose;
47 82f2fb69 2018-01-26 stsp
48 82f2fb69 2018-01-26 stsp void
49 82f2fb69 2018-01-26 stsp test_printf(char *fmt, ...)
50 82f2fb69 2018-01-26 stsp {
51 82f2fb69 2018-01-26 stsp va_list ap;
52 82f2fb69 2018-01-26 stsp
53 82f2fb69 2018-01-26 stsp if (!verbose)
54 82f2fb69 2018-01-26 stsp return;
55 82f2fb69 2018-01-26 stsp
56 82f2fb69 2018-01-26 stsp va_start(ap, fmt);
57 82f2fb69 2018-01-26 stsp vprintf(fmt, ap);
58 82f2fb69 2018-01-26 stsp va_end(ap);
59 82f2fb69 2018-01-26 stsp }
60 82f2fb69 2018-01-26 stsp
61 bfab4d9a 2017-11-12 stsp static const struct got_error *
62 15a94983 2018-12-23 stsp print_commit_object(struct got_object_id *, struct got_repository *);
63 1c852fbe 2017-11-12 stsp
64 1c852fbe 2017-11-12 stsp static const struct got_error *
65 bfab4d9a 2017-11-12 stsp print_parent_commits(struct got_commit_object *commit,
66 bfab4d9a 2017-11-12 stsp struct got_repository *repo)
67 bfab4d9a 2017-11-12 stsp {
68 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
69 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
70 a37d050f 2018-01-26 stsp const struct got_error *err = NULL;
71 bfab4d9a 2017-11-12 stsp
72 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
73 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
74 15a94983 2018-12-23 stsp err = print_commit_object(qid->id, repo);
75 2178c42e 2018-04-22 stsp if (err)
76 2178c42e 2018-04-22 stsp break;
77 bfab4d9a 2017-11-12 stsp }
78 bfab4d9a 2017-11-12 stsp
79 a37d050f 2018-01-26 stsp return err;
80 bfab4d9a 2017-11-12 stsp }
81 bfab4d9a 2017-11-12 stsp
82 bfab4d9a 2017-11-12 stsp static const struct got_error *
83 15a94983 2018-12-23 stsp print_tree_object(struct got_object_id *id, char *parent,
84 f715ca7f 2017-11-27 stsp struct got_repository *repo)
85 0ffeb3c2 2017-11-26 stsp {
86 0ffeb3c2 2017-11-26 stsp struct got_tree_object *tree;
87 883f0469 2018-06-23 stsp const struct got_tree_entries *entries;
88 f715ca7f 2017-11-27 stsp struct got_tree_entry *te;
89 0ffeb3c2 2017-11-26 stsp const struct got_error *err;
90 0ffeb3c2 2017-11-26 stsp
91 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree, repo, id);
92 0ffeb3c2 2017-11-26 stsp if (err != NULL)
93 0ffeb3c2 2017-11-26 stsp return err;
94 0ffeb3c2 2017-11-26 stsp
95 883f0469 2018-06-23 stsp entries = got_object_tree_get_entries(tree);
96 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &entries->head, entry) {
97 f715ca7f 2017-11-27 stsp char *next_parent;
98 ef0981d5 2018-02-12 stsp char *hex;
99 f715ca7f 2017-11-27 stsp
100 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, te->id);
101 ef0981d5 2018-02-12 stsp if (err)
102 ef0981d5 2018-02-12 stsp break;
103 ef0981d5 2018-02-12 stsp
104 f715ca7f 2017-11-27 stsp if (!S_ISDIR(te->mode)) {
105 ef0981d5 2018-02-12 stsp test_printf("%s %s/%s\n", hex, parent, te->name);
106 f78ec441 2018-03-17 stsp free(hex);
107 f715ca7f 2017-11-27 stsp continue;
108 f715ca7f 2017-11-27 stsp }
109 ef0981d5 2018-02-12 stsp test_printf("%s %s/%s\n", hex, parent, te->name);
110 ef0981d5 2018-02-12 stsp free(hex);
111 f715ca7f 2017-11-27 stsp
112 f715ca7f 2017-11-27 stsp if (asprintf(&next_parent, "%s/%s", parent, te->name) == -1) {
113 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
114 f715ca7f 2017-11-27 stsp break;
115 f715ca7f 2017-11-27 stsp }
116 f715ca7f 2017-11-27 stsp
117 15a94983 2018-12-23 stsp err = print_tree_object(te->id, next_parent, repo);
118 f715ca7f 2017-11-27 stsp free(next_parent);
119 15a94983 2018-12-23 stsp if (err)
120 f715ca7f 2017-11-27 stsp break;
121 f715ca7f 2017-11-27 stsp }
122 f715ca7f 2017-11-27 stsp
123 0ffeb3c2 2017-11-26 stsp got_object_tree_close(tree);
124 f715ca7f 2017-11-27 stsp return err;
125 0ffeb3c2 2017-11-26 stsp }
126 0ffeb3c2 2017-11-26 stsp
127 0ffeb3c2 2017-11-26 stsp static const struct got_error *
128 15a94983 2018-12-23 stsp print_commit_object(struct got_object_id *id, struct got_repository *repo)
129 1c852fbe 2017-11-12 stsp {
130 1c852fbe 2017-11-12 stsp struct got_commit_object *commit;
131 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
132 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
133 ef0981d5 2018-02-12 stsp char *buf;
134 1c852fbe 2017-11-12 stsp const struct got_error *err;
135 15a94983 2018-12-23 stsp int obj_type;
136 1c852fbe 2017-11-12 stsp
137 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, id);
138 ef0981d5 2018-02-12 stsp if (err)
139 1c852fbe 2017-11-12 stsp return err;
140 1c852fbe 2017-11-12 stsp
141 15a94983 2018-12-23 stsp err = got_object_id_str(&buf, id);
142 15a94983 2018-12-23 stsp if (err) {
143 15a94983 2018-12-23 stsp got_object_commit_close(commit);
144 ef0981d5 2018-02-12 stsp return err;
145 15a94983 2018-12-23 stsp }
146 ef0981d5 2018-02-12 stsp test_printf("tree: %s\n", buf);
147 ef0981d5 2018-02-12 stsp free(buf);
148 45d799e2 2018-12-23 stsp test_printf("parent%s: ",
149 45d799e2 2018-12-23 stsp (got_object_commit_get_nparents(commit) == 1) ? "" : "s");
150 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
151 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
152 79f35eb3 2018-06-11 stsp err = got_object_id_str(&buf, qid->id);
153 15a94983 2018-12-23 stsp if (err) {
154 15a94983 2018-12-23 stsp got_object_commit_close(commit);
155 ef0981d5 2018-02-12 stsp return err;
156 15a94983 2018-12-23 stsp }
157 ef0981d5 2018-02-12 stsp test_printf("%s\n", buf);
158 ef0981d5 2018-02-12 stsp free(buf);
159 1c852fbe 2017-11-12 stsp }
160 45d799e2 2018-12-23 stsp test_printf("author: %s\n", got_object_commit_get_author(commit));
161 45d799e2 2018-12-23 stsp test_printf("committer: %s\n", got_object_commit_get_committer(commit));
162 45d799e2 2018-12-23 stsp test_printf("log: %s\n", got_object_commit_get_logmsg(commit));
163 bfab4d9a 2017-11-12 stsp
164 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo,
165 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
166 15a94983 2018-12-23 stsp if (err != NULL) {
167 15a94983 2018-12-23 stsp got_object_commit_close(commit);
168 0ffeb3c2 2017-11-26 stsp return err;
169 f715ca7f 2017-11-27 stsp }
170 15a94983 2018-12-23 stsp if (obj_type == GOT_OBJ_TYPE_TREE)
171 15a94983 2018-12-23 stsp test_printf("\n");
172 0ffeb3c2 2017-11-26 stsp
173 bfab4d9a 2017-11-12 stsp err = print_parent_commits(commit, repo);
174 1c852fbe 2017-11-12 stsp got_object_commit_close(commit);
175 1c852fbe 2017-11-12 stsp
176 bfab4d9a 2017-11-12 stsp return err;
177 1c852fbe 2017-11-12 stsp }
178 1c852fbe 2017-11-12 stsp
179 4027f31a 2017-11-04 stsp static int
180 bfab4d9a 2017-11-12 stsp repo_read_log(const char *repo_path)
181 11995603 2017-11-05 stsp {
182 11995603 2017-11-05 stsp const struct got_error *err;
183 11995603 2017-11-05 stsp struct got_repository *repo;
184 11995603 2017-11-05 stsp struct got_reference *head_ref;
185 11995603 2017-11-05 stsp struct got_object_id *id;
186 ef0981d5 2018-02-12 stsp char *buf;
187 11995603 2017-11-05 stsp
188 11995603 2017-11-05 stsp err = got_repo_open(&repo, repo_path);
189 11995603 2017-11-05 stsp if (err != NULL || repo == NULL)
190 11995603 2017-11-05 stsp return 0;
191 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
192 11995603 2017-11-05 stsp if (err != NULL || head_ref == NULL)
193 11995603 2017-11-05 stsp return 0;
194 11995603 2017-11-05 stsp err = got_ref_resolve(&id, repo, head_ref);
195 11995603 2017-11-05 stsp if (err != NULL || head_ref == NULL)
196 11995603 2017-11-05 stsp return 0;
197 ef0981d5 2018-02-12 stsp err = got_object_id_str(&buf, id);
198 ef0981d5 2018-02-12 stsp if (err != NULL)
199 ef0981d5 2018-02-12 stsp return 0;
200 ef0981d5 2018-02-12 stsp test_printf("HEAD is at %s\n", buf);
201 ef0981d5 2018-02-12 stsp free(buf);
202 15a94983 2018-12-23 stsp err = print_commit_object(id, repo);
203 15a94983 2018-12-23 stsp if (err)
204 a37d050f 2018-01-26 stsp return 0;
205 11995603 2017-11-05 stsp free(id);
206 11995603 2017-11-05 stsp got_ref_close(head_ref);
207 11995603 2017-11-05 stsp got_repo_close(repo);
208 11995603 2017-11-05 stsp return 1;
209 11995603 2017-11-05 stsp }
210 044e7393 2018-02-11 stsp
211 044e7393 2018-02-11 stsp static int
212 044e7393 2018-02-11 stsp repo_read_tree(const char *repo_path)
213 044e7393 2018-02-11 stsp {
214 044e7393 2018-02-11 stsp const char *tree_sha1 = "6cc96e0e093fb30630ba7f199d0a008b24c6a690";
215 044e7393 2018-02-11 stsp const struct got_error *err;
216 044e7393 2018-02-11 stsp struct got_repository *repo;
217 15a94983 2018-12-23 stsp struct got_object_id *id;
218 044e7393 2018-02-11 stsp
219 044e7393 2018-02-11 stsp err = got_repo_open(&repo, repo_path);
220 044e7393 2018-02-11 stsp if (err != NULL || repo == NULL)
221 044e7393 2018-02-11 stsp return 0;
222 15a94983 2018-12-23 stsp err = got_object_resolve_id_str(&id, repo, tree_sha1);
223 15a94983 2018-12-23 stsp if (err != NULL)
224 044e7393 2018-02-11 stsp return 0;
225 11995603 2017-11-05 stsp
226 15a94983 2018-12-23 stsp print_tree_object(id, "", repo);
227 044e7393 2018-02-11 stsp test_printf("\n");
228 044e7393 2018-02-11 stsp
229 044e7393 2018-02-11 stsp got_repo_close(repo);
230 044e7393 2018-02-11 stsp return (err == NULL);
231 044e7393 2018-02-11 stsp }
232 f78ec441 2018-03-17 stsp
233 68482ea3 2017-11-27 stsp static int
234 68482ea3 2017-11-27 stsp repo_read_blob(const char *repo_path)
235 68482ea3 2017-11-27 stsp {
236 68482ea3 2017-11-27 stsp const char *blob_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
237 68482ea3 2017-11-27 stsp const struct got_error *err;
238 68482ea3 2017-11-27 stsp struct got_repository *repo;
239 15a94983 2018-12-23 stsp struct got_object_id *id;
240 68482ea3 2017-11-27 stsp struct got_blob_object *blob;
241 68482ea3 2017-11-27 stsp int i;
242 68482ea3 2017-11-27 stsp size_t len;
243 68482ea3 2017-11-27 stsp
244 68482ea3 2017-11-27 stsp err = got_repo_open(&repo, repo_path);
245 68482ea3 2017-11-27 stsp if (err != NULL || repo == NULL)
246 68482ea3 2017-11-27 stsp return 0;
247 15a94983 2018-12-23 stsp err = got_object_resolve_id_str(&id, repo, blob_sha1);
248 15a94983 2018-12-23 stsp if (err != NULL)
249 68482ea3 2017-11-27 stsp return 0;
250 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, id, 64);
251 68482ea3 2017-11-27 stsp if (err != NULL)
252 68482ea3 2017-11-27 stsp return 0;
253 68482ea3 2017-11-27 stsp
254 82f2fb69 2018-01-26 stsp test_printf("\n");
255 68482ea3 2017-11-27 stsp do {
256 f934cf2c 2018-02-12 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
257 eb651edf 2018-02-11 stsp err = got_object_blob_read_block(&len, blob);
258 68482ea3 2017-11-27 stsp if (err)
259 68482ea3 2017-11-27 stsp break;
260 68482ea3 2017-11-27 stsp for (i = 0; i < len; i++)
261 f934cf2c 2018-02-12 stsp test_printf("%c", buf[i]);
262 68482ea3 2017-11-27 stsp } while (len != 0);
263 82f2fb69 2018-01-26 stsp test_printf("\n");
264 68482ea3 2017-11-27 stsp
265 68482ea3 2017-11-27 stsp got_object_blob_close(blob);
266 7d283eee 2017-11-29 stsp got_repo_close(repo);
267 7d283eee 2017-11-29 stsp return (err == NULL);
268 7d283eee 2017-11-29 stsp }
269 7d283eee 2017-11-29 stsp
270 7d283eee 2017-11-29 stsp static int
271 7d283eee 2017-11-29 stsp repo_diff_blob(const char *repo_path)
272 7d283eee 2017-11-29 stsp {
273 7d283eee 2017-11-29 stsp const char *blob1_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
274 7d283eee 2017-11-29 stsp const char *blob2_sha1 = "de7eb21b21c7823a753261aadf7cba35c9580fbf";
275 7d283eee 2017-11-29 stsp const struct got_error *err;
276 7d283eee 2017-11-29 stsp struct got_repository *repo;
277 15a94983 2018-12-23 stsp struct got_object_id *id1, *id2;
278 7d283eee 2017-11-29 stsp struct got_blob_object *blob1;
279 7d283eee 2017-11-29 stsp struct got_blob_object *blob2;
280 354a7e12 2018-02-11 stsp FILE *outfile;
281 5a83d54e 2018-04-01 stsp int i;
282 5a83d54e 2018-04-01 stsp char *line;
283 5a83d54e 2018-04-01 stsp size_t len;
284 5a83d54e 2018-04-01 stsp const char delim[3] = {'\0', '\0', '\0'};
285 5a83d54e 2018-04-01 stsp const char *expected_output[] = {
286 0de8fe28 2018-12-24 stsp "blob - 141f5fdc96126c1f4195558560a3c915e3d9b4c3",
287 0de8fe28 2018-12-24 stsp "blob + de7eb21b21c7823a753261aadf7cba35c9580fbf",
288 3b8ef1a8 2018-09-13 stsp "--- 141f5fdc96126c1f4195558560a3c915e3d9b4c3",
289 3b8ef1a8 2018-09-13 stsp "+++ de7eb21b21c7823a753261aadf7cba35c9580fbf",
290 5a83d54e 2018-04-01 stsp "@@ -1,10 +1,10 @@",
291 5a83d54e 2018-04-01 stsp " .PATH:${.CURDIR}/../../lib",
292 5a83d54e 2018-04-01 stsp " ",
293 5a83d54e 2018-04-01 stsp " PROG = repository_test",
294 5a83d54e 2018-04-01 stsp "-SRCS = path.c repository.c error.c refs.c repository_test.c",
295 5a83d54e 2018-04-01 stsp "+SRCS = path.c repository.c error.c refs.c object.c sha1.c repository_test.c",
296 5a83d54e 2018-04-01 stsp " ",
297 5a83d54e 2018-04-01 stsp " CPPFLAGS = -I${.CURDIR}/../../include",
298 5a83d54e 2018-04-01 stsp "-LDADD = -lutil",
299 5a83d54e 2018-04-01 stsp "+LDADD = -lutil -lz",
300 5a83d54e 2018-04-01 stsp " ",
301 5a83d54e 2018-04-01 stsp " NOMAN = yes"
302 5a83d54e 2018-04-01 stsp };
303 7d283eee 2017-11-29 stsp
304 7d283eee 2017-11-29 stsp err = got_repo_open(&repo, repo_path);
305 7d283eee 2017-11-29 stsp if (err != NULL || repo == NULL)
306 7d283eee 2017-11-29 stsp return 0;
307 7d283eee 2017-11-29 stsp
308 15a94983 2018-12-23 stsp err = got_object_resolve_id_str(&id1, repo, blob1_sha1);
309 15a94983 2018-12-23 stsp if (err != NULL)
310 7d283eee 2017-11-29 stsp return 0;
311 15a94983 2018-12-23 stsp
312 15a94983 2018-12-23 stsp err = got_object_resolve_id_str(&id2, repo, blob2_sha1);
313 15a94983 2018-12-23 stsp if (err != NULL)
314 7d283eee 2017-11-29 stsp return 0;
315 7d283eee 2017-11-29 stsp
316 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob1, repo, id1, 512);
317 7d283eee 2017-11-29 stsp if (err != NULL)
318 7d283eee 2017-11-29 stsp return 0;
319 7d283eee 2017-11-29 stsp
320 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob2, repo, id2, 512);
321 7d283eee 2017-11-29 stsp if (err != NULL)
322 7d283eee 2017-11-29 stsp return 0;
323 7d283eee 2017-11-29 stsp
324 82f2fb69 2018-01-26 stsp test_printf("\n");
325 5a83d54e 2018-04-01 stsp outfile = got_opentemp();
326 5a83d54e 2018-04-01 stsp if (outfile == NULL)
327 5a83d54e 2018-04-01 stsp return 0;
328 54156555 2018-12-24 stsp got_diff_blob(blob1, blob2, NULL, NULL, 3, outfile);
329 5a83d54e 2018-04-01 stsp rewind(outfile);
330 5a83d54e 2018-04-01 stsp i = 0;
331 5a83d54e 2018-04-01 stsp while ((line = fparseln(outfile, &len, NULL, delim, 0)) != NULL) {
332 5a83d54e 2018-04-01 stsp test_printf(line);
333 5a83d54e 2018-04-01 stsp test_printf("\n");
334 5a83d54e 2018-04-01 stsp if (i < nitems(expected_output) &&
335 5a83d54e 2018-04-01 stsp strcmp(line, expected_output[i]) != 0) {
336 5a83d54e 2018-04-01 stsp test_printf("diff output mismatch; expected: '%s'\n",
337 5a83d54e 2018-04-01 stsp expected_output[i]);
338 3b8ef1a8 2018-09-13 stsp return 0;
339 5a83d54e 2018-04-01 stsp }
340 5a83d54e 2018-04-01 stsp i++;
341 5a83d54e 2018-04-01 stsp }
342 fb43ecf1 2019-02-11 stsp if (fclose(outfile) != 0 && err == NULL)
343 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
344 82f2fb69 2018-01-26 stsp test_printf("\n");
345 5a83d54e 2018-04-01 stsp if (i != nitems(expected_output) + 1) {
346 5a83d54e 2018-04-01 stsp test_printf("number of lines expected: %d; actual: %d\n",
347 5a83d54e 2018-04-01 stsp nitems(expected_output), i - 1);
348 5a83d54e 2018-04-01 stsp return 0;
349 5a83d54e 2018-04-01 stsp }
350 7d283eee 2017-11-29 stsp
351 7d283eee 2017-11-29 stsp got_object_blob_close(blob1);
352 7d283eee 2017-11-29 stsp got_object_blob_close(blob2);
353 98abbc84 2017-11-30 stsp got_repo_close(repo);
354 98abbc84 2017-11-30 stsp return (err == NULL);
355 98abbc84 2017-11-30 stsp }
356 98abbc84 2017-11-30 stsp
357 98abbc84 2017-11-30 stsp static int
358 98abbc84 2017-11-30 stsp repo_diff_tree(const char *repo_path)
359 98abbc84 2017-11-30 stsp {
360 4a0235dd 2017-11-30 stsp const char *tree1_sha1 = "1efc41caf761a0a1f119d0c5121eedcb2e7a88c3";
361 a3e2cbea 2017-12-01 stsp const char *tree2_sha1 = "4aa8f2933839ff8a8fb3f905a4c232d22c6ff5f3";
362 98abbc84 2017-11-30 stsp const struct got_error *err;
363 98abbc84 2017-11-30 stsp struct got_repository *repo;
364 15a94983 2018-12-23 stsp struct got_object_id *id1;
365 15a94983 2018-12-23 stsp struct got_object_id *id2;
366 98abbc84 2017-11-30 stsp struct got_tree_object *tree1;
367 98abbc84 2017-11-30 stsp struct got_tree_object *tree2;
368 354a7e12 2018-02-11 stsp FILE *outfile;
369 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
370 98abbc84 2017-11-30 stsp
371 98abbc84 2017-11-30 stsp err = got_repo_open(&repo, repo_path);
372 98abbc84 2017-11-30 stsp if (err != NULL || repo == NULL)
373 98abbc84 2017-11-30 stsp return 0;
374 98abbc84 2017-11-30 stsp
375 15a94983 2018-12-23 stsp err = got_object_resolve_id_str(&id1, repo, tree1_sha1);
376 15a94983 2018-12-23 stsp if (err != NULL)
377 98abbc84 2017-11-30 stsp return 0;
378 15a94983 2018-12-23 stsp err = got_object_resolve_id_str(&id2, repo, tree2_sha1);
379 15a94983 2018-12-23 stsp if (err != NULL)
380 98abbc84 2017-11-30 stsp return 0;
381 98abbc84 2017-11-30 stsp
382 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo, id1);
383 98abbc84 2017-11-30 stsp if (err != NULL)
384 98abbc84 2017-11-30 stsp return 0;
385 98abbc84 2017-11-30 stsp
386 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo, id2);
387 98abbc84 2017-11-30 stsp if (err != NULL)
388 98abbc84 2017-11-30 stsp return 0;
389 98abbc84 2017-11-30 stsp
390 354a7e12 2018-02-11 stsp if (!verbose) {
391 354a7e12 2018-02-11 stsp outfile = fopen("/dev/null", "w+");
392 354a7e12 2018-02-11 stsp if (outfile == NULL)
393 354a7e12 2018-02-11 stsp return 0;
394 354a7e12 2018-02-11 stsp } else
395 354a7e12 2018-02-11 stsp outfile = stdout;
396 82f2fb69 2018-01-26 stsp test_printf("\n");
397 aaa13589 2019-06-01 stsp arg.diff_context = 3;
398 aaa13589 2019-06-01 stsp arg.outfile = outfile;
399 aaa13589 2019-06-01 stsp got_diff_tree(tree1, tree2, "", "", repo,
400 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
401 82f2fb69 2018-01-26 stsp test_printf("\n");
402 98abbc84 2017-11-30 stsp
403 98abbc84 2017-11-30 stsp got_object_tree_close(tree1);
404 98abbc84 2017-11-30 stsp got_object_tree_close(tree2);
405 68482ea3 2017-11-27 stsp got_repo_close(repo);
406 68482ea3 2017-11-27 stsp return (err == NULL);
407 68482ea3 2017-11-27 stsp }
408 b08fe7be 2018-01-26 stsp
409 b08fe7be 2018-01-26 stsp #define RUN_TEST(expr, name) \
410 b08fe7be 2018-01-26 stsp { test_ok = (expr); \
411 9465d522 2019-01-03 stsp printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
412 b08fe7be 2018-01-26 stsp failure = (failure || !test_ok); }
413 68482ea3 2017-11-27 stsp
414 82f2fb69 2018-01-26 stsp
415 82f2fb69 2018-01-26 stsp void
416 82f2fb69 2018-01-26 stsp usage(void)
417 82f2fb69 2018-01-26 stsp {
418 82f2fb69 2018-01-26 stsp fprintf(stderr, "usage: repository_test [-v] [REPO_PATH]\n");
419 cb44a3e5 2019-01-04 stsp }
420 cb44a3e5 2019-01-04 stsp
421 cb44a3e5 2019-01-04 stsp static const struct got_error *
422 cb44a3e5 2019-01-04 stsp apply_unveil(const char *repo_path)
423 cb44a3e5 2019-01-04 stsp {
424 cb44a3e5 2019-01-04 stsp const struct got_error *error;
425 cb44a3e5 2019-01-04 stsp
426 cb44a3e5 2019-01-04 stsp if (repo_path) {
427 48c84e60 2019-07-22 stsp if (unveil(repo_path, "r") != 0)
428 48c84e60 2019-07-22 stsp return got_error_from_errno2("unveil", repo_path);
429 cb44a3e5 2019-01-04 stsp }
430 cb44a3e5 2019-01-04 stsp
431 cb44a3e5 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
432 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
433 cb44a3e5 2019-01-04 stsp
434 cb44a3e5 2019-01-04 stsp if (unveil("/dev/null", "rwc") != 0)
435 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/dev/null");
436 cb44a3e5 2019-01-04 stsp
437 cb44a3e5 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
438 cb44a3e5 2019-01-04 stsp if (error != NULL)
439 cb44a3e5 2019-01-04 stsp return error;
440 cb44a3e5 2019-01-04 stsp
441 cb44a3e5 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
442 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
443 cb44a3e5 2019-01-04 stsp
444 cb44a3e5 2019-01-04 stsp return NULL;
445 82f2fb69 2018-01-26 stsp }
446 82f2fb69 2018-01-26 stsp
447 4027f31a 2017-11-04 stsp int
448 82f2fb69 2018-01-26 stsp main(int argc, char *argv[])
449 4027f31a 2017-11-04 stsp {
450 b08fe7be 2018-01-26 stsp int test_ok = 0, failure = 0;
451 6e6049be 2019-07-23 stsp char *repo_path;
452 82f2fb69 2018-01-26 stsp int ch;
453 cb44a3e5 2019-01-04 stsp const struct got_error *error;
454 4027f31a 2017-11-04 stsp
455 2ff12563 2018-09-15 stsp #ifndef PROFILE
456 cb44a3e5 2019-01-04 stsp if (pledge("stdio rpath wpath cpath proc exec sendfd unveil", NULL)
457 cb44a3e5 2019-01-04 stsp == -1)
458 f8352b2a 2018-03-12 stsp err(1, "pledge");
459 2ff12563 2018-09-15 stsp #endif
460 f8352b2a 2018-03-12 stsp
461 82f2fb69 2018-01-26 stsp while ((ch = getopt(argc, argv, "v")) != -1) {
462 82f2fb69 2018-01-26 stsp switch (ch) {
463 82f2fb69 2018-01-26 stsp case 'v':
464 82f2fb69 2018-01-26 stsp verbose = 1;
465 82f2fb69 2018-01-26 stsp break;
466 82f2fb69 2018-01-26 stsp default:
467 82f2fb69 2018-01-26 stsp usage();
468 82f2fb69 2018-01-26 stsp return 1;
469 82f2fb69 2018-01-26 stsp }
470 82f2fb69 2018-01-26 stsp }
471 82f2fb69 2018-01-26 stsp argc -= optind;
472 82f2fb69 2018-01-26 stsp argv += optind;
473 82f2fb69 2018-01-26 stsp
474 6e6049be 2019-07-23 stsp switch (argc) {
475 6e6049be 2019-07-23 stsp case 0:
476 6e6049be 2019-07-23 stsp repo_path = realpath(GOT_REPO_PATH, NULL);
477 6e6049be 2019-07-23 stsp break;
478 6e6049be 2019-07-23 stsp case 1:
479 6e6049be 2019-07-23 stsp repo_path = realpath(argv[0], NULL);
480 6e6049be 2019-07-23 stsp break;
481 6e6049be 2019-07-23 stsp default:
482 82f2fb69 2018-01-26 stsp usage();
483 4027f31a 2017-11-04 stsp return 1;
484 4027f31a 2017-11-04 stsp }
485 6e6049be 2019-07-23 stsp if (repo_path == NULL) {
486 6e6049be 2019-07-23 stsp fprintf(stderr, "realpath: %s\n", strerror(errno));
487 6e6049be 2019-07-23 stsp return 1;
488 6e6049be 2019-07-23 stsp }
489 4027f31a 2017-11-04 stsp
490 cb44a3e5 2019-01-04 stsp error = apply_unveil(repo_path);
491 cb44a3e5 2019-01-04 stsp if (error) {
492 6e6049be 2019-07-23 stsp fprintf(stderr, "unveil: %s\n", error->msg);
493 6e6049be 2019-07-23 stsp free(repo_path);
494 cb44a3e5 2019-01-04 stsp return 1;
495 cb44a3e5 2019-01-04 stsp }
496 cb44a3e5 2019-01-04 stsp
497 044e7393 2018-02-11 stsp RUN_TEST(repo_read_tree(repo_path), "read_tree");
498 bfab4d9a 2017-11-12 stsp RUN_TEST(repo_read_log(repo_path), "read_log");
499 68482ea3 2017-11-27 stsp RUN_TEST(repo_read_blob(repo_path), "read_blob");
500 7d283eee 2017-11-29 stsp RUN_TEST(repo_diff_blob(repo_path), "diff_blob");
501 98abbc84 2017-11-30 stsp RUN_TEST(repo_diff_tree(repo_path), "diff_tree");
502 4027f31a 2017-11-04 stsp
503 6e6049be 2019-07-23 stsp free(repo_path);
504 4027f31a 2017-11-04 stsp return failure ? 1 : 0;
505 4027f31a 2017-11-04 stsp }