2 * "$Id: testhttp.c 6003 2006-10-02 16:26:04Z mike $"
4 * HTTP test program for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * main() - Main entry.
32 * Include necessary headers...
42 * Types and structures...
45 typedef struct uri_test_s
/**** URI test cases ****/
47 http_uri_status_t result
; /* Expected return value */
48 const char *uri
, /* URI */
49 *scheme
, /* Scheme string */
50 *username
, /* Username:password string */
51 *hostname
, /* Hostname string */
52 *resource
; /* Resource string */
53 int port
, /* Port number */
54 assemble_port
; /* Port number for httpAssembleURI() */
62 static uri_test_t uri_tests
[] = /* URI test data */
64 /* Start with valid URIs */
65 { HTTP_URI_OK
, "file:/filename",
66 "file", "", "", "/filename", 0, 0 },
67 { HTTP_URI_OK
, "file:/filename%20with%20spaces",
68 "file", "", "", "/filename with spaces", 0, 0 },
69 { HTTP_URI_OK
, "file:///filename",
70 "file", "", "", "/filename", 0, 0 },
71 { HTTP_URI_OK
, "file:///filename%20with%20spaces",
72 "file", "", "", "/filename with spaces", 0, 0 },
73 { HTTP_URI_OK
, "file://localhost/filename",
74 "file", "", "localhost", "/filename", 0, 0 },
75 { HTTP_URI_OK
, "file://localhost/filename%20with%20spaces",
76 "file", "", "localhost", "/filename with spaces", 0, 0 },
77 { HTTP_URI_OK
, "http://server/",
78 "http", "", "server", "/", 80, 0 },
79 { HTTP_URI_OK
, "http://username@server/",
80 "http", "username", "server", "/", 80, 0 },
81 { HTTP_URI_OK
, "http://username:passwor%64@server/",
82 "http", "username:password", "server", "/", 80, 0 },
83 { HTTP_URI_OK
, "http://username:passwor%64@server:8080/",
84 "http", "username:password", "server", "/", 8080, 8080 },
85 { HTTP_URI_OK
, "http://username:passwor%64@server:8080/directory/filename",
86 "http", "username:password", "server", "/directory/filename", 8080, 8080 },
87 { HTTP_URI_OK
, "http://[2000::10:100]:631/ipp",
88 "http", "", "2000::10:100", "/ipp", 631, 631 },
89 { HTTP_URI_OK
, "https://username:passwor%64@server/directory/filename",
90 "https", "username:password", "server", "/directory/filename", 443, 0 },
91 { HTTP_URI_OK
, "ipp://username:passwor%64@[::1]/ipp",
92 "ipp", "username:password", "::1", "/ipp", 631, 0 },
93 { HTTP_URI_OK
, "lpd://server/queue?reserve=yes",
94 "lpd", "", "server", "/queue?reserve=yes", 515, 0 },
95 { HTTP_URI_OK
, "mailto:user@domain.com",
96 "mailto", "", "", "user@domain.com", 0, 0 },
97 { HTTP_URI_OK
, "socket://server/",
98 "socket", "", "server", "/", 9100, 0 },
99 { HTTP_URI_OK
, "socket://192.168.1.1:9101/",
100 "socket", "", "192.168.1.1", "/", 9101, 9101 },
101 { HTTP_URI_OK
, "ipp://username:password@[v1.fe80::200:1234:5678:9abc+eth0]:999/ipp",
102 "ipp", "username:password", "fe80::200:1234:5678:9abc%eth0", "/ipp", 999, 999 },
103 { HTTP_URI_OK
, "http://server/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400",
104 "http", "", "server", "/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400", 80, 0 },
107 { HTTP_URI_MISSING_SCHEME
, "/path/to/file/index.html",
108 "file", "", "", "/path/to/file/index.html", 0, 0 },
109 { HTTP_URI_MISSING_SCHEME
, "//server/ipp",
110 "ipp", "", "server", "/ipp", 631, 0 },
113 { HTTP_URI_UNKNOWN_SCHEME
, "vendor://server/resource",
114 "vendor", "", "server", "/resource", 0, 0 },
116 /* Missing resource */
117 { HTTP_URI_MISSING_RESOURCE
, "socket://[::192.168.2.1]",
118 "socket", "", "::192.168.2.1", "/", 9100, 0 },
119 { HTTP_URI_MISSING_RESOURCE
, "socket://192.168.1.1:9101",
120 "socket", "", "192.168.1.1", "/", 9101 },
123 { HTTP_URI_BAD_URI
, "",
124 "", "", "", "", 0, 0 },
127 { HTTP_URI_BAD_SCHEME
, "bad_scheme://server/resource",
128 "", "", "", "", 0, 0 },
131 { HTTP_URI_BAD_USERNAME
, "http://username:passwor%6@server/resource",
132 "http", "", "", "", 80, 0 },
135 { HTTP_URI_BAD_HOSTNAME
, "http://[/::1]/index.html",
136 "http", "", "", "", 80, 0 },
137 { HTTP_URI_BAD_HOSTNAME
, "http://[",
138 "http", "", "", "", 80, 0 },
139 { HTTP_URI_BAD_HOSTNAME
, "http://serve%7/index.html",
140 "http", "", "", "", 80, 0 },
142 /* Bad port number */
143 { HTTP_URI_BAD_PORT
, "http://127.0.0.1:9999a/index.html",
144 "http", "", "127.0.0.1", "", 0, 0 },
147 { HTTP_URI_BAD_RESOURCE
, "http://server/index.html%",
148 "http", "", "server", "", 80, 0 }
150 static const char * const base64_tests
[][2] =
155 /* 010000 010100 0010 */
157 /* 010000 010100 001001 000011 */
158 { "ABCD", "QUJDRA==" },
159 /* 010000 010100 001001 000011 010001 00 */
160 { "ABCDE", "QUJDREU=" },
161 /* 010000 010100 001001 000011 010001 000100 0101 */
162 { "ABCDEF", "QUJDREVG" },
163 /* 010000 010100 001001 000011 010001 000100 010101 000110 */
168 * 'main()' - Main entry.
171 int /* O - Exit status */
172 main(int argc
, /* I - Number of command-line arguments */
173 char *argv
[]) /* I - Command-line arguments */
175 int i
, j
, k
; /* Looping vars */
176 http_t
*http
; /* HTTP connection */
177 http_status_t status
; /* Status of GET command */
178 int failures
; /* Number of test failures */
179 char buffer
[8192]; /* Input buffer */
180 long bytes
; /* Number of bytes read */
181 FILE *out
; /* Output file */
182 char encode
[256], /* Base64-encoded string */
183 decode
[256]; /* Base64-decoded string */
184 int decodelen
; /* Length of decoded string */
185 char scheme
[HTTP_MAX_URI
], /* Scheme from URI */
186 hostname
[HTTP_MAX_URI
], /* Hostname from URI */
187 username
[HTTP_MAX_URI
], /* Username:password from URI */
188 resource
[HTTP_MAX_URI
]; /* Resource from URI */
189 int port
; /* Port number from URI */
190 http_uri_status_t uri_status
; /* Status of URI separation */
191 http_addrlist_t
*addrlist
, /* Address list */
192 *addr
; /* Current address */
193 off_t length
, total
; /* Length and total bytes */
194 time_t start
, current
; /* Start and end time */
195 static const char * const uri_status_strings
[] =
198 "HTTP_URI_BAD_ARGUMENTS",
199 "HTTP_URI_BAD_RESOURCE",
201 "HTTP_URI_BAD_HOSTNAME",
202 "HTTP_URI_BAD_USERNAME",
203 "HTTP_URI_BAD_SCHEME",
206 "HTTP_URI_MISSING_SCHEME",
207 "HTTP_URI_UNKNOWN_SCHEME",
208 "HTTP_URI_MISSING_RESOURCE"
213 * Do API tests if we don't have a URL on the command-line...
221 * httpGetDateString()/httpGetDateTime()
224 fputs("httpGetDateString()/httpGetDateTime(): ", stdout
);
227 strcpy(buffer
, httpGetDateString(start
));
228 current
= httpGetDateTime(buffer
);
230 i
= (int)(current
- start
);
240 printf(" Difference is %d seconds, %02d:%02d:%02d...\n", i
, i
/ 3600,
241 (i
/ 60) % 60, i
% 60);
242 printf(" httpGetDateString(%d) returned \"%s\"\n", (int)start
, buffer
);
243 printf(" httpGetDateTime(\"%s\") returned %d\n", buffer
, (int)current
);
244 printf(" httpGetDateString(%d) returned \"%s\"\n", (int)current
,
245 httpGetDateString(current
));
249 * httpDecode64_2()/httpEncode64_2()
252 fputs("httpDecode64_2()/httpEncode64_2(): ", stdout
);
254 for (i
= 0, j
= 0; i
< (int)(sizeof(base64_tests
) / sizeof(base64_tests
[0])); i
++)
256 httpEncode64_2(encode
, sizeof(encode
), base64_tests
[i
][0],
257 strlen(base64_tests
[i
][0]));
258 decodelen
= (int)sizeof(decode
);
259 httpDecode64_2(decode
, &decodelen
, base64_tests
[i
][1]);
261 if (strcmp(decode
, base64_tests
[i
][0]))
271 printf(" httpDecode64_2() returned \"%s\", expected \"%s\"...\n",
272 decode
, base64_tests
[i
][0]);
275 if (strcmp(encode
, base64_tests
[i
][1]))
285 printf(" httpEncode64_2() returned \"%s\", expected \"%s\"...\n",
286 encode
, base64_tests
[i
][1]);
297 fputs("httpGetHostname(): ", stdout
);
299 if (httpGetHostname(NULL
, hostname
, sizeof(hostname
)))
300 printf("PASS (%s)\n", hostname
);
311 fputs("httpAddrGetList(): ", stdout
);
313 addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, NULL
);
316 for (i
= 0, addr
= addrlist
; addr
; i
++, addr
= addr
->next
)
318 char numeric
[1024]; /* Numeric IP address */
321 httpAddrString(&(addr
->addr
), numeric
, sizeof(numeric
));
322 if (!strcmp(numeric
, "UNKNOWN"))
327 printf("FAIL (bad address for %s)\n", hostname
);
329 printf("PASS (%d address(es) for %s)\n", i
, hostname
);
331 httpAddrFreeList(addrlist
);
340 * Test httpSeparateURI()...
343 fputs("httpSeparateURI(): ", stdout
);
344 for (i
= 0, j
= 0; i
< (int)(sizeof(uri_tests
) / sizeof(uri_tests
[0])); i
++)
346 uri_status
= httpSeparateURI(HTTP_URI_CODING_MOST
,
347 uri_tests
[i
].uri
, scheme
, sizeof(scheme
),
348 username
, sizeof(username
),
349 hostname
, sizeof(hostname
), &port
,
350 resource
, sizeof(resource
));
351 if (uri_status
!= uri_tests
[i
].result
||
352 strcmp(scheme
, uri_tests
[i
].scheme
) ||
353 strcmp(username
, uri_tests
[i
].username
) ||
354 strcmp(hostname
, uri_tests
[i
].hostname
) ||
355 port
!= uri_tests
[i
].port
||
356 strcmp(resource
, uri_tests
[i
].resource
))
366 printf(" \"%s\":\n", uri_tests
[i
].uri
);
368 if (uri_status
!= uri_tests
[i
].result
)
369 printf(" Returned %s instead of %s\n",
370 uri_status_strings
[uri_status
+ 8],
371 uri_status_strings
[uri_tests
[i
].result
+ 8]);
373 if (strcmp(scheme
, uri_tests
[i
].scheme
))
374 printf(" Scheme \"%s\" instead of \"%s\"\n",
375 scheme
, uri_tests
[i
].scheme
);
377 if (strcmp(username
, uri_tests
[i
].username
))
378 printf(" Username \"%s\" instead of \"%s\"\n",
379 username
, uri_tests
[i
].username
);
381 if (strcmp(hostname
, uri_tests
[i
].hostname
))
382 printf(" Hostname \"%s\" instead of \"%s\"\n",
383 hostname
, uri_tests
[i
].hostname
);
385 if (port
!= uri_tests
[i
].port
)
386 printf(" Port %d instead of %d\n",
387 port
, uri_tests
[i
].port
);
389 if (strcmp(resource
, uri_tests
[i
].resource
))
390 printf(" Resource \"%s\" instead of \"%s\"\n",
391 resource
, uri_tests
[i
].resource
);
396 printf("PASS (%d URIs tested)\n",
397 (int)(sizeof(uri_tests
) / sizeof(uri_tests
[0])));
400 * Test httpAssembleURI()...
403 fputs("httpAssembleURI(): ", stdout
);
404 for (i
= 0, j
= 0, k
= 0;
405 i
< (int)(sizeof(uri_tests
) / sizeof(uri_tests
[0]));
407 if (uri_tests
[i
].result
== HTTP_URI_OK
&&
408 !strstr(uri_tests
[i
].uri
, "%64") &&
409 strstr(uri_tests
[i
].uri
, "//"))
412 uri_status
= httpAssembleURI(HTTP_URI_CODING_MOST
,
413 buffer
, sizeof(buffer
),
415 uri_tests
[i
].username
,
416 uri_tests
[i
].hostname
,
417 uri_tests
[i
].assemble_port
,
418 uri_tests
[i
].resource
);
420 if (uri_status
!= HTTP_URI_OK
)
430 printf(" \"%s\": %s\n", uri_tests
[i
].uri
,
431 uri_status_strings
[uri_status
+ 8]);
433 else if (strcmp(buffer
, uri_tests
[i
].uri
))
443 printf(" \"%s\": assembled = \"%s\"\n", uri_tests
[i
].uri
,
449 printf("PASS (%d URIs tested)\n", k
);
452 * Show a summary and return...
456 printf("\n%d TESTS FAILED!\n", failures
);
458 puts("\nALL TESTS PASSED!");
464 * Test HTTP GET requests...
470 for (i
= 1; i
< argc
; i
++)
472 if (!strcmp(argv
[i
], "-o"))
478 out
= fopen(argv
[i
], "wb");
482 httpSeparateURI(HTTP_URI_CODING_MOST
, argv
[i
], scheme
, sizeof(scheme
),
483 username
, sizeof(username
),
484 hostname
, sizeof(hostname
), &port
,
485 resource
, sizeof(resource
));
487 http
= httpConnectEncrypt(hostname
, port
, HTTP_ENCRYPT_IF_REQUESTED
);
493 printf("Requesting file \"%s\"...\n", resource
);
494 httpClearFields(http
);
495 httpSetField(http
, HTTP_FIELD_ACCEPT_LANGUAGE
, "en");
496 httpGet(http
, resource
);
497 while ((status
= httpUpdate(http
)) == HTTP_CONTINUE
);
499 if (status
== HTTP_OK
)
502 printf("GET failed with status %d...\n", status
);
506 length
= httpGetLength2(http
);
509 while ((bytes
= httpRead2(http
, buffer
, sizeof(buffer
))) > 0)
512 fwrite(buffer
, bytes
, 1, out
);
515 current
= time(NULL
);
516 if (current
== start
) current
++;
517 printf("\r" CUPS_LLFMT
"/" CUPS_LLFMT
" bytes ("
518 CUPS_LLFMT
" bytes/sec) ", CUPS_LLCAST total
,
519 CUPS_LLCAST length
, CUPS_LLCAST (total
/ (current
- start
)));
525 puts("Closing connection to server...");
536 * End of "$Id: testhttp.c 6003 2006-10-02 16:26:04Z mike $".