]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/testhttp.c
Merge changes from CUPS 1.5svn-r9229.
[thirdparty/cups.git] / cups / testhttp.c
CommitLineData
ef416fc2 1/*
b19ccc9e 2 * "$Id: testhttp.c 7742 2008-07-15 20:23:09Z mike $"
ef416fc2 3 *
71e16022 4 * HTTP test program for CUPS.
ef416fc2 5 *
71e16022 6 * Copyright 2007-2010 by Apple Inc.
ed486911 7 * Copyright 1997-2006 by Easy Software Products.
ef416fc2 8 *
9 * These coded instructions, statements, and computer programs are the
bc44d920 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/".
ef416fc2 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
71e16022 26#include "string-private.h"
1f0275e3 27#include "http-private.h"
ef416fc2 28
29
30/*
31 * Types and structures...
32 */
33
34typedef 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
51static uri_test_t uri_tests[] = /* URI test data */
52 {
53 /* Start with valid URIs */
54 { HTTP_URI_OK, "file:/filename",
55 "file", "", "", "/filename", 0, 0 },
56 { HTTP_URI_OK, "file:/filename%20with%20spaces",
57 "file", "", "", "/filename with spaces", 0, 0 },
58 { HTTP_URI_OK, "file:///filename",
59 "file", "", "", "/filename", 0, 0 },
60 { HTTP_URI_OK, "file:///filename%20with%20spaces",
61 "file", "", "", "/filename with spaces", 0, 0 },
62 { HTTP_URI_OK, "file://localhost/filename",
63 "file", "", "localhost", "/filename", 0, 0 },
64 { HTTP_URI_OK, "file://localhost/filename%20with%20spaces",
65 "file", "", "localhost", "/filename with spaces", 0, 0 },
66 { HTTP_URI_OK, "http://server/",
67 "http", "", "server", "/", 80, 0 },
68 { HTTP_URI_OK, "http://username@server/",
69 "http", "username", "server", "/", 80, 0 },
70 { HTTP_URI_OK, "http://username:passwor%64@server/",
71 "http", "username:password", "server", "/", 80, 0 },
72 { HTTP_URI_OK, "http://username:passwor%64@server:8080/",
73 "http", "username:password", "server", "/", 8080, 8080 },
74 { HTTP_URI_OK, "http://username:passwor%64@server:8080/directory/filename",
75 "http", "username:password", "server", "/directory/filename", 8080, 8080 },
2abf387c 76 { HTTP_URI_OK, "http://[2000::10:100]:631/ipp",
77 "http", "", "2000::10:100", "/ipp", 631, 631 },
ef416fc2 78 { HTTP_URI_OK, "https://username:passwor%64@server/directory/filename",
79 "https", "username:password", "server", "/directory/filename", 443, 0 },
80 { HTTP_URI_OK, "ipp://username:passwor%64@[::1]/ipp",
81 "ipp", "username:password", "::1", "/ipp", 631, 0 },
82 { HTTP_URI_OK, "lpd://server/queue?reserve=yes",
83 "lpd", "", "server", "/queue?reserve=yes", 515, 0 },
84 { HTTP_URI_OK, "mailto:user@domain.com",
85 "mailto", "", "", "user@domain.com", 0, 0 },
86 { HTTP_URI_OK, "socket://server/",
87 "socket", "", "server", "/", 9100, 0 },
80ca4592 88 { HTTP_URI_OK, "socket://192.168.1.1:9101/",
89 "socket", "", "192.168.1.1", "/", 9101, 9101 },
ef416fc2 90 { HTTP_URI_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 },
fa73b229 92 { HTTP_URI_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 },
c9fc04c6
MS
94 { HTTP_URI_OK, "lpd://Acme%20Laser%20(01%3A23%3A45).local._tcp._printer/",
95 "lpd", "", "Acme Laser (01:23:45).local._tcp._printer", "/", 515, 0 },
ef416fc2 96
97 /* Missing scheme */
98 { HTTP_URI_MISSING_SCHEME, "/path/to/file/index.html",
99 "file", "", "", "/path/to/file/index.html", 0, 0 },
100 { HTTP_URI_MISSING_SCHEME, "//server/ipp",
101 "ipp", "", "server", "/ipp", 631, 0 },
102
103 /* Unknown scheme */
104 { HTTP_URI_UNKNOWN_SCHEME, "vendor://server/resource",
105 "vendor", "", "server", "/resource", 0, 0 },
106
107 /* Missing resource */
108 { HTTP_URI_MISSING_RESOURCE, "socket://[::192.168.2.1]",
109 "socket", "", "::192.168.2.1", "/", 9100, 0 },
d6ae789d 110 { HTTP_URI_MISSING_RESOURCE, "socket://192.168.1.1:9101",
111 "socket", "", "192.168.1.1", "/", 9101 },
ef416fc2 112
113 /* Bad URI */
114 { HTTP_URI_BAD_URI, "",
115 "", "", "", "", 0, 0 },
116
117 /* Bad scheme */
118 { HTTP_URI_BAD_SCHEME, "bad_scheme://server/resource",
119 "", "", "", "", 0, 0 },
120
121 /* Bad username */
122 { HTTP_URI_BAD_USERNAME, "http://username:passwor%6@server/resource",
123 "http", "", "", "", 80, 0 },
124
125 /* Bad hostname */
126 { HTTP_URI_BAD_HOSTNAME, "http://[/::1]/index.html",
127 "http", "", "", "", 80, 0 },
128 { HTTP_URI_BAD_HOSTNAME, "http://[",
129 "http", "", "", "", 80, 0 },
130 { HTTP_URI_BAD_HOSTNAME, "http://serve%7/index.html",
131 "http", "", "", "", 80, 0 },
132
133 /* Bad port number */
134 { HTTP_URI_BAD_PORT, "http://127.0.0.1:9999a/index.html",
135 "http", "", "127.0.0.1", "", 0, 0 },
136
137 /* Bad resource */
138 { HTTP_URI_BAD_RESOURCE, "http://server/index.html%",
139 "http", "", "server", "", 80, 0 }
140 };
141static const char * const base64_tests[][2] =
142 {
143 { "A", "QQ==" },
144 /* 010000 01 */
145 { "AB", "QUI=" },
146 /* 010000 010100 0010 */
147 { "ABC", "QUJD" },
148 /* 010000 010100 001001 000011 */
149 { "ABCD", "QUJDRA==" },
150 /* 010000 010100 001001 000011 010001 00 */
151 { "ABCDE", "QUJDREU=" },
152 /* 010000 010100 001001 000011 010001 000100 0101 */
153 { "ABCDEF", "QUJDREVG" },
154 /* 010000 010100 001001 000011 010001 000100 010101 000110 */
155 };
156
157
158/*
159 * 'main()' - Main entry.
160 */
161
162int /* O - Exit status */
163main(int argc, /* I - Number of command-line arguments */
164 char *argv[]) /* I - Command-line arguments */
165{
166 int i, j, k; /* Looping vars */
167 http_t *http; /* HTTP connection */
168 http_status_t status; /* Status of GET command */
169 int failures; /* Number of test failures */
170 char buffer[8192]; /* Input buffer */
171 long bytes; /* Number of bytes read */
172 FILE *out; /* Output file */
173 char encode[256], /* Base64-encoded string */
174 decode[256]; /* Base64-decoded string */
175 int decodelen; /* Length of decoded string */
176 char scheme[HTTP_MAX_URI], /* Scheme from URI */
177 hostname[HTTP_MAX_URI], /* Hostname from URI */
178 username[HTTP_MAX_URI], /* Username:password from URI */
179 resource[HTTP_MAX_URI]; /* Resource from URI */
180 int port; /* Port number from URI */
181 http_uri_status_t uri_status; /* Status of URI separation */
182 http_addrlist_t *addrlist, /* Address list */
183 *addr; /* Current address */
184 off_t length, total; /* Length and total bytes */
185 time_t start, current; /* Start and end time */
186 static const char * const uri_status_strings[] =
187 {
188 "HTTP_URI_OVERFLOW",
189 "HTTP_URI_BAD_ARGUMENTS",
190 "HTTP_URI_BAD_RESOURCE",
191 "HTTP_URI_BAD_PORT",
192 "HTTP_URI_BAD_HOSTNAME",
193 "HTTP_URI_BAD_USERNAME",
194 "HTTP_URI_BAD_SCHEME",
195 "HTTP_URI_BAD_URI",
196 "HTTP_URI_OK",
197 "HTTP_URI_MISSING_SCHEME",
198 "HTTP_URI_UNKNOWN_SCHEME",
199 "HTTP_URI_MISSING_RESOURCE"
200 };
201
202
203 /*
204 * Do API tests if we don't have a URL on the command-line...
205 */
206
207 if (argc == 1)
208 {
209 failures = 0;
210
211 /*
212 * httpGetDateString()/httpGetDateTime()
213 */
214
215 fputs("httpGetDateString()/httpGetDateTime(): ", stdout);
216
217 start = time(NULL);
218 strcpy(buffer, httpGetDateString(start));
219 current = httpGetDateTime(buffer);
220
221 i = (int)(current - start);
222 if (i < 0)
223 i = -i;
224
225 if (!i)
226 puts("PASS");
227 else
228 {
229 failures ++;
230 puts("FAIL");
231 printf(" Difference is %d seconds, %02d:%02d:%02d...\n", i, i / 3600,
232 (i / 60) % 60, i % 60);
233 printf(" httpGetDateString(%d) returned \"%s\"\n", (int)start, buffer);
234 printf(" httpGetDateTime(\"%s\") returned %d\n", buffer, (int)current);
235 printf(" httpGetDateString(%d) returned \"%s\"\n", (int)current,
236 httpGetDateString(current));
237 }
238
239 /*
240 * httpDecode64_2()/httpEncode64_2()
241 */
242
243 fputs("httpDecode64_2()/httpEncode64_2(): ", stdout);
244
245 for (i = 0, j = 0; i < (int)(sizeof(base64_tests) / sizeof(base64_tests[0])); i ++)
246 {
247 httpEncode64_2(encode, sizeof(encode), base64_tests[i][0],
7a0cbd5e 248 (int)strlen(base64_tests[i][0]));
ef416fc2 249 decodelen = (int)sizeof(decode);
250 httpDecode64_2(decode, &decodelen, base64_tests[i][1]);
251
252 if (strcmp(decode, base64_tests[i][0]))
253 {
254 failures ++;
255
256 if (j)
257 {
258 puts("FAIL");
259 j = 1;
260 }
261
262 printf(" httpDecode64_2() returned \"%s\", expected \"%s\"...\n",
263 decode, base64_tests[i][0]);
264 }
265
266 if (strcmp(encode, base64_tests[i][1]))
267 {
268 failures ++;
269
270 if (j)
271 {
272 puts("FAIL");
273 j = 1;
274 }
275
276 printf(" httpEncode64_2() returned \"%s\", expected \"%s\"...\n",
277 encode, base64_tests[i][1]);
278 }
279 }
280
281 if (!j)
282 puts("PASS");
283
284 /*
285 * httpGetHostname()
286 */
287
288 fputs("httpGetHostname(): ", stdout);
289
757d2cad 290 if (httpGetHostname(NULL, hostname, sizeof(hostname)))
ef416fc2 291 printf("PASS (%s)\n", hostname);
292 else
293 {
294 failures ++;
295 puts("FAIL");
296 }
297
298 /*
299 * httpAddrGetList()
300 */
301
75bd9771 302 printf("httpAddrGetList(%s): ", hostname);
ef416fc2 303
304 addrlist = httpAddrGetList(hostname, AF_UNSPEC, NULL);
305 if (addrlist)
306 {
ed486911 307 for (i = 0, addr = addrlist; addr; i ++, addr = addr->next)
308 {
309 char numeric[1024]; /* Numeric IP address */
310
311
312 httpAddrString(&(addr->addr), numeric, sizeof(numeric));
313 if (!strcmp(numeric, "UNKNOWN"))
314 break;
315 }
316
317 if (addr)
318 printf("FAIL (bad address for %s)\n", hostname);
319 else
320 printf("PASS (%d address(es) for %s)\n", i, hostname);
ef416fc2 321
ef416fc2 322 httpAddrFreeList(addrlist);
323 }
58dc1933
MS
324 else if (isdigit(hostname[0] & 255))
325 {
326 puts("FAIL (ignored because hostname is numeric)");
327 }
ef416fc2 328 else
329 {
330 failures ++;
331 puts("FAIL");
332 }
333
334 /*
335 * Test httpSeparateURI()...
336 */
337
338 fputs("httpSeparateURI(): ", stdout);
339 for (i = 0, j = 0; i < (int)(sizeof(uri_tests) / sizeof(uri_tests[0])); i ++)
340 {
a4d04587 341 uri_status = httpSeparateURI(HTTP_URI_CODING_MOST,
342 uri_tests[i].uri, scheme, sizeof(scheme),
ef416fc2 343 username, sizeof(username),
344 hostname, sizeof(hostname), &port,
345 resource, sizeof(resource));
346 if (uri_status != uri_tests[i].result ||
347 strcmp(scheme, uri_tests[i].scheme) ||
348 strcmp(username, uri_tests[i].username) ||
349 strcmp(hostname, uri_tests[i].hostname) ||
350 port != uri_tests[i].port ||
351 strcmp(resource, uri_tests[i].resource))
352 {
353 failures ++;
354
355 if (!j)
356 {
357 puts("FAIL");
358 j = 1;
359 }
360
361 printf(" \"%s\":\n", uri_tests[i].uri);
362
363 if (uri_status != uri_tests[i].result)
364 printf(" Returned %s instead of %s\n",
365 uri_status_strings[uri_status + 8],
366 uri_status_strings[uri_tests[i].result + 8]);
367
368 if (strcmp(scheme, uri_tests[i].scheme))
369 printf(" Scheme \"%s\" instead of \"%s\"\n",
370 scheme, uri_tests[i].scheme);
371
372 if (strcmp(username, uri_tests[i].username))
373 printf(" Username \"%s\" instead of \"%s\"\n",
374 username, uri_tests[i].username);
375
376 if (strcmp(hostname, uri_tests[i].hostname))
377 printf(" Hostname \"%s\" instead of \"%s\"\n",
378 hostname, uri_tests[i].hostname);
379
380 if (port != uri_tests[i].port)
381 printf(" Port %d instead of %d\n",
382 port, uri_tests[i].port);
383
384 if (strcmp(resource, uri_tests[i].resource))
385 printf(" Resource \"%s\" instead of \"%s\"\n",
386 resource, uri_tests[i].resource);
387 }
388 }
389
390 if (!j)
391 printf("PASS (%d URIs tested)\n",
392 (int)(sizeof(uri_tests) / sizeof(uri_tests[0])));
393
394 /*
395 * Test httpAssembleURI()...
396 */
397
398 fputs("httpAssembleURI(): ", stdout);
399 for (i = 0, j = 0, k = 0;
400 i < (int)(sizeof(uri_tests) / sizeof(uri_tests[0]));
401 i ++)
402 if (uri_tests[i].result == HTTP_URI_OK &&
403 !strstr(uri_tests[i].uri, "%64") &&
404 strstr(uri_tests[i].uri, "//"))
405 {
406 k ++;
a4d04587 407 uri_status = httpAssembleURI(HTTP_URI_CODING_MOST,
408 buffer, sizeof(buffer),
ef416fc2 409 uri_tests[i].scheme,
410 uri_tests[i].username,
411 uri_tests[i].hostname,
412 uri_tests[i].assemble_port,
413 uri_tests[i].resource);
414
415 if (uri_status != HTTP_URI_OK)
416 {
417 failures ++;
418
419 if (!j)
420 {
421 puts("FAIL");
422 j = 1;
423 }
424
425 printf(" \"%s\": %s\n", uri_tests[i].uri,
426 uri_status_strings[uri_status + 8]);
427 }
428 else if (strcmp(buffer, uri_tests[i].uri))
429 {
430 failures ++;
431
432 if (!j)
433 {
434 puts("FAIL");
435 j = 1;
436 }
437
438 printf(" \"%s\": assembled = \"%s\"\n", uri_tests[i].uri,
439 buffer);
440 }
441 }
442
443 if (!j)
444 printf("PASS (%d URIs tested)\n", k);
445
446 /*
447 * Show a summary and return...
448 */
449
450 if (failures)
451 printf("\n%d TESTS FAILED!\n", failures);
452 else
453 puts("\nALL TESTS PASSED!");
454
455 return (failures);
456 }
1f0275e3
MS
457 else if (strstr(argv[1], "._tcp"))
458 {
459 /*
460 * Test resolving an mDNS name.
461 */
462
463 char resolved[1024]; /* Resolved URI */
464
465
466 printf("_httpResolveURI(%s): ", argv[1]);
467 fflush(stdout);
468
38e73f87 469 if (!_httpResolveURI(argv[1], resolved, sizeof(resolved), 1))
1f0275e3
MS
470 {
471 puts("FAIL");
472 return (1);
473 }
474 else
475 {
476 printf("PASS (%s)\n", resolved);
477 return (0);
478 }
479 }
dfd5680b
MS
480 else if (!strcmp(argv[1], "-u") && argc == 3)
481 {
482 /*
483 * Test URI separation...
484 */
485
486 uri_status = httpSeparateURI(HTTP_URI_CODING_ALL, argv[2], scheme,
487 sizeof(scheme), username, sizeof(username),
488 hostname, sizeof(hostname), &port,
489 resource, sizeof(resource));
490 printf("uri_status = %s\n", uri_status_strings[uri_status + 8]);
491 printf("scheme = \"%s\"\n", scheme);
492 printf("username = \"%s\"\n", username);
493 printf("hostname = \"%s\"\n", hostname);
494 printf("port = %d\n", port);
495 printf("resource = \"%s\"\n", resource);
496
497 return (0);
498 }
ef416fc2 499
500 /*
501 * Test HTTP GET requests...
502 */
503
504 http = NULL;
505 out = stdout;
506
507 for (i = 1; i < argc; i ++)
508 {
509 if (!strcmp(argv[i], "-o"))
510 {
511 i ++;
512 if (i >= argc)
513 break;
514
515 out = fopen(argv[i], "wb");
516 continue;
517 }
518
a4d04587 519 httpSeparateURI(HTTP_URI_CODING_MOST, argv[i], scheme, sizeof(scheme),
520 username, sizeof(username),
ef416fc2 521 hostname, sizeof(hostname), &port,
522 resource, sizeof(resource));
523
524 http = httpConnectEncrypt(hostname, port, HTTP_ENCRYPT_IF_REQUESTED);
525 if (http == NULL)
526 {
527 perror(hostname);
528 continue;
529 }
530 printf("Requesting file \"%s\"...\n", resource);
531 httpClearFields(http);
532 httpSetField(http, HTTP_FIELD_ACCEPT_LANGUAGE, "en");
533 httpGet(http, resource);
534 while ((status = httpUpdate(http)) == HTTP_CONTINUE);
535
536 if (status == HTTP_OK)
537 puts("GET OK:");
538 else
539 printf("GET failed with status %d...\n", status);
540
541
542 start = time(NULL);
543 length = httpGetLength2(http);
544 total = 0;
545
a4d04587 546 while ((bytes = httpRead2(http, buffer, sizeof(buffer))) > 0)
ef416fc2 547 {
548 total += bytes;
549 fwrite(buffer, bytes, 1, out);
550 if (out != stdout)
551 {
552 current = time(NULL);
553 if (current == start) current ++;
554 printf("\r" CUPS_LLFMT "/" CUPS_LLFMT " bytes ("
e1d6a774 555 CUPS_LLFMT " bytes/sec) ", CUPS_LLCAST total,
556 CUPS_LLCAST length, CUPS_LLCAST (total / (current - start)));
ef416fc2 557 fflush(stdout);
558 }
559 }
560 }
561
562 puts("Closing connection to server...");
563 httpClose(http);
564
565 if (out != stdout)
566 fclose(out);
567
568 return (0);
569}
570
571
572/*
b19ccc9e 573 * End of "$Id: testhttp.c 7742 2008-07-15 20:23:09Z mike $".
ef416fc2 574 */