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 |
|
16
|
10 |
struct url_schema basic_http = { 1, { "http" } };
|
15
|
11 |
|
|
12 |
struct url_test {
|
|
13 |
const char *url;
|
|
14 |
const struct url expected;
|
|
15 |
} url_tests[] = {
|
|
16 |
{ "localhost:http", {
|
|
17 |
NULL, NULL, NULL, "localhost", "http", NULL, NULL
|
|
18 |
} },
|
|
19 |
|
16
|
20 |
{ "http://example.com/path", {
|
|
21 |
&basic_http, NULL, NULL, "example.com", NULL, "path", NULL
|
|
22 |
} },
|
15
|
23 |
|
|
24 |
{ NULL, { } },
|
|
25 |
};
|
|
26 |
|
|
27 |
int cmp_url_str (const char *field, const char *test, const char *real) {
|
|
28 |
if (!test) {
|
|
29 |
if (real)
|
16
|
30 |
FAIL("%s shouldn't be present", field);
|
15
|
31 |
|
|
32 |
} else if (!real) {
|
16
|
33 |
FAIL("%s is missing", field);
|
15
|
34 |
|
|
35 |
} else {
|
|
36 |
if (strcmp(test, real) != 0)
|
16
|
37 |
FAIL("%s differs: %s -> %s", field, test, real);
|
15
|
38 |
}
|
|
39 |
|
|
40 |
// ok
|
|
41 |
return 0;
|
|
42 |
}
|
|
43 |
|
|
44 |
int cmp_url (const struct url *test, const struct url *real) {
|
|
45 |
int i;
|
|
46 |
|
|
47 |
// test schema
|
|
48 |
if (!test->schema) {
|
|
49 |
if (real->schema)
|
|
50 |
FAIL("test has no schema, but real does");
|
|
51 |
|
|
52 |
} else if (!real->schema) {
|
|
53 |
FAIL("test has a schema, but real doesn't");
|
|
54 |
|
|
55 |
} else {
|
|
56 |
if (test->schema->count != test->schema->count)
|
|
57 |
FAIL("inconsistent scheme count");
|
|
58 |
|
|
59 |
for (i = 0; i < test->schema->count; i++) {
|
|
60 |
if (strcmp(test->schema->list[i], real->schema->list[i]) != 0)
|
|
61 |
FAIL("differing scheme #%d", i);
|
|
62 |
}
|
|
63 |
}
|
|
64 |
|
|
65 |
// test username
|
|
66 |
if (cmp_url_str("username", test->username, real->username))
|
|
67 |
goto error;
|
|
68 |
|
|
69 |
// test password
|
|
70 |
if (cmp_url_str("password", test->password, real->password))
|
|
71 |
goto error;
|
|
72 |
|
|
73 |
// test hostname
|
|
74 |
if (cmp_url_str("hostname", test->hostname, real->hostname))
|
|
75 |
goto error;
|
|
76 |
|
|
77 |
// test service
|
|
78 |
if (cmp_url_str("service", test->service, real->service))
|
|
79 |
goto error;
|
|
80 |
|
|
81 |
// test path
|
|
82 |
if (cmp_url_str("path", test->path, real->path))
|
|
83 |
goto error;
|
|
84 |
|
|
85 |
// test query
|
|
86 |
if (!test->opts) {
|
|
87 |
if (real->opts)
|
|
88 |
FAIL("test has no opts, but real does");
|
|
89 |
|
|
90 |
} else if (!real->opts) {
|
|
91 |
FAIL("test has opts, but real doesn't");
|
|
92 |
|
|
93 |
} else {
|
|
94 |
if (test->opts->count != test->opts->count)
|
|
95 |
FAIL("inconsistent opts count");
|
|
96 |
|
|
97 |
for (i = 0; i < test->opts->count; i++) {
|
16
|
98 |
if (strcmp(test->opts->list[i].key, real->opts->list[i].key) != 0)
|
15
|
99 |
FAIL("differing scheme key #%d", i);
|
|
100 |
|
16
|
101 |
if (strcmp(test->opts->list[i].value, real->opts->list[i].value) != 0)
|
15
|
102 |
FAIL("differing scheme value #%d", i);
|
|
103 |
}
|
|
104 |
}
|
|
105 |
|
|
106 |
// ok
|
|
107 |
return 0;
|
|
108 |
|
|
109 |
error:
|
|
110 |
return -1;
|
|
111 |
}
|
|
112 |
|
|
113 |
void usage (const char *exec_name) {
|
|
114 |
printf("Usage: %s\n\n\tNo arguments are accepted\n", exec_name);
|
|
115 |
|
|
116 |
exit(EXIT_FAILURE);
|
|
117 |
}
|
|
118 |
|
|
119 |
int main (int argc, char **argv) {
|
|
120 |
const struct url_test *test;
|
|
121 |
struct url url;
|
|
122 |
|
|
123 |
if (argc > 1)
|
|
124 |
usage(argv[0]);
|
|
125 |
|
|
126 |
// run the tests
|
|
127 |
for (test = url_tests; test->url; test++) {
|
|
128 |
// first output the URL we are handling...
|
16
|
129 |
printf("%-80s - ", test->url);
|
15
|
130 |
fflush(stdout);
|
|
131 |
|
|
132 |
// parse the URL
|
|
133 |
memset(&url, 0, sizeof(url));
|
|
134 |
|
|
135 |
if (url_parse(&url, test->url)) {
|
|
136 |
printf("FATAL: url_parse failed\n");
|
|
137 |
return EXIT_FAILURE;
|
|
138 |
}
|
|
139 |
|
|
140 |
// compare it
|
|
141 |
if (cmp_url(&test->expected, &url)) {
|
|
142 |
printf("\texpected: ");
|
16
|
143 |
url_dump(&test->expected, stdout);
|
15
|
144 |
|
|
145 |
printf("\tresult: ");
|
16
|
146 |
url_dump(&url, stdout);
|
15
|
147 |
|
|
148 |
} else {
|
|
149 |
printf("OK\n\t");
|
16
|
150 |
url_dump(&url, stdout);
|
15
|
151 |
}
|
|
152 |
}
|
|
153 |
}
|
|
154 |
|