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