2 82ebf666 2020-03-18 stsp * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
4 82ebf666 2020-03-18 stsp * Permission to use, copy, modify, and distribute this software for any
5 82ebf666 2020-03-18 stsp * purpose with or without fee is hereby granted, provided that the above
6 82ebf666 2020-03-18 stsp * copyright notice and this permission notice appear in all copies.
8 82ebf666 2020-03-18 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 82ebf666 2020-03-18 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 82ebf666 2020-03-18 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 82ebf666 2020-03-18 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 82ebf666 2020-03-18 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 82ebf666 2020-03-18 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 82ebf666 2020-03-18 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
19 82ebf666 2020-03-18 stsp #include <limits.h>
20 82ebf666 2020-03-18 stsp #include <stdarg.h>
21 82ebf666 2020-03-18 stsp #include <stdlib.h>
22 82ebf666 2020-03-18 stsp #include <stdio.h>
23 82ebf666 2020-03-18 stsp #include <string.h>
24 82ebf666 2020-03-18 stsp #include <unistd.h>
25 82ebf666 2020-03-18 stsp #include <err.h>
26 82ebf666 2020-03-18 stsp #include <zlib.h>
27 82ebf666 2020-03-18 stsp #include <time.h>
29 82ebf666 2020-03-18 stsp #include "got_error.h"
30 82ebf666 2020-03-18 stsp #include "got_object.h"
31 629bd8f3 2020-03-18 stsp #include "got_path.h"
32 82ebf666 2020-03-18 stsp #include "got_fetch.h"
33 2e601464 2021-09-06 stsp #include "got_dial.h"
35 82ebf666 2020-03-18 stsp #include "got_lib_object_idset.h"
36 82ebf666 2020-03-18 stsp #include "got_lib_sha1.h"
37 82ebf666 2020-03-18 stsp #include "got_lib_inflate.h"
38 82ebf666 2020-03-18 stsp #include "got_lib_delta.h"
40 82ebf666 2020-03-18 stsp #ifndef nitems
41 82ebf666 2020-03-18 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
44 82ebf666 2020-03-18 stsp static int verbose;
45 7fb414ae 2020-08-08 stsp static int quiet;
47 ef20f542 2022-06-26 thomas static void
48 0c6f49ba 2022-07-01 thomas test_printf(const char *fmt, ...)
52 82ebf666 2020-03-18 stsp if (!verbose)
55 82ebf666 2020-03-18 stsp va_start(ap, fmt);
56 82ebf666 2020-03-18 stsp vprintf(fmt, ap);
61 82ebf666 2020-03-18 stsp fetch_parse_uri(void)
63 82ebf666 2020-03-18 stsp const struct got_error *err = NULL;
64 fa8129f7 2022-03-22 thomas const struct parse_uri_test {
65 82ebf666 2020-03-18 stsp const char *uri;
66 82ebf666 2020-03-18 stsp const char *proto;
67 82ebf666 2020-03-18 stsp const char *host;
68 82ebf666 2020-03-18 stsp const char *port;
69 82ebf666 2020-03-18 stsp const char *server_path;
70 82ebf666 2020-03-18 stsp const char *repo_name;
71 82ebf666 2020-03-18 stsp int errcode;
72 82ebf666 2020-03-18 stsp } test_data[] = {
73 82ebf666 2020-03-18 stsp { "", NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
74 82ebf666 2020-03-18 stsp { "git:", NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
75 82ebf666 2020-03-18 stsp { "git://localhost/",
76 82ebf666 2020-03-18 stsp NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
77 82ebf666 2020-03-18 stsp { "git://localhost////",
78 82ebf666 2020-03-18 stsp NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
79 82ebf666 2020-03-18 stsp { "git://127.0.0.1/git/",
80 26ec43f5 2022-03-07 thomas "git", "127.0.0.1", NULL, "/git", "git", GOT_ERR_OK },
81 82ebf666 2020-03-18 stsp { "git:///127.0.0.1/git/",
82 82ebf666 2020-03-18 stsp NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
83 9a682fbe 2020-03-19 stsp { "/127.0.0.1:/git/",
84 9a682fbe 2020-03-19 stsp NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
86 82ebf666 2020-03-18 stsp { "git://127.0.0.1/git/myrepo",
87 0921e08f 2020-09-24 stsp "git", "127.0.0.1", NULL,
88 0921e08f 2020-09-24 stsp "/git/myrepo", "myrepo", GOT_ERR_OK },
89 0921e08f 2020-09-24 stsp { "git://127.0.0.1//git/myrepo",
90 62a4c94c 2020-03-20 stsp "git", "127.0.0.1", NULL,
91 a244cd92 2020-03-19 stsp "/git/myrepo", "myrepo", GOT_ERR_OK },
92 0921e08f 2020-09-24 stsp { "git://127.0.0.1/////git//myrepo",
93 0921e08f 2020-09-24 stsp "git", "127.0.0.1", NULL,
94 0921e08f 2020-09-24 stsp "/git//myrepo", "myrepo", GOT_ERR_OK },
95 82ebf666 2020-03-18 stsp { "http://127.0.0.1/git/myrepo",
96 62a4c94c 2020-03-20 stsp "http", "127.0.0.1", NULL,
97 a244cd92 2020-03-19 stsp "/git/myrepo", "myrepo", GOT_ERR_OK },
98 82ebf666 2020-03-18 stsp { "gopher://127.0.0.1/git/myrepo",
99 62a4c94c 2020-03-20 stsp "gopher", "127.0.0.1", NULL,
100 a244cd92 2020-03-19 stsp "/git/myrepo", "myrepo", GOT_ERR_OK },
102 82ebf666 2020-03-18 stsp { "git://127.0.0.1:22/git/myrepo",
103 a244cd92 2020-03-19 stsp "git", "127.0.0.1", "22", "/git/myrepo", "myrepo",
104 a244cd92 2020-03-19 stsp GOT_ERR_OK },
105 82ebf666 2020-03-18 stsp { "git://127.0.0.1/git/repos/foo/bar/myrepo.git",
106 62a4c94c 2020-03-20 stsp "git", "127.0.0.1", NULL,
107 a244cd92 2020-03-19 stsp "/git/repos/foo/bar/myrepo.git", "myrepo", GOT_ERR_OK },
108 82ebf666 2020-03-18 stsp { "https://127.0.0.1/git/repos/foo/../bar/myrepo.git",
109 62a4c94c 2020-03-20 stsp "https", "127.0.0.1", NULL,
110 a244cd92 2020-03-19 stsp "/git/repos/foo/../bar/myrepo.git", "myrepo",
111 a244cd92 2020-03-19 stsp GOT_ERR_OK },
113 9a682fbe 2020-03-19 stsp { "git+ssh://127.0.0.1:22/git/myrepo",
114 a244cd92 2020-03-19 stsp "git+ssh", "127.0.0.1", "22", "/git/myrepo", "myrepo",
115 a244cd92 2020-03-19 stsp GOT_ERR_OK },
116 9a682fbe 2020-03-19 stsp { "ssh://127.0.0.1:22/git/myrepo",
117 a244cd92 2020-03-19 stsp "ssh", "127.0.0.1", "22", "/git/myrepo", "myrepo",
118 a244cd92 2020-03-19 stsp GOT_ERR_OK },
120 9a682fbe 2020-03-19 stsp { "127.0.0.1:git/myrepo",
121 62a4c94c 2020-03-20 stsp "ssh", "127.0.0.1", NULL, "git/myrepo", "myrepo",
122 a244cd92 2020-03-19 stsp GOT_ERR_OK },
123 a244cd92 2020-03-19 stsp { "127.0.0.1:/git/myrepo",
124 62a4c94c 2020-03-20 stsp "ssh", "127.0.0.1", NULL, "/git/myrepo", "myrepo",
125 a244cd92 2020-03-19 stsp GOT_ERR_OK },
126 9a682fbe 2020-03-19 stsp { "127.0.0.1:22/git/myrepo",
127 62a4c94c 2020-03-20 stsp "ssh", "127.0.0.1", NULL, "22/git/myrepo", "myrepo",
128 a244cd92 2020-03-19 stsp GOT_ERR_OK },
132 82ebf666 2020-03-18 stsp for (i = 0; i < nitems(test_data); i++) {
133 82ebf666 2020-03-18 stsp const char *uri = test_data[i].uri;
134 82ebf666 2020-03-18 stsp const char *expected_proto = test_data[i].proto;
135 82ebf666 2020-03-18 stsp const char *expected_host = test_data[i].host;
136 82ebf666 2020-03-18 stsp const char *expected_port = test_data[i].port;
137 82ebf666 2020-03-18 stsp const char *expected_server_path = test_data[i].server_path;
138 82ebf666 2020-03-18 stsp const char *expected_repo_name = test_data[i].repo_name;
139 82ebf666 2020-03-18 stsp char *proto, *host, *port, *server_path, *repo_name;
141 2e601464 2021-09-06 stsp err = got_dial_parse_uri(&proto, &host, &port, &server_path,
142 82ebf666 2020-03-18 stsp &repo_name, uri);
143 82ebf666 2020-03-18 stsp if (err && err->code != test_data[i].errcode) {
144 82ebf666 2020-03-18 stsp test_printf("%d: error code %d; expected %d\n",
145 82ebf666 2020-03-18 stsp i, err->code, test_data[i].errcode);
149 82ebf666 2020-03-18 stsp if (expected_proto == NULL && proto != NULL) {
150 82ebf666 2020-03-18 stsp test_printf("%d: proto %s; expected NULL\n", i, proto);
153 82ebf666 2020-03-18 stsp if (expected_host == NULL && host != NULL) {
154 82ebf666 2020-03-18 stsp test_printf("%d: host %s; expected NULL\n", i, host);
157 82ebf666 2020-03-18 stsp if (expected_port == NULL && port != NULL) {
158 82ebf666 2020-03-18 stsp test_printf("%d: port %s; expected NULL\n", i, port);
161 82ebf666 2020-03-18 stsp if (expected_server_path == NULL && server_path != NULL) {
162 82ebf666 2020-03-18 stsp test_printf("%d: server path %s; expected NULL\n", i,
163 82ebf666 2020-03-18 stsp server_path);
166 82ebf666 2020-03-18 stsp if (expected_repo_name == NULL && repo_name != NULL) {
167 82ebf666 2020-03-18 stsp test_printf("%d: repo name %s; expected NULL\n", i,
168 82ebf666 2020-03-18 stsp repo_name);
172 82ebf666 2020-03-18 stsp if (expected_proto != NULL && proto == NULL) {
173 82ebf666 2020-03-18 stsp test_printf("%d: proto NULL; expected %s\n", i,
174 82ebf666 2020-03-18 stsp expected_proto);
177 82ebf666 2020-03-18 stsp if (expected_host != NULL && host == NULL) {
178 82ebf666 2020-03-18 stsp test_printf("%d: host NULL; expected %s\n", i,
179 82ebf666 2020-03-18 stsp expected_host);
182 82ebf666 2020-03-18 stsp if (expected_port != NULL && port == NULL) {
183 82ebf666 2020-03-18 stsp test_printf("%d: port NULL; expected %s\n", i,
184 82ebf666 2020-03-18 stsp expected_port);
187 82ebf666 2020-03-18 stsp if (expected_server_path != NULL && server_path == NULL) {
188 82ebf666 2020-03-18 stsp test_printf("%d: server path %s; expected %s\n", i,
189 82ebf666 2020-03-18 stsp expected_server_path);
192 82ebf666 2020-03-18 stsp if (expected_repo_name != NULL && repo_name == NULL) {
193 82ebf666 2020-03-18 stsp test_printf("%d: repo name NULL; expected %s\n", i,
194 82ebf666 2020-03-18 stsp repo_name);
198 82ebf666 2020-03-18 stsp if (expected_proto != NULL && strcmp(expected_proto, proto)) {
199 82ebf666 2020-03-18 stsp test_printf("%d: proto %s; expected %s\n", i, proto,
200 82ebf666 2020-03-18 stsp expected_proto);
204 a244cd92 2020-03-19 stsp if (expected_host != NULL && strcmp(expected_host, host)) {
205 a244cd92 2020-03-19 stsp test_printf("%d: host %s; expected %s\n", i, host,
206 a244cd92 2020-03-19 stsp expected_host);
210 a244cd92 2020-03-19 stsp if (expected_port != NULL && strcmp(expected_port, port)) {
211 a244cd92 2020-03-19 stsp test_printf("%d: port %s; expected %s\n", i, port,
212 a244cd92 2020-03-19 stsp expected_port);
216 a244cd92 2020-03-19 stsp if (expected_server_path != NULL &&
217 a244cd92 2020-03-19 stsp strcmp(expected_server_path, server_path)) {
218 a244cd92 2020-03-19 stsp test_printf("%d: server_path %s; expected %s\n", i,
219 a244cd92 2020-03-19 stsp server_path, expected_server_path);
223 a244cd92 2020-03-19 stsp if (expected_repo_name != NULL &&
224 a244cd92 2020-03-19 stsp strcmp(expected_repo_name, repo_name)) {
225 a244cd92 2020-03-19 stsp test_printf("%d: repo_name %s; expected %s\n", i,
226 a244cd92 2020-03-19 stsp repo_name, expected_repo_name);
230 82ebf666 2020-03-18 stsp free(proto);
231 82ebf666 2020-03-18 stsp proto = NULL;
232 82ebf666 2020-03-18 stsp free(host);
233 82ebf666 2020-03-18 stsp host = NULL;
234 82ebf666 2020-03-18 stsp free(port);
235 82ebf666 2020-03-18 stsp port = NULL;
236 82ebf666 2020-03-18 stsp free(server_path);
237 82ebf666 2020-03-18 stsp server_path = NULL;
238 82ebf666 2020-03-18 stsp free(repo_name);
239 82ebf666 2020-03-18 stsp repo_name = NULL;
245 82ebf666 2020-03-18 stsp #define RUN_TEST(expr, name) \
246 82ebf666 2020-03-18 stsp { test_ok = (expr); \
247 7fb414ae 2020-08-08 stsp if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
248 82ebf666 2020-03-18 stsp failure = (failure || !test_ok); }
250 ef20f542 2022-06-26 thomas static void
251 82ebf666 2020-03-18 stsp usage(void)
253 7fb414ae 2020-08-08 stsp fprintf(stderr, "usage: fetch_test [-v] [-q]\n");
257 82ebf666 2020-03-18 stsp main(int argc, char *argv[])
259 82ebf666 2020-03-18 stsp int test_ok = 0, failure = 0;
262 82ebf666 2020-03-18 stsp #ifndef PROFILE
263 82ebf666 2020-03-18 stsp if (pledge("stdio", NULL) == -1)
264 82ebf666 2020-03-18 stsp err(1, "pledge");
267 f7065961 2022-10-27 thomas while ((ch = getopt(argc, argv, "qv")) != -1) {
268 82ebf666 2020-03-18 stsp switch (ch) {
271 7fb414ae 2020-08-08 stsp verbose = 0;
273 f7065961 2022-10-27 thomas case 'v':
274 f7065961 2022-10-27 thomas verbose = 1;
275 f7065961 2022-10-27 thomas quiet = 0;
282 82ebf666 2020-03-18 stsp argc -= optind;
283 82ebf666 2020-03-18 stsp argv += optind;
285 82ebf666 2020-03-18 stsp RUN_TEST(fetch_parse_uri(), "fetch_parse_uri");
287 82ebf666 2020-03-18 stsp return failure ? 1 : 0;