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