Mercurial > hg > nginx-tests
annotate userid.t @ 1263:ea3c7659b6c1
Tests: handled early pidfile write on win32 in the run() routine.
In addition to the present waiting for pidfile, which is insufficient on win32
due to the CreateProcess model, and may lead to rare startup races, search now
for the certain error message which indicates started worker process.
This change allows tolerating moderate hiccups on win32 hosts.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Tue, 19 Dec 2017 19:55:01 +0300 |
parents | 382011b269f1 |
children | 2d7fcd713b66 |
rev | line source |
---|---|
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 | |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
15 use Config; |
604 | 16 use MIME::Base64; |
17 use Time::Local; | |
18 | |
19 BEGIN { use FindBin; chdir($FindBin::Bin); } | |
20 | |
21 use lib 'lib'; | |
22 use Test::Nginx; | |
23 | |
24 ############################################################################### | |
25 | |
26 select STDERR; $| = 1; | |
27 select STDOUT; $| = 1; | |
28 | |
29 my $t = Test::Nginx->new()->has(qw/http userid map/)->plan(33); | |
30 | |
31 $t->write_file_expand('nginx.conf', <<'EOF'); | |
32 | |
33 %%TEST_GLOBALS%% | |
34 | |
35 daemon off; | |
36 | |
37 events { | |
38 } | |
39 | |
40 http { | |
41 %%TEST_GLOBALS_HTTP%% | |
42 | |
43 map $args $uid_reset { | |
44 default 0; | |
45 value 1; | |
46 log log; | |
47 } | |
48 | |
49 server { | |
974
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
50 listen 127.0.0.1:8080; |
604 | 51 server_name localhost; |
52 | |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
53 add_header X-Got $uid_got; |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
54 add_header X-Reset $uid_reset; |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
55 add_header X-Set $uid_set; |
604 | 56 userid on; |
57 | |
58 location / { | |
1177
382011b269f1
Tests: do not inadvertently disable debug logging in userid.t.
Sergey Kandaurov <pluknet@nginx.com>
parents:
974
diff
changeset
|
59 error_log %%TESTDIR%%/error.log debug; |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
60 error_log %%TESTDIR%%/error_reset.log info; |
604 | 61 } |
62 | |
63 location /name { | |
64 userid_name test; | |
65 } | |
66 | |
67 location /path { | |
68 userid_path /0123456789; | |
69 | |
70 location /path/r { | |
71 userid_path /9876543210; | |
72 } | |
73 } | |
74 | |
75 location /domain { | |
76 userid_domain test.domain; | |
77 } | |
78 | |
79 location /mark_off { | |
80 userid_mark off; | |
81 } | |
82 location /mark_eq { | |
83 userid_mark =; | |
84 } | |
85 location /mark_let { | |
86 userid_mark t; | |
87 } | |
88 location /mark_num { | |
89 userid_mark 9; | |
90 } | |
91 | |
92 location /expires_time { | |
93 add_header X-Msec $msec; | |
94 userid_expires 100; | |
95 } | |
96 location /expires_max { | |
97 userid_expires max; | |
98 | |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
99 location /expires_max/off { |
604 | 100 userid_expires off; |
101 } | |
102 } | |
103 location /expires_off { | |
104 userid_expires off; | |
105 } | |
106 | |
107 location /p3p { | |
108 userid_p3p policyref="/w3c/p3p.xml"; | |
109 } | |
110 | |
111 location /service { | |
112 userid_service 65534; | |
113 } | |
114 | |
115 location /cv1 { | |
116 userid v1; | |
117 userid_mark t; | |
118 } | |
119 | |
120 location /clog { | |
121 userid log; | |
122 } | |
123 | |
124 location /coff { | |
125 userid off; | |
126 } | |
127 } | |
128 } | |
129 | |
130 EOF | |
131 | |
132 $t->write_file('index.html', ''); | |
133 $t->write_file('expires_time', ''); | |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
134 $t->write_file('service', ''); |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
135 $t->write_file('cv1', ''); |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
136 $t->write_file('clog', ''); |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
137 $t->write_file('coff', ''); |
604 | 138 $t->run(); |
139 | |
140 ############################################################################### | |
141 | |
142 # userid | |
143 | |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
144 like(http_get('/'), qr/Set-Cookie:/, 'cookie on'); |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
145 like(http_get('/cv1'), qr/Set-Cookie:/, 'cookie v1'); |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
146 unlike(http_get('/clog'), qr/Set-Cookie:/, 'cookie log'); |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
147 unlike(http_get('/coff'), qr/Set-Cookie:/, 'cookie off'); |
604 | 148 |
149 # default | |
150 | |
151 my %cookie = get_cookie('/'); | |
152 isnt($cookie{'uid'}, undef, 'name default'); | |
153 is($cookie{'path'}, '/', 'path default'); | |
154 is($cookie{'domain'}, undef, 'domain default'); | |
155 is($cookie{'expires'}, undef, 'expires default'); | |
156 like($cookie{'uid'}, '/\w+={0,2}$/', 'mark default'); | |
817
ada7d1ad985b
Tests: fixed occasional negative match failure in userid.t.
Sergey Kandaurov <pluknet@nginx.com>
parents:
614
diff
changeset
|
157 unlike(http_get('/'), qr/^P3P/m, 'p3p default'); |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
158 like(http_get('/'), qr/X-Reset: 0/, 'uid reset variable default'); |
604 | 159 |
160 # name, path, domain and p3p | |
161 | |
162 isnt(get_cookie('/name', 'test'), undef, 'name'); | |
163 is(get_cookie('/path', 'path'), '/0123456789', 'path'); | |
164 is(get_cookie('/domain', 'domain'), 'test.domain', 'domain'); | |
165 like(http_get('/p3p'), qr!P3P: policyref="/w3c/p3p.xml"!, 'p3p'); | |
166 | |
167 # mark | |
168 | |
169 like(get_cookie('/mark_off', 'uid'), '/\w+={0,2}$/', 'mark off'); | |
170 like(get_cookie('/mark_eq', 'uid'), '/==$/', 'mark equal'); | |
171 like(get_cookie('/mark_let', 'uid'), '/t=$/', 'mark letter'); | |
172 like(get_cookie('/mark_num', 'uid'), '/9=$/', 'mark number'); | |
173 | |
174 # expires | |
175 | |
176 my $r = http_get('/expires_time'); | |
177 my ($t1) = $r =~ /X-Msec: (\d+)/; | |
178 is(expires2timegm(cookie($r, 'expires')), $t1 + 100, 'expires time'); | |
179 is(get_cookie('/expires_max', 'expires'), 'Thu, 31-Dec-37 23:55:55 GMT', | |
180 'expires max'); | |
181 is(get_cookie('/expires_off', 'expires'), undef, 'expires off'); | |
182 | |
183 # redefinition | |
184 | |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
185 unlike(http_get('/expires_max/off'), qr/expires/, 'redefine expires'); |
604 | 186 like(http_get('/path/r'), qr!/9876543210!, 'redefine path'); |
187 | |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
188 # requests |
604 | 189 |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
190 $r = http_get('/'); |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
191 my ($uid) = uid_set($r); |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
192 isnt($uid, undef, 'uid set variable'); |
604 | 193 |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
194 $r = send_uid('/', cookie($r, 'uid')); |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
195 is(uid_got($r), $uid, 'uid got variable'); |
604 | 196 unlike($r, qr/Set-Cookie:/, 'same path request'); |
197 | |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
198 $r = send_uid('/coff', $uid); |
604 | 199 unlike($r, qr/Set-Cookie:/, 'other path request'); |
200 | |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
201 $r = send_uid('/?value', $uid); |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
202 like($r, qr/Set-Cookie:/, 'uid reset variable value'); |
604 | 203 |
204 # service | |
205 | |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
206 is(substr(uid_set(http_get('/cv1')), 0, 8), '00000000', 'service default v1'); |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
207 |
614
0597ca82c26a
Tests: respected byteorder value on longsize=4 but USE_64_BIT_INT.
Sergey Kandaurov <pluknet@nginx.com>
parents:
605
diff
changeset
|
208 my $bigendian = $Config{byteorder} =~ '1234' ? 0 : 1; |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
209 my $addr = $bigendian ? "7F000001" : "0100007F"; |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
210 is(substr(uid_set(http_get('/')), 0, 8), $addr, 'service default v2'); |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
211 |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
212 $addr = $bigendian ? "0000FFFE" : "FEFF0000"; |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
213 is(substr(uid_set(http_get('/service')), 0, 8), $addr, 'service custom'); |
604 | 214 |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
215 # reset log |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
216 |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
217 send_uid('/?log', cookie($r, 'uid')); |
604 | 218 |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
219 $t->stop(); |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
220 |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
221 like($t->read_file('error_reset.log'), |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
222 '/userid cookie "uid=\w+" was reset/m', 'uid reset variable log'); |
604 | 223 |
224 ############################################################################### | |
225 | |
226 sub cookie { | |
227 my ($r, $key) = @_; | |
228 my %cookie; | |
229 | |
230 $r =~ /(Set-Cookie:[^\x0d]*).*\x0d\x0a?\x0d/ms; | |
231 if ($1) { | |
232 %cookie = $1 =~ /(\w+)=([^;]+)/g; | |
233 } | |
234 | |
235 return $cookie{$key} if defined $key; | |
236 return %cookie; | |
237 } | |
238 | |
239 sub get_cookie { | |
240 my ($url, $key) = @_; | |
241 return cookie(http_get($url), $key); | |
242 } | |
243 | |
244 sub expires2timegm { | |
245 my ($e) = @_; | |
246 my %months = (Jan => 0, Feb => 1, Mar => 2, Apr => 3, May =>4, Jun => 5, | |
247 Jul => 6, Aug => 7, Sep => 8, Oct => 9, Nov => 10, Dec => 11); | |
248 | |
249 my ($w, $date, $time) = split(" ", $e); | |
250 my ($day, $month, $year) = split("-", $date); | |
251 my ($hour, $min, $sec) = split(":", $time); | |
252 | |
253 return timegm($sec, $min, $hour, $day, $months{$month}, $year); | |
254 } | |
255 | |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
256 sub uid_set { |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
257 my ($r) = @_; |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
258 my ($uid) = $r =~ /X-Set: uid=(.*)\n/m; |
604 | 259 return $uid; |
260 } | |
261 | |
605
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
262 sub uid_got { |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
263 my ($r) = @_; |
a77f19282f63
Tests: switched to using headers to test variables in userid.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
604
diff
changeset
|
264 my ($uid) = $r =~ /X-Got: uid=(.*)\n/m; |
604 | 265 return $uid; |
266 } | |
267 | |
268 sub send_uid { | |
269 my ($url, $uid) = @_; | |
270 return http(<<EOF); | |
271 GET $url HTTP/1.0 | |
272 Host: localhost | |
273 Cookie: uid=$uid | |
274 | |
275 EOF | |
276 } | |
277 | |
278 ############################################################################### |