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