3 #include <stdio.h> |
3 #include <stdio.h> |
4 #include <string.h> |
4 #include <string.h> |
5 |
5 |
6 #include "lib/url.h" |
6 #include "lib/url.h" |
7 |
7 |
8 #define FAIL(...) do { printf("FAIL: "); printf(__VA_ARGS__); return -1; } while (0) |
8 #define FAIL(...) do { printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); return -1; } while (0) |
9 |
9 |
|
10 struct url_schema basic_http = { 1, { "http" } }; |
10 |
11 |
11 struct url_test { |
12 struct url_test { |
12 const char *url; |
13 const char *url; |
13 const struct url expected; |
14 const struct url expected; |
14 } url_tests[] = { |
15 } url_tests[] = { |
15 { "localhost:http", { |
16 { "localhost:http", { |
16 NULL, NULL, NULL, "localhost", "http", NULL, NULL |
17 NULL, NULL, NULL, "localhost", "http", NULL, NULL |
17 } }, |
18 } }, |
18 |
19 |
19 /* { "http://example.com/path", { |
20 { "http://example.com/path", { |
20 { 1, { "http" } }, NULL, NULL, "example.com", NULL, "path", NULL |
21 &basic_http, NULL, NULL, "example.com", NULL, "path", NULL |
21 } }, */ |
22 } }, |
22 |
23 |
23 { NULL, { } }, |
24 { NULL, { } }, |
24 }; |
25 }; |
25 |
26 |
26 int cmp_url_str (const char *field, const char *test, const char *real) { |
27 int cmp_url_str (const char *field, const char *test, const char *real) { |
27 if (!test) { |
28 if (!test) { |
28 if (real) |
29 if (real) |
29 FAIL("%s: shouldn't be present", field); |
30 FAIL("%s shouldn't be present", field); |
30 |
31 |
31 } else if (!real) { |
32 } else if (!real) { |
32 FAIL("%s: missing", field); |
33 FAIL("%s is missing", field); |
33 |
34 |
34 } else { |
35 } else { |
35 if (strcmp(test, real) != 0) |
36 if (strcmp(test, real) != 0) |
36 FAIL("%s: differs: %s -> %s", field, test, real); |
37 FAIL("%s differs: %s -> %s", field, test, real); |
37 } |
38 } |
38 |
39 |
39 // ok |
40 // ok |
40 return 0; |
41 return 0; |
41 } |
42 } |
92 } else { |
93 } else { |
93 if (test->opts->count != test->opts->count) |
94 if (test->opts->count != test->opts->count) |
94 FAIL("inconsistent opts count"); |
95 FAIL("inconsistent opts count"); |
95 |
96 |
96 for (i = 0; i < test->opts->count; i++) { |
97 for (i = 0; i < test->opts->count; i++) { |
97 if (strcmp(test->opts->list[i]->key, real->opts->list[i]->key) != 0) |
98 if (strcmp(test->opts->list[i].key, real->opts->list[i].key) != 0) |
98 FAIL("differing scheme key #%d", i); |
99 FAIL("differing scheme key #%d", i); |
99 |
100 |
100 if (strcmp(test->opts->list[i]->value, real->opts->list[i]->value) != 0) |
101 if (strcmp(test->opts->list[i].value, real->opts->list[i].value) != 0) |
101 FAIL("differing scheme value #%d", i); |
102 FAIL("differing scheme value #%d", i); |
102 } |
103 } |
103 } |
104 } |
104 |
105 |
105 // ok |
106 // ok |
106 return 0; |
107 return 0; |
107 |
108 |
108 error: |
109 error: |
109 return -1; |
110 return -1; |
110 } |
|
111 |
|
112 void print_url_part (const char *field, const char *val) { |
|
113 if (val) { |
|
114 printf("%s=%s ", field, val); |
|
115 } |
|
116 } |
|
117 |
|
118 void print_url (const struct url *url) { |
|
119 int i; |
|
120 |
|
121 if (url->schema) { |
|
122 printf("schema="); |
|
123 |
|
124 for (i = 0; i < url->schema->count; i++) { |
|
125 if (i > 0) |
|
126 printf("+"); |
|
127 |
|
128 printf("%s", url->schema->list[i]); |
|
129 } |
|
130 |
|
131 printf(" "); |
|
132 } |
|
133 |
|
134 print_url_part("username", url->username); |
|
135 print_url_part("password", url->password); |
|
136 print_url_part("hostname", url->hostname); |
|
137 print_url_part("service", url->service); |
|
138 print_url_part("path", url->path); |
|
139 |
|
140 if (url->opts) { |
|
141 printf("opts: "); |
|
142 |
|
143 for (i = 0; i < url->opts->count; i++) { |
|
144 printf("%s=%s ", url->opts->list[i]->key, url->opts->list[i]->value); |
|
145 } |
|
146 } |
|
147 |
|
148 printf("\n"); |
|
149 } |
111 } |
150 |
112 |
151 void usage (const char *exec_name) { |
113 void usage (const char *exec_name) { |
152 printf("Usage: %s\n\n\tNo arguments are accepted\n", exec_name); |
114 printf("Usage: %s\n\n\tNo arguments are accepted\n", exec_name); |
153 |
115 |
162 usage(argv[0]); |
124 usage(argv[0]); |
163 |
125 |
164 // run the tests |
126 // run the tests |
165 for (test = url_tests; test->url; test++) { |
127 for (test = url_tests; test->url; test++) { |
166 // first output the URL we are handling... |
128 // first output the URL we are handling... |
167 printf("%s... ", test->url); |
129 printf("%-80s - ", test->url); |
168 fflush(stdout); |
130 fflush(stdout); |
169 |
131 |
170 // parse the URL |
132 // parse the URL |
171 memset(&url, 0, sizeof(url)); |
133 memset(&url, 0, sizeof(url)); |
172 |
134 |
176 } |
138 } |
177 |
139 |
178 // compare it |
140 // compare it |
179 if (cmp_url(&test->expected, &url)) { |
141 if (cmp_url(&test->expected, &url)) { |
180 printf("\texpected: "); |
142 printf("\texpected: "); |
181 print_url(&test->expected); |
143 url_dump(&test->expected, stdout); |
182 |
144 |
183 printf("\tresult: "); |
145 printf("\tresult: "); |
184 print_url(&url); |
146 url_dump(&url, stdout); |
185 |
147 |
186 } else { |
148 } else { |
187 printf("OK\n\t"); |
149 printf("OK\n\t"); |
188 print_url(&url); |
150 url_dump(&url, stdout); |
189 } |
151 } |
190 } |
152 } |
191 } |
153 } |
192 |
154 |