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 */
18 #include <limits.h>
19 #include <stdarg.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <err.h>
25 #include <zlib.h>
26 #include <time.h>
28 #include "got_error.h"
29 #include "got_object.h"
30 #include "got_path.h"
31 #include "got_fetch.h"
32 #include "got_dial.h"
34 #include "got_lib_object_idset.h"
35 #include "got_lib_sha1.h"
36 #include "got_lib_inflate.h"
37 #include "got_lib_delta.h"
39 #ifndef nitems
40 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
41 #endif
43 static int verbose;
44 static int quiet;
46 void
47 test_printf(char *fmt, ...)
48 {
49 va_list ap;
51 if (!verbose)
52 return;
54 va_start(ap, fmt);
55 vprintf(fmt, ap);
56 va_end(ap);
57 }
59 static int
60 fetch_parse_uri(void)
61 {
62 const struct got_error *err = NULL;
63 struct parse_uri_test {
64 const char *uri;
65 const char *proto;
66 const char *host;
67 const char *port;
68 const char *server_path;
69 const char *repo_name;
70 int errcode;
71 } test_data[] = {
72 { "", NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
73 { "git:", NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
74 { "git://localhost/",
75 NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
76 { "git://localhost////",
77 NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
78 { "git://127.0.0.1/git/",
79 NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
80 { "git:///127.0.0.1/git/",
81 NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
82 { "/127.0.0.1:/git/",
83 NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
85 { "git://127.0.0.1/git/myrepo",
86 "git", "127.0.0.1", NULL,
87 "/git/myrepo", "myrepo", GOT_ERR_OK },
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 { "http://127.0.0.1/git/myrepo",
95 "http", "127.0.0.1", NULL,
96 "/git/myrepo", "myrepo", GOT_ERR_OK },
97 { "gopher://127.0.0.1/git/myrepo",
98 "gopher", "127.0.0.1", NULL,
99 "/git/myrepo", "myrepo", GOT_ERR_OK },
101 { "git://127.0.0.1:22/git/myrepo",
102 "git", "127.0.0.1", "22", "/git/myrepo", "myrepo",
103 GOT_ERR_OK },
104 { "git://127.0.0.1/git/repos/foo/bar/myrepo.git",
105 "git", "127.0.0.1", NULL,
106 "/git/repos/foo/bar/myrepo.git", "myrepo", GOT_ERR_OK },
107 { "https://127.0.0.1/git/repos/foo/../bar/myrepo.git",
108 "https", "127.0.0.1", NULL,
109 "/git/repos/foo/../bar/myrepo.git", "myrepo",
110 GOT_ERR_OK },
112 { "git+ssh://127.0.0.1:22/git/myrepo",
113 "git+ssh", "127.0.0.1", "22", "/git/myrepo", "myrepo",
114 GOT_ERR_OK },
115 { "ssh://127.0.0.1:22/git/myrepo",
116 "ssh", "127.0.0.1", "22", "/git/myrepo", "myrepo",
117 GOT_ERR_OK },
119 { "127.0.0.1:git/myrepo",
120 "ssh", "127.0.0.1", NULL, "git/myrepo", "myrepo",
121 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:22/git/myrepo",
126 "ssh", "127.0.0.1", NULL, "22/git/myrepo", "myrepo",
127 GOT_ERR_OK },
128 };
129 size_t i;
131 for (i = 0; i < nitems(test_data); i++) {
132 const char *uri = test_data[i].uri;
133 const char *expected_proto = test_data[i].proto;
134 const char *expected_host = test_data[i].host;
135 const char *expected_port = test_data[i].port;
136 const char *expected_server_path = test_data[i].server_path;
137 const char *expected_repo_name = test_data[i].repo_name;
138 char *proto, *host, *port, *server_path, *repo_name;
140 err = got_dial_parse_uri(&proto, &host, &port, &server_path,
141 &repo_name, uri);
142 if (err && err->code != test_data[i].errcode) {
143 test_printf("%d: error code %d; expected %d\n",
144 i, err->code, test_data[i].errcode);
145 return 0;
148 if (expected_proto == NULL && proto != NULL) {
149 test_printf("%d: proto %s; expected NULL\n", i, proto);
150 return 0;
152 if (expected_host == NULL && host != NULL) {
153 test_printf("%d: host %s; expected NULL\n", i, host);
154 return 0;
156 if (expected_port == NULL && port != NULL) {
157 test_printf("%d: port %s; expected NULL\n", i, port);
158 return 0;
160 if (expected_server_path == NULL && server_path != NULL) {
161 test_printf("%d: server path %s; expected NULL\n", i,
162 server_path);
163 return 0;
165 if (expected_repo_name == NULL && repo_name != NULL) {
166 test_printf("%d: repo name %s; expected NULL\n", i,
167 repo_name);
168 return 0;
171 if (expected_proto != NULL && proto == NULL) {
172 test_printf("%d: proto NULL; expected %s\n", i,
173 expected_proto);
174 return 0;
176 if (expected_host != NULL && host == NULL) {
177 test_printf("%d: host NULL; expected %s\n", i,
178 expected_host);
179 return 0;
181 if (expected_port != NULL && port == NULL) {
182 test_printf("%d: port NULL; expected %s\n", i,
183 expected_port);
184 return 0;
186 if (expected_server_path != NULL && server_path == NULL) {
187 test_printf("%d: server path %s; expected %s\n", i,
188 expected_server_path);
189 return 0;
191 if (expected_repo_name != NULL && repo_name == NULL) {
192 test_printf("%d: repo name NULL; expected %s\n", i,
193 repo_name);
194 return 0;
197 if (expected_proto != NULL && strcmp(expected_proto, proto)) {
198 test_printf("%d: proto %s; expected %s\n", i, proto,
199 expected_proto);
200 return 0;
203 if (expected_host != NULL && strcmp(expected_host, host)) {
204 test_printf("%d: host %s; expected %s\n", i, host,
205 expected_host);
206 return 0;
209 if (expected_port != NULL && strcmp(expected_port, port)) {
210 test_printf("%d: port %s; expected %s\n", i, port,
211 expected_port);
212 return 0;
215 if (expected_server_path != NULL &&
216 strcmp(expected_server_path, server_path)) {
217 test_printf("%d: server_path %s; expected %s\n", i,
218 server_path, expected_server_path);
219 return 0;
222 if (expected_repo_name != NULL &&
223 strcmp(expected_repo_name, repo_name)) {
224 test_printf("%d: repo_name %s; expected %s\n", i,
225 repo_name, expected_repo_name);
226 return 0;
229 free(proto);
230 proto = NULL;
231 free(host);
232 host = NULL;
233 free(port);
234 port = NULL;
235 free(server_path);
236 server_path = NULL;
237 free(repo_name);
238 repo_name = NULL;
241 return 1;
244 #define RUN_TEST(expr, name) \
245 { test_ok = (expr); \
246 if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
247 failure = (failure || !test_ok); }
249 void
250 usage(void)
252 fprintf(stderr, "usage: fetch_test [-v] [-q]\n");
255 int
256 main(int argc, char *argv[])
258 int test_ok = 0, failure = 0;
259 int ch;
261 #ifndef PROFILE
262 if (pledge("stdio", NULL) == -1)
263 err(1, "pledge");
264 #endif
266 while ((ch = getopt(argc, argv, "vq")) != -1) {
267 switch (ch) {
268 case 'v':
269 verbose = 1;
270 quiet = 0;
271 break;
272 case 'q':
273 quiet = 1;
274 verbose = 0;
275 break;
276 default:
277 usage();
278 return 1;
281 argc -= optind;
282 argv += optind;
284 RUN_TEST(fetch_parse_uri(), "fetch_parse_uri");
286 return failure ? 1 : 0;