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 82ebf666 2020-03-18 stsp #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 <sha1.h>
27 5822e79e 2023-02-23 op #include <sha2.h>
28 82ebf666 2020-03-18 stsp #include <zlib.h>
29 82ebf666 2020-03-18 stsp #include <time.h>
31 82ebf666 2020-03-18 stsp #include "got_error.h"
32 82ebf666 2020-03-18 stsp #include "got_object.h"
33 629bd8f3 2020-03-18 stsp #include "got_path.h"
34 82ebf666 2020-03-18 stsp #include "got_fetch.h"
35 2e601464 2021-09-06 stsp #include "got_dial.h"
37 82ebf666 2020-03-18 stsp #include "got_lib_object_idset.h"
38 53bf0b54 2023-02-23 op #include "got_lib_hash.h"
39 82ebf666 2020-03-18 stsp #include "got_lib_inflate.h"
40 82ebf666 2020-03-18 stsp #include "got_lib_delta.h"
42 82ebf666 2020-03-18 stsp #ifndef nitems
43 82ebf666 2020-03-18 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
46 82ebf666 2020-03-18 stsp static int verbose;
47 7fb414ae 2020-08-08 stsp static int quiet;
50 58e31a80 2022-06-27 op test_printf(const char *fmt, ...)
54 82ebf666 2020-03-18 stsp if (!verbose)
57 82ebf666 2020-03-18 stsp va_start(ap, fmt);
58 82ebf666 2020-03-18 stsp vprintf(fmt, ap);
63 82ebf666 2020-03-18 stsp fetch_parse_uri(void)
65 82ebf666 2020-03-18 stsp const struct got_error *err = NULL;
66 d58ddaf3 2022-03-17 naddy const struct parse_uri_test {
67 82ebf666 2020-03-18 stsp const char *uri;
68 82ebf666 2020-03-18 stsp const char *proto;
69 82ebf666 2020-03-18 stsp const char *host;
70 82ebf666 2020-03-18 stsp const char *port;
71 82ebf666 2020-03-18 stsp const char *server_path;
72 82ebf666 2020-03-18 stsp const char *repo_name;
73 82ebf666 2020-03-18 stsp int errcode;
74 82ebf666 2020-03-18 stsp } test_data[] = {
75 82ebf666 2020-03-18 stsp { "", NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
76 82ebf666 2020-03-18 stsp { "git:", 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://localhost////",
80 82ebf666 2020-03-18 stsp NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
81 82ebf666 2020-03-18 stsp { "git://127.0.0.1/git/",
82 3a12860c 2022-03-07 stsp "git", "127.0.0.1", NULL, "/git", "git", GOT_ERR_OK },
83 82ebf666 2020-03-18 stsp { "git:///127.0.0.1/git/",
84 82ebf666 2020-03-18 stsp NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
85 9a682fbe 2020-03-19 stsp { "/127.0.0.1:/git/",
86 9a682fbe 2020-03-19 stsp NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
88 82ebf666 2020-03-18 stsp { "git://127.0.0.1/git/myrepo",
89 0921e08f 2020-09-24 stsp "git", "127.0.0.1", NULL,
90 0921e08f 2020-09-24 stsp "/git/myrepo", "myrepo", GOT_ERR_OK },
91 0921e08f 2020-09-24 stsp { "git://127.0.0.1//git/myrepo",
92 62a4c94c 2020-03-20 stsp "git", "127.0.0.1", NULL,
93 a244cd92 2020-03-19 stsp "/git/myrepo", "myrepo", GOT_ERR_OK },
94 0921e08f 2020-09-24 stsp { "git://127.0.0.1/////git//myrepo",
95 0921e08f 2020-09-24 stsp "git", "127.0.0.1", NULL,
96 0921e08f 2020-09-24 stsp "/git//myrepo", "myrepo", GOT_ERR_OK },
97 82ebf666 2020-03-18 stsp { "http://127.0.0.1/git/myrepo",
98 62a4c94c 2020-03-20 stsp "http", "127.0.0.1", NULL,
99 a244cd92 2020-03-19 stsp "/git/myrepo", "myrepo", GOT_ERR_OK },
100 82ebf666 2020-03-18 stsp { "gopher://127.0.0.1/git/myrepo",
101 62a4c94c 2020-03-20 stsp "gopher", "127.0.0.1", NULL,
102 a244cd92 2020-03-19 stsp "/git/myrepo", "myrepo", GOT_ERR_OK },
104 82ebf666 2020-03-18 stsp { "git://127.0.0.1:22/git/myrepo",
105 a244cd92 2020-03-19 stsp "git", "127.0.0.1", "22", "/git/myrepo", "myrepo",
106 a244cd92 2020-03-19 stsp GOT_ERR_OK },
107 82ebf666 2020-03-18 stsp { "git://127.0.0.1/git/repos/foo/bar/myrepo.git",
108 62a4c94c 2020-03-20 stsp "git", "127.0.0.1", NULL,
109 a244cd92 2020-03-19 stsp "/git/repos/foo/bar/myrepo.git", "myrepo", GOT_ERR_OK },
110 82ebf666 2020-03-18 stsp { "https://127.0.0.1/git/repos/foo/../bar/myrepo.git",
111 62a4c94c 2020-03-20 stsp "https", "127.0.0.1", NULL,
112 a244cd92 2020-03-19 stsp "/git/repos/foo/../bar/myrepo.git", "myrepo",
113 a244cd92 2020-03-19 stsp GOT_ERR_OK },
115 9a682fbe 2020-03-19 stsp { "git+ssh://127.0.0.1:22/git/myrepo",
116 a244cd92 2020-03-19 stsp "git+ssh", "127.0.0.1", "22", "/git/myrepo", "myrepo",
117 a244cd92 2020-03-19 stsp GOT_ERR_OK },
118 9a682fbe 2020-03-19 stsp { "ssh://127.0.0.1:22/git/myrepo",
119 a244cd92 2020-03-19 stsp "ssh", "127.0.0.1", "22", "/git/myrepo", "myrepo",
120 a244cd92 2020-03-19 stsp GOT_ERR_OK },
122 9a682fbe 2020-03-19 stsp { "127.0.0.1:git/myrepo",
123 62a4c94c 2020-03-20 stsp "ssh", "127.0.0.1", NULL, "git/myrepo", "myrepo",
124 a244cd92 2020-03-19 stsp GOT_ERR_OK },
125 a244cd92 2020-03-19 stsp { "127.0.0.1:/git/myrepo",
126 62a4c94c 2020-03-20 stsp "ssh", "127.0.0.1", NULL, "/git/myrepo", "myrepo",
127 a244cd92 2020-03-19 stsp GOT_ERR_OK },
128 9a682fbe 2020-03-19 stsp { "127.0.0.1:22/git/myrepo",
129 62a4c94c 2020-03-20 stsp "ssh", "127.0.0.1", NULL, "22/git/myrepo", "myrepo",
130 a244cd92 2020-03-19 stsp GOT_ERR_OK },
134 82ebf666 2020-03-18 stsp for (i = 0; i < nitems(test_data); i++) {
135 82ebf666 2020-03-18 stsp const char *uri = test_data[i].uri;
136 82ebf666 2020-03-18 stsp const char *expected_proto = test_data[i].proto;
137 82ebf666 2020-03-18 stsp const char *expected_host = test_data[i].host;
138 82ebf666 2020-03-18 stsp const char *expected_port = test_data[i].port;
139 82ebf666 2020-03-18 stsp const char *expected_server_path = test_data[i].server_path;
140 82ebf666 2020-03-18 stsp const char *expected_repo_name = test_data[i].repo_name;
141 82ebf666 2020-03-18 stsp char *proto, *host, *port, *server_path, *repo_name;
143 2e601464 2021-09-06 stsp err = got_dial_parse_uri(&proto, &host, &port, &server_path,
144 82ebf666 2020-03-18 stsp &repo_name, uri);
145 82ebf666 2020-03-18 stsp if (err && err->code != test_data[i].errcode) {
146 82ebf666 2020-03-18 stsp test_printf("%d: error code %d; expected %d\n",
147 82ebf666 2020-03-18 stsp i, err->code, test_data[i].errcode);
151 82ebf666 2020-03-18 stsp if (expected_proto == NULL && proto != NULL) {
152 82ebf666 2020-03-18 stsp test_printf("%d: proto %s; expected NULL\n", i, proto);
155 82ebf666 2020-03-18 stsp if (expected_host == NULL && host != NULL) {
156 82ebf666 2020-03-18 stsp test_printf("%d: host %s; expected NULL\n", i, host);
159 82ebf666 2020-03-18 stsp if (expected_port == NULL && port != NULL) {
160 82ebf666 2020-03-18 stsp test_printf("%d: port %s; expected NULL\n", i, port);
163 82ebf666 2020-03-18 stsp if (expected_server_path == NULL && server_path != NULL) {
164 82ebf666 2020-03-18 stsp test_printf("%d: server path %s; expected NULL\n", i,
165 82ebf666 2020-03-18 stsp server_path);
168 82ebf666 2020-03-18 stsp if (expected_repo_name == NULL && repo_name != NULL) {
169 82ebf666 2020-03-18 stsp test_printf("%d: repo name %s; expected NULL\n", i,
170 82ebf666 2020-03-18 stsp repo_name);
174 82ebf666 2020-03-18 stsp if (expected_proto != NULL && proto == NULL) {
175 82ebf666 2020-03-18 stsp test_printf("%d: proto NULL; expected %s\n", i,
176 82ebf666 2020-03-18 stsp expected_proto);
179 82ebf666 2020-03-18 stsp if (expected_host != NULL && host == NULL) {
180 82ebf666 2020-03-18 stsp test_printf("%d: host NULL; expected %s\n", i,
181 82ebf666 2020-03-18 stsp expected_host);
184 82ebf666 2020-03-18 stsp if (expected_port != NULL && port == NULL) {
185 82ebf666 2020-03-18 stsp test_printf("%d: port NULL; expected %s\n", i,
186 82ebf666 2020-03-18 stsp expected_port);
189 82ebf666 2020-03-18 stsp if (expected_server_path != NULL && server_path == NULL) {
190 82ebf666 2020-03-18 stsp test_printf("%d: server path %s; expected %s\n", i,
191 82ebf666 2020-03-18 stsp expected_server_path);
194 82ebf666 2020-03-18 stsp if (expected_repo_name != NULL && repo_name == NULL) {
195 82ebf666 2020-03-18 stsp test_printf("%d: repo name NULL; expected %s\n", i,
196 82ebf666 2020-03-18 stsp repo_name);
200 82ebf666 2020-03-18 stsp if (expected_proto != NULL && strcmp(expected_proto, proto)) {
201 82ebf666 2020-03-18 stsp test_printf("%d: proto %s; expected %s\n", i, proto,
202 82ebf666 2020-03-18 stsp expected_proto);
206 a244cd92 2020-03-19 stsp if (expected_host != NULL && strcmp(expected_host, host)) {
207 a244cd92 2020-03-19 stsp test_printf("%d: host %s; expected %s\n", i, host,
208 a244cd92 2020-03-19 stsp expected_host);
212 a244cd92 2020-03-19 stsp if (expected_port != NULL && strcmp(expected_port, port)) {
213 a244cd92 2020-03-19 stsp test_printf("%d: port %s; expected %s\n", i, port,
214 a244cd92 2020-03-19 stsp expected_port);
218 a244cd92 2020-03-19 stsp if (expected_server_path != NULL &&
219 a244cd92 2020-03-19 stsp strcmp(expected_server_path, server_path)) {
220 a244cd92 2020-03-19 stsp test_printf("%d: server_path %s; expected %s\n", i,
221 a244cd92 2020-03-19 stsp server_path, expected_server_path);
225 a244cd92 2020-03-19 stsp if (expected_repo_name != NULL &&
226 a244cd92 2020-03-19 stsp strcmp(expected_repo_name, repo_name)) {
227 a244cd92 2020-03-19 stsp test_printf("%d: repo_name %s; expected %s\n", i,
228 a244cd92 2020-03-19 stsp repo_name, expected_repo_name);
232 82ebf666 2020-03-18 stsp free(proto);
233 82ebf666 2020-03-18 stsp proto = NULL;
234 82ebf666 2020-03-18 stsp free(host);
235 82ebf666 2020-03-18 stsp host = NULL;
236 82ebf666 2020-03-18 stsp free(port);
237 82ebf666 2020-03-18 stsp port = NULL;
238 82ebf666 2020-03-18 stsp free(server_path);
239 82ebf666 2020-03-18 stsp server_path = NULL;
240 82ebf666 2020-03-18 stsp free(repo_name);
241 82ebf666 2020-03-18 stsp repo_name = NULL;
247 82ebf666 2020-03-18 stsp #define RUN_TEST(expr, name) \
248 82ebf666 2020-03-18 stsp { test_ok = (expr); \
249 7fb414ae 2020-08-08 stsp if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
250 82ebf666 2020-03-18 stsp failure = (failure || !test_ok); }
253 82ebf666 2020-03-18 stsp usage(void)
255 7fb414ae 2020-08-08 stsp fprintf(stderr, "usage: fetch_test [-v] [-q]\n");
259 82ebf666 2020-03-18 stsp main(int argc, char *argv[])
261 82ebf666 2020-03-18 stsp int test_ok = 0, failure = 0;
264 82ebf666 2020-03-18 stsp #ifndef PROFILE
265 82ebf666 2020-03-18 stsp if (pledge("stdio", NULL) == -1)
266 82ebf666 2020-03-18 stsp err(1, "pledge");
269 6f319063 2022-10-27 stsp while ((ch = getopt(argc, argv, "qv")) != -1) {
270 82ebf666 2020-03-18 stsp switch (ch) {
273 7fb414ae 2020-08-08 stsp verbose = 0;
276 6f319063 2022-10-27 stsp verbose = 1;
284 82ebf666 2020-03-18 stsp argc -= optind;
285 82ebf666 2020-03-18 stsp argv += optind;
287 82ebf666 2020-03-18 stsp RUN_TEST(fetch_parse_uri(), "fetch_parse_uri");
289 82ebf666 2020-03-18 stsp return failure ? 1 : 0;