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 },
83 { "git://127.0.0.1/git/myrepo",
84 "git", "localhost", GOT_DEFAULT_GIT_PORT_STR, "git",
85 "myrepo", GOT_ERR_OK },
86 { "http://127.0.0.1/git/myrepo",
87 "http", "localhost", GOT_DEFAULT_GIT_PORT_STR, "git",
88 "myrepo", GOT_ERR_OK },
89 { "gopher://127.0.0.1/git/myrepo",
90 "gopher", "localhost", GOT_DEFAULT_GIT_PORT_STR, "git",
91 "myrepo", GOT_ERR_OK },
93 { "git://127.0.0.1:22/git/myrepo",
94 "git", "localhost", "22", "git", "myrepo", GOT_ERR_OK },
96 { "git://127.0.0.1/git/repos/foo/bar/myrepo.git",
97 "git", "localhost", GOT_DEFAULT_GIT_PORT_STR,
98 "git/repos/foo/bar", "myrepo", GOT_ERR_OK },
99 { "https://127.0.0.1/git/repos/foo/../bar/myrepo.git",
100 "https", "localhost", GOT_DEFAULT_GIT_PORT_STR,
101 "git/repos/foo/../bar", "myrepo", GOT_ERR_OK },
103 };
104 int i;
106 for (i = 0; i < nitems(test_data); i++) {
107 const char *uri = test_data[i].uri;
108 const char *expected_proto = test_data[i].proto;
109 const char *expected_host = test_data[i].host;
110 const char *expected_port = test_data[i].port;
111 const char *expected_server_path = test_data[i].server_path;
112 const char *expected_repo_name = test_data[i].repo_name;
113 char *proto, *host, *port, *server_path, *repo_name;
115 err = got_fetch_parse_uri(&proto, &host, &port, &server_path,
116 &repo_name, uri);
117 if (err && err->code != test_data[i].errcode) {
118 test_printf("%d: error code %d; expected %d\n",
119 i, err->code, test_data[i].errcode);
120 return 0;
123 if (expected_proto == NULL && proto != NULL) {
124 test_printf("%d: proto %s; expected NULL\n", i, proto);
125 return 0;
127 if (expected_host == NULL && host != NULL) {
128 test_printf("%d: host %s; expected NULL\n", i, host);
129 return 0;
131 if (expected_port == NULL && port != NULL) {
132 test_printf("%d: port %s; expected NULL\n", i, port);
133 return 0;
135 if (expected_server_path == NULL && server_path != NULL) {
136 test_printf("%d: server path %s; expected NULL\n", i,
137 server_path);
138 return 0;
140 if (expected_repo_name == NULL && repo_name != NULL) {
141 test_printf("%d: repo name %s; expected NULL\n", i,
142 repo_name);
143 return 0;
146 if (expected_proto != NULL && proto == NULL) {
147 test_printf("%d: proto NULL; expected %s\n", i,
148 expected_proto);
149 return 0;
151 if (expected_host != NULL && host == NULL) {
152 test_printf("%d: host NULL; expected %s\n", i,
153 expected_host);
154 return 0;
156 if (expected_port != NULL && port == NULL) {
157 test_printf("%d: port NULL; expected %s\n", i,
158 expected_port);
159 return 0;
161 if (expected_server_path != NULL && server_path == NULL) {
162 test_printf("%d: server path %s; expected %s\n", i,
163 expected_server_path);
164 return 0;
166 if (expected_repo_name != NULL && repo_name == NULL) {
167 test_printf("%d: repo name NULL; expected %s\n", i,
168 repo_name);
169 return 0;
172 if (expected_proto != NULL && strcmp(expected_proto, proto)) {
173 test_printf("%d: proto %s; expected %s\n", i, proto,
174 expected_proto);
175 return 0;
178 free(proto);
179 proto = NULL;
180 free(host);
181 host = NULL;
182 free(port);
183 port = NULL;
184 free(server_path);
185 server_path = NULL;
186 free(repo_name);
187 repo_name = NULL;
190 return 1;
193 #define RUN_TEST(expr, name) \
194 { test_ok = (expr); \
195 printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
196 failure = (failure || !test_ok); }
198 void
199 usage(void)
201 fprintf(stderr, "usage: fetch_test [-v]\n");
204 int
205 main(int argc, char *argv[])
207 int test_ok = 0, failure = 0;
208 int ch;
210 #ifndef PROFILE
211 if (pledge("stdio", NULL) == -1)
212 err(1, "pledge");
213 #endif
215 while ((ch = getopt(argc, argv, "v")) != -1) {
216 switch (ch) {
217 case 'v':
218 verbose = 1;
219 break;
220 default:
221 usage();
222 return 1;
225 argc -= optind;
226 argv += optind;
228 RUN_TEST(fetch_parse_uri(), "fetch_parse_uri");
230 return failure ? 1 : 0;