2 * "$Id: testhttp.c 181 2006-06-22 20:01:18Z jlovell $"
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
, "https://username:passwor%64@server/directory/filename",
88 "https", "username:password", "server", "/directory/filename", 443, 0 },
89 { HTTP_URI_OK
, "ipp://username:passwor%64@[::1]/ipp",
90 "ipp", "username:password", "::1", "/ipp", 631, 0 },
91 { HTTP_URI_OK
, "lpd://server/queue?reserve=yes",
92 "lpd", "", "server", "/queue?reserve=yes", 515, 0 },
93 { HTTP_URI_OK
, "mailto:user@domain.com",
94 "mailto", "", "", "user@domain.com", 0, 0 },
95 { HTTP_URI_OK
, "socket://server/",
96 "socket", "", "server", "/", 9100, 0 },
97 { HTTP_URI_OK
, "socket://192.168.1.1:9101/",
98 "socket", "", "192.168.1.1", "/", 9101, 9101 },
99 { HTTP_URI_OK
, "ipp://username:password@[v1.fe80::200:1234:5678:9abc+eth0]:999/ipp",
100 "ipp", "username:password", "fe80::200:1234:5678:9abc%eth0", "/ipp", 999, 999 },
101 { HTTP_URI_OK
, "http://server/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400",
102 "http", "", "server", "/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400", 80, 0 },
105 { HTTP_URI_MISSING_SCHEME
, "/path/to/file/index.html",
106 "file", "", "", "/path/to/file/index.html", 0, 0 },
107 { HTTP_URI_MISSING_SCHEME
, "//server/ipp",
108 "ipp", "", "server", "/ipp", 631, 0 },
111 { HTTP_URI_UNKNOWN_SCHEME
, "vendor://server/resource",
112 "vendor", "", "server", "/resource", 0, 0 },
114 /* Missing resource */
115 { HTTP_URI_MISSING_RESOURCE
, "socket://[::192.168.2.1]",
116 "socket", "", "::192.168.2.1", "/", 9100, 0 },
117 { HTTP_URI_MISSING_RESOURCE
, "socket://192.168.1.1:9101",
118 "socket", "", "192.168.1.1", "/", 9101 },
121 { HTTP_URI_BAD_URI
, "",
122 "", "", "", "", 0, 0 },
125 { HTTP_URI_BAD_SCHEME
, "bad_scheme://server/resource",
126 "", "", "", "", 0, 0 },
129 { HTTP_URI_BAD_USERNAME
, "http://username:passwor%6@server/resource",
130 "http", "", "", "", 80, 0 },
133 { HTTP_URI_BAD_HOSTNAME
, "http://[/::1]/index.html",
134 "http", "", "", "", 80, 0 },
135 { HTTP_URI_BAD_HOSTNAME
, "http://[",
136 "http", "", "", "", 80, 0 },
137 { HTTP_URI_BAD_HOSTNAME
, "http://serve%7/index.html",
138 "http", "", "", "", 80, 0 },
140 /* Bad port number */
141 { HTTP_URI_BAD_PORT
, "http://127.0.0.1:9999a/index.html",
142 "http", "", "127.0.0.1", "", 0, 0 },
145 { HTTP_URI_BAD_RESOURCE
, "http://server/index.html%",
146 "http", "", "server", "", 80, 0 }
148 static const char * const base64_tests
[][2] =
153 /* 010000 010100 0010 */
155 /* 010000 010100 001001 000011 */
156 { "ABCD", "QUJDRA==" },
157 /* 010000 010100 001001 000011 010001 00 */
158 { "ABCDE", "QUJDREU=" },
159 /* 010000 010100 001001 000011 010001 000100 0101 */
160 { "ABCDEF", "QUJDREVG" },
161 /* 010000 010100 001001 000011 010001 000100 010101 000110 */
166 * 'main()' - Main entry.
169 int /* O - Exit status */
170 main(int argc
, /* I - Number of command-line arguments */
171 char *argv
[]) /* I - Command-line arguments */
173 int i
, j
, k
; /* Looping vars */
174 http_t
*http
; /* HTTP connection */
175 http_status_t status
; /* Status of GET command */
176 int failures
; /* Number of test failures */
177 char buffer
[8192]; /* Input buffer */
178 long bytes
; /* Number of bytes read */
179 FILE *out
; /* Output file */
180 char encode
[256], /* Base64-encoded string */
181 decode
[256]; /* Base64-decoded string */
182 int decodelen
; /* Length of decoded string */
183 char scheme
[HTTP_MAX_URI
], /* Scheme from URI */
184 hostname
[HTTP_MAX_URI
], /* Hostname from URI */
185 username
[HTTP_MAX_URI
], /* Username:password from URI */
186 resource
[HTTP_MAX_URI
]; /* Resource from URI */
187 int port
; /* Port number from URI */
188 http_uri_status_t uri_status
; /* Status of URI separation */
189 http_addrlist_t
*addrlist
, /* Address list */
190 *addr
; /* Current address */
191 off_t length
, total
; /* Length and total bytes */
192 time_t start
, current
; /* Start and end time */
193 static const char * const uri_status_strings
[] =
196 "HTTP_URI_BAD_ARGUMENTS",
197 "HTTP_URI_BAD_RESOURCE",
199 "HTTP_URI_BAD_HOSTNAME",
200 "HTTP_URI_BAD_USERNAME",
201 "HTTP_URI_BAD_SCHEME",
204 "HTTP_URI_MISSING_SCHEME",
205 "HTTP_URI_UNKNOWN_SCHEME",
206 "HTTP_URI_MISSING_RESOURCE"
211 * Do API tests if we don't have a URL on the command-line...
219 * httpGetDateString()/httpGetDateTime()
222 fputs("httpGetDateString()/httpGetDateTime(): ", stdout
);
225 strcpy(buffer
, httpGetDateString(start
));
226 current
= httpGetDateTime(buffer
);
228 i
= (int)(current
- start
);
238 printf(" Difference is %d seconds, %02d:%02d:%02d...\n", i
, i
/ 3600,
239 (i
/ 60) % 60, i
% 60);
240 printf(" httpGetDateString(%d) returned \"%s\"\n", (int)start
, buffer
);
241 printf(" httpGetDateTime(\"%s\") returned %d\n", buffer
, (int)current
);
242 printf(" httpGetDateString(%d) returned \"%s\"\n", (int)current
,
243 httpGetDateString(current
));
247 * httpDecode64_2()/httpEncode64_2()
250 fputs("httpDecode64_2()/httpEncode64_2(): ", stdout
);
252 for (i
= 0, j
= 0; i
< (int)(sizeof(base64_tests
) / sizeof(base64_tests
[0])); i
++)
254 httpEncode64_2(encode
, sizeof(encode
), base64_tests
[i
][0],
255 strlen(base64_tests
[i
][0]));
256 decodelen
= (int)sizeof(decode
);
257 httpDecode64_2(decode
, &decodelen
, base64_tests
[i
][1]);
259 if (strcmp(decode
, base64_tests
[i
][0]))
269 printf(" httpDecode64_2() returned \"%s\", expected \"%s\"...\n",
270 decode
, base64_tests
[i
][0]);
273 if (strcmp(encode
, base64_tests
[i
][1]))
283 printf(" httpEncode64_2() returned \"%s\", expected \"%s\"...\n",
284 encode
, base64_tests
[i
][1]);
295 fputs("httpGetHostname(): ", stdout
);
297 if (httpGetHostname(NULL
, hostname
, sizeof(hostname
)))
298 printf("PASS (%s)\n", hostname
);
309 fputs("httpAddrGetList(): ", stdout
);
311 addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, NULL
);
314 for (i
= 0, addr
= addrlist
; addr
; i
++, addr
= addr
->next
)
316 char numeric
[1024]; /* Numeric IP address */
319 httpAddrString(&(addr
->addr
), numeric
, sizeof(numeric
));
320 if (!strcmp(numeric
, "UNKNOWN"))
325 printf("FAIL (bad address for %s)\n", hostname
);
327 printf("PASS (%d address(es) for %s)\n", i
, hostname
);
329 httpAddrFreeList(addrlist
);
338 * Test httpSeparateURI()...
341 fputs("httpSeparateURI(): ", stdout
);
342 for (i
= 0, j
= 0; i
< (int)(sizeof(uri_tests
) / sizeof(uri_tests
[0])); i
++)
344 uri_status
= httpSeparateURI(HTTP_URI_CODING_MOST
,
345 uri_tests
[i
].uri
, scheme
, sizeof(scheme
),
346 username
, sizeof(username
),
347 hostname
, sizeof(hostname
), &port
,
348 resource
, sizeof(resource
));
349 if (uri_status
!= uri_tests
[i
].result
||
350 strcmp(scheme
, uri_tests
[i
].scheme
) ||
351 strcmp(username
, uri_tests
[i
].username
) ||
352 strcmp(hostname
, uri_tests
[i
].hostname
) ||
353 port
!= uri_tests
[i
].port
||
354 strcmp(resource
, uri_tests
[i
].resource
))
364 printf(" \"%s\":\n", uri_tests
[i
].uri
);
366 if (uri_status
!= uri_tests
[i
].result
)
367 printf(" Returned %s instead of %s\n",
368 uri_status_strings
[uri_status
+ 8],
369 uri_status_strings
[uri_tests
[i
].result
+ 8]);
371 if (strcmp(scheme
, uri_tests
[i
].scheme
))
372 printf(" Scheme \"%s\" instead of \"%s\"\n",
373 scheme
, uri_tests
[i
].scheme
);
375 if (strcmp(username
, uri_tests
[i
].username
))
376 printf(" Username \"%s\" instead of \"%s\"\n",
377 username
, uri_tests
[i
].username
);
379 if (strcmp(hostname
, uri_tests
[i
].hostname
))
380 printf(" Hostname \"%s\" instead of \"%s\"\n",
381 hostname
, uri_tests
[i
].hostname
);
383 if (port
!= uri_tests
[i
].port
)
384 printf(" Port %d instead of %d\n",
385 port
, uri_tests
[i
].port
);
387 if (strcmp(resource
, uri_tests
[i
].resource
))
388 printf(" Resource \"%s\" instead of \"%s\"\n",
389 resource
, uri_tests
[i
].resource
);
394 printf("PASS (%d URIs tested)\n",
395 (int)(sizeof(uri_tests
) / sizeof(uri_tests
[0])));
398 * Test httpAssembleURI()...
401 fputs("httpAssembleURI(): ", stdout
);
402 for (i
= 0, j
= 0, k
= 0;
403 i
< (int)(sizeof(uri_tests
) / sizeof(uri_tests
[0]));
405 if (uri_tests
[i
].result
== HTTP_URI_OK
&&
406 !strstr(uri_tests
[i
].uri
, "%64") &&
407 strstr(uri_tests
[i
].uri
, "//"))
410 uri_status
= httpAssembleURI(HTTP_URI_CODING_MOST
,
411 buffer
, sizeof(buffer
),
413 uri_tests
[i
].username
,
414 uri_tests
[i
].hostname
,
415 uri_tests
[i
].assemble_port
,
416 uri_tests
[i
].resource
);
418 if (uri_status
!= HTTP_URI_OK
)
428 printf(" \"%s\": %s\n", uri_tests
[i
].uri
,
429 uri_status_strings
[uri_status
+ 8]);
431 else if (strcmp(buffer
, uri_tests
[i
].uri
))
441 printf(" \"%s\": assembled = \"%s\"\n", uri_tests
[i
].uri
,
447 printf("PASS (%d URIs tested)\n", k
);
450 * Show a summary and return...
454 printf("\n%d TESTS FAILED!\n", failures
);
456 puts("\nALL TESTS PASSED!");
462 * Test HTTP GET requests...
468 for (i
= 1; i
< argc
; i
++)
470 if (!strcmp(argv
[i
], "-o"))
476 out
= fopen(argv
[i
], "wb");
480 httpSeparateURI(HTTP_URI_CODING_MOST
, argv
[i
], scheme
, sizeof(scheme
),
481 username
, sizeof(username
),
482 hostname
, sizeof(hostname
), &port
,
483 resource
, sizeof(resource
));
485 http
= httpConnectEncrypt(hostname
, port
, HTTP_ENCRYPT_IF_REQUESTED
);
491 printf("Requesting file \"%s\"...\n", resource
);
492 httpClearFields(http
);
493 httpSetField(http
, HTTP_FIELD_ACCEPT_LANGUAGE
, "en");
494 httpGet(http
, resource
);
495 while ((status
= httpUpdate(http
)) == HTTP_CONTINUE
);
497 if (status
== HTTP_OK
)
500 printf("GET failed with status %d...\n", status
);
504 length
= httpGetLength2(http
);
507 while ((bytes
= httpRead2(http
, buffer
, sizeof(buffer
))) > 0)
510 fwrite(buffer
, bytes
, 1, out
);
513 current
= time(NULL
);
514 if (current
== start
) current
++;
515 printf("\r" CUPS_LLFMT
"/" CUPS_LLFMT
" bytes ("
516 CUPS_LLFMT
" bytes/sec) ", CUPS_LLCAST total
,
517 CUPS_LLCAST length
, CUPS_LLCAST (total
/ (current
- start
)));
523 puts("Closing connection to server...");
534 * End of "$Id: testhttp.c 181 2006-06-22 20:01:18Z jlovell $".