]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
f2d18633 | 2 | * "$Id$" |
ef416fc2 | 3 | * |
71e16022 | 4 | * HTTP test program for CUPS. |
ef416fc2 | 5 | * |
cb7f98ee | 6 | * Copyright 2007-2013 by Apple Inc. |
ed486911 | 7 | * Copyright 1997-2006 by Easy Software Products. |
ef416fc2 | 8 | * |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 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 | * |
15 | * This file is subject to the Apple OS-Developed Software exception. | |
16 | * | |
17 | * Contents: | |
18 | * | |
19 | * main() - Main entry. | |
20 | */ | |
21 | ||
22 | /* | |
23 | * Include necessary headers... | |
24 | */ | |
25 | ||
71e16022 | 26 | #include "string-private.h" |
1f0275e3 | 27 | #include "http-private.h" |
ef416fc2 | 28 | |
29 | ||
30 | /* | |
31 | * Types and structures... | |
32 | */ | |
33 | ||
34 | typedef struct uri_test_s /**** URI test cases ****/ | |
35 | { | |
36 | http_uri_status_t result; /* Expected return value */ | |
37 | const char *uri, /* URI */ | |
38 | *scheme, /* Scheme string */ | |
39 | *username, /* Username:password string */ | |
40 | *hostname, /* Hostname string */ | |
41 | *resource; /* Resource string */ | |
42 | int port, /* Port number */ | |
86c809d9 MS |
43 | assemble_port, /* Port number for httpAssembleURI() */ |
44 | assemble_coding;/* Coding for httpAssembleURI() */ | |
ef416fc2 | 45 | } uri_test_t; |
46 | ||
47 | ||
48 | /* | |
49 | * Local globals... | |
50 | */ | |
51 | ||
52 | static uri_test_t uri_tests[] = /* URI test data */ | |
53 | { | |
54 | /* Start with valid URIs */ | |
cb7f98ee | 55 | { HTTP_URI_STATUS_OK, "file:/filename", |
86c809d9 MS |
56 | "file", "", "", "/filename", 0, 0, |
57 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 58 | { HTTP_URI_STATUS_OK, "file:/filename%20with%20spaces", |
86c809d9 MS |
59 | "file", "", "", "/filename with spaces", 0, 0, |
60 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 61 | { HTTP_URI_STATUS_OK, "file:///filename", |
86c809d9 MS |
62 | "file", "", "", "/filename", 0, 0, |
63 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 64 | { HTTP_URI_STATUS_OK, "file:///filename%20with%20spaces", |
86c809d9 MS |
65 | "file", "", "", "/filename with spaces", 0, 0, |
66 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 67 | { HTTP_URI_STATUS_OK, "file://localhost/filename", |
86c809d9 MS |
68 | "file", "", "localhost", "/filename", 0, 0, |
69 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 70 | { HTTP_URI_STATUS_OK, "file://localhost/filename%20with%20spaces", |
86c809d9 MS |
71 | "file", "", "localhost", "/filename with spaces", 0, 0, |
72 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 73 | { HTTP_URI_STATUS_OK, "http://server/", |
86c809d9 MS |
74 | "http", "", "server", "/", 80, 0, |
75 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 76 | { HTTP_URI_STATUS_OK, "http://username@server/", |
86c809d9 MS |
77 | "http", "username", "server", "/", 80, 0, |
78 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 79 | { HTTP_URI_STATUS_OK, "http://username:passwor%64@server/", |
86c809d9 MS |
80 | "http", "username:password", "server", "/", 80, 0, |
81 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 82 | { HTTP_URI_STATUS_OK, "http://username:passwor%64@server:8080/", |
86c809d9 MS |
83 | "http", "username:password", "server", "/", 8080, 8080, |
84 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 85 | { HTTP_URI_STATUS_OK, "http://username:passwor%64@server:8080/directory/filename", |
86c809d9 MS |
86 | "http", "username:password", "server", "/directory/filename", 8080, 8080, |
87 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 88 | { HTTP_URI_STATUS_OK, "http://[2000::10:100]:631/ipp", |
86c809d9 MS |
89 | "http", "", "2000::10:100", "/ipp", 631, 631, |
90 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 91 | { HTTP_URI_STATUS_OK, "https://username:passwor%64@server/directory/filename", |
86c809d9 MS |
92 | "https", "username:password", "server", "/directory/filename", 443, 0, |
93 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 94 | { HTTP_URI_STATUS_OK, "ipp://username:passwor%64@[::1]/ipp", |
86c809d9 MS |
95 | "ipp", "username:password", "::1", "/ipp", 631, 0, |
96 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 97 | { HTTP_URI_STATUS_OK, "lpd://server/queue?reserve=yes", |
86c809d9 MS |
98 | "lpd", "", "server", "/queue?reserve=yes", 515, 0, |
99 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 100 | { HTTP_URI_STATUS_OK, "mailto:user@domain.com", |
86c809d9 MS |
101 | "mailto", "", "", "user@domain.com", 0, 0, |
102 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 103 | { HTTP_URI_STATUS_OK, "socket://server/", |
86c809d9 MS |
104 | "socket", "", "server", "/", 9100, 0, |
105 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 106 | { HTTP_URI_STATUS_OK, "socket://192.168.1.1:9101/", |
86c809d9 MS |
107 | "socket", "", "192.168.1.1", "/", 9101, 9101, |
108 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 109 | { HTTP_URI_STATUS_OK, "ipp://username:password@[v1.fe80::200:1234:5678:9abc+eth0]:999/ipp", |
86c809d9 MS |
110 | "ipp", "username:password", "fe80::200:1234:5678:9abc%eth0", "/ipp", 999, 999, |
111 | HTTP_URI_CODING_MOST }, | |
112 | { HTTP_URI_STATUS_OK, "ipp://username:password@[fe80::200:1234:5678:9abc%25eth0]:999/ipp", | |
113 | "ipp", "username:password", "fe80::200:1234:5678:9abc%eth0", "/ipp", 999, 999, | |
114 | HTTP_URI_CODING_MOST | HTTP_URI_CODING_RFC6874 }, | |
cb7f98ee | 115 | { HTTP_URI_STATUS_OK, "http://server/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400", |
86c809d9 MS |
116 | "http", "", "server", "/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400", 80, 0, |
117 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 118 | { HTTP_URI_STATUS_OK, "lpd://Acme%20Laser%20(01%3A23%3A45).local._tcp._printer/", |
86c809d9 MS |
119 | "lpd", "", "Acme Laser (01:23:45).local._tcp._printer", "/", 515, 0, |
120 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 121 | { HTTP_URI_STATUS_OK, "ipp://HP%20Officejet%204500%20G510n-z%20%40%20Will's%20MacBook%20Pro%2015%22._ipp._tcp.local./", |
86c809d9 MS |
122 | "ipp", "", "HP Officejet 4500 G510n-z @ Will's MacBook Pro 15\"._ipp._tcp.local.", "/", 631, 0, |
123 | HTTP_URI_CODING_MOST }, | |
ef416fc2 | 124 | |
125 | /* Missing scheme */ | |
cb7f98ee | 126 | { HTTP_URI_STATUS_MISSING_SCHEME, "/path/to/file/index.html", |
86c809d9 MS |
127 | "file", "", "", "/path/to/file/index.html", 0, 0, |
128 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 129 | { HTTP_URI_STATUS_MISSING_SCHEME, "//server/ipp", |
86c809d9 MS |
130 | "ipp", "", "server", "/ipp", 631, 0, |
131 | HTTP_URI_CODING_MOST }, | |
ef416fc2 | 132 | |
133 | /* Unknown scheme */ | |
cb7f98ee | 134 | { HTTP_URI_STATUS_UNKNOWN_SCHEME, "vendor://server/resource", |
86c809d9 MS |
135 | "vendor", "", "server", "/resource", 0, 0, |
136 | HTTP_URI_CODING_MOST }, | |
ef416fc2 | 137 | |
138 | /* Missing resource */ | |
cb7f98ee | 139 | { HTTP_URI_STATUS_MISSING_RESOURCE, "socket://[::192.168.2.1]", |
86c809d9 MS |
140 | "socket", "", "::192.168.2.1", "/", 9100, 0, |
141 | HTTP_URI_CODING_MOST }, | |
cb7f98ee | 142 | { HTTP_URI_STATUS_MISSING_RESOURCE, "socket://192.168.1.1:9101", |
86c809d9 MS |
143 | "socket", "", "192.168.1.1", "/", 9101, 0, |
144 | HTTP_URI_CODING_MOST }, | |
ef416fc2 | 145 | |
146 | /* Bad URI */ | |
cb7f98ee | 147 | { HTTP_URI_STATUS_BAD_URI, "", |
86c809d9 MS |
148 | "", "", "", "", 0, 0, |
149 | HTTP_URI_CODING_MOST }, | |
ef416fc2 | 150 | |
151 | /* Bad scheme */ | |
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 }, | |
ef416fc2 | 174 | |
175 | /* Bad port number */ | |
cb7f98ee | 176 | { HTTP_URI_STATUS_BAD_PORT, "http://127.0.0.1:9999a/index.html", |
86c809d9 MS |
177 | "http", "", "127.0.0.1", "", 0, 0, |
178 | HTTP_URI_CODING_MOST }, | |
ef416fc2 | 179 | |
180 | /* Bad resource */ | |
cb7f98ee | 181 | { HTTP_URI_STATUS_BAD_RESOURCE, "http://server/index.html%", |
86c809d9 MS |
182 | "http", "", "server", "", 80, 0, |
183 | HTTP_URI_CODING_MOST }, | |
6961465f | 184 | { HTTP_URI_STATUS_BAD_RESOURCE, "http://server/index with spaces.html", |
86c809d9 MS |
185 | "http", "", "server", "", 80, 0, |
186 | HTTP_URI_CODING_MOST } | |
ef416fc2 | 187 | }; |
188 | static const char * const base64_tests[][2] = | |
189 | { | |
190 | { "A", "QQ==" }, | |
191 | /* 010000 01 */ | |
192 | { "AB", "QUI=" }, | |
193 | /* 010000 010100 0010 */ | |
194 | { "ABC", "QUJD" }, | |
195 | /* 010000 010100 001001 000011 */ | |
196 | { "ABCD", "QUJDRA==" }, | |
197 | /* 010000 010100 001001 000011 010001 00 */ | |
198 | { "ABCDE", "QUJDREU=" }, | |
199 | /* 010000 010100 001001 000011 010001 000100 0101 */ | |
200 | { "ABCDEF", "QUJDREVG" }, | |
201 | /* 010000 010100 001001 000011 010001 000100 010101 000110 */ | |
202 | }; | |
203 | ||
204 | ||
205 | /* | |
206 | * 'main()' - Main entry. | |
207 | */ | |
208 | ||
209 | int /* O - Exit status */ | |
210 | main(int argc, /* I - Number of command-line arguments */ | |
211 | char *argv[]) /* I - Command-line arguments */ | |
212 | { | |
213 | int i, j, k; /* Looping vars */ | |
214 | http_t *http; /* HTTP connection */ | |
83e08001 | 215 | http_encryption_t encryption; /* Encryption type */ |
ef416fc2 | 216 | http_status_t status; /* Status of GET command */ |
217 | int failures; /* Number of test failures */ | |
218 | char buffer[8192]; /* Input buffer */ | |
219 | long bytes; /* Number of bytes read */ | |
220 | FILE *out; /* Output file */ | |
221 | char encode[256], /* Base64-encoded string */ | |
222 | decode[256]; /* Base64-decoded string */ | |
223 | int decodelen; /* Length of decoded string */ | |
224 | char scheme[HTTP_MAX_URI], /* Scheme from URI */ | |
225 | hostname[HTTP_MAX_URI], /* Hostname from URI */ | |
226 | username[HTTP_MAX_URI], /* Username:password from URI */ | |
227 | resource[HTTP_MAX_URI]; /* Resource from URI */ | |
228 | int port; /* Port number from URI */ | |
229 | http_uri_status_t uri_status; /* Status of URI separation */ | |
230 | http_addrlist_t *addrlist, /* Address list */ | |
231 | *addr; /* Current address */ | |
232 | off_t length, total; /* Length and total bytes */ | |
233 | time_t start, current; /* Start and end time */ | |
a469f8a5 | 234 | const char *encoding; /* Negotiated Content-Encoding */ |
ef416fc2 | 235 | static const char * const uri_status_strings[] = |
236 | { | |
cb7f98ee MS |
237 | "HTTP_URI_STATUS_OVERFLOW", |
238 | "HTTP_URI_STATUS_BAD_ARGUMENTS", | |
239 | "HTTP_URI_STATUS_BAD_RESOURCE", | |
240 | "HTTP_URI_STATUS_BAD_PORT", | |
241 | "HTTP_URI_STATUS_BAD_HOSTNAME", | |
242 | "HTTP_URI_STATUS_BAD_USERNAME", | |
243 | "HTTP_URI_STATUS_BAD_SCHEME", | |
244 | "HTTP_URI_STATUS_BAD_URI", | |
245 | "HTTP_URI_STATUS_OK", | |
246 | "HTTP_URI_STATUS_MISSING_SCHEME", | |
247 | "HTTP_URI_STATUS_UNKNOWN_SCHEME", | |
248 | "HTTP_URI_STATUS_MISSING_RESOURCE" | |
ef416fc2 | 249 | }; |
250 | ||
251 | ||
252 | /* | |
253 | * Do API tests if we don't have a URL on the command-line... | |
254 | */ | |
255 | ||
256 | if (argc == 1) | |
257 | { | |
258 | failures = 0; | |
259 | ||
260 | /* | |
261 | * httpGetDateString()/httpGetDateTime() | |
262 | */ | |
263 | ||
264 | fputs("httpGetDateString()/httpGetDateTime(): ", stdout); | |
265 | ||
266 | start = time(NULL); | |
5a9febac | 267 | strlcpy(buffer, httpGetDateString(start), sizeof(buffer)); |
ef416fc2 | 268 | current = httpGetDateTime(buffer); |
269 | ||
270 | i = (int)(current - start); | |
271 | if (i < 0) | |
272 | i = -i; | |
273 | ||
274 | if (!i) | |
275 | puts("PASS"); | |
276 | else | |
277 | { | |
278 | failures ++; | |
279 | puts("FAIL"); | |
280 | printf(" Difference is %d seconds, %02d:%02d:%02d...\n", i, i / 3600, | |
281 | (i / 60) % 60, i % 60); | |
282 | printf(" httpGetDateString(%d) returned \"%s\"\n", (int)start, buffer); | |
283 | printf(" httpGetDateTime(\"%s\") returned %d\n", buffer, (int)current); | |
284 | printf(" httpGetDateString(%d) returned \"%s\"\n", (int)current, | |
285 | httpGetDateString(current)); | |
286 | } | |
287 | ||
288 | /* | |
289 | * httpDecode64_2()/httpEncode64_2() | |
290 | */ | |
291 | ||
292 | fputs("httpDecode64_2()/httpEncode64_2(): ", stdout); | |
293 | ||
294 | for (i = 0, j = 0; i < (int)(sizeof(base64_tests) / sizeof(base64_tests[0])); i ++) | |
295 | { | |
296 | httpEncode64_2(encode, sizeof(encode), base64_tests[i][0], | |
7a0cbd5e | 297 | (int)strlen(base64_tests[i][0])); |
ef416fc2 | 298 | decodelen = (int)sizeof(decode); |
299 | httpDecode64_2(decode, &decodelen, base64_tests[i][1]); | |
300 | ||
301 | if (strcmp(decode, base64_tests[i][0])) | |
302 | { | |
303 | failures ++; | |
304 | ||
305 | if (j) | |
306 | { | |
307 | puts("FAIL"); | |
308 | j = 1; | |
309 | } | |
310 | ||
311 | printf(" httpDecode64_2() returned \"%s\", expected \"%s\"...\n", | |
312 | decode, base64_tests[i][0]); | |
313 | } | |
314 | ||
315 | if (strcmp(encode, base64_tests[i][1])) | |
316 | { | |
317 | failures ++; | |
318 | ||
319 | if (j) | |
320 | { | |
321 | puts("FAIL"); | |
322 | j = 1; | |
323 | } | |
324 | ||
325 | printf(" httpEncode64_2() returned \"%s\", expected \"%s\"...\n", | |
326 | encode, base64_tests[i][1]); | |
327 | } | |
328 | } | |
329 | ||
330 | if (!j) | |
331 | puts("PASS"); | |
332 | ||
333 | /* | |
334 | * httpGetHostname() | |
335 | */ | |
336 | ||
337 | fputs("httpGetHostname(): ", stdout); | |
338 | ||
757d2cad | 339 | if (httpGetHostname(NULL, hostname, sizeof(hostname))) |
ef416fc2 | 340 | printf("PASS (%s)\n", hostname); |
341 | else | |
342 | { | |
343 | failures ++; | |
344 | puts("FAIL"); | |
345 | } | |
346 | ||
347 | /* | |
348 | * httpAddrGetList() | |
349 | */ | |
350 | ||
75bd9771 | 351 | printf("httpAddrGetList(%s): ", hostname); |
ef416fc2 | 352 | |
353 | addrlist = httpAddrGetList(hostname, AF_UNSPEC, NULL); | |
354 | if (addrlist) | |
355 | { | |
ed486911 | 356 | for (i = 0, addr = addrlist; addr; i ++, addr = addr->next) |
357 | { | |
358 | char numeric[1024]; /* Numeric IP address */ | |
359 | ||
360 | ||
361 | httpAddrString(&(addr->addr), numeric, sizeof(numeric)); | |
362 | if (!strcmp(numeric, "UNKNOWN")) | |
363 | break; | |
364 | } | |
365 | ||
366 | if (addr) | |
367 | printf("FAIL (bad address for %s)\n", hostname); | |
368 | else | |
369 | printf("PASS (%d address(es) for %s)\n", i, hostname); | |
ef416fc2 | 370 | |
ef416fc2 | 371 | httpAddrFreeList(addrlist); |
372 | } | |
58dc1933 MS |
373 | else if (isdigit(hostname[0] & 255)) |
374 | { | |
375 | puts("FAIL (ignored because hostname is numeric)"); | |
376 | } | |
ef416fc2 | 377 | else |
378 | { | |
379 | failures ++; | |
380 | puts("FAIL"); | |
381 | } | |
382 | ||
383 | /* | |
384 | * Test httpSeparateURI()... | |
385 | */ | |
386 | ||
387 | fputs("httpSeparateURI(): ", stdout); | |
388 | for (i = 0, j = 0; i < (int)(sizeof(uri_tests) / sizeof(uri_tests[0])); i ++) | |
389 | { | |
a4d04587 | 390 | uri_status = httpSeparateURI(HTTP_URI_CODING_MOST, |
391 | uri_tests[i].uri, scheme, sizeof(scheme), | |
ef416fc2 | 392 | username, sizeof(username), |
393 | hostname, sizeof(hostname), &port, | |
394 | resource, sizeof(resource)); | |
395 | if (uri_status != uri_tests[i].result || | |
396 | strcmp(scheme, uri_tests[i].scheme) || | |
397 | strcmp(username, uri_tests[i].username) || | |
398 | strcmp(hostname, uri_tests[i].hostname) || | |
399 | port != uri_tests[i].port || | |
400 | strcmp(resource, uri_tests[i].resource)) | |
401 | { | |
402 | failures ++; | |
403 | ||
404 | if (!j) | |
405 | { | |
406 | puts("FAIL"); | |
407 | j = 1; | |
408 | } | |
409 | ||
410 | printf(" \"%s\":\n", uri_tests[i].uri); | |
411 | ||
412 | if (uri_status != uri_tests[i].result) | |
413 | printf(" Returned %s instead of %s\n", | |
414 | uri_status_strings[uri_status + 8], | |
415 | uri_status_strings[uri_tests[i].result + 8]); | |
416 | ||
417 | if (strcmp(scheme, uri_tests[i].scheme)) | |
418 | printf(" Scheme \"%s\" instead of \"%s\"\n", | |
419 | scheme, uri_tests[i].scheme); | |
420 | ||
421 | if (strcmp(username, uri_tests[i].username)) | |
422 | printf(" Username \"%s\" instead of \"%s\"\n", | |
423 | username, uri_tests[i].username); | |
424 | ||
425 | if (strcmp(hostname, uri_tests[i].hostname)) | |
426 | printf(" Hostname \"%s\" instead of \"%s\"\n", | |
427 | hostname, uri_tests[i].hostname); | |
428 | ||
429 | if (port != uri_tests[i].port) | |
430 | printf(" Port %d instead of %d\n", | |
431 | port, uri_tests[i].port); | |
432 | ||
433 | if (strcmp(resource, uri_tests[i].resource)) | |
434 | printf(" Resource \"%s\" instead of \"%s\"\n", | |
435 | resource, uri_tests[i].resource); | |
436 | } | |
437 | } | |
438 | ||
439 | if (!j) | |
440 | printf("PASS (%d URIs tested)\n", | |
441 | (int)(sizeof(uri_tests) / sizeof(uri_tests[0]))); | |
442 | ||
443 | /* | |
444 | * Test httpAssembleURI()... | |
445 | */ | |
446 | ||
447 | fputs("httpAssembleURI(): ", stdout); | |
448 | for (i = 0, j = 0, k = 0; | |
449 | i < (int)(sizeof(uri_tests) / sizeof(uri_tests[0])); | |
450 | i ++) | |
cb7f98ee | 451 | if (uri_tests[i].result == HTTP_URI_STATUS_OK && |
ef416fc2 | 452 | !strstr(uri_tests[i].uri, "%64") && |
453 | strstr(uri_tests[i].uri, "//")) | |
454 | { | |
455 | k ++; | |
86c809d9 | 456 | uri_status = httpAssembleURI(uri_tests[i].assemble_coding, |
a4d04587 | 457 | buffer, sizeof(buffer), |
ef416fc2 | 458 | uri_tests[i].scheme, |
459 | uri_tests[i].username, | |
460 | uri_tests[i].hostname, | |
461 | uri_tests[i].assemble_port, | |
462 | uri_tests[i].resource); | |
463 | ||
cb7f98ee | 464 | if (uri_status != HTTP_URI_STATUS_OK) |
ef416fc2 | 465 | { |
466 | failures ++; | |
467 | ||
468 | if (!j) | |
469 | { | |
470 | puts("FAIL"); | |
471 | j = 1; | |
472 | } | |
473 | ||
474 | printf(" \"%s\": %s\n", uri_tests[i].uri, | |
475 | uri_status_strings[uri_status + 8]); | |
476 | } | |
477 | else if (strcmp(buffer, uri_tests[i].uri)) | |
478 | { | |
479 | failures ++; | |
480 | ||
481 | if (!j) | |
482 | { | |
483 | puts("FAIL"); | |
484 | j = 1; | |
485 | } | |
486 | ||
487 | printf(" \"%s\": assembled = \"%s\"\n", uri_tests[i].uri, | |
488 | buffer); | |
489 | } | |
490 | } | |
491 | ||
492 | if (!j) | |
493 | printf("PASS (%d URIs tested)\n", k); | |
494 | ||
495 | /* | |
496 | * Show a summary and return... | |
497 | */ | |
498 | ||
499 | if (failures) | |
500 | printf("\n%d TESTS FAILED!\n", failures); | |
501 | else | |
502 | puts("\nALL TESTS PASSED!"); | |
503 | ||
504 | return (failures); | |
505 | } | |
1f0275e3 MS |
506 | else if (strstr(argv[1], "._tcp")) |
507 | { | |
508 | /* | |
509 | * Test resolving an mDNS name. | |
510 | */ | |
511 | ||
512 | char resolved[1024]; /* Resolved URI */ | |
513 | ||
514 | ||
eac3a0a0 | 515 | printf("_httpResolveURI(%s, _HTTP_RESOLVE_DEFAULT): ", argv[1]); |
1f0275e3 MS |
516 | fflush(stdout); |
517 | ||
eac3a0a0 MS |
518 | if (!_httpResolveURI(argv[1], resolved, sizeof(resolved), |
519 | _HTTP_RESOLVE_DEFAULT, NULL, NULL)) | |
1f0275e3 MS |
520 | { |
521 | puts("FAIL"); | |
522 | return (1); | |
523 | } | |
eac3a0a0 MS |
524 | else |
525 | printf("PASS (%s)\n", resolved); | |
526 | ||
527 | printf("_httpResolveURI(%s, _HTTP_RESOLVE_FQDN): ", argv[1]); | |
528 | fflush(stdout); | |
529 | ||
530 | if (!_httpResolveURI(argv[1], resolved, sizeof(resolved), | |
531 | _HTTP_RESOLVE_FQDN, NULL, NULL)) | |
532 | { | |
533 | puts("FAIL"); | |
534 | return (1); | |
535 | } | |
536 | else if (strstr(resolved, ".local:")) | |
537 | { | |
538 | printf("FAIL (%s)\n", resolved); | |
539 | return (1); | |
540 | } | |
1f0275e3 MS |
541 | else |
542 | { | |
543 | printf("PASS (%s)\n", resolved); | |
544 | return (0); | |
545 | } | |
546 | } | |
dfd5680b MS |
547 | else if (!strcmp(argv[1], "-u") && argc == 3) |
548 | { | |
549 | /* | |
550 | * Test URI separation... | |
551 | */ | |
552 | ||
553 | uri_status = httpSeparateURI(HTTP_URI_CODING_ALL, argv[2], scheme, | |
554 | sizeof(scheme), username, sizeof(username), | |
555 | hostname, sizeof(hostname), &port, | |
556 | resource, sizeof(resource)); | |
557 | printf("uri_status = %s\n", uri_status_strings[uri_status + 8]); | |
558 | printf("scheme = \"%s\"\n", scheme); | |
559 | printf("username = \"%s\"\n", username); | |
560 | printf("hostname = \"%s\"\n", hostname); | |
561 | printf("port = %d\n", port); | |
562 | printf("resource = \"%s\"\n", resource); | |
563 | ||
564 | return (0); | |
565 | } | |
ef416fc2 | 566 | |
567 | /* | |
568 | * Test HTTP GET requests... | |
569 | */ | |
570 | ||
571 | http = NULL; | |
572 | out = stdout; | |
573 | ||
574 | for (i = 1; i < argc; i ++) | |
575 | { | |
576 | if (!strcmp(argv[i], "-o")) | |
577 | { | |
578 | i ++; | |
579 | if (i >= argc) | |
580 | break; | |
581 | ||
582 | out = fopen(argv[i], "wb"); | |
583 | continue; | |
584 | } | |
585 | ||
a4d04587 | 586 | httpSeparateURI(HTTP_URI_CODING_MOST, argv[i], scheme, sizeof(scheme), |
587 | username, sizeof(username), | |
ef416fc2 | 588 | hostname, sizeof(hostname), &port, |
589 | resource, sizeof(resource)); | |
590 | ||
83e08001 MS |
591 | if (!_cups_strcasecmp(scheme, "https") || !_cups_strcasecmp(scheme, "ipps") || |
592 | port == 443) | |
cb7f98ee | 593 | encryption = HTTP_ENCRYPTION_ALWAYS; |
83e08001 | 594 | else |
cb7f98ee | 595 | encryption = HTTP_ENCRYPTION_IF_REQUESTED; |
83e08001 | 596 | |
cb7f98ee MS |
597 | http = httpConnect2(hostname, port, NULL, AF_UNSPEC, encryption, 1, 30000, |
598 | NULL); | |
ef416fc2 | 599 | if (http == NULL) |
600 | { | |
601 | perror(hostname); | |
602 | continue; | |
603 | } | |
a469f8a5 | 604 | printf("Checking file \"%s\"...\n", resource); |
ef416fc2 | 605 | httpClearFields(http); |
606 | httpSetField(http, HTTP_FIELD_ACCEPT_LANGUAGE, "en"); | |
a469f8a5 | 607 | httpHead(http, resource); |
cb7f98ee | 608 | while ((status = httpUpdate(http)) == HTTP_STATUS_CONTINUE); |
a469f8a5 | 609 | |
cb7f98ee | 610 | if (status == HTTP_STATUS_OK) |
a469f8a5 MS |
611 | puts("HEAD OK:"); |
612 | else | |
613 | printf("HEAD failed with status %d...\n", status); | |
614 | ||
615 | encoding = httpGetContentEncoding(http); | |
616 | ||
617 | printf("Requesting file \"%s\" (Accept-Encoding: %s)...\n", resource, | |
618 | encoding ? encoding : "identity"); | |
619 | httpClearFields(http); | |
620 | httpSetField(http, HTTP_FIELD_ACCEPT_LANGUAGE, "en"); | |
621 | httpSetField(http, HTTP_FIELD_ACCEPT_ENCODING, encoding); | |
ef416fc2 | 622 | httpGet(http, resource); |
cb7f98ee | 623 | while ((status = httpUpdate(http)) == HTTP_STATUS_CONTINUE); |
ef416fc2 | 624 | |
cb7f98ee | 625 | if (status == HTTP_STATUS_OK) |
ef416fc2 | 626 | puts("GET OK:"); |
627 | else | |
628 | printf("GET failed with status %d...\n", status); | |
629 | ||
ef416fc2 | 630 | start = time(NULL); |
631 | length = httpGetLength2(http); | |
632 | total = 0; | |
633 | ||
a4d04587 | 634 | while ((bytes = httpRead2(http, buffer, sizeof(buffer))) > 0) |
ef416fc2 | 635 | { |
636 | total += bytes; | |
637 | fwrite(buffer, bytes, 1, out); | |
638 | if (out != stdout) | |
639 | { | |
640 | current = time(NULL); | |
641 | if (current == start) current ++; | |
642 | printf("\r" CUPS_LLFMT "/" CUPS_LLFMT " bytes (" | |
e1d6a774 | 643 | CUPS_LLFMT " bytes/sec) ", CUPS_LLCAST total, |
644 | CUPS_LLCAST length, CUPS_LLCAST (total / (current - start))); | |
ef416fc2 | 645 | fflush(stdout); |
646 | } | |
647 | } | |
648 | } | |
649 | ||
650 | puts("Closing connection to server..."); | |
651 | httpClose(http); | |
652 | ||
653 | if (out != stdout) | |
654 | fclose(out); | |
655 | ||
656 | return (0); | |
657 | } | |
658 | ||
659 | ||
660 | /* | |
f2d18633 | 661 | * End of "$Id$". |
ef416fc2 | 662 | */ |