test.pl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. #!/usr/bin/env perl
  2. # This script is used to test Civetweb web server
  3. use IO::Socket;
  4. use File::Path;
  5. use Cwd;
  6. use strict;
  7. use warnings;
  8. #use diagnostics;
  9. sub on_windows { $^O =~ /win32/i; }
  10. my $port = 23456;
  11. my $pid = undef;
  12. my $num_requests;
  13. my $dir_separator = on_windows() ? '\\' : '/';
  14. my $copy_cmd = on_windows() ? 'copy' : 'cp';
  15. my $test_dir_uri = "test_dir";
  16. my $root = 'test';
  17. my $test_dir = $root . $dir_separator. $test_dir_uri;
  18. my $config = 'civetweb.conf';
  19. my $exe_ext = on_windows() ? '.exe' : '';
  20. my $civetweb_exe = '.' . $dir_separator . 'civetweb' . $exe_ext;
  21. my $embed_exe = '.' . $dir_separator . 'embed' . $exe_ext;
  22. my $unit_test_exe = '.' . $dir_separator . 'unit_test' . $exe_ext;
  23. my $exit_code = 0;
  24. my @files_to_delete = ('debug.log', 'access.log', $config, "$root/a/put.txt",
  25. "$root/a+.txt", "$root/.htpasswd", "$root/binary_file", "$root/a",
  26. "$root/myperl", $embed_exe, $unit_test_exe);
  27. END {
  28. unlink @files_to_delete;
  29. kill_spawned_child();
  30. File::Path::rmtree($test_dir);
  31. exit $exit_code;
  32. }
  33. sub fail {
  34. print "FAILED: @_\n";
  35. $exit_code = 1;
  36. exit 1;
  37. }
  38. sub get_num_of_log_entries {
  39. open FD, "access.log" or return 0;
  40. my @lines = (<FD>);
  41. close FD;
  42. return scalar @lines;
  43. }
  44. # Send the request to the 127.0.0.1:$port and return the reply
  45. sub req {
  46. my ($request, $inc, $timeout) = @_;
  47. my $sock = IO::Socket::INET->new(Proto => 6,
  48. PeerAddr => '127.0.0.1', PeerPort => $port);
  49. fail("Cannot connect to http://127.0.0.1:$port : $!") unless $sock;
  50. $sock->autoflush(1);
  51. foreach my $byte (split //, $request) {
  52. last unless print $sock $byte;
  53. select undef, undef, undef, .001 if length($request) < 256;
  54. }
  55. my ($out, $buf) = ('', '');
  56. eval {
  57. alarm $timeout if $timeout;
  58. $out .= $buf while (sysread($sock, $buf, 1024) > 0);
  59. alarm 0 if $timeout;
  60. };
  61. close $sock;
  62. $num_requests += defined($inc) ? $inc : 1;
  63. my $num_logs = get_num_of_log_entries();
  64. unless ($num_requests == $num_logs) {
  65. fail("Request has not been logged: [$request], output: [$out]");
  66. }
  67. return $out;
  68. }
  69. # Send the request. Compare with the expected reply. Fail if no match
  70. sub o {
  71. my ($request, $expected_reply, $message, $num_logs) = @_;
  72. print "==> $message ... ";
  73. my $reply = req($request, $num_logs);
  74. if ($reply =~ /$expected_reply/s) {
  75. print "OK\n";
  76. } else {
  77. #fail("Requested: [$request]\nExpected: [$expected_reply], got: [$reply]");
  78. fail("Expected: [$expected_reply], got: [$reply]");
  79. }
  80. }
  81. # Spawn a server listening on specified port
  82. sub spawn {
  83. my ($cmdline) = @_;
  84. print 'Executing: ', @_, "\n";
  85. if (on_windows()) {
  86. my @args = split /\s+/, $cmdline;
  87. my $executable = $args[0];
  88. Win32::Spawn($executable, $cmdline, $pid);
  89. die "Cannot spawn @_: $!" unless $pid;
  90. } else {
  91. unless ($pid = fork()) {
  92. exec $cmdline;
  93. die "cannot exec [$cmdline]: $!\n";
  94. }
  95. }
  96. sleep 1;
  97. }
  98. sub write_file {
  99. open FD, ">$_[0]" or fail "Cannot open $_[0]: $!";
  100. binmode FD;
  101. print FD $_[1];
  102. close FD;
  103. }
  104. sub read_file {
  105. open FD, $_[0] or fail "Cannot open $_[0]: $!";
  106. my @lines = <FD>;
  107. close FD;
  108. return join '', @lines;
  109. }
  110. sub kill_spawned_child {
  111. if (defined($pid)) {
  112. kill(9, $pid);
  113. waitpid($pid, 0);
  114. }
  115. }
  116. ####################################################### ENTRY POINT
  117. unlink @files_to_delete;
  118. $SIG{PIPE} = 'IGNORE';
  119. $SIG{ALRM} = sub { die "timeout\n" };
  120. #local $| =1;
  121. # Make sure we export only symbols that start with "mg_", and keep local
  122. # symbols static.
  123. if ($^O =~ /darwin|bsd|linux/) {
  124. my $out = `(cc -c src/civetweb.c && nm src/civetweb.o) | grep ' T '`;
  125. foreach (split /\n/, $out) {
  126. /T\s+_?mg_.+/ or fail("Exported symbol $_")
  127. }
  128. }
  129. if (scalar(@ARGV) > 0 and $ARGV[0] eq 'unit') {
  130. do_unit_test();
  131. exit 0;
  132. }
  133. # Make sure we load config file if no options are given.
  134. # Command line options override config files settings
  135. write_file($config, "access_log_file access.log\n" .
  136. "listening_ports 127.0.0.1:12345\n");
  137. spawn("$civetweb_exe -listening_ports 127.0.0.1:$port");
  138. o("GET /test/hello.txt HTTP/1.0\n\n", 'HTTP/1.1 200 OK', 'Loading config file');
  139. unlink $config;
  140. kill_spawned_child();
  141. # Spawn the server on port $port
  142. my $cmd = "$civetweb_exe ".
  143. "-listening_ports 127.0.0.1:$port ".
  144. "-access_log_file access.log ".
  145. "-error_log_file debug.log ".
  146. "-cgi_environment CGI_FOO=foo,CGI_BAR=bar,CGI_BAZ=baz " .
  147. "-extra_mime_types .bar=foo/bar,.tar.gz=blah,.baz=foo " .
  148. '-put_delete_auth_file test/passfile ' .
  149. '-access_control_list -0.0.0.0/0,+127.0.0.1 ' .
  150. "-document_root $root ".
  151. "-hide_files_patterns **exploit.PL ".
  152. "-enable_keep_alive yes ".
  153. "-url_rewrite_patterns /aiased=/etc/,/ta=$test_dir";
  154. $cmd .= ' -cgi_interpreter perl' if on_windows();
  155. spawn($cmd);
  156. o("GET /hello.txt HTTP/1.1\nConnection: close\nRange: bytes=3-50\r\n\r\n",
  157. 'Content-Length: 15\s', 'Range past the file end');
  158. o("GET /hello.txt HTTP/1.1\n\n GET /hello.txt HTTP/1.0\n\n",
  159. 'HTTP/1.1 200.+keep-alive.+HTTP/1.1 200.+close',
  160. 'Request pipelining', 2);
  161. my $x = 'x=' . 'A' x (200 * 1024);
  162. my $len = length($x);
  163. o("POST /env.cgi HTTP/1.0\r\nContent-Length: $len\r\n\r\n$x",
  164. '^HTTP/1.1 200 OK', 'Long POST');
  165. # Try to overflow: Send very long request
  166. req('POST ' . '/..' x 100 . 'ABCD' x 3000 . "\n\n", 0); # don't log this one
  167. o("GET /hello.txt HTTP/1.0\n\n", 'HTTP/1.1 200 OK', 'GET regular file');
  168. o("GET /hello.txt HTTP/1.0\nContent-Length: -2147483648\n\n",
  169. 'HTTP/1.1 200 OK', 'Negative content length');
  170. o("GET /hello.txt HTTP/1.0\n\n", 'Content-Length: 17\s',
  171. 'GET regular file Content-Length');
  172. o("GET /%68%65%6c%6c%6f%2e%74%78%74 HTTP/1.0\n\n",
  173. 'HTTP/1.1 200 OK', 'URL-decoding');
  174. # Break CGI reading after 1 second. We must get full output.
  175. # Since CGI script does sleep, we sleep as well and increase request count
  176. # manually.
  177. my $slow_cgi_reply;
  178. print "==> Slow CGI output ... ";
  179. fail('Slow CGI output forward reply=', $slow_cgi_reply) unless
  180. ($slow_cgi_reply = req("GET /timeout.cgi HTTP/1.0\r\n\r\n", 0, 1)) =~ /Some data/s;
  181. print "OK\n";
  182. sleep 3;
  183. $num_requests++;
  184. # '+' in URI must not be URL-decoded to space
  185. write_file("$root/a+.txt", '');
  186. o("GET /a+.txt HTTP/1.0\n\n", 'HTTP/1.1 200 OK', 'URL-decoding, + in URI');
  187. # Test HTTP version parsing
  188. o("GET / HTTPX/1.0\r\n\r\n", '^HTTP/1.1 500', 'Bad HTTP Version', 0);
  189. o("GET / HTTP/x.1\r\n\r\n", '^HTTP/1.1 505', 'Bad HTTP maj Version', 0);
  190. o("GET / HTTP/1.1z\r\n\r\n", '^HTTP/1.1 505', 'Bad HTTP min Version', 0);
  191. o("GET / HTTP/02.0\r\n\r\n", '^HTTP/1.1 505', 'HTTP Version >1.1', 0);
  192. # File with leading single dot
  193. o("GET /.leading.dot.txt HTTP/1.0\n\n", 'abc123', 'Leading dot 1');
  194. o("GET /...leading.dot.txt HTTP/1.0\n\n", 'abc123', 'Leading dot 2');
  195. o("GET /../\\\\/.//...leading.dot.txt HTTP/1.0\n\n", 'abc123', 'Leading dot 3')
  196. if on_windows();
  197. o("GET .. HTTP/1.0\n\n", '400 Bad Request', 'Leading dot 4', 0);
  198. mkdir $test_dir unless -d $test_dir;
  199. o("GET /$test_dir_uri/not_exist HTTP/1.0\n\n",
  200. 'HTTP/1.1 404', 'PATH_INFO loop problem');
  201. o("GET /$test_dir_uri HTTP/1.0\n\n", 'HTTP/1.1 301', 'Directory redirection');
  202. o("GET /$test_dir_uri/ HTTP/1.0\n\n", 'Modified', 'Directory listing');
  203. write_file("$test_dir/index.html", "tralala");
  204. o("GET /$test_dir_uri/ HTTP/1.0\n\n", 'tralala', 'Index substitution');
  205. o("GET / HTTP/1.0\n\n", 'embed.c', 'Directory listing - file name');
  206. o("GET /ta/ HTTP/1.0\n\n", 'Modified', 'Aliases');
  207. o("GET /not-exist HTTP/1.0\r\n\n", 'HTTP/1.1 404', 'Not existent file');
  208. mkdir $test_dir . $dir_separator . 'x';
  209. my $path = $test_dir . $dir_separator . 'x' . $dir_separator . 'index.cgi';
  210. write_file($path, read_file($root . $dir_separator . 'env.cgi'));
  211. chmod(0755, $path);
  212. o("GET /$test_dir_uri/x/ HTTP/1.0\n\n", "Content-Type: text/html\r\n\r\n",
  213. 'index.cgi execution');
  214. my $cwd = getcwd();
  215. o("GET /$test_dir_uri/x/ HTTP/1.0\n\n",
  216. "SCRIPT_FILENAME=$cwd/test/test_dir/x/index.cgi", 'SCRIPT_FILENAME');
  217. o("GET /ta/x/ HTTP/1.0\n\n", "SCRIPT_NAME=/ta/x/index.cgi",
  218. 'Aliases SCRIPT_NAME');
  219. o("GET /hello.txt HTTP/1.1\nConnection: close\n\n", 'Connection: close',
  220. 'No keep-alive');
  221. $path = $test_dir . $dir_separator . 'x' . $dir_separator . 'a.cgi';
  222. system("ln -s `which perl` $root/myperl") == 0 or fail("Can't symlink perl");
  223. write_file($path, "#!../../myperl\n" .
  224. "print \"Content-Type: text/plain\\n\\nhi\";");
  225. chmod(0755, $path);
  226. o("GET /$test_dir_uri/x/a.cgi HTTP/1.0\n\n", "hi", 'Relative CGI interp path');
  227. o("GET * HTTP/1.0\n\n", "^HTTP/1.1 404", '* URI');
  228. my $mime_types = {
  229. html => 'text/html',
  230. htm => 'text/html',
  231. txt => 'text/plain',
  232. unknown_extension => 'text/plain',
  233. js => 'application/x-javascript',
  234. css => 'text/css',
  235. jpg => 'image/jpeg',
  236. c => 'text/plain',
  237. 'tar.gz' => 'blah',
  238. bar => 'foo/bar',
  239. baz => 'foo',
  240. };
  241. foreach my $key (keys %$mime_types) {
  242. my $filename = "_mime_file_test.$key";
  243. write_file("$root/$filename", '');
  244. o("GET /$filename HTTP/1.0\n\n",
  245. "Content-Type: $mime_types->{$key}", ".$key mime type");
  246. unlink "$root/$filename";
  247. }
  248. # Get binary file and check the integrity
  249. my $binary_file = 'binary_file';
  250. my $f2 = '';
  251. foreach (0..123456) { $f2 .= chr(int(rand() * 255)); }
  252. write_file("$root/$binary_file", $f2);
  253. my $f1 = req("GET /$binary_file HTTP/1.0\r\n\n");
  254. while ($f1 =~ /^.*\r\n/) { $f1 =~ s/^.*\r\n// }
  255. $f1 eq $f2 or fail("Integrity check for downloaded binary file");
  256. my $range_request = "GET /hello.txt HTTP/1.1\nConnection: close\n".
  257. "Range: bytes=3-5\r\n\r\n";
  258. o($range_request, '206 Partial Content', 'Range: 206 status code');
  259. o($range_request, 'Content-Length: 3\s', 'Range: Content-Length');
  260. o($range_request, 'Content-Range: bytes 3-5/17', 'Range: Content-Range');
  261. o($range_request, '\nple$', 'Range: body content');
  262. # Test directory sorting. Sleep between file creation for 1.1 seconds,
  263. # to make sure modification time are different.
  264. mkdir "$test_dir/sort";
  265. write_file("$test_dir/sort/11", 'xx');
  266. select undef, undef, undef, 1.1;
  267. write_file("$test_dir/sort/aa", 'xxxx');
  268. select undef, undef, undef, 1.1;
  269. write_file("$test_dir/sort/bb", 'xxx');
  270. select undef, undef, undef, 1.1;
  271. write_file("$test_dir/sort/22", 'x');
  272. o("GET /$test_dir_uri/sort/?n HTTP/1.0\n\n",
  273. '200 OK.+>11<.+>22<.+>aa<.+>bb<',
  274. 'Directory listing (name, ascending)');
  275. o("GET /$test_dir_uri/sort/?nd HTTP/1.0\n\n",
  276. '200 OK.+>bb<.+>aa<.+>22<.+>11<',
  277. 'Directory listing (name, descending)');
  278. o("GET /$test_dir_uri/sort/?s HTTP/1.0\n\n",
  279. '200 OK.+>22<.+>11<.+>bb<.+>aa<',
  280. 'Directory listing (size, ascending)');
  281. o("GET /$test_dir_uri/sort/?sd HTTP/1.0\n\n",
  282. '200 OK.+>aa<.+>bb<.+>11<.+>22<',
  283. 'Directory listing (size, descending)');
  284. o("GET /$test_dir_uri/sort/?d HTTP/1.0\n\n",
  285. '200 OK.+>11<.+>aa<.+>bb<.+>22<',
  286. 'Directory listing (modification time, ascending)');
  287. o("GET /$test_dir_uri/sort/?dd HTTP/1.0\n\n",
  288. '200 OK.+>22<.+>bb<.+>aa<.+>11<',
  289. 'Directory listing (modification time, descending)');
  290. unless (scalar(@ARGV) > 0 and $ARGV[0] eq "basic_tests") {
  291. # Check that .htpasswd file existence trigger authorization
  292. write_file("$root/.htpasswd", 'user with space, " and comma:mydomain.com:5deda12442309cbdcdffc6b2737a894f');
  293. o("GET /hello.txt HTTP/1.1\n\n", '401 Unauthorized',
  294. '.htpasswd - triggering auth on file request');
  295. o("GET / HTTP/1.1\n\n", '401 Unauthorized',
  296. '.htpasswd - triggering auth on directory request');
  297. # Test various funky things in an authentication header.
  298. o("GET /hello.txt HTTP/1.0\nAuthorization: Digest eq== empty=\"\", empty2=, quoted=\"blah foo bar, baz\\\"\\\" more\\\"\", unterminatedquoted=\" doesn't stop\n\n",
  299. '401 Unauthorized', 'weird auth values should not cause crashes');
  300. my $auth_header = "Digest username=\"user with space, \\\" and comma\", ".
  301. "realm=\"mydomain.com\", nonce=\"1291376417\", uri=\"/\",".
  302. "response=\"e8dec0c2a1a0c8a7e9a97b4b5ea6a6e6\", qop=auth, nc=00000001, cnonce=\"1a49b53a47a66e82\"";
  303. o("GET /hello.txt HTTP/1.0\nAuthorization: $auth_header\n\n", 'HTTP/1.1 200 OK', 'GET regular file with auth');
  304. o("GET / HTTP/1.0\nAuthorization: $auth_header\n\n", '^(.(?!(.htpasswd)))*$',
  305. '.htpasswd is hidden from the directory list');
  306. o("GET / HTTP/1.0\nAuthorization: $auth_header\n\n", '^(.(?!(exploit.pl)))*$',
  307. 'hidden file is hidden from the directory list');
  308. o("GET /.htpasswd HTTP/1.0\nAuthorization: $auth_header\n\n",
  309. '^HTTP/1.1 404 ', '.htpasswd must not be shown');
  310. o("GET /exploit.pl HTTP/1.0\nAuthorization: $auth_header\n\n",
  311. '^HTTP/1.1 404', 'hidden files must not be shown');
  312. unlink "$root/.htpasswd";
  313. o("GET /dir%20with%20spaces/hello.cgi HTTP/1.0\n\r\n",
  314. 'HTTP/1.1 200 OK.+hello', 'CGI script with spaces in path');
  315. o("GET /env.cgi HTTP/1.0\n\r\n", 'HTTP/1.1 200 OK', 'GET CGI file');
  316. o("GET /bad2.cgi HTTP/1.0\n\n", "HTTP/1.1 123 Please pass me to the client\r",
  317. 'CGI Status code text');
  318. o("GET /sh.cgi HTTP/1.0\n\r\n", 'shell script CGI',
  319. 'GET sh CGI file') unless on_windows();
  320. o("GET /env.cgi?var=HELLO HTTP/1.0\n\n", 'QUERY_STRING=var=HELLO',
  321. 'QUERY_STRING wrong');
  322. o("POST /env.cgi HTTP/1.0\r\nContent-Length: 9\r\n\r\nvar=HELLO",
  323. 'var=HELLO', 'CGI POST wrong');
  324. o("POST /env.cgi HTTP/1.0\r\nContent-Length: 9\r\n\r\nvar=HELLO",
  325. '\x0aCONTENT_LENGTH=9', 'Content-Length not being passed to CGI');
  326. o("GET /env.cgi HTTP/1.0\nMy-HdR: abc\n\r\n",
  327. 'HTTP_MY_HDR=abc', 'HTTP_* env');
  328. o("GET /env.cgi HTTP/1.0\n\r\nSOME_TRAILING_DATA_HERE",
  329. 'HTTP/1.1 200 OK', 'GET CGI with trailing data');
  330. o("GET /env.cgi%20 HTTP/1.0\n\r\n",
  331. 'HTTP/1.1 404', 'CGI Win32 code disclosure (%20)');
  332. o("GET /env.cgi%ff HTTP/1.0\n\r\n",
  333. 'HTTP/1.1 404', 'CGI Win32 code disclosure (%ff)');
  334. o("GET /env.cgi%2e HTTP/1.0\n\r\n",
  335. 'HTTP/1.1 404', 'CGI Win32 code disclosure (%2e)');
  336. o("GET /env.cgi%2b HTTP/1.0\n\r\n",
  337. 'HTTP/1.1 404', 'CGI Win32 code disclosure (%2b)');
  338. o("GET /env.cgi HTTP/1.0\n\r\n", '\nHTTPS=off\n', 'CGI HTTPS');
  339. o("GET /env.cgi HTTP/1.0\n\r\n", '\nCGI_FOO=foo\n', '-cgi_env 1');
  340. o("GET /env.cgi HTTP/1.0\n\r\n", '\nCGI_BAR=bar\n', '-cgi_env 2');
  341. o("GET /env.cgi HTTP/1.0\n\r\n", '\nCGI_BAZ=baz\n', '-cgi_env 3');
  342. o("GET /env.cgi/a/b/98 HTTP/1.0\n\r\n", 'PATH_INFO=/a/b/98\n', 'PATH_INFO');
  343. o("GET /env.cgi/a/b/9 HTTP/1.0\n\r\n", 'PATH_INFO=/a/b/9\n', 'PATH_INFO');
  344. # Check that CGI's current directory is set to script's directory
  345. my $copy_cmd = on_windows() ? 'copy' : 'cp';
  346. system("$copy_cmd $root" . $dir_separator . "env.cgi $test_dir" .
  347. $dir_separator . 'env.cgi');
  348. o("GET /$test_dir_uri/env.cgi HTTP/1.0\n\n",
  349. "CURRENT_DIR=.*$root/$test_dir_uri", "CGI chdir()");
  350. # SSI tests
  351. o("GET /ssi1.shtml HTTP/1.0\n\n",
  352. 'ssi_begin.+CFLAGS.+ssi_end', 'SSI #include file=');
  353. o("GET /ssi2.shtml HTTP/1.0\n\n",
  354. 'ssi_begin.+Unit test.+ssi_end', 'SSI #include virtual=');
  355. my $ssi_exec = on_windows() ? 'ssi4.shtml' : 'ssi3.shtml';
  356. o("GET /$ssi_exec HTTP/1.0\n\n",
  357. 'ssi_begin.+Makefile.+ssi_end', 'SSI #exec');
  358. my $abs_path = on_windows() ? 'ssi6.shtml' : 'ssi5.shtml';
  359. my $word = on_windows() ? 'boot loader' : 'root';
  360. o("GET /$abs_path HTTP/1.0\n\n",
  361. "ssi_begin.+$word.+ssi_end", 'SSI #include abspath');
  362. o("GET /ssi7.shtml HTTP/1.0\n\n",
  363. 'ssi_begin.+Unit test.+ssi_end', 'SSI #include "..."');
  364. o("GET /ssi8.shtml HTTP/1.0\n\n",
  365. 'ssi_begin.+CFLAGS.+ssi_end', 'SSI nested #includes');
  366. # Manipulate the passwords file
  367. my $path = 'test_htpasswd';
  368. unlink $path;
  369. system("$civetweb_exe -A $path a b c") == 0
  370. or fail("Cannot add user in a passwd file");
  371. system("$civetweb_exe -A $path a b c2") == 0
  372. or fail("Cannot edit user in a passwd file");
  373. my $content = read_file($path);
  374. $content =~ /^b:a:\w+$/gs or fail("Bad content of the passwd file");
  375. unlink $path;
  376. do_PUT_test();
  377. kill_spawned_child();
  378. do_unit_test();
  379. }
  380. sub do_PUT_test {
  381. # This only works because civetweb currently doesn't look at the nonce.
  382. # It should really be rejected...
  383. my $auth_header = "Authorization: Digest username=guest, ".
  384. "realm=mydomain.com, nonce=1145872809, uri=/put.txt, ".
  385. "response=896327350763836180c61d87578037d9, qop=auth, ".
  386. "nc=00000002, cnonce=53eddd3be4e26a98\n";
  387. o("PUT /a/put.txt HTTP/1.0\nContent-Length: 7\n$auth_header\n1234567",
  388. "HTTP/1.1 201 OK", 'PUT file, status 201');
  389. fail("PUT content mismatch")
  390. unless read_file("$root/a/put.txt") eq '1234567';
  391. o("PUT /a/put.txt HTTP/1.0\nContent-Length: 4\n$auth_header\nabcd",
  392. "HTTP/1.1 200 OK", 'PUT file, status 200');
  393. fail("PUT content mismatch")
  394. unless read_file("$root/a/put.txt") eq 'abcd';
  395. o("PUT /a/put.txt HTTP/1.0\n$auth_header\nabcd",
  396. "HTTP/1.1 411 Length Required", 'PUT 411 error');
  397. o("PUT /a/put.txt HTTP/1.0\nExpect: blah\nContent-Length: 1\n".
  398. "$auth_header\nabcd",
  399. "HTTP/1.1 417 Expectation Failed", 'PUT 417 error');
  400. o("PUT /a/put.txt HTTP/1.0\nExpect: 100-continue\nContent-Length: 4\n".
  401. "$auth_header\nabcd",
  402. "HTTP/1.1 100 Continue.+HTTP/1.1 200", 'PUT 100-Continue');
  403. }
  404. sub do_unit_test {
  405. my $target = on_windows() ? 'wi' : 'un';
  406. system("make $target") == 0 or fail("Unit test failed!");
  407. }
  408. print "SUCCESS! All tests passed.\n";