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>
30 #include "got_error.h"
31 #include "got_object.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"
41 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
47 test_printf(char *fmt, ...)
62 const struct got_error *err = NULL;
63 struct parse_uri_test {
68 const char *server_path;
69 const char *repo_name;
72 { "", NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
73 { "git:", NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
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 },
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,
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);
123 if (expected_proto == NULL && proto != NULL) {
124 test_printf("%d: proto %s; expected NULL\n", i, proto);
127 if (expected_host == NULL && host != NULL) {
128 test_printf("%d: host %s; expected NULL\n", i, host);
131 if (expected_port == NULL && port != NULL) {
132 test_printf("%d: port %s; expected NULL\n", i, port);
135 if (expected_server_path == NULL && server_path != NULL) {
136 test_printf("%d: server path %s; expected NULL\n", i,
140 if (expected_repo_name == NULL && repo_name != NULL) {
141 test_printf("%d: repo name %s; expected NULL\n", i,
146 if (expected_proto != NULL && proto == NULL) {
147 test_printf("%d: proto NULL; expected %s\n", i,
151 if (expected_host != NULL && host == NULL) {
152 test_printf("%d: host NULL; expected %s\n", i,
156 if (expected_port != NULL && port == NULL) {
157 test_printf("%d: port NULL; expected %s\n", i,
161 if (expected_server_path != NULL && server_path == NULL) {
162 test_printf("%d: server path %s; expected %s\n", i,
163 expected_server_path);
166 if (expected_repo_name != NULL && repo_name == NULL) {
167 test_printf("%d: repo name NULL; expected %s\n", i,
172 if (expected_proto != NULL && strcmp(expected_proto, proto)) {
173 test_printf("%d: proto %s; expected %s\n", i, proto,
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); }
201 fprintf(stderr, "usage: fetch_test [-v]\n");
205 main(int argc, char *argv[])
207 int test_ok = 0, failure = 0;
211 if (pledge("stdio", NULL) == -1)
215 while ((ch = getopt(argc, argv, "v")) != -1) {
228 RUN_TEST(fetch_parse_uri(), "fetch_parse_uri");
230 return failure ? 1 : 0;