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", "localhost", GOT_DEFAULT_GIT_PORT_STR, "git",
87 "myrepo", GOT_ERR_OK },
88 { "http://127.0.0.1/git/myrepo",
89 "http", "localhost", GOT_DEFAULT_GIT_PORT_STR, "git",
90 "myrepo", GOT_ERR_OK },
91 { "gopher://127.0.0.1/git/myrepo",
92 "gopher", "localhost", GOT_DEFAULT_GIT_PORT_STR, "git",
93 "myrepo", GOT_ERR_OK },
95 { "git://127.0.0.1:22/git/myrepo",
96 "git", "localhost", "22", "git", "myrepo", GOT_ERR_OK },
98 { "git://127.0.0.1/git/repos/foo/bar/myrepo.git",
99 "git", "localhost", GOT_DEFAULT_GIT_PORT_STR,
100 "git/repos/foo/bar", "myrepo", GOT_ERR_OK },
101 { "https://127.0.0.1/git/repos/foo/../bar/myrepo.git",
102 "https", "localhost", GOT_DEFAULT_GIT_PORT_STR,
103 "git/repos/foo/../bar", "myrepo", GOT_ERR_OK },
105 { "git+ssh://127.0.0.1:22/git/myrepo",
106 "git+ssh", "localhost", "22", "git", "myrepo", GOT_ERR_OK },
107 { "ssh://127.0.0.1:22/git/myrepo",
108 "ssh", "localhost", "22", "git", "myrepo", GOT_ERR_OK },
109 { "127.0.0.1:git/myrepo",
110 "ssh", "localhost", "22", "git", "myrepo", GOT_ERR_OK },
111 { "127.0.0.1:/git/myrepo",
112 "ssh", "localhost", "22", "git", "myrepo", GOT_ERR_OK },
113 { "127.0.0.1:22/git/myrepo",
114 "ssh", "localhost", "22", "git", "myrepo", GOT_ERR_OK },
115 };
116 int i;
118 for (i = 0; i < nitems(test_data); i++) {
119 const char *uri = test_data[i].uri;
120 const char *expected_proto = test_data[i].proto;
121 const char *expected_host = test_data[i].host;
122 const char *expected_port = test_data[i].port;
123 const char *expected_server_path = test_data[i].server_path;
124 const char *expected_repo_name = test_data[i].repo_name;
125 char *proto, *host, *port, *server_path, *repo_name;
127 err = got_fetch_parse_uri(&proto, &host, &port, &server_path,
128 &repo_name, uri);
129 if (err && err->code != test_data[i].errcode) {
130 test_printf("%d: error code %d; expected %d\n",
131 i, err->code, test_data[i].errcode);
132 return 0;
135 if (expected_proto == NULL && proto != NULL) {
136 test_printf("%d: proto %s; expected NULL\n", i, proto);
137 return 0;
139 if (expected_host == NULL && host != NULL) {
140 test_printf("%d: host %s; expected NULL\n", i, host);
141 return 0;
143 if (expected_port == NULL && port != NULL) {
144 test_printf("%d: port %s; expected NULL\n", i, port);
145 return 0;
147 if (expected_server_path == NULL && server_path != NULL) {
148 test_printf("%d: server path %s; expected NULL\n", i,
149 server_path);
150 return 0;
152 if (expected_repo_name == NULL && repo_name != NULL) {
153 test_printf("%d: repo name %s; expected NULL\n", i,
154 repo_name);
155 return 0;
158 if (expected_proto != NULL && proto == NULL) {
159 test_printf("%d: proto NULL; expected %s\n", i,
160 expected_proto);
161 return 0;
163 if (expected_host != NULL && host == NULL) {
164 test_printf("%d: host NULL; expected %s\n", i,
165 expected_host);
166 return 0;
168 if (expected_port != NULL && port == NULL) {
169 test_printf("%d: port NULL; expected %s\n", i,
170 expected_port);
171 return 0;
173 if (expected_server_path != NULL && server_path == NULL) {
174 test_printf("%d: server path %s; expected %s\n", i,
175 expected_server_path);
176 return 0;
178 if (expected_repo_name != NULL && repo_name == NULL) {
179 test_printf("%d: repo name NULL; expected %s\n", i,
180 repo_name);
181 return 0;
184 if (expected_proto != NULL && strcmp(expected_proto, proto)) {
185 test_printf("%d: proto %s; expected %s\n", i, proto,
186 expected_proto);
187 return 0;
190 free(proto);
191 proto = NULL;
192 free(host);
193 host = NULL;
194 free(port);
195 port = NULL;
196 free(server_path);
197 server_path = NULL;
198 free(repo_name);
199 repo_name = NULL;
202 return 1;
205 #define RUN_TEST(expr, name) \
206 { test_ok = (expr); \
207 printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
208 failure = (failure || !test_ok); }
210 void
211 usage(void)
213 fprintf(stderr, "usage: fetch_test [-v]\n");
216 int
217 main(int argc, char *argv[])
219 int test_ok = 0, failure = 0;
220 int ch;
222 #ifndef PROFILE
223 if (pledge("stdio", NULL) == -1)
224 err(1, "pledge");
225 #endif
227 while ((ch = getopt(argc, argv, "v")) != -1) {
228 switch (ch) {
229 case 'v':
230 verbose = 1;
231 break;
232 default:
233 usage();
234 return 1;
237 argc -= optind;
238 argv += optind;
240 RUN_TEST(fetch_parse_uri(), "fetch_parse_uri");
242 return failure ? 1 : 0;