build with libressl
[lighttpd.git] / tests / mod-secdownload.t
blob96baf9d7eb4604b65230f673cb11f3598a3b1030
1 #!/usr/bin/env perl
2 BEGIN {
3 # add current source dir to the include-path
4 # we need this for make distcheck
5 (my $srcdir = $0) =~ s,/[^/]+$,/,;
6 unshift @INC, $srcdir;
9 use strict;
10 use IO::Socket;
11 use Test::More tests => 15;
12 use LightyTest;
13 use Digest::MD5 qw(md5_hex);
14 use Digest::SHA qw(hmac_sha1 hmac_sha256);
15 use MIME::Base64 qw(encode_base64url);
17 my $tf = LightyTest->new();
18 my $t;
20 ok($tf->start_proc == 0, "Starting lighttpd") or die();
22 my $secret = "verysecret";
23 my ($f, $thex, $m);
25 $t->{REQUEST} = ( <<EOF
26 GET /index.html HTTP/1.0
27 Host: www.example.org
28 EOF
30 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
32 ok($tf->handle_http($t) == 0, 'skipping secdownload - direct access');
34 ## MD5
35 $f = "/index.html";
36 $thex = sprintf("%08x", time);
37 $m = md5_hex($secret.$f.$thex);
39 $t->{REQUEST} = ( <<EOF
40 GET /sec/$m/$thex$f HTTP/1.0
41 Host: vvv.example.org
42 EOF
44 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
46 ok($tf->handle_http($t) == 0, 'secdownload (md5)');
48 $thex = sprintf("%08x", time - 1800);
49 $m = md5_hex($secret.$f.$thex);
51 $t->{REQUEST} = ( <<EOF
52 GET /sec/$m/$thex$f HTTP/1.0
53 Host: vvv.example.org
54 EOF
56 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 410 } ];
58 ok($tf->handle_http($t) == 0, 'secdownload - gone (timeout) (md5)');
60 $t->{REQUEST} = ( <<EOF
61 GET /sec$f HTTP/1.0
62 Host: vvv.example.org
63 EOF
65 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404 } ];
67 ok($tf->handle_http($t) == 0, 'secdownload - direct access (md5)');
69 $f = "/noexists";
70 $thex = sprintf("%08x", time);
71 $m = md5_hex($secret.$f.$thex);
73 $t->{REQUEST} = ( <<EOF
74 GET /sec/$m/$thex$f HTTP/1.0
75 Host: vvv.example.org
76 EOF
78 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404 } ];
80 ok($tf->handle_http($t) == 0, 'secdownload - timeout (md5)');
82 ## HMAC-SHA1
83 $f = "/index.html";
84 $thex = sprintf("%08x", time);
85 $m = encode_base64url(hmac_sha1("/$thex$f", $secret));
87 $t->{REQUEST} = ( <<EOF
88 GET /sec/$m/$thex$f HTTP/1.0
89 Host: vvv-sha1.example.org
90 EOF
92 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
94 ok($tf->handle_http($t) == 0, 'secdownload (hmac-sha1)');
96 $thex = sprintf("%08x", time - 1800);
97 $m = encode_base64url(hmac_sha1("/$thex$f", $secret));
99 $t->{REQUEST} = ( <<EOF
100 GET /sec/$m/$thex$f HTTP/1.0
101 Host: vvv-sha1.example.org
104 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 410 } ];
106 ok($tf->handle_http($t) == 0, 'secdownload - gone (timeout) (hmac-sha1)');
108 $t->{REQUEST} = ( <<EOF
109 GET /sec$f HTTP/1.0
110 Host: vvv-sha1.example.org
113 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404 } ];
115 ok($tf->handle_http($t) == 0, 'secdownload - direct access (hmac-sha1)');
118 $f = "/noexists";
119 $thex = sprintf("%08x", time);
120 $m = encode_base64url(hmac_sha1("/$thex$f", $secret));
122 $t->{REQUEST} = ( <<EOF
123 GET /sec/$m/$thex$f HTTP/1.0
124 Host: vvv-sha1.example.org
127 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404 } ];
129 ok($tf->handle_http($t) == 0, 'secdownload - timeout (hmac-sha1)');
131 ## HMAC-SHA256
132 $f = "/index.html";
133 $thex = sprintf("%08x", time);
134 $m = encode_base64url(hmac_sha256("/$thex$f", $secret));
136 $t->{REQUEST} = ( <<EOF
137 GET /sec/$m/$thex$f HTTP/1.0
138 Host: vvv-sha256.example.org
141 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
143 ok($tf->handle_http($t) == 0, 'secdownload (hmac-sha256)');
145 $thex = sprintf("%08x", time - 1800);
146 $m = encode_base64url(hmac_sha256("/$thex$f", $secret));
148 $t->{REQUEST} = ( <<EOF
149 GET /sec/$m/$thex$f HTTP/1.0
150 Host: vvv-sha256.example.org
153 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 410 } ];
155 ok($tf->handle_http($t) == 0, 'secdownload - gone (timeout) (hmac-sha256)');
157 $t->{REQUEST} = ( <<EOF
158 GET /sec$f HTTP/1.0
159 Host: vvv-sha256.example.org
162 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404 } ];
164 ok($tf->handle_http($t) == 0, 'secdownload - direct access (hmac-sha256)');
167 $f = "/noexists";
168 $thex = sprintf("%08x", time);
169 $m = encode_base64url(hmac_sha256("/$thex$f", $secret));
171 $t->{REQUEST} = ( <<EOF
172 GET /sec/$m/$thex$f HTTP/1.0
173 Host: vvv-sha256.example.org
176 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404 } ];
178 ok($tf->handle_http($t) == 0, 'secdownload - timeout (hmac-sha256)');
180 ## THE END
182 ok($tf->stop_proc == 0, "Stopping lighttpd");