Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/external/curl/tests/http/test_19_shutdown.py
2654 views
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
#***************************************************************************
4
# _ _ ____ _
5
# Project ___| | | | _ \| |
6
# / __| | | | |_) | |
7
# | (__| |_| | _ <| |___
8
# \___|\___/|_| \_\_____|
9
#
10
# Copyright (C) Daniel Stenberg, <[email protected]>, et al.
11
#
12
# This software is licensed as described in the file COPYING, which
13
# you should have received as part of this distribution. The terms
14
# are also available at https://curl.se/docs/copyright.html.
15
#
16
# You may opt to use, copy, modify, merge, publish, distribute and/or sell
17
# copies of the Software, and permit persons to whom the Software is
18
# furnished to do so, under the terms of the COPYING file.
19
#
20
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21
# KIND, either express or implied.
22
#
23
# SPDX-License-Identifier: curl
24
#
25
###########################################################################
26
#
27
import logging
28
import os
29
import re
30
import pytest
31
32
from testenv import Env, CurlClient, LocalClient
33
34
35
log = logging.getLogger(__name__)
36
37
38
class TestShutdown:
39
40
@pytest.fixture(autouse=True, scope='class')
41
def _class_scope(self, env, httpd):
42
indir = httpd.docs_dir
43
env.make_data_file(indir=indir, fname="data-10k", fsize=10*1024)
44
env.make_data_file(indir=indir, fname="data-100k", fsize=100*1024)
45
env.make_data_file(indir=indir, fname="data-1m", fsize=1024*1024)
46
47
# check with `tcpdump` that we see curl TCP RST packets
48
@pytest.mark.skipif(condition=not Env.tcpdump(), reason="tcpdump not available")
49
@pytest.mark.parametrize("proto", ['http/1.1'])
50
def test_19_01_check_tcp_rst(self, env: Env, httpd, proto):
51
if env.ci_run:
52
pytest.skip("seems not to work in CI")
53
# timing critical, disable trace overrides
54
run_env = os.environ.copy()
55
if 'CURL_DEBUG' in run_env:
56
del run_env['CURL_DEBUG']
57
curl = CurlClient(env=env, run_env=run_env)
58
port = env.port_for(alpn_proto=proto)
59
url = f'https://{env.domain1}:{port}/data.json?[0-1]'
60
r = curl.http_download(urls=[url], alpn_proto=proto, with_tcpdump=True, extra_args=[
61
'--parallel'
62
])
63
r.check_response(http_status=200, count=2)
64
assert r.tcpdump
65
assert len(r.tcpdump.get_rsts(ports=[port])) != 0, f'Expected TCP RSTs packets: {r.tcpdump.stderr}'
66
67
# check with `tcpdump` that we do NOT see TCP RST when CURL_GRACEFUL_SHUTDOWN set
68
@pytest.mark.skipif(condition=not Env.tcpdump(), reason="tcpdump not available")
69
@pytest.mark.parametrize("proto", ['http/1.1', 'h2'])
70
def test_19_02_check_shutdown(self, env: Env, httpd, proto):
71
if not env.curl_is_debug():
72
pytest.skip('only works for curl debug builds')
73
run_env = os.environ.copy()
74
run_env.update({
75
'CURL_GRACEFUL_SHUTDOWN': '2000',
76
'CURL_DEBUG': 'ssl,tcp,lib-ids,multi'
77
})
78
curl = CurlClient(env=env, run_env=run_env)
79
port = env.port_for(alpn_proto=proto)
80
url = f'https://{env.domain1}:{port}/data.json?[0-1]'
81
r = curl.http_download(urls=[url], alpn_proto=proto, with_tcpdump=True, extra_args=[
82
'--parallel'
83
])
84
r.check_response(http_status=200, count=2)
85
assert r.tcpdump
86
assert len(r.tcpdump.get_rsts(ports=[port])) == 0, 'Unexpected TCP RST packets'
87
88
# run downloads where the server closes the connection after each request
89
@pytest.mark.parametrize("proto", ['http/1.1'])
90
def test_19_03_shutdown_by_server(self, env: Env, httpd, proto):
91
if not env.curl_is_debug():
92
pytest.skip('only works for curl debug builds')
93
if not env.curl_is_verbose():
94
pytest.skip('only works for curl with verbose strings')
95
count = 10
96
curl = CurlClient(env=env, run_env={
97
'CURL_GRACEFUL_SHUTDOWN': '2000',
98
'CURL_DEBUG': 'ssl,multi'
99
})
100
url = f'https://{env.authority_for(env.domain1, proto)}/curltest/tweak/?'\
101
f'id=[0-{count-1}]&with_cl&close'
102
r = curl.http_download(urls=[url], alpn_proto=proto)
103
r.check_response(http_status=200, count=count)
104
shutdowns = [line for line in r.trace_lines
105
if re.match(r'.*\[SHUTDOWN] shutdown, done=1', line)]
106
assert len(shutdowns) == count, f'{shutdowns}'
107
108
# run downloads with CURLOPT_FORBID_REUSE set, meaning *we* close
109
# the connection after each request
110
@pytest.mark.parametrize("proto", ['http/1.1'])
111
def test_19_04_shutdown_by_curl(self, env: Env, httpd, proto):
112
if not env.curl_is_debug():
113
pytest.skip('only works for curl debug builds')
114
if not env.curl_is_verbose():
115
pytest.skip('only works for curl with verbose strings')
116
count = 10
117
docname = 'data.json'
118
url = f'https://localhost:{env.https_port}/{docname}'
119
client = LocalClient(name='cli_hx_download', env=env, run_env={
120
'CURL_GRACEFUL_SHUTDOWN': '2000',
121
'CURL_DEBUG': 'ssl,multi'
122
})
123
if not client.exists():
124
pytest.skip(f'example client not built: {client.name}')
125
r = client.run(args=[
126
'-n', f'{count}', '-f', '-V', proto, url
127
])
128
r.check_exit_code(0)
129
shutdowns = [line for line in r.trace_lines
130
if re.match(r'.*SHUTDOWN] shutdown, done=1', line)]
131
assert len(shutdowns) == count, f'{shutdowns}'
132
133
# run event-based downloads with CURLOPT_FORBID_REUSE set, meaning *we* close
134
# the connection after each request
135
@pytest.mark.parametrize("proto", ['http/1.1'])
136
def test_19_05_event_shutdown_by_server(self, env: Env, httpd, proto):
137
if not env.curl_is_debug():
138
pytest.skip('only works for curl debug builds')
139
if not env.curl_is_verbose():
140
pytest.skip('only works for curl with verbose strings')
141
count = 10
142
run_env = os.environ.copy()
143
# forbid connection reuse to trigger shutdowns after transfer
144
run_env['CURL_FORBID_REUSE'] = '1'
145
# make socket receives block 50% of the time to delay shutdown
146
run_env['CURL_DBG_SOCK_RBLOCK'] = '50'
147
run_env['CURL_DEBUG'] = 'ssl,multi,lib-ids'
148
curl = CurlClient(env=env, run_env=run_env)
149
url = f'https://{env.authority_for(env.domain1, proto)}/curltest/tweak/?'\
150
f'id=[0-{count-1}]&with_cl&'
151
r = curl.http_download(urls=[url], alpn_proto=proto, extra_args=[
152
'--test-event'
153
])
154
r.check_response(http_status=200, count=count)
155
# check that we closed all connections
156
closings = [line for line in r.trace_lines
157
if re.match(r'.*SHUTDOWN] (force )?closing', line)]
158
assert len(closings) == count, f'{closings}'
159
# check that all connection sockets were removed from event
160
removes = [line for line in r.trace_lines
161
if re.match(r'.*socket cb: socket \d+ REMOVED', line)]
162
assert len(removes) == count, f'{removes}'
163
164
# check graceful shutdown on multiplexed http
165
@pytest.mark.parametrize("proto", ['h2', 'h3'])
166
def test_19_06_check_shutdown(self, env: Env, httpd, nghttpx, proto):
167
if proto == 'h3' and not env.have_h3():
168
pytest.skip("h3 not supported")
169
if not env.curl_is_debug():
170
pytest.skip('only works for curl debug builds')
171
if not env.curl_is_verbose():
172
pytest.skip('only works for curl with verbose strings')
173
curl = CurlClient(env=env, run_env={
174
'CURL_GRACEFUL_SHUTDOWN': '2000',
175
'CURL_DEBUG': 'all'
176
})
177
url = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-1]'
178
r = curl.http_download(urls=[url], alpn_proto=proto, with_tcpdump=True, extra_args=[
179
'--parallel'
180
])
181
r.check_response(http_status=200, count=2)
182
# check connection cache closings
183
shutdowns = [line for line in r.trace_lines
184
if re.match(r'.*SHUTDOWN] shutdown, done=1', line)]
185
assert len(shutdowns) == 1, f'{shutdowns}'
186
187
# run connection pressure, many small transfers, not reusing connections,
188
# limited total
189
@pytest.mark.parametrize("proto", ['http/1.1'])
190
def test_19_07_shutdown_by_curl(self, env: Env, httpd, proto):
191
if not env.curl_is_debug():
192
pytest.skip('only works for curl debug builds')
193
count = 500
194
docname = 'data.json'
195
url = f'https://localhost:{env.https_port}/{docname}'
196
client = LocalClient(name='cli_hx_download', env=env, run_env={
197
'CURL_GRACEFUL_SHUTDOWN': '2000',
198
'CURL_DEBUG': 'ssl,multi'
199
})
200
if not client.exists():
201
pytest.skip(f'example client not built: {client.name}')
202
r = client.run(args=[
203
'-n', f'{count}', # that many transfers
204
'-f', # forbid conn reuse
205
'-m', '10', # max parallel
206
'-T', '5', # max total conns at a time
207
'-V', proto,
208
url
209
])
210
r.check_exit_code(0)
211
shutdowns = [line for line in r.trace_lines
212
if re.match(r'.*SHUTDOWN] shutdown, done=1', line)]
213
# we see less clean shutdowns as total limit forces early closes
214
assert len(shutdowns) < count, f'{shutdowns}'
215
216