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