|
terom@15
|
1 |
#include <stdlib.h> |
|
terom@15
|
2 |
#include <stdio.h> |
|
terom@15
|
3 |
#include <string.h> |
|
terom@15
|
4 |
|
|
terom@15
|
5 |
#include "lib/url.h" |
|
terom@15
|
6 |
|
|
terom@15
|
7 |
#define FAIL(...) do { printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); return -1; } while (0) |
|
terom@16
|
8 |
|
|
terom@15
|
9 |
struct url_schema |
|
terom@17
|
10 |
basic_http = { 1, { "http" } }, |
|
terom@17
|
11 |
svn_ssh = { 2, { "svn", "ssh" } }, |
|
terom@18
|
12 |
schema_unix = { 1, { "unix" } } |
|
terom@18
|
13 |
; |
|
terom@18
|
14 |
|
|
terom@18
|
15 |
struct url_opts |
|
terom@18
|
16 |
opts_single = { 1, { { "key0", "val0" } } }, |
|
terom@18
|
17 |
opts_multi = { 2, { { "key0", "val0" }, { "key1", "val1" } } }, |
|
terom@18
|
18 |
opts_nullval = { 1, { { "keyN", NULL } } } |
|
terom@18
|
19 |
; |
|
terom@18
|
20 |
|
|
terom@15
|
21 |
struct url_test { |
|
terom@15
|
22 |
const char *url; |
|
terom@15
|
23 |
const struct url expected; |
|
terom@15
|
24 |
} url_tests[] = { |
|
terom@15
|
25 |
{ "localhost:http", { |
|
terom@15
|
26 |
NULL, NULL, NULL, "localhost", "http", NULL, NULL |
|
terom@15
|
27 |
} }, |
|
terom@15
|
28 |
|
|
terom@15
|
29 |
{ "http://example.com/path", { |
|
terom@16
|
30 |
&basic_http, NULL, NULL, "example.com", NULL, "path", NULL |
|
terom@16
|
31 |
} }, |
|
terom@16
|
32 |
|
|
terom@17
|
33 |
{ "svn+ssh://user:passwd@someplace:someport/something", { |
|
terom@17
|
34 |
&svn_ssh, "user", "passwd", "someplace", "someport", "something" |
|
terom@17
|
35 |
} }, |
|
terom@17
|
36 |
|
|
terom@17
|
37 |
{ "user@:service/", { |
|
terom@17
|
38 |
NULL, "user", NULL, NULL, "service", NULL |
|
terom@17
|
39 |
} }, |
|
terom@17
|
40 |
|
|
terom@18
|
41 |
{ "unix:////tmp/foo.sock", { |
|
terom@18
|
42 |
&schema_unix, NULL, NULL, NULL, NULL, "/tmp/foo.sock" |
|
terom@18
|
43 |
} }, |
|
terom@18
|
44 |
|
|
terom@18
|
45 |
{ "unix:///tmp/foo.sock", { |
|
terom@18
|
46 |
&schema_unix, NULL, NULL, NULL, NULL, "tmp/foo.sock" |
|
terom@18
|
47 |
} }, |
|
terom@18
|
48 |
|
|
terom@18
|
49 |
{ "/tmp/foo.sock", { |
|
terom@18
|
50 |
NULL, NULL, NULL, NULL, NULL, "tmp/foo.sock" |
|
terom@18
|
51 |
} }, |
|
terom@18
|
52 |
|
|
terom@18
|
53 |
{ "?key0=val0", { |
|
terom@18
|
54 |
NULL, NULL, NULL, NULL, NULL, NULL, &opts_single |
|
terom@18
|
55 |
} }, |
|
terom@18
|
56 |
|
|
terom@18
|
57 |
{ "http://foo.com/index.php?key0=val0&key1=val1", { |
|
terom@18
|
58 |
&basic_http, NULL, NULL, "foo.com", NULL, "index.php", &opts_multi |
|
terom@18
|
59 |
} }, |
|
terom@18
|
60 |
|
|
terom@18
|
61 |
{ "example.org:81/?keyN", { |
|
terom@18
|
62 |
NULL, NULL, NULL, "example.org", "81", NULL, &opts_nullval |
|
terom@18
|
63 |
} }, |
|
terom@18
|
64 |
|
|
terom@18
|
65 |
{ NULL, { } }, |
|
terom@15
|
66 |
}; |
|
terom@15
|
67 |
|
|
terom@15
|
68 |
int cmp_url_str (const char *field, const char *test, const char *real) { |
|
terom@15
|
69 |
if (!test) { |
|
terom@15
|
70 |
if (real) |
|
terom@15
|
71 |
FAIL("%s shouldn't be present", field); |
|
terom@16
|
72 |
|
|
terom@15
|
73 |
} else if (!real) { |
|
terom@15
|
74 |
FAIL("%s is missing", field); |
|
terom@16
|
75 |
|
|
terom@15
|
76 |
} else { |
|
terom@15
|
77 |
if (strcmp(test, real) != 0) |
|
terom@15
|
78 |
FAIL("%s differs: %s -> %s", field, test, real); |
|
terom@16
|
79 |
} |
|
terom@15
|
80 |
|
|
terom@15
|
81 |
// ok |
|
terom@15
|
82 |
return 0; |
|
terom@15
|
83 |
} |
|
terom@15
|
84 |
|
|
terom@15
|
85 |
int cmp_url (const struct url *test, const struct url *real) { |
|
terom@15
|
86 |
int i; |
|
terom@15
|
87 |
|
|
terom@15
|
88 |
// test schema |
|
terom@15
|
89 |
if (!test->schema) { |
|
terom@15
|
90 |
if (real->schema) |
|
terom@15
|
91 |
FAIL("test has no schema, but real does"); |
|
terom@15
|
92 |
|
|
terom@15
|
93 |
} else if (!real->schema) { |
|
terom@15
|
94 |
FAIL("test has a schema, but real doesn't"); |
|
terom@15
|
95 |
|
|
terom@15
|
96 |
} else { |
|
terom@15
|
97 |
if (test->schema->count != test->schema->count) |
|
terom@15
|
98 |
FAIL("inconsistent scheme count"); |
|
terom@15
|
99 |
|
|
terom@15
|
100 |
for (i = 0; i < test->schema->count; i++) { |
|
terom@15
|
101 |
if (strcmp(test->schema->list[i], real->schema->list[i]) != 0) |
|
terom@15
|
102 |
FAIL("differing scheme #%d", i); |
|
terom@15
|
103 |
} |
|
terom@15
|
104 |
} |
|
terom@15
|
105 |
|
|
terom@15
|
106 |
// test username |
|
terom@15
|
107 |
if (cmp_url_str("username", test->username, real->username)) |
|
terom@15
|
108 |
goto error; |
|
terom@15
|
109 |
|
|
terom@15
|
110 |
// test password |
|
terom@15
|
111 |
if (cmp_url_str("password", test->password, real->password)) |
|
terom@15
|
112 |
goto error; |
|
terom@15
|
113 |
|
|
terom@15
|
114 |
// test hostname |
|
terom@15
|
115 |
if (cmp_url_str("hostname", test->hostname, real->hostname)) |
|
terom@15
|
116 |
goto error; |
|
terom@15
|
117 |
|
|
terom@15
|
118 |
// test service |
|
terom@15
|
119 |
if (cmp_url_str("service", test->service, real->service)) |
|
terom@15
|
120 |
goto error; |
|
terom@15
|
121 |
|
|
terom@15
|
122 |
// test path |
|
terom@15
|
123 |
if (cmp_url_str("path", test->path, real->path)) |
|
terom@15
|
124 |
goto error; |
|
terom@15
|
125 |
|
|
terom@15
|
126 |
// test query |
|
terom@15
|
127 |
if (!test->opts) { |
|
terom@15
|
128 |
if (real->opts) |
|
terom@15
|
129 |
FAIL("test has no opts, but real does"); |
|
terom@15
|
130 |
|
|
terom@15
|
131 |
} else if (!real->opts) { |
|
terom@15
|
132 |
FAIL("test has opts, but real doesn't"); |
|
terom@15
|
133 |
|
|
terom@15
|
134 |
} else { |
|
terom@15
|
135 |
if (test->opts->count != test->opts->count) |
|
terom@15
|
136 |
FAIL("inconsistent opts count"); |
|
terom@15
|
137 |
|
|
terom@15
|
138 |
for (i = 0; i < test->opts->count; i++) { |
|
terom@15
|
139 |
if (cmp_url_str("opt key", test->opts->list[i].key, real->opts->list[i].key)) |
|
terom@18
|
140 |
FAIL("differing opt key #%d", i); |
|
terom@18
|
141 |
|
|
terom@15
|
142 |
if (cmp_url_str("opt value", test->opts->list[i].value, real->opts->list[i].value)) |
|
terom@18
|
143 |
FAIL("differing opt value #%d", i); |
|
terom@18
|
144 |
} |
|
terom@15
|
145 |
} |
|
terom@15
|
146 |
|
|
terom@15
|
147 |
// ok |
|
terom@15
|
148 |
return 0; |
|
terom@15
|
149 |
|
|
terom@15
|
150 |
error: |
|
terom@15
|
151 |
return -1; |
|
terom@15
|
152 |
} |
|
terom@15
|
153 |
|
|
terom@15
|
154 |
void usage (const char *exec_name) { |
|
terom@15
|
155 |
printf("Usage: %s\n\n\tNo arguments are accepted\n", exec_name); |
|
terom@15
|
156 |
|
|
terom@15
|
157 |
exit(EXIT_FAILURE); |
|
terom@15
|
158 |
} |
|
terom@15
|
159 |
|
|
terom@15
|
160 |
int main (int argc, char **argv) { |
|
terom@15
|
161 |
const struct url_test *test; |
|
terom@15
|
162 |
struct url url; |
|
terom@15
|
163 |
|
|
terom@15
|
164 |
if (argc > 1) |
|
terom@15
|
165 |
usage(argv[0]); |
|
terom@15
|
166 |
|
|
terom@15
|
167 |
// run the tests |
|
terom@15
|
168 |
for (test = url_tests; test->url; test++) { |
|
terom@15
|
169 |
// first output the URL we are handling... |
|
terom@15
|
170 |
printf("%-80s - ", test->url); |
|
terom@16
|
171 |
fflush(stdout); |
|
terom@15
|
172 |
|
|
terom@15
|
173 |
// parse the URL |
|
terom@15
|
174 |
memset(&url, 0, sizeof(url)); |
|
terom@15
|
175 |
|
|
terom@15
|
176 |
if (url_parse(&url, test->url)) { |
|
terom@15
|
177 |
printf("FATAL: url_parse failed\n"); |
|
terom@15
|
178 |
return EXIT_FAILURE; |
|
terom@15
|
179 |
} |
|
terom@15
|
180 |
|
|
terom@15
|
181 |
// compare it |
|
terom@15
|
182 |
if (cmp_url(&test->expected, &url)) { |
|
terom@15
|
183 |
printf("\texpected: "); |
|
terom@15
|
184 |
url_dump(&test->expected, stdout); |
|
terom@16
|
185 |
|
|
terom@15
|
186 |
printf("\tresult: "); |
|
terom@15
|
187 |
url_dump(&url, stdout); |
|
terom@16
|
188 |
|
|
terom@15
|
189 |
} else { |
|
terom@15
|
190 |
printf("OK\n\t"); |
|
terom@15
|
191 |
url_dump(&url, stdout); |
|
terom@16
|
192 |
} |
|
terom@15
|
193 |
|
|
terom@18
|
194 |
printf("\n"); |
|
terom@18
|
195 |
} |
|
terom@15
|
196 |
} |
|
terom@15
|
197 |
|