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