604
|
1 #!/usr/bin/perl
|
|
2
|
|
3 # (C) Andrey Zelenkov
|
|
4 # (C) Nginx, Inc.
|
|
5
|
|
6 # Tests for userid filter module.
|
|
7
|
|
8 ###############################################################################
|
|
9
|
|
10 use warnings;
|
|
11 use strict;
|
|
12
|
|
13 use Test::More;
|
|
14
|
|
15 use MIME::Base64;
|
|
16 use Time::Local;
|
|
17
|
|
18 BEGIN { use FindBin; chdir($FindBin::Bin); }
|
|
19
|
|
20 use lib 'lib';
|
|
21 use Test::Nginx;
|
|
22
|
|
23 ###############################################################################
|
|
24
|
|
25 select STDERR; $| = 1;
|
|
26 select STDOUT; $| = 1;
|
|
27
|
|
28 my $t = Test::Nginx->new()->has(qw/http userid map/)->plan(33);
|
|
29
|
|
30 $t->write_file_expand('nginx.conf', <<'EOF');
|
|
31
|
|
32 %%TEST_GLOBALS%%
|
|
33
|
|
34 daemon off;
|
|
35
|
|
36 events {
|
|
37 }
|
|
38
|
|
39 http {
|
|
40 %%TEST_GLOBALS_HTTP%%
|
|
41
|
|
42 log_format uid '$uid_got:$uid_set';
|
|
43 access_log %%TESTDIR%%/userid.log uid;
|
|
44
|
|
45 map $args $uid_reset {
|
|
46 default 0;
|
|
47 value 1;
|
|
48 log log;
|
|
49 }
|
|
50
|
|
51 server {
|
|
52 listen 127.0.0.1:8080;
|
|
53 server_name localhost;
|
|
54
|
|
55 userid on;
|
|
56
|
|
57 location / {
|
|
58 add_header X-Reset $uid_reset;
|
|
59 }
|
|
60
|
|
61 location /name {
|
|
62 userid_name test;
|
|
63 }
|
|
64
|
|
65 location /path {
|
|
66 userid_path /0123456789;
|
|
67
|
|
68 location /path/r {
|
|
69 userid_path /9876543210;
|
|
70 }
|
|
71 }
|
|
72
|
|
73 location /domain {
|
|
74 userid_domain test.domain;
|
|
75 }
|
|
76
|
|
77 location /mark_off {
|
|
78 userid_mark off;
|
|
79 }
|
|
80 location /mark_eq {
|
|
81 userid_mark =;
|
|
82 }
|
|
83 location /mark_let {
|
|
84 userid_mark t;
|
|
85 }
|
|
86 location /mark_num {
|
|
87 userid_mark 9;
|
|
88 }
|
|
89
|
|
90 location /expires_time {
|
|
91 add_header X-Msec $msec;
|
|
92 userid_expires 100;
|
|
93 }
|
|
94 location /expires_max {
|
|
95 userid_expires max;
|
|
96
|
|
97 location /expires_max/r {
|
|
98 userid_expires off;
|
|
99 }
|
|
100 }
|
|
101 location /expires_off {
|
|
102 userid_expires off;
|
|
103 }
|
|
104
|
|
105 location /p3p {
|
|
106 userid_p3p policyref="/w3c/p3p.xml";
|
|
107 }
|
|
108
|
|
109 location /service {
|
|
110 userid_service 65534;
|
|
111 }
|
|
112
|
|
113 location /cv1 {
|
|
114 userid v1;
|
|
115 userid_mark t;
|
|
116 }
|
|
117
|
|
118 location /clog {
|
|
119 userid log;
|
|
120 }
|
|
121
|
|
122 location /coff {
|
|
123 userid off;
|
|
124 }
|
|
125 }
|
|
126 }
|
|
127
|
|
128 EOF
|
|
129
|
|
130 $t->write_file('index.html', '');
|
|
131 $t->write_file('expires_time', '');
|
|
132 $t->run();
|
|
133
|
|
134 ###############################################################################
|
|
135
|
|
136 # userid
|
|
137
|
|
138 like(http_get('/'), qr/Set-Cookie:/, 'on cookie');
|
|
139 like(http_get('/cv1'), qr/Set-Cookie:/, 'v1 cookie');
|
|
140 unlike(http_get('/clog'), qr/Set-Cookie:/, 'log no cookie');
|
|
141 unlike(http_get('/coff'), qr/Set-Cookie:/, 'off no cookie');
|
|
142
|
|
143 # default
|
|
144
|
|
145 my %cookie = get_cookie('/');
|
|
146 isnt($cookie{'uid'}, undef, 'name default');
|
|
147 is($cookie{'path'}, '/', 'path default');
|
|
148 is($cookie{'domain'}, undef, 'domain default');
|
|
149 is($cookie{'expires'}, undef, 'expires default');
|
|
150 like($cookie{'uid'}, '/\w+={0,2}$/', 'mark default');
|
|
151 unlike(http_get('/'), qr/P3P/, 'p3p default');
|
|
152 like(http_get('/'), qr/X-Reset: 0/, 'reset var default');
|
|
153
|
|
154 # name, path, domain and p3p
|
|
155
|
|
156 isnt(get_cookie('/name', 'test'), undef, 'name');
|
|
157 is(get_cookie('/path', 'path'), '/0123456789', 'path');
|
|
158 is(get_cookie('/domain', 'domain'), 'test.domain', 'domain');
|
|
159 like(http_get('/p3p'), qr!P3P: policyref="/w3c/p3p.xml"!, 'p3p');
|
|
160
|
|
161 # mark
|
|
162
|
|
163 like(get_cookie('/mark_off', 'uid'), '/\w+={0,2}$/', 'mark off');
|
|
164 like(get_cookie('/mark_eq', 'uid'), '/==$/', 'mark equal');
|
|
165 like(get_cookie('/mark_let', 'uid'), '/t=$/', 'mark letter');
|
|
166 like(get_cookie('/mark_num', 'uid'), '/9=$/', 'mark number');
|
|
167
|
|
168 # expires
|
|
169
|
|
170 my $r = http_get('/expires_time');
|
|
171 my ($t1) = $r =~ /X-Msec: (\d+)/;
|
|
172 is(expires2timegm(cookie($r, 'expires')), $t1 + 100, 'expires time');
|
|
173 is(get_cookie('/expires_max', 'expires'), 'Thu, 31-Dec-37 23:55:55 GMT',
|
|
174 'expires max');
|
|
175 is(get_cookie('/expires_off', 'expires'), undef, 'expires off');
|
|
176
|
|
177 # redefinition
|
|
178
|
|
179 unlike(http_get('/expires_max/r'), qr/expires/, 'redefine expires');
|
|
180 like(http_get('/path/r'), qr!/9876543210!, 'redefine path');
|
|
181
|
|
182 # log and requests
|
|
183
|
|
184 my $uidc = get_cookie('/', 'uid');
|
|
185 my $uidl = last_set($t);
|
|
186 isnt($uidl, undef, 'log uid_set');
|
|
187
|
|
188 $r = send_uid('/', $uidc);
|
|
189 is(last_got($t), $uidl, 'log uid_got');
|
|
190 unlike($r, qr/Set-Cookie:/, 'same path request');
|
|
191
|
|
192 $r = send_uid('/coff', $uidc);
|
|
193 unlike($r, qr/Set-Cookie:/, 'other path request');
|
|
194
|
|
195 $r = send_uid('/?value', $uidc);
|
|
196 like($r, qr/Set-Cookie:/, 'reset request');
|
|
197
|
|
198 $uidc = cookie($r, 'uid');
|
|
199 $r = send_uid('/?log', $uidc);
|
|
200 isnt(last_got($t), $uidc, 'log reset request');
|
|
201
|
|
202 # service
|
|
203
|
|
204 http_get('/cv1');
|
|
205 is(substr(last_set($t), 0, 8), '00000000', 'service default v1');
|
|
206
|
|
207 http_get('/');
|
|
208 like(substr(last_set($t), 0, 8), '/[0100007F|F7000010|00000000]/',
|
|
209 'service default v2');
|
|
210
|
|
211 http_get('/service');
|
|
212 like(substr(last_set($t), 0, 8), '/[0000FEFF|FEFF0000]/', 'service custom');
|
|
213
|
|
214 ###############################################################################
|
|
215
|
|
216 sub cookie {
|
|
217 my ($r, $key) = @_;
|
|
218 my %cookie;
|
|
219
|
|
220 $r =~ /(Set-Cookie:[^\x0d]*).*\x0d\x0a?\x0d/ms;
|
|
221 if ($1) {
|
|
222 %cookie = $1 =~ /(\w+)=([^;]+)/g;
|
|
223 }
|
|
224
|
|
225 return $cookie{$key} if defined $key;
|
|
226 return %cookie;
|
|
227 }
|
|
228
|
|
229 sub get_cookie {
|
|
230 my ($url, $key) = @_;
|
|
231 return cookie(http_get($url), $key);
|
|
232 }
|
|
233
|
|
234 sub expires2timegm {
|
|
235 my ($e) = @_;
|
|
236 my %months = (Jan => 0, Feb => 1, Mar => 2, Apr => 3, May =>4, Jun => 5,
|
|
237 Jul => 6, Aug => 7, Sep => 8, Oct => 9, Nov => 10, Dec => 11);
|
|
238
|
|
239 my ($w, $date, $time) = split(" ", $e);
|
|
240 my ($day, $month, $year) = split("-", $date);
|
|
241 my ($hour, $min, $sec) = split(":", $time);
|
|
242
|
|
243 return timegm($sec, $min, $hour, $day, $months{$month}, $year);
|
|
244 }
|
|
245
|
|
246 sub last_got {
|
|
247 my ($t) = @_;
|
|
248 my $log = $t->read_file('userid.log');
|
|
249 my ($uid) = $log =~ /uid=(.*):.*\n$/m;
|
|
250 return $uid;
|
|
251 }
|
|
252
|
|
253 sub last_set {
|
|
254 my ($t) = @_;
|
|
255 my $log = $t->read_file('userid.log');
|
|
256 my ($uid) = $log =~ /:uid=(.*)\n$/m;
|
|
257 return $uid;
|
|
258 }
|
|
259
|
|
260 sub send_uid {
|
|
261 my ($url, $uid) = @_;
|
|
262 return http(<<EOF);
|
|
263 GET $url HTTP/1.0
|
|
264 Host: localhost
|
|
265 Cookie: uid=$uid
|
|
266
|
|
267 EOF
|
|
268 }
|
|
269
|
|
270 ###############################################################################
|