comparison http_disable_symlinks.t @ 201:fc297a64142e

Tests: disable_symlinks directive tests added.
author Andrey Belov <defan@nginx.com>
date Mon, 06 Feb 2012 21:45:56 +0400
parents
children 067b6cb4793d
comparison
equal deleted inserted replaced
200:0ca8725e5958 201:fc297a64142e
1 #!/usr/bin/perl
2
3 # (C) Andrey Belov
4
5 # Tests for disable_symlinks directive.
6
7 ###############################################################################
8
9 use warnings;
10 use strict;
11
12 use Test::More;
13 use POSIX;
14
15 BEGIN { use FindBin; chdir($FindBin::Bin); }
16
17 use lib 'lib';
18 use Test::Nginx;
19
20 ###############################################################################
21
22 select STDERR; $| = 1;
23 select STDOUT; $| = 1;
24
25 my $t = Test::Nginx->new()->has(qw/http rewrite/)
26 ->write_file_expand('nginx.conf', <<'EOF');
27
28 %%TEST_GLOBALS%%
29
30 daemon off;
31
32 events {
33 }
34
35 http {
36 %%TEST_GLOBALS_HTTP%%
37
38 server {
39 listen 127.0.0.1:8080;
40 server_name s1;
41
42 location /on/ {
43 disable_symlinks on;
44 }
45
46 location /not_owner/ {
47 disable_symlinks if_not_owner;
48 }
49
50 location /try_on/ {
51 disable_symlinks on;
52 try_files $uri $uri.html =404;
53 }
54
55 location /try_not_owner/ {
56 disable_symlinks if_not_owner;
57 try_files $uri $uri.txt =404;
58 }
59
60 location /if_on/ {
61 disable_symlinks on;
62 if (-f $request_filename) {
63 return 204;
64 }
65 }
66
67 location /if_not_owner/ {
68 disable_symlinks if_not_owner;
69 if (-f $request_filename) {
70 return 204;
71 }
72 }
73 }
74
75 server {
76 listen 127.0.0.1:8080;
77 server_name s2;
78
79 open_file_cache max=16 inactive=60s;
80 open_file_cache_valid 30s;
81 open_file_cache_min_uses 1;
82 open_file_cache_errors on;
83
84 location /cached-off/ {
85 disable_symlinks off;
86 alias %%TESTDIR%%/cached/;
87 }
88
89 location /cached-on/ {
90 disable_symlinks on;
91 alias %%TESTDIR%%/cached/;
92 }
93
94 location /cached-if-not-owner/ {
95 disable_symlinks if_not_owner;
96 alias %%TESTDIR%%/cached/;
97 }
98
99 location / {
100 disable_symlinks off;
101 }
102 }
103 }
104
105 EOF
106
107 eval {
108 open OLDERR, ">&", \*STDERR; close STDERR;
109 $t->run();
110 open STDERR, ">&", \*OLDERR;
111 };
112 plan(skip_all => 'no disable_symlinks') if $@;
113
114 my $uid = getuid();
115
116 my @extfiles = ('/etc/resolv.conf', '/etc/protocols', '/etc/host.conf');
117 my $extfile = undef;
118
119 foreach (@extfiles) {
120 if (-f "$_" && $uid != (stat($_))[4]) {
121 $extfile = $_;
122 last;
123 }
124 }
125
126 if (defined($extfile)) {
127 $t->plan(17);
128 } else {
129 plan(skip_all => 'external suitable object not found');
130 }
131
132 my $d = $t->testdir();
133
134 mkdir("$d/on");
135 mkdir("$d/not_owner");
136 mkdir("$d/try_on");
137 mkdir("$d/try_not_owner");
138 mkdir("$d/if_on");
139 mkdir("$d/if_not_owner");
140 mkdir("$d/cached");
141
142 $t->write_file("empty.html", "");
143 symlink("empty.html", "$d/link");
144 symlink($extfile, "$d/link2");
145
146 $t->write_file("on/empty.html", "");
147 symlink("empty.html", "$d/on/link");
148 symlink($extfile, "$d/on/link2");
149
150 $t->write_file("not_owner/empty.html", "");
151 symlink("empty.html", "$d/not_owner/link");
152 symlink($extfile, "$d/not_owner/link2");
153
154 $t->write_file("try_on/try.html", "LOCAL TRY");
155 symlink($extfile, "$d/try_on/try");
156
157 $t->write_file("try_not_owner/try.html", "LOCAL TRY");
158 symlink($extfile, "$d/try_not_owner/try");
159 symlink("try.html", "$d/try_not_owner/try.txt");
160
161 $t->write_file("if_on/empty.html", "");
162 symlink("empty.html", "$d/if_on/link");
163 symlink($extfile, "$d/if_on/link2");
164
165 $t->write_file("if_not_owner/empty.html", "");
166 symlink("empty.html", "$d/if_not_owner/link");
167 symlink($extfile, "$d/if_not_owner/link2");
168
169 symlink($extfile, "$d/cached/link");
170
171 ###############################################################################
172
173 like(http_get_host('s1', '/link'), qr!200 OK!, 'static (off, same uid)');
174 like(http_get_host('s1', '/link2'), qr!200 OK!, 'static (off, other uid)');
175
176 like(http_get_host('s1', '/on/link'), qr!403 Forbidden!,
177 'static (on, same uid)');
178 like(http_get_host('s1', '/on/link2'), qr!403 Forbidden!,
179 'static (on, other uid)');
180
181 like(http_get_host('s1', '/not_owner/link'), qr!200 OK!,
182 'static (if_not_owner, same uid)');
183 like(http_get_host('s1', '/not_owner/link2'), qr!403 Forbidden!,
184 'static (if_not_owner, other uid)');
185
186 like(http_get_host('s1', '/try_on/try'), qr/LOCAL TRY/,
187 'try_files (on)');
188 like(http_get_host('s1', '/try_not_owner/try'), qr/LOCAL TRY/,
189 'try_files (if_not_owner)');
190
191 like(http_get_host('s1', '/if_on/link'), qr!403 Forbidden!,
192 'if (on, same uid)');
193 like(http_get_host('s1', '/if_on/link2'), qr!403 Forbidden!,
194 'if (on, other uid)');
195
196 like(http_get_host('s1', '/if_not_owner/link'), qr!204 No Content!,
197 'if (if_not_owner, same uid)');
198 like(http_get_host('s1', '/if_not_owner/link2'), qr!403 Forbidden!,
199 'if (if_not_owner, other uid)');
200
201 like(http_get_host('s2', '/cached-off/link'), qr!200 OK!,
202 'open_file_cache (pass #1)');
203 like(http_get_host('s2', '/cached-on/link'), qr!403 Forbidden!,
204 'open_file_cache (pass #2)');
205 like(http_get_host('s2', '/cached-off/link'), qr!200 OK!,
206 'open_file_cache (pass #3)');
207 like(http_get_host('s2', '/cached-if-not-owner/link'), qr!403 Forbidden!,
208 'open_file_cache (pass #4)');
209 like(http_get_host('s2', '/cached-off/link'), qr!200 OK!,
210 'open_file_cache (pass #5)');
211
212 ###############################################################################
213
214 sub http_get_host {
215 my ($host, $url) = @_;
216 return http(<<EOF);
217 GET $url HTTP/1.0
218 Host: $host
219
220 EOF
221 }
222
223 ###############################################################################