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