]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/testhttp.c
Sync up with CUPS 1.7svn-r10893
[thirdparty/cups.git] / cups / testhttp.c
1 /*
2 * "$Id: testhttp.c 7742 2008-07-15 20:23:09Z mike $"
3 *
4 * HTTP test program for CUPS.
5 *
6 * Copyright 2007-2013 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
8 *
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
14 *
15 * This file is subject to the Apple OS-Developed Software exception.
16 *
17 * Contents:
18 *
19 * main() - Main entry.
20 */
21
22 /*
23 * Include necessary headers...
24 */
25
26 #include "string-private.h"
27 #include "http-private.h"
28
29
30 /*
31 * Types and structures...
32 */
33
34 typedef struct uri_test_s /**** URI test cases ****/
35 {
36 http_uri_status_t result; /* Expected return value */
37 const char *uri, /* URI */
38 *scheme, /* Scheme string */
39 *username, /* Username:password string */
40 *hostname, /* Hostname string */
41 *resource; /* Resource string */
42 int port, /* Port number */
43 assemble_port; /* Port number for httpAssembleURI() */
44 } uri_test_t;
45
46
47 /*
48 * Local globals...
49 */
50
51 static uri_test_t uri_tests[] = /* URI test data */
52 {
53 /* Start with valid URIs */
54 { HTTP_URI_STATUS_OK, "file:/filename",
55 "file", "", "", "/filename", 0, 0 },
56 { HTTP_URI_STATUS_OK, "file:/filename%20with%20spaces",
57 "file", "", "", "/filename with spaces", 0, 0 },
58 { HTTP_URI_STATUS_OK, "file:///filename",
59 "file", "", "", "/filename", 0, 0 },
60 { HTTP_URI_STATUS_OK, "file:///filename%20with%20spaces",
61 "file", "", "", "/filename with spaces", 0, 0 },
62 { HTTP_URI_STATUS_OK, "file://localhost/filename",
63 "file", "", "localhost", "/filename", 0, 0 },
64 { HTTP_URI_STATUS_OK, "file://localhost/filename%20with%20spaces",
65 "file", "", "localhost", "/filename with spaces", 0, 0 },
66 { HTTP_URI_STATUS_OK, "http://server/",
67 "http", "", "server", "/", 80, 0 },
68 { HTTP_URI_STATUS_OK, "http://username@server/",
69 "http", "username", "server", "/", 80, 0 },
70 { HTTP_URI_STATUS_OK, "http://username:passwor%64@server/",
71 "http", "username:password", "server", "/", 80, 0 },
72 { HTTP_URI_STATUS_OK, "http://username:passwor%64@server:8080/",
73 "http", "username:password", "server", "/", 8080, 8080 },
74 { HTTP_URI_STATUS_OK, "http://username:passwor%64@server:8080/directory/filename",
75 "http", "username:password", "server", "/directory/filename", 8080, 8080 },
76 { HTTP_URI_STATUS_OK, "http://[2000::10:100]:631/ipp",
77 "http", "", "2000::10:100", "/ipp", 631, 631 },
78 { HTTP_URI_STATUS_OK, "https://username:passwor%64@server/directory/filename",
79 "https", "username:password", "server", "/directory/filename", 443, 0 },
80 { HTTP_URI_STATUS_OK, "ipp://username:passwor%64@[::1]/ipp",
81 "ipp", "username:password", "::1", "/ipp", 631, 0 },
82 { HTTP_URI_STATUS_OK, "lpd://server/queue?reserve=yes",
83 "lpd", "", "server", "/queue?reserve=yes", 515, 0 },
84 { HTTP_URI_STATUS_OK, "mailto:user@domain.com",
85 "mailto", "", "", "user@domain.com", 0, 0 },
86 { HTTP_URI_STATUS_OK, "socket://server/",
87 "socket", "", "server", "/", 9100, 0 },
88 { HTTP_URI_STATUS_OK, "socket://192.168.1.1:9101/",
89 "socket", "", "192.168.1.1", "/", 9101, 9101 },
90 { HTTP_URI_STATUS_OK, "ipp://username:password@[v1.fe80::200:1234:5678:9abc+eth0]:999/ipp",
91 "ipp", "username:password", "fe80::200:1234:5678:9abc%eth0", "/ipp", 999, 999 },
92 { HTTP_URI_STATUS_OK, "http://server/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400",
93 "http", "", "server", "/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400", 80, 0 },
94 { HTTP_URI_STATUS_OK, "lpd://Acme%20Laser%20(01%3A23%3A45).local._tcp._printer/",
95 "lpd", "", "Acme Laser (01:23:45).local._tcp._printer", "/", 515, 0 },
96 { HTTP_URI_STATUS_OK, "ipp://HP%20Officejet%204500%20G510n-z%20%40%20Will's%20MacBook%20Pro%2015%22._ipp._tcp.local./",
97 "ipp", "", "HP Officejet 4500 G510n-z @ Will's MacBook Pro 15\"._ipp._tcp.local.", "/", 631, 0 },
98
99 /* Missing scheme */
100 { HTTP_URI_STATUS_MISSING_SCHEME, "/path/to/file/index.html",
101 "file", "", "", "/path/to/file/index.html", 0, 0 },
102 { HTTP_URI_STATUS_MISSING_SCHEME, "//server/ipp",
103 "ipp", "", "server", "/ipp", 631, 0 },
104
105 /* Unknown scheme */
106 { HTTP_URI_STATUS_UNKNOWN_SCHEME, "vendor://server/resource",
107 "vendor", "", "server", "/resource", 0, 0 },
108
109 /* Missing resource */
110 { HTTP_URI_STATUS_MISSING_RESOURCE, "socket://[::192.168.2.1]",
111 "socket", "", "::192.168.2.1", "/", 9100, 0 },
112 { HTTP_URI_STATUS_MISSING_RESOURCE, "socket://192.168.1.1:9101",
113 "socket", "", "192.168.1.1", "/", 9101 },
114
115 /* Bad URI */
116 { HTTP_URI_STATUS_BAD_URI, "",
117 "", "", "", "", 0, 0 },
118
119 /* Bad scheme */
120 { HTTP_URI_STATUS_BAD_SCHEME, "bad_scheme://server/resource",
121 "", "", "", "", 0, 0 },
122
123 /* Bad username */
124 { HTTP_URI_STATUS_BAD_USERNAME, "http://username:passwor%6@server/resource",
125 "http", "", "", "", 80, 0 },
126
127 /* Bad hostname */
128 { HTTP_URI_STATUS_BAD_HOSTNAME, "http://[/::1]/index.html",
129 "http", "", "", "", 80, 0 },
130 { HTTP_URI_STATUS_BAD_HOSTNAME, "http://[",
131 "http", "", "", "", 80, 0 },
132 { HTTP_URI_STATUS_BAD_HOSTNAME, "http://serve%7/index.html",
133 "http", "", "", "", 80, 0 },
134
135 /* Bad port number */
136 { HTTP_URI_STATUS_BAD_PORT, "http://127.0.0.1:9999a/index.html",
137 "http", "", "127.0.0.1", "", 0, 0 },
138
139 /* Bad resource */
140 { HTTP_URI_STATUS_BAD_RESOURCE, "http://server/index.html%",
141 "http", "", "server", "", 80, 0 }
142 };
143 static const char * const base64_tests[][2] =
144 {
145 { "A", "QQ==" },
146 /* 010000 01 */
147 { "AB", "QUI=" },
148 /* 010000 010100 0010 */
149 { "ABC", "QUJD" },
150 /* 010000 010100 001001 000011 */
151 { "ABCD", "QUJDRA==" },
152 /* 010000 010100 001001 000011 010001 00 */
153 { "ABCDE", "QUJDREU=" },
154 /* 010000 010100 001001 000011 010001 000100 0101 */
155 { "ABCDEF", "QUJDREVG" },
156 /* 010000 010100 001001 000011 010001 000100 010101 000110 */
157 };
158
159
160 /*
161 * 'main()' - Main entry.
162 */
163
164 int /* O - Exit status */
165 main(int argc, /* I - Number of command-line arguments */
166 char *argv[]) /* I - Command-line arguments */
167 {
168 int i, j, k; /* Looping vars */
169 http_t *http; /* HTTP connection */
170 http_encryption_t encryption; /* Encryption type */
171 http_status_t status; /* Status of GET command */
172 int failures; /* Number of test failures */
173 char buffer[8192]; /* Input buffer */
174 long bytes; /* Number of bytes read */
175 FILE *out; /* Output file */
176 char encode[256], /* Base64-encoded string */
177 decode[256]; /* Base64-decoded string */
178 int decodelen; /* Length of decoded string */
179 char scheme[HTTP_MAX_URI], /* Scheme from URI */
180 hostname[HTTP_MAX_URI], /* Hostname from URI */
181 username[HTTP_MAX_URI], /* Username:password from URI */
182 resource[HTTP_MAX_URI]; /* Resource from URI */
183 int port; /* Port number from URI */
184 http_uri_status_t uri_status; /* Status of URI separation */
185 http_addrlist_t *addrlist, /* Address list */
186 *addr; /* Current address */
187 off_t length, total; /* Length and total bytes */
188 time_t start, current; /* Start and end time */
189 const char *encoding; /* Negotiated Content-Encoding */
190 static const char * const uri_status_strings[] =
191 {
192 "HTTP_URI_STATUS_OVERFLOW",
193 "HTTP_URI_STATUS_BAD_ARGUMENTS",
194 "HTTP_URI_STATUS_BAD_RESOURCE",
195 "HTTP_URI_STATUS_BAD_PORT",
196 "HTTP_URI_STATUS_BAD_HOSTNAME",
197 "HTTP_URI_STATUS_BAD_USERNAME",
198 "HTTP_URI_STATUS_BAD_SCHEME",
199 "HTTP_URI_STATUS_BAD_URI",
200 "HTTP_URI_STATUS_OK",
201 "HTTP_URI_STATUS_MISSING_SCHEME",
202 "HTTP_URI_STATUS_UNKNOWN_SCHEME",
203 "HTTP_URI_STATUS_MISSING_RESOURCE"
204 };
205
206
207 /*
208 * Do API tests if we don't have a URL on the command-line...
209 */
210
211 if (argc == 1)
212 {
213 failures = 0;
214
215 /*
216 * httpGetDateString()/httpGetDateTime()
217 */
218
219 fputs("httpGetDateString()/httpGetDateTime(): ", stdout);
220
221 start = time(NULL);
222 strlcpy(buffer, httpGetDateString(start), sizeof(buffer));
223 current = httpGetDateTime(buffer);
224
225 i = (int)(current - start);
226 if (i < 0)
227 i = -i;
228
229 if (!i)
230 puts("PASS");
231 else
232 {
233 failures ++;
234 puts("FAIL");
235 printf(" Difference is %d seconds, %02d:%02d:%02d...\n", i, i / 3600,
236 (i / 60) % 60, i % 60);
237 printf(" httpGetDateString(%d) returned \"%s\"\n", (int)start, buffer);
238 printf(" httpGetDateTime(\"%s\") returned %d\n", buffer, (int)current);
239 printf(" httpGetDateString(%d) returned \"%s\"\n", (int)current,
240 httpGetDateString(current));
241 }
242
243 /*
244 * httpDecode64_2()/httpEncode64_2()
245 */
246
247 fputs("httpDecode64_2()/httpEncode64_2(): ", stdout);
248
249 for (i = 0, j = 0; i < (int)(sizeof(base64_tests) / sizeof(base64_tests[0])); i ++)
250 {
251 httpEncode64_2(encode, sizeof(encode), base64_tests[i][0],
252 (int)strlen(base64_tests[i][0]));
253 decodelen = (int)sizeof(decode);
254 httpDecode64_2(decode, &decodelen, base64_tests[i][1]);
255
256 if (strcmp(decode, base64_tests[i][0]))
257 {
258 failures ++;
259
260 if (j)
261 {
262 puts("FAIL");
263 j = 1;
264 }
265
266 printf(" httpDecode64_2() returned \"%s\", expected \"%s\"...\n",
267 decode, base64_tests[i][0]);
268 }
269
270 if (strcmp(encode, base64_tests[i][1]))
271 {
272 failures ++;
273
274 if (j)
275 {
276 puts("FAIL");
277 j = 1;
278 }
279
280 printf(" httpEncode64_2() returned \"%s\", expected \"%s\"...\n",
281 encode, base64_tests[i][1]);
282 }
283 }
284
285 if (!j)
286 puts("PASS");
287
288 /*
289 * httpGetHostname()
290 */
291
292 fputs("httpGetHostname(): ", stdout);
293
294 if (httpGetHostname(NULL, hostname, sizeof(hostname)))
295 printf("PASS (%s)\n", hostname);
296 else
297 {
298 failures ++;
299 puts("FAIL");
300 }
301
302 /*
303 * httpAddrGetList()
304 */
305
306 printf("httpAddrGetList(%s): ", hostname);
307
308 addrlist = httpAddrGetList(hostname, AF_UNSPEC, NULL);
309 if (addrlist)
310 {
311 for (i = 0, addr = addrlist; addr; i ++, addr = addr->next)
312 {
313 char numeric[1024]; /* Numeric IP address */
314
315
316 httpAddrString(&(addr->addr), numeric, sizeof(numeric));
317 if (!strcmp(numeric, "UNKNOWN"))
318 break;
319 }
320
321 if (addr)
322 printf("FAIL (bad address for %s)\n", hostname);
323 else
324 printf("PASS (%d address(es) for %s)\n", i, hostname);
325
326 httpAddrFreeList(addrlist);
327 }
328 else if (isdigit(hostname[0] & 255))
329 {
330 puts("FAIL (ignored because hostname is numeric)");
331 }
332 else
333 {
334 failures ++;
335 puts("FAIL");
336 }
337
338 /*
339 * Test httpSeparateURI()...
340 */
341
342 fputs("httpSeparateURI(): ", stdout);
343 for (i = 0, j = 0; i < (int)(sizeof(uri_tests) / sizeof(uri_tests[0])); i ++)
344 {
345 uri_status = httpSeparateURI(HTTP_URI_CODING_MOST,
346 uri_tests[i].uri, scheme, sizeof(scheme),
347 username, sizeof(username),
348 hostname, sizeof(hostname), &port,
349 resource, sizeof(resource));
350 if (uri_status != uri_tests[i].result ||
351 strcmp(scheme, uri_tests[i].scheme) ||
352 strcmp(username, uri_tests[i].username) ||
353 strcmp(hostname, uri_tests[i].hostname) ||
354 port != uri_tests[i].port ||
355 strcmp(resource, uri_tests[i].resource))
356 {
357 failures ++;
358
359 if (!j)
360 {
361 puts("FAIL");
362 j = 1;
363 }
364
365 printf(" \"%s\":\n", uri_tests[i].uri);
366
367 if (uri_status != uri_tests[i].result)
368 printf(" Returned %s instead of %s\n",
369 uri_status_strings[uri_status + 8],
370 uri_status_strings[uri_tests[i].result + 8]);
371
372 if (strcmp(scheme, uri_tests[i].scheme))
373 printf(" Scheme \"%s\" instead of \"%s\"\n",
374 scheme, uri_tests[i].scheme);
375
376 if (strcmp(username, uri_tests[i].username))
377 printf(" Username \"%s\" instead of \"%s\"\n",
378 username, uri_tests[i].username);
379
380 if (strcmp(hostname, uri_tests[i].hostname))
381 printf(" Hostname \"%s\" instead of \"%s\"\n",
382 hostname, uri_tests[i].hostname);
383
384 if (port != uri_tests[i].port)
385 printf(" Port %d instead of %d\n",
386 port, uri_tests[i].port);
387
388 if (strcmp(resource, uri_tests[i].resource))
389 printf(" Resource \"%s\" instead of \"%s\"\n",
390 resource, uri_tests[i].resource);
391 }
392 }
393
394 if (!j)
395 printf("PASS (%d URIs tested)\n",
396 (int)(sizeof(uri_tests) / sizeof(uri_tests[0])));
397
398 /*
399 * Test httpAssembleURI()...
400 */
401
402 fputs("httpAssembleURI(): ", stdout);
403 for (i = 0, j = 0, k = 0;
404 i < (int)(sizeof(uri_tests) / sizeof(uri_tests[0]));
405 i ++)
406 if (uri_tests[i].result == HTTP_URI_STATUS_OK &&
407 !strstr(uri_tests[i].uri, "%64") &&
408 strstr(uri_tests[i].uri, "//"))
409 {
410 k ++;
411 uri_status = httpAssembleURI(HTTP_URI_CODING_MOST,
412 buffer, sizeof(buffer),
413 uri_tests[i].scheme,
414 uri_tests[i].username,
415 uri_tests[i].hostname,
416 uri_tests[i].assemble_port,
417 uri_tests[i].resource);
418
419 if (uri_status != HTTP_URI_STATUS_OK)
420 {
421 failures ++;
422
423 if (!j)
424 {
425 puts("FAIL");
426 j = 1;
427 }
428
429 printf(" \"%s\": %s\n", uri_tests[i].uri,
430 uri_status_strings[uri_status + 8]);
431 }
432 else if (strcmp(buffer, uri_tests[i].uri))
433 {
434 failures ++;
435
436 if (!j)
437 {
438 puts("FAIL");
439 j = 1;
440 }
441
442 printf(" \"%s\": assembled = \"%s\"\n", uri_tests[i].uri,
443 buffer);
444 }
445 }
446
447 if (!j)
448 printf("PASS (%d URIs tested)\n", k);
449
450 /*
451 * Show a summary and return...
452 */
453
454 if (failures)
455 printf("\n%d TESTS FAILED!\n", failures);
456 else
457 puts("\nALL TESTS PASSED!");
458
459 return (failures);
460 }
461 else if (strstr(argv[1], "._tcp"))
462 {
463 /*
464 * Test resolving an mDNS name.
465 */
466
467 char resolved[1024]; /* Resolved URI */
468
469
470 printf("_httpResolveURI(%s, _HTTP_RESOLVE_DEFAULT): ", argv[1]);
471 fflush(stdout);
472
473 if (!_httpResolveURI(argv[1], resolved, sizeof(resolved),
474 _HTTP_RESOLVE_DEFAULT, NULL, NULL))
475 {
476 puts("FAIL");
477 return (1);
478 }
479 else
480 printf("PASS (%s)\n", resolved);
481
482 printf("_httpResolveURI(%s, _HTTP_RESOLVE_FQDN): ", argv[1]);
483 fflush(stdout);
484
485 if (!_httpResolveURI(argv[1], resolved, sizeof(resolved),
486 _HTTP_RESOLVE_FQDN, NULL, NULL))
487 {
488 puts("FAIL");
489 return (1);
490 }
491 else if (strstr(resolved, ".local:"))
492 {
493 printf("FAIL (%s)\n", resolved);
494 return (1);
495 }
496 else
497 {
498 printf("PASS (%s)\n", resolved);
499 return (0);
500 }
501 }
502 else if (!strcmp(argv[1], "-u") && argc == 3)
503 {
504 /*
505 * Test URI separation...
506 */
507
508 uri_status = httpSeparateURI(HTTP_URI_CODING_ALL, argv[2], scheme,
509 sizeof(scheme), username, sizeof(username),
510 hostname, sizeof(hostname), &port,
511 resource, sizeof(resource));
512 printf("uri_status = %s\n", uri_status_strings[uri_status + 8]);
513 printf("scheme = \"%s\"\n", scheme);
514 printf("username = \"%s\"\n", username);
515 printf("hostname = \"%s\"\n", hostname);
516 printf("port = %d\n", port);
517 printf("resource = \"%s\"\n", resource);
518
519 return (0);
520 }
521
522 /*
523 * Test HTTP GET requests...
524 */
525
526 http = NULL;
527 out = stdout;
528
529 for (i = 1; i < argc; i ++)
530 {
531 if (!strcmp(argv[i], "-o"))
532 {
533 i ++;
534 if (i >= argc)
535 break;
536
537 out = fopen(argv[i], "wb");
538 continue;
539 }
540
541 httpSeparateURI(HTTP_URI_CODING_MOST, argv[i], scheme, sizeof(scheme),
542 username, sizeof(username),
543 hostname, sizeof(hostname), &port,
544 resource, sizeof(resource));
545
546 if (!_cups_strcasecmp(scheme, "https") || !_cups_strcasecmp(scheme, "ipps") ||
547 port == 443)
548 encryption = HTTP_ENCRYPTION_ALWAYS;
549 else
550 encryption = HTTP_ENCRYPTION_IF_REQUESTED;
551
552 http = httpConnect2(hostname, port, NULL, AF_UNSPEC, encryption, 1, 30000,
553 NULL);
554 if (http == NULL)
555 {
556 perror(hostname);
557 continue;
558 }
559 printf("Checking file \"%s\"...\n", resource);
560 httpClearFields(http);
561 httpSetField(http, HTTP_FIELD_ACCEPT_LANGUAGE, "en");
562 httpHead(http, resource);
563 while ((status = httpUpdate(http)) == HTTP_STATUS_CONTINUE);
564
565 if (status == HTTP_STATUS_OK)
566 puts("HEAD OK:");
567 else
568 printf("HEAD failed with status %d...\n", status);
569
570 encoding = httpGetContentEncoding(http);
571
572 printf("Requesting file \"%s\" (Accept-Encoding: %s)...\n", resource,
573 encoding ? encoding : "identity");
574 httpClearFields(http);
575 httpSetField(http, HTTP_FIELD_ACCEPT_LANGUAGE, "en");
576 httpSetField(http, HTTP_FIELD_ACCEPT_ENCODING, encoding);
577 httpGet(http, resource);
578 while ((status = httpUpdate(http)) == HTTP_STATUS_CONTINUE);
579
580 if (status == HTTP_STATUS_OK)
581 puts("GET OK:");
582 else
583 printf("GET failed with status %d...\n", status);
584
585 start = time(NULL);
586 length = httpGetLength2(http);
587 total = 0;
588
589 while ((bytes = httpRead2(http, buffer, sizeof(buffer))) > 0)
590 {
591 total += bytes;
592 fwrite(buffer, bytes, 1, out);
593 if (out != stdout)
594 {
595 current = time(NULL);
596 if (current == start) current ++;
597 printf("\r" CUPS_LLFMT "/" CUPS_LLFMT " bytes ("
598 CUPS_LLFMT " bytes/sec) ", CUPS_LLCAST total,
599 CUPS_LLCAST length, CUPS_LLCAST (total / (current - start)));
600 fflush(stdout);
601 }
602 }
603 }
604
605 puts("Closing connection to server...");
606 httpClose(http);
607
608 if (out != stdout)
609 fclose(out);
610
611 return (0);
612 }
613
614
615 /*
616 * End of "$Id: testhttp.c 7742 2008-07-15 20:23:09Z mike $".
617 */