1 /*
2  * HTTP test program for CUPS.
3  *
4  * Copyright © 2007-2018 by Apple Inc.
5  * Copyright © 1997-2006 by Easy Software Products.
6  *
7  * Licensed under Apache License v2.0.  See the file "LICENSE" for more
8  * information.
9  */
10 
11 /*
12  * Include necessary headers...
13  */
14 
15 #include "cups-private.h"
16 
17 
18 /*
19  * Types and structures...
20  */
21 
22 typedef 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 */
31 			assemble_port;	/* Port number for httpAssembleURI() */
32   http_uri_coding_t	assemble_coding;/* Coding for httpAssembleURI() */
33 } uri_test_t;
34 
35 
36 /*
37  * Local globals...
38  */
39 
40 static uri_test_t	uri_tests[] =	/* URI test data */
41 			{
42 			  /* Start with valid URIs */
43 			  { HTTP_URI_STATUS_OK, "file:/filename",
44 			    "file", "", "", "/filename", 0, 0,
45 			    HTTP_URI_CODING_MOST },
46 			  { HTTP_URI_STATUS_OK, "file:/filename%20with%20spaces",
47 			    "file", "", "", "/filename with spaces", 0, 0,
48 			    HTTP_URI_CODING_MOST  },
49 			  { HTTP_URI_STATUS_OK, "file:///filename",
50 			    "file", "", "", "/filename", 0, 0,
51 			    HTTP_URI_CODING_MOST  },
52 			  { HTTP_URI_STATUS_OK, "file:///filename%20with%20spaces",
53 			    "file", "", "", "/filename with spaces", 0, 0,
54 			    HTTP_URI_CODING_MOST  },
55 			  { HTTP_URI_STATUS_OK, "file://localhost/filename",
56 			    "file", "", "localhost", "/filename", 0, 0,
57 			    HTTP_URI_CODING_MOST  },
58 			  { HTTP_URI_STATUS_OK, "file://localhost/filename%20with%20spaces",
59 			    "file", "", "localhost", "/filename with spaces", 0, 0,
60 			    HTTP_URI_CODING_MOST  },
61 			  { HTTP_URI_STATUS_OK, "http://server/",
62 			    "http", "", "server", "/", 80, 0,
63 			    HTTP_URI_CODING_MOST  },
64 			  { HTTP_URI_STATUS_OK, "http://username@server/",
65 			    "http", "username", "server", "/", 80, 0,
66 			    HTTP_URI_CODING_MOST  },
67 			  { HTTP_URI_STATUS_OK, "http://username:passwor%64@server/",
68 			    "http", "username:password", "server", "/", 80, 0,
69 			    HTTP_URI_CODING_MOST  },
70 			  { HTTP_URI_STATUS_OK, "http://username:passwor%64@server:8080/",
71 			    "http", "username:password", "server", "/", 8080, 8080,
72 			    HTTP_URI_CODING_MOST  },
73 			  { HTTP_URI_STATUS_OK, "http://username:passwor%64@server:8080/directory/filename",
74 			    "http", "username:password", "server", "/directory/filename", 8080, 8080,
75 			    HTTP_URI_CODING_MOST  },
76 			  { HTTP_URI_STATUS_OK, "http://[2000::10:100]:631/ipp",
77 			    "http", "", "2000::10:100", "/ipp", 631, 631,
78 			    HTTP_URI_CODING_MOST  },
79 			  { HTTP_URI_STATUS_OK, "https://username:passwor%64@server/directory/filename",
80 			    "https", "username:password", "server", "/directory/filename", 443, 0,
81 			    HTTP_URI_CODING_MOST  },
82 			  { HTTP_URI_STATUS_OK, "ipp://username:passwor%64@[::1]/ipp",
83 			    "ipp", "username:password", "::1", "/ipp", 631, 0,
84 			    HTTP_URI_CODING_MOST  },
85 			  { HTTP_URI_STATUS_OK, "lpd://server/queue?reserve=yes",
86 			    "lpd", "", "server", "/queue?reserve=yes", 515, 0,
87 			    HTTP_URI_CODING_MOST  },
88 			  { HTTP_URI_STATUS_OK, "mailto:user@domain.com",
89 			    "mailto", "", "", "user@domain.com", 0, 0,
90 			    HTTP_URI_CODING_MOST  },
91 			  { HTTP_URI_STATUS_OK, "socket://server/",
92 			    "socket", "", "server", "/", 9100, 0,
93 			    HTTP_URI_CODING_MOST  },
94 			  { HTTP_URI_STATUS_OK, "socket://192.168.1.1:9101/",
95 			    "socket", "", "192.168.1.1", "/", 9101, 9101,
96 			    HTTP_URI_CODING_MOST  },
97 			  { HTTP_URI_STATUS_OK, "tel:8005551212",
98 			    "tel", "", "", "8005551212", 0, 0,
99 			    HTTP_URI_CODING_MOST  },
100 			  { HTTP_URI_STATUS_OK, "ipp://username:password@[v1.fe80::200:1234:5678:9abc+eth0]:999/ipp",
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,
105 			    (http_uri_coding_t)(HTTP_URI_CODING_MOST | HTTP_URI_CODING_RFC6874) },
106 			  { HTTP_URI_STATUS_OK, "http://server/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400",
107 			    "http", "", "server", "/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400", 80, 0,
108 			    HTTP_URI_CODING_MOST  },
109 			  { HTTP_URI_STATUS_OK, "lpd://Acme%20Laser%20(01%3A23%3A45).local._tcp._printer/",
110 			    "lpd", "", "Acme Laser (01:23:45).local._tcp._printer", "/", 515, 0,
111 			    HTTP_URI_CODING_MOST  },
112 			  { HTTP_URI_STATUS_OK, "ipp://HP%20Officejet%204500%20G510n-z%20%40%20Will's%20MacBook%20Pro%2015%22._ipp._tcp.local./",
113 			    "ipp", "", "HP Officejet 4500 G510n-z @ Will's MacBook Pro 15\"._ipp._tcp.local.", "/", 631, 0,
114 			    HTTP_URI_CODING_MOST  },
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  },
118 			  { HTTP_URI_STATUS_UNKNOWN_SCHEME, "smb://server/Some%20Printer",
119 			    "smb", "", "server", "/Some Printer", 0, 0,
120 			    HTTP_URI_CODING_ALL },
121 
122 			  /* Missing scheme */
123 			  { HTTP_URI_STATUS_MISSING_SCHEME, "/path/to/file/index.html",
124 			    "file", "", "", "/path/to/file/index.html", 0, 0,
125 			    HTTP_URI_CODING_MOST  },
126 			  { HTTP_URI_STATUS_MISSING_SCHEME, "//server/ipp",
127 			    "ipp", "", "server", "/ipp", 631, 0,
128 			    HTTP_URI_CODING_MOST  },
129 
130 			  /* Unknown scheme */
131 			  { HTTP_URI_STATUS_UNKNOWN_SCHEME, "vendor://server/resource",
132 			    "vendor", "", "server", "/resource", 0, 0,
133 			    HTTP_URI_CODING_MOST  },
134 
135 			  /* Missing resource */
136 			  { HTTP_URI_STATUS_MISSING_RESOURCE, "socket://[::192.168.2.1]",
137 			    "socket", "", "::192.168.2.1", "/", 9100, 0,
138 			    HTTP_URI_CODING_MOST  },
139 			  { HTTP_URI_STATUS_MISSING_RESOURCE, "socket://192.168.1.1:9101",
140 			    "socket", "", "192.168.1.1", "/", 9101, 0,
141 			    HTTP_URI_CODING_MOST  },
142 
143 			  /* Bad URI */
144 			  { HTTP_URI_STATUS_BAD_URI, "",
145 			    "", "", "", "", 0, 0,
146 			    HTTP_URI_CODING_MOST  },
147 
148 			  /* Bad scheme */
149 			  { HTTP_URI_STATUS_BAD_SCHEME, "://server/ipp",
150 			    "", "", "", "", 0, 0,
151 			    HTTP_URI_CODING_MOST  },
152 			  { HTTP_URI_STATUS_BAD_SCHEME, "bad_scheme://server/resource",
153 			    "", "", "", "", 0, 0,
154 			    HTTP_URI_CODING_MOST  },
155 
156 			  /* Bad username */
157 			  { HTTP_URI_STATUS_BAD_USERNAME, "http://username:passwor%6@server/resource",
158 			    "http", "", "", "", 80, 0,
159 			    HTTP_URI_CODING_MOST  },
160 
161 			  /* Bad hostname */
162 			  { HTTP_URI_STATUS_BAD_HOSTNAME, "http://[/::1]/index.html",
163 			    "http", "", "", "", 80, 0,
164 			    HTTP_URI_CODING_MOST  },
165 			  { HTTP_URI_STATUS_BAD_HOSTNAME, "http://[",
166 			    "http", "", "", "", 80, 0,
167 			    HTTP_URI_CODING_MOST  },
168 			  { HTTP_URI_STATUS_BAD_HOSTNAME, "http://serve%7/index.html",
169 			    "http", "", "", "", 80, 0,
170 			    HTTP_URI_CODING_MOST  },
171 			  { HTTP_URI_STATUS_BAD_HOSTNAME, "http://server with spaces/index.html",
172 			    "http", "", "", "", 80, 0,
173 			    HTTP_URI_CODING_MOST  },
174 			  { HTTP_URI_STATUS_BAD_HOSTNAME, "ipp://\"#/:<>?@[\\]^`{|}/",
175 			    "ipp", "", "", "", 631, 0,
176 			    HTTP_URI_CODING_MOST  },
177 
178 			  /* Bad port number */
179 			  { HTTP_URI_STATUS_BAD_PORT, "http://127.0.0.1:9999a/index.html",
180 			    "http", "", "127.0.0.1", "", 0, 0,
181 			    HTTP_URI_CODING_MOST  },
182 
183 			  /* Bad resource */
184 			  { HTTP_URI_STATUS_BAD_RESOURCE, "mailto:\r\nbla",
185 			    "mailto", "", "", "", 0, 0,
186 			    HTTP_URI_CODING_MOST  },
187 			  { HTTP_URI_STATUS_BAD_RESOURCE, "http://server/index.html%",
188 			    "http", "", "server", "", 80, 0,
189 			    HTTP_URI_CODING_MOST  },
190 			  { HTTP_URI_STATUS_BAD_RESOURCE, "http://server/index with spaces.html",
191 			    "http", "", "server", "", 80, 0,
192 			    HTTP_URI_CODING_MOST  }
193 			};
194 static 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 
215 int					/* O - Exit status */
main(int argc,char * argv[])216 main(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 */
221   http_encryption_t encryption;		/* Encryption type */
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 */
240   const char	*encoding;		/* Negotiated Content-Encoding */
241   static const char * const uri_status_strings[] =
242 		{
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"
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);
273     strlcpy(buffer, httpGetDateString(start), sizeof(buffer));
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],
303                      (int)strlen(base64_tests[i][0]));
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 
339 #if 0
340    /*
341     * _httpDigest()
342     */
343 
344     fputs("_httpDigest(MD5): ", stdout);
345     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"))
346     {
347       failures ++;
348       puts("FAIL (unable to calculate hash)");
349     }
350     else if (strcmp(buffer, "8ca523f5e9506fed4657c9700eebdbec"))
351     {
352       failures ++;
353       printf("FAIL (got \"%s\", expected \"8ca523f5e9506fed4657c9700eebdbec\")\n", buffer);
354     }
355     else
356       puts("PASS");
357 
358     fputs("_httpDigest(SHA-256): ", stdout);
359     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"))
360     {
361       failures ++;
362       puts("FAIL (unable to calculate hash)");
363     }
364     else if (strcmp(buffer, "753927fa0e85d155564e2e272a28d1802ca10daf4496794697cf8db5856cb6c1"))
365     {
366       failures ++;
367       printf("FAIL (got \"%s\", expected \"753927fa0e85d155564e2e272a28d1802ca10daf4496794697cf8db5856cb6c1\")\n", buffer);
368     }
369     else
370       puts("PASS");
371 #endif /* 0 */
372 
373    /*
374     * httpGetHostname()
375     */
376 
377     fputs("httpGetHostname(): ", stdout);
378 
379     if (httpGetHostname(NULL, hostname, sizeof(hostname)))
380       printf("PASS (%s)\n", hostname);
381     else
382     {
383       failures ++;
384       puts("FAIL");
385     }
386 
387    /*
388     * httpAddrGetList()
389     */
390 
391     printf("httpAddrGetList(%s): ", hostname);
392 
393     addrlist = httpAddrGetList(hostname, AF_UNSPEC, NULL);
394     if (addrlist)
395     {
396       for (i = 0, addr = addrlist; addr; i ++, addr = addr->next)
397       {
398         char	numeric[1024];		/* Numeric IP address */
399 
400 
401 	httpAddrString(&(addr->addr), numeric, sizeof(numeric));
402 	if (!strcmp(numeric, "UNKNOWN"))
403 	  break;
404       }
405 
406       if (addr)
407         printf("FAIL (bad address for %s)\n", hostname);
408       else
409         printf("PASS (%d address(es) for %s)\n", i, hostname);
410 
411       httpAddrFreeList(addrlist);
412     }
413     else if (isdigit(hostname[0] & 255))
414     {
415       puts("FAIL (ignored because hostname is numeric)");
416     }
417     else
418     {
419       failures ++;
420       puts("FAIL");
421     }
422 
423    /*
424     * Test httpSeparateURI()...
425     */
426 
427     fputs("httpSeparateURI(): ", stdout);
428     for (i = 0, j = 0; i < (int)(sizeof(uri_tests) / sizeof(uri_tests[0])); i ++)
429     {
430       uri_status = httpSeparateURI(HTTP_URI_CODING_MOST,
431 				   uri_tests[i].uri, scheme, sizeof(scheme),
432                                    username, sizeof(username),
433 				   hostname, sizeof(hostname), &port,
434 				   resource, sizeof(resource));
435       if (uri_status != uri_tests[i].result ||
436           strcmp(scheme, uri_tests[i].scheme) ||
437 	  strcmp(username, uri_tests[i].username) ||
438 	  strcmp(hostname, uri_tests[i].hostname) ||
439 	  port != uri_tests[i].port ||
440 	  strcmp(resource, uri_tests[i].resource))
441       {
442         failures ++;
443 
444 	if (!j)
445 	{
446 	  puts("FAIL");
447 	  j = 1;
448 	}
449 
450         printf("    \"%s\":\n", uri_tests[i].uri);
451 
452 	if (uri_status != uri_tests[i].result)
453 	  printf("        Returned %s instead of %s\n",
454 	         uri_status_strings[uri_status + 8],
455 		 uri_status_strings[uri_tests[i].result + 8]);
456 
457         if (strcmp(scheme, uri_tests[i].scheme))
458 	  printf("        Scheme \"%s\" instead of \"%s\"\n",
459 	         scheme, uri_tests[i].scheme);
460 
461 	if (strcmp(username, uri_tests[i].username))
462 	  printf("        Username \"%s\" instead of \"%s\"\n",
463 	         username, uri_tests[i].username);
464 
465 	if (strcmp(hostname, uri_tests[i].hostname))
466 	  printf("        Hostname \"%s\" instead of \"%s\"\n",
467 	         hostname, uri_tests[i].hostname);
468 
469 	if (port != uri_tests[i].port)
470 	  printf("        Port %d instead of %d\n",
471 	         port, uri_tests[i].port);
472 
473 	if (strcmp(resource, uri_tests[i].resource))
474 	  printf("        Resource \"%s\" instead of \"%s\"\n",
475 	         resource, uri_tests[i].resource);
476       }
477     }
478 
479     if (!j)
480       printf("PASS (%d URIs tested)\n",
481              (int)(sizeof(uri_tests) / sizeof(uri_tests[0])));
482 
483    /*
484     * Test httpAssembleURI()...
485     */
486 
487     fputs("httpAssembleURI(): ", stdout);
488     for (i = 0, j = 0, k = 0;
489          i < (int)(sizeof(uri_tests) / sizeof(uri_tests[0]));
490 	 i ++)
491       if (uri_tests[i].result == HTTP_URI_STATUS_OK &&
492           !strstr(uri_tests[i].uri, "%64") &&
493           strstr(uri_tests[i].uri, "//"))
494       {
495         k ++;
496 	uri_status = httpAssembleURI(uri_tests[i].assemble_coding,
497 				     buffer, sizeof(buffer),
498 	                             uri_tests[i].scheme,
499 				     uri_tests[i].username,
500 	                             uri_tests[i].hostname,
501 				     uri_tests[i].assemble_port,
502 				     uri_tests[i].resource);
503 
504         if (uri_status != HTTP_URI_STATUS_OK)
505 	{
506           failures ++;
507 
508 	  if (!j)
509 	  {
510 	    puts("FAIL");
511 	    j = 1;
512 	  }
513 
514           printf("    \"%s\": %s\n", uri_tests[i].uri,
515 	         uri_status_strings[uri_status + 8]);
516         }
517 	else if (strcmp(buffer, uri_tests[i].uri))
518 	{
519           failures ++;
520 
521 	  if (!j)
522 	  {
523 	    puts("FAIL");
524 	    j = 1;
525 	  }
526 
527           printf("    \"%s\": assembled = \"%s\"\n", uri_tests[i].uri,
528 	         buffer);
529 	}
530       }
531 
532     if (!j)
533       printf("PASS (%d URIs tested)\n", k);
534 
535    /*
536     * httpAssembleUUID
537     */
538 
539     fputs("httpAssembleUUID: ", stdout);
540     httpAssembleUUID("hostname.example.com", 631, "printer", 12345, buffer,
541                      sizeof(buffer));
542     if (strncmp(buffer, "urn:uuid:", 9))
543     {
544       printf("FAIL (%s)\n", buffer);
545       failures ++;
546     }
547     else
548       printf("PASS (%s)\n", buffer);
549 
550    /*
551     * Show a summary and return...
552     */
553 
554     if (failures)
555       printf("\n%d TESTS FAILED!\n", failures);
556     else
557       puts("\nALL TESTS PASSED!");
558 
559     return (failures);
560   }
561   else if (strstr(argv[1], "._tcp"))
562   {
563    /*
564     * Test resolving an mDNS name.
565     */
566 
567     char	resolved[1024];		/* Resolved URI */
568 
569 
570     printf("_httpResolveURI(%s, _HTTP_RESOLVE_DEFAULT): ", argv[1]);
571     fflush(stdout);
572 
573     if (!_httpResolveURI(argv[1], resolved, sizeof(resolved),
574                          _HTTP_RESOLVE_DEFAULT, NULL, NULL))
575     {
576       puts("FAIL");
577       return (1);
578     }
579     else
580       printf("PASS (%s)\n", resolved);
581 
582     printf("_httpResolveURI(%s, _HTTP_RESOLVE_FQDN): ", argv[1]);
583     fflush(stdout);
584 
585     if (!_httpResolveURI(argv[1], resolved, sizeof(resolved),
586                          _HTTP_RESOLVE_FQDN, NULL, NULL))
587     {
588       puts("FAIL");
589       return (1);
590     }
591     else if (strstr(resolved, ".local:"))
592     {
593       printf("FAIL (%s)\n", resolved);
594       return (1);
595     }
596     else
597     {
598       printf("PASS (%s)\n", resolved);
599       return (0);
600     }
601   }
602   else if (!strcmp(argv[1], "-u") && argc == 3)
603   {
604    /*
605     * Test URI separation...
606     */
607 
608     uri_status = httpSeparateURI(HTTP_URI_CODING_ALL, argv[2], scheme,
609                                  sizeof(scheme), username, sizeof(username),
610 				 hostname, sizeof(hostname), &port,
611 				 resource, sizeof(resource));
612     printf("uri_status = %s\n", uri_status_strings[uri_status + 8]);
613     printf("scheme     = \"%s\"\n", scheme);
614     printf("username   = \"%s\"\n", username);
615     printf("hostname   = \"%s\"\n", hostname);
616     printf("port       = %d\n", port);
617     printf("resource   = \"%s\"\n", resource);
618 
619     return (0);
620   }
621 
622  /*
623   * Test HTTP GET requests...
624   */
625 
626   http = NULL;
627   out = stdout;
628 
629   for (i = 1; i < argc; i ++)
630   {
631     int new_auth;
632 
633     if (!strcmp(argv[i], "-o"))
634     {
635       i ++;
636       if (i >= argc)
637         break;
638 
639       out = fopen(argv[i], "wb");
640       continue;
641     }
642 
643     httpSeparateURI(HTTP_URI_CODING_MOST, argv[i], scheme, sizeof(scheme),
644                     username, sizeof(username),
645                     hostname, sizeof(hostname), &port,
646 		    resource, sizeof(resource));
647 
648     if (!_cups_strcasecmp(scheme, "https") || !_cups_strcasecmp(scheme, "ipps") ||
649         port == 443)
650       encryption = HTTP_ENCRYPTION_ALWAYS;
651     else
652       encryption = HTTP_ENCRYPTION_IF_REQUESTED;
653 
654     http = httpConnect2(hostname, port, NULL, AF_UNSPEC, encryption, 1, 30000, NULL);
655     if (http == NULL)
656     {
657       perror(hostname);
658       continue;
659     }
660 
661     if (httpIsEncrypted(http))
662     {
663       cups_array_t *creds;
664       char info[1024];
665       static const char *trusts[] = { "OK", "Invalid", "Changed", "Expired", "Renewed", "Unknown" };
666       if (!httpCopyCredentials(http, &creds))
667       {
668 	cups_array_t *lcreds;
669         http_trust_t trust = httpCredentialsGetTrust(creds, hostname);
670 
671         httpCredentialsString(creds, info, sizeof(info));
672 
673 	printf("Count: %d\n", cupsArrayCount(creds));
674         printf("Trust: %s\n", trusts[trust]);
675         printf("Expiration: %s\n", httpGetDateString(httpCredentialsGetExpiration(creds)));
676         printf("IsValidName: %d\n", httpCredentialsAreValidForName(creds, hostname));
677         printf("String: \"%s\"\n", info);
678 
679 	printf("LoadCredentials: %d\n", httpLoadCredentials(NULL, &lcreds, hostname));
680 	httpCredentialsString(lcreds, info, sizeof(info));
681 	printf("    Count: %d\n", cupsArrayCount(lcreds));
682 	printf("    String: \"%s\"\n", info);
683 
684         if (lcreds && cupsArrayCount(creds) == cupsArrayCount(lcreds))
685         {
686           http_credential_t	*cred, *lcred;
687 
688           for (i = 1, cred = (http_credential_t *)cupsArrayFirst(creds), lcred = (http_credential_t *)cupsArrayFirst(lcreds);
689                cred && lcred;
690                i ++, cred = (http_credential_t *)cupsArrayNext(creds), lcred = (http_credential_t *)cupsArrayNext(lcreds))
691           {
692             if (cred->datalen != lcred->datalen)
693               printf("    Credential #%d: Different lengths (saved=%d, current=%d)\n", i, (int)cred->datalen, (int)lcred->datalen);
694             else if (memcmp(cred->data, lcred->data, cred->datalen))
695               printf("    Credential #%d: Different data\n", i);
696             else
697               printf("    Credential #%d: Matches\n", i);
698           }
699         }
700 
701         if (trust != HTTP_TRUST_OK)
702 	{
703 	  printf("SaveCredentials: %d\n", httpSaveCredentials(NULL, creds, hostname));
704 	  trust = httpCredentialsGetTrust(creds, hostname);
705 	  printf("New Trust: %s\n", trusts[trust]);
706 	}
707 
708         httpFreeCredentials(creds);
709       }
710       else
711         puts("No credentials!");
712     }
713 
714     printf("Checking file \"%s\"...\n", resource);
715 
716     new_auth = 0;
717 
718     do
719     {
720       if (!_cups_strcasecmp(httpGetField(http, HTTP_FIELD_CONNECTION), "close"))
721       {
722 	httpClearFields(http);
723 	if (httpReconnect2(http, 30000, NULL))
724 	{
725           status = HTTP_STATUS_ERROR;
726           break;
727 	}
728       }
729 
730       if (http->authstring && !strncmp(http->authstring, "Digest ", 7) && !new_auth)
731         _httpSetDigestAuthString(http, http->nextnonce, "HEAD", resource);
732 
733       httpClearFields(http);
734       httpSetField(http, HTTP_FIELD_AUTHORIZATION, httpGetAuthString(http));
735       httpSetField(http, HTTP_FIELD_ACCEPT_LANGUAGE, "en");
736 
737       if (httpHead(http, resource))
738       {
739         if (httpReconnect2(http, 30000, NULL))
740         {
741           status = HTTP_STATUS_ERROR;
742           break;
743         }
744         else
745         {
746           status = HTTP_STATUS_UNAUTHORIZED;
747           continue;
748         }
749       }
750 
751       while ((status = httpUpdate(http)) == HTTP_STATUS_CONTINUE);
752 
753       new_auth = 0;
754 
755       if (status == HTTP_STATUS_UNAUTHORIZED)
756       {
757        /*
758 	* Flush any error message...
759 	*/
760 
761 	httpFlush(http);
762 
763        /*
764 	* See if we can do authentication...
765 	*/
766 
767         new_auth = 1;
768 
769 	if (cupsDoAuthentication(http, "HEAD", resource))
770 	{
771 	  status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED;
772 	  break;
773 	}
774 
775 	if (httpReconnect2(http, 30000, NULL))
776 	{
777 	  status = HTTP_STATUS_ERROR;
778 	  break;
779 	}
780 
781 	continue;
782       }
783 #ifdef HAVE_SSL
784       else if (status == HTTP_STATUS_UPGRADE_REQUIRED)
785       {
786 	/* Flush any error message... */
787 	httpFlush(http);
788 
789 	/* Reconnect... */
790 	if (httpReconnect2(http, 30000, NULL))
791 	{
792 	  status = HTTP_STATUS_ERROR;
793 	  break;
794 	}
795 
796 	/* Upgrade with encryption... */
797 	httpEncryption(http, HTTP_ENCRYPTION_REQUIRED);
798 
799 	/* Try again, this time with encryption enabled... */
800 	continue;
801       }
802 #endif /* HAVE_SSL */
803     }
804     while (status == HTTP_STATUS_UNAUTHORIZED ||
805            status == HTTP_STATUS_UPGRADE_REQUIRED);
806 
807     if (status == HTTP_STATUS_OK)
808       puts("HEAD OK:");
809     else
810       printf("HEAD failed with status %d...\n", status);
811 
812     encoding = httpGetContentEncoding(http);
813 
814     printf("Requesting file \"%s\" (Accept-Encoding: %s)...\n", resource,
815            encoding ? encoding : "identity");
816 
817     new_auth = 0;
818 
819     do
820     {
821       if (!_cups_strcasecmp(httpGetField(http, HTTP_FIELD_CONNECTION), "close"))
822       {
823 	httpClearFields(http);
824 	if (httpReconnect2(http, 30000, NULL))
825 	{
826           status = HTTP_STATUS_ERROR;
827           break;
828 	}
829       }
830 
831       if (http->authstring && !strncmp(http->authstring, "Digest ", 7) && !new_auth)
832         _httpSetDigestAuthString(http, http->nextnonce, "GET", resource);
833 
834       httpClearFields(http);
835       httpSetField(http, HTTP_FIELD_AUTHORIZATION, httpGetAuthString(http));
836       httpSetField(http, HTTP_FIELD_ACCEPT_LANGUAGE, "en");
837       httpSetField(http, HTTP_FIELD_ACCEPT_ENCODING, encoding);
838 
839       if (httpGet(http, resource))
840       {
841         if (httpReconnect2(http, 30000, NULL))
842         {
843           status = HTTP_STATUS_ERROR;
844           break;
845         }
846         else
847         {
848           status = HTTP_STATUS_UNAUTHORIZED;
849           continue;
850         }
851       }
852 
853       while ((status = httpUpdate(http)) == HTTP_STATUS_CONTINUE);
854 
855       new_auth = 0;
856 
857       if (status == HTTP_STATUS_UNAUTHORIZED)
858       {
859        /*
860 	* Flush any error message...
861 	*/
862 
863 	httpFlush(http);
864 
865        /*
866 	* See if we can do authentication...
867 	*/
868 
869         new_auth = 1;
870 
871 	if (cupsDoAuthentication(http, "GET", resource))
872 	{
873 	  status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED;
874 	  break;
875 	}
876 
877 	if (httpReconnect2(http, 30000, NULL))
878 	{
879 	  status = HTTP_STATUS_ERROR;
880 	  break;
881 	}
882 
883 	continue;
884       }
885 #ifdef HAVE_SSL
886       else if (status == HTTP_STATUS_UPGRADE_REQUIRED)
887       {
888 	/* Flush any error message... */
889 	httpFlush(http);
890 
891 	/* Reconnect... */
892 	if (httpReconnect2(http, 30000, NULL))
893 	{
894 	  status = HTTP_STATUS_ERROR;
895 	  break;
896 	}
897 
898 	/* Upgrade with encryption... */
899 	httpEncryption(http, HTTP_ENCRYPTION_REQUIRED);
900 
901 	/* Try again, this time with encryption enabled... */
902 	continue;
903       }
904 #endif /* HAVE_SSL */
905     }
906     while (status == HTTP_STATUS_UNAUTHORIZED || status == HTTP_STATUS_UPGRADE_REQUIRED);
907 
908     if (status == HTTP_STATUS_OK)
909       puts("GET OK:");
910     else
911       printf("GET failed with status %d...\n", status);
912 
913     start  = time(NULL);
914     length = httpGetLength2(http);
915     total  = 0;
916 
917     while ((bytes = httpRead2(http, buffer, sizeof(buffer))) > 0)
918     {
919       total += bytes;
920       fwrite(buffer, (size_t)bytes, 1, out);
921       if (out != stdout)
922       {
923         current = time(NULL);
924         if (current == start)
925           current ++;
926 
927         printf("\r" CUPS_LLFMT "/" CUPS_LLFMT " bytes ("
928 	       CUPS_LLFMT " bytes/sec)      ", CUPS_LLCAST total,
929 	       CUPS_LLCAST length, CUPS_LLCAST (total / (current - start)));
930         fflush(stdout);
931       }
932     }
933   }
934 
935   if (out != stdout)
936     putchar('\n');
937 
938   puts("Closing connection to server...");
939   httpClose(http);
940 
941   if (out != stdout)
942     fclose(out);
943 
944   return (0);
945 }
946