Debian Codegraph @

deps/curl/tests/libtest/lib1560.c

raw
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2020, Daniel Stenberg, <[email protected]>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22
23 /*
24 * Note:
25 *
26 * Since the URL parser by default only accepts schemes that *this instance*
27 * of libcurl supports, make sure that the test1560 file lists all the schemes
28 * that this test will assume to be present!
29 */
30
31 #include "test.h"
32
33 #include "testutil.h"
34 #include "warnless.h"
35 #include "memdebug.h" /* LAST include file */
36
37 struct part {
38 CURLUPart part;
39 const char *name;
40 };
41
42
43 static int checkparts(CURLU *u, const char *in, const char *wanted,
44 unsigned int getflags)
45 {
46 int i;
47 CURLUcode rc;
48 char buf[256];
49 char *bufp = &buf[0];
50 size_t len = sizeof(buf);
51 struct part parts[] = {
52 {CURLUPART_SCHEME, "scheme"},
53 {CURLUPART_USER, "user"},
54 {CURLUPART_PASSWORD, "password"},
55 {CURLUPART_OPTIONS, "options"},
56 {CURLUPART_HOST, "host"},
57 {CURLUPART_PORT, "port"},
58 {CURLUPART_PATH, "path"},
59 {CURLUPART_QUERY, "query"},
60 {CURLUPART_FRAGMENT, "fragment"},
61 {0, NULL}
62 };
63 buf[0] = 0;
64
65 for(i = 0; parts[i].name; i++) {
66 char *p = NULL;
67 size_t n;
68 rc = curl_url_get(u, parts[i].part, &p, getflags);
69 if(!rc && p) {
70 msnprintf(bufp, len, "%s%s", buf[0]?" | ":"", p);
71 }
72 else
73 msnprintf(bufp, len, "%s[%d]", buf[0]?" | ":"", (int)rc);
74
75 n = strlen(bufp);
76 bufp += n;
77 len -= n;
78 curl_free(p);
79 }
80 if(strcmp(buf, wanted)) {
81 fprintf(stderr, "in: %s\nwanted: %s\ngot: %s\n", in, wanted, buf);
82 return 1;
83 }
84 return 0;
85 }
86
87 struct redircase {
88 const char *in;
89 const char *set;
90 const char *out;
91 unsigned int urlflags;
92 unsigned int setflags;
93 CURLUcode ucode;
94 };
95
96 struct setcase {
97 const char *in;
98 const char *set;
99 const char *out;
100 unsigned int urlflags;
101 unsigned int setflags;
102 CURLUcode ucode; /* for the main URL set */
103 CURLUcode pcode; /* for updating parts */
104 };
105
106 struct testcase {
107 const char *in;
108 const char *out;
109 unsigned int urlflags;
110 unsigned int getflags;
111 CURLUcode ucode;
112 };
113
114 struct urltestcase {
115 const char *in;
116 const char *out;
117 unsigned int urlflags; /* pass to curl_url() */
118 unsigned int getflags; /* pass to curl_url_get() */
119 CURLUcode ucode;
120 };
121
122 struct querycase {
123 const char *in;
124 const char *q;
125 const char *out;
126 unsigned int urlflags; /* pass to curl_url() */
127 unsigned int qflags; /* pass to curl_url_get() */
128 CURLUcode ucode;
129 };
130
131 static struct testcase get_parts_list[] ={
132 {"user:[email protected]/color/#green?no-black",
133 "ftp | user | moo | [13] | ftp.example.com | [15] | /color/ | [16] | "
134 "green?no-black",
135 CURLU_GUESS_SCHEME, 0, CURLUE_OK },
136 {"ftp.user:[email protected]/color/#green?no-black",
137 "http | ftp.user | moo | [13] | example.com | [15] | /color/ | [16] | "
138 "green?no-black",
139 CURLU_GUESS_SCHEME, 0, CURLUE_OK },
140 #ifdef WIN32
141 {"file:/C:\\programs\\foo",
142 "file | [11] | [12] | [13] | [14] | [15] | C:\\programs\\foo | [16] | [17]",
143 CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
144 {"file://C:\\programs\\foo",
145 "file | [11] | [12] | [13] | [14] | [15] | C:\\programs\\foo | [16] | [17]",
146 CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
147 {"file:///C:\\programs\\foo",
148 "file | [11] | [12] | [13] | [14] | [15] | C:\\programs\\foo | [16] | [17]",
149 CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
150 #endif
151 {"https://example.com/color/#green?no-black",
152 "https | [11] | [12] | [13] | example.com | [15] | /color/ | [16] | "
153 "green?no-black",
154 CURLU_DEFAULT_SCHEME, 0, CURLUE_OK },
155 {"https://example.com/color/#green#no-black",
156 "https | [11] | [12] | [13] | example.com | [15] | /color/ | [16] | "
157 "green#no-black",
158 CURLU_DEFAULT_SCHEME, 0, CURLUE_OK },
159 {"https://example.com/color/?green#no-black",
160 "https | [11] | [12] | [13] | example.com | [15] | /color/ | green | "
161 "no-black",
162 CURLU_DEFAULT_SCHEME, 0, CURLUE_OK },
163 {"https://example.com/#color/?green#no-black",
164 "https | [11] | [12] | [13] | example.com | [15] | / | [16] | "
165 "color/?green#no-black",
166 CURLU_DEFAULT_SCHEME, 0, CURLUE_OK },
167 {"https://example.#com/color/?green#no-black",
168 "https | [11] | [12] | [13] | example. | [15] | / | [16] | "
169 "com/color/?green#no-black",
170 CURLU_DEFAULT_SCHEME, 0, CURLUE_OK },
171 {"http://[ab.be:1]/x", "",
172 CURLU_DEFAULT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
173 {"http://[ab.be]/x", "",
174 CURLU_DEFAULT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
175 /* URL without host name */
176 {"http://a:b@/x", "",
177 CURLU_DEFAULT_SCHEME, 0, CURLUE_NO_HOST},
178 {"boing:80",
179 "https | [11] | [12] | [13] | boing | 80 | / | [16] | [17]",
180 CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
181 {"http://[fd00:a41::50]:8080",
182 "http | [11] | [12] | [13] | [fd00:a41::50] | 8080 | / | [16] | [17]",
183 CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
184 {"http://[fd00:a41::50]/",
185 "http | [11] | [12] | [13] | [fd00:a41::50] | [15] | / | [16] | [17]",
186 CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
187 {"http://[fd00:a41::50]",
188 "http | [11] | [12] | [13] | [fd00:a41::50] | [15] | / | [16] | [17]",
189 CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
190 {"https://[::1%252]:1234",
191 "https | [11] | [12] | [13] | [::1] | 1234 | / | [16] | [17]",
192 CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
193
194 /* here's "bad" zone id */
195 {"https://[fe80::20c:29ff:fe9c:409b%eth0]:1234",
196 "https | [11] | [12] | [13] | [fe80::20c:29ff:fe9c:409b] | 1234 "
197 "| / | [16] | [17]",
198 CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
199 {"https://127.0.0.1:443",
200 "https | [11] | [12] | [13] | 127.0.0.1 | [15] | / | [16] | [17]",
201 0, CURLU_NO_DEFAULT_PORT, CURLUE_OK},
202 {"http://%3a:%3a@ex%0ample/%3f+?+%3f+%23#+%23%3f%g7",
203 "http | : | : | [13] | [6] | [15] | /?+ | ? # | +#?%g7",
204 0, CURLU_URLDECODE, CURLUE_OK},
205 {"http://%3a:%3a@ex%0ample/%3f?%3f%35#%35%3f%g7",
206 "http | %3a | %3a | [13] | ex%0ample | [15] | /%3f | %3f%35 | %35%3f%g7",
207 0, 0, CURLUE_OK},
208 {"http://HO0_-st%41/",
209 "http | [11] | [12] | [13] | HO0_-st%41 | [15] | / | [16] | [17]",
210 0, 0, CURLUE_OK},
211 {"file://hello.html",
212 "",
213 0, 0, CURLUE_MALFORMED_INPUT},
214 {"http://HO0_-st/",
215 "http | [11] | [12] | [13] | HO0_-st | [15] | / | [16] | [17]",
216 0, 0, CURLUE_OK},
217 {"imap://user:pass;option@server/path",
218 "imap | user | pass | option | server | [15] | /path | [16] | [17]",
219 0, 0, CURLUE_OK},
220 {"http://user:pass;option@server/path",
221 "http | user | pass;option | [13] | server | [15] | /path | [16] | [17]",
222 0, 0, CURLUE_OK},
223 {"file:/hello.html",
224 "file | [11] | [12] | [13] | [14] | [15] | /hello.html | [16] | [17]",
225 0, 0, CURLUE_OK},
226 {"file://127.0.0.1/hello.html",
227 "file | [11] | [12] | [13] | [14] | [15] | /hello.html | [16] | [17]",
228 0, 0, CURLUE_OK},
229 {"file:////hello.html",
230 "file | [11] | [12] | [13] | [14] | [15] | //hello.html | [16] | [17]",
231 0, 0, CURLUE_OK},
232 {"file:///hello.html",
233 "file | [11] | [12] | [13] | [14] | [15] | /hello.html | [16] | [17]",
234 0, 0, CURLUE_OK},
235 {"https://127.0.0.1",
236 "https | [11] | [12] | [13] | 127.0.0.1 | 443 | / | [16] | [17]",
237 0, CURLU_DEFAULT_PORT, CURLUE_OK},
238 {"https://127.0.0.1",
239 "https | [11] | [12] | [13] | 127.0.0.1 | [15] | / | [16] | [17]",
240 CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
241 {"https://[::1]:1234",
242 "https | [11] | [12] | [13] | [::1] | 1234 | / | [16] | [17]",
243 CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
244 {"https://127abc.com",
245 "https | [11] | [12] | [13] | 127abc.com | [15] | / | [16] | [17]",
246 CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
247 {"https:// example.com?check",
248 "",
249 CURLU_DEFAULT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
250 {"https://e x a m p l e.com?check",
251 "",
252 CURLU_DEFAULT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
253 {"https://example.com?check",
254 "https | [11] | [12] | [13] | example.com | [15] | / | check | [17]",
255 CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
256 {"https://example.com:65536",
257 "",
258 CURLU_DEFAULT_SCHEME, 0, CURLUE_BAD_PORT_NUMBER},
259 {"https://example.com:0#moo",
260 "",
261 CURLU_DEFAULT_SCHEME, 0, CURLUE_BAD_PORT_NUMBER},
262 {"https://example.com:01#moo",
263 "https | [11] | [12] | [13] | example.com | 1 | / | "
264 "[16] | moo",
265 CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
266 {"https://example.com:1#moo",
267 "https | [11] | [12] | [13] | example.com | 1 | / | "
268 "[16] | moo",
269 CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
270 {"http://example.com#moo",
271 "http | [11] | [12] | [13] | example.com | [15] | / | "
272 "[16] | moo",
273 CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
274 {"http://example.com",
275 "http | [11] | [12] | [13] | example.com | [15] | / | "
276 "[16] | [17]",
277 CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
278 {"http://example.com/path/html",
279 "http | [11] | [12] | [13] | example.com | [15] | /path/html | "
280 "[16] | [17]",
281 CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
282 {"http://example.com/path/html?query=name",
283 "http | [11] | [12] | [13] | example.com | [15] | /path/html | "
284 "query=name | [17]",
285 CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
286 {"http://example.com/path/html?query=name#anchor",
287 "http | [11] | [12] | [13] | example.com | [15] | /path/html | "
288 "query=name | anchor",
289 CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
290 {"http://example.com:1234/path/html?query=name#anchor",
291 "http | [11] | [12] | [13] | example.com | 1234 | /path/html | "
292 "query=name | anchor",
293 CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
294 {"http:///user:[email protected]:1234/path/html?query=name#anchor",
295 "http | user | password | [13] | example.com | 1234 | /path/html | "
296 "query=name | anchor",
297 CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
298 {"https://user:[email protected]:1234/path/html?query=name#anchor",
299 "https | user | password | [13] | example.com | 1234 | /path/html | "
300 "query=name | anchor",
301 CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
302 {"http://user:[email protected]:1234/path/html?query=name#anchor",
303 "http | user | password | [13] | example.com | 1234 | /path/html | "
304 "query=name | anchor",
305 CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
306 {"http:/user:[email protected]:1234/path/html?query=name#anchor",
307 "http | user | password | [13] | example.com | 1234 | /path/html | "
308 "query=name | anchor",
309 CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
310 {"http:////user:[email protected]:1234/path/html?query=name#anchor",
311 "",
312 CURLU_DEFAULT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
313 {NULL, NULL, 0, 0, CURLUE_OK},
314 };
315
316 static struct urltestcase get_url_list[] = {
317 /* 40 bytes scheme is the max allowed */
318 {"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA://hostname/path",
319 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa://hostname/path",
320 CURLU_NON_SUPPORT_SCHEME, 0, CURLUE_OK},
321 /* 41 bytes scheme is not allowed */
322 {"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA://hostname/path",
323 "",
324 CURLU_NON_SUPPORT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
325 {"https://[fe80::20c:29ff:fe9c:409b%]:1234",
326 "",
327 0, 0, CURLUE_MALFORMED_INPUT},
328 {"https://[fe80::20c:29ff:fe9c:409b%25]:1234",
329 "https://[fe80::20c:29ff:fe9c:409b%2525]:1234/",
330 0, 0, CURLUE_OK},
331 {"https://[fe80::20c:29ff:fe9c:409b%eth0]:1234",
332 "https://[fe80::20c:29ff:fe9c:409b%25eth0]:1234/",
333 0, 0, CURLUE_OK},
334 {"https://[::%25fakeit]/moo",
335 "https://[::%25fakeit]/moo",
336 0, 0, CURLUE_OK},
337 {"smtp.example.com/path/html",
338 "smtp://smtp.example.com/path/html",
339 CURLU_GUESS_SCHEME, 0, CURLUE_OK},
340 {"https.example.com/path/html",
341 "http://https.example.com/path/html",
342 CURLU_GUESS_SCHEME, 0, CURLUE_OK},
343 {"dict.example.com/path/html",
344 "dict://dict.example.com/path/html",
345 CURLU_GUESS_SCHEME, 0, CURLUE_OK},
346 {"pop3.example.com/path/html",
347 "pop3://pop3.example.com/path/html",
348 CURLU_GUESS_SCHEME, 0, CURLUE_OK},
349 {"ldap.example.com/path/html",
350 "ldap://ldap.example.com/path/html",
351 CURLU_GUESS_SCHEME, 0, CURLUE_OK},
352 {"imap.example.com/path/html",
353 "imap://imap.example.com/path/html",
354 CURLU_GUESS_SCHEME, 0, CURLUE_OK},
355 {"ftp.example.com/path/html",
356 "ftp://ftp.example.com/path/html",
357 CURLU_GUESS_SCHEME, 0, CURLUE_OK},
358 {"example.com/path/html",
359 "http://example.com/path/html",
360 CURLU_GUESS_SCHEME, 0, CURLUE_OK},
361 {"HTTP://test/", "http://test/", 0, 0, CURLUE_OK},
362 {"http://HO0_-st..~./", "http://HO0_-st..~./", 0, 0, CURLUE_OK},
363 {"http:/@example.com: 123/", "", 0, 0, CURLUE_BAD_PORT_NUMBER},
364 {"http:/@example.com:123 /", "", 0, 0, CURLUE_BAD_PORT_NUMBER},
365 {"http:/@example.com:123a/", "", 0, 0, CURLUE_BAD_PORT_NUMBER},
366 {"http://host/file\r", "", 0, 0, CURLUE_MALFORMED_INPUT},
367 {"http://host/file\n\x03", "", 0, 0, CURLUE_MALFORMED_INPUT},
368 {"htt\x02://host/file", "",
369 CURLU_NON_SUPPORT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
370 {" http://host/file", "", 0, 0, CURLUE_MALFORMED_INPUT},
371 /* here the password ends at the semicolon and options is 'word' */
372 {"imap://user:pass;word@host/file",
373 "imap://user:pass;word@host/file",
374 0, 0, CURLUE_OK},
375 /* here the password has the semicolon */
376 {"http://user:pass;word@host/file",
377 "http://user:pass;word@host/file",
378 0, 0, CURLUE_OK},
379 {"file:///file.txt#moo",
380 "file:///file.txt#moo",
381 0, 0, CURLUE_OK},
382 {"file:////file.txt",
383 "file:////file.txt",
384 0, 0, CURLUE_OK},
385 {"file:///file.txt",
386 "file:///file.txt",
387 0, 0, CURLUE_OK},
388 {"file:./",
389 "file://",
390 0, 0, CURLUE_MALFORMED_INPUT},
391 {"http://example.com/hello/../here",
392 "http://example.com/hello/../here",
393 CURLU_PATH_AS_IS, 0, CURLUE_OK},
394 {"http://example.com/hello/../here",
395 "http://example.com/here",
396 0, 0, CURLUE_OK},
397 {"http://example.com:80",
398 "http://example.com/",
399 0, CURLU_NO_DEFAULT_PORT, CURLUE_OK},
400 {"tp://example.com/path/html",
401 "",
402 0, 0, CURLUE_UNSUPPORTED_SCHEME},
403 {"http://hello:[email protected]",
404 "",
405 CURLU_DISALLOW_USER, 0, CURLUE_USER_NOT_ALLOWED},
406 {"http:/@example.com:123",
407 "http://example.com:123/",
408 0, 0, CURLUE_OK},
409 {"http:/:[email protected]",
410 "http://:[email protected]/",
411 0, 0, CURLUE_OK},
412 {"http://[email protected]?#",
413 "http://[email protected]/",
414 0, 0, CURLUE_OK},
415 {"http://[email protected]?",
416 "http://[email protected]/",
417 0, 0, CURLUE_OK},
418 {"http://[email protected]#anchor",
419 "http://[email protected]/#anchor",
420 0, 0, CURLUE_OK},
421 {"example.com/path/html",
422 "https://example.com/path/html",
423 CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
424 {"example.com/path/html",
425 "",
426 0, 0, CURLUE_MALFORMED_INPUT},
427 {"http://user:[email protected]:1234/path/html?query=name#anchor",
428 "http://user:[email protected]:1234/path/html?query=name#anchor",
429 0, 0, CURLUE_OK},
430 {"http://example.com:1234/path/html?query=name#anchor",
431 "http://example.com:1234/path/html?query=name#anchor",
432 0, 0, CURLUE_OK},
433 {"http://example.com/path/html?query=name#anchor",
434 "http://example.com/path/html?query=name#anchor",
435 0, 0, CURLUE_OK},
436 {"http://example.com/path/html?query=name",
437 "http://example.com/path/html?query=name",
438 0, 0, CURLUE_OK},
439 {"http://example.com/path/html",
440 "http://example.com/path/html",
441 0, 0, CURLUE_OK},
442 {"tp://example.com/path/html",
443 "tp://example.com/path/html",
444 CURLU_NON_SUPPORT_SCHEME, 0, CURLUE_OK},
445 {"custom-scheme://host?expected=test-good",
446 "custom-scheme://host/?expected=test-good",
447 CURLU_NON_SUPPORT_SCHEME, 0, CURLUE_OK},
448 {"custom-scheme://?expected=test-bad",
449 "",
450 CURLU_NON_SUPPORT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
451 {"custom-scheme://?expected=test-new-good",
452 "custom-scheme:///?expected=test-new-good",
453 CURLU_NON_SUPPORT_SCHEME | CURLU_NO_AUTHORITY, 0, CURLUE_OK},
454 {"custom-scheme://host?expected=test-still-good",
455 "custom-scheme://host/?expected=test-still-good",
456 CURLU_NON_SUPPORT_SCHEME | CURLU_NO_AUTHORITY, 0, CURLUE_OK},
457 {NULL, NULL, 0, 0, 0}
458 };
459
460 static int checkurl(const char *url, const char *out)
461 {
462 if(strcmp(out, url)) {
463 fprintf(stderr, "Wanted: %s\nGot : %s\n",
464 out, url);
465 return 1;
466 }
467 return 0;
468 }
469
470 /* !checksrc! disable SPACEBEFORECOMMA 1 */
471 static struct setcase set_parts_list[] = {
472 {"https://example.com/",
473 /* Set a 41 bytes scheme. That's too long so the old scheme remains set. */
474 "scheme=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbc,",
475 "https://example.com/",
476 0, CURLU_NON_SUPPORT_SCHEME, CURLUE_OK, CURLUE_MALFORMED_INPUT},
477 {"https://example.com/",
478 /* set a 40 bytes scheme */
479 "scheme=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,",
480 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb://example.com/",
481 0, CURLU_NON_SUPPORT_SCHEME, CURLUE_OK, CURLUE_OK},
482 {"https://[::1%25fake]:1234/",
483 "zoneid=NULL,",
484 "https://[::1]:1234/",
485 0, 0, CURLUE_OK, CURLUE_OK},
486 {"https://host:1234/",
487 "port=NULL,",
488 "https://host/",
489 0, 0, CURLUE_OK, CURLUE_OK},
490 {"https://host:1234/",
491 "port=\"\",",
492 "https://host:1234/",
493 0, 0, CURLUE_OK, CURLUE_BAD_PORT_NUMBER},
494 {"https://host:1234/",
495 "port=56 78,",
496 "https://host:1234/",
497 0, 0, CURLUE_OK, CURLUE_MALFORMED_INPUT},
498 {"https://host:1234/",
499 "port=0,",
500 "https://host:1234/",
501 0, 0, CURLUE_OK, CURLUE_BAD_PORT_NUMBER},
502 {"https://host:1234/",
503 "port=65535,",
504 "https://host:65535/",
505 0, 0, CURLUE_OK, CURLUE_OK},
506 {"https://host:1234/",
507 "port=65536,",
508 "https://host:1234/",
509 0, 0, CURLUE_OK, CURLUE_BAD_PORT_NUMBER},
510 {"https://host/",
511 "path=%4A%4B%4C,",
512 "https://host/%4a%4b%4c",
513 0, 0, CURLUE_OK, CURLUE_OK},
514 {"https://host/mooo?q#f",
515 "path=NULL,query=NULL,fragment=NULL,",
516 "https://host/",
517 0, 0, CURLUE_OK, CURLUE_OK},
518 {"https://user:secret@host/",
519 "user=NULL,password=NULL,",
520 "https://host/",
521 0, 0, CURLUE_OK, CURLUE_OK},
522 {NULL,
523 "scheme=https,user= @:,host=foobar,",
524 "https://%20%20%20%40%3a@foobar/",
525 0, CURLU_URLENCODE, CURLUE_OK, CURLUE_OK},
526 {NULL,
527 "scheme=https,host= ,path= ,user= ,password= ,query= ,fragment= ,",
528 "https://%20:%20@%20%20/%20?+#%20",
529 0, CURLU_URLENCODE, CURLUE_OK, CURLUE_OK},
530 {NULL,
531 "scheme=https,host=foobar,path=/this /path /is /here,",
532 "https://foobar/this%20/path%20/is%20/here",
533 0, CURLU_URLENCODE, CURLUE_OK, CURLUE_OK},
534 {NULL,
535 "scheme=https,host=foobar,path=\xc3\xa4\xc3\xb6\xc3\xbc,",
536 "https://foobar/%c3%a4%c3%b6%c3%bc",
537 0, CURLU_URLENCODE, CURLUE_OK, CURLUE_OK},
538 {"imap://user:secret;opt@host/",
539 "options=updated,scheme=imaps,password=p4ssw0rd,",
540 "imaps://user:p4ssw0rd;updated@host/",
541 0, 0, CURLUE_NO_HOST, CURLUE_OK},
542 {"imap://user:secret;optit@host/",
543 "scheme=https,",
544 "https://user:secret@host/",
545 0, 0, CURLUE_NO_HOST, CURLUE_OK},
546 {"file:///file#anchor",
547 "scheme=https,host=example,",
548 "https://example/file#anchor",
549 0, 0, CURLUE_NO_HOST, CURLUE_OK},
550 {NULL, /* start fresh! */
551 "scheme=file,host=127.0.0.1,path=/no,user=anonymous,",
552 "file:///no",
553 0, 0, CURLUE_OK, CURLUE_OK},
554 {NULL, /* start fresh! */
555 "scheme=ftp,host=127.0.0.1,path=/no,user=anonymous,",
556 "ftp://[email protected]/no",
557 0, 0, CURLUE_OK, CURLUE_OK},
558 {NULL, /* start fresh! */
559 "scheme=https,host=example.com,",
560 "https://example.com/",
561 0, CURLU_NON_SUPPORT_SCHEME, CURLUE_OK, CURLUE_OK},
562 {"http://user:[email protected]/path?query#frag",
563 "fragment=changed,",
564 "http://user:[email protected]/path?query#changed",
565 0, CURLU_NON_SUPPORT_SCHEME, CURLUE_OK, CURLUE_OK},
566 {"http://example.com/",
567 "scheme=foo,", /* not accepted */
568 "http://example.com/",
569 0, 0, CURLUE_OK, CURLUE_UNSUPPORTED_SCHEME},
570 {"http://example.com/",
571 "scheme=https,path=/hello,fragment=snippet,",
572 "https://example.com/hello#snippet",
573 0, 0, CURLUE_OK, CURLUE_OK},
574 {"http://example.com:80",
575 "user=foo,port=1922,",
576 "http://[email protected]:1922/",
577 0, 0, CURLUE_OK, CURLUE_OK},
578 {"http://example.com:80",
579 "user=foo,password=bar,",
580 "http://foo:[email protected]:80/",
581 0, 0, CURLUE_OK, CURLUE_OK},
582 {"http://example.com:80",
583 "user=foo,",
584 "http://[email protected]:80/",
585 0, 0, CURLUE_OK, CURLUE_OK},
586 {"http://example.com",
587 "host=www.example.com,",
588 "http://www.example.com/",
589 0, 0, CURLUE_OK, CURLUE_OK},
590 {"http://example.com:80",
591 "scheme=ftp,",
592 "ftp://example.com:80/",
593 0, 0, CURLUE_OK, CURLUE_OK},
594 {"custom-scheme://host",
595 "host=\"\",",
596 "custom-scheme://host/",
597 CURLU_NON_SUPPORT_SCHEME, CURLU_NON_SUPPORT_SCHEME, CURLUE_OK,
598 CURLUE_MALFORMED_INPUT},
599 {"custom-scheme://host",
600 "host=\"\",",
601 "custom-scheme:///",
602 CURLU_NON_SUPPORT_SCHEME, CURLU_NON_SUPPORT_SCHEME | CURLU_NO_AUTHORITY,
603 CURLUE_OK, CURLUE_OK},
604
605 {NULL, NULL, NULL, 0, 0, 0, 0}
606 };
607
608 static CURLUPart part2id(char *part)
609 {
610 if(!strcmp("url", part))
611 return CURLUPART_URL;
612 if(!strcmp("scheme", part))
613 return CURLUPART_SCHEME;
614 if(!strcmp("user", part))
615 return CURLUPART_USER;
616 if(!strcmp("password", part))
617 return CURLUPART_PASSWORD;
618 if(!strcmp("options", part))
619 return CURLUPART_OPTIONS;
620 if(!strcmp("host", part))
621 return CURLUPART_HOST;
622 if(!strcmp("port", part))
623 return CURLUPART_PORT;
624 if(!strcmp("path", part))
625 return CURLUPART_PATH;
626 if(!strcmp("query", part))
627 return CURLUPART_QUERY;
628 if(!strcmp("fragment", part))
629 return CURLUPART_FRAGMENT;
630 if(!strcmp("zoneid", part))
631 return CURLUPART_ZONEID;
632 return 9999; /* bad input => bad output */
633 }
634
635 static CURLUcode updateurl(CURLU *u, const char *cmd, unsigned int setflags)
636 {
637 const char *p = cmd;
638 CURLUcode uc;
639
640 /* make sure the last command ends with a comma too! */
641 while(p) {
642 char *e = strchr(p, ',');
643 if(e) {
644 size_t n = e-p;
645 char buf[80];
646 char part[80];
647 char value[80];
648 memcpy(buf, p, n);
649 buf[n] = 0;
650 if(2 == sscanf(buf, "%79[^=]=%79[^,]", part, value)) {
651 CURLUPart what = part2id(part);
652 #if 0
653 /* for debugging this */
654 fprintf(stderr, "%s = %s [%d]\n", part, value, (int)what);
655 #endif
656 if(what > CURLUPART_ZONEID)
657 fprintf(stderr, "UNKNOWN part '%s'\n", part);
658
659 if(!strcmp("NULL", value))
660 uc = curl_url_set(u, what, NULL, setflags);
661 else if(!strcmp("\"\"", value))
662 uc = curl_url_set(u, what, "", setflags);
663 else
664 uc = curl_url_set(u, what, value, setflags);
665 if(uc)
666 return uc;
667 }
668 p = e + 1;
669 continue;
670 }
671 break;
672 }
673 return CURLUE_OK;
674 }
675
676 static struct redircase set_url_list[] = {
677 {"file://localhost/path?query#frag",
678 "foo#another",
679 "file:///foo#another",
680 0, 0, 0},
681 {"http://example.com/path?query#frag",
682 "https://two.example.com/bradnew",
683 "https://two.example.com/bradnew",
684 0, 0, 0},
685 {"http://example.com/path?query#frag",
686 "../../newpage#foo",
687 "http://example.com/newpage#foo",
688 0, 0, 0},
689 {"http://user:[email protected]/path?query#frag",
690 "../../newpage",
691 "http://user:[email protected]/newpage",
692 0, 0, 0},
693 {"http://user:[email protected]/path?query#frag",
694 "../newpage",
695 "http://user:[email protected]/newpage",
696 0, 0, 0},
697 {NULL, NULL, NULL, 0, 0, 0}
698 };
699
700 static int set_url(void)
701 {
702 int i;
703 int error = 0;
704
705 for(i = 0; set_url_list[i].in && !error; i++) {
706 CURLUcode rc;
707 CURLU *urlp = curl_url();
708 if(!urlp)
709 break;
710 rc = curl_url_set(urlp, CURLUPART_URL, set_url_list[i].in,
711 set_url_list[i].urlflags);
712 if(!rc) {
713 rc = curl_url_set(urlp, CURLUPART_URL, set_url_list[i].set,
714 set_url_list[i].setflags);
715 if(rc) {
716 fprintf(stderr, "%s:%d Set URL %s returned %d\n",
717 __FILE__, __LINE__, set_url_list[i].set,
718 (int)rc);
719 error++;
720 }
721 else {
722 char *url = NULL;
723 rc = curl_url_get(urlp, CURLUPART_URL, &url, 0);
724 if(rc) {
725 fprintf(stderr, "%s:%d Get URL returned %d\n",
726 __FILE__, __LINE__, (int)rc);
727 error++;
728 }
729 else {
730 if(checkurl(url, set_url_list[i].out)) {
731 error++;
732 }
733 }
734 curl_free(url);
735 }
736 }
737 else if(rc != set_url_list[i].ucode) {
738 fprintf(stderr, "Set URL\nin: %s\nreturned %d (expected %d)\n",
739 set_url_list[i].in, (int)rc, set_url_list[i].ucode);
740 error++;
741 }
742 curl_url_cleanup(urlp);
743 }
744 return error;
745 }
746
747 static int set_parts(void)
748 {
749 int i;
750 int error = 0;
751
752 for(i = 0; set_parts_list[i].set && !error; i++) {
753 CURLUcode rc;
754 CURLU *urlp = curl_url();
755 if(!urlp) {
756 error++;
757 break;
758 }
759 if(set_parts_list[i].in)
760 rc = curl_url_set(urlp, CURLUPART_URL, set_parts_list[i].in,
761 set_parts_list[i].urlflags);
762 else
763 rc = CURLUE_OK;
764 if(!rc) {
765 char *url = NULL;
766 CURLUcode uc = updateurl(urlp, set_parts_list[i].set,
767 set_parts_list[i].setflags);
768
769 if(uc != set_parts_list[i].pcode) {
770 fprintf(stderr, "updateurl\nin: %s\nreturned %d (expected %d)\n",
771 set_parts_list[i].set, (int)uc, set_parts_list[i].pcode);
772 error++;
773 }
774
775 rc = curl_url_get(urlp, CURLUPART_URL, &url, 0);
776
777 if(rc) {
778 fprintf(stderr, "%s:%d Get URL returned %d\n",
779 __FILE__, __LINE__, (int)rc);
780 error++;
781 }
782 else if(checkurl(url, set_parts_list[i].out)) {
783 error++;
784 }
785 curl_free(url);
786 }
787 else if(rc != set_parts_list[i].ucode) {
788 fprintf(stderr, "Set parts\nin: %s\nreturned %d (expected %d)\n",
789 set_parts_list[i].in, (int)rc, set_parts_list[i].ucode);
790 error++;
791 }
792 curl_url_cleanup(urlp);
793 }
794 return error;
795 }
796
797 static int get_url(void)
798 {
799 int i;
800 int error = 0;
801 for(i = 0; get_url_list[i].in && !error; i++) {
802 CURLUcode rc;
803 CURLU *urlp = curl_url();
804 if(!urlp) {
805 error++;
806 break;
807 }
808 rc = curl_url_set(urlp, CURLUPART_URL, get_url_list[i].in,
809 get_url_list[i].urlflags);
810 if(!rc) {
811 char *url = NULL;
812 rc = curl_url_get(urlp, CURLUPART_URL, &url, get_url_list[i].getflags);
813
814 if(rc) {
815 fprintf(stderr, "%s:%d returned %d\n",
816 __FILE__, __LINE__, (int)rc);
817 error++;
818 }
819 else {
820 if(checkurl(url, get_url_list[i].out)) {
821 error++;
822 }
823 }
824 curl_free(url);
825 }
826 else if(rc != get_url_list[i].ucode) {
827 fprintf(stderr, "Get URL\nin: %s\nreturned %d (expected %d)\n",
828 get_url_list[i].in, (int)rc, get_url_list[i].ucode);
829 error++;
830 }
831 curl_url_cleanup(urlp);
832 }
833 return error;
834 }
835
836 static int get_parts(void)
837 {
838 int i;
839 int error = 0;
840 for(i = 0; get_parts_list[i].in && !error; i++) {
841 CURLUcode rc;
842 CURLU *urlp = curl_url();
843 if(!urlp) {
844 error++;
845 break;
846 }
847 rc = curl_url_set(urlp, CURLUPART_URL,
848 get_parts_list[i].in,
849 get_parts_list[i].urlflags);
850 if(rc != get_parts_list[i].ucode) {
851 fprintf(stderr, "Get parts\nin: %s\nreturned %d (expected %d)\n",
852 get_parts_list[i].in, (int)rc, get_parts_list[i].ucode);
853 error++;
854 }
855 else if(get_parts_list[i].ucode) {
856 /* the expected error happened */
857 }
858 else if(checkparts(urlp, get_parts_list[i].in, get_parts_list[i].out,
859 get_parts_list[i].getflags))
860 error++;
861 curl_url_cleanup(urlp);
862 }
863 return error;
864 }
865
866 static struct querycase append_list[] = {
867 {"HTTP://test/?s", "name=joe\x02", "http://test/?s&name=joe%02",
868 0, CURLU_URLENCODE, CURLUE_OK},
869 {"HTTP://test/?size=2#f", "name=joe=", "http://test/?size=2&name=joe%3d#f",
870 0, CURLU_URLENCODE, CURLUE_OK},
871 {"HTTP://test/?size=2#f", "name=joe doe",
872 "http://test/?size=2&name=joe+doe#f",
873 0, CURLU_URLENCODE, CURLUE_OK},
874 {"HTTP://test/", "name=joe", "http://test/?name=joe", 0, 0, CURLUE_OK},
875 {"HTTP://test/?size=2", "name=joe", "http://test/?size=2&name=joe",
876 0, 0, CURLUE_OK},
877 {"HTTP://test/?size=2&", "name=joe", "http://test/?size=2&name=joe",
878 0, 0, CURLUE_OK},
879 {"HTTP://test/?size=2#f", "name=joe", "http://test/?size=2&name=joe#f",
880 0, 0, CURLUE_OK},
881 {NULL, NULL, NULL, 0, 0, 0}
882 };
883
884 static int append(void)
885 {
886 int i;
887 int error = 0;
888 for(i = 0; append_list[i].in && !error; i++) {
889 CURLUcode rc;
890 CURLU *urlp = curl_url();
891 if(!urlp) {
892 error++;
893 break;
894 }
895 rc = curl_url_set(urlp, CURLUPART_URL,
896 append_list[i].in,
897 append_list[i].urlflags);
898 if(rc)
899 error++;
900 else
901 rc = curl_url_set(urlp, CURLUPART_QUERY,
902 append_list[i].q,
903 append_list[i].qflags | CURLU_APPENDQUERY);
904 if(error)
905 ;
906 else if(rc != append_list[i].ucode) {
907 fprintf(stderr, "Append\nin: %s\nreturned %d (expected %d)\n",
908 append_list[i].in, (int)rc, append_list[i].ucode);
909 error++;
910 }
911 else if(append_list[i].ucode) {
912 /* the expected error happened */
913 }
914 else {
915 char *url;
916 rc = curl_url_get(urlp, CURLUPART_URL, &url, 0);
917 if(rc) {
918 fprintf(stderr, "%s:%d Get URL returned %d\n",
919 __FILE__, __LINE__, (int)rc);
920 error++;
921 }
922 else {
923 if(checkurl(url, append_list[i].out)) {
924 error++;
925 }
926 curl_free(url);
927 }
928 }
929 curl_url_cleanup(urlp);
930 }
931 return error;
932 }
933
934 static int scopeid(void)
935 {
936 CURLU *u = curl_url();
937 int error = 0;
938 CURLUcode rc;
939 char *url;
940
941 rc = curl_url_set(u, CURLUPART_URL,
942 "https://[fe80::20c:29ff:fe9c:409b%25eth0]/hello.html", 0);
943 if(rc != CURLUE_OK) {
944 fprintf(stderr, "%s:%d curl_url_set returned %d\n",
945 __FILE__, __LINE__, (int)rc);
946 error++;
947 }
948
949 rc = curl_url_get(u, CURLUPART_HOST, &url, 0);
950 if(rc != CURLUE_OK) {
951 fprintf(stderr, "%s:%d curl_url_get CURLUPART_HOST returned %d\n",
952 __FILE__, __LINE__, (int)rc);
953 error++;
954 }
955 else {
956 printf("we got %s\n", url);
957 curl_free(url);
958 }
959
960 rc = curl_url_set(u, CURLUPART_HOST, "[::1]", 0);
961 if(rc != CURLUE_OK) {
962 fprintf(stderr, "%s:%d curl_url_set CURLUPART_HOST returned %d\n",
963 __FILE__, __LINE__, (int)rc);
964 error++;
965 }
966
967 rc = curl_url_get(u, CURLUPART_URL, &url, 0);
968 if(rc != CURLUE_OK) {
969 fprintf(stderr, "%s:%d curl_url_get CURLUPART_URL returned %d\n",
970 __FILE__, __LINE__, (int)rc);
971 error++;
972 }
973 else {
974 printf("we got %s\n", url);
975 curl_free(url);
976 }
977
978 rc = curl_url_set(u, CURLUPART_HOST, "example.com", 0);
979 if(rc != CURLUE_OK) {
980 fprintf(stderr, "%s:%d curl_url_set CURLUPART_HOST returned %d\n",
981 __FILE__, __LINE__, (int)rc);
982 error++;
983 }
984
985 rc = curl_url_get(u, CURLUPART_URL, &url, 0);
986 if(rc != CURLUE_OK) {
987 fprintf(stderr, "%s:%d curl_url_get CURLUPART_URL returned %d\n",
988 __FILE__, __LINE__, (int)rc);
989 error++;
990 }
991 else {
992 printf("we got %s\n", url);
993 curl_free(url);
994 }
995
996 rc = curl_url_set(u, CURLUPART_HOST,
997 "[fe80::20c:29ff:fe9c:409b%25eth0]", 0);
998 if(rc != CURLUE_OK) {
999 fprintf(stderr, "%s:%d curl_url_set CURLUPART_HOST returned %d\n",
1000 __FILE__, __LINE__, (int)rc);
1001 error++;
1002 }
1003
1004 rc = curl_url_get(u, CURLUPART_URL, &url, 0);
1005 if(rc != CURLUE_OK) {
1006 fprintf(stderr, "%s:%d curl_url_get CURLUPART_URL returned %d\n",
1007 __FILE__, __LINE__, (int)rc);
1008 error++;
1009 }
1010 else {
1011 printf("we got %s\n", url);
1012 curl_free(url);
1013 }
1014
1015 rc = curl_url_get(u, CURLUPART_HOST, &url, 0);
1016 if(rc != CURLUE_OK) {
1017 fprintf(stderr, "%s:%d curl_url_get CURLUPART_HOST returned %d\n",
1018 __FILE__, __LINE__, (int)rc);
1019 error++;
1020 }
1021 else {
1022 printf("we got %s\n", url);
1023 curl_free(url);
1024 }
1025
1026 rc = curl_url_get(u, CURLUPART_ZONEID, &url, 0);
1027 if(rc != CURLUE_OK) {
1028 fprintf(stderr, "%s:%d curl_url_get CURLUPART_ZONEID returned %d\n",
1029 __FILE__, __LINE__, (int)rc);
1030 error++;
1031 }
1032 else {
1033 printf("we got %s\n", url);
1034 curl_free(url);
1035 }
1036
1037 rc = curl_url_set(u, CURLUPART_ZONEID, "clown", 0);
1038 if(rc != CURLUE_OK) {
1039 fprintf(stderr, "%s:%d curl_url_set CURLUPART_ZONEID returned %d\n",
1040 __FILE__, __LINE__, (int)rc);
1041 error++;
1042 }
1043
1044 rc = curl_url_get(u, CURLUPART_URL, &url, 0);
1045 if(rc != CURLUE_OK) {
1046 fprintf(stderr, "%s:%d curl_url_get CURLUPART_URL returned %d\n",
1047 __FILE__, __LINE__, (int)rc);
1048 error++;
1049 }
1050 else {
1051 printf("we got %s\n", url);
1052 curl_free(url);
1053 }
1054
1055 curl_url_cleanup(u);
1056
1057 return error;
1058 }
1059
1060 int test(char *URL)
1061 {
1062 (void)URL; /* not used */
1063
1064 if(scopeid())
1065 return 6;
1066
1067 if(append())
1068 return 5;
1069
1070 if(set_url())
1071 return 1;
1072
1073 if(set_parts())
1074 return 2;
1075
1076 if(get_url())
1077 return 3;
1078
1079 if(get_parts())
1080 return 4;
1081
1082 printf("success\n");
1083 return 0;
1084 }