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