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 <zlib.h>
28 #include <time.h>
30 #include "got_error.h"
31 #include "got_object.h"
32 #include "got_path.h"
33 #include "got_fetch.h"
34 #include "got_dial.h"
36 #include "got_lib_object_idset.h"
37 #include "got_lib_sha1.h"
38 #include "got_lib_inflate.h"
39 #include "got_lib_delta.h"
41 #ifndef nitems
42 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
43 #endif
45 static int verbose;
46 static int quiet;
48 static void
49 test_printf(const char *fmt, ...)
50 {
51 va_list ap;
53 if (!verbose)
54 return;
56 va_start(ap, fmt);
57 vprintf(fmt, ap);
58 va_end(ap);
59 }
61 static int
62 fetch_parse_uri(void)
63 {
64 const struct got_error *err = NULL;
65 const struct parse_uri_test {
66 const char *uri;
67 const char *proto;
68 const char *host;
69 const char *port;
70 const char *server_path;
71 const char *repo_name;
72 int errcode;
73 } test_data[] = {
74 { "", NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
75 { "git:", NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
76 { "git://localhost/",
77 NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
78 { "git://localhost////",
79 NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
80 { "git://127.0.0.1/git/",
81 "git", "127.0.0.1", NULL, "/git", "git", GOT_ERR_OK },
82 { "git:///127.0.0.1/git/",
83 NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
84 { "/127.0.0.1:/git/",
85 NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
87 { "git://127.0.0.1/git/myrepo",
88 "git", "127.0.0.1", NULL,
89 "/git/myrepo", "myrepo", GOT_ERR_OK },
90 { "git://127.0.0.1//git/myrepo",
91 "git", "127.0.0.1", NULL,
92 "/git/myrepo", "myrepo", GOT_ERR_OK },
93 { "git://127.0.0.1/////git//myrepo",
94 "git", "127.0.0.1", NULL,
95 "/git//myrepo", "myrepo", GOT_ERR_OK },
96 { "http://127.0.0.1/git/myrepo",
97 "http", "127.0.0.1", NULL,
98 "/git/myrepo", "myrepo", GOT_ERR_OK },
99 { "gopher://127.0.0.1/git/myrepo",
100 "gopher", "127.0.0.1", NULL,
101 "/git/myrepo", "myrepo", GOT_ERR_OK },
103 { "git://127.0.0.1:22/git/myrepo",
104 "git", "127.0.0.1", "22", "/git/myrepo", "myrepo",
105 GOT_ERR_OK },
106 { "git://127.0.0.1/git/repos/foo/bar/myrepo.git",
107 "git", "127.0.0.1", NULL,
108 "/git/repos/foo/bar/myrepo.git", "myrepo", GOT_ERR_OK },
109 { "https://127.0.0.1/git/repos/foo/../bar/myrepo.git",
110 "https", "127.0.0.1", NULL,
111 "/git/repos/foo/../bar/myrepo.git", "myrepo",
112 GOT_ERR_OK },
114 { "git+ssh://127.0.0.1:22/git/myrepo",
115 "git+ssh", "127.0.0.1", "22", "/git/myrepo", "myrepo",
116 GOT_ERR_OK },
117 { "ssh://127.0.0.1:22/git/myrepo",
118 "ssh", "127.0.0.1", "22", "/git/myrepo", "myrepo",
119 GOT_ERR_OK },
121 { "127.0.0.1:git/myrepo",
122 "ssh", "127.0.0.1", NULL, "git/myrepo", "myrepo",
123 GOT_ERR_OK },
124 { "127.0.0.1:/git/myrepo",
125 "ssh", "127.0.0.1", NULL, "/git/myrepo", "myrepo",
126 GOT_ERR_OK },
127 { "127.0.0.1:22/git/myrepo",
128 "ssh", "127.0.0.1", NULL, "22/git/myrepo", "myrepo",
129 GOT_ERR_OK },
130 };
131 size_t i;
133 for (i = 0; i < nitems(test_data); i++) {
134 const char *uri = test_data[i].uri;
135 const char *expected_proto = test_data[i].proto;
136 const char *expected_host = test_data[i].host;
137 const char *expected_port = test_data[i].port;
138 const char *expected_server_path = test_data[i].server_path;
139 const char *expected_repo_name = test_data[i].repo_name;
140 char *proto, *host, *port, *server_path, *repo_name;
142 err = got_dial_parse_uri(&proto, &host, &port, &server_path,
143 &repo_name, uri);
144 if (err && err->code != test_data[i].errcode) {
145 test_printf("%d: error code %d; expected %d\n",
146 i, err->code, test_data[i].errcode);
147 return 0;
150 if (expected_proto == NULL && proto != NULL) {
151 test_printf("%d: proto %s; expected NULL\n", i, proto);
152 return 0;
154 if (expected_host == NULL && host != NULL) {
155 test_printf("%d: host %s; expected NULL\n", i, host);
156 return 0;
158 if (expected_port == NULL && port != NULL) {
159 test_printf("%d: port %s; expected NULL\n", i, port);
160 return 0;
162 if (expected_server_path == NULL && server_path != NULL) {
163 test_printf("%d: server path %s; expected NULL\n", i,
164 server_path);
165 return 0;
167 if (expected_repo_name == NULL && repo_name != NULL) {
168 test_printf("%d: repo name %s; expected NULL\n", i,
169 repo_name);
170 return 0;
173 if (expected_proto != NULL && proto == NULL) {
174 test_printf("%d: proto NULL; expected %s\n", i,
175 expected_proto);
176 return 0;
178 if (expected_host != NULL && host == NULL) {
179 test_printf("%d: host NULL; expected %s\n", i,
180 expected_host);
181 return 0;
183 if (expected_port != NULL && port == NULL) {
184 test_printf("%d: port NULL; expected %s\n", i,
185 expected_port);
186 return 0;
188 if (expected_server_path != NULL && server_path == NULL) {
189 test_printf("%d: server path %s; expected %s\n", i,
190 expected_server_path);
191 return 0;
193 if (expected_repo_name != NULL && repo_name == NULL) {
194 test_printf("%d: repo name NULL; expected %s\n", i,
195 repo_name);
196 return 0;
199 if (expected_proto != NULL && strcmp(expected_proto, proto)) {
200 test_printf("%d: proto %s; expected %s\n", i, proto,
201 expected_proto);
202 return 0;
205 if (expected_host != NULL && strcmp(expected_host, host)) {
206 test_printf("%d: host %s; expected %s\n", i, host,
207 expected_host);
208 return 0;
211 if (expected_port != NULL && strcmp(expected_port, port)) {
212 test_printf("%d: port %s; expected %s\n", i, port,
213 expected_port);
214 return 0;
217 if (expected_server_path != NULL &&
218 strcmp(expected_server_path, server_path)) {
219 test_printf("%d: server_path %s; expected %s\n", i,
220 server_path, expected_server_path);
221 return 0;
224 if (expected_repo_name != NULL &&
225 strcmp(expected_repo_name, repo_name)) {
226 test_printf("%d: repo_name %s; expected %s\n", i,
227 repo_name, expected_repo_name);
228 return 0;
231 free(proto);
232 proto = NULL;
233 free(host);
234 host = NULL;
235 free(port);
236 port = NULL;
237 free(server_path);
238 server_path = NULL;
239 free(repo_name);
240 repo_name = NULL;
243 return 1;
246 #define RUN_TEST(expr, name) \
247 { test_ok = (expr); \
248 if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
249 failure = (failure || !test_ok); }
251 static void
252 usage(void)
254 fprintf(stderr, "usage: fetch_test [-v] [-q]\n");
257 int
258 main(int argc, char *argv[])
260 int test_ok = 0, failure = 0;
261 int ch;
263 #ifndef PROFILE
264 if (pledge("stdio", NULL) == -1)
265 err(1, "pledge");
266 #endif
268 while ((ch = getopt(argc, argv, "qv")) != -1) {
269 switch (ch) {
270 case 'q':
271 quiet = 1;
272 verbose = 0;
273 break;
274 case 'v':
275 verbose = 1;
276 quiet = 0;
277 break;
278 default:
279 usage();
280 return 1;
283 argc -= optind;
284 argv += optind;
286 RUN_TEST(fetch_parse_uri(), "fetch_parse_uri");
288 return failure ? 1 : 0;