[mod_wstunnel] fix ping-interval for big-endian (fixes #2944)
[lighttpd.git] / tests / mod-secdownload.t
blob91276cdda76a8ff190b4d8650a3ceb0d82ea1bb4
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 => 16;
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 $tf->{CONFIGFILE} = 'mod-secdownload.conf';
21 ok($tf->start_proc == 0, "Starting lighttpd") or die();
23 my $secret = "verysecret";
24 my ($f, $thex, $m);
26 $t->{REQUEST} = ( <<EOF
27 GET /index.html HTTP/1.0
28 Host: www.example.org
29 EOF
31 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
33 ok($tf->handle_http($t) == 0, 'skipping secdownload - direct access');
35 ## MD5
36 $f = "/index.html";
37 $thex = sprintf("%08x", time);
38 $m = md5_hex($secret.$f.$thex);
40 $t->{REQUEST} = ( <<EOF
41 GET /sec/$m/$thex$f HTTP/1.0
42 Host: vvv.example.org
43 EOF
45 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
47 ok($tf->handle_http($t) == 0, 'secdownload (md5)');
49 $thex = sprintf("%08x", time - 1800);
50 $m = md5_hex($secret.$f.$thex);
52 $t->{REQUEST} = ( <<EOF
53 GET /sec/$m/$thex$f HTTP/1.0
54 Host: vvv.example.org
55 EOF
57 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 410 } ];
59 ok($tf->handle_http($t) == 0, 'secdownload - gone (timeout) (md5)');
61 $t->{REQUEST} = ( <<EOF
62 GET /sec$f HTTP/1.0
63 Host: vvv.example.org
64 EOF
66 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404 } ];
68 ok($tf->handle_http($t) == 0, 'secdownload - direct access (md5)');
70 $f = "/noexists";
71 $thex = sprintf("%08x", time);
72 $m = md5_hex($secret.$f.$thex);
74 $t->{REQUEST} = ( <<EOF
75 GET /sec/$m/$thex$f HTTP/1.0
76 Host: vvv.example.org
77 EOF
79 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404 } ];
81 ok($tf->handle_http($t) == 0, 'secdownload - timeout (md5)');
83 ## HMAC-SHA1
84 $f = "/index.html";
85 $thex = sprintf("%08x", time);
86 $m = encode_base64url(hmac_sha1("/$thex$f", $secret));
88 $t->{REQUEST} = ( <<EOF
89 GET /sec/$m/$thex$f HTTP/1.0
90 Host: vvv-sha1.example.org
91 EOF
93 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
95 ok($tf->handle_http($t) == 0, 'secdownload (hmac-sha1)');
97 $thex = sprintf("%08x", time - 1800);
98 $m = encode_base64url(hmac_sha1("/$thex$f", $secret));
100 $t->{REQUEST} = ( <<EOF
101 GET /sec/$m/$thex$f HTTP/1.0
102 Host: vvv-sha1.example.org
105 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 410 } ];
107 ok($tf->handle_http($t) == 0, 'secdownload - gone (timeout) (hmac-sha1)');
109 $t->{REQUEST} = ( <<EOF
110 GET /sec$f HTTP/1.0
111 Host: vvv-sha1.example.org
114 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404 } ];
116 ok($tf->handle_http($t) == 0, 'secdownload - direct access (hmac-sha1)');
119 $f = "/noexists";
120 $thex = sprintf("%08x", time);
121 $m = encode_base64url(hmac_sha1("/$thex$f", $secret));
123 $t->{REQUEST} = ( <<EOF
124 GET /sec/$m/$thex$f HTTP/1.0
125 Host: vvv-sha1.example.org
128 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404 } ];
130 ok($tf->handle_http($t) == 0, 'secdownload - timeout (hmac-sha1)');
132 ## HMAC-SHA256
133 $f = "/index.html";
134 $thex = sprintf("%08x", time);
135 $m = encode_base64url(hmac_sha256("/$thex$f", $secret));
137 $t->{REQUEST} = ( <<EOF
138 GET /sec/$m/$thex$f HTTP/1.0
139 Host: vvv-sha256.example.org
142 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
144 ok($tf->handle_http($t) == 0, 'secdownload (hmac-sha256)');
146 ## HMAC-SHA256
147 $f = "/index.html?qs=1";
148 $thex = sprintf("%08x", time);
149 $m = encode_base64url(hmac_sha256("/$thex$f", $secret));
151 $t->{REQUEST} = ( <<EOF
152 GET /sec/$m/$thex$f HTTP/1.0
153 Host: vvv-sha256.example.org
156 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
158 ok($tf->handle_http($t) == 0, 'secdownload (hmac-sha256) with hash-querystr');
160 $thex = sprintf("%08x", time - 1800);
161 $m = encode_base64url(hmac_sha256("/$thex$f", $secret));
163 $t->{REQUEST} = ( <<EOF
164 GET /sec/$m/$thex$f HTTP/1.0
165 Host: vvv-sha256.example.org
168 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 410 } ];
170 ok($tf->handle_http($t) == 0, 'secdownload - gone (timeout) (hmac-sha256)');
172 $t->{REQUEST} = ( <<EOF
173 GET /sec$f HTTP/1.0
174 Host: vvv-sha256.example.org
177 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404 } ];
179 ok($tf->handle_http($t) == 0, 'secdownload - direct access (hmac-sha256)');
182 $f = "/noexists";
183 $thex = sprintf("%08x", time);
184 $m = encode_base64url(hmac_sha256("/$thex$f", $secret));
186 $t->{REQUEST} = ( <<EOF
187 GET /sec/$m/$thex$f HTTP/1.0
188 Host: vvv-sha256.example.org
191 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404 } ];
193 ok($tf->handle_http($t) == 0, 'secdownload - timeout (hmac-sha256)');
195 ## THE END
197 ok($tf->stop_proc == 0, "Stopping lighttpd");