]>
Commit | Line | Data |
---|---|---|
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 | ||
36 | typedef 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 | ||
53 | static 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 }, | |
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 | }; | |
141 | static 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 | ||
162 | int /* O - Exit status */ | |
163 | main(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], | |
248 | strlen(base64_tests[i][0])); | |
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 | ||
302 | fputs("httpAddrGetList(): ", stdout); | |
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 | } | |
324 | else | |
325 | { | |
326 | failures ++; | |
327 | puts("FAIL"); | |
328 | } | |
329 | ||
330 | /* | |
331 | * Test httpSeparateURI()... | |
332 | */ | |
333 | ||
334 | fputs("httpSeparateURI(): ", stdout); | |
335 | for (i = 0, j = 0; i < (int)(sizeof(uri_tests) / sizeof(uri_tests[0])); i ++) | |
336 | { | |
a4d04587 | 337 | uri_status = httpSeparateURI(HTTP_URI_CODING_MOST, |
338 | uri_tests[i].uri, scheme, sizeof(scheme), | |
ef416fc2 | 339 | username, sizeof(username), |
340 | hostname, sizeof(hostname), &port, | |
341 | resource, sizeof(resource)); | |
342 | if (uri_status != uri_tests[i].result || | |
343 | strcmp(scheme, uri_tests[i].scheme) || | |
344 | strcmp(username, uri_tests[i].username) || | |
345 | strcmp(hostname, uri_tests[i].hostname) || | |
346 | port != uri_tests[i].port || | |
347 | strcmp(resource, uri_tests[i].resource)) | |
348 | { | |
349 | failures ++; | |
350 | ||
351 | if (!j) | |
352 | { | |
353 | puts("FAIL"); | |
354 | j = 1; | |
355 | } | |
356 | ||
357 | printf(" \"%s\":\n", uri_tests[i].uri); | |
358 | ||
359 | if (uri_status != uri_tests[i].result) | |
360 | printf(" Returned %s instead of %s\n", | |
361 | uri_status_strings[uri_status + 8], | |
362 | uri_status_strings[uri_tests[i].result + 8]); | |
363 | ||
364 | if (strcmp(scheme, uri_tests[i].scheme)) | |
365 | printf(" Scheme \"%s\" instead of \"%s\"\n", | |
366 | scheme, uri_tests[i].scheme); | |
367 | ||
368 | if (strcmp(username, uri_tests[i].username)) | |
369 | printf(" Username \"%s\" instead of \"%s\"\n", | |
370 | username, uri_tests[i].username); | |
371 | ||
372 | if (strcmp(hostname, uri_tests[i].hostname)) | |
373 | printf(" Hostname \"%s\" instead of \"%s\"\n", | |
374 | hostname, uri_tests[i].hostname); | |
375 | ||
376 | if (port != uri_tests[i].port) | |
377 | printf(" Port %d instead of %d\n", | |
378 | port, uri_tests[i].port); | |
379 | ||
380 | if (strcmp(resource, uri_tests[i].resource)) | |
381 | printf(" Resource \"%s\" instead of \"%s\"\n", | |
382 | resource, uri_tests[i].resource); | |
383 | } | |
384 | } | |
385 | ||
386 | if (!j) | |
387 | printf("PASS (%d URIs tested)\n", | |
388 | (int)(sizeof(uri_tests) / sizeof(uri_tests[0]))); | |
389 | ||
390 | /* | |
391 | * Test httpAssembleURI()... | |
392 | */ | |
393 | ||
394 | fputs("httpAssembleURI(): ", stdout); | |
395 | for (i = 0, j = 0, k = 0; | |
396 | i < (int)(sizeof(uri_tests) / sizeof(uri_tests[0])); | |
397 | i ++) | |
398 | if (uri_tests[i].result == HTTP_URI_OK && | |
399 | !strstr(uri_tests[i].uri, "%64") && | |
400 | strstr(uri_tests[i].uri, "//")) | |
401 | { | |
402 | k ++; | |
a4d04587 | 403 | uri_status = httpAssembleURI(HTTP_URI_CODING_MOST, |
404 | buffer, sizeof(buffer), | |
ef416fc2 | 405 | uri_tests[i].scheme, |
406 | uri_tests[i].username, | |
407 | uri_tests[i].hostname, | |
408 | uri_tests[i].assemble_port, | |
409 | uri_tests[i].resource); | |
410 | ||
411 | if (uri_status != HTTP_URI_OK) | |
412 | { | |
413 | failures ++; | |
414 | ||
415 | if (!j) | |
416 | { | |
417 | puts("FAIL"); | |
418 | j = 1; | |
419 | } | |
420 | ||
421 | printf(" \"%s\": %s\n", uri_tests[i].uri, | |
422 | uri_status_strings[uri_status + 8]); | |
423 | } | |
424 | else if (strcmp(buffer, uri_tests[i].uri)) | |
425 | { | |
426 | failures ++; | |
427 | ||
428 | if (!j) | |
429 | { | |
430 | puts("FAIL"); | |
431 | j = 1; | |
432 | } | |
433 | ||
434 | printf(" \"%s\": assembled = \"%s\"\n", uri_tests[i].uri, | |
435 | buffer); | |
436 | } | |
437 | } | |
438 | ||
439 | if (!j) | |
440 | printf("PASS (%d URIs tested)\n", k); | |
441 | ||
442 | /* | |
443 | * Show a summary and return... | |
444 | */ | |
445 | ||
446 | if (failures) | |
447 | printf("\n%d TESTS FAILED!\n", failures); | |
448 | else | |
449 | puts("\nALL TESTS PASSED!"); | |
450 | ||
451 | return (failures); | |
452 | } | |
453 | ||
454 | /* | |
455 | * Test HTTP GET requests... | |
456 | */ | |
457 | ||
458 | http = NULL; | |
459 | out = stdout; | |
460 | ||
461 | for (i = 1; i < argc; i ++) | |
462 | { | |
463 | if (!strcmp(argv[i], "-o")) | |
464 | { | |
465 | i ++; | |
466 | if (i >= argc) | |
467 | break; | |
468 | ||
469 | out = fopen(argv[i], "wb"); | |
470 | continue; | |
471 | } | |
472 | ||
a4d04587 | 473 | httpSeparateURI(HTTP_URI_CODING_MOST, argv[i], scheme, sizeof(scheme), |
474 | username, sizeof(username), | |
ef416fc2 | 475 | hostname, sizeof(hostname), &port, |
476 | resource, sizeof(resource)); | |
477 | ||
478 | http = httpConnectEncrypt(hostname, port, HTTP_ENCRYPT_IF_REQUESTED); | |
479 | if (http == NULL) | |
480 | { | |
481 | perror(hostname); | |
482 | continue; | |
483 | } | |
484 | printf("Requesting file \"%s\"...\n", resource); | |
485 | httpClearFields(http); | |
486 | httpSetField(http, HTTP_FIELD_ACCEPT_LANGUAGE, "en"); | |
487 | httpGet(http, resource); | |
488 | while ((status = httpUpdate(http)) == HTTP_CONTINUE); | |
489 | ||
490 | if (status == HTTP_OK) | |
491 | puts("GET OK:"); | |
492 | else | |
493 | printf("GET failed with status %d...\n", status); | |
494 | ||
495 | ||
496 | start = time(NULL); | |
497 | length = httpGetLength2(http); | |
498 | total = 0; | |
499 | ||
a4d04587 | 500 | while ((bytes = httpRead2(http, buffer, sizeof(buffer))) > 0) |
ef416fc2 | 501 | { |
502 | total += bytes; | |
503 | fwrite(buffer, bytes, 1, out); | |
504 | if (out != stdout) | |
505 | { | |
506 | current = time(NULL); | |
507 | if (current == start) current ++; | |
508 | printf("\r" CUPS_LLFMT "/" CUPS_LLFMT " bytes (" | |
e1d6a774 | 509 | CUPS_LLFMT " bytes/sec) ", CUPS_LLCAST total, |
510 | CUPS_LLCAST length, CUPS_LLCAST (total / (current - start))); | |
ef416fc2 | 511 | fflush(stdout); |
512 | } | |
513 | } | |
514 | } | |
515 | ||
516 | puts("Closing connection to server..."); | |
517 | httpClose(http); | |
518 | ||
519 | if (out != stdout) | |
520 | fclose(out); | |
521 | ||
522 | return (0); | |
523 | } | |
524 | ||
525 | ||
526 | /* | |
bc44d920 | 527 | * End of "$Id: testhttp.c 6649 2007-07-11 21:46:42Z mike $". |
ef416fc2 | 528 | */ |