2 * "$Id: testhttp.c 7742 2008-07-15 20:23:09Z mike $"
4 * HTTP test program for CUPS.
6 * Copyright 2007-2013 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * main() - Main entry.
23 * Include necessary headers...
26 #include "string-private.h"
27 #include "http-private.h"
31 * Types and structures...
34 typedef struct uri_test_s
/**** URI test cases ****/
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 */
43 assemble_port
, /* Port number for httpAssembleURI() */
44 assemble_coding
;/* Coding for httpAssembleURI() */
52 static uri_test_t uri_tests
[] = /* URI test data */
54 /* Start with valid URIs */
55 { HTTP_URI_STATUS_OK
, "file:/filename",
56 "file", "", "", "/filename", 0, 0,
57 HTTP_URI_CODING_MOST
},
58 { HTTP_URI_STATUS_OK
, "file:/filename%20with%20spaces",
59 "file", "", "", "/filename with spaces", 0, 0,
60 HTTP_URI_CODING_MOST
},
61 { HTTP_URI_STATUS_OK
, "file:///filename",
62 "file", "", "", "/filename", 0, 0,
63 HTTP_URI_CODING_MOST
},
64 { HTTP_URI_STATUS_OK
, "file:///filename%20with%20spaces",
65 "file", "", "", "/filename with spaces", 0, 0,
66 HTTP_URI_CODING_MOST
},
67 { HTTP_URI_STATUS_OK
, "file://localhost/filename",
68 "file", "", "localhost", "/filename", 0, 0,
69 HTTP_URI_CODING_MOST
},
70 { HTTP_URI_STATUS_OK
, "file://localhost/filename%20with%20spaces",
71 "file", "", "localhost", "/filename with spaces", 0, 0,
72 HTTP_URI_CODING_MOST
},
73 { HTTP_URI_STATUS_OK
, "http://server/",
74 "http", "", "server", "/", 80, 0,
75 HTTP_URI_CODING_MOST
},
76 { HTTP_URI_STATUS_OK
, "http://username@server/",
77 "http", "username", "server", "/", 80, 0,
78 HTTP_URI_CODING_MOST
},
79 { HTTP_URI_STATUS_OK
, "http://username:passwor%64@server/",
80 "http", "username:password", "server", "/", 80, 0,
81 HTTP_URI_CODING_MOST
},
82 { HTTP_URI_STATUS_OK
, "http://username:passwor%64@server:8080/",
83 "http", "username:password", "server", "/", 8080, 8080,
84 HTTP_URI_CODING_MOST
},
85 { HTTP_URI_STATUS_OK
, "http://username:passwor%64@server:8080/directory/filename",
86 "http", "username:password", "server", "/directory/filename", 8080, 8080,
87 HTTP_URI_CODING_MOST
},
88 { HTTP_URI_STATUS_OK
, "http://[2000::10:100]:631/ipp",
89 "http", "", "2000::10:100", "/ipp", 631, 631,
90 HTTP_URI_CODING_MOST
},
91 { HTTP_URI_STATUS_OK
, "https://username:passwor%64@server/directory/filename",
92 "https", "username:password", "server", "/directory/filename", 443, 0,
93 HTTP_URI_CODING_MOST
},
94 { HTTP_URI_STATUS_OK
, "ipp://username:passwor%64@[::1]/ipp",
95 "ipp", "username:password", "::1", "/ipp", 631, 0,
96 HTTP_URI_CODING_MOST
},
97 { HTTP_URI_STATUS_OK
, "lpd://server/queue?reserve=yes",
98 "lpd", "", "server", "/queue?reserve=yes", 515, 0,
99 HTTP_URI_CODING_MOST
},
100 { HTTP_URI_STATUS_OK
, "mailto:user@domain.com",
101 "mailto", "", "", "user@domain.com", 0, 0,
102 HTTP_URI_CODING_MOST
},
103 { HTTP_URI_STATUS_OK
, "socket://server/",
104 "socket", "", "server", "/", 9100, 0,
105 HTTP_URI_CODING_MOST
},
106 { HTTP_URI_STATUS_OK
, "socket://192.168.1.1:9101/",
107 "socket", "", "192.168.1.1", "/", 9101, 9101,
108 HTTP_URI_CODING_MOST
},
109 { HTTP_URI_STATUS_OK
, "ipp://username:password@[v1.fe80::200:1234:5678:9abc+eth0]:999/ipp",
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
},
115 { HTTP_URI_STATUS_OK
, "http://server/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400",
116 "http", "", "server", "/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400", 80, 0,
117 HTTP_URI_CODING_MOST
},
118 { HTTP_URI_STATUS_OK
, "lpd://Acme%20Laser%20(01%3A23%3A45).local._tcp._printer/",
119 "lpd", "", "Acme Laser (01:23:45).local._tcp._printer", "/", 515, 0,
120 HTTP_URI_CODING_MOST
},
121 { HTTP_URI_STATUS_OK
, "ipp://HP%20Officejet%204500%20G510n-z%20%40%20Will's%20MacBook%20Pro%2015%22._ipp._tcp.local./",
122 "ipp", "", "HP Officejet 4500 G510n-z @ Will's MacBook Pro 15\"._ipp._tcp.local.", "/", 631, 0,
123 HTTP_URI_CODING_MOST
},
126 { HTTP_URI_STATUS_MISSING_SCHEME
, "/path/to/file/index.html",
127 "file", "", "", "/path/to/file/index.html", 0, 0,
128 HTTP_URI_CODING_MOST
},
129 { HTTP_URI_STATUS_MISSING_SCHEME
, "//server/ipp",
130 "ipp", "", "server", "/ipp", 631, 0,
131 HTTP_URI_CODING_MOST
},
134 { HTTP_URI_STATUS_UNKNOWN_SCHEME
, "vendor://server/resource",
135 "vendor", "", "server", "/resource", 0, 0,
136 HTTP_URI_CODING_MOST
},
138 /* Missing resource */
139 { HTTP_URI_STATUS_MISSING_RESOURCE
, "socket://[::192.168.2.1]",
140 "socket", "", "::192.168.2.1", "/", 9100, 0,
141 HTTP_URI_CODING_MOST
},
142 { HTTP_URI_STATUS_MISSING_RESOURCE
, "socket://192.168.1.1:9101",
143 "socket", "", "192.168.1.1", "/", 9101, 0,
144 HTTP_URI_CODING_MOST
},
147 { HTTP_URI_STATUS_BAD_URI
, "",
148 "", "", "", "", 0, 0,
149 HTTP_URI_CODING_MOST
},
152 { HTTP_URI_STATUS_BAD_SCHEME
, "bad_scheme://server/resource",
153 "", "", "", "", 0, 0,
154 HTTP_URI_CODING_MOST
},
157 { HTTP_URI_STATUS_BAD_USERNAME
, "http://username:passwor%6@server/resource",
158 "http", "", "", "", 80, 0,
159 HTTP_URI_CODING_MOST
},
162 { HTTP_URI_STATUS_BAD_HOSTNAME
, "http://[/::1]/index.html",
163 "http", "", "", "", 80, 0,
164 HTTP_URI_CODING_MOST
},
165 { HTTP_URI_STATUS_BAD_HOSTNAME
, "http://[",
166 "http", "", "", "", 80, 0,
167 HTTP_URI_CODING_MOST
},
168 { HTTP_URI_STATUS_BAD_HOSTNAME
, "http://serve%7/index.html",
169 "http", "", "", "", 80, 0,
170 HTTP_URI_CODING_MOST
},
171 { HTTP_URI_STATUS_BAD_HOSTNAME
, "http://server with spaces/index.html",
172 "http", "", "", "", 80, 0,
173 HTTP_URI_CODING_MOST
},
175 /* Bad port number */
176 { HTTP_URI_STATUS_BAD_PORT
, "http://127.0.0.1:9999a/index.html",
177 "http", "", "127.0.0.1", "", 0, 0,
178 HTTP_URI_CODING_MOST
},
181 { HTTP_URI_STATUS_BAD_RESOURCE
, "http://server/index.html%",
182 "http", "", "server", "", 80, 0,
183 HTTP_URI_CODING_MOST
},
184 { HTTP_URI_STATUS_BAD_RESOURCE
, "http://server/index with spaces.html",
185 "http", "", "server", "", 80, 0,
186 HTTP_URI_CODING_MOST
}
188 static const char * const base64_tests
[][2] =
193 /* 010000 010100 0010 */
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 */
206 * 'main()' - Main entry.
209 int /* O - Exit status */
210 main(int argc
, /* I - Number of command-line arguments */
211 char *argv
[]) /* I - Command-line arguments */
213 int i
, j
, k
; /* Looping vars */
214 http_t
*http
; /* HTTP connection */
215 http_encryption_t encryption
; /* Encryption type */
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 */
234 const char *encoding
; /* Negotiated Content-Encoding */
235 static const char * const uri_status_strings
[] =
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"
253 * Do API tests if we don't have a URL on the command-line...
261 * httpGetDateString()/httpGetDateTime()
264 fputs("httpGetDateString()/httpGetDateTime(): ", stdout
);
267 strlcpy(buffer
, httpGetDateString(start
), sizeof(buffer
));
268 current
= httpGetDateTime(buffer
);
270 i
= (int)(current
- start
);
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
));
289 * httpDecode64_2()/httpEncode64_2()
292 fputs("httpDecode64_2()/httpEncode64_2(): ", stdout
);
294 for (i
= 0, j
= 0; i
< (int)(sizeof(base64_tests
) / sizeof(base64_tests
[0])); i
++)
296 httpEncode64_2(encode
, sizeof(encode
), base64_tests
[i
][0],
297 (int)strlen(base64_tests
[i
][0]));
298 decodelen
= (int)sizeof(decode
);
299 httpDecode64_2(decode
, &decodelen
, base64_tests
[i
][1]);
301 if (strcmp(decode
, base64_tests
[i
][0]))
311 printf(" httpDecode64_2() returned \"%s\", expected \"%s\"...\n",
312 decode
, base64_tests
[i
][0]);
315 if (strcmp(encode
, base64_tests
[i
][1]))
325 printf(" httpEncode64_2() returned \"%s\", expected \"%s\"...\n",
326 encode
, base64_tests
[i
][1]);
337 fputs("httpGetHostname(): ", stdout
);
339 if (httpGetHostname(NULL
, hostname
, sizeof(hostname
)))
340 printf("PASS (%s)\n", hostname
);
351 printf("httpAddrGetList(%s): ", hostname
);
353 addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, NULL
);
356 for (i
= 0, addr
= addrlist
; addr
; i
++, addr
= addr
->next
)
358 char numeric
[1024]; /* Numeric IP address */
361 httpAddrString(&(addr
->addr
), numeric
, sizeof(numeric
));
362 if (!strcmp(numeric
, "UNKNOWN"))
367 printf("FAIL (bad address for %s)\n", hostname
);
369 printf("PASS (%d address(es) for %s)\n", i
, hostname
);
371 httpAddrFreeList(addrlist
);
373 else if (isdigit(hostname
[0] & 255))
375 puts("FAIL (ignored because hostname is numeric)");
384 * Test httpSeparateURI()...
387 fputs("httpSeparateURI(): ", stdout
);
388 for (i
= 0, j
= 0; i
< (int)(sizeof(uri_tests
) / sizeof(uri_tests
[0])); i
++)
390 uri_status
= httpSeparateURI(HTTP_URI_CODING_MOST
,
391 uri_tests
[i
].uri
, scheme
, sizeof(scheme
),
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
))
410 printf(" \"%s\":\n", uri_tests
[i
].uri
);
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]);
417 if (strcmp(scheme
, uri_tests
[i
].scheme
))
418 printf(" Scheme \"%s\" instead of \"%s\"\n",
419 scheme
, uri_tests
[i
].scheme
);
421 if (strcmp(username
, uri_tests
[i
].username
))
422 printf(" Username \"%s\" instead of \"%s\"\n",
423 username
, uri_tests
[i
].username
);
425 if (strcmp(hostname
, uri_tests
[i
].hostname
))
426 printf(" Hostname \"%s\" instead of \"%s\"\n",
427 hostname
, uri_tests
[i
].hostname
);
429 if (port
!= uri_tests
[i
].port
)
430 printf(" Port %d instead of %d\n",
431 port
, uri_tests
[i
].port
);
433 if (strcmp(resource
, uri_tests
[i
].resource
))
434 printf(" Resource \"%s\" instead of \"%s\"\n",
435 resource
, uri_tests
[i
].resource
);
440 printf("PASS (%d URIs tested)\n",
441 (int)(sizeof(uri_tests
) / sizeof(uri_tests
[0])));
444 * Test httpAssembleURI()...
447 fputs("httpAssembleURI(): ", stdout
);
448 for (i
= 0, j
= 0, k
= 0;
449 i
< (int)(sizeof(uri_tests
) / sizeof(uri_tests
[0]));
451 if (uri_tests
[i
].result
== HTTP_URI_STATUS_OK
&&
452 !strstr(uri_tests
[i
].uri
, "%64") &&
453 strstr(uri_tests
[i
].uri
, "//"))
456 uri_status
= httpAssembleURI(uri_tests
[i
].assemble_coding
,
457 buffer
, sizeof(buffer
),
459 uri_tests
[i
].username
,
460 uri_tests
[i
].hostname
,
461 uri_tests
[i
].assemble_port
,
462 uri_tests
[i
].resource
);
464 if (uri_status
!= HTTP_URI_STATUS_OK
)
474 printf(" \"%s\": %s\n", uri_tests
[i
].uri
,
475 uri_status_strings
[uri_status
+ 8]);
477 else if (strcmp(buffer
, uri_tests
[i
].uri
))
487 printf(" \"%s\": assembled = \"%s\"\n", uri_tests
[i
].uri
,
493 printf("PASS (%d URIs tested)\n", k
);
496 * Show a summary and return...
500 printf("\n%d TESTS FAILED!\n", failures
);
502 puts("\nALL TESTS PASSED!");
506 else if (strstr(argv
[1], "._tcp"))
509 * Test resolving an mDNS name.
512 char resolved
[1024]; /* Resolved URI */
515 printf("_httpResolveURI(%s, _HTTP_RESOLVE_DEFAULT): ", argv
[1]);
518 if (!_httpResolveURI(argv
[1], resolved
, sizeof(resolved
),
519 _HTTP_RESOLVE_DEFAULT
, NULL
, NULL
))
525 printf("PASS (%s)\n", resolved
);
527 printf("_httpResolveURI(%s, _HTTP_RESOLVE_FQDN): ", argv
[1]);
530 if (!_httpResolveURI(argv
[1], resolved
, sizeof(resolved
),
531 _HTTP_RESOLVE_FQDN
, NULL
, NULL
))
536 else if (strstr(resolved
, ".local:"))
538 printf("FAIL (%s)\n", resolved
);
543 printf("PASS (%s)\n", resolved
);
547 else if (!strcmp(argv
[1], "-u") && argc
== 3)
550 * Test URI separation...
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
);
568 * Test HTTP GET requests...
574 for (i
= 1; i
< argc
; i
++)
576 if (!strcmp(argv
[i
], "-o"))
582 out
= fopen(argv
[i
], "wb");
586 httpSeparateURI(HTTP_URI_CODING_MOST
, argv
[i
], scheme
, sizeof(scheme
),
587 username
, sizeof(username
),
588 hostname
, sizeof(hostname
), &port
,
589 resource
, sizeof(resource
));
591 if (!_cups_strcasecmp(scheme
, "https") || !_cups_strcasecmp(scheme
, "ipps") ||
593 encryption
= HTTP_ENCRYPTION_ALWAYS
;
595 encryption
= HTTP_ENCRYPTION_IF_REQUESTED
;
597 http
= httpConnect2(hostname
, port
, NULL
, AF_UNSPEC
, encryption
, 1, 30000,
604 printf("Checking file \"%s\"...\n", resource
);
605 httpClearFields(http
);
606 httpSetField(http
, HTTP_FIELD_ACCEPT_LANGUAGE
, "en");
607 httpHead(http
, resource
);
608 while ((status
= httpUpdate(http
)) == HTTP_STATUS_CONTINUE
);
610 if (status
== HTTP_STATUS_OK
)
613 printf("HEAD failed with status %d...\n", status
);
615 encoding
= httpGetContentEncoding(http
);
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
);
622 httpGet(http
, resource
);
623 while ((status
= httpUpdate(http
)) == HTTP_STATUS_CONTINUE
);
625 if (status
== HTTP_STATUS_OK
)
628 printf("GET failed with status %d...\n", status
);
631 length
= httpGetLength2(http
);
634 while ((bytes
= httpRead2(http
, buffer
, sizeof(buffer
))) > 0)
637 fwrite(buffer
, bytes
, 1, out
);
640 current
= time(NULL
);
641 if (current
== start
) current
++;
642 printf("\r" CUPS_LLFMT
"/" CUPS_LLFMT
" bytes ("
643 CUPS_LLFMT
" bytes/sec) ", CUPS_LLCAST total
,
644 CUPS_LLCAST length
, CUPS_LLCAST (total
/ (current
- start
)));
650 puts("Closing connection to server...");
661 * End of "$Id: testhttp.c 7742 2008-07-15 20:23:09Z mike $".