2 * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/queue.h>
31 #include "got_error.h"
32 #include "got_object.h"
34 #include "got_fetch.h"
37 #include "got_lib_object_idset.h"
38 #include "got_lib_hash.h"
39 #include "got_lib_inflate.h"
40 #include "got_lib_delta.h"
43 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
50 test_printf(const char *fmt, ...)
65 const struct got_error *err = NULL;
66 const struct parse_uri_test {
71 const char *server_path;
72 const char *repo_name;
75 { "", NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
76 { "git:", NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
78 NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
79 { "git://localhost////",
80 NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
81 { "git://127.0.0.1/git/",
82 "git", "127.0.0.1", NULL, "/git", "git", GOT_ERR_OK },
83 { "git:///127.0.0.1/git/",
84 NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
86 NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
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 { "git://127.0.0.1/////git//myrepo",
95 "git", "127.0.0.1", NULL,
96 "/git//myrepo", "myrepo", GOT_ERR_OK },
97 { "http://127.0.0.1/git/myrepo",
98 "http", "127.0.0.1", NULL,
99 "/git/myrepo", "myrepo", GOT_ERR_OK },
100 { "gopher://127.0.0.1/git/myrepo",
101 "gopher", "127.0.0.1", NULL,
102 "/git/myrepo", "myrepo", GOT_ERR_OK },
104 { "git://127.0.0.1:22/git/myrepo",
105 "git", "127.0.0.1", "22", "/git/myrepo", "myrepo",
107 { "git://127.0.0.1/git/repos/foo/bar/myrepo.git",
108 "git", "127.0.0.1", NULL,
109 "/git/repos/foo/bar/myrepo.git", "myrepo", GOT_ERR_OK },
110 { "https://127.0.0.1/git/repos/foo/../bar/myrepo.git",
111 "https", "127.0.0.1", NULL,
112 "/git/repos/foo/../bar/myrepo.git", "myrepo",
115 { "git+ssh://127.0.0.1:22/git/myrepo",
116 "git+ssh", "127.0.0.1", "22", "/git/myrepo", "myrepo",
118 { "ssh://127.0.0.1:22/git/myrepo",
119 "ssh", "127.0.0.1", "22", "/git/myrepo", "myrepo",
122 { "127.0.0.1:git/myrepo",
123 "ssh", "127.0.0.1", NULL, "git/myrepo", "myrepo",
125 { "127.0.0.1:/git/myrepo",
126 "ssh", "127.0.0.1", NULL, "/git/myrepo", "myrepo",
128 { "127.0.0.1:22/git/myrepo",
129 "ssh", "127.0.0.1", NULL, "22/git/myrepo", "myrepo",
134 for (i = 0; i < nitems(test_data); i++) {
135 const char *uri = test_data[i].uri;
136 const char *expected_proto = test_data[i].proto;
137 const char *expected_host = test_data[i].host;
138 const char *expected_port = test_data[i].port;
139 const char *expected_server_path = test_data[i].server_path;
140 const char *expected_repo_name = test_data[i].repo_name;
141 char *proto, *host, *port, *server_path, *repo_name;
143 err = got_dial_parse_uri(&proto, &host, &port, &server_path,
145 if (err && err->code != test_data[i].errcode) {
146 test_printf("%d: error code %d; expected %d\n",
147 i, err->code, test_data[i].errcode);
151 if (expected_proto == NULL && proto != NULL) {
152 test_printf("%d: proto %s; expected NULL\n", i, proto);
155 if (expected_host == NULL && host != NULL) {
156 test_printf("%d: host %s; expected NULL\n", i, host);
159 if (expected_port == NULL && port != NULL) {
160 test_printf("%d: port %s; expected NULL\n", i, port);
163 if (expected_server_path == NULL && server_path != NULL) {
164 test_printf("%d: server path %s; expected NULL\n", i,
168 if (expected_repo_name == NULL && repo_name != NULL) {
169 test_printf("%d: repo name %s; expected NULL\n", i,
174 if (expected_proto != NULL && proto == NULL) {
175 test_printf("%d: proto NULL; expected %s\n", i,
179 if (expected_host != NULL && host == NULL) {
180 test_printf("%d: host NULL; expected %s\n", i,
184 if (expected_port != NULL && port == NULL) {
185 test_printf("%d: port NULL; expected %s\n", i,
189 if (expected_server_path != NULL && server_path == NULL) {
190 test_printf("%d: server path %s; expected %s\n", i,
191 expected_server_path);
194 if (expected_repo_name != NULL && repo_name == NULL) {
195 test_printf("%d: repo name NULL; expected %s\n", i,
200 if (expected_proto != NULL && strcmp(expected_proto, proto)) {
201 test_printf("%d: proto %s; expected %s\n", i, proto,
206 if (expected_host != NULL && strcmp(expected_host, host)) {
207 test_printf("%d: host %s; expected %s\n", i, host,
212 if (expected_port != NULL && strcmp(expected_port, port)) {
213 test_printf("%d: port %s; expected %s\n", i, port,
218 if (expected_server_path != NULL &&
219 strcmp(expected_server_path, server_path)) {
220 test_printf("%d: server_path %s; expected %s\n", i,
221 server_path, expected_server_path);
225 if (expected_repo_name != NULL &&
226 strcmp(expected_repo_name, repo_name)) {
227 test_printf("%d: repo_name %s; expected %s\n", i,
228 repo_name, expected_repo_name);
247 #define RUN_TEST(expr, name) \
248 { test_ok = (expr); \
249 if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
250 failure = (failure || !test_ok); }
255 fprintf(stderr, "usage: fetch_test [-v] [-q]\n");
259 main(int argc, char *argv[])
261 int test_ok = 0, failure = 0;
265 if (pledge("stdio", NULL) == -1)
269 while ((ch = getopt(argc, argv, "qv")) != -1) {
287 RUN_TEST(fetch_parse_uri(), "fetch_parse_uri");
289 return failure ? 1 : 0;