test_php_application.py (1529:7cd42e6d8c20) test_php_application.py (1596:b7e2d4d92624)
1import os
1import os
2import pytest
2import re
3import shutil
4import time
3import re
4import shutil
5import time
5import unittest
6
7from unit.applications.lang.php import TestApplicationPHP
6
7from unit.applications.lang.php import TestApplicationPHP
8from conftest import option
8
9class TestPHPApplication(TestApplicationPHP):
10 prerequisites = {'modules': {'php': 'all'}}
11
12 def before_disable_functions(self):
13 body = self.get()['body']
14
9
10class TestPHPApplication(TestApplicationPHP):
11 prerequisites = {'modules': {'php': 'all'}}
12
13 def before_disable_functions(self):
14 body = self.get()['body']
15
15 self.assertRegex(body, r'time: \d+', 'disable_functions before time')
16 self.assertRegex(body, r'exec: \/\w+', 'disable_functions before exec')
16 assert re.search(r'time: \d+', body), 'disable_functions before time'
17 assert re.search(r'exec: \/\w+', body), 'disable_functions before exec'
17
18 def set_opcache(self, app, val):
18
19 def set_opcache(self, app, val):
19 self.assertIn(
20 'success',
21 self.conf(
22 {
23 "admin": {
24 "opcache.enable": val,
25 "opcache.enable_cli": val,
26 },
27 },
28 'applications/' + app + '/options',
29 ),
20 assert 'success' in self.conf(
21 {"admin": {"opcache.enable": val, "opcache.enable_cli": val,},},
22 'applications/' + app + '/options',
30 )
31
32 opcache = self.get()['headers']['X-OPcache']
33
34 if not opcache or opcache == '-1':
23 )
24
25 opcache = self.get()['headers']['X-OPcache']
26
27 if not opcache or opcache == '-1':
35 print('opcache is not supported')
36 raise unittest.SkipTest()
28 pytest.skip('opcache is not supported')
37
29
38 self.assertEqual(opcache, val, 'opcache value')
30 assert opcache == val, 'opcache value'
39
40 def test_php_application_variables(self):
41 self.load('variables')
42
43 body = 'Test body string.'
44
45 resp = self.post(
46 headers={
47 'Host': 'localhost',
48 'Content-Type': 'text/html',
49 'Custom-Header': 'blah',
50 'Connection': 'close',
51 },
52 body=body,
31
32 def test_php_application_variables(self):
33 self.load('variables')
34
35 body = 'Test body string.'
36
37 resp = self.post(
38 headers={
39 'Host': 'localhost',
40 'Content-Type': 'text/html',
41 'Custom-Header': 'blah',
42 'Connection': 'close',
43 },
44 body=body,
53 url='/index.php/blah?var=val'
45 url='/index.php/blah?var=val',
54 )
55
46 )
47
56 self.assertEqual(resp['status'], 200, 'status')
48 assert resp['status'] == 200, 'status'
57 headers = resp['headers']
58 header_server = headers.pop('Server')
49 headers = resp['headers']
50 header_server = headers.pop('Server')
59 self.assertRegex(header_server, r'Unit/[\d\.]+', 'server header')
60 self.assertEqual(
61 headers.pop('Server-Software'),
62 header_server,
63 'server software header',
64 )
51 assert re.search(r'Unit/[\d\.]+', header_server), 'server header'
52 assert (
53 headers.pop('Server-Software') == header_server
54 ), 'server software header'
65
66 date = headers.pop('Date')
55
56 date = headers.pop('Date')
67 self.assertEqual(date[-4:], ' GMT', 'date header timezone')
68 self.assertLess(
69 abs(self.date_to_sec_epoch(date) - self.sec_epoch()),
70 5,
71 'date header',
72 )
57 assert date[-4:] == ' GMT', 'date header timezone'
58 assert (
59 abs(self.date_to_sec_epoch(date) - self.sec_epoch()) < 5
60 ), 'date header'
73
74 if 'X-Powered-By' in headers:
75 headers.pop('X-Powered-By')
76
77 headers.pop('Content-type')
61
62 if 'X-Powered-By' in headers:
63 headers.pop('X-Powered-By')
64
65 headers.pop('Content-type')
78 self.assertDictEqual(
79 headers,
80 {
81 'Connection': 'close',
82 'Content-Length': str(len(body)),
83 'Request-Method': 'POST',
84 'Path-Info': '/blah',
85 'Request-Uri': '/index.php/blah?var=val',
86 'Http-Host': 'localhost',
87 'Server-Protocol': 'HTTP/1.1',
88 'Custom-Header': 'blah',
89 },
90 'headers',
91 )
92 self.assertEqual(resp['body'], body, 'body')
66 assert headers == {
67 'Connection': 'close',
68 'Content-Length': str(len(body)),
69 'Request-Method': 'POST',
70 'Path-Info': '/blah',
71 'Request-Uri': '/index.php/blah?var=val',
72 'Http-Host': 'localhost',
73 'Server-Protocol': 'HTTP/1.1',
74 'Custom-Header': 'blah',
75 }, 'headers'
76 assert resp['body'] == body, 'body'
93
94 def test_php_application_query_string(self):
95 self.load('query_string')
96
97 resp = self.get(url='/?var1=val1&var2=val2')
98
77
78 def test_php_application_query_string(self):
79 self.load('query_string')
80
81 resp = self.get(url='/?var1=val1&var2=val2')
82
99 self.assertEqual(
100 resp['headers']['Query-String'],
101 'var1=val1&var2=val2',
102 'query string',
103 )
83 assert (
84 resp['headers']['Query-String'] == 'var1=val1&var2=val2'
85 ), 'query string'
104
105 def test_php_application_query_string_empty(self):
106 self.load('query_string')
107
108 resp = self.get(url='/?')
109
86
87 def test_php_application_query_string_empty(self):
88 self.load('query_string')
89
90 resp = self.get(url='/?')
91
110 self.assertEqual(resp['status'], 200, 'query string empty status')
111 self.assertEqual(
112 resp['headers']['Query-String'], '', 'query string empty'
113 )
92 assert resp['status'] == 200, 'query string empty status'
93 assert resp['headers']['Query-String'] == '', 'query string empty'
114
115 def test_php_application_query_string_absent(self):
116 self.load('query_string')
117
118 resp = self.get()
119
94
95 def test_php_application_query_string_absent(self):
96 self.load('query_string')
97
98 resp = self.get()
99
120 self.assertEqual(resp['status'], 200, 'query string absent status')
121 self.assertEqual(
122 resp['headers']['Query-String'], '', 'query string absent'
123 )
100 assert resp['status'] == 200, 'query string absent status'
101 assert resp['headers']['Query-String'] == '', 'query string absent'
124
125 def test_php_application_phpinfo(self):
126 self.load('phpinfo')
127
128 resp = self.get()
129
102
103 def test_php_application_phpinfo(self):
104 self.load('phpinfo')
105
106 resp = self.get()
107
130 self.assertEqual(resp['status'], 200, 'status')
131 self.assertNotEqual(resp['body'], '', 'body not empty')
108 assert resp['status'] == 200, 'status'
109 assert resp['body'] != '', 'body not empty'
132
133 def test_php_application_header_status(self):
134 self.load('header')
135
110
111 def test_php_application_header_status(self):
112 self.load('header')
113
136 self.assertEqual(
114 assert (
137 self.get(
138 headers={
139 'Host': 'localhost',
140 'Connection': 'close',
141 'X-Header': 'HTTP/1.1 404 Not Found',
142 }
115 self.get(
116 headers={
117 'Host': 'localhost',
118 'Connection': 'close',
119 'X-Header': 'HTTP/1.1 404 Not Found',
120 }
143 )['status'],
144 404,
145 'status',
146 )
121 )['status']
122 == 404
123 ), 'status'
147
124
148 self.assertEqual(
125 assert (
149 self.get(
150 headers={
151 'Host': 'localhost',
152 'Connection': 'close',
153 'X-Header': 'http/1.1 404 Not Found',
154 }
126 self.get(
127 headers={
128 'Host': 'localhost',
129 'Connection': 'close',
130 'X-Header': 'http/1.1 404 Not Found',
131 }
155 )['status'],
156 404,
157 'status case insensitive',
158 )
132 )['status']
133 == 404
134 ), 'status case insensitive'
159
135
160 self.assertEqual(
136 assert (
161 self.get(
162 headers={
163 'Host': 'localhost',
164 'Connection': 'close',
165 'X-Header': 'HTTP/ 404 Not Found',
166 }
137 self.get(
138 headers={
139 'Host': 'localhost',
140 'Connection': 'close',
141 'X-Header': 'HTTP/ 404 Not Found',
142 }
167 )['status'],
168 404,
169 'status version empty',
170 )
143 )['status']
144 == 404
145 ), 'status version empty'
171
146
172
173 def test_php_application_404(self):
174 self.load('404')
175
176 resp = self.get()
177
147 def test_php_application_404(self):
148 self.load('404')
149
150 resp = self.get()
151
178 self.assertEqual(resp['status'], 404, '404 status')
179 self.assertRegex(
180 resp['body'], r'<title>404 Not Found</title>', '404 body'
181 )
152 assert resp['status'] == 404, '404 status'
153 assert re.search(
154 r'<title>404 Not Found</title>', resp['body']
155 ), '404 body'
182
183 def test_php_application_keepalive_body(self):
184 self.load('mirror')
185
156
157 def test_php_application_keepalive_body(self):
158 self.load('mirror')
159
186 self.assertEqual(self.get()['status'], 200, 'init')
160 assert self.get()['status'] == 200, 'init'
187
188 body = '0123456789' * 500
189 (resp, sock) = self.post(
190 headers={
191 'Host': 'localhost',
192 'Connection': 'keep-alive',
193 'Content-Type': 'text/html',
194 },
195 start=True,
196 body=body,
197 read_timeout=1,
198 )
199
161
162 body = '0123456789' * 500
163 (resp, sock) = self.post(
164 headers={
165 'Host': 'localhost',
166 'Connection': 'keep-alive',
167 'Content-Type': 'text/html',
168 },
169 start=True,
170 body=body,
171 read_timeout=1,
172 )
173
200 self.assertEqual(resp['body'], body, 'keep-alive 1')
174 assert resp['body'] == body, 'keep-alive 1'
201
202 body = '0123456789'
203 resp = self.post(
204 headers={
205 'Host': 'localhost',
206 'Connection': 'close',
207 'Content-Type': 'text/html',
208 },
209 sock=sock,
210 body=body,
211 )
212
175
176 body = '0123456789'
177 resp = self.post(
178 headers={
179 'Host': 'localhost',
180 'Connection': 'close',
181 'Content-Type': 'text/html',
182 },
183 sock=sock,
184 body=body,
185 )
186
213 self.assertEqual(resp['body'], body, 'keep-alive 2')
187 assert resp['body'] == body, 'keep-alive 2'
214
215 def test_php_application_conditional(self):
216 self.load('conditional')
217
188
189 def test_php_application_conditional(self):
190 self.load('conditional')
191
218 self.assertRegex(self.get()['body'], r'True', 'conditional true')
219 self.assertRegex(self.post()['body'], r'False', 'conditional false')
192 assert re.search(r'True', self.get()['body']), 'conditional true'
193 assert re.search(r'False', self.post()['body']), 'conditional false'
220
221 def test_php_application_get_variables(self):
222 self.load('get_variables')
223
224 resp = self.get(url='/?var1=val1&var2=&var3')
194
195 def test_php_application_get_variables(self):
196 self.load('get_variables')
197
198 resp = self.get(url='/?var1=val1&var2=&var3')
225 self.assertEqual(resp['headers']['X-Var-1'], 'val1', 'GET variables')
226 self.assertEqual(resp['headers']['X-Var-2'], '1', 'GET variables 2')
227 self.assertEqual(resp['headers']['X-Var-3'], '1', 'GET variables 3')
228 self.assertEqual(resp['headers']['X-Var-4'], '', 'GET variables 4')
199 assert resp['headers']['X-Var-1'] == 'val1', 'GET variables'
200 assert resp['headers']['X-Var-2'] == '1', 'GET variables 2'
201 assert resp['headers']['X-Var-3'] == '1', 'GET variables 3'
202 assert resp['headers']['X-Var-4'] == '', 'GET variables 4'
229
230 def test_php_application_post_variables(self):
231 self.load('post_variables')
232
233 resp = self.post(
234 headers={
235 'Content-Type': 'application/x-www-form-urlencoded',
236 'Host': 'localhost',
237 'Connection': 'close',
238 },
239 body='var1=val1&var2=',
240 )
203
204 def test_php_application_post_variables(self):
205 self.load('post_variables')
206
207 resp = self.post(
208 headers={
209 'Content-Type': 'application/x-www-form-urlencoded',
210 'Host': 'localhost',
211 'Connection': 'close',
212 },
213 body='var1=val1&var2=',
214 )
241 self.assertEqual(resp['headers']['X-Var-1'], 'val1', 'POST variables')
242 self.assertEqual(resp['headers']['X-Var-2'], '1', 'POST variables 2')
243 self.assertEqual(resp['headers']['X-Var-3'], '', 'POST variables 3')
215 assert resp['headers']['X-Var-1'] == 'val1', 'POST variables'
216 assert resp['headers']['X-Var-2'] == '1', 'POST variables 2'
217 assert resp['headers']['X-Var-3'] == '', 'POST variables 3'
244
245 def test_php_application_cookies(self):
246 self.load('cookies')
247
248 resp = self.get(
249 headers={
250 'Cookie': 'var=val; var2=val2',
251 'Host': 'localhost',
252 'Connection': 'close',
253 }
254 )
255
218
219 def test_php_application_cookies(self):
220 self.load('cookies')
221
222 resp = self.get(
223 headers={
224 'Cookie': 'var=val; var2=val2',
225 'Host': 'localhost',
226 'Connection': 'close',
227 }
228 )
229
256 self.assertEqual(resp['headers']['X-Cookie-1'], 'val', 'cookie')
257 self.assertEqual(resp['headers']['X-Cookie-2'], 'val2', 'cookie')
230 assert resp['headers']['X-Cookie-1'] == 'val', 'cookie'
231 assert resp['headers']['X-Cookie-2'] == 'val2', 'cookie'
258
259 def test_php_application_ini_precision(self):
260 self.load('ini_precision')
261
232
233 def test_php_application_ini_precision(self):
234 self.load('ini_precision')
235
262 self.assertNotEqual(
263 self.get()['headers']['X-Precision'], '4', 'ini value default'
264 )
236 assert self.get()['headers']['X-Precision'] != '4', 'ini value default'
265
266 self.conf(
267 {"file": "ini/php.ini"}, 'applications/ini_precision/options'
268 )
269
237
238 self.conf(
239 {"file": "ini/php.ini"}, 'applications/ini_precision/options'
240 )
241
270 self.assertEqual(
271 self.get()['headers']['X-File'],
272 self.current_dir + '/php/ini_precision/ini/php.ini',
273 'ini file',
274 )
275 self.assertEqual(
276 self.get()['headers']['X-Precision'], '4', 'ini value'
277 )
242 assert (
243 self.get()['headers']['X-File']
244 == option.test_dir + '/php/ini_precision/ini/php.ini'
245 ), 'ini file'
246 assert self.get()['headers']['X-Precision'] == '4', 'ini value'
278
247
279 @unittest.skip('not yet')
248 @pytest.mark.skip('not yet')
280 def test_php_application_ini_admin_user(self):
281 self.load('ini_precision')
282
249 def test_php_application_ini_admin_user(self):
250 self.load('ini_precision')
251
283 self.assertIn(
284 'error',
285 self.conf(
286 {"user": {"precision": "4"}, "admin": {"precision": "5"}},
287 'applications/ini_precision/options',
288 ),
289 'ini admin user',
290 )
252 assert 'error' in self.conf(
253 {"user": {"precision": "4"}, "admin": {"precision": "5"}},
254 'applications/ini_precision/options',
255 ), 'ini admin user'
291
292 def test_php_application_ini_admin(self):
293 self.load('ini_precision')
294
295 self.conf(
296 {"file": "php.ini", "admin": {"precision": "5"}},
297 'applications/ini_precision/options',
298 )
299
256
257 def test_php_application_ini_admin(self):
258 self.load('ini_precision')
259
260 self.conf(
261 {"file": "php.ini", "admin": {"precision": "5"}},
262 'applications/ini_precision/options',
263 )
264
300 self.assertEqual(
301 self.get()['headers']['X-Precision'], '5', 'ini value admin'
302 )
265 assert self.get()['headers']['X-Precision'] == '5', 'ini value admin'
303
304 def test_php_application_ini_user(self):
305 self.load('ini_precision')
306
307 self.conf(
308 {"file": "php.ini", "user": {"precision": "5"}},
309 'applications/ini_precision/options',
310 )
311
266
267 def test_php_application_ini_user(self):
268 self.load('ini_precision')
269
270 self.conf(
271 {"file": "php.ini", "user": {"precision": "5"}},
272 'applications/ini_precision/options',
273 )
274
312 self.assertEqual(
313 self.get()['headers']['X-Precision'], '5', 'ini value user'
314 )
275 assert self.get()['headers']['X-Precision'] == '5', 'ini value user'
315
316 def test_php_application_ini_user_2(self):
317 self.load('ini_precision')
318
319 self.conf(
320 {"file": "ini/php.ini"}, 'applications/ini_precision/options'
321 )
322
276
277 def test_php_application_ini_user_2(self):
278 self.load('ini_precision')
279
280 self.conf(
281 {"file": "ini/php.ini"}, 'applications/ini_precision/options'
282 )
283
323 self.assertEqual(
324 self.get()['headers']['X-Precision'], '4', 'ini user file'
325 )
284 assert self.get()['headers']['X-Precision'] == '4', 'ini user file'
326
327 self.conf(
328 {"precision": "5"}, 'applications/ini_precision/options/user'
329 )
330
285
286 self.conf(
287 {"precision": "5"}, 'applications/ini_precision/options/user'
288 )
289
331 self.assertEqual(
332 self.get()['headers']['X-Precision'], '5', 'ini value user'
333 )
290 assert self.get()['headers']['X-Precision'] == '5', 'ini value user'
334
335 def test_php_application_ini_set_admin(self):
336 self.load('ini_precision')
337
338 self.conf(
339 {"admin": {"precision": "5"}}, 'applications/ini_precision/options'
340 )
341
291
292 def test_php_application_ini_set_admin(self):
293 self.load('ini_precision')
294
295 self.conf(
296 {"admin": {"precision": "5"}}, 'applications/ini_precision/options'
297 )
298
342 self.assertEqual(
343 self.get(url='/?precision=6')['headers']['X-Precision'],
344 '5',
345 'ini set admin',
346 )
299 assert (
300 self.get(url='/?precision=6')['headers']['X-Precision'] == '5'
301 ), 'ini set admin'
347
348 def test_php_application_ini_set_user(self):
349 self.load('ini_precision')
350
351 self.conf(
352 {"user": {"precision": "5"}}, 'applications/ini_precision/options'
353 )
354
302
303 def test_php_application_ini_set_user(self):
304 self.load('ini_precision')
305
306 self.conf(
307 {"user": {"precision": "5"}}, 'applications/ini_precision/options'
308 )
309
355 self.assertEqual(
356 self.get(url='/?precision=6')['headers']['X-Precision'],
357 '6',
358 'ini set user',
359 )
310 assert (
311 self.get(url='/?precision=6')['headers']['X-Precision'] == '6'
312 ), 'ini set user'
360
361 def test_php_application_ini_repeat(self):
362 self.load('ini_precision')
363
364 self.conf(
365 {"user": {"precision": "5"}}, 'applications/ini_precision/options'
366 )
367
313
314 def test_php_application_ini_repeat(self):
315 self.load('ini_precision')
316
317 self.conf(
318 {"user": {"precision": "5"}}, 'applications/ini_precision/options'
319 )
320
368 self.assertEqual(
369 self.get()['headers']['X-Precision'], '5', 'ini value'
370 )
321 assert self.get()['headers']['X-Precision'] == '5', 'ini value'
371
322
372 self.assertEqual(
373 self.get()['headers']['X-Precision'], '5', 'ini value repeat'
374 )
323 assert self.get()['headers']['X-Precision'] == '5', 'ini value repeat'
375
376 def test_php_application_disable_functions_exec(self):
377 self.load('time_exec')
378
379 self.before_disable_functions()
380
381 self.conf(
382 {"admin": {"disable_functions": "exec"}},
383 'applications/time_exec/options',
384 )
385
386 body = self.get()['body']
387
324
325 def test_php_application_disable_functions_exec(self):
326 self.load('time_exec')
327
328 self.before_disable_functions()
329
330 self.conf(
331 {"admin": {"disable_functions": "exec"}},
332 'applications/time_exec/options',
333 )
334
335 body = self.get()['body']
336
388 self.assertRegex(body, r'time: \d+', 'disable_functions time')
389 self.assertNotRegex(body, r'exec: \/\w+', 'disable_functions exec')
337 assert re.search(r'time: \d+', body), 'disable_functions time'
338 assert not re.search(r'exec: \/\w+', body), 'disable_functions exec'
390
391 def test_php_application_disable_functions_comma(self):
392 self.load('time_exec')
393
394 self.before_disable_functions()
395
396 self.conf(
397 {"admin": {"disable_functions": "exec,time"}},
398 'applications/time_exec/options',
399 )
400
401 body = self.get()['body']
402
339
340 def test_php_application_disable_functions_comma(self):
341 self.load('time_exec')
342
343 self.before_disable_functions()
344
345 self.conf(
346 {"admin": {"disable_functions": "exec,time"}},
347 'applications/time_exec/options',
348 )
349
350 body = self.get()['body']
351
403 self.assertNotRegex(body, r'time: \d+', 'disable_functions comma time')
404 self.assertNotRegex(
405 body, r'exec: \/\w+', 'disable_functions comma exec'
406 )
352 assert not re.search(
353 r'time: \d+', body
354 ), 'disable_functions comma time'
355 assert not re.search(
356 r'exec: \/\w+', body
357 ), 'disable_functions comma exec'
407
408 def test_php_application_disable_functions_space(self):
409 self.load('time_exec')
410
411 self.before_disable_functions()
412
413 self.conf(
414 {"admin": {"disable_functions": "exec time"}},
415 'applications/time_exec/options',
416 )
417
418 body = self.get()['body']
419
358
359 def test_php_application_disable_functions_space(self):
360 self.load('time_exec')
361
362 self.before_disable_functions()
363
364 self.conf(
365 {"admin": {"disable_functions": "exec time"}},
366 'applications/time_exec/options',
367 )
368
369 body = self.get()['body']
370
420 self.assertNotRegex(body, r'time: \d+', 'disable_functions space time')
421 self.assertNotRegex(
422 body, r'exec: \/\w+', 'disable_functions space exec'
423 )
371 assert not re.search(
372 r'time: \d+', body
373 ), 'disable_functions space time'
374 assert not re.search(
375 r'exec: \/\w+', body
376 ), 'disable_functions space exec'
424
425 def test_php_application_disable_functions_user(self):
426 self.load('time_exec')
427
428 self.before_disable_functions()
429
430 self.conf(
431 {"user": {"disable_functions": "exec"}},
432 'applications/time_exec/options',
433 )
434
435 body = self.get()['body']
436
377
378 def test_php_application_disable_functions_user(self):
379 self.load('time_exec')
380
381 self.before_disable_functions()
382
383 self.conf(
384 {"user": {"disable_functions": "exec"}},
385 'applications/time_exec/options',
386 )
387
388 body = self.get()['body']
389
437 self.assertRegex(body, r'time: \d+', 'disable_functions user time')
438 self.assertNotRegex(
439 body, r'exec: \/\w+', 'disable_functions user exec'
440 )
390 assert re.search(r'time: \d+', body), 'disable_functions user time'
391 assert not re.search(
392 r'exec: \/\w+', body
393 ), 'disable_functions user exec'
441
442 def test_php_application_disable_functions_nonexistent(self):
443 self.load('time_exec')
444
445 self.before_disable_functions()
446
447 self.conf(
448 {"admin": {"disable_functions": "blah"}},
449 'applications/time_exec/options',
450 )
451
452 body = self.get()['body']
453
394
395 def test_php_application_disable_functions_nonexistent(self):
396 self.load('time_exec')
397
398 self.before_disable_functions()
399
400 self.conf(
401 {"admin": {"disable_functions": "blah"}},
402 'applications/time_exec/options',
403 )
404
405 body = self.get()['body']
406
454 self.assertRegex(
455 body, r'time: \d+', 'disable_functions nonexistent time'
456 )
457 self.assertRegex(
458 body, r'exec: \/\w+', 'disable_functions nonexistent exec'
459 )
407 assert re.search(
408 r'time: \d+', body
409 ), 'disable_functions nonexistent time'
410 assert re.search(
411 r'exec: \/\w+', body
412 ), 'disable_functions nonexistent exec'
460
461 def test_php_application_disable_classes(self):
462 self.load('date_time')
463
413
414 def test_php_application_disable_classes(self):
415 self.load('date_time')
416
464 self.assertRegex(
465 self.get()['body'], r'012345', 'disable_classes before'
466 )
417 assert re.search(
418 r'012345', self.get()['body']
419 ), 'disable_classes before'
467
468 self.conf(
469 {"admin": {"disable_classes": "DateTime"}},
470 'applications/date_time/options',
471 )
472
420
421 self.conf(
422 {"admin": {"disable_classes": "DateTime"}},
423 'applications/date_time/options',
424 )
425
473 self.assertNotRegex(
474 self.get()['body'], r'012345', 'disable_classes before'
475 )
426 assert not re.search(
427 r'012345', self.get()['body']
428 ), 'disable_classes before'
476
477 def test_php_application_disable_classes_user(self):
478 self.load('date_time')
479
429
430 def test_php_application_disable_classes_user(self):
431 self.load('date_time')
432
480 self.assertRegex(
481 self.get()['body'], r'012345', 'disable_classes before'
482 )
433 assert re.search(
434 r'012345', self.get()['body']
435 ), 'disable_classes before'
483
484 self.conf(
485 {"user": {"disable_classes": "DateTime"}},
486 'applications/date_time/options',
487 )
488
436
437 self.conf(
438 {"user": {"disable_classes": "DateTime"}},
439 'applications/date_time/options',
440 )
441
489 self.assertNotRegex(
490 self.get()['body'], r'012345', 'disable_classes before'
491 )
442 assert not re.search(
443 r'012345', self.get()['body']
444 ), 'disable_classes before'
492
493 def test_php_application_error_log(self):
494 self.load('error_log')
495
445
446 def test_php_application_error_log(self):
447 self.load('error_log')
448
496 self.assertEqual(self.get()['status'], 200, 'status')
449 assert self.get()['status'] == 200, 'status'
497
498 time.sleep(1)
499
450
451 time.sleep(1)
452
500 self.assertEqual(self.get()['status'], 200, 'status 2')
453 assert self.get()['status'] == 200, 'status 2'
501
502 self.stop()
503
504 pattern = r'\d{4}\/\d\d\/\d\d\s\d\d:.+\[notice\].+Error in application'
505
454
455 self.stop()
456
457 pattern = r'\d{4}\/\d\d\/\d\d\s\d\d:.+\[notice\].+Error in application'
458
506 self.assertIsNotNone(self.wait_for_record(pattern), 'errors print')
459 assert self.wait_for_record(pattern) is not None, 'errors print'
507
460
508 with open(self.testdir + '/unit.log', 'r', errors='ignore') as f:
461 with open(self.temp_dir + '/unit.log', 'r', errors='ignore') as f:
509 errs = re.findall(pattern, f.read())
510
462 errs = re.findall(pattern, f.read())
463
511 self.assertEqual(len(errs), 2, 'error_log count')
464 assert len(errs) == 2, 'error_log count'
512
513 date = errs[0].split('[')[0]
514 date2 = errs[1].split('[')[0]
465
466 date = errs[0].split('[')[0]
467 date2 = errs[1].split('[')[0]
515 self.assertNotEqual(date, date2, 'date diff')
468 assert date != date2, 'date diff'
516
517 def test_php_application_script(self):
469
470 def test_php_application_script(self):
518 self.assertIn(
519 'success',
520 self.conf(
521 {
522 "listeners": {"*:7080": {"pass": "applications/script"}},
523 "applications": {
524 "script": {
525 "type": "php",
526 "processes": {"spare": 0},
527 "root": self.current_dir + "/php/script",
528 "script": "phpinfo.php",
529 }
530 },
531 }
532 ),
533 'configure script',
534 )
471 assert 'success' in self.conf(
472 {
473 "listeners": {"*:7080": {"pass": "applications/script"}},
474 "applications": {
475 "script": {
476 "type": "php",
477 "processes": {"spare": 0},
478 "root": option.test_dir + "/php/script",
479 "script": "phpinfo.php",
480 }
481 },
482 }
483 ), 'configure script'
535
536 resp = self.get()
537
484
485 resp = self.get()
486
538 self.assertEqual(resp['status'], 200, 'status')
539 self.assertNotEqual(resp['body'], '', 'body not empty')
487 assert resp['status'] == 200, 'status'
488 assert resp['body'] != '', 'body not empty'
540
541 def test_php_application_index_default(self):
489
490 def test_php_application_index_default(self):
542 self.assertIn(
543 'success',
544 self.conf(
545 {
546 "listeners": {"*:7080": {"pass": "applications/phpinfo"}},
547 "applications": {
548 "phpinfo": {
549 "type": "php",
550 "processes": {"spare": 0},
551 "root": self.current_dir + "/php/phpinfo",
552 }
553 },
554 }
555 ),
556 'configure index default',
557 )
491 assert 'success' in self.conf(
492 {
493 "listeners": {"*:7080": {"pass": "applications/phpinfo"}},
494 "applications": {
495 "phpinfo": {
496 "type": "php",
497 "processes": {"spare": 0},
498 "root": option.test_dir + "/php/phpinfo",
499 }
500 },
501 }
502 ), 'configure index default'
558
559 resp = self.get()
560
503
504 resp = self.get()
505
561 self.assertEqual(resp['status'], 200, 'status')
562 self.assertNotEqual(resp['body'], '', 'body not empty')
506 assert resp['status'] == 200, 'status'
507 assert resp['body'] != '', 'body not empty'
563
564 def test_php_application_extension_check(self):
565 self.load('phpinfo')
566
508
509 def test_php_application_extension_check(self):
510 self.load('phpinfo')
511
567 self.assertNotEqual(
568 self.get(url='/index.wrong')['status'], 200, 'status'
569 )
512 assert self.get(url='/index.wrong')['status'] != 200, 'status'
570
513
571 new_root = self.testdir + "/php"
514 new_root = self.temp_dir + "/php"
572 os.mkdir(new_root)
515 os.mkdir(new_root)
573 shutil.copy(self.current_dir + '/php/phpinfo/index.wrong', new_root)
516 shutil.copy(option.test_dir + '/php/phpinfo/index.wrong', new_root)
574
517
575 self.assertIn(
576 'success',
577 self.conf(
578 {
579 "listeners": {"*:7080": {"pass": "applications/phpinfo"}},
580 "applications": {
581 "phpinfo": {
582 "type": "php",
583 "processes": {"spare": 0},
584 "root": new_root,
585 "working_directory": new_root,
586 }
587 },
588 }
589 ),
590 'configure new root',
591 )
518 assert 'success' in self.conf(
519 {
520 "listeners": {"*:7080": {"pass": "applications/phpinfo"}},
521 "applications": {
522 "phpinfo": {
523 "type": "php",
524 "processes": {"spare": 0},
525 "root": new_root,
526 "working_directory": new_root,
527 }
528 },
529 }
530 ), 'configure new root'
592
593 resp = self.get()
531
532 resp = self.get()
594 self.assertNotEqual(
595 str(resp['status']) + resp['body'], '200', 'status new root'
596 )
533 assert str(resp['status']) + resp['body'] != '200', 'status new root'
597
598 def run_php_application_cwd_root_tests(self):
534
535 def run_php_application_cwd_root_tests(self):
599 self.assertIn(
600 'success', self.conf_delete('applications/cwd/working_directory')
536 assert 'success' in self.conf_delete(
537 'applications/cwd/working_directory'
601 )
602
538 )
539
603 script_cwd = self.current_dir + '/php/cwd'
540 script_cwd = option.test_dir + '/php/cwd'
604
605 resp = self.get()
541
542 resp = self.get()
606 self.assertEqual(resp['status'], 200, 'status ok')
607 self.assertEqual(resp['body'], script_cwd, 'default cwd')
543 assert resp['status'] == 200, 'status ok'
544 assert resp['body'] == script_cwd, 'default cwd'
608
545
609 self.assertIn(
610 'success',
611 self.conf(
612 '"' + self.current_dir + '"',
613 'applications/cwd/working_directory',
614 ),
546 assert 'success' in self.conf(
547 '"' + option.test_dir + '"', 'applications/cwd/working_directory',
615 )
616
617 resp = self.get()
548 )
549
550 resp = self.get()
618 self.assertEqual(resp['status'], 200, 'status ok')
619 self.assertEqual(resp['body'], script_cwd, 'wdir cwd')
551 assert resp['status'] == 200, 'status ok'
552 assert resp['body'] == script_cwd, 'wdir cwd'
620
621 resp = self.get(url='/?chdir=/')
553
554 resp = self.get(url='/?chdir=/')
622 self.assertEqual(resp['status'], 200, 'status ok')
623 self.assertEqual(resp['body'], '/', 'cwd after chdir')
555 assert resp['status'] == 200, 'status ok'
556 assert resp['body'] == '/', 'cwd after chdir'
624
625 # cwd must be restored
626
627 resp = self.get()
557
558 # cwd must be restored
559
560 resp = self.get()
628 self.assertEqual(resp['status'], 200, 'status ok')
629 self.assertEqual(resp['body'], script_cwd, 'cwd restored')
561 assert resp['status'] == 200, 'status ok'
562 assert resp['body'] == script_cwd, 'cwd restored'
630
631 resp = self.get(url='/subdir/')
563
564 resp = self.get(url='/subdir/')
632 self.assertEqual(
633 resp['body'], script_cwd + '/subdir', 'cwd subdir',
634 )
565 assert resp['body'] == script_cwd + '/subdir', 'cwd subdir'
635
636 def test_php_application_cwd_root(self):
637 self.load('cwd')
638 self.run_php_application_cwd_root_tests()
639
640 def test_php_application_cwd_opcache_disabled(self):
641 self.load('cwd')
642 self.set_opcache('cwd', '0')
643 self.run_php_application_cwd_root_tests()
644
645 def test_php_application_cwd_opcache_enabled(self):
646 self.load('cwd')
647 self.set_opcache('cwd', '1')
648 self.run_php_application_cwd_root_tests()
649
650 def run_php_application_cwd_script_tests(self):
651 self.load('cwd')
652
566
567 def test_php_application_cwd_root(self):
568 self.load('cwd')
569 self.run_php_application_cwd_root_tests()
570
571 def test_php_application_cwd_opcache_disabled(self):
572 self.load('cwd')
573 self.set_opcache('cwd', '0')
574 self.run_php_application_cwd_root_tests()
575
576 def test_php_application_cwd_opcache_enabled(self):
577 self.load('cwd')
578 self.set_opcache('cwd', '1')
579 self.run_php_application_cwd_root_tests()
580
581 def run_php_application_cwd_script_tests(self):
582 self.load('cwd')
583
653 script_cwd = self.current_dir + '/php/cwd'
584 script_cwd = option.test_dir + '/php/cwd'
654
585
655 self.assertIn(
656 'success', self.conf_delete('applications/cwd/working_directory')
586 assert 'success' in self.conf_delete(
587 'applications/cwd/working_directory'
657 )
658
588 )
589
659 self.assertIn(
660 'success', self.conf('"index.php"', 'applications/cwd/script')
661 )
590 assert 'success' in self.conf('"index.php"', 'applications/cwd/script')
662
591
663 self.assertEqual(
664 self.get()['body'], script_cwd, 'default cwd',
665 )
592 assert self.get()['body'] == script_cwd, 'default cwd'
666
593
667 self.assertEqual(
668 self.get(url='/?chdir=/')['body'], '/', 'cwd after chdir',
669 )
594 assert self.get(url='/?chdir=/')['body'] == '/', 'cwd after chdir'
670
671 # cwd must be restored
595
596 # cwd must be restored
672 self.assertEqual(self.get()['body'], script_cwd, 'cwd restored')
597 assert self.get()['body'] == script_cwd, 'cwd restored'
673
674 def test_php_application_cwd_script(self):
675 self.load('cwd')
676 self.run_php_application_cwd_script_tests()
677
678 def test_php_application_cwd_script_opcache_disabled(self):
679 self.load('cwd')
680 self.set_opcache('cwd', '0')
681 self.run_php_application_cwd_script_tests()
682
683 def test_php_application_cwd_script_opcache_enabled(self):
684 self.load('cwd')
685 self.set_opcache('cwd', '1')
686 self.run_php_application_cwd_script_tests()
687
688 def test_php_application_path_relative(self):
689 self.load('open')
690
598
599 def test_php_application_cwd_script(self):
600 self.load('cwd')
601 self.run_php_application_cwd_script_tests()
602
603 def test_php_application_cwd_script_opcache_disabled(self):
604 self.load('cwd')
605 self.set_opcache('cwd', '0')
606 self.run_php_application_cwd_script_tests()
607
608 def test_php_application_cwd_script_opcache_enabled(self):
609 self.load('cwd')
610 self.set_opcache('cwd', '1')
611 self.run_php_application_cwd_script_tests()
612
613 def test_php_application_path_relative(self):
614 self.load('open')
615
691 self.assertEqual(self.get()['body'], 'test', 'relative path')
616 assert self.get()['body'] == 'test', 'relative path'
692
617
693 self.assertNotEqual(
694 self.get(url='/?chdir=/')['body'], 'test', 'relative path w/ chdir'
695 )
618 assert (
619 self.get(url='/?chdir=/')['body'] != 'test'
620 ), 'relative path w/ chdir'
696
621
697 self.assertEqual(self.get()['body'], 'test', 'relative path 2')
698
699
700if __name__ == '__main__':
701 TestPHPApplication.main()
622 assert self.get()['body'] == 'test', 'relative path 2'