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"
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 void
48 test_printf(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 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 NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
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 { "http://127.0.0.1/git/myrepo",
90 "http", "127.0.0.1", NULL,
91 "/git/myrepo", "myrepo", GOT_ERR_OK },
92 { "gopher://127.0.0.1/git/myrepo",
93 "gopher", "127.0.0.1", NULL,
94 "/git/myrepo", "myrepo", GOT_ERR_OK },
96 { "git://127.0.0.1:22/git/myrepo",
97 "git", "127.0.0.1", "22", "/git/myrepo", "myrepo",
98 GOT_ERR_OK },
99 { "git://127.0.0.1/git/repos/foo/bar/myrepo.git",
100 "git", "127.0.0.1", NULL,
101 "/git/repos/foo/bar/myrepo.git", "myrepo", GOT_ERR_OK },
102 { "https://127.0.0.1/git/repos/foo/../bar/myrepo.git",
103 "https", "127.0.0.1", NULL,
104 "/git/repos/foo/../bar/myrepo.git", "myrepo",
105 GOT_ERR_OK },
107 { "git+ssh://127.0.0.1:22/git/myrepo",
108 "git+ssh", "127.0.0.1", "22", "/git/myrepo", "myrepo",
109 GOT_ERR_OK },
110 { "ssh://127.0.0.1:22/git/myrepo",
111 "ssh", "127.0.0.1", "22", "/git/myrepo", "myrepo",
112 GOT_ERR_OK },
114 { "127.0.0.1:git/myrepo",
115 "ssh", "127.0.0.1", NULL, "git/myrepo", "myrepo",
116 GOT_ERR_OK },
117 { "127.0.0.1:/git/myrepo",
118 "ssh", "127.0.0.1", NULL, "/git/myrepo", "myrepo",
119 GOT_ERR_OK },
120 { "127.0.0.1:22/git/myrepo",
121 "ssh", "127.0.0.1", NULL, "22/git/myrepo", "myrepo",
122 GOT_ERR_OK },
123 };
124 int i;
126 for (i = 0; i < nitems(test_data); i++) {
127 const char *uri = test_data[i].uri;
128 const char *expected_proto = test_data[i].proto;
129 const char *expected_host = test_data[i].host;
130 const char *expected_port = test_data[i].port;
131 const char *expected_server_path = test_data[i].server_path;
132 const char *expected_repo_name = test_data[i].repo_name;
133 char *proto, *host, *port, *server_path, *repo_name;
135 err = got_fetch_parse_uri(&proto, &host, &port, &server_path,
136 &repo_name, uri);
137 if (err && err->code != test_data[i].errcode) {
138 test_printf("%d: error code %d; expected %d\n",
139 i, err->code, test_data[i].errcode);
140 return 0;
143 if (expected_proto == NULL && proto != NULL) {
144 test_printf("%d: proto %s; expected NULL\n", i, proto);
145 return 0;
147 if (expected_host == NULL && host != NULL) {
148 test_printf("%d: host %s; expected NULL\n", i, host);
149 return 0;
151 if (expected_port == NULL && port != NULL) {
152 test_printf("%d: port %s; expected NULL\n", i, port);
153 return 0;
155 if (expected_server_path == NULL && server_path != NULL) {
156 test_printf("%d: server path %s; expected NULL\n", i,
157 server_path);
158 return 0;
160 if (expected_repo_name == NULL && repo_name != NULL) {
161 test_printf("%d: repo name %s; expected NULL\n", i,
162 repo_name);
163 return 0;
166 if (expected_proto != NULL && proto == NULL) {
167 test_printf("%d: proto NULL; expected %s\n", i,
168 expected_proto);
169 return 0;
171 if (expected_host != NULL && host == NULL) {
172 test_printf("%d: host NULL; expected %s\n", i,
173 expected_host);
174 return 0;
176 if (expected_port != NULL && port == NULL) {
177 test_printf("%d: port NULL; expected %s\n", i,
178 expected_port);
179 return 0;
181 if (expected_server_path != NULL && server_path == NULL) {
182 test_printf("%d: server path %s; expected %s\n", i,
183 expected_server_path);
184 return 0;
186 if (expected_repo_name != NULL && repo_name == NULL) {
187 test_printf("%d: repo name NULL; expected %s\n", i,
188 repo_name);
189 return 0;
192 if (expected_proto != NULL && strcmp(expected_proto, proto)) {
193 test_printf("%d: proto %s; expected %s\n", i, proto,
194 expected_proto);
195 return 0;
198 if (expected_host != NULL && strcmp(expected_host, host)) {
199 test_printf("%d: host %s; expected %s\n", i, host,
200 expected_host);
201 return 0;
204 if (expected_port != NULL && strcmp(expected_port, port)) {
205 test_printf("%d: port %s; expected %s\n", i, port,
206 expected_port);
207 return 0;
210 if (expected_server_path != NULL &&
211 strcmp(expected_server_path, server_path)) {
212 test_printf("%d: server_path %s; expected %s\n", i,
213 server_path, expected_server_path);
214 return 0;
217 if (expected_repo_name != NULL &&
218 strcmp(expected_repo_name, repo_name)) {
219 test_printf("%d: repo_name %s; expected %s\n", i,
220 repo_name, expected_repo_name);
221 return 0;
224 free(proto);
225 proto = NULL;
226 free(host);
227 host = NULL;
228 free(port);
229 port = NULL;
230 free(server_path);
231 server_path = NULL;
232 free(repo_name);
233 repo_name = NULL;
236 return 1;
239 #define RUN_TEST(expr, name) \
240 { test_ok = (expr); \
241 if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
242 failure = (failure || !test_ok); }
244 void
245 usage(void)
247 fprintf(stderr, "usage: fetch_test [-v] [-q]\n");
250 int
251 main(int argc, char *argv[])
253 int test_ok = 0, failure = 0;
254 int ch;
256 #ifndef PROFILE
257 if (pledge("stdio", NULL) == -1)
258 err(1, "pledge");
259 #endif
261 while ((ch = getopt(argc, argv, "vq")) != -1) {
262 switch (ch) {
263 case 'v':
264 verbose = 1;
265 quiet = 0;
266 break;
267 case 'q':
268 quiet = 1;
269 verbose = 0;
270 break;
271 default:
272 usage();
273 return 1;
276 argc -= optind;
277 argv += optind;
279 RUN_TEST(fetch_parse_uri(), "fetch_parse_uri");
281 return failure ? 1 : 0;