7 #define FAIL(...) do { printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); return -1; } while (0)
10 basic_http = { 1, { "http" } },
11 svn_ssh = { 2, { "svn", "ssh" } },
12 schema_unix = { 1, { "unix" } }
16 opts_single = { 1, { { "key0", "val0" } } },
17 opts_multi = { 2, { { "key0", "val0" }, { "key1", "val1" } } },
18 opts_nullval = { 1, { { "keyN", NULL } } }
23 const struct url expected;
26 NULL, NULL, NULL, "localhost", "http", NULL, NULL
29 { "http://example.com/path", {
30 &basic_http, NULL, NULL, "example.com", NULL, "path", NULL
33 { "svn+ssh://user:passwd@someplace:someport/something", {
34 &svn_ssh, "user", "passwd", "someplace", "someport", "something"
38 NULL, "user", NULL, NULL, "service", NULL
41 { "unix:////tmp/foo.sock", {
42 &schema_unix, NULL, NULL, NULL, NULL, "/tmp/foo.sock"
45 { "unix:///tmp/foo.sock", {
46 &schema_unix, NULL, NULL, NULL, NULL, "tmp/foo.sock"
50 NULL, NULL, NULL, NULL, NULL, "tmp/foo.sock"
54 NULL, NULL, NULL, NULL, NULL, NULL, &opts_single
57 { "http://foo.com/index.php?key0=val0&key1=val1", {
58 &basic_http, NULL, NULL, "foo.com", NULL, "index.php", &opts_multi
61 { "example.org:81/?keyN", {
62 NULL, NULL, NULL, "example.org", "81", NULL, &opts_nullval
68 int cmp_url_str (const char *field, const char *test, const char *real) {
71 FAIL("%s shouldn't be present", field);
74 FAIL("%s is missing", field);
77 if (strcmp(test, real) != 0)
78 FAIL("%s differs: %s -> %s", field, test, real);
85 int cmp_url (const struct url *test, const struct url *real) {
91 FAIL("test has no schema, but real does");
93 } else if (!real->schema) {
94 FAIL("test has a schema, but real doesn't");
97 if (test->schema->count != test->schema->count)
98 FAIL("inconsistent scheme count");
100 for (i = 0; i < test->schema->count; i++) {
101 if (strcmp(test->schema->list[i], real->schema->list[i]) != 0)
102 FAIL("differing scheme #%d", i);
107 if (cmp_url_str("username", test->username, real->username))
111 if (cmp_url_str("password", test->password, real->password))
115 if (cmp_url_str("hostname", test->hostname, real->hostname))
119 if (cmp_url_str("service", test->service, real->service))
123 if (cmp_url_str("path", test->path, real->path))
129 FAIL("test has no opts, but real does");
131 } else if (!real->opts) {
132 FAIL("test has opts, but real doesn't");
135 if (test->opts->count != test->opts->count)
136 FAIL("inconsistent opts count");
138 for (i = 0; i < test->opts->count; i++) {
139 if (cmp_url_str("opt key", test->opts->list[i].key, real->opts->list[i].key))
140 FAIL("differing opt key #%d", i);
142 if (cmp_url_str("opt value", test->opts->list[i].value, real->opts->list[i].value))
143 FAIL("differing opt value #%d", i);
154 void usage (const char *exec_name) {
155 printf("Usage: %s\n\n\tNo arguments are accepted\n", exec_name);
160 int main (int argc, char **argv) {
161 const struct url_test *test;
168 for (test = url_tests; test->url; test++) {
169 // first output the URL we are handling...
170 printf("%-80s - ", test->url);
174 memset(&url, 0, sizeof(url));
176 if (url_parse(&url, test->url)) {
177 printf("FATAL: url_parse failed\n");
182 if (cmp_url(&test->expected, &url)) {
183 printf("\texpected: ");
184 url_dump(&test->expected, stdout);
186 printf("\tresult: ");
187 url_dump(&url, stdout);
191 url_dump(&url, stdout);