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;
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 { "http://127.0.0.1/git/myrepo",
89 "http", "127.0.0.1", NULL,
90 "/git/myrepo", "myrepo", GOT_ERR_OK },
91 { "gopher://127.0.0.1/git/myrepo",
92 "gopher", "127.0.0.1", NULL,
93 "/git/myrepo", "myrepo", GOT_ERR_OK },
95 { "git://127.0.0.1:22/git/myrepo",
96 "git", "127.0.0.1", "22", "/git/myrepo", "myrepo",
97 GOT_ERR_OK },
98 { "git://127.0.0.1/git/repos/foo/bar/myrepo.git",
99 "git", "127.0.0.1", NULL,
100 "/git/repos/foo/bar/myrepo.git", "myrepo", GOT_ERR_OK },
101 { "https://127.0.0.1/git/repos/foo/../bar/myrepo.git",
102 "https", "127.0.0.1", NULL,
103 "/git/repos/foo/../bar/myrepo.git", "myrepo",
104 GOT_ERR_OK },
106 { "git+ssh://127.0.0.1:22/git/myrepo",
107 "git+ssh", "127.0.0.1", "22", "/git/myrepo", "myrepo",
108 GOT_ERR_OK },
109 { "ssh://127.0.0.1:22/git/myrepo",
110 "ssh", "127.0.0.1", "22", "/git/myrepo", "myrepo",
111 GOT_ERR_OK },
113 { "127.0.0.1:git/myrepo",
114 "ssh", "127.0.0.1", NULL, "git/myrepo", "myrepo",
115 GOT_ERR_OK },
116 { "127.0.0.1:/git/myrepo",
117 "ssh", "127.0.0.1", NULL, "/git/myrepo", "myrepo",
118 GOT_ERR_OK },
119 { "127.0.0.1:22/git/myrepo",
120 "ssh", "127.0.0.1", NULL, "22/git/myrepo", "myrepo",
121 GOT_ERR_OK },
122 };
123 int i;
125 for (i = 0; i < nitems(test_data); i++) {
126 const char *uri = test_data[i].uri;
127 const char *expected_proto = test_data[i].proto;
128 const char *expected_host = test_data[i].host;
129 const char *expected_port = test_data[i].port;
130 const char *expected_server_path = test_data[i].server_path;
131 const char *expected_repo_name = test_data[i].repo_name;
132 char *proto, *host, *port, *server_path, *repo_name;
134 err = got_fetch_parse_uri(&proto, &host, &port, &server_path,
135 &repo_name, uri);
136 if (err && err->code != test_data[i].errcode) {
137 test_printf("%d: error code %d; expected %d\n",
138 i, err->code, test_data[i].errcode);
139 return 0;
142 if (expected_proto == NULL && proto != NULL) {
143 test_printf("%d: proto %s; expected NULL\n", i, proto);
144 return 0;
146 if (expected_host == NULL && host != NULL) {
147 test_printf("%d: host %s; expected NULL\n", i, host);
148 return 0;
150 if (expected_port == NULL && port != NULL) {
151 test_printf("%d: port %s; expected NULL\n", i, port);
152 return 0;
154 if (expected_server_path == NULL && server_path != NULL) {
155 test_printf("%d: server path %s; expected NULL\n", i,
156 server_path);
157 return 0;
159 if (expected_repo_name == NULL && repo_name != NULL) {
160 test_printf("%d: repo name %s; expected NULL\n", i,
161 repo_name);
162 return 0;
165 if (expected_proto != NULL && proto == NULL) {
166 test_printf("%d: proto NULL; expected %s\n", i,
167 expected_proto);
168 return 0;
170 if (expected_host != NULL && host == NULL) {
171 test_printf("%d: host NULL; expected %s\n", i,
172 expected_host);
173 return 0;
175 if (expected_port != NULL && port == NULL) {
176 test_printf("%d: port NULL; expected %s\n", i,
177 expected_port);
178 return 0;
180 if (expected_server_path != NULL && server_path == NULL) {
181 test_printf("%d: server path %s; expected %s\n", i,
182 expected_server_path);
183 return 0;
185 if (expected_repo_name != NULL && repo_name == NULL) {
186 test_printf("%d: repo name NULL; expected %s\n", i,
187 repo_name);
188 return 0;
191 if (expected_proto != NULL && strcmp(expected_proto, proto)) {
192 test_printf("%d: proto %s; expected %s\n", i, proto,
193 expected_proto);
194 return 0;
197 if (expected_host != NULL && strcmp(expected_host, host)) {
198 test_printf("%d: host %s; expected %s\n", i, host,
199 expected_host);
200 return 0;
203 if (expected_port != NULL && strcmp(expected_port, port)) {
204 test_printf("%d: port %s; expected %s\n", i, port,
205 expected_port);
206 return 0;
209 if (expected_server_path != NULL &&
210 strcmp(expected_server_path, server_path)) {
211 test_printf("%d: server_path %s; expected %s\n", i,
212 server_path, expected_server_path);
213 return 0;
216 if (expected_repo_name != NULL &&
217 strcmp(expected_repo_name, repo_name)) {
218 test_printf("%d: repo_name %s; expected %s\n", i,
219 repo_name, expected_repo_name);
220 return 0;
223 free(proto);
224 proto = NULL;
225 free(host);
226 host = NULL;
227 free(port);
228 port = NULL;
229 free(server_path);
230 server_path = NULL;
231 free(repo_name);
232 repo_name = NULL;
235 return 1;
238 #define RUN_TEST(expr, name) \
239 { test_ok = (expr); \
240 printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
241 failure = (failure || !test_ok); }
243 void
244 usage(void)
246 fprintf(stderr, "usage: fetch_test [-v]\n");
249 int
250 main(int argc, char *argv[])
252 int test_ok = 0, failure = 0;
253 int ch;
255 #ifndef PROFILE
256 if (pledge("stdio", NULL) == -1)
257 err(1, "pledge");
258 #endif
260 while ((ch = getopt(argc, argv, "v")) != -1) {
261 switch (ch) {
262 case 'v':
263 verbose = 1;
264 break;
265 default:
266 usage();
267 return 1;
270 argc -= optind;
271 argv += optind;
273 RUN_TEST(fetch_parse_uri(), "fetch_parse_uri");
275 return failure ? 1 : 0;