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