]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
f4047845 | 2 | * HTTP test program for CUPS. |
ef416fc2 | 3 | * |
4c37eb9f MS |
4 | * Copyright © 2007-2018 by Apple Inc. |
5 | * Copyright © 1997-2006 by Easy Software Products. | |
ef416fc2 | 6 | * |
f4047845 MS |
7 | * These coded instructions, statements, and computer programs are the |
8 | * property of Apple Inc. and are protected by Federal copyright | |
9 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
10 | * which should have been included with this file. If this file is | |
57b7b66b | 11 | * missing or damaged, see the license at "http://www.cups.org/". |
ef416fc2 | 12 | * |
f4047845 | 13 | * This file is subject to the Apple OS-Developed Software exception. |
ef416fc2 | 14 | */ |
15 | ||
16 | /* | |
17 | * Include necessary headers... | |
18 | */ | |
19 | ||
0c4bedc4 | 20 | #include "cups-private.h" |
ef416fc2 | 21 | |
22 | ||
23 | /* | |
24 | * Types and structures... | |
25 | */ | |
26 | ||
27 | typedef struct uri_test_s /**** URI test cases ****/ | |
28 | { | |
29 | http_uri_status_t result; /* Expected return value */ | |
30 | const char *uri, /* URI */ | |
31 | *scheme, /* Scheme string */ | |
32 | *username, /* Username:password string */ | |
33 | *hostname, /* Hostname string */ | |
34 | *resource; /* Resource string */ | |
35 | int port, /* Port number */ | |
7e86f2f6 MS |
36 | assemble_port; /* Port number for httpAssembleURI() */ |
37 | http_uri_coding_t assemble_coding;/* Coding for httpAssembleURI() */ | |
ef416fc2 | 38 | } uri_test_t; |
39 | ||
40 | ||
41 | /* | |
42 | * Local globals... | |
43 | */ | |
44 | ||
45 | static uri_test_t uri_tests[] = /* URI test data */ | |
46 | { | |
47 | /* Start with valid URIs */ | |
cb7f98ee | 48 | { HTTP_URI_STATUS_OK, "file:/filename", |
86c809d9 MS |
49 | "file", "", "", "/filename", 0, 0, |
50 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 51 | { HTTP_URI_STATUS_OK, "file:/filename%20with%20spaces", |
86c809d9 MS |
52 | "file", "", "", "/filename with spaces", 0, 0, |
53 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 54 | { HTTP_URI_STATUS_OK, "file:///filename", |
86c809d9 MS |
55 | "file", "", "", "/filename", 0, 0, |
56 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 57 | { HTTP_URI_STATUS_OK, "file:///filename%20with%20spaces", |
86c809d9 MS |
58 | "file", "", "", "/filename with spaces", 0, 0, |
59 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 60 | { HTTP_URI_STATUS_OK, "file://localhost/filename", |
86c809d9 MS |
61 | "file", "", "localhost", "/filename", 0, 0, |
62 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 63 | { HTTP_URI_STATUS_OK, "file://localhost/filename%20with%20spaces", |
86c809d9 MS |
64 | "file", "", "localhost", "/filename with spaces", 0, 0, |
65 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 66 | { HTTP_URI_STATUS_OK, "http://server/", |
86c809d9 MS |
67 | "http", "", "server", "/", 80, 0, |
68 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 69 | { HTTP_URI_STATUS_OK, "http://username@server/", |
86c809d9 MS |
70 | "http", "username", "server", "/", 80, 0, |
71 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 72 | { HTTP_URI_STATUS_OK, "http://username:passwor%64@server/", |
86c809d9 MS |
73 | "http", "username:password", "server", "/", 80, 0, |
74 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 75 | { HTTP_URI_STATUS_OK, "http://username:passwor%64@server:8080/", |
86c809d9 MS |
76 | "http", "username:password", "server", "/", 8080, 8080, |
77 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 78 | { HTTP_URI_STATUS_OK, "http://username:passwor%64@server:8080/directory/filename", |
86c809d9 MS |
79 | "http", "username:password", "server", "/directory/filename", 8080, 8080, |
80 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 81 | { HTTP_URI_STATUS_OK, "http://[2000::10:100]:631/ipp", |
86c809d9 MS |
82 | "http", "", "2000::10:100", "/ipp", 631, 631, |
83 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 84 | { HTTP_URI_STATUS_OK, "https://username:passwor%64@server/directory/filename", |
86c809d9 MS |
85 | "https", "username:password", "server", "/directory/filename", 443, 0, |
86 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 87 | { HTTP_URI_STATUS_OK, "ipp://username:passwor%64@[::1]/ipp", |
86c809d9 MS |
88 | "ipp", "username:password", "::1", "/ipp", 631, 0, |
89 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 90 | { HTTP_URI_STATUS_OK, "lpd://server/queue?reserve=yes", |
86c809d9 MS |
91 | "lpd", "", "server", "/queue?reserve=yes", 515, 0, |
92 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 93 | { HTTP_URI_STATUS_OK, "mailto:user@domain.com", |
86c809d9 MS |
94 | "mailto", "", "", "user@domain.com", 0, 0, |
95 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 96 | { HTTP_URI_STATUS_OK, "socket://server/", |
86c809d9 MS |
97 | "socket", "", "server", "/", 9100, 0, |
98 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 99 | { HTTP_URI_STATUS_OK, "socket://192.168.1.1:9101/", |
86c809d9 MS |
100 | "socket", "", "192.168.1.1", "/", 9101, 9101, |
101 | HTTP_URI_CODING_MOST }, | |
f4047845 MS |
102 | { HTTP_URI_STATUS_OK, "tel:8005551212", |
103 | "tel", "", "", "8005551212", 0, 0, | |
104 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 105 | { HTTP_URI_STATUS_OK, "ipp://username:password@[v1.fe80::200:1234:5678:9abc+eth0]:999/ipp", |
86c809d9 MS |
106 | "ipp", "username:password", "fe80::200:1234:5678:9abc%eth0", "/ipp", 999, 999, |
107 | HTTP_URI_CODING_MOST }, | |
108 | { HTTP_URI_STATUS_OK, "ipp://username:password@[fe80::200:1234:5678:9abc%25eth0]:999/ipp", | |
109 | "ipp", "username:password", "fe80::200:1234:5678:9abc%eth0", "/ipp", 999, 999, | |
7e86f2f6 | 110 | (http_uri_coding_t)(HTTP_URI_CODING_MOST | HTTP_URI_CODING_RFC6874) }, |
cb7f98ee | 111 | { HTTP_URI_STATUS_OK, "http://server/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400", |
86c809d9 MS |
112 | "http", "", "server", "/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400", 80, 0, |
113 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 114 | { HTTP_URI_STATUS_OK, "lpd://Acme%20Laser%20(01%3A23%3A45).local._tcp._printer/", |
86c809d9 MS |
115 | "lpd", "", "Acme Laser (01:23:45).local._tcp._printer", "/", 515, 0, |
116 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 117 | { HTTP_URI_STATUS_OK, "ipp://HP%20Officejet%204500%20G510n-z%20%40%20Will's%20MacBook%20Pro%2015%22._ipp._tcp.local./", |
86c809d9 MS |
118 | "ipp", "", "HP Officejet 4500 G510n-z @ Will's MacBook Pro 15\"._ipp._tcp.local.", "/", 631, 0, |
119 | HTTP_URI_CODING_MOST }, | |
ff862e50 MS |
120 | { HTTP_URI_STATUS_OK, "ipp://%22%23%2F%3A%3C%3E%3F%40%5B%5C%5D%5E%60%7B%7C%7D/", |
121 | "ipp", "", "\"#/:<>?@[\\]^`{|}", "/", 631, 0, | |
122 | HTTP_URI_CODING_MOST }, | |
ef416fc2 | 123 | |
124 | /* Missing scheme */ | |
cb7f98ee | 125 | { HTTP_URI_STATUS_MISSING_SCHEME, "/path/to/file/index.html", |
86c809d9 MS |
126 | "file", "", "", "/path/to/file/index.html", 0, 0, |
127 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 128 | { HTTP_URI_STATUS_MISSING_SCHEME, "//server/ipp", |
86c809d9 MS |
129 | "ipp", "", "server", "/ipp", 631, 0, |
130 | HTTP_URI_CODING_MOST }, | |
ef416fc2 | 131 | |
132 | /* Unknown scheme */ | |
cb7f98ee | 133 | { HTTP_URI_STATUS_UNKNOWN_SCHEME, "vendor://server/resource", |
86c809d9 MS |
134 | "vendor", "", "server", "/resource", 0, 0, |
135 | HTTP_URI_CODING_MOST }, | |
ef416fc2 | 136 | |
137 | /* Missing resource */ | |
cb7f98ee | 138 | { HTTP_URI_STATUS_MISSING_RESOURCE, "socket://[::192.168.2.1]", |
86c809d9 MS |
139 | "socket", "", "::192.168.2.1", "/", 9100, 0, |
140 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 141 | { HTTP_URI_STATUS_MISSING_RESOURCE, "socket://192.168.1.1:9101", |
86c809d9 MS |
142 | "socket", "", "192.168.1.1", "/", 9101, 0, |
143 | HTTP_URI_CODING_MOST }, | |
ef416fc2 | 144 | |
145 | /* Bad URI */ | |
cb7f98ee | 146 | { HTTP_URI_STATUS_BAD_URI, "", |
86c809d9 MS |
147 | "", "", "", "", 0, 0, |
148 | HTTP_URI_CODING_MOST }, | |
ef416fc2 | 149 | |
150 | /* Bad scheme */ | |
cb7f98ee | 151 | { HTTP_URI_STATUS_BAD_SCHEME, "bad_scheme://server/resource", |
86c809d9 MS |
152 | "", "", "", "", 0, 0, |
153 | HTTP_URI_CODING_MOST }, | |
ef416fc2 | 154 | |
155 | /* Bad username */ | |
cb7f98ee | 156 | { HTTP_URI_STATUS_BAD_USERNAME, "http://username:passwor%6@server/resource", |
86c809d9 MS |
157 | "http", "", "", "", 80, 0, |
158 | HTTP_URI_CODING_MOST }, | |
ef416fc2 | 159 | |
160 | /* Bad hostname */ | |
cb7f98ee | 161 | { HTTP_URI_STATUS_BAD_HOSTNAME, "http://[/::1]/index.html", |
86c809d9 MS |
162 | "http", "", "", "", 80, 0, |
163 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 164 | { HTTP_URI_STATUS_BAD_HOSTNAME, "http://[", |
86c809d9 MS |
165 | "http", "", "", "", 80, 0, |
166 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 167 | { HTTP_URI_STATUS_BAD_HOSTNAME, "http://serve%7/index.html", |
86c809d9 MS |
168 | "http", "", "", "", 80, 0, |
169 | HTTP_URI_CODING_MOST }, | |
6961465f | 170 | { HTTP_URI_STATUS_BAD_HOSTNAME, "http://server with spaces/index.html", |
86c809d9 MS |
171 | "http", "", "", "", 80, 0, |
172 | HTTP_URI_CODING_MOST }, | |
ff862e50 MS |
173 | { HTTP_URI_STATUS_BAD_HOSTNAME, "ipp://\"#/:<>?@[\\]^`{|}/", |
174 | "ipp", "", "", "", 631, 0, | |
175 | HTTP_URI_CODING_MOST }, | |
ef416fc2 | 176 | |
177 | /* Bad port number */ | |
cb7f98ee | 178 | { HTTP_URI_STATUS_BAD_PORT, "http://127.0.0.1:9999a/index.html", |
86c809d9 MS |
179 | "http", "", "127.0.0.1", "", 0, 0, |
180 | HTTP_URI_CODING_MOST }, | |
ef416fc2 | 181 | |
182 | /* Bad resource */ | |
4c37eb9f MS |
183 | { HTTP_URI_STATUS_BAD_RESOURCE, "mailto:\r\nbla", |
184 | "mailto", "", "", "", 0, 0, | |
185 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 186 | { HTTP_URI_STATUS_BAD_RESOURCE, "http://server/index.html%", |
86c809d9 MS |
187 | "http", "", "server", "", 80, 0, |
188 | HTTP_URI_CODING_MOST }, | |
6961465f | 189 | { HTTP_URI_STATUS_BAD_RESOURCE, "http://server/index with spaces.html", |
86c809d9 MS |
190 | "http", "", "server", "", 80, 0, |
191 | HTTP_URI_CODING_MOST } | |
ef416fc2 | 192 | }; |
193 | static const char * const base64_tests[][2] = | |
194 | { | |
195 | { "A", "QQ==" }, | |
196 | /* 010000 01 */ | |
197 | { "AB", "QUI=" }, | |
198 | /* 010000 010100 0010 */ | |
199 | { "ABC", "QUJD" }, | |
200 | /* 010000 010100 001001 000011 */ | |
201 | { "ABCD", "QUJDRA==" }, | |
202 | /* 010000 010100 001001 000011 010001 00 */ | |
203 | { "ABCDE", "QUJDREU=" }, | |
204 | /* 010000 010100 001001 000011 010001 000100 0101 */ | |
205 | { "ABCDEF", "QUJDREVG" }, | |
206 | /* 010000 010100 001001 000011 010001 000100 010101 000110 */ | |
207 | }; | |
208 | ||
209 | ||
210 | /* | |
211 | * 'main()' - Main entry. | |
212 | */ | |
213 | ||
214 | int /* O - Exit status */ | |
215 | main(int argc, /* I - Number of command-line arguments */ | |
216 | char *argv[]) /* I - Command-line arguments */ | |
217 | { | |
218 | int i, j, k; /* Looping vars */ | |
219 | http_t *http; /* HTTP connection */ | |
83e08001 | 220 | http_encryption_t encryption; /* Encryption type */ |
ef416fc2 | 221 | http_status_t status; /* Status of GET command */ |
222 | int failures; /* Number of test failures */ | |
223 | char buffer[8192]; /* Input buffer */ | |
224 | long bytes; /* Number of bytes read */ | |
225 | FILE *out; /* Output file */ | |
226 | char encode[256], /* Base64-encoded string */ | |
227 | decode[256]; /* Base64-decoded string */ | |
228 | int decodelen; /* Length of decoded string */ | |
229 | char scheme[HTTP_MAX_URI], /* Scheme from URI */ | |
230 | hostname[HTTP_MAX_URI], /* Hostname from URI */ | |
231 | username[HTTP_MAX_URI], /* Username:password from URI */ | |
232 | resource[HTTP_MAX_URI]; /* Resource from URI */ | |
233 | int port; /* Port number from URI */ | |
234 | http_uri_status_t uri_status; /* Status of URI separation */ | |
235 | http_addrlist_t *addrlist, /* Address list */ | |
236 | *addr; /* Current address */ | |
237 | off_t length, total; /* Length and total bytes */ | |
238 | time_t start, current; /* Start and end time */ | |
a469f8a5 | 239 | const char *encoding; /* Negotiated Content-Encoding */ |
ef416fc2 | 240 | static const char * const uri_status_strings[] = |
241 | { | |
cb7f98ee MS |
242 | "HTTP_URI_STATUS_OVERFLOW", |
243 | "HTTP_URI_STATUS_BAD_ARGUMENTS", | |
244 | "HTTP_URI_STATUS_BAD_RESOURCE", | |
245 | "HTTP_URI_STATUS_BAD_PORT", | |
246 | "HTTP_URI_STATUS_BAD_HOSTNAME", | |
247 | "HTTP_URI_STATUS_BAD_USERNAME", | |
248 | "HTTP_URI_STATUS_BAD_SCHEME", | |
249 | "HTTP_URI_STATUS_BAD_URI", | |
250 | "HTTP_URI_STATUS_OK", | |
251 | "HTTP_URI_STATUS_MISSING_SCHEME", | |
252 | "HTTP_URI_STATUS_UNKNOWN_SCHEME", | |
253 | "HTTP_URI_STATUS_MISSING_RESOURCE" | |
ef416fc2 | 254 | }; |
255 | ||
256 | ||
257 | /* | |
258 | * Do API tests if we don't have a URL on the command-line... | |
259 | */ | |
260 | ||
261 | if (argc == 1) | |
262 | { | |
263 | failures = 0; | |
264 | ||
265 | /* | |
266 | * httpGetDateString()/httpGetDateTime() | |
267 | */ | |
268 | ||
269 | fputs("httpGetDateString()/httpGetDateTime(): ", stdout); | |
270 | ||
271 | start = time(NULL); | |
5a9febac | 272 | strlcpy(buffer, httpGetDateString(start), sizeof(buffer)); |
ef416fc2 | 273 | current = httpGetDateTime(buffer); |
274 | ||
275 | i = (int)(current - start); | |
276 | if (i < 0) | |
277 | i = -i; | |
278 | ||
279 | if (!i) | |
280 | puts("PASS"); | |
281 | else | |
282 | { | |
283 | failures ++; | |
284 | puts("FAIL"); | |
285 | printf(" Difference is %d seconds, %02d:%02d:%02d...\n", i, i / 3600, | |
286 | (i / 60) % 60, i % 60); | |
287 | printf(" httpGetDateString(%d) returned \"%s\"\n", (int)start, buffer); | |
288 | printf(" httpGetDateTime(\"%s\") returned %d\n", buffer, (int)current); | |
289 | printf(" httpGetDateString(%d) returned \"%s\"\n", (int)current, | |
290 | httpGetDateString(current)); | |
291 | } | |
292 | ||
293 | /* | |
294 | * httpDecode64_2()/httpEncode64_2() | |
295 | */ | |
296 | ||
297 | fputs("httpDecode64_2()/httpEncode64_2(): ", stdout); | |
298 | ||
299 | for (i = 0, j = 0; i < (int)(sizeof(base64_tests) / sizeof(base64_tests[0])); i ++) | |
300 | { | |
301 | httpEncode64_2(encode, sizeof(encode), base64_tests[i][0], | |
7a0cbd5e | 302 | (int)strlen(base64_tests[i][0])); |
ef416fc2 | 303 | decodelen = (int)sizeof(decode); |
304 | httpDecode64_2(decode, &decodelen, base64_tests[i][1]); | |
305 | ||
306 | if (strcmp(decode, base64_tests[i][0])) | |
307 | { | |
308 | failures ++; | |
309 | ||
310 | if (j) | |
311 | { | |
312 | puts("FAIL"); | |
313 | j = 1; | |
314 | } | |
315 | ||
316 | printf(" httpDecode64_2() returned \"%s\", expected \"%s\"...\n", | |
317 | decode, base64_tests[i][0]); | |
318 | } | |
319 | ||
320 | if (strcmp(encode, base64_tests[i][1])) | |
321 | { | |
322 | failures ++; | |
323 | ||
324 | if (j) | |
325 | { | |
326 | puts("FAIL"); | |
327 | j = 1; | |
328 | } | |
329 | ||
330 | printf(" httpEncode64_2() returned \"%s\", expected \"%s\"...\n", | |
331 | encode, base64_tests[i][1]); | |
332 | } | |
333 | } | |
334 | ||
335 | if (!j) | |
336 | puts("PASS"); | |
337 | ||
338 | /* | |
339 | * httpGetHostname() | |
340 | */ | |
341 | ||
342 | fputs("httpGetHostname(): ", stdout); | |
343 | ||
757d2cad | 344 | if (httpGetHostname(NULL, hostname, sizeof(hostname))) |
ef416fc2 | 345 | printf("PASS (%s)\n", hostname); |
346 | else | |
347 | { | |
348 | failures ++; | |
349 | puts("FAIL"); | |
350 | } | |
351 | ||
352 | /* | |
353 | * httpAddrGetList() | |
354 | */ | |
355 | ||
75bd9771 | 356 | printf("httpAddrGetList(%s): ", hostname); |
ef416fc2 | 357 | |
358 | addrlist = httpAddrGetList(hostname, AF_UNSPEC, NULL); | |
359 | if (addrlist) | |
360 | { | |
ed486911 | 361 | for (i = 0, addr = addrlist; addr; i ++, addr = addr->next) |
362 | { | |
363 | char numeric[1024]; /* Numeric IP address */ | |
364 | ||
365 | ||
366 | httpAddrString(&(addr->addr), numeric, sizeof(numeric)); | |
367 | if (!strcmp(numeric, "UNKNOWN")) | |
368 | break; | |
369 | } | |
370 | ||
371 | if (addr) | |
372 | printf("FAIL (bad address for %s)\n", hostname); | |
373 | else | |
374 | printf("PASS (%d address(es) for %s)\n", i, hostname); | |
ef416fc2 | 375 | |
ef416fc2 | 376 | httpAddrFreeList(addrlist); |
377 | } | |
58dc1933 MS |
378 | else if (isdigit(hostname[0] & 255)) |
379 | { | |
380 | puts("FAIL (ignored because hostname is numeric)"); | |
381 | } | |
ef416fc2 | 382 | else |
383 | { | |
384 | failures ++; | |
385 | puts("FAIL"); | |
386 | } | |
387 | ||
388 | /* | |
389 | * Test httpSeparateURI()... | |
390 | */ | |
391 | ||
392 | fputs("httpSeparateURI(): ", stdout); | |
393 | for (i = 0, j = 0; i < (int)(sizeof(uri_tests) / sizeof(uri_tests[0])); i ++) | |
394 | { | |
a4d04587 | 395 | uri_status = httpSeparateURI(HTTP_URI_CODING_MOST, |
396 | uri_tests[i].uri, scheme, sizeof(scheme), | |
ef416fc2 | 397 | username, sizeof(username), |
398 | hostname, sizeof(hostname), &port, | |
399 | resource, sizeof(resource)); | |
400 | if (uri_status != uri_tests[i].result || | |
401 | strcmp(scheme, uri_tests[i].scheme) || | |
402 | strcmp(username, uri_tests[i].username) || | |
403 | strcmp(hostname, uri_tests[i].hostname) || | |
404 | port != uri_tests[i].port || | |
405 | strcmp(resource, uri_tests[i].resource)) | |
406 | { | |
407 | failures ++; | |
408 | ||
409 | if (!j) | |
410 | { | |
411 | puts("FAIL"); | |
412 | j = 1; | |
413 | } | |
414 | ||
415 | printf(" \"%s\":\n", uri_tests[i].uri); | |
416 | ||
417 | if (uri_status != uri_tests[i].result) | |
418 | printf(" Returned %s instead of %s\n", | |
419 | uri_status_strings[uri_status + 8], | |
420 | uri_status_strings[uri_tests[i].result + 8]); | |
421 | ||
422 | if (strcmp(scheme, uri_tests[i].scheme)) | |
423 | printf(" Scheme \"%s\" instead of \"%s\"\n", | |
424 | scheme, uri_tests[i].scheme); | |
425 | ||
426 | if (strcmp(username, uri_tests[i].username)) | |
427 | printf(" Username \"%s\" instead of \"%s\"\n", | |
428 | username, uri_tests[i].username); | |
429 | ||
430 | if (strcmp(hostname, uri_tests[i].hostname)) | |
431 | printf(" Hostname \"%s\" instead of \"%s\"\n", | |
432 | hostname, uri_tests[i].hostname); | |
433 | ||
434 | if (port != uri_tests[i].port) | |
435 | printf(" Port %d instead of %d\n", | |
436 | port, uri_tests[i].port); | |
437 | ||
438 | if (strcmp(resource, uri_tests[i].resource)) | |
439 | printf(" Resource \"%s\" instead of \"%s\"\n", | |
440 | resource, uri_tests[i].resource); | |
441 | } | |
442 | } | |
443 | ||
444 | if (!j) | |
445 | printf("PASS (%d URIs tested)\n", | |
446 | (int)(sizeof(uri_tests) / sizeof(uri_tests[0]))); | |
447 | ||
448 | /* | |
449 | * Test httpAssembleURI()... | |
450 | */ | |
451 | ||
452 | fputs("httpAssembleURI(): ", stdout); | |
453 | for (i = 0, j = 0, k = 0; | |
454 | i < (int)(sizeof(uri_tests) / sizeof(uri_tests[0])); | |
455 | i ++) | |
cb7f98ee | 456 | if (uri_tests[i].result == HTTP_URI_STATUS_OK && |
ef416fc2 | 457 | !strstr(uri_tests[i].uri, "%64") && |
458 | strstr(uri_tests[i].uri, "//")) | |
459 | { | |
460 | k ++; | |
86c809d9 | 461 | uri_status = httpAssembleURI(uri_tests[i].assemble_coding, |
a4d04587 | 462 | buffer, sizeof(buffer), |
ef416fc2 | 463 | uri_tests[i].scheme, |
464 | uri_tests[i].username, | |
465 | uri_tests[i].hostname, | |
466 | uri_tests[i].assemble_port, | |
467 | uri_tests[i].resource); | |
468 | ||
cb7f98ee | 469 | if (uri_status != HTTP_URI_STATUS_OK) |
ef416fc2 | 470 | { |
471 | failures ++; | |
472 | ||
473 | if (!j) | |
474 | { | |
475 | puts("FAIL"); | |
476 | j = 1; | |
477 | } | |
478 | ||
479 | printf(" \"%s\": %s\n", uri_tests[i].uri, | |
480 | uri_status_strings[uri_status + 8]); | |
481 | } | |
482 | else if (strcmp(buffer, uri_tests[i].uri)) | |
483 | { | |
484 | failures ++; | |
485 | ||
486 | if (!j) | |
487 | { | |
488 | puts("FAIL"); | |
489 | j = 1; | |
490 | } | |
491 | ||
492 | printf(" \"%s\": assembled = \"%s\"\n", uri_tests[i].uri, | |
493 | buffer); | |
494 | } | |
495 | } | |
496 | ||
497 | if (!j) | |
498 | printf("PASS (%d URIs tested)\n", k); | |
499 | ||
71f294ce MS |
500 | /* |
501 | * httpAssembleUUID | |
502 | */ | |
503 | ||
504 | fputs("httpAssembleUUID: ", stdout); | |
505 | httpAssembleUUID("hostname.example.com", 631, "printer", 12345, buffer, | |
506 | sizeof(buffer)); | |
507 | if (strncmp(buffer, "urn:uuid:", 9)) | |
508 | { | |
509 | printf("FAIL (%s)\n", buffer); | |
510 | failures ++; | |
511 | } | |
512 | else | |
513 | printf("PASS (%s)\n", buffer); | |
514 | ||
ef416fc2 | 515 | /* |
516 | * Show a summary and return... | |
517 | */ | |
518 | ||
519 | if (failures) | |
520 | printf("\n%d TESTS FAILED!\n", failures); | |
521 | else | |
522 | puts("\nALL TESTS PASSED!"); | |
523 | ||
524 | return (failures); | |
525 | } | |
1f0275e3 MS |
526 | else if (strstr(argv[1], "._tcp")) |
527 | { | |
528 | /* | |
529 | * Test resolving an mDNS name. | |
530 | */ | |
531 | ||
532 | char resolved[1024]; /* Resolved URI */ | |
533 | ||
534 | ||
eac3a0a0 | 535 | printf("_httpResolveURI(%s, _HTTP_RESOLVE_DEFAULT): ", argv[1]); |
1f0275e3 MS |
536 | fflush(stdout); |
537 | ||
eac3a0a0 MS |
538 | if (!_httpResolveURI(argv[1], resolved, sizeof(resolved), |
539 | _HTTP_RESOLVE_DEFAULT, NULL, NULL)) | |
1f0275e3 MS |
540 | { |
541 | puts("FAIL"); | |
542 | return (1); | |
543 | } | |
eac3a0a0 MS |
544 | else |
545 | printf("PASS (%s)\n", resolved); | |
546 | ||
547 | printf("_httpResolveURI(%s, _HTTP_RESOLVE_FQDN): ", argv[1]); | |
548 | fflush(stdout); | |
549 | ||
550 | if (!_httpResolveURI(argv[1], resolved, sizeof(resolved), | |
551 | _HTTP_RESOLVE_FQDN, NULL, NULL)) | |
552 | { | |
553 | puts("FAIL"); | |
554 | return (1); | |
555 | } | |
556 | else if (strstr(resolved, ".local:")) | |
557 | { | |
558 | printf("FAIL (%s)\n", resolved); | |
559 | return (1); | |
560 | } | |
1f0275e3 MS |
561 | else |
562 | { | |
563 | printf("PASS (%s)\n", resolved); | |
564 | return (0); | |
565 | } | |
566 | } | |
dfd5680b MS |
567 | else if (!strcmp(argv[1], "-u") && argc == 3) |
568 | { | |
569 | /* | |
570 | * Test URI separation... | |
571 | */ | |
572 | ||
573 | uri_status = httpSeparateURI(HTTP_URI_CODING_ALL, argv[2], scheme, | |
574 | sizeof(scheme), username, sizeof(username), | |
575 | hostname, sizeof(hostname), &port, | |
576 | resource, sizeof(resource)); | |
577 | printf("uri_status = %s\n", uri_status_strings[uri_status + 8]); | |
578 | printf("scheme = \"%s\"\n", scheme); | |
579 | printf("username = \"%s\"\n", username); | |
580 | printf("hostname = \"%s\"\n", hostname); | |
581 | printf("port = %d\n", port); | |
582 | printf("resource = \"%s\"\n", resource); | |
583 | ||
584 | return (0); | |
585 | } | |
ef416fc2 | 586 | |
587 | /* | |
588 | * Test HTTP GET requests... | |
589 | */ | |
590 | ||
591 | http = NULL; | |
592 | out = stdout; | |
593 | ||
594 | for (i = 1; i < argc; i ++) | |
595 | { | |
596 | if (!strcmp(argv[i], "-o")) | |
597 | { | |
598 | i ++; | |
599 | if (i >= argc) | |
600 | break; | |
601 | ||
602 | out = fopen(argv[i], "wb"); | |
603 | continue; | |
604 | } | |
605 | ||
a4d04587 | 606 | httpSeparateURI(HTTP_URI_CODING_MOST, argv[i], scheme, sizeof(scheme), |
607 | username, sizeof(username), | |
ef416fc2 | 608 | hostname, sizeof(hostname), &port, |
609 | resource, sizeof(resource)); | |
610 | ||
83e08001 MS |
611 | if (!_cups_strcasecmp(scheme, "https") || !_cups_strcasecmp(scheme, "ipps") || |
612 | port == 443) | |
cb7f98ee | 613 | encryption = HTTP_ENCRYPTION_ALWAYS; |
83e08001 | 614 | else |
cb7f98ee | 615 | encryption = HTTP_ENCRYPTION_IF_REQUESTED; |
83e08001 | 616 | |
376d7c69 | 617 | http = httpConnect2(hostname, port, NULL, AF_UNSPEC, encryption, 1, 30000, NULL); |
ef416fc2 | 618 | if (http == NULL) |
619 | { | |
620 | perror(hostname); | |
621 | continue; | |
622 | } | |
9653cfdf MS |
623 | |
624 | if (httpIsEncrypted(http)) | |
625 | { | |
626 | cups_array_t *creds; | |
627 | char info[1024]; | |
524c65e6 | 628 | static const char *trusts[] = { "OK", "Invalid", "Changed", "Expired", "Renewed", "Unknown" }; |
9653cfdf MS |
629 | if (!httpCopyCredentials(http, &creds)) |
630 | { | |
bdc8d1ad | 631 | cups_array_t *lcreds; |
524c65e6 | 632 | http_trust_t trust = httpCredentialsGetTrust(creds, hostname); |
88f1e9c8 | 633 | |
9653cfdf | 634 | httpCredentialsString(creds, info, sizeof(info)); |
376d7c69 | 635 | |
bdc8d1ad | 636 | printf("Count: %d\n", cupsArrayCount(creds)); |
524c65e6 | 637 | printf("Trust: %s\n", trusts[trust]); |
376d7c69 | 638 | printf("Expiration: %s\n", httpGetDateString(httpCredentialsGetExpiration(creds))); |
524c65e6 | 639 | printf("IsValidName: %d\n", httpCredentialsAreValidForName(creds, hostname)); |
376d7c69 MS |
640 | printf("String: \"%s\"\n", info); |
641 | ||
bdc8d1ad MS |
642 | printf("LoadCredentials: %d\n", httpLoadCredentials(NULL, &lcreds, hostname)); |
643 | httpCredentialsString(lcreds, info, sizeof(info)); | |
644 | printf(" Count: %d\n", cupsArrayCount(lcreds)); | |
645 | printf(" String: \"%s\"\n", info); | |
646 | ||
647 | if (lcreds && cupsArrayCount(creds) == cupsArrayCount(lcreds)) | |
648 | { | |
bdc8d1ad MS |
649 | http_credential_t *cred, *lcred; |
650 | ||
651 | for (i = 1, cred = (http_credential_t *)cupsArrayFirst(creds), lcred = (http_credential_t *)cupsArrayFirst(lcreds); | |
652 | cred && lcred; | |
653 | i ++, cred = (http_credential_t *)cupsArrayNext(creds), lcred = (http_credential_t *)cupsArrayNext(lcreds)) | |
654 | { | |
655 | if (cred->datalen != lcred->datalen) | |
656 | printf(" Credential #%d: Different lengths (saved=%d, current=%d)\n", i, (int)cred->datalen, (int)lcred->datalen); | |
657 | else if (memcmp(cred->data, lcred->data, cred->datalen)) | |
658 | printf(" Credential #%d: Different data\n", i); | |
659 | else | |
660 | printf(" Credential #%d: Matches\n", i); | |
661 | } | |
662 | } | |
663 | ||
524c65e6 | 664 | if (trust != HTTP_TRUST_OK) |
88f1e9c8 MS |
665 | { |
666 | printf("SaveCredentials: %d\n", httpSaveCredentials(NULL, creds, hostname)); | |
524c65e6 MS |
667 | trust = httpCredentialsGetTrust(creds, hostname); |
668 | printf("New Trust: %s\n", trusts[trust]); | |
88f1e9c8 MS |
669 | } |
670 | ||
9653cfdf | 671 | httpFreeCredentials(creds); |
9653cfdf MS |
672 | } |
673 | else | |
376d7c69 | 674 | puts("No credentials!"); |
9653cfdf MS |
675 | } |
676 | ||
a469f8a5 | 677 | printf("Checking file \"%s\"...\n", resource); |
0c4bedc4 MS |
678 | |
679 | do | |
680 | { | |
681 | if (!_cups_strcasecmp(httpGetField(http, HTTP_FIELD_CONNECTION), "close")) | |
682 | { | |
683 | httpClearFields(http); | |
684 | if (httpReconnect2(http, 30000, NULL)) | |
685 | { | |
686 | status = HTTP_STATUS_ERROR; | |
687 | break; | |
688 | } | |
689 | } | |
690 | ||
691 | httpClearFields(http); | |
692 | httpSetField(http, HTTP_FIELD_AUTHORIZATION, httpGetAuthString(http)); | |
693 | httpSetField(http, HTTP_FIELD_ACCEPT_LANGUAGE, "en"); | |
694 | if (httpHead(http, resource)) | |
695 | { | |
696 | if (httpReconnect2(http, 30000, NULL)) | |
697 | { | |
698 | status = HTTP_STATUS_ERROR; | |
699 | break; | |
700 | } | |
701 | else | |
702 | { | |
703 | status = HTTP_STATUS_UNAUTHORIZED; | |
704 | continue; | |
705 | } | |
706 | } | |
707 | ||
708 | while ((status = httpUpdate(http)) == HTTP_STATUS_CONTINUE); | |
709 | ||
710 | if (status == HTTP_STATUS_UNAUTHORIZED) | |
711 | { | |
712 | /* | |
713 | * Flush any error message... | |
714 | */ | |
715 | ||
716 | httpFlush(http); | |
717 | ||
718 | /* | |
719 | * See if we can do authentication... | |
720 | */ | |
721 | ||
722 | if (cupsDoAuthentication(http, "GET", resource)) | |
723 | { | |
724 | status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED; | |
725 | break; | |
726 | } | |
727 | ||
728 | if (httpReconnect2(http, 30000, NULL)) | |
729 | { | |
730 | status = HTTP_STATUS_ERROR; | |
731 | break; | |
732 | } | |
733 | ||
734 | continue; | |
735 | } | |
736 | #ifdef HAVE_SSL | |
737 | else if (status == HTTP_STATUS_UPGRADE_REQUIRED) | |
738 | { | |
739 | /* Flush any error message... */ | |
740 | httpFlush(http); | |
741 | ||
742 | /* Reconnect... */ | |
743 | if (httpReconnect2(http, 30000, NULL)) | |
744 | { | |
745 | status = HTTP_STATUS_ERROR; | |
746 | break; | |
747 | } | |
748 | ||
749 | /* Upgrade with encryption... */ | |
750 | httpEncryption(http, HTTP_ENCRYPTION_REQUIRED); | |
751 | ||
752 | /* Try again, this time with encryption enabled... */ | |
753 | continue; | |
754 | } | |
755 | #endif /* HAVE_SSL */ | |
756 | } | |
757 | while (status == HTTP_STATUS_UNAUTHORIZED || | |
758 | status == HTTP_STATUS_UPGRADE_REQUIRED); | |
a469f8a5 | 759 | |
cb7f98ee | 760 | if (status == HTTP_STATUS_OK) |
a469f8a5 MS |
761 | puts("HEAD OK:"); |
762 | else | |
763 | printf("HEAD failed with status %d...\n", status); | |
764 | ||
765 | encoding = httpGetContentEncoding(http); | |
766 | ||
767 | printf("Requesting file \"%s\" (Accept-Encoding: %s)...\n", resource, | |
768 | encoding ? encoding : "identity"); | |
0c4bedc4 MS |
769 | |
770 | do | |
771 | { | |
772 | if (!_cups_strcasecmp(httpGetField(http, HTTP_FIELD_CONNECTION), "close")) | |
773 | { | |
774 | httpClearFields(http); | |
775 | if (httpReconnect2(http, 30000, NULL)) | |
776 | { | |
777 | status = HTTP_STATUS_ERROR; | |
778 | break; | |
779 | } | |
780 | } | |
781 | ||
782 | httpClearFields(http); | |
783 | httpSetField(http, HTTP_FIELD_AUTHORIZATION, httpGetAuthString(http)); | |
784 | httpSetField(http, HTTP_FIELD_ACCEPT_LANGUAGE, "en"); | |
785 | httpSetField(http, HTTP_FIELD_ACCEPT_ENCODING, encoding); | |
786 | ||
787 | if (httpGet(http, resource)) | |
788 | { | |
789 | if (httpReconnect2(http, 30000, NULL)) | |
790 | { | |
791 | status = HTTP_STATUS_ERROR; | |
792 | break; | |
793 | } | |
794 | else | |
795 | { | |
796 | status = HTTP_STATUS_UNAUTHORIZED; | |
797 | continue; | |
798 | } | |
799 | } | |
800 | ||
801 | while ((status = httpUpdate(http)) == HTTP_STATUS_CONTINUE); | |
802 | ||
803 | if (status == HTTP_STATUS_UNAUTHORIZED) | |
804 | { | |
805 | /* | |
806 | * Flush any error message... | |
807 | */ | |
808 | ||
809 | httpFlush(http); | |
810 | ||
811 | /* | |
812 | * See if we can do authentication... | |
813 | */ | |
814 | ||
815 | if (cupsDoAuthentication(http, "GET", resource)) | |
816 | { | |
817 | status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED; | |
818 | break; | |
819 | } | |
820 | ||
821 | if (httpReconnect2(http, 30000, NULL)) | |
822 | { | |
823 | status = HTTP_STATUS_ERROR; | |
824 | break; | |
825 | } | |
826 | ||
827 | continue; | |
828 | } | |
829 | #ifdef HAVE_SSL | |
830 | else if (status == HTTP_STATUS_UPGRADE_REQUIRED) | |
831 | { | |
832 | /* Flush any error message... */ | |
833 | httpFlush(http); | |
834 | ||
835 | /* Reconnect... */ | |
836 | if (httpReconnect2(http, 30000, NULL)) | |
837 | { | |
838 | status = HTTP_STATUS_ERROR; | |
839 | break; | |
840 | } | |
841 | ||
842 | /* Upgrade with encryption... */ | |
843 | httpEncryption(http, HTTP_ENCRYPTION_REQUIRED); | |
844 | ||
845 | /* Try again, this time with encryption enabled... */ | |
846 | continue; | |
847 | } | |
848 | #endif /* HAVE_SSL */ | |
849 | } | |
850 | while (status == HTTP_STATUS_UNAUTHORIZED || status == HTTP_STATUS_UPGRADE_REQUIRED); | |
ef416fc2 | 851 | |
cb7f98ee | 852 | if (status == HTTP_STATUS_OK) |
ef416fc2 | 853 | puts("GET OK:"); |
854 | else | |
855 | printf("GET failed with status %d...\n", status); | |
856 | ||
ef416fc2 | 857 | start = time(NULL); |
858 | length = httpGetLength2(http); | |
859 | total = 0; | |
860 | ||
a4d04587 | 861 | while ((bytes = httpRead2(http, buffer, sizeof(buffer))) > 0) |
ef416fc2 | 862 | { |
863 | total += bytes; | |
7e86f2f6 | 864 | fwrite(buffer, (size_t)bytes, 1, out); |
ef416fc2 | 865 | if (out != stdout) |
866 | { | |
867 | current = time(NULL); | |
376d7c69 MS |
868 | if (current == start) |
869 | current ++; | |
870 | ||
ef416fc2 | 871 | printf("\r" CUPS_LLFMT "/" CUPS_LLFMT " bytes (" |
e1d6a774 | 872 | CUPS_LLFMT " bytes/sec) ", CUPS_LLCAST total, |
873 | CUPS_LLCAST length, CUPS_LLCAST (total / (current - start))); | |
ef416fc2 | 874 | fflush(stdout); |
875 | } | |
876 | } | |
877 | } | |
878 | ||
376d7c69 MS |
879 | if (out != stdout) |
880 | putchar('\n'); | |
881 | ||
ef416fc2 | 882 | puts("Closing connection to server..."); |
883 | httpClose(http); | |
884 | ||
885 | if (out != stdout) | |
886 | fclose(out); | |
887 | ||
888 | return (0); | |
889 | } |