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