2 * "$Id: testhttp.c 6649 2007-07-11 21:46:42Z mike $"
4 * HTTP test program for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 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...
33 * Types and structures...
36 typedef struct uri_test_s
/**** URI test cases ****/
38 http_uri_status_t result
; /* Expected return value */
39 const char *uri
, /* URI */
40 *scheme
, /* Scheme string */
41 *username
, /* Username:password string */
42 *hostname
, /* Hostname string */
43 *resource
; /* Resource string */
44 int port
, /* Port number */
45 assemble_port
; /* Port number for httpAssembleURI() */
53 static uri_test_t uri_tests
[] = /* URI test data */
55 /* Start with valid URIs */
56 { HTTP_URI_OK
, "file:/filename",
57 "file", "", "", "/filename", 0, 0 },
58 { HTTP_URI_OK
, "file:/filename%20with%20spaces",
59 "file", "", "", "/filename with spaces", 0, 0 },
60 { HTTP_URI_OK
, "file:///filename",
61 "file", "", "", "/filename", 0, 0 },
62 { HTTP_URI_OK
, "file:///filename%20with%20spaces",
63 "file", "", "", "/filename with spaces", 0, 0 },
64 { HTTP_URI_OK
, "file://localhost/filename",
65 "file", "", "localhost", "/filename", 0, 0 },
66 { HTTP_URI_OK
, "file://localhost/filename%20with%20spaces",
67 "file", "", "localhost", "/filename with spaces", 0, 0 },
68 { HTTP_URI_OK
, "http://server/",
69 "http", "", "server", "/", 80, 0 },
70 { HTTP_URI_OK
, "http://username@server/",
71 "http", "username", "server", "/", 80, 0 },
72 { HTTP_URI_OK
, "http://username:passwor%64@server/",
73 "http", "username:password", "server", "/", 80, 0 },
74 { HTTP_URI_OK
, "http://username:passwor%64@server:8080/",
75 "http", "username:password", "server", "/", 8080, 8080 },
76 { HTTP_URI_OK
, "http://username:passwor%64@server:8080/directory/filename",
77 "http", "username:password", "server", "/directory/filename", 8080, 8080 },
78 { HTTP_URI_OK
, "http://[2000::10:100]:631/ipp",
79 "http", "", "2000::10:100", "/ipp", 631, 631 },
80 { HTTP_URI_OK
, "https://username:passwor%64@server/directory/filename",
81 "https", "username:password", "server", "/directory/filename", 443, 0 },
82 { HTTP_URI_OK
, "ipp://username:passwor%64@[::1]/ipp",
83 "ipp", "username:password", "::1", "/ipp", 631, 0 },
84 { HTTP_URI_OK
, "lpd://server/queue?reserve=yes",
85 "lpd", "", "server", "/queue?reserve=yes", 515, 0 },
86 { HTTP_URI_OK
, "mailto:user@domain.com",
87 "mailto", "", "", "user@domain.com", 0, 0 },
88 { HTTP_URI_OK
, "socket://server/",
89 "socket", "", "server", "/", 9100, 0 },
90 { HTTP_URI_OK
, "socket://192.168.1.1:9101/",
91 "socket", "", "192.168.1.1", "/", 9101, 9101 },
92 { HTTP_URI_OK
, "ipp://username:password@[v1.fe80::200:1234:5678:9abc+eth0]:999/ipp",
93 "ipp", "username:password", "fe80::200:1234:5678:9abc%eth0", "/ipp", 999, 999 },
94 { HTTP_URI_OK
, "http://server/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400",
95 "http", "", "server", "/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400", 80, 0 },
98 { HTTP_URI_MISSING_SCHEME
, "/path/to/file/index.html",
99 "file", "", "", "/path/to/file/index.html", 0, 0 },
100 { HTTP_URI_MISSING_SCHEME
, "//server/ipp",
101 "ipp", "", "server", "/ipp", 631, 0 },
104 { HTTP_URI_UNKNOWN_SCHEME
, "vendor://server/resource",
105 "vendor", "", "server", "/resource", 0, 0 },
107 /* Missing resource */
108 { HTTP_URI_MISSING_RESOURCE
, "socket://[::192.168.2.1]",
109 "socket", "", "::192.168.2.1", "/", 9100, 0 },
110 { HTTP_URI_MISSING_RESOURCE
, "socket://192.168.1.1:9101",
111 "socket", "", "192.168.1.1", "/", 9101 },
114 { HTTP_URI_BAD_URI
, "",
115 "", "", "", "", 0, 0 },
118 { HTTP_URI_BAD_SCHEME
, "bad_scheme://server/resource",
119 "", "", "", "", 0, 0 },
122 { HTTP_URI_BAD_USERNAME
, "http://username:passwor%6@server/resource",
123 "http", "", "", "", 80, 0 },
126 { HTTP_URI_BAD_HOSTNAME
, "http://[/::1]/index.html",
127 "http", "", "", "", 80, 0 },
128 { HTTP_URI_BAD_HOSTNAME
, "http://[",
129 "http", "", "", "", 80, 0 },
130 { HTTP_URI_BAD_HOSTNAME
, "http://serve%7/index.html",
131 "http", "", "", "", 80, 0 },
133 /* Bad port number */
134 { HTTP_URI_BAD_PORT
, "http://127.0.0.1:9999a/index.html",
135 "http", "", "127.0.0.1", "", 0, 0 },
138 { HTTP_URI_BAD_RESOURCE
, "http://server/index.html%",
139 "http", "", "server", "", 80, 0 }
141 static const char * const base64_tests
[][2] =
146 /* 010000 010100 0010 */
148 /* 010000 010100 001001 000011 */
149 { "ABCD", "QUJDRA==" },
150 /* 010000 010100 001001 000011 010001 00 */
151 { "ABCDE", "QUJDREU=" },
152 /* 010000 010100 001001 000011 010001 000100 0101 */
153 { "ABCDEF", "QUJDREVG" },
154 /* 010000 010100 001001 000011 010001 000100 010101 000110 */
159 * 'main()' - Main entry.
162 int /* O - Exit status */
163 main(int argc
, /* I - Number of command-line arguments */
164 char *argv
[]) /* I - Command-line arguments */
166 int i
, j
, k
; /* Looping vars */
167 http_t
*http
; /* HTTP connection */
168 http_status_t status
; /* Status of GET command */
169 int failures
; /* Number of test failures */
170 char buffer
[8192]; /* Input buffer */
171 long bytes
; /* Number of bytes read */
172 FILE *out
; /* Output file */
173 char encode
[256], /* Base64-encoded string */
174 decode
[256]; /* Base64-decoded string */
175 int decodelen
; /* Length of decoded string */
176 char scheme
[HTTP_MAX_URI
], /* Scheme from URI */
177 hostname
[HTTP_MAX_URI
], /* Hostname from URI */
178 username
[HTTP_MAX_URI
], /* Username:password from URI */
179 resource
[HTTP_MAX_URI
]; /* Resource from URI */
180 int port
; /* Port number from URI */
181 http_uri_status_t uri_status
; /* Status of URI separation */
182 http_addrlist_t
*addrlist
, /* Address list */
183 *addr
; /* Current address */
184 off_t length
, total
; /* Length and total bytes */
185 time_t start
, current
; /* Start and end time */
186 static const char * const uri_status_strings
[] =
189 "HTTP_URI_BAD_ARGUMENTS",
190 "HTTP_URI_BAD_RESOURCE",
192 "HTTP_URI_BAD_HOSTNAME",
193 "HTTP_URI_BAD_USERNAME",
194 "HTTP_URI_BAD_SCHEME",
197 "HTTP_URI_MISSING_SCHEME",
198 "HTTP_URI_UNKNOWN_SCHEME",
199 "HTTP_URI_MISSING_RESOURCE"
204 * Do API tests if we don't have a URL on the command-line...
212 * httpGetDateString()/httpGetDateTime()
215 fputs("httpGetDateString()/httpGetDateTime(): ", stdout
);
218 strcpy(buffer
, httpGetDateString(start
));
219 current
= httpGetDateTime(buffer
);
221 i
= (int)(current
- start
);
231 printf(" Difference is %d seconds, %02d:%02d:%02d...\n", i
, i
/ 3600,
232 (i
/ 60) % 60, i
% 60);
233 printf(" httpGetDateString(%d) returned \"%s\"\n", (int)start
, buffer
);
234 printf(" httpGetDateTime(\"%s\") returned %d\n", buffer
, (int)current
);
235 printf(" httpGetDateString(%d) returned \"%s\"\n", (int)current
,
236 httpGetDateString(current
));
240 * httpDecode64_2()/httpEncode64_2()
243 fputs("httpDecode64_2()/httpEncode64_2(): ", stdout
);
245 for (i
= 0, j
= 0; i
< (int)(sizeof(base64_tests
) / sizeof(base64_tests
[0])); i
++)
247 httpEncode64_2(encode
, sizeof(encode
), base64_tests
[i
][0],
248 strlen(base64_tests
[i
][0]));
249 decodelen
= (int)sizeof(decode
);
250 httpDecode64_2(decode
, &decodelen
, base64_tests
[i
][1]);
252 if (strcmp(decode
, base64_tests
[i
][0]))
262 printf(" httpDecode64_2() returned \"%s\", expected \"%s\"...\n",
263 decode
, base64_tests
[i
][0]);
266 if (strcmp(encode
, base64_tests
[i
][1]))
276 printf(" httpEncode64_2() returned \"%s\", expected \"%s\"...\n",
277 encode
, base64_tests
[i
][1]);
288 fputs("httpGetHostname(): ", stdout
);
290 if (httpGetHostname(NULL
, hostname
, sizeof(hostname
)))
291 printf("PASS (%s)\n", hostname
);
302 fputs("httpAddrGetList(): ", stdout
);
304 addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, NULL
);
307 for (i
= 0, addr
= addrlist
; addr
; i
++, addr
= addr
->next
)
309 char numeric
[1024]; /* Numeric IP address */
312 httpAddrString(&(addr
->addr
), numeric
, sizeof(numeric
));
313 if (!strcmp(numeric
, "UNKNOWN"))
318 printf("FAIL (bad address for %s)\n", hostname
);
320 printf("PASS (%d address(es) for %s)\n", i
, hostname
);
322 httpAddrFreeList(addrlist
);
331 * Test httpSeparateURI()...
334 fputs("httpSeparateURI(): ", stdout
);
335 for (i
= 0, j
= 0; i
< (int)(sizeof(uri_tests
) / sizeof(uri_tests
[0])); i
++)
337 uri_status
= httpSeparateURI(HTTP_URI_CODING_MOST
,
338 uri_tests
[i
].uri
, scheme
, sizeof(scheme
),
339 username
, sizeof(username
),
340 hostname
, sizeof(hostname
), &port
,
341 resource
, sizeof(resource
));
342 if (uri_status
!= uri_tests
[i
].result
||
343 strcmp(scheme
, uri_tests
[i
].scheme
) ||
344 strcmp(username
, uri_tests
[i
].username
) ||
345 strcmp(hostname
, uri_tests
[i
].hostname
) ||
346 port
!= uri_tests
[i
].port
||
347 strcmp(resource
, uri_tests
[i
].resource
))
357 printf(" \"%s\":\n", uri_tests
[i
].uri
);
359 if (uri_status
!= uri_tests
[i
].result
)
360 printf(" Returned %s instead of %s\n",
361 uri_status_strings
[uri_status
+ 8],
362 uri_status_strings
[uri_tests
[i
].result
+ 8]);
364 if (strcmp(scheme
, uri_tests
[i
].scheme
))
365 printf(" Scheme \"%s\" instead of \"%s\"\n",
366 scheme
, uri_tests
[i
].scheme
);
368 if (strcmp(username
, uri_tests
[i
].username
))
369 printf(" Username \"%s\" instead of \"%s\"\n",
370 username
, uri_tests
[i
].username
);
372 if (strcmp(hostname
, uri_tests
[i
].hostname
))
373 printf(" Hostname \"%s\" instead of \"%s\"\n",
374 hostname
, uri_tests
[i
].hostname
);
376 if (port
!= uri_tests
[i
].port
)
377 printf(" Port %d instead of %d\n",
378 port
, uri_tests
[i
].port
);
380 if (strcmp(resource
, uri_tests
[i
].resource
))
381 printf(" Resource \"%s\" instead of \"%s\"\n",
382 resource
, uri_tests
[i
].resource
);
387 printf("PASS (%d URIs tested)\n",
388 (int)(sizeof(uri_tests
) / sizeof(uri_tests
[0])));
391 * Test httpAssembleURI()...
394 fputs("httpAssembleURI(): ", stdout
);
395 for (i
= 0, j
= 0, k
= 0;
396 i
< (int)(sizeof(uri_tests
) / sizeof(uri_tests
[0]));
398 if (uri_tests
[i
].result
== HTTP_URI_OK
&&
399 !strstr(uri_tests
[i
].uri
, "%64") &&
400 strstr(uri_tests
[i
].uri
, "//"))
403 uri_status
= httpAssembleURI(HTTP_URI_CODING_MOST
,
404 buffer
, sizeof(buffer
),
406 uri_tests
[i
].username
,
407 uri_tests
[i
].hostname
,
408 uri_tests
[i
].assemble_port
,
409 uri_tests
[i
].resource
);
411 if (uri_status
!= HTTP_URI_OK
)
421 printf(" \"%s\": %s\n", uri_tests
[i
].uri
,
422 uri_status_strings
[uri_status
+ 8]);
424 else if (strcmp(buffer
, uri_tests
[i
].uri
))
434 printf(" \"%s\": assembled = \"%s\"\n", uri_tests
[i
].uri
,
440 printf("PASS (%d URIs tested)\n", k
);
443 * Show a summary and return...
447 printf("\n%d TESTS FAILED!\n", failures
);
449 puts("\nALL TESTS PASSED!");
455 * Test HTTP GET requests...
461 for (i
= 1; i
< argc
; i
++)
463 if (!strcmp(argv
[i
], "-o"))
469 out
= fopen(argv
[i
], "wb");
473 httpSeparateURI(HTTP_URI_CODING_MOST
, argv
[i
], scheme
, sizeof(scheme
),
474 username
, sizeof(username
),
475 hostname
, sizeof(hostname
), &port
,
476 resource
, sizeof(resource
));
478 http
= httpConnectEncrypt(hostname
, port
, HTTP_ENCRYPT_IF_REQUESTED
);
484 printf("Requesting file \"%s\"...\n", resource
);
485 httpClearFields(http
);
486 httpSetField(http
, HTTP_FIELD_ACCEPT_LANGUAGE
, "en");
487 httpGet(http
, resource
);
488 while ((status
= httpUpdate(http
)) == HTTP_CONTINUE
);
490 if (status
== HTTP_OK
)
493 printf("GET failed with status %d...\n", status
);
497 length
= httpGetLength2(http
);
500 while ((bytes
= httpRead2(http
, buffer
, sizeof(buffer
))) > 0)
503 fwrite(buffer
, bytes
, 1, out
);
506 current
= time(NULL
);
507 if (current
== start
) current
++;
508 printf("\r" CUPS_LLFMT
"/" CUPS_LLFMT
" bytes ("
509 CUPS_LLFMT
" bytes/sec) ", CUPS_LLCAST total
,
510 CUPS_LLCAST length
, CUPS_LLCAST (total
/ (current
- start
)));
516 puts("Closing connection to server...");
527 * End of "$Id: testhttp.c 6649 2007-07-11 21:46:42Z mike $".