2 * "$Id: testhttp.c 5023 2006-01-29 14:39:44Z mike $"
4 * HTTP test program for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2005 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
, "ipp://username:password@[v1.fe80::200:1234:5678:9abc+eth0]:999/ipp",
98 "ipp", "username:password", "fe80::200:1234:5678:9abc%eth0", "/ipp", 999, 999 },
99 { HTTP_URI_OK
, "http://server/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400",
100 "http", "", "server", "/admin?DEVICE_URI=usb://HP/Photosmart%25202600%2520series?serial=MY53OK70V10400", 80, 0 },
103 { HTTP_URI_MISSING_SCHEME
, "/path/to/file/index.html",
104 "file", "", "", "/path/to/file/index.html", 0, 0 },
105 { HTTP_URI_MISSING_SCHEME
, "//server/ipp",
106 "ipp", "", "server", "/ipp", 631, 0 },
109 { HTTP_URI_UNKNOWN_SCHEME
, "vendor://server/resource",
110 "vendor", "", "server", "/resource", 0, 0 },
112 /* Missing resource */
113 { HTTP_URI_MISSING_RESOURCE
, "socket://[::192.168.2.1]",
114 "socket", "", "::192.168.2.1", "/", 9100, 0 },
117 { HTTP_URI_BAD_URI
, "",
118 "", "", "", "", 0, 0 },
121 { HTTP_URI_BAD_SCHEME
, "bad_scheme://server/resource",
122 "", "", "", "", 0, 0 },
125 { HTTP_URI_BAD_USERNAME
, "http://username:passwor%6@server/resource",
126 "http", "", "", "", 80, 0 },
129 { HTTP_URI_BAD_HOSTNAME
, "http://[/::1]/index.html",
130 "http", "", "", "", 80, 0 },
131 { HTTP_URI_BAD_HOSTNAME
, "http://[",
132 "http", "", "", "", 80, 0 },
133 { HTTP_URI_BAD_HOSTNAME
, "http://serve%7/index.html",
134 "http", "", "", "", 80, 0 },
136 /* Bad port number */
137 { HTTP_URI_BAD_PORT
, "http://127.0.0.1:9999a/index.html",
138 "http", "", "127.0.0.1", "", 0, 0 },
141 { HTTP_URI_BAD_RESOURCE
, "http://server/index.html%",
142 "http", "", "server", "", 80, 0 }
144 static const char * const base64_tests
[][2] =
149 /* 010000 010100 0010 */
151 /* 010000 010100 001001 000011 */
152 { "ABCD", "QUJDRA==" },
153 /* 010000 010100 001001 000011 010001 00 */
154 { "ABCDE", "QUJDREU=" },
155 /* 010000 010100 001001 000011 010001 000100 0101 */
156 { "ABCDEF", "QUJDREVG" },
157 /* 010000 010100 001001 000011 010001 000100 010101 000110 */
162 * 'main()' - Main entry.
165 int /* O - Exit status */
166 main(int argc
, /* I - Number of command-line arguments */
167 char *argv
[]) /* I - Command-line arguments */
169 int i
, j
, k
; /* Looping vars */
170 http_t
*http
; /* HTTP connection */
171 http_status_t status
; /* Status of GET command */
172 int failures
; /* Number of test failures */
173 char buffer
[8192]; /* Input buffer */
174 long bytes
; /* Number of bytes read */
175 FILE *out
; /* Output file */
176 char encode
[256], /* Base64-encoded string */
177 decode
[256]; /* Base64-decoded string */
178 int decodelen
; /* Length of decoded string */
179 char scheme
[HTTP_MAX_URI
], /* Scheme from URI */
180 hostname
[HTTP_MAX_URI
], /* Hostname from URI */
181 username
[HTTP_MAX_URI
], /* Username:password from URI */
182 resource
[HTTP_MAX_URI
]; /* Resource from URI */
183 int port
; /* Port number from URI */
184 http_uri_status_t uri_status
; /* Status of URI separation */
185 http_addrlist_t
*addrlist
, /* Address list */
186 *addr
; /* Current address */
187 off_t length
, total
; /* Length and total bytes */
188 time_t start
, current
; /* Start and end time */
189 static const char * const uri_status_strings
[] =
192 "HTTP_URI_BAD_ARGUMENTS",
193 "HTTP_URI_BAD_RESOURCE",
195 "HTTP_URI_BAD_HOSTNAME",
196 "HTTP_URI_BAD_USERNAME",
197 "HTTP_URI_BAD_SCHEME",
200 "HTTP_URI_MISSING_SCHEME",
201 "HTTP_URI_UNKNOWN_SCHEME",
202 "HTTP_URI_MISSING_RESOURCE"
207 * Do API tests if we don't have a URL on the command-line...
215 * httpGetDateString()/httpGetDateTime()
218 fputs("httpGetDateString()/httpGetDateTime(): ", stdout
);
221 strcpy(buffer
, httpGetDateString(start
));
222 current
= httpGetDateTime(buffer
);
224 i
= (int)(current
- start
);
234 printf(" Difference is %d seconds, %02d:%02d:%02d...\n", i
, i
/ 3600,
235 (i
/ 60) % 60, i
% 60);
236 printf(" httpGetDateString(%d) returned \"%s\"\n", (int)start
, buffer
);
237 printf(" httpGetDateTime(\"%s\") returned %d\n", buffer
, (int)current
);
238 printf(" httpGetDateString(%d) returned \"%s\"\n", (int)current
,
239 httpGetDateString(current
));
243 * httpDecode64_2()/httpEncode64_2()
246 fputs("httpDecode64_2()/httpEncode64_2(): ", stdout
);
248 for (i
= 0, j
= 0; i
< (int)(sizeof(base64_tests
) / sizeof(base64_tests
[0])); i
++)
250 httpEncode64_2(encode
, sizeof(encode
), base64_tests
[i
][0],
251 strlen(base64_tests
[i
][0]));
252 decodelen
= (int)sizeof(decode
);
253 httpDecode64_2(decode
, &decodelen
, base64_tests
[i
][1]);
255 if (strcmp(decode
, base64_tests
[i
][0]))
265 printf(" httpDecode64_2() returned \"%s\", expected \"%s\"...\n",
266 decode
, base64_tests
[i
][0]);
269 if (strcmp(encode
, base64_tests
[i
][1]))
279 printf(" httpEncode64_2() returned \"%s\", expected \"%s\"...\n",
280 encode
, base64_tests
[i
][1]);
291 fputs("httpGetHostname(): ", stdout
);
293 if (httpGetHostname(hostname
, sizeof(hostname
)))
294 printf("PASS (%s)\n", hostname
);
305 fputs("httpAddrGetList(): ", stdout
);
307 addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, NULL
);
310 for (i
= 0, addr
= addrlist
; addr
; i
++, addr
= addr
->next
);
312 printf("PASS (%d address(es) for %s)\n", i
, hostname
);
313 httpAddrFreeList(addrlist
);
322 * Test httpSeparateURI()...
325 fputs("httpSeparateURI(): ", stdout
);
326 for (i
= 0, j
= 0; i
< (int)(sizeof(uri_tests
) / sizeof(uri_tests
[0])); i
++)
328 uri_status
= httpSeparateURI(HTTP_URI_CODING_MOST
,
329 uri_tests
[i
].uri
, scheme
, sizeof(scheme
),
330 username
, sizeof(username
),
331 hostname
, sizeof(hostname
), &port
,
332 resource
, sizeof(resource
));
333 if (uri_status
!= uri_tests
[i
].result
||
334 strcmp(scheme
, uri_tests
[i
].scheme
) ||
335 strcmp(username
, uri_tests
[i
].username
) ||
336 strcmp(hostname
, uri_tests
[i
].hostname
) ||
337 port
!= uri_tests
[i
].port
||
338 strcmp(resource
, uri_tests
[i
].resource
))
348 printf(" \"%s\":\n", uri_tests
[i
].uri
);
350 if (uri_status
!= uri_tests
[i
].result
)
351 printf(" Returned %s instead of %s\n",
352 uri_status_strings
[uri_status
+ 8],
353 uri_status_strings
[uri_tests
[i
].result
+ 8]);
355 if (strcmp(scheme
, uri_tests
[i
].scheme
))
356 printf(" Scheme \"%s\" instead of \"%s\"\n",
357 scheme
, uri_tests
[i
].scheme
);
359 if (strcmp(username
, uri_tests
[i
].username
))
360 printf(" Username \"%s\" instead of \"%s\"\n",
361 username
, uri_tests
[i
].username
);
363 if (strcmp(hostname
, uri_tests
[i
].hostname
))
364 printf(" Hostname \"%s\" instead of \"%s\"\n",
365 hostname
, uri_tests
[i
].hostname
);
367 if (port
!= uri_tests
[i
].port
)
368 printf(" Port %d instead of %d\n",
369 port
, uri_tests
[i
].port
);
371 if (strcmp(resource
, uri_tests
[i
].resource
))
372 printf(" Resource \"%s\" instead of \"%s\"\n",
373 resource
, uri_tests
[i
].resource
);
378 printf("PASS (%d URIs tested)\n",
379 (int)(sizeof(uri_tests
) / sizeof(uri_tests
[0])));
382 * Test httpAssembleURI()...
385 fputs("httpAssembleURI(): ", stdout
);
386 for (i
= 0, j
= 0, k
= 0;
387 i
< (int)(sizeof(uri_tests
) / sizeof(uri_tests
[0]));
389 if (uri_tests
[i
].result
== HTTP_URI_OK
&&
390 !strstr(uri_tests
[i
].uri
, "%64") &&
391 strstr(uri_tests
[i
].uri
, "//"))
394 uri_status
= httpAssembleURI(HTTP_URI_CODING_MOST
,
395 buffer
, sizeof(buffer
),
397 uri_tests
[i
].username
,
398 uri_tests
[i
].hostname
,
399 uri_tests
[i
].assemble_port
,
400 uri_tests
[i
].resource
);
402 if (uri_status
!= HTTP_URI_OK
)
412 printf(" \"%s\": %s\n", uri_tests
[i
].uri
,
413 uri_status_strings
[uri_status
+ 8]);
415 else if (strcmp(buffer
, uri_tests
[i
].uri
))
425 printf(" \"%s\": assembled = \"%s\"\n", uri_tests
[i
].uri
,
431 printf("PASS (%d URIs tested)\n", k
);
434 * Show a summary and return...
438 printf("\n%d TESTS FAILED!\n", failures
);
440 puts("\nALL TESTS PASSED!");
446 * Test HTTP GET requests...
452 for (i
= 1; i
< argc
; i
++)
454 if (!strcmp(argv
[i
], "-o"))
460 out
= fopen(argv
[i
], "wb");
464 httpSeparateURI(HTTP_URI_CODING_MOST
, argv
[i
], scheme
, sizeof(scheme
),
465 username
, sizeof(username
),
466 hostname
, sizeof(hostname
), &port
,
467 resource
, sizeof(resource
));
469 http
= httpConnectEncrypt(hostname
, port
, HTTP_ENCRYPT_IF_REQUESTED
);
475 printf("Requesting file \"%s\"...\n", resource
);
476 httpClearFields(http
);
477 httpSetField(http
, HTTP_FIELD_ACCEPT_LANGUAGE
, "en");
478 httpGet(http
, resource
);
479 while ((status
= httpUpdate(http
)) == HTTP_CONTINUE
);
481 if (status
== HTTP_OK
)
484 printf("GET failed with status %d...\n", status
);
488 length
= httpGetLength2(http
);
491 while ((bytes
= httpRead2(http
, buffer
, sizeof(buffer
))) > 0)
494 fwrite(buffer
, bytes
, 1, out
);
497 current
= time(NULL
);
498 if (current
== start
) current
++;
499 printf("\r" CUPS_LLFMT
"/" CUPS_LLFMT
" bytes ("
500 CUPS_LLFMT
" bytes/sec) ", total
, length
,
501 total
/ (current
- start
));
507 puts("Closing connection to server...");
518 * End of "$Id: testhttp.c 5023 2006-01-29 14:39:44Z mike $".