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