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