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_fetch.h"
34 #include "got_lib_object_idset.h"
35 #include "got_lib_sha1.h"
36 #include "got_lib_inflate.h"
37 #include "got_lib_delta.h"
39 #ifndef nitems
40 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
41 #endif
43 static int verbose;
45 void
46 test_printf(char *fmt, ...)
47 {
48 va_list ap;
50 if (!verbose)
51 return;
53 va_start(ap, fmt);
54 vprintf(fmt, ap);
55 va_end(ap);
56 }
58 static int
59 fetch_parse_uri(void)
60 {
61 const struct got_error *err = NULL;
62 struct parse_uri_test {
63 const char *uri;
64 const char *proto;
65 const char *host;
66 const char *port;
67 const char *server_path;
68 const char *repo_name;
69 int errcode;
70 } test_data[] = {
71 { "", NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
72 { "git:", NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
73 { "git://localhost/",
74 NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
75 { "git://localhost////",
76 NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
77 { "git://127.0.0.1/git/",
78 NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
79 { "git:///127.0.0.1/git/",
80 NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
82 { "git://127.0.0.1/git/myrepo",
83 "git", "localhost", GOT_DEFAULT_GIT_PORT_STR, "git",
84 "myrepo", GOT_ERR_OK },
85 { "http://127.0.0.1/git/myrepo",
86 "http", "localhost", GOT_DEFAULT_GIT_PORT_STR, "git",
87 "myrepo", GOT_ERR_OK },
88 { "gopher://127.0.0.1/git/myrepo",
89 "gopher", "localhost", GOT_DEFAULT_GIT_PORT_STR, "git",
90 "myrepo", GOT_ERR_OK },
92 { "git://127.0.0.1:22/git/myrepo",
93 "git", "localhost", "22", "git", "myrepo", GOT_ERR_OK },
95 { "git://127.0.0.1/git/repos/foo/bar/myrepo.git",
96 "git", "localhost", GOT_DEFAULT_GIT_PORT_STR,
97 "git/repos/foo/bar", "myrepo", GOT_ERR_OK },
98 { "https://127.0.0.1/git/repos/foo/../bar/myrepo.git",
99 "https", "localhost", GOT_DEFAULT_GIT_PORT_STR,
100 "git/repos/foo/../bar", "myrepo", GOT_ERR_OK },
102 };
103 int i;
105 for (i = 0; i < nitems(test_data); i++) {
106 const char *uri = test_data[i].uri;
107 const char *expected_proto = test_data[i].proto;
108 const char *expected_host = test_data[i].host;
109 const char *expected_port = test_data[i].port;
110 const char *expected_server_path = test_data[i].server_path;
111 const char *expected_repo_name = test_data[i].repo_name;
112 char *proto, *host, *port, *server_path, *repo_name;
114 err = got_fetch_parse_uri(&proto, &host, &port, &server_path,
115 &repo_name, uri);
116 if (err && err->code != test_data[i].errcode) {
117 test_printf("%d: error code %d; expected %d\n",
118 i, err->code, test_data[i].errcode);
119 return 0;
122 if (expected_proto == NULL && proto != NULL) {
123 test_printf("%d: proto %s; expected NULL\n", i, proto);
124 return 0;
126 if (expected_host == NULL && host != NULL) {
127 test_printf("%d: host %s; expected NULL\n", i, host);
128 return 0;
130 if (expected_port == NULL && port != NULL) {
131 test_printf("%d: port %s; expected NULL\n", i, port);
132 return 0;
134 if (expected_server_path == NULL && server_path != NULL) {
135 test_printf("%d: server path %s; expected NULL\n", i,
136 server_path);
137 return 0;
139 if (expected_repo_name == NULL && repo_name != NULL) {
140 test_printf("%d: repo name %s; expected NULL\n", i,
141 repo_name);
142 return 0;
145 if (expected_proto != NULL && proto == NULL) {
146 test_printf("%d: proto NULL; expected %s\n", i,
147 expected_proto);
148 return 0;
150 if (expected_host != NULL && host == NULL) {
151 test_printf("%d: host NULL; expected %s\n", i,
152 expected_host);
153 return 0;
155 if (expected_port != NULL && port == NULL) {
156 test_printf("%d: port NULL; expected %s\n", i,
157 expected_port);
158 return 0;
160 if (expected_server_path != NULL && server_path == NULL) {
161 test_printf("%d: server path %s; expected %s\n", i,
162 expected_server_path);
163 return 0;
165 if (expected_repo_name != NULL && repo_name == NULL) {
166 test_printf("%d: repo name NULL; expected %s\n", i,
167 repo_name);
168 return 0;
171 if (expected_proto != NULL && strcmp(expected_proto, proto)) {
172 test_printf("%d: proto %s; expected %s\n", i, proto,
173 expected_proto);
174 return 0;
177 free(proto);
178 proto = NULL;
179 free(host);
180 host = NULL;
181 free(port);
182 port = NULL;
183 free(server_path);
184 server_path = NULL;
185 free(repo_name);
186 repo_name = NULL;
189 return 1;
192 #define RUN_TEST(expr, name) \
193 { test_ok = (expr); \
194 printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
195 failure = (failure || !test_ok); }
197 void
198 usage(void)
200 fprintf(stderr, "usage: fetch_test [-v]\n");
203 int
204 main(int argc, char *argv[])
206 int test_ok = 0, failure = 0;
207 int ch;
209 #ifndef PROFILE
210 if (pledge("stdio", NULL) == -1)
211 err(1, "pledge");
212 #endif
214 while ((ch = getopt(argc, argv, "v")) != -1) {
215 switch (ch) {
216 case 'v':
217 verbose = 1;
218 break;
219 default:
220 usage();
221 return 1;
224 argc -= optind;
225 argv += optind;
227 RUN_TEST(fetch_parse_uri(), "fetch_parse_uri");
229 return failure ? 1 : 0;