Blob


1 /*
2 * Copyright (c) 2020 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/queue.h>
19 #include <limits.h>
20 #include <stdarg.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <err.h>
26 #include <sha1.h>
27 #include <sha2.h>
28 #include <zlib.h>
29 #include <time.h>
31 #include "got_error.h"
32 #include "got_object.h"
33 #include "got_path.h"
34 #include "got_fetch.h"
35 #include "got_dial.h"
37 #include "got_lib_object_idset.h"
38 #include "got_lib_hash.h"
39 #include "got_lib_inflate.h"
40 #include "got_lib_delta.h"
42 #ifndef nitems
43 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
44 #endif
46 static int verbose;
47 static int quiet;
49 static void
50 test_printf(const char *fmt, ...)
51 {
52 va_list ap;
54 if (!verbose)
55 return;
57 va_start(ap, fmt);
58 vprintf(fmt, ap);
59 va_end(ap);
60 }
62 static int
63 fetch_parse_uri(void)
64 {
65 const struct got_error *err = NULL;
66 const struct parse_uri_test {
67 const char *uri;
68 const char *proto;
69 const char *host;
70 const char *port;
71 const char *server_path;
72 const char *repo_name;
73 int errcode;
74 } test_data[] = {
75 { "", NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
76 { "git:", NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
77 { "git://localhost/",
78 NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
79 { "git://localhost////",
80 NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
81 { "git://127.0.0.1/git/",
82 "git", "127.0.0.1", NULL, "/git", "git", GOT_ERR_OK },
83 { "git:///127.0.0.1/git/",
84 NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
85 { "/127.0.0.1:/git/",
86 NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
88 { "git://127.0.0.1/git/myrepo",
89 "git", "127.0.0.1", NULL,
90 "/git/myrepo", "myrepo", GOT_ERR_OK },
91 { "git://127.0.0.1//git/myrepo",
92 "git", "127.0.0.1", NULL,
93 "/git/myrepo", "myrepo", GOT_ERR_OK },
94 { "git://127.0.0.1/////git//myrepo",
95 "git", "127.0.0.1", NULL,
96 "/git//myrepo", "myrepo", GOT_ERR_OK },
97 { "http://127.0.0.1/git/myrepo",
98 "http", "127.0.0.1", NULL,
99 "/git/myrepo", "myrepo", GOT_ERR_OK },
100 { "gopher://127.0.0.1/git/myrepo",
101 "gopher", "127.0.0.1", NULL,
102 "/git/myrepo", "myrepo", GOT_ERR_OK },
104 { "git://127.0.0.1:22/git/myrepo",
105 "git", "127.0.0.1", "22", "/git/myrepo", "myrepo",
106 GOT_ERR_OK },
107 { "git://127.0.0.1/git/repos/foo/bar/myrepo.git",
108 "git", "127.0.0.1", NULL,
109 "/git/repos/foo/bar/myrepo.git", "myrepo", GOT_ERR_OK },
110 { "https://127.0.0.1/git/repos/foo/../bar/myrepo.git",
111 "https", "127.0.0.1", NULL,
112 "/git/repos/foo/../bar/myrepo.git", "myrepo",
113 GOT_ERR_OK },
115 { "git+ssh://127.0.0.1:22/git/myrepo",
116 "git+ssh", "127.0.0.1", "22", "/git/myrepo", "myrepo",
117 GOT_ERR_OK },
118 { "ssh://127.0.0.1:22/git/myrepo",
119 "ssh", "127.0.0.1", "22", "/git/myrepo", "myrepo",
120 GOT_ERR_OK },
122 { "127.0.0.1:git/myrepo",
123 "ssh", "127.0.0.1", NULL, "git/myrepo", "myrepo",
124 GOT_ERR_OK },
125 { "127.0.0.1:/git/myrepo",
126 "ssh", "127.0.0.1", NULL, "/git/myrepo", "myrepo",
127 GOT_ERR_OK },
128 { "127.0.0.1:22/git/myrepo",
129 "ssh", "127.0.0.1", NULL, "22/git/myrepo", "myrepo",
130 GOT_ERR_OK },
131 };
132 size_t i;
134 for (i = 0; i < nitems(test_data); i++) {
135 const char *uri = test_data[i].uri;
136 const char *expected_proto = test_data[i].proto;
137 const char *expected_host = test_data[i].host;
138 const char *expected_port = test_data[i].port;
139 const char *expected_server_path = test_data[i].server_path;
140 const char *expected_repo_name = test_data[i].repo_name;
141 char *proto, *host, *port, *server_path, *repo_name;
143 err = got_dial_parse_uri(&proto, &host, &port, &server_path,
144 &repo_name, uri);
145 if (err && err->code != test_data[i].errcode) {
146 test_printf("%d: error code %d; expected %d\n",
147 i, err->code, test_data[i].errcode);
148 return 0;
151 if (expected_proto == NULL && proto != NULL) {
152 test_printf("%d: proto %s; expected NULL\n", i, proto);
153 return 0;
155 if (expected_host == NULL && host != NULL) {
156 test_printf("%d: host %s; expected NULL\n", i, host);
157 return 0;
159 if (expected_port == NULL && port != NULL) {
160 test_printf("%d: port %s; expected NULL\n", i, port);
161 return 0;
163 if (expected_server_path == NULL && server_path != NULL) {
164 test_printf("%d: server path %s; expected NULL\n", i,
165 server_path);
166 return 0;
168 if (expected_repo_name == NULL && repo_name != NULL) {
169 test_printf("%d: repo name %s; expected NULL\n", i,
170 repo_name);
171 return 0;
174 if (expected_proto != NULL && proto == NULL) {
175 test_printf("%d: proto NULL; expected %s\n", i,
176 expected_proto);
177 return 0;
179 if (expected_host != NULL && host == NULL) {
180 test_printf("%d: host NULL; expected %s\n", i,
181 expected_host);
182 return 0;
184 if (expected_port != NULL && port == NULL) {
185 test_printf("%d: port NULL; expected %s\n", i,
186 expected_port);
187 return 0;
189 if (expected_server_path != NULL && server_path == NULL) {
190 test_printf("%d: server path %s; expected %s\n", i,
191 expected_server_path);
192 return 0;
194 if (expected_repo_name != NULL && repo_name == NULL) {
195 test_printf("%d: repo name NULL; expected %s\n", i,
196 repo_name);
197 return 0;
200 if (expected_proto != NULL && strcmp(expected_proto, proto)) {
201 test_printf("%d: proto %s; expected %s\n", i, proto,
202 expected_proto);
203 return 0;
206 if (expected_host != NULL && strcmp(expected_host, host)) {
207 test_printf("%d: host %s; expected %s\n", i, host,
208 expected_host);
209 return 0;
212 if (expected_port != NULL && strcmp(expected_port, port)) {
213 test_printf("%d: port %s; expected %s\n", i, port,
214 expected_port);
215 return 0;
218 if (expected_server_path != NULL &&
219 strcmp(expected_server_path, server_path)) {
220 test_printf("%d: server_path %s; expected %s\n", i,
221 server_path, expected_server_path);
222 return 0;
225 if (expected_repo_name != NULL &&
226 strcmp(expected_repo_name, repo_name)) {
227 test_printf("%d: repo_name %s; expected %s\n", i,
228 repo_name, expected_repo_name);
229 return 0;
232 free(proto);
233 proto = NULL;
234 free(host);
235 host = NULL;
236 free(port);
237 port = NULL;
238 free(server_path);
239 server_path = NULL;
240 free(repo_name);
241 repo_name = NULL;
244 return 1;
247 #define RUN_TEST(expr, name) \
248 { test_ok = (expr); \
249 if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
250 failure = (failure || !test_ok); }
252 static void
253 usage(void)
255 fprintf(stderr, "usage: fetch_test [-v] [-q]\n");
258 int
259 main(int argc, char *argv[])
261 int test_ok = 0, failure = 0;
262 int ch;
264 #ifndef PROFILE
265 if (pledge("stdio", NULL) == -1)
266 err(1, "pledge");
267 #endif
269 while ((ch = getopt(argc, argv, "qv")) != -1) {
270 switch (ch) {
271 case 'q':
272 quiet = 1;
273 verbose = 0;
274 break;
275 case 'v':
276 verbose = 1;
277 quiet = 0;
278 break;
279 default:
280 usage();
281 return 1;
284 argc -= optind;
285 argv += optind;
287 RUN_TEST(fetch_parse_uri(), "fetch_parse_uri");
289 return failure ? 1 : 0;