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 <zlib.h>
27 #include <time.h>
29 #include "got_error.h"
30 #include "got_object.h"
31 #include "got_path.h"
32 #include "got_fetch.h"
33 #include "got_dial.h"
35 #include "got_lib_object_idset.h"
36 #include "got_lib_sha1.h"
37 #include "got_lib_inflate.h"
38 #include "got_lib_delta.h"
40 #ifndef nitems
41 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
42 #endif
44 static int verbose;
45 static int quiet;
47 static void
48 test_printf(const char *fmt, ...)
49 {
50 va_list ap;
52 if (!verbose)
53 return;
55 va_start(ap, fmt);
56 vprintf(fmt, ap);
57 va_end(ap);
58 }
60 static int
61 fetch_parse_uri(void)
62 {
63 const struct got_error *err = NULL;
64 const struct parse_uri_test {
65 const char *uri;
66 const char *proto;
67 const char *host;
68 const char *port;
69 const char *server_path;
70 const char *repo_name;
71 int errcode;
72 } test_data[] = {
73 { "", NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
74 { "git:", NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
75 { "git://localhost/",
76 NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
77 { "git://localhost////",
78 NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
79 { "git://127.0.0.1/git/",
80 "git", "127.0.0.1", NULL, "/git", "git", GOT_ERR_OK },
81 { "git:///127.0.0.1/git/",
82 NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
83 { "/127.0.0.1:/git/",
84 NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
86 { "git://127.0.0.1/git/myrepo",
87 "git", "127.0.0.1", NULL,
88 "/git/myrepo", "myrepo", GOT_ERR_OK },
89 { "git://127.0.0.1//git/myrepo",
90 "git", "127.0.0.1", NULL,
91 "/git/myrepo", "myrepo", GOT_ERR_OK },
92 { "git://127.0.0.1/////git//myrepo",
93 "git", "127.0.0.1", NULL,
94 "/git//myrepo", "myrepo", GOT_ERR_OK },
95 { "http://127.0.0.1/git/myrepo",
96 "http", "127.0.0.1", NULL,
97 "/git/myrepo", "myrepo", GOT_ERR_OK },
98 { "gopher://127.0.0.1/git/myrepo",
99 "gopher", "127.0.0.1", NULL,
100 "/git/myrepo", "myrepo", GOT_ERR_OK },
102 { "git://127.0.0.1:22/git/myrepo",
103 "git", "127.0.0.1", "22", "/git/myrepo", "myrepo",
104 GOT_ERR_OK },
105 { "git://127.0.0.1/git/repos/foo/bar/myrepo.git",
106 "git", "127.0.0.1", NULL,
107 "/git/repos/foo/bar/myrepo.git", "myrepo", GOT_ERR_OK },
108 { "https://127.0.0.1/git/repos/foo/../bar/myrepo.git",
109 "https", "127.0.0.1", NULL,
110 "/git/repos/foo/../bar/myrepo.git", "myrepo",
111 GOT_ERR_OK },
113 { "git+ssh://127.0.0.1:22/git/myrepo",
114 "git+ssh", "127.0.0.1", "22", "/git/myrepo", "myrepo",
115 GOT_ERR_OK },
116 { "ssh://127.0.0.1:22/git/myrepo",
117 "ssh", "127.0.0.1", "22", "/git/myrepo", "myrepo",
118 GOT_ERR_OK },
120 { "127.0.0.1:git/myrepo",
121 "ssh", "127.0.0.1", NULL, "git/myrepo", "myrepo",
122 GOT_ERR_OK },
123 { "127.0.0.1:/git/myrepo",
124 "ssh", "127.0.0.1", NULL, "/git/myrepo", "myrepo",
125 GOT_ERR_OK },
126 { "127.0.0.1:22/git/myrepo",
127 "ssh", "127.0.0.1", NULL, "22/git/myrepo", "myrepo",
128 GOT_ERR_OK },
129 };
130 size_t i;
132 for (i = 0; i < nitems(test_data); i++) {
133 const char *uri = test_data[i].uri;
134 const char *expected_proto = test_data[i].proto;
135 const char *expected_host = test_data[i].host;
136 const char *expected_port = test_data[i].port;
137 const char *expected_server_path = test_data[i].server_path;
138 const char *expected_repo_name = test_data[i].repo_name;
139 char *proto, *host, *port, *server_path, *repo_name;
141 err = got_dial_parse_uri(&proto, &host, &port, &server_path,
142 &repo_name, uri);
143 if (err && err->code != test_data[i].errcode) {
144 test_printf("%d: error code %d; expected %d\n",
145 i, err->code, test_data[i].errcode);
146 return 0;
149 if (expected_proto == NULL && proto != NULL) {
150 test_printf("%d: proto %s; expected NULL\n", i, proto);
151 return 0;
153 if (expected_host == NULL && host != NULL) {
154 test_printf("%d: host %s; expected NULL\n", i, host);
155 return 0;
157 if (expected_port == NULL && port != NULL) {
158 test_printf("%d: port %s; expected NULL\n", i, port);
159 return 0;
161 if (expected_server_path == NULL && server_path != NULL) {
162 test_printf("%d: server path %s; expected NULL\n", i,
163 server_path);
164 return 0;
166 if (expected_repo_name == NULL && repo_name != NULL) {
167 test_printf("%d: repo name %s; expected NULL\n", i,
168 repo_name);
169 return 0;
172 if (expected_proto != NULL && proto == NULL) {
173 test_printf("%d: proto NULL; expected %s\n", i,
174 expected_proto);
175 return 0;
177 if (expected_host != NULL && host == NULL) {
178 test_printf("%d: host NULL; expected %s\n", i,
179 expected_host);
180 return 0;
182 if (expected_port != NULL && port == NULL) {
183 test_printf("%d: port NULL; expected %s\n", i,
184 expected_port);
185 return 0;
187 if (expected_server_path != NULL && server_path == NULL) {
188 test_printf("%d: server path %s; expected %s\n", i,
189 expected_server_path);
190 return 0;
192 if (expected_repo_name != NULL && repo_name == NULL) {
193 test_printf("%d: repo name NULL; expected %s\n", i,
194 repo_name);
195 return 0;
198 if (expected_proto != NULL && strcmp(expected_proto, proto)) {
199 test_printf("%d: proto %s; expected %s\n", i, proto,
200 expected_proto);
201 return 0;
204 if (expected_host != NULL && strcmp(expected_host, host)) {
205 test_printf("%d: host %s; expected %s\n", i, host,
206 expected_host);
207 return 0;
210 if (expected_port != NULL && strcmp(expected_port, port)) {
211 test_printf("%d: port %s; expected %s\n", i, port,
212 expected_port);
213 return 0;
216 if (expected_server_path != NULL &&
217 strcmp(expected_server_path, server_path)) {
218 test_printf("%d: server_path %s; expected %s\n", i,
219 server_path, expected_server_path);
220 return 0;
223 if (expected_repo_name != NULL &&
224 strcmp(expected_repo_name, repo_name)) {
225 test_printf("%d: repo_name %s; expected %s\n", i,
226 repo_name, expected_repo_name);
227 return 0;
230 free(proto);
231 proto = NULL;
232 free(host);
233 host = NULL;
234 free(port);
235 port = NULL;
236 free(server_path);
237 server_path = NULL;
238 free(repo_name);
239 repo_name = NULL;
242 return 1;
245 #define RUN_TEST(expr, name) \
246 { test_ok = (expr); \
247 if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
248 failure = (failure || !test_ok); }
250 static void
251 usage(void)
253 fprintf(stderr, "usage: fetch_test [-v] [-q]\n");
256 int
257 main(int argc, char *argv[])
259 int test_ok = 0, failure = 0;
260 int ch;
262 #ifndef PROFILE
263 if (pledge("stdio", NULL) == -1)
264 err(1, "pledge");
265 #endif
267 while ((ch = getopt(argc, argv, "qv")) != -1) {
268 switch (ch) {
269 case 'q':
270 quiet = 1;
271 verbose = 0;
272 break;
273 case 'v':
274 verbose = 1;
275 quiet = 0;
276 break;
277 default:
278 usage();
279 return 1;
282 argc -= optind;
283 argv += optind;
285 RUN_TEST(fetch_parse_uri(), "fetch_parse_uri");
287 return failure ? 1 : 0;