Blame


1 82ebf666 2020-03-18 stsp /*
2 82ebf666 2020-03-18 stsp * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
3 82ebf666 2020-03-18 stsp *
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.
7 82ebf666 2020-03-18 stsp *
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.
15 82ebf666 2020-03-18 stsp */
16 82ebf666 2020-03-18 stsp
17 82ebf666 2020-03-18 stsp #include <sys/queue.h>
18 82ebf666 2020-03-18 stsp
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 82ebf666 2020-03-18 stsp #include <zlib.h>
28 82ebf666 2020-03-18 stsp #include <time.h>
29 82ebf666 2020-03-18 stsp
30 82ebf666 2020-03-18 stsp #include "got_error.h"
31 82ebf666 2020-03-18 stsp #include "got_object.h"
32 629bd8f3 2020-03-18 stsp #include "got_path.h"
33 82ebf666 2020-03-18 stsp #include "got_fetch.h"
34 82ebf666 2020-03-18 stsp
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"
39 82ebf666 2020-03-18 stsp
40 82ebf666 2020-03-18 stsp #ifndef nitems
41 82ebf666 2020-03-18 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
42 82ebf666 2020-03-18 stsp #endif
43 82ebf666 2020-03-18 stsp
44 82ebf666 2020-03-18 stsp static int verbose;
45 82ebf666 2020-03-18 stsp
46 82ebf666 2020-03-18 stsp void
47 82ebf666 2020-03-18 stsp test_printf(char *fmt, ...)
48 82ebf666 2020-03-18 stsp {
49 82ebf666 2020-03-18 stsp va_list ap;
50 82ebf666 2020-03-18 stsp
51 82ebf666 2020-03-18 stsp if (!verbose)
52 82ebf666 2020-03-18 stsp return;
53 82ebf666 2020-03-18 stsp
54 82ebf666 2020-03-18 stsp va_start(ap, fmt);
55 82ebf666 2020-03-18 stsp vprintf(fmt, ap);
56 82ebf666 2020-03-18 stsp va_end(ap);
57 82ebf666 2020-03-18 stsp }
58 82ebf666 2020-03-18 stsp
59 82ebf666 2020-03-18 stsp static int
60 82ebf666 2020-03-18 stsp fetch_parse_uri(void)
61 82ebf666 2020-03-18 stsp {
62 82ebf666 2020-03-18 stsp const struct got_error *err = NULL;
63 82ebf666 2020-03-18 stsp struct parse_uri_test {
64 82ebf666 2020-03-18 stsp const char *uri;
65 82ebf666 2020-03-18 stsp const char *proto;
66 82ebf666 2020-03-18 stsp const char *host;
67 82ebf666 2020-03-18 stsp const char *port;
68 82ebf666 2020-03-18 stsp const char *server_path;
69 82ebf666 2020-03-18 stsp const char *repo_name;
70 82ebf666 2020-03-18 stsp int errcode;
71 82ebf666 2020-03-18 stsp } test_data[] = {
72 82ebf666 2020-03-18 stsp { "", NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
73 82ebf666 2020-03-18 stsp { "git:", NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
74 82ebf666 2020-03-18 stsp { "git://localhost/",
75 82ebf666 2020-03-18 stsp NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
76 82ebf666 2020-03-18 stsp { "git://localhost////",
77 82ebf666 2020-03-18 stsp NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
78 82ebf666 2020-03-18 stsp { "git://127.0.0.1/git/",
79 82ebf666 2020-03-18 stsp NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
80 82ebf666 2020-03-18 stsp { "git:///127.0.0.1/git/",
81 82ebf666 2020-03-18 stsp NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
82 82ebf666 2020-03-18 stsp
83 82ebf666 2020-03-18 stsp { "git://127.0.0.1/git/myrepo",
84 82ebf666 2020-03-18 stsp "git", "localhost", GOT_DEFAULT_GIT_PORT_STR, "git",
85 82ebf666 2020-03-18 stsp "myrepo", GOT_ERR_OK },
86 82ebf666 2020-03-18 stsp { "http://127.0.0.1/git/myrepo",
87 82ebf666 2020-03-18 stsp "http", "localhost", GOT_DEFAULT_GIT_PORT_STR, "git",
88 82ebf666 2020-03-18 stsp "myrepo", GOT_ERR_OK },
89 82ebf666 2020-03-18 stsp { "gopher://127.0.0.1/git/myrepo",
90 82ebf666 2020-03-18 stsp "gopher", "localhost", GOT_DEFAULT_GIT_PORT_STR, "git",
91 82ebf666 2020-03-18 stsp "myrepo", GOT_ERR_OK },
92 82ebf666 2020-03-18 stsp
93 82ebf666 2020-03-18 stsp { "git://127.0.0.1:22/git/myrepo",
94 82ebf666 2020-03-18 stsp "git", "localhost", "22", "git", "myrepo", GOT_ERR_OK },
95 82ebf666 2020-03-18 stsp
96 82ebf666 2020-03-18 stsp { "git://127.0.0.1/git/repos/foo/bar/myrepo.git",
97 82ebf666 2020-03-18 stsp "git", "localhost", GOT_DEFAULT_GIT_PORT_STR,
98 82ebf666 2020-03-18 stsp "git/repos/foo/bar", "myrepo", GOT_ERR_OK },
99 82ebf666 2020-03-18 stsp { "https://127.0.0.1/git/repos/foo/../bar/myrepo.git",
100 82ebf666 2020-03-18 stsp "https", "localhost", GOT_DEFAULT_GIT_PORT_STR,
101 82ebf666 2020-03-18 stsp "git/repos/foo/../bar", "myrepo", GOT_ERR_OK },
102 82ebf666 2020-03-18 stsp
103 82ebf666 2020-03-18 stsp };
104 82ebf666 2020-03-18 stsp int i;
105 82ebf666 2020-03-18 stsp
106 82ebf666 2020-03-18 stsp for (i = 0; i < nitems(test_data); i++) {
107 82ebf666 2020-03-18 stsp const char *uri = test_data[i].uri;
108 82ebf666 2020-03-18 stsp const char *expected_proto = test_data[i].proto;
109 82ebf666 2020-03-18 stsp const char *expected_host = test_data[i].host;
110 82ebf666 2020-03-18 stsp const char *expected_port = test_data[i].port;
111 82ebf666 2020-03-18 stsp const char *expected_server_path = test_data[i].server_path;
112 82ebf666 2020-03-18 stsp const char *expected_repo_name = test_data[i].repo_name;
113 82ebf666 2020-03-18 stsp char *proto, *host, *port, *server_path, *repo_name;
114 82ebf666 2020-03-18 stsp
115 82ebf666 2020-03-18 stsp err = got_fetch_parse_uri(&proto, &host, &port, &server_path,
116 82ebf666 2020-03-18 stsp &repo_name, uri);
117 82ebf666 2020-03-18 stsp if (err && err->code != test_data[i].errcode) {
118 82ebf666 2020-03-18 stsp test_printf("%d: error code %d; expected %d\n",
119 82ebf666 2020-03-18 stsp i, err->code, test_data[i].errcode);
120 82ebf666 2020-03-18 stsp return 0;
121 82ebf666 2020-03-18 stsp }
122 82ebf666 2020-03-18 stsp
123 82ebf666 2020-03-18 stsp if (expected_proto == NULL && proto != NULL) {
124 82ebf666 2020-03-18 stsp test_printf("%d: proto %s; expected NULL\n", i, proto);
125 82ebf666 2020-03-18 stsp return 0;
126 82ebf666 2020-03-18 stsp }
127 82ebf666 2020-03-18 stsp if (expected_host == NULL && host != NULL) {
128 82ebf666 2020-03-18 stsp test_printf("%d: host %s; expected NULL\n", i, host);
129 82ebf666 2020-03-18 stsp return 0;
130 82ebf666 2020-03-18 stsp }
131 82ebf666 2020-03-18 stsp if (expected_port == NULL && port != NULL) {
132 82ebf666 2020-03-18 stsp test_printf("%d: port %s; expected NULL\n", i, port);
133 82ebf666 2020-03-18 stsp return 0;
134 82ebf666 2020-03-18 stsp }
135 82ebf666 2020-03-18 stsp if (expected_server_path == NULL && server_path != NULL) {
136 82ebf666 2020-03-18 stsp test_printf("%d: server path %s; expected NULL\n", i,
137 82ebf666 2020-03-18 stsp server_path);
138 82ebf666 2020-03-18 stsp return 0;
139 82ebf666 2020-03-18 stsp }
140 82ebf666 2020-03-18 stsp if (expected_repo_name == NULL && repo_name != NULL) {
141 82ebf666 2020-03-18 stsp test_printf("%d: repo name %s; expected NULL\n", i,
142 82ebf666 2020-03-18 stsp repo_name);
143 82ebf666 2020-03-18 stsp return 0;
144 82ebf666 2020-03-18 stsp }
145 82ebf666 2020-03-18 stsp
146 82ebf666 2020-03-18 stsp if (expected_proto != NULL && proto == NULL) {
147 82ebf666 2020-03-18 stsp test_printf("%d: proto NULL; expected %s\n", i,
148 82ebf666 2020-03-18 stsp expected_proto);
149 82ebf666 2020-03-18 stsp return 0;
150 82ebf666 2020-03-18 stsp }
151 82ebf666 2020-03-18 stsp if (expected_host != NULL && host == NULL) {
152 82ebf666 2020-03-18 stsp test_printf("%d: host NULL; expected %s\n", i,
153 82ebf666 2020-03-18 stsp expected_host);
154 82ebf666 2020-03-18 stsp return 0;
155 82ebf666 2020-03-18 stsp }
156 82ebf666 2020-03-18 stsp if (expected_port != NULL && port == NULL) {
157 82ebf666 2020-03-18 stsp test_printf("%d: port NULL; expected %s\n", i,
158 82ebf666 2020-03-18 stsp expected_port);
159 82ebf666 2020-03-18 stsp return 0;
160 82ebf666 2020-03-18 stsp }
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 %s\n", i,
163 82ebf666 2020-03-18 stsp expected_server_path);
164 82ebf666 2020-03-18 stsp return 0;
165 82ebf666 2020-03-18 stsp }
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 NULL; expected %s\n", i,
168 82ebf666 2020-03-18 stsp repo_name);
169 82ebf666 2020-03-18 stsp return 0;
170 82ebf666 2020-03-18 stsp }
171 82ebf666 2020-03-18 stsp
172 82ebf666 2020-03-18 stsp if (expected_proto != NULL && strcmp(expected_proto, proto)) {
173 82ebf666 2020-03-18 stsp test_printf("%d: proto %s; expected %s\n", i, proto,
174 82ebf666 2020-03-18 stsp expected_proto);
175 82ebf666 2020-03-18 stsp return 0;
176 82ebf666 2020-03-18 stsp }
177 82ebf666 2020-03-18 stsp
178 82ebf666 2020-03-18 stsp free(proto);
179 82ebf666 2020-03-18 stsp proto = NULL;
180 82ebf666 2020-03-18 stsp free(host);
181 82ebf666 2020-03-18 stsp host = NULL;
182 82ebf666 2020-03-18 stsp free(port);
183 82ebf666 2020-03-18 stsp port = NULL;
184 82ebf666 2020-03-18 stsp free(server_path);
185 82ebf666 2020-03-18 stsp server_path = NULL;
186 82ebf666 2020-03-18 stsp free(repo_name);
187 82ebf666 2020-03-18 stsp repo_name = NULL;
188 82ebf666 2020-03-18 stsp }
189 82ebf666 2020-03-18 stsp
190 82ebf666 2020-03-18 stsp return 1;
191 82ebf666 2020-03-18 stsp }
192 82ebf666 2020-03-18 stsp
193 82ebf666 2020-03-18 stsp #define RUN_TEST(expr, name) \
194 82ebf666 2020-03-18 stsp { test_ok = (expr); \
195 82ebf666 2020-03-18 stsp printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
196 82ebf666 2020-03-18 stsp failure = (failure || !test_ok); }
197 82ebf666 2020-03-18 stsp
198 82ebf666 2020-03-18 stsp void
199 82ebf666 2020-03-18 stsp usage(void)
200 82ebf666 2020-03-18 stsp {
201 82ebf666 2020-03-18 stsp fprintf(stderr, "usage: fetch_test [-v]\n");
202 82ebf666 2020-03-18 stsp }
203 82ebf666 2020-03-18 stsp
204 82ebf666 2020-03-18 stsp int
205 82ebf666 2020-03-18 stsp main(int argc, char *argv[])
206 82ebf666 2020-03-18 stsp {
207 82ebf666 2020-03-18 stsp int test_ok = 0, failure = 0;
208 82ebf666 2020-03-18 stsp int ch;
209 82ebf666 2020-03-18 stsp
210 82ebf666 2020-03-18 stsp #ifndef PROFILE
211 82ebf666 2020-03-18 stsp if (pledge("stdio", NULL) == -1)
212 82ebf666 2020-03-18 stsp err(1, "pledge");
213 82ebf666 2020-03-18 stsp #endif
214 82ebf666 2020-03-18 stsp
215 82ebf666 2020-03-18 stsp while ((ch = getopt(argc, argv, "v")) != -1) {
216 82ebf666 2020-03-18 stsp switch (ch) {
217 82ebf666 2020-03-18 stsp case 'v':
218 82ebf666 2020-03-18 stsp verbose = 1;
219 82ebf666 2020-03-18 stsp break;
220 82ebf666 2020-03-18 stsp default:
221 82ebf666 2020-03-18 stsp usage();
222 82ebf666 2020-03-18 stsp return 1;
223 82ebf666 2020-03-18 stsp }
224 82ebf666 2020-03-18 stsp }
225 82ebf666 2020-03-18 stsp argc -= optind;
226 82ebf666 2020-03-18 stsp argv += optind;
227 82ebf666 2020-03-18 stsp
228 82ebf666 2020-03-18 stsp RUN_TEST(fetch_parse_uri(), "fetch_parse_uri");
229 82ebf666 2020-03-18 stsp
230 82ebf666 2020-03-18 stsp return failure ? 1 : 0;
231 82ebf666 2020-03-18 stsp }