2 * "$Id: testhttp.c 7742 2008-07-15 20:23:09Z mike $"
4 * HTTP test program for CUPS.
6 * Copyright 2007-2010 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
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/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * main() - Main entry.
23 * Include necessary headers...
26 #include "string-private.h"
27 #include "http-private.h"
32 * Types and structures...
35 typedef struct uri_test_s
/**** URI test cases ****/
37 http_uri_status_t result
; /* Expected return value */
38 const char *uri
, /* URI */
39 *scheme
, /* Scheme string */
40 *username
, /* Username:password string */
41 *hostname
, /* Hostname string */
42 *resource
; /* Resource string */
43 int port
, /* Port number */
44 assemble_port
; /* Port number for httpAssembleURI() */
52 static uri_test_t uri_tests
[] = /* URI test data */
54 /* Start with valid URIs */
55 { HTTP_URI_OK
, "file:/filename",
56 "file", "", "", "/filename", 0, 0 },
57 { HTTP_URI_OK
, "file:/filename%20with%20spaces",
58 "file", "", "", "/filename with spaces", 0, 0 },
59 { HTTP_URI_OK
, "file:///filename",
60 "file", "", "", "/filename", 0, 0 },
61 { HTTP_URI_OK
, "file:///filename%20with%20spaces",
62 "file", "", "", "/filename with spaces", 0, 0 },
63 { HTTP_URI_OK
, "file://localhost/filename",
64 "file", "", "localhost", "/filename", 0, 0 },
65 { HTTP_URI_OK
, "file://localhost/filename%20with%20spaces",
66 "file", "", "localhost", "/filename with spaces", 0, 0 },
67 { HTTP_URI_OK
, "http://server/",
68 "http", "", "server", "/", 80, 0 },
69 { HTTP_URI_OK
, "http://username@server/",
70 "http", "username", "server", "/", 80, 0 },
71 { HTTP_URI_OK
, "http://username:passwor%64@server/",
72 "http", "username:password", "server", "/", 80, 0 },
73 { HTTP_URI_OK
, "http://username:passwor%64@server:8080/",
74 "http", "username:password", "server", "/", 8080, 8080 },
75 { HTTP_URI_OK
, "http://username:passwor%64@server:8080/directory/filename",
76 "http", "username:password", "server", "/directory/filename", 8080, 8080 },
77 { HTTP_URI_OK
, "http://[2000::10:100]:631/ipp",
78 "http", "", "2000::10:100", "/ipp", 631, 631 },
79 { HTTP_URI_OK
, "https://username:passwor%64@server/directory/filename",
80 "https", "username:password", "server", "/directory/filename", 443, 0 },
81 { HTTP_URI_OK
, "ipp://username:passwor%64@[::1]/ipp",
82 "ipp", "username:password", "::1", "/ipp", 631, 0 },
83 { HTTP_URI_OK
, "lpd://server/queue?reserve=yes",
84 "lpd", "", "server", "/queue?reserve=yes", 515, 0 },
85 { HTTP_URI_OK
, "mailto:user@domain.com",
86 "mailto", "", "", "user@domain.com", 0, 0 },
87 { HTTP_URI_OK
, "socket://server/",
88 "socket", "", "server", "/", 9100, 0 },
89 { HTTP_URI_OK
, "socket://192.168.1.1:9101/",
90 "socket", "", "192.168.1.1", "/", 9101, 9101 },
91 { HTTP_URI_OK
, "ipp://username:password@[v1.fe80::200:1234:5678:9abc+eth0]:999/ipp",
92 "ipp", "username:password", "fe80::200:1234:5678:9abc%eth0", "/ipp", 999, 999 },
93 { HTTP_URI_OK
, "http://server/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400",
94 "http", "", "server", "/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400", 80, 0 },
95 { HTTP_URI_OK
, "lpd://Acme%20Laser%20(01%3A23%3A45).local._tcp._printer/",
96 "lpd", "", "Acme Laser (01:23:45).local._tcp._printer", "/", 515, 0 },
99 { HTTP_URI_MISSING_SCHEME
, "/path/to/file/index.html",
100 "file", "", "", "/path/to/file/index.html", 0, 0 },
101 { HTTP_URI_MISSING_SCHEME
, "//server/ipp",
102 "ipp", "", "server", "/ipp", 631, 0 },
105 { HTTP_URI_UNKNOWN_SCHEME
, "vendor://server/resource",
106 "vendor", "", "server", "/resource", 0, 0 },
108 /* Missing resource */
109 { HTTP_URI_MISSING_RESOURCE
, "socket://[::192.168.2.1]",
110 "socket", "", "::192.168.2.1", "/", 9100, 0 },
111 { HTTP_URI_MISSING_RESOURCE
, "socket://192.168.1.1:9101",
112 "socket", "", "192.168.1.1", "/", 9101 },
115 { HTTP_URI_BAD_URI
, "",
116 "", "", "", "", 0, 0 },
119 { HTTP_URI_BAD_SCHEME
, "bad_scheme://server/resource",
120 "", "", "", "", 0, 0 },
123 { HTTP_URI_BAD_USERNAME
, "http://username:passwor%6@server/resource",
124 "http", "", "", "", 80, 0 },
127 { HTTP_URI_BAD_HOSTNAME
, "http://[/::1]/index.html",
128 "http", "", "", "", 80, 0 },
129 { HTTP_URI_BAD_HOSTNAME
, "http://[",
130 "http", "", "", "", 80, 0 },
131 { HTTP_URI_BAD_HOSTNAME
, "http://serve%7/index.html",
132 "http", "", "", "", 80, 0 },
134 /* Bad port number */
135 { HTTP_URI_BAD_PORT
, "http://127.0.0.1:9999a/index.html",
136 "http", "", "127.0.0.1", "", 0, 0 },
139 { HTTP_URI_BAD_RESOURCE
, "http://server/index.html%",
140 "http", "", "server", "", 80, 0 }
142 static const char * const base64_tests
[][2] =
147 /* 010000 010100 0010 */
149 /* 010000 010100 001001 000011 */
150 { "ABCD", "QUJDRA==" },
151 /* 010000 010100 001001 000011 010001 00 */
152 { "ABCDE", "QUJDREU=" },
153 /* 010000 010100 001001 000011 010001 000100 0101 */
154 { "ABCDEF", "QUJDREVG" },
155 /* 010000 010100 001001 000011 010001 000100 010101 000110 */
160 * 'main()' - Main entry.
163 int /* O - Exit status */
164 main(int argc
, /* I - Number of command-line arguments */
165 char *argv
[]) /* I - Command-line arguments */
167 int i
, j
, k
; /* Looping vars */
168 http_t
*http
; /* HTTP connection */
169 http_status_t status
; /* Status of GET command */
170 int failures
; /* Number of test failures */
171 char buffer
[8192]; /* Input buffer */
172 long bytes
; /* Number of bytes read */
173 FILE *out
; /* Output file */
174 char encode
[256], /* Base64-encoded string */
175 decode
[256]; /* Base64-decoded string */
176 int decodelen
; /* Length of decoded string */
177 char scheme
[HTTP_MAX_URI
], /* Scheme from URI */
178 hostname
[HTTP_MAX_URI
], /* Hostname from URI */
179 username
[HTTP_MAX_URI
], /* Username:password from URI */
180 resource
[HTTP_MAX_URI
]; /* Resource from URI */
181 int port
; /* Port number from URI */
182 http_uri_status_t uri_status
; /* Status of URI separation */
183 http_addrlist_t
*addrlist
, /* Address list */
184 *addr
; /* Current address */
185 off_t length
, total
; /* Length and total bytes */
186 time_t start
, current
; /* Start and end time */
187 static const char * const uri_status_strings
[] =
190 "HTTP_URI_BAD_ARGUMENTS",
191 "HTTP_URI_BAD_RESOURCE",
193 "HTTP_URI_BAD_HOSTNAME",
194 "HTTP_URI_BAD_USERNAME",
195 "HTTP_URI_BAD_SCHEME",
198 "HTTP_URI_MISSING_SCHEME",
199 "HTTP_URI_UNKNOWN_SCHEME",
200 "HTTP_URI_MISSING_RESOURCE"
205 * Do API tests if we don't have a URL on the command-line...
213 * httpGetDateString()/httpGetDateTime()
216 fputs("httpGetDateString()/httpGetDateTime(): ", stdout
);
219 strcpy(buffer
, httpGetDateString(start
));
220 current
= httpGetDateTime(buffer
);
222 i
= (int)(current
- start
);
232 printf(" Difference is %d seconds, %02d:%02d:%02d...\n", i
, i
/ 3600,
233 (i
/ 60) % 60, i
% 60);
234 printf(" httpGetDateString(%d) returned \"%s\"\n", (int)start
, buffer
);
235 printf(" httpGetDateTime(\"%s\") returned %d\n", buffer
, (int)current
);
236 printf(" httpGetDateString(%d) returned \"%s\"\n", (int)current
,
237 httpGetDateString(current
));
241 * httpDecode64_2()/httpEncode64_2()
244 fputs("httpDecode64_2()/httpEncode64_2(): ", stdout
);
246 for (i
= 0, j
= 0; i
< (int)(sizeof(base64_tests
) / sizeof(base64_tests
[0])); i
++)
248 httpEncode64_2(encode
, sizeof(encode
), base64_tests
[i
][0],
249 (int)strlen(base64_tests
[i
][0]));
250 decodelen
= (int)sizeof(decode
);
251 httpDecode64_2(decode
, &decodelen
, base64_tests
[i
][1]);
253 if (strcmp(decode
, base64_tests
[i
][0]))
263 printf(" httpDecode64_2() returned \"%s\", expected \"%s\"...\n",
264 decode
, base64_tests
[i
][0]);
267 if (strcmp(encode
, base64_tests
[i
][1]))
277 printf(" httpEncode64_2() returned \"%s\", expected \"%s\"...\n",
278 encode
, base64_tests
[i
][1]);
289 fputs("httpGetHostname(): ", stdout
);
291 if (httpGetHostname(NULL
, hostname
, sizeof(hostname
)))
292 printf("PASS (%s)\n", hostname
);
303 printf("httpAddrGetList(%s): ", hostname
);
305 addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, NULL
);
308 for (i
= 0, addr
= addrlist
; addr
; i
++, addr
= addr
->next
)
310 char numeric
[1024]; /* Numeric IP address */
313 httpAddrString(&(addr
->addr
), numeric
, sizeof(numeric
));
314 if (!strcmp(numeric
, "UNKNOWN"))
319 printf("FAIL (bad address for %s)\n", hostname
);
321 printf("PASS (%d address(es) for %s)\n", i
, hostname
);
323 httpAddrFreeList(addrlist
);
325 else if (isdigit(hostname
[0] & 255))
327 puts("FAIL (ignored because hostname is numeric)");
336 * Test httpSeparateURI()...
339 fputs("httpSeparateURI(): ", stdout
);
340 for (i
= 0, j
= 0; i
< (int)(sizeof(uri_tests
) / sizeof(uri_tests
[0])); i
++)
342 uri_status
= httpSeparateURI(HTTP_URI_CODING_MOST
,
343 uri_tests
[i
].uri
, scheme
, sizeof(scheme
),
344 username
, sizeof(username
),
345 hostname
, sizeof(hostname
), &port
,
346 resource
, sizeof(resource
));
347 if (uri_status
!= uri_tests
[i
].result
||
348 strcmp(scheme
, uri_tests
[i
].scheme
) ||
349 strcmp(username
, uri_tests
[i
].username
) ||
350 strcmp(hostname
, uri_tests
[i
].hostname
) ||
351 port
!= uri_tests
[i
].port
||
352 strcmp(resource
, uri_tests
[i
].resource
))
362 printf(" \"%s\":\n", uri_tests
[i
].uri
);
364 if (uri_status
!= uri_tests
[i
].result
)
365 printf(" Returned %s instead of %s\n",
366 uri_status_strings
[uri_status
+ 8],
367 uri_status_strings
[uri_tests
[i
].result
+ 8]);
369 if (strcmp(scheme
, uri_tests
[i
].scheme
))
370 printf(" Scheme \"%s\" instead of \"%s\"\n",
371 scheme
, uri_tests
[i
].scheme
);
373 if (strcmp(username
, uri_tests
[i
].username
))
374 printf(" Username \"%s\" instead of \"%s\"\n",
375 username
, uri_tests
[i
].username
);
377 if (strcmp(hostname
, uri_tests
[i
].hostname
))
378 printf(" Hostname \"%s\" instead of \"%s\"\n",
379 hostname
, uri_tests
[i
].hostname
);
381 if (port
!= uri_tests
[i
].port
)
382 printf(" Port %d instead of %d\n",
383 port
, uri_tests
[i
].port
);
385 if (strcmp(resource
, uri_tests
[i
].resource
))
386 printf(" Resource \"%s\" instead of \"%s\"\n",
387 resource
, uri_tests
[i
].resource
);
392 printf("PASS (%d URIs tested)\n",
393 (int)(sizeof(uri_tests
) / sizeof(uri_tests
[0])));
396 * Test httpAssembleURI()...
399 fputs("httpAssembleURI(): ", stdout
);
400 for (i
= 0, j
= 0, k
= 0;
401 i
< (int)(sizeof(uri_tests
) / sizeof(uri_tests
[0]));
403 if (uri_tests
[i
].result
== HTTP_URI_OK
&&
404 !strstr(uri_tests
[i
].uri
, "%64") &&
405 strstr(uri_tests
[i
].uri
, "//"))
408 uri_status
= httpAssembleURI(HTTP_URI_CODING_MOST
,
409 buffer
, sizeof(buffer
),
411 uri_tests
[i
].username
,
412 uri_tests
[i
].hostname
,
413 uri_tests
[i
].assemble_port
,
414 uri_tests
[i
].resource
);
416 if (uri_status
!= HTTP_URI_OK
)
426 printf(" \"%s\": %s\n", uri_tests
[i
].uri
,
427 uri_status_strings
[uri_status
+ 8]);
429 else if (strcmp(buffer
, uri_tests
[i
].uri
))
439 printf(" \"%s\": assembled = \"%s\"\n", uri_tests
[i
].uri
,
445 printf("PASS (%d URIs tested)\n", k
);
448 * Show a summary and return...
452 printf("\n%d TESTS FAILED!\n", failures
);
454 puts("\nALL TESTS PASSED!");
458 else if (strstr(argv
[1], "._tcp"))
461 * Test resolving an mDNS name.
464 char resolved
[1024]; /* Resolved URI */
467 printf("_httpResolveURI(%s): ", argv
[1]);
470 if (!_httpResolveURI(argv
[1], resolved
, sizeof(resolved
), 1))
477 printf("PASS (%s)\n", resolved
);
481 else if (!strcmp(argv
[1], "-u") && argc
== 3)
484 * Test URI separation...
487 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
, argv
[2], scheme
,
488 sizeof(scheme
), username
, sizeof(username
),
489 hostname
, sizeof(hostname
), &port
,
490 resource
, sizeof(resource
));
491 printf("uri_status = %s\n", uri_status_strings
[uri_status
+ 8]);
492 printf("scheme = \"%s\"\n", scheme
);
493 printf("username = \"%s\"\n", username
);
494 printf("hostname = \"%s\"\n", hostname
);
495 printf("port = %d\n", port
);
496 printf("resource = \"%s\"\n", resource
);
502 * Test HTTP GET requests...
508 for (i
= 1; i
< argc
; i
++)
510 if (!strcmp(argv
[i
], "-o"))
516 out
= fopen(argv
[i
], "wb");
520 httpSeparateURI(HTTP_URI_CODING_MOST
, argv
[i
], scheme
, sizeof(scheme
),
521 username
, sizeof(username
),
522 hostname
, sizeof(hostname
), &port
,
523 resource
, sizeof(resource
));
525 http
= httpConnectEncrypt(hostname
, port
, HTTP_ENCRYPT_IF_REQUESTED
);
531 printf("Requesting file \"%s\"...\n", resource
);
532 httpClearFields(http
);
533 httpSetField(http
, HTTP_FIELD_ACCEPT_LANGUAGE
, "en");
534 httpGet(http
, resource
);
535 while ((status
= httpUpdate(http
)) == HTTP_CONTINUE
);
537 if (status
== HTTP_OK
)
540 printf("GET failed with status %d...\n", status
);
544 length
= httpGetLength2(http
);
547 while ((bytes
= httpRead2(http
, buffer
, sizeof(buffer
))) > 0)
550 fwrite(buffer
, bytes
, 1, out
);
553 current
= time(NULL
);
554 if (current
== start
) current
++;
555 printf("\r" CUPS_LLFMT
"/" CUPS_LLFMT
" bytes ("
556 CUPS_LLFMT
" bytes/sec) ", CUPS_LLCAST total
,
557 CUPS_LLCAST length
, CUPS_LLCAST (total
/ (current
- start
)));
563 puts("Closing connection to server...");
574 * End of "$Id: testhttp.c 7742 2008-07-15 20:23:09Z mike $".