1#!/usr/bin/env perl
2BEGIN {
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;
7}
8
9use strict;
10use IO::Socket;
11use Test::More tests => 10;
12use LightyTest;
13
14my $tf = LightyTest->new();
15my $t;
16my $docroot = $tf->{'TESTDIR'}."/tmp/lighttpd/servers/www.example.org/pages";
17
18sub init_testbed {
19    return 0 unless eval { symlink("",""); 1 };
20    my $f = "$docroot/index.html";
21    my $l = "$docroot/index.xhtml";
22    my $rc = undef;
23    unless (-l $l) {
24        return 0 unless symlink($f,$l);
25    };
26    $f = "$docroot/expire";
27    $l = "$docroot/symlinked";
28    $rc = undef;
29    unless (-l $l) {
30        return 0 unless symlink($f,$l);
31    }
32    return 1;
33};
34
35SKIP: {
36    skip "perl does not support symlinking or setting up the symlinks failed.", 10 unless init_testbed;
37    ok($tf->start_proc == 0, "Starting lighttpd") or die();
38
39# allow case
40# simple file
41	$t->{REQUEST} = ( <<EOF
42GET /index.html HTTP/1.0
43Host: symlink.example.org
44EOF
45 );
46	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
47	ok($tf->handle_http($t) == 0, 'allow: simple file');
48
49# symlinked file
50	$t->{REQUEST} = ( <<EOF
51GET /index.xhtml HTTP/1.0
52Host: symlink.example.org
53EOF
54 );
55	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
56	ok($tf->handle_http($t) == 0, 'allow: symlinked file');
57
58# directly symlinked dir
59	$t->{REQUEST} = ( <<EOF
60GET /symlinked/ HTTP/1.0
61Host: symlink.example.org
62EOF
63 );
64	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
65	ok($tf->handle_http($t) == 0, 'allow: directly symlinked dir');
66
67# symlinked dir in path
68	$t->{REQUEST} = ( <<EOF
69GET /symlinked/access.txt HTTP/1.0
70Host: symlink.example.org
71EOF
72 );
73	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
74	ok($tf->handle_http($t) == 0, 'allow: symlinked dir in path');
75
76# deny case
77# simple file
78	$t->{REQUEST} = ( <<EOF
79GET /index.html HTTP/1.0
80Host: nosymlink.example.org
81EOF
82 );
83	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
84	ok($tf->handle_http($t) == 0, 'deny: simple file');
85
86# symlinked file
87	$t->{REQUEST} = ( <<EOF
88GET /index.xhtml HTTP/1.0
89Host: nosymlink.example.org
90EOF
91 );
92	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 403 } ];
93	ok($tf->handle_http($t) == 0, 'deny: symlinked file');
94
95# directly symlinked dir
96	$t->{REQUEST} = ( <<EOF
97GET /symlinked/ HTTP/1.0
98Host: nosymlink.example.org
99EOF
100 );
101	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 403 } ];
102	ok($tf->handle_http($t) == 0, 'deny: directly symlinked dir');
103
104# symlinked dir in path
105	$t->{REQUEST} = ( <<EOF
106GET /symlinked/access.txt HTTP/1.0
107Host: nosymlink.example.org
108EOF
109 );
110	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 403 } ];
111	ok($tf->handle_http($t) == 0, 'deny: symlinked dir in path');
112
113# cleanup
114    ok($tf->stop_proc == 0, "Stopping lighttpd");
115};
116