xref: /unit/test/test_settings.py (revision 1878)
11635Szelenkov@nginx.comimport re
21477Szelenkov@nginx.comimport socket
3730Szelenkov@nginx.comimport time
41477Szelenkov@nginx.com
51635Szelenkov@nginx.comimport pytest
61848Szelenkov@nginx.com
71019Szelenkov@nginx.comfrom unit.applications.lang.python import TestApplicationPython
81877Szelenkov@nginx.comfrom unit.utils import sysctl
9730Szelenkov@nginx.com
101017Szelenkov@nginx.com
111019Szelenkov@nginx.comclass TestSettings(TestApplicationPython):
121467Szelenkov@nginx.com    prerequisites = {'modules': {'python': 'any'}}
13730Szelenkov@nginx.com
14730Szelenkov@nginx.com    def test_settings_header_read_timeout(self):
15730Szelenkov@nginx.com        self.load('empty')
16730Szelenkov@nginx.com
17*1878Szelenkov@nginx.com        def req():
18*1878Szelenkov@nginx.com            (resp, sock) = self.http(
19*1878Szelenkov@nginx.com                b"""GET / HTTP/1.1
201017Szelenkov@nginx.com""",
21*1878Szelenkov@nginx.com                start=True,
22*1878Szelenkov@nginx.com                read_timeout=1,
23*1878Szelenkov@nginx.com                raw=True,
24*1878Szelenkov@nginx.com            )
25730Szelenkov@nginx.com
26*1878Szelenkov@nginx.com            time.sleep(3)
27730Szelenkov@nginx.com
28*1878Szelenkov@nginx.com            return self.http(
29*1878Szelenkov@nginx.com                b"""Host: localhost
30730Szelenkov@nginx.comConnection: close
31730Szelenkov@nginx.com
32*1878Szelenkov@nginx.com    """,
33*1878Szelenkov@nginx.com                sock=sock,
34*1878Szelenkov@nginx.com                raw=True,
35*1878Szelenkov@nginx.com            )
36*1878Szelenkov@nginx.com
37*1878Szelenkov@nginx.com        assert 'success' in self.conf(
38*1878Szelenkov@nginx.com            {'http': {'header_read_timeout': 2}}, 'settings'
391017Szelenkov@nginx.com        )
40*1878Szelenkov@nginx.com        assert req()['status'] == 408, 'status header read timeout'
41730Szelenkov@nginx.com
42*1878Szelenkov@nginx.com        assert 'success' in self.conf(
43*1878Szelenkov@nginx.com            {'http': {'header_read_timeout': 7}}, 'settings'
44*1878Szelenkov@nginx.com        )
45*1878Szelenkov@nginx.com        assert req()['status'] == 200, 'status header read timeout 2'
46730Szelenkov@nginx.com
47756Szelenkov@nginx.com    def test_settings_header_read_timeout_update(self):
48756Szelenkov@nginx.com        self.load('empty')
49756Szelenkov@nginx.com
50*1878Szelenkov@nginx.com        assert 'success' in self.conf(
51*1878Szelenkov@nginx.com            {'http': {'header_read_timeout': 4}}, 'settings'
52*1878Szelenkov@nginx.com        )
53756Szelenkov@nginx.com
541017Szelenkov@nginx.com        (resp, sock) = self.http(
551017Szelenkov@nginx.com            b"""GET / HTTP/1.1
561017Szelenkov@nginx.com""",
571017Szelenkov@nginx.com            start=True,
581017Szelenkov@nginx.com            raw=True,
591017Szelenkov@nginx.com            no_recv=True,
601017Szelenkov@nginx.com        )
61756Szelenkov@nginx.com
62756Szelenkov@nginx.com        time.sleep(2)
63756Szelenkov@nginx.com
641017Szelenkov@nginx.com        (resp, sock) = self.http(
651017Szelenkov@nginx.com            b"""Host: localhost
661017Szelenkov@nginx.com""",
671017Szelenkov@nginx.com            start=True,
681017Szelenkov@nginx.com            sock=sock,
691017Szelenkov@nginx.com            raw=True,
701017Szelenkov@nginx.com            no_recv=True,
711017Szelenkov@nginx.com        )
72759Szelenkov@nginx.com
73759Szelenkov@nginx.com        time.sleep(2)
74759Szelenkov@nginx.com
751017Szelenkov@nginx.com        (resp, sock) = self.http(
761017Szelenkov@nginx.com            b"""X-Blah: blah
771017Szelenkov@nginx.com""",
781017Szelenkov@nginx.com            start=True,
791017Szelenkov@nginx.com            sock=sock,
801017Szelenkov@nginx.com            read_timeout=1,
811017Szelenkov@nginx.com            raw=True,
821017Szelenkov@nginx.com        )
83767Szelenkov@nginx.com
84767Szelenkov@nginx.com        if len(resp) != 0:
85767Szelenkov@nginx.com            sock.close()
86756Szelenkov@nginx.com
87767Szelenkov@nginx.com        else:
88767Szelenkov@nginx.com            time.sleep(2)
89756Szelenkov@nginx.com
901017Szelenkov@nginx.com            resp = self.http(
911017Szelenkov@nginx.com                b"""Connection: close
92756Szelenkov@nginx.com
931017Szelenkov@nginx.com""",
941017Szelenkov@nginx.com                sock=sock,
951017Szelenkov@nginx.com                raw=True,
961017Szelenkov@nginx.com            )
97756Szelenkov@nginx.com
981596Szelenkov@nginx.com        assert resp['status'] == 408, 'status header read timeout update'
99756Szelenkov@nginx.com
100730Szelenkov@nginx.com    def test_settings_body_read_timeout(self):
101730Szelenkov@nginx.com        self.load('empty')
102730Szelenkov@nginx.com
103*1878Szelenkov@nginx.com        def req():
104*1878Szelenkov@nginx.com            (resp, sock) = self.http(
105*1878Szelenkov@nginx.com                b"""POST / HTTP/1.1
106730Szelenkov@nginx.comHost: localhost
107730Szelenkov@nginx.comContent-Length: 10
108730Szelenkov@nginx.comConnection: close
109730Szelenkov@nginx.com
1101017Szelenkov@nginx.com""",
111*1878Szelenkov@nginx.com                start=True,
112*1878Szelenkov@nginx.com                raw_resp=True,
113*1878Szelenkov@nginx.com                read_timeout=1,
114*1878Szelenkov@nginx.com                raw=True,
115*1878Szelenkov@nginx.com            )
116*1878Szelenkov@nginx.com
117*1878Szelenkov@nginx.com            time.sleep(3)
118*1878Szelenkov@nginx.com
119*1878Szelenkov@nginx.com            return self.http(b"""0123456789""", sock=sock, raw=True)
120730Szelenkov@nginx.com
121*1878Szelenkov@nginx.com        assert 'success' in self.conf(
122*1878Szelenkov@nginx.com            {'http': {'body_read_timeout': 2}}, 'settings'
123*1878Szelenkov@nginx.com        )
124*1878Szelenkov@nginx.com        assert req()['status'] == 408, 'status body read timeout'
125730Szelenkov@nginx.com
126*1878Szelenkov@nginx.com        assert 'success' in self.conf(
127*1878Szelenkov@nginx.com            {'http': {'body_read_timeout': 7}}, 'settings'
128*1878Szelenkov@nginx.com        )
129*1878Szelenkov@nginx.com        assert req()['status'] == 200, 'status body read timeout 2'
130730Szelenkov@nginx.com
131756Szelenkov@nginx.com    def test_settings_body_read_timeout_update(self):
132756Szelenkov@nginx.com        self.load('empty')
133756Szelenkov@nginx.com
134*1878Szelenkov@nginx.com        assert 'success' in self.conf(
135*1878Szelenkov@nginx.com            {'http': {'body_read_timeout': 4}}, 'settings'
136*1878Szelenkov@nginx.com        )
137756Szelenkov@nginx.com
1381017Szelenkov@nginx.com        (resp, sock) = self.http(
1391017Szelenkov@nginx.com            b"""POST / HTTP/1.1
140756Szelenkov@nginx.comHost: localhost
141756Szelenkov@nginx.comContent-Length: 10
142756Szelenkov@nginx.comConnection: close
143756Szelenkov@nginx.com
1441017Szelenkov@nginx.com""",
1451017Szelenkov@nginx.com            start=True,
1461017Szelenkov@nginx.com            read_timeout=1,
1471017Szelenkov@nginx.com            raw=True,
1481017Szelenkov@nginx.com        )
149756Szelenkov@nginx.com
150756Szelenkov@nginx.com        time.sleep(2)
151756Szelenkov@nginx.com
1521017Szelenkov@nginx.com        (resp, sock) = self.http(
1531017Szelenkov@nginx.com            b"""012""", start=True, sock=sock, read_timeout=1, raw=True
1541017Szelenkov@nginx.com        )
155756Szelenkov@nginx.com
156756Szelenkov@nginx.com        time.sleep(2)
157756Szelenkov@nginx.com
1581017Szelenkov@nginx.com        (resp, sock) = self.http(
1591017Szelenkov@nginx.com            b"""345""", start=True, sock=sock, read_timeout=1, raw=True
1601017Szelenkov@nginx.com        )
161756Szelenkov@nginx.com
162756Szelenkov@nginx.com        time.sleep(2)
163756Szelenkov@nginx.com
164756Szelenkov@nginx.com        resp = self.http(b"""6789""", sock=sock, raw=True)
165756Szelenkov@nginx.com
1661596Szelenkov@nginx.com        assert resp['status'] == 200, 'status body read timeout update'
167756Szelenkov@nginx.com
1681654Szelenkov@nginx.com    def test_settings_send_timeout(self, temp_dir):
1691877Szelenkov@nginx.com        self.load('body_generate')
170730Szelenkov@nginx.com
171*1878Szelenkov@nginx.com        def req(addr, data_len):
172*1878Szelenkov@nginx.com            sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
173*1878Szelenkov@nginx.com            sock.connect(addr)
174*1878Szelenkov@nginx.com
175*1878Szelenkov@nginx.com            req = (
176*1878Szelenkov@nginx.com                """GET / HTTP/1.1
177*1878Szelenkov@nginx.comHost: localhost
178*1878Szelenkov@nginx.comX-Length: %d
179*1878Szelenkov@nginx.comConnection: close
180*1878Szelenkov@nginx.com
181*1878Szelenkov@nginx.com"""
182*1878Szelenkov@nginx.com                % data_len
183*1878Szelenkov@nginx.com            )
184*1878Szelenkov@nginx.com
185*1878Szelenkov@nginx.com            sock.sendall(req.encode())
186*1878Szelenkov@nginx.com
187*1878Szelenkov@nginx.com            data = sock.recv(16).decode()
188*1878Szelenkov@nginx.com
189*1878Szelenkov@nginx.com            time.sleep(3)
190*1878Szelenkov@nginx.com
191*1878Szelenkov@nginx.com            data += self.recvall(sock).decode()
192*1878Szelenkov@nginx.com
193*1878Szelenkov@nginx.com            sock.close()
194*1878Szelenkov@nginx.com
195*1878Szelenkov@nginx.com            return data
196*1878Szelenkov@nginx.com
1971877Szelenkov@nginx.com        sysctl_out = sysctl()
1981877Szelenkov@nginx.com        values = re.findall(
1991877Szelenkov@nginx.com            r'net.core.[rw]mem_(?:max|default).*?(\d+)', sysctl_out
2001877Szelenkov@nginx.com        )
2011877Szelenkov@nginx.com        values = [int(v) for v in values]
2021877Szelenkov@nginx.com
2031877Szelenkov@nginx.com        data_len = 1048576 if len(values) == 0 else 10 * max(values)
204730Szelenkov@nginx.com
2051654Szelenkov@nginx.com        addr = temp_dir + '/sock'
206730Szelenkov@nginx.com
207*1878Szelenkov@nginx.com        assert 'success' in self.conf(
2081877Szelenkov@nginx.com            {"unix:" + addr: {'application': 'body_generate'}}, 'listeners'
2091877Szelenkov@nginx.com        )
210730Szelenkov@nginx.com
211*1878Szelenkov@nginx.com        assert 'success' in self.conf(
212*1878Szelenkov@nginx.com            {'http': {'send_timeout': 1}}, 'settings'
2131017Szelenkov@nginx.com        )
214730Szelenkov@nginx.com
215*1878Szelenkov@nginx.com        data = req(addr, data_len)
216*1878Szelenkov@nginx.com        assert re.search(r'200 OK', data), 'send timeout status'
217*1878Szelenkov@nginx.com        assert len(data) < data_len, 'send timeout data '
218730Szelenkov@nginx.com
219*1878Szelenkov@nginx.com        self.conf({'http': {'send_timeout': 7}}, 'settings')
220730Szelenkov@nginx.com
221*1878Szelenkov@nginx.com        data = req(addr, data_len)
222*1878Szelenkov@nginx.com        assert re.search(r'200 OK', data), 'send timeout status  2'
223*1878Szelenkov@nginx.com        assert len(data) > data_len, 'send timeout data 2'
224730Szelenkov@nginx.com
225730Szelenkov@nginx.com    def test_settings_idle_timeout(self):
226730Szelenkov@nginx.com        self.load('empty')
227730Szelenkov@nginx.com
228*1878Szelenkov@nginx.com        def req():
229*1878Szelenkov@nginx.com            (resp, sock) = self.get(
230*1878Szelenkov@nginx.com                headers={'Host': 'localhost', 'Connection': 'keep-alive'},
231*1878Szelenkov@nginx.com                start=True,
232*1878Szelenkov@nginx.com                read_timeout=1,
233*1878Szelenkov@nginx.com            )
234*1878Szelenkov@nginx.com
235*1878Szelenkov@nginx.com            time.sleep(3)
236*1878Szelenkov@nginx.com
237*1878Szelenkov@nginx.com            return self.get(sock=sock)
238*1878Szelenkov@nginx.com
2391596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'init'
2401029Szelenkov@nginx.com
241*1878Szelenkov@nginx.com        assert 'success' in self.conf(
242*1878Szelenkov@nginx.com            {'http': {'idle_timeout': 2}}, 'settings'
2431017Szelenkov@nginx.com        )
244*1878Szelenkov@nginx.com        assert req()['status'] == 408, 'status idle timeout'
245730Szelenkov@nginx.com
246*1878Szelenkov@nginx.com        assert 'success' in self.conf(
247*1878Szelenkov@nginx.com            {'http': {'idle_timeout': 7}}, 'settings'
2481017Szelenkov@nginx.com        )
249*1878Szelenkov@nginx.com        assert req()['status'] == 200, 'status idle timeout 2'
250730Szelenkov@nginx.com
2511606Szelenkov@nginx.com    def test_settings_idle_timeout_2(self):
2521606Szelenkov@nginx.com        self.load('empty')
2531606Szelenkov@nginx.com
254*1878Szelenkov@nginx.com        def req():
255*1878Szelenkov@nginx.com            _, sock = self.http(b'', start=True, raw=True, no_recv=True)
256*1878Szelenkov@nginx.com
257*1878Szelenkov@nginx.com            time.sleep(3)
258*1878Szelenkov@nginx.com
259*1878Szelenkov@nginx.com            return self.get(sock=sock)
260*1878Szelenkov@nginx.com
2611606Szelenkov@nginx.com        assert self.get()['status'] == 200, 'init'
2621606Szelenkov@nginx.com
263*1878Szelenkov@nginx.com        assert 'success' in self.conf(
264*1878Szelenkov@nginx.com            {'http': {'idle_timeout': 1}}, 'settings'
265*1878Szelenkov@nginx.com        )
266*1878Szelenkov@nginx.com        assert req()['status'] == 408, 'status idle timeout'
2671606Szelenkov@nginx.com
268*1878Szelenkov@nginx.com        assert 'success' in self.conf(
269*1878Szelenkov@nginx.com            {'http': {'idle_timeout': 7}}, 'settings'
270*1878Szelenkov@nginx.com        )
271*1878Szelenkov@nginx.com        assert req()['status'] == 200, 'status idle timeout 2'
2721606Szelenkov@nginx.com
273730Szelenkov@nginx.com    def test_settings_max_body_size(self):
274730Szelenkov@nginx.com        self.load('empty')
275730Szelenkov@nginx.com
276*1878Szelenkov@nginx.com        assert 'success' in self.conf(
277*1878Szelenkov@nginx.com            {'http': {'max_body_size': 5}}, 'settings'
278*1878Szelenkov@nginx.com        )
279730Szelenkov@nginx.com
2801596Szelenkov@nginx.com        assert self.post(body='01234')['status'] == 200, 'status size'
2811596Szelenkov@nginx.com        assert self.post(body='012345')['status'] == 413, 'status size max'
282730Szelenkov@nginx.com
2831365Szelenkov@nginx.com    def test_settings_max_body_size_large(self):
2841365Szelenkov@nginx.com        self.load('mirror')
2851365Szelenkov@nginx.com
286*1878Szelenkov@nginx.com        assert 'success' in self.conf(
287*1878Szelenkov@nginx.com            {'http': {'max_body_size': 32 * 1024 * 1024}}, 'settings'
288*1878Szelenkov@nginx.com        )
2891365Szelenkov@nginx.com
2901365Szelenkov@nginx.com        body = '0123456789abcdef' * 4 * 64 * 1024
2911365Szelenkov@nginx.com        resp = self.post(body=body, read_buffer_size=1024 * 1024)
2921596Szelenkov@nginx.com        assert resp['status'] == 200, 'status size 4'
2931596Szelenkov@nginx.com        assert resp['body'] == body, 'status body 4'
2941365Szelenkov@nginx.com
2951365Szelenkov@nginx.com        body = '0123456789abcdef' * 8 * 64 * 1024
2961365Szelenkov@nginx.com        resp = self.post(body=body, read_buffer_size=1024 * 1024)
2971596Szelenkov@nginx.com        assert resp['status'] == 200, 'status size 8'
2981596Szelenkov@nginx.com        assert resp['body'] == body, 'status body 8'
2991365Szelenkov@nginx.com
3001365Szelenkov@nginx.com        body = '0123456789abcdef' * 16 * 64 * 1024
3011365Szelenkov@nginx.com        resp = self.post(body=body, read_buffer_size=1024 * 1024)
3021596Szelenkov@nginx.com        assert resp['status'] == 200, 'status size 16'
3031596Szelenkov@nginx.com        assert resp['body'] == body, 'status body 16'
3041365Szelenkov@nginx.com
3051365Szelenkov@nginx.com        body = '0123456789abcdef' * 32 * 64 * 1024
3061365Szelenkov@nginx.com        resp = self.post(body=body, read_buffer_size=1024 * 1024)
3071596Szelenkov@nginx.com        assert resp['status'] == 200, 'status size 32'
3081596Szelenkov@nginx.com        assert resp['body'] == body, 'status body 32'
3091365Szelenkov@nginx.com
3101596Szelenkov@nginx.com    @pytest.mark.skip('not yet')
311730Szelenkov@nginx.com    def test_settings_negative_value(self):
3121596Szelenkov@nginx.com        assert 'error' in self.conf(
3131596Szelenkov@nginx.com            {'http': {'max_body_size': -1}}, 'settings'
3141596Szelenkov@nginx.com        ), 'settings negative value'
3151702Szelenkov@nginx.com
3161702Szelenkov@nginx.com    def test_settings_body_buffer_size(self):
3171702Szelenkov@nginx.com        self.load('mirror')
3181702Szelenkov@nginx.com
3191702Szelenkov@nginx.com        assert 'success' in self.conf(
3201702Szelenkov@nginx.com            {
3211702Szelenkov@nginx.com                'http': {
3221702Szelenkov@nginx.com                    'max_body_size': 64 * 1024 * 1024,
3231702Szelenkov@nginx.com                    'body_buffer_size': 32 * 1024 * 1024,
3241702Szelenkov@nginx.com                }
3251702Szelenkov@nginx.com            },
3261702Szelenkov@nginx.com            'settings',
3271702Szelenkov@nginx.com        )
3281702Szelenkov@nginx.com
3291702Szelenkov@nginx.com        body = '0123456789abcdef'
3301702Szelenkov@nginx.com        resp = self.post(body=body)
3311702Szelenkov@nginx.com        assert bool(resp), 'response from application'
3321702Szelenkov@nginx.com        assert resp['status'] == 200, 'status'
3331702Szelenkov@nginx.com        assert resp['body'] == body, 'body'
3341702Szelenkov@nginx.com
3351702Szelenkov@nginx.com        body = '0123456789abcdef' * 1024 * 1024
3361702Szelenkov@nginx.com        resp = self.post(body=body, read_buffer_size=1024 * 1024)
3371702Szelenkov@nginx.com        assert bool(resp), 'response from application 2'
3381702Szelenkov@nginx.com        assert resp['status'] == 200, 'status 2'
3391702Szelenkov@nginx.com        assert resp['body'] == body, 'body 2'
3401702Szelenkov@nginx.com
3411702Szelenkov@nginx.com        body = '0123456789abcdef' * 2 * 1024 * 1024
3421702Szelenkov@nginx.com        resp = self.post(body=body, read_buffer_size=1024 * 1024)
3431702Szelenkov@nginx.com        assert bool(resp), 'response from application 3'
3441702Szelenkov@nginx.com        assert resp['status'] == 200, 'status 3'
3451702Szelenkov@nginx.com        assert resp['body'] == body, 'body 3'
3461702Szelenkov@nginx.com
3471702Szelenkov@nginx.com        body = '0123456789abcdef' * 3 * 1024 * 1024
3481702Szelenkov@nginx.com        resp = self.post(body=body, read_buffer_size=1024 * 1024)
3491702Szelenkov@nginx.com        assert bool(resp), 'response from application 4'
3501702Szelenkov@nginx.com        assert resp['status'] == 200, 'status 4'
3511702Szelenkov@nginx.com        assert resp['body'] == body, 'body 4'
352