]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
f2d18633 | 2 | * "$Id$" |
ef416fc2 | 3 | * |
f4047845 | 4 | * HTTP test program for CUPS. |
ef416fc2 | 5 | * |
7e86f2f6 | 6 | * Copyright 2007-2014 by Apple Inc. |
f4047845 | 7 | * Copyright 1997-2006 by Easy Software Products. |
ef416fc2 | 8 | * |
f4047845 MS |
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/". | |
ef416fc2 | 14 | * |
f4047845 | 15 | * This file is subject to the Apple OS-Developed Software exception. |
ef416fc2 | 16 | */ |
17 | ||
18 | /* | |
19 | * Include necessary headers... | |
20 | */ | |
21 | ||
0c4bedc4 | 22 | #include "cups-private.h" |
ef416fc2 | 23 | |
24 | ||
25 | /* | |
26 | * Types and structures... | |
27 | */ | |
28 | ||
29 | typedef struct uri_test_s /**** URI test cases ****/ | |
30 | { | |
31 | http_uri_status_t result; /* Expected return value */ | |
32 | const char *uri, /* URI */ | |
33 | *scheme, /* Scheme string */ | |
34 | *username, /* Username:password string */ | |
35 | *hostname, /* Hostname string */ | |
36 | *resource; /* Resource string */ | |
37 | int port, /* Port number */ | |
7e86f2f6 MS |
38 | assemble_port; /* Port number for httpAssembleURI() */ |
39 | http_uri_coding_t assemble_coding;/* Coding for httpAssembleURI() */ | |
ef416fc2 | 40 | } uri_test_t; |
41 | ||
42 | ||
43 | /* | |
44 | * Local globals... | |
45 | */ | |
46 | ||
47 | static uri_test_t uri_tests[] = /* URI test data */ | |
48 | { | |
49 | /* Start with valid URIs */ | |
cb7f98ee | 50 | { HTTP_URI_STATUS_OK, "file:/filename", |
86c809d9 MS |
51 | "file", "", "", "/filename", 0, 0, |
52 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 53 | { HTTP_URI_STATUS_OK, "file:/filename%20with%20spaces", |
86c809d9 MS |
54 | "file", "", "", "/filename with spaces", 0, 0, |
55 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 56 | { HTTP_URI_STATUS_OK, "file:///filename", |
86c809d9 MS |
57 | "file", "", "", "/filename", 0, 0, |
58 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 59 | { HTTP_URI_STATUS_OK, "file:///filename%20with%20spaces", |
86c809d9 MS |
60 | "file", "", "", "/filename with spaces", 0, 0, |
61 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 62 | { HTTP_URI_STATUS_OK, "file://localhost/filename", |
86c809d9 MS |
63 | "file", "", "localhost", "/filename", 0, 0, |
64 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 65 | { HTTP_URI_STATUS_OK, "file://localhost/filename%20with%20spaces", |
86c809d9 MS |
66 | "file", "", "localhost", "/filename with spaces", 0, 0, |
67 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 68 | { HTTP_URI_STATUS_OK, "http://server/", |
86c809d9 MS |
69 | "http", "", "server", "/", 80, 0, |
70 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 71 | { HTTP_URI_STATUS_OK, "http://username@server/", |
86c809d9 MS |
72 | "http", "username", "server", "/", 80, 0, |
73 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 74 | { HTTP_URI_STATUS_OK, "http://username:passwor%64@server/", |
86c809d9 MS |
75 | "http", "username:password", "server", "/", 80, 0, |
76 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 77 | { HTTP_URI_STATUS_OK, "http://username:passwor%64@server:8080/", |
86c809d9 MS |
78 | "http", "username:password", "server", "/", 8080, 8080, |
79 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 80 | { HTTP_URI_STATUS_OK, "http://username:passwor%64@server:8080/directory/filename", |
86c809d9 MS |
81 | "http", "username:password", "server", "/directory/filename", 8080, 8080, |
82 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 83 | { HTTP_URI_STATUS_OK, "http://[2000::10:100]:631/ipp", |
86c809d9 MS |
84 | "http", "", "2000::10:100", "/ipp", 631, 631, |
85 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 86 | { HTTP_URI_STATUS_OK, "https://username:passwor%64@server/directory/filename", |
86c809d9 MS |
87 | "https", "username:password", "server", "/directory/filename", 443, 0, |
88 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 89 | { HTTP_URI_STATUS_OK, "ipp://username:passwor%64@[::1]/ipp", |
86c809d9 MS |
90 | "ipp", "username:password", "::1", "/ipp", 631, 0, |
91 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 92 | { HTTP_URI_STATUS_OK, "lpd://server/queue?reserve=yes", |
86c809d9 MS |
93 | "lpd", "", "server", "/queue?reserve=yes", 515, 0, |
94 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 95 | { HTTP_URI_STATUS_OK, "mailto:user@domain.com", |
86c809d9 MS |
96 | "mailto", "", "", "user@domain.com", 0, 0, |
97 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 98 | { HTTP_URI_STATUS_OK, "socket://server/", |
86c809d9 MS |
99 | "socket", "", "server", "/", 9100, 0, |
100 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 101 | { HTTP_URI_STATUS_OK, "socket://192.168.1.1:9101/", |
86c809d9 MS |
102 | "socket", "", "192.168.1.1", "/", 9101, 9101, |
103 | HTTP_URI_CODING_MOST }, | |
f4047845 MS |
104 | { HTTP_URI_STATUS_OK, "tel:8005551212", |
105 | "tel", "", "", "8005551212", 0, 0, | |
106 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 107 | { HTTP_URI_STATUS_OK, "ipp://username:password@[v1.fe80::200:1234:5678:9abc+eth0]:999/ipp", |
86c809d9 MS |
108 | "ipp", "username:password", "fe80::200:1234:5678:9abc%eth0", "/ipp", 999, 999, |
109 | HTTP_URI_CODING_MOST }, | |
110 | { HTTP_URI_STATUS_OK, "ipp://username:password@[fe80::200:1234:5678:9abc%25eth0]:999/ipp", | |
111 | "ipp", "username:password", "fe80::200:1234:5678:9abc%eth0", "/ipp", 999, 999, | |
7e86f2f6 | 112 | (http_uri_coding_t)(HTTP_URI_CODING_MOST | HTTP_URI_CODING_RFC6874) }, |
cb7f98ee | 113 | { HTTP_URI_STATUS_OK, "http://server/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400", |
86c809d9 MS |
114 | "http", "", "server", "/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400", 80, 0, |
115 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 116 | { HTTP_URI_STATUS_OK, "lpd://Acme%20Laser%20(01%3A23%3A45).local._tcp._printer/", |
86c809d9 MS |
117 | "lpd", "", "Acme Laser (01:23:45).local._tcp._printer", "/", 515, 0, |
118 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 119 | { HTTP_URI_STATUS_OK, "ipp://HP%20Officejet%204500%20G510n-z%20%40%20Will's%20MacBook%20Pro%2015%22._ipp._tcp.local./", |
86c809d9 MS |
120 | "ipp", "", "HP Officejet 4500 G510n-z @ Will's MacBook Pro 15\"._ipp._tcp.local.", "/", 631, 0, |
121 | HTTP_URI_CODING_MOST }, | |
ff862e50 MS |
122 | { HTTP_URI_STATUS_OK, "ipp://%22%23%2F%3A%3C%3E%3F%40%5B%5C%5D%5E%60%7B%7C%7D/", |
123 | "ipp", "", "\"#/:<>?@[\\]^`{|}", "/", 631, 0, | |
124 | HTTP_URI_CODING_MOST }, | |
ef416fc2 | 125 | |
126 | /* Missing scheme */ | |
cb7f98ee | 127 | { HTTP_URI_STATUS_MISSING_SCHEME, "/path/to/file/index.html", |
86c809d9 MS |
128 | "file", "", "", "/path/to/file/index.html", 0, 0, |
129 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 130 | { HTTP_URI_STATUS_MISSING_SCHEME, "//server/ipp", |
86c809d9 MS |
131 | "ipp", "", "server", "/ipp", 631, 0, |
132 | HTTP_URI_CODING_MOST }, | |
ef416fc2 | 133 | |
134 | /* Unknown scheme */ | |
cb7f98ee | 135 | { HTTP_URI_STATUS_UNKNOWN_SCHEME, "vendor://server/resource", |
86c809d9 MS |
136 | "vendor", "", "server", "/resource", 0, 0, |
137 | HTTP_URI_CODING_MOST }, | |
ef416fc2 | 138 | |
139 | /* Missing resource */ | |
cb7f98ee | 140 | { HTTP_URI_STATUS_MISSING_RESOURCE, "socket://[::192.168.2.1]", |
86c809d9 MS |
141 | "socket", "", "::192.168.2.1", "/", 9100, 0, |
142 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 143 | { HTTP_URI_STATUS_MISSING_RESOURCE, "socket://192.168.1.1:9101", |
86c809d9 MS |
144 | "socket", "", "192.168.1.1", "/", 9101, 0, |
145 | HTTP_URI_CODING_MOST }, | |
ef416fc2 | 146 | |
147 | /* Bad URI */ | |
cb7f98ee | 148 | { HTTP_URI_STATUS_BAD_URI, "", |
86c809d9 MS |
149 | "", "", "", "", 0, 0, |
150 | HTTP_URI_CODING_MOST }, | |
ef416fc2 | 151 | |
152 | /* Bad scheme */ | |
cb7f98ee | 153 | { HTTP_URI_STATUS_BAD_SCHEME, "bad_scheme://server/resource", |
86c809d9 MS |
154 | "", "", "", "", 0, 0, |
155 | HTTP_URI_CODING_MOST }, | |
ef416fc2 | 156 | |
157 | /* Bad username */ | |
cb7f98ee | 158 | { HTTP_URI_STATUS_BAD_USERNAME, "http://username:passwor%6@server/resource", |
86c809d9 MS |
159 | "http", "", "", "", 80, 0, |
160 | HTTP_URI_CODING_MOST }, | |
ef416fc2 | 161 | |
162 | /* Bad hostname */ | |
cb7f98ee | 163 | { HTTP_URI_STATUS_BAD_HOSTNAME, "http://[/::1]/index.html", |
86c809d9 MS |
164 | "http", "", "", "", 80, 0, |
165 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 166 | { HTTP_URI_STATUS_BAD_HOSTNAME, "http://[", |
86c809d9 MS |
167 | "http", "", "", "", 80, 0, |
168 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 169 | { HTTP_URI_STATUS_BAD_HOSTNAME, "http://serve%7/index.html", |
86c809d9 MS |
170 | "http", "", "", "", 80, 0, |
171 | HTTP_URI_CODING_MOST }, | |
6961465f | 172 | { HTTP_URI_STATUS_BAD_HOSTNAME, "http://server with spaces/index.html", |
86c809d9 MS |
173 | "http", "", "", "", 80, 0, |
174 | HTTP_URI_CODING_MOST }, | |
ff862e50 MS |
175 | { HTTP_URI_STATUS_BAD_HOSTNAME, "ipp://\"#/:<>?@[\\]^`{|}/", |
176 | "ipp", "", "", "", 631, 0, | |
177 | HTTP_URI_CODING_MOST }, | |
ef416fc2 | 178 | |
179 | /* Bad port number */ | |
cb7f98ee | 180 | { HTTP_URI_STATUS_BAD_PORT, "http://127.0.0.1:9999a/index.html", |
86c809d9 MS |
181 | "http", "", "127.0.0.1", "", 0, 0, |
182 | HTTP_URI_CODING_MOST }, | |
ef416fc2 | 183 | |
184 | /* Bad resource */ | |
cb7f98ee | 185 | { HTTP_URI_STATUS_BAD_RESOURCE, "http://server/index.html%", |
86c809d9 MS |
186 | "http", "", "server", "", 80, 0, |
187 | HTTP_URI_CODING_MOST }, | |
6961465f | 188 | { HTTP_URI_STATUS_BAD_RESOURCE, "http://server/index with spaces.html", |
86c809d9 MS |
189 | "http", "", "server", "", 80, 0, |
190 | HTTP_URI_CODING_MOST } | |
ef416fc2 | 191 | }; |
192 | static const char * const base64_tests[][2] = | |
193 | { | |
194 | { "A", "QQ==" }, | |
195 | /* 010000 01 */ | |
196 | { "AB", "QUI=" }, | |
197 | /* 010000 010100 0010 */ | |
198 | { "ABC", "QUJD" }, | |
199 | /* 010000 010100 001001 000011 */ | |
200 | { "ABCD", "QUJDRA==" }, | |
201 | /* 010000 010100 001001 000011 010001 00 */ | |
202 | { "ABCDE", "QUJDREU=" }, | |
203 | /* 010000 010100 001001 000011 010001 000100 0101 */ | |
204 | { "ABCDEF", "QUJDREVG" }, | |
205 | /* 010000 010100 001001 000011 010001 000100 010101 000110 */ | |
206 | }; | |
207 | ||
208 | ||
209 | /* | |
210 | * 'main()' - Main entry. | |
211 | */ | |
212 | ||
213 | int /* O - Exit status */ | |
214 | main(int argc, /* I - Number of command-line arguments */ | |
215 | char *argv[]) /* I - Command-line arguments */ | |
216 | { | |
217 | int i, j, k; /* Looping vars */ | |
218 | http_t *http; /* HTTP connection */ | |
83e08001 | 219 | http_encryption_t encryption; /* Encryption type */ |
ef416fc2 | 220 | http_status_t status; /* Status of GET command */ |
221 | int failures; /* Number of test failures */ | |
222 | char buffer[8192]; /* Input buffer */ | |
223 | long bytes; /* Number of bytes read */ | |
224 | FILE *out; /* Output file */ | |
225 | char encode[256], /* Base64-encoded string */ | |
226 | decode[256]; /* Base64-decoded string */ | |
227 | int decodelen; /* Length of decoded string */ | |
228 | char scheme[HTTP_MAX_URI], /* Scheme from URI */ | |
229 | hostname[HTTP_MAX_URI], /* Hostname from URI */ | |
230 | username[HTTP_MAX_URI], /* Username:password from URI */ | |
231 | resource[HTTP_MAX_URI]; /* Resource from URI */ | |
232 | int port; /* Port number from URI */ | |
233 | http_uri_status_t uri_status; /* Status of URI separation */ | |
234 | http_addrlist_t *addrlist, /* Address list */ | |
235 | *addr; /* Current address */ | |
236 | off_t length, total; /* Length and total bytes */ | |
237 | time_t start, current; /* Start and end time */ | |
a469f8a5 | 238 | const char *encoding; /* Negotiated Content-Encoding */ |
ef416fc2 | 239 | static const char * const uri_status_strings[] = |
240 | { | |
cb7f98ee MS |
241 | "HTTP_URI_STATUS_OVERFLOW", |
242 | "HTTP_URI_STATUS_BAD_ARGUMENTS", | |
243 | "HTTP_URI_STATUS_BAD_RESOURCE", | |
244 | "HTTP_URI_STATUS_BAD_PORT", | |
245 | "HTTP_URI_STATUS_BAD_HOSTNAME", | |
246 | "HTTP_URI_STATUS_BAD_USERNAME", | |
247 | "HTTP_URI_STATUS_BAD_SCHEME", | |
248 | "HTTP_URI_STATUS_BAD_URI", | |
249 | "HTTP_URI_STATUS_OK", | |
250 | "HTTP_URI_STATUS_MISSING_SCHEME", | |
251 | "HTTP_URI_STATUS_UNKNOWN_SCHEME", | |
252 | "HTTP_URI_STATUS_MISSING_RESOURCE" | |
ef416fc2 | 253 | }; |
254 | ||
255 | ||
256 | /* | |
257 | * Do API tests if we don't have a URL on the command-line... | |
258 | */ | |
259 | ||
260 | if (argc == 1) | |
261 | { | |
262 | failures = 0; | |
263 | ||
264 | /* | |
265 | * httpGetDateString()/httpGetDateTime() | |
266 | */ | |
267 | ||
268 | fputs("httpGetDateString()/httpGetDateTime(): ", stdout); | |
269 | ||
270 | start = time(NULL); | |
5a9febac | 271 | strlcpy(buffer, httpGetDateString(start), sizeof(buffer)); |
ef416fc2 | 272 | current = httpGetDateTime(buffer); |
273 | ||
274 | i = (int)(current - start); | |
275 | if (i < 0) | |
276 | i = -i; | |
277 | ||
278 | if (!i) | |
279 | puts("PASS"); | |
280 | else | |
281 | { | |
282 | failures ++; | |
283 | puts("FAIL"); | |
284 | printf(" Difference is %d seconds, %02d:%02d:%02d...\n", i, i / 3600, | |
285 | (i / 60) % 60, i % 60); | |
286 | printf(" httpGetDateString(%d) returned \"%s\"\n", (int)start, buffer); | |
287 | printf(" httpGetDateTime(\"%s\") returned %d\n", buffer, (int)current); | |
288 | printf(" httpGetDateString(%d) returned \"%s\"\n", (int)current, | |
289 | httpGetDateString(current)); | |
290 | } | |
291 | ||
292 | /* | |
293 | * httpDecode64_2()/httpEncode64_2() | |
294 | */ | |
295 | ||
296 | fputs("httpDecode64_2()/httpEncode64_2(): ", stdout); | |
297 | ||
298 | for (i = 0, j = 0; i < (int)(sizeof(base64_tests) / sizeof(base64_tests[0])); i ++) | |
299 | { | |
300 | httpEncode64_2(encode, sizeof(encode), base64_tests[i][0], | |
7a0cbd5e | 301 | (int)strlen(base64_tests[i][0])); |
ef416fc2 | 302 | decodelen = (int)sizeof(decode); |
303 | httpDecode64_2(decode, &decodelen, base64_tests[i][1]); | |
304 | ||
305 | if (strcmp(decode, base64_tests[i][0])) | |
306 | { | |
307 | failures ++; | |
308 | ||
309 | if (j) | |
310 | { | |
311 | puts("FAIL"); | |
312 | j = 1; | |
313 | } | |
314 | ||
315 | printf(" httpDecode64_2() returned \"%s\", expected \"%s\"...\n", | |
316 | decode, base64_tests[i][0]); | |
317 | } | |
318 | ||
319 | if (strcmp(encode, base64_tests[i][1])) | |
320 | { | |
321 | failures ++; | |
322 | ||
323 | if (j) | |
324 | { | |
325 | puts("FAIL"); | |
326 | j = 1; | |
327 | } | |
328 | ||
329 | printf(" httpEncode64_2() returned \"%s\", expected \"%s\"...\n", | |
330 | encode, base64_tests[i][1]); | |
331 | } | |
332 | } | |
333 | ||
334 | if (!j) | |
335 | puts("PASS"); | |
336 | ||
337 | /* | |
338 | * httpGetHostname() | |
339 | */ | |
340 | ||
341 | fputs("httpGetHostname(): ", stdout); | |
342 | ||
757d2cad | 343 | if (httpGetHostname(NULL, hostname, sizeof(hostname))) |
ef416fc2 | 344 | printf("PASS (%s)\n", hostname); |
345 | else | |
346 | { | |
347 | failures ++; | |
348 | puts("FAIL"); | |
349 | } | |
350 | ||
351 | /* | |
352 | * httpAddrGetList() | |
353 | */ | |
354 | ||
75bd9771 | 355 | printf("httpAddrGetList(%s): ", hostname); |
ef416fc2 | 356 | |
357 | addrlist = httpAddrGetList(hostname, AF_UNSPEC, NULL); | |
358 | if (addrlist) | |
359 | { | |
ed486911 | 360 | for (i = 0, addr = addrlist; addr; i ++, addr = addr->next) |
361 | { | |
362 | char numeric[1024]; /* Numeric IP address */ | |
363 | ||
364 | ||
365 | httpAddrString(&(addr->addr), numeric, sizeof(numeric)); | |
366 | if (!strcmp(numeric, "UNKNOWN")) | |
367 | break; | |
368 | } | |
369 | ||
370 | if (addr) | |
371 | printf("FAIL (bad address for %s)\n", hostname); | |
372 | else | |
373 | printf("PASS (%d address(es) for %s)\n", i, hostname); | |
ef416fc2 | 374 | |
ef416fc2 | 375 | httpAddrFreeList(addrlist); |
376 | } | |
58dc1933 MS |
377 | else if (isdigit(hostname[0] & 255)) |
378 | { | |
379 | puts("FAIL (ignored because hostname is numeric)"); | |
380 | } | |
ef416fc2 | 381 | else |
382 | { | |
383 | failures ++; | |
384 | puts("FAIL"); | |
385 | } | |
386 | ||
387 | /* | |
388 | * Test httpSeparateURI()... | |
389 | */ | |
390 | ||
391 | fputs("httpSeparateURI(): ", stdout); | |
392 | for (i = 0, j = 0; i < (int)(sizeof(uri_tests) / sizeof(uri_tests[0])); i ++) | |
393 | { | |
a4d04587 | 394 | uri_status = httpSeparateURI(HTTP_URI_CODING_MOST, |
395 | uri_tests[i].uri, scheme, sizeof(scheme), | |
ef416fc2 | 396 | username, sizeof(username), |
397 | hostname, sizeof(hostname), &port, | |
398 | resource, sizeof(resource)); | |
399 | if (uri_status != uri_tests[i].result || | |
400 | strcmp(scheme, uri_tests[i].scheme) || | |
401 | strcmp(username, uri_tests[i].username) || | |
402 | strcmp(hostname, uri_tests[i].hostname) || | |
403 | port != uri_tests[i].port || | |
404 | strcmp(resource, uri_tests[i].resource)) | |
405 | { | |
406 | failures ++; | |
407 | ||
408 | if (!j) | |
409 | { | |
410 | puts("FAIL"); | |
411 | j = 1; | |
412 | } | |
413 | ||
414 | printf(" \"%s\":\n", uri_tests[i].uri); | |
415 | ||
416 | if (uri_status != uri_tests[i].result) | |
417 | printf(" Returned %s instead of %s\n", | |
418 | uri_status_strings[uri_status + 8], | |
419 | uri_status_strings[uri_tests[i].result + 8]); | |
420 | ||
421 | if (strcmp(scheme, uri_tests[i].scheme)) | |
422 | printf(" Scheme \"%s\" instead of \"%s\"\n", | |
423 | scheme, uri_tests[i].scheme); | |
424 | ||
425 | if (strcmp(username, uri_tests[i].username)) | |
426 | printf(" Username \"%s\" instead of \"%s\"\n", | |
427 | username, uri_tests[i].username); | |
428 | ||
429 | if (strcmp(hostname, uri_tests[i].hostname)) | |
430 | printf(" Hostname \"%s\" instead of \"%s\"\n", | |
431 | hostname, uri_tests[i].hostname); | |
432 | ||
433 | if (port != uri_tests[i].port) | |
434 | printf(" Port %d instead of %d\n", | |
435 | port, uri_tests[i].port); | |
436 | ||
437 | if (strcmp(resource, uri_tests[i].resource)) | |
438 | printf(" Resource \"%s\" instead of \"%s\"\n", | |
439 | resource, uri_tests[i].resource); | |
440 | } | |
441 | } | |
442 | ||
443 | if (!j) | |
444 | printf("PASS (%d URIs tested)\n", | |
445 | (int)(sizeof(uri_tests) / sizeof(uri_tests[0]))); | |
446 | ||
447 | /* | |
448 | * Test httpAssembleURI()... | |
449 | */ | |
450 | ||
451 | fputs("httpAssembleURI(): ", stdout); | |
452 | for (i = 0, j = 0, k = 0; | |
453 | i < (int)(sizeof(uri_tests) / sizeof(uri_tests[0])); | |
454 | i ++) | |
cb7f98ee | 455 | if (uri_tests[i].result == HTTP_URI_STATUS_OK && |
ef416fc2 | 456 | !strstr(uri_tests[i].uri, "%64") && |
457 | strstr(uri_tests[i].uri, "//")) | |
458 | { | |
459 | k ++; | |
86c809d9 | 460 | uri_status = httpAssembleURI(uri_tests[i].assemble_coding, |
a4d04587 | 461 | buffer, sizeof(buffer), |
ef416fc2 | 462 | uri_tests[i].scheme, |
463 | uri_tests[i].username, | |
464 | uri_tests[i].hostname, | |
465 | uri_tests[i].assemble_port, | |
466 | uri_tests[i].resource); | |
467 | ||
cb7f98ee | 468 | if (uri_status != HTTP_URI_STATUS_OK) |
ef416fc2 | 469 | { |
470 | failures ++; | |
471 | ||
472 | if (!j) | |
473 | { | |
474 | puts("FAIL"); | |
475 | j = 1; | |
476 | } | |
477 | ||
478 | printf(" \"%s\": %s\n", uri_tests[i].uri, | |
479 | uri_status_strings[uri_status + 8]); | |
480 | } | |
481 | else if (strcmp(buffer, uri_tests[i].uri)) | |
482 | { | |
483 | failures ++; | |
484 | ||
485 | if (!j) | |
486 | { | |
487 | puts("FAIL"); | |
488 | j = 1; | |
489 | } | |
490 | ||
491 | printf(" \"%s\": assembled = \"%s\"\n", uri_tests[i].uri, | |
492 | buffer); | |
493 | } | |
494 | } | |
495 | ||
496 | if (!j) | |
497 | printf("PASS (%d URIs tested)\n", k); | |
498 | ||
71f294ce MS |
499 | /* |
500 | * httpAssembleUUID | |
501 | */ | |
502 | ||
503 | fputs("httpAssembleUUID: ", stdout); | |
504 | httpAssembleUUID("hostname.example.com", 631, "printer", 12345, buffer, | |
505 | sizeof(buffer)); | |
506 | if (strncmp(buffer, "urn:uuid:", 9)) | |
507 | { | |
508 | printf("FAIL (%s)\n", buffer); | |
509 | failures ++; | |
510 | } | |
511 | else | |
512 | printf("PASS (%s)\n", buffer); | |
513 | ||
ef416fc2 | 514 | /* |
515 | * Show a summary and return... | |
516 | */ | |
517 | ||
518 | if (failures) | |
519 | printf("\n%d TESTS FAILED!\n", failures); | |
520 | else | |
521 | puts("\nALL TESTS PASSED!"); | |
522 | ||
523 | return (failures); | |
524 | } | |
1f0275e3 MS |
525 | else if (strstr(argv[1], "._tcp")) |
526 | { | |
527 | /* | |
528 | * Test resolving an mDNS name. | |
529 | */ | |
530 | ||
531 | char resolved[1024]; /* Resolved URI */ | |
532 | ||
533 | ||
eac3a0a0 | 534 | printf("_httpResolveURI(%s, _HTTP_RESOLVE_DEFAULT): ", argv[1]); |
1f0275e3 MS |
535 | fflush(stdout); |
536 | ||
eac3a0a0 MS |
537 | if (!_httpResolveURI(argv[1], resolved, sizeof(resolved), |
538 | _HTTP_RESOLVE_DEFAULT, NULL, NULL)) | |
1f0275e3 MS |
539 | { |
540 | puts("FAIL"); | |
541 | return (1); | |
542 | } | |
eac3a0a0 MS |
543 | else |
544 | printf("PASS (%s)\n", resolved); | |
545 | ||
546 | printf("_httpResolveURI(%s, _HTTP_RESOLVE_FQDN): ", argv[1]); | |
547 | fflush(stdout); | |
548 | ||
549 | if (!_httpResolveURI(argv[1], resolved, sizeof(resolved), | |
550 | _HTTP_RESOLVE_FQDN, NULL, NULL)) | |
551 | { | |
552 | puts("FAIL"); | |
553 | return (1); | |
554 | } | |
555 | else if (strstr(resolved, ".local:")) | |
556 | { | |
557 | printf("FAIL (%s)\n", resolved); | |
558 | return (1); | |
559 | } | |
1f0275e3 MS |
560 | else |
561 | { | |
562 | printf("PASS (%s)\n", resolved); | |
563 | return (0); | |
564 | } | |
565 | } | |
dfd5680b MS |
566 | else if (!strcmp(argv[1], "-u") && argc == 3) |
567 | { | |
568 | /* | |
569 | * Test URI separation... | |
570 | */ | |
571 | ||
572 | uri_status = httpSeparateURI(HTTP_URI_CODING_ALL, argv[2], scheme, | |
573 | sizeof(scheme), username, sizeof(username), | |
574 | hostname, sizeof(hostname), &port, | |
575 | resource, sizeof(resource)); | |
576 | printf("uri_status = %s\n", uri_status_strings[uri_status + 8]); | |
577 | printf("scheme = \"%s\"\n", scheme); | |
578 | printf("username = \"%s\"\n", username); | |
579 | printf("hostname = \"%s\"\n", hostname); | |
580 | printf("port = %d\n", port); | |
581 | printf("resource = \"%s\"\n", resource); | |
582 | ||
583 | return (0); | |
584 | } | |
ef416fc2 | 585 | |
586 | /* | |
587 | * Test HTTP GET requests... | |
588 | */ | |
589 | ||
590 | http = NULL; | |
591 | out = stdout; | |
592 | ||
593 | for (i = 1; i < argc; i ++) | |
594 | { | |
595 | if (!strcmp(argv[i], "-o")) | |
596 | { | |
597 | i ++; | |
598 | if (i >= argc) | |
599 | break; | |
600 | ||
601 | out = fopen(argv[i], "wb"); | |
602 | continue; | |
603 | } | |
604 | ||
a4d04587 | 605 | httpSeparateURI(HTTP_URI_CODING_MOST, argv[i], scheme, sizeof(scheme), |
606 | username, sizeof(username), | |
ef416fc2 | 607 | hostname, sizeof(hostname), &port, |
608 | resource, sizeof(resource)); | |
609 | ||
83e08001 MS |
610 | if (!_cups_strcasecmp(scheme, "https") || !_cups_strcasecmp(scheme, "ipps") || |
611 | port == 443) | |
cb7f98ee | 612 | encryption = HTTP_ENCRYPTION_ALWAYS; |
83e08001 | 613 | else |
cb7f98ee | 614 | encryption = HTTP_ENCRYPTION_IF_REQUESTED; |
83e08001 | 615 | |
376d7c69 | 616 | http = httpConnect2(hostname, port, NULL, AF_UNSPEC, encryption, 1, 30000, NULL); |
ef416fc2 | 617 | if (http == NULL) |
618 | { | |
619 | perror(hostname); | |
620 | continue; | |
621 | } | |
9653cfdf MS |
622 | |
623 | if (httpIsEncrypted(http)) | |
624 | { | |
625 | cups_array_t *creds; | |
626 | char info[1024]; | |
627 | ||
628 | if (!httpCopyCredentials(http, &creds)) | |
629 | { | |
630 | httpCredentialsString(creds, info, sizeof(info)); | |
376d7c69 MS |
631 | |
632 | printf("AreTrusted: %d\n", httpCredentialsAreTrusted(creds, hostname)); | |
633 | printf("Expiration: %s\n", httpGetDateString(httpCredentialsGetExpiration(creds))); | |
634 | printf("IsValidName: %d\n", httpCredentialsIsValidName(creds, hostname)); | |
635 | printf("String: \"%s\"\n", info); | |
636 | ||
9653cfdf | 637 | httpFreeCredentials(creds); |
9653cfdf MS |
638 | } |
639 | else | |
376d7c69 | 640 | puts("No credentials!"); |
9653cfdf MS |
641 | } |
642 | ||
a469f8a5 | 643 | printf("Checking file \"%s\"...\n", resource); |
0c4bedc4 MS |
644 | |
645 | do | |
646 | { | |
647 | if (!_cups_strcasecmp(httpGetField(http, HTTP_FIELD_CONNECTION), "close")) | |
648 | { | |
649 | httpClearFields(http); | |
650 | if (httpReconnect2(http, 30000, NULL)) | |
651 | { | |
652 | status = HTTP_STATUS_ERROR; | |
653 | break; | |
654 | } | |
655 | } | |
656 | ||
657 | httpClearFields(http); | |
658 | httpSetField(http, HTTP_FIELD_AUTHORIZATION, httpGetAuthString(http)); | |
659 | httpSetField(http, HTTP_FIELD_ACCEPT_LANGUAGE, "en"); | |
660 | if (httpHead(http, resource)) | |
661 | { | |
662 | if (httpReconnect2(http, 30000, NULL)) | |
663 | { | |
664 | status = HTTP_STATUS_ERROR; | |
665 | break; | |
666 | } | |
667 | else | |
668 | { | |
669 | status = HTTP_STATUS_UNAUTHORIZED; | |
670 | continue; | |
671 | } | |
672 | } | |
673 | ||
674 | while ((status = httpUpdate(http)) == HTTP_STATUS_CONTINUE); | |
675 | ||
676 | if (status == HTTP_STATUS_UNAUTHORIZED) | |
677 | { | |
678 | /* | |
679 | * Flush any error message... | |
680 | */ | |
681 | ||
682 | httpFlush(http); | |
683 | ||
684 | /* | |
685 | * See if we can do authentication... | |
686 | */ | |
687 | ||
688 | if (cupsDoAuthentication(http, "GET", resource)) | |
689 | { | |
690 | status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED; | |
691 | break; | |
692 | } | |
693 | ||
694 | if (httpReconnect2(http, 30000, NULL)) | |
695 | { | |
696 | status = HTTP_STATUS_ERROR; | |
697 | break; | |
698 | } | |
699 | ||
700 | continue; | |
701 | } | |
702 | #ifdef HAVE_SSL | |
703 | else if (status == HTTP_STATUS_UPGRADE_REQUIRED) | |
704 | { | |
705 | /* Flush any error message... */ | |
706 | httpFlush(http); | |
707 | ||
708 | /* Reconnect... */ | |
709 | if (httpReconnect2(http, 30000, NULL)) | |
710 | { | |
711 | status = HTTP_STATUS_ERROR; | |
712 | break; | |
713 | } | |
714 | ||
715 | /* Upgrade with encryption... */ | |
716 | httpEncryption(http, HTTP_ENCRYPTION_REQUIRED); | |
717 | ||
718 | /* Try again, this time with encryption enabled... */ | |
719 | continue; | |
720 | } | |
721 | #endif /* HAVE_SSL */ | |
722 | } | |
723 | while (status == HTTP_STATUS_UNAUTHORIZED || | |
724 | status == HTTP_STATUS_UPGRADE_REQUIRED); | |
a469f8a5 | 725 | |
cb7f98ee | 726 | if (status == HTTP_STATUS_OK) |
a469f8a5 MS |
727 | puts("HEAD OK:"); |
728 | else | |
729 | printf("HEAD failed with status %d...\n", status); | |
730 | ||
731 | encoding = httpGetContentEncoding(http); | |
732 | ||
733 | printf("Requesting file \"%s\" (Accept-Encoding: %s)...\n", resource, | |
734 | encoding ? encoding : "identity"); | |
0c4bedc4 MS |
735 | |
736 | do | |
737 | { | |
738 | if (!_cups_strcasecmp(httpGetField(http, HTTP_FIELD_CONNECTION), "close")) | |
739 | { | |
740 | httpClearFields(http); | |
741 | if (httpReconnect2(http, 30000, NULL)) | |
742 | { | |
743 | status = HTTP_STATUS_ERROR; | |
744 | break; | |
745 | } | |
746 | } | |
747 | ||
748 | httpClearFields(http); | |
749 | httpSetField(http, HTTP_FIELD_AUTHORIZATION, httpGetAuthString(http)); | |
750 | httpSetField(http, HTTP_FIELD_ACCEPT_LANGUAGE, "en"); | |
751 | httpSetField(http, HTTP_FIELD_ACCEPT_ENCODING, encoding); | |
752 | ||
753 | if (httpGet(http, resource)) | |
754 | { | |
755 | if (httpReconnect2(http, 30000, NULL)) | |
756 | { | |
757 | status = HTTP_STATUS_ERROR; | |
758 | break; | |
759 | } | |
760 | else | |
761 | { | |
762 | status = HTTP_STATUS_UNAUTHORIZED; | |
763 | continue; | |
764 | } | |
765 | } | |
766 | ||
767 | while ((status = httpUpdate(http)) == HTTP_STATUS_CONTINUE); | |
768 | ||
769 | if (status == HTTP_STATUS_UNAUTHORIZED) | |
770 | { | |
771 | /* | |
772 | * Flush any error message... | |
773 | */ | |
774 | ||
775 | httpFlush(http); | |
776 | ||
777 | /* | |
778 | * See if we can do authentication... | |
779 | */ | |
780 | ||
781 | if (cupsDoAuthentication(http, "GET", resource)) | |
782 | { | |
783 | status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED; | |
784 | break; | |
785 | } | |
786 | ||
787 | if (httpReconnect2(http, 30000, NULL)) | |
788 | { | |
789 | status = HTTP_STATUS_ERROR; | |
790 | break; | |
791 | } | |
792 | ||
793 | continue; | |
794 | } | |
795 | #ifdef HAVE_SSL | |
796 | else if (status == HTTP_STATUS_UPGRADE_REQUIRED) | |
797 | { | |
798 | /* Flush any error message... */ | |
799 | httpFlush(http); | |
800 | ||
801 | /* Reconnect... */ | |
802 | if (httpReconnect2(http, 30000, NULL)) | |
803 | { | |
804 | status = HTTP_STATUS_ERROR; | |
805 | break; | |
806 | } | |
807 | ||
808 | /* Upgrade with encryption... */ | |
809 | httpEncryption(http, HTTP_ENCRYPTION_REQUIRED); | |
810 | ||
811 | /* Try again, this time with encryption enabled... */ | |
812 | continue; | |
813 | } | |
814 | #endif /* HAVE_SSL */ | |
815 | } | |
816 | while (status == HTTP_STATUS_UNAUTHORIZED || status == HTTP_STATUS_UPGRADE_REQUIRED); | |
ef416fc2 | 817 | |
cb7f98ee | 818 | if (status == HTTP_STATUS_OK) |
ef416fc2 | 819 | puts("GET OK:"); |
820 | else | |
821 | printf("GET failed with status %d...\n", status); | |
822 | ||
ef416fc2 | 823 | start = time(NULL); |
824 | length = httpGetLength2(http); | |
825 | total = 0; | |
826 | ||
a4d04587 | 827 | while ((bytes = httpRead2(http, buffer, sizeof(buffer))) > 0) |
ef416fc2 | 828 | { |
829 | total += bytes; | |
7e86f2f6 | 830 | fwrite(buffer, (size_t)bytes, 1, out); |
ef416fc2 | 831 | if (out != stdout) |
832 | { | |
833 | current = time(NULL); | |
376d7c69 MS |
834 | if (current == start) |
835 | current ++; | |
836 | ||
ef416fc2 | 837 | printf("\r" CUPS_LLFMT "/" CUPS_LLFMT " bytes (" |
e1d6a774 | 838 | CUPS_LLFMT " bytes/sec) ", CUPS_LLCAST total, |
839 | CUPS_LLCAST length, CUPS_LLCAST (total / (current - start))); | |
ef416fc2 | 840 | fflush(stdout); |
841 | } | |
842 | } | |
843 | } | |
844 | ||
376d7c69 MS |
845 | if (out != stdout) |
846 | putchar('\n'); | |
847 | ||
ef416fc2 | 848 | puts("Closing connection to server..."); |
849 | httpClose(http); | |
850 | ||
851 | if (out != stdout) | |
852 | fclose(out); | |
853 | ||
854 | return (0); | |
855 | } | |
856 | ||
857 | ||
858 | /* | |
f2d18633 | 859 | * End of "$Id$". |
ef416fc2 | 860 | */ |