Path: blob/main/external/curl/tests/http/test_19_shutdown.py
2654 views
#!/usr/bin/env python31# -*- coding: utf-8 -*-2#***************************************************************************3# _ _ ____ _4# Project ___| | | | _ \| |5# / __| | | | |_) | |6# | (__| |_| | _ <| |___7# \___|\___/|_| \_\_____|8#9# Copyright (C) Daniel Stenberg, <[email protected]>, et al.10#11# This software is licensed as described in the file COPYING, which12# you should have received as part of this distribution. The terms13# are also available at https://curl.se/docs/copyright.html.14#15# You may opt to use, copy, modify, merge, publish, distribute and/or sell16# copies of the Software, and permit persons to whom the Software is17# furnished to do so, under the terms of the COPYING file.18#19# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY20# KIND, either express or implied.21#22# SPDX-License-Identifier: curl23#24###########################################################################25#26import logging27import os28import re29import pytest3031from testenv import Env, CurlClient, LocalClient323334log = logging.getLogger(__name__)353637class TestShutdown:3839@pytest.fixture(autouse=True, scope='class')40def _class_scope(self, env, httpd):41indir = httpd.docs_dir42env.make_data_file(indir=indir, fname="data-10k", fsize=10*1024)43env.make_data_file(indir=indir, fname="data-100k", fsize=100*1024)44env.make_data_file(indir=indir, fname="data-1m", fsize=1024*1024)4546# check with `tcpdump` that we see curl TCP RST packets47@pytest.mark.skipif(condition=not Env.tcpdump(), reason="tcpdump not available")48@pytest.mark.parametrize("proto", ['http/1.1'])49def test_19_01_check_tcp_rst(self, env: Env, httpd, proto):50if env.ci_run:51pytest.skip("seems not to work in CI")52# timing critical, disable trace overrides53run_env = os.environ.copy()54if 'CURL_DEBUG' in run_env:55del run_env['CURL_DEBUG']56curl = CurlClient(env=env, run_env=run_env)57port = env.port_for(alpn_proto=proto)58url = f'https://{env.domain1}:{port}/data.json?[0-1]'59r = curl.http_download(urls=[url], alpn_proto=proto, with_tcpdump=True, extra_args=[60'--parallel'61])62r.check_response(http_status=200, count=2)63assert r.tcpdump64assert len(r.tcpdump.get_rsts(ports=[port])) != 0, f'Expected TCP RSTs packets: {r.tcpdump.stderr}'6566# check with `tcpdump` that we do NOT see TCP RST when CURL_GRACEFUL_SHUTDOWN set67@pytest.mark.skipif(condition=not Env.tcpdump(), reason="tcpdump not available")68@pytest.mark.parametrize("proto", ['http/1.1', 'h2'])69def test_19_02_check_shutdown(self, env: Env, httpd, proto):70if not env.curl_is_debug():71pytest.skip('only works for curl debug builds')72run_env = os.environ.copy()73run_env.update({74'CURL_GRACEFUL_SHUTDOWN': '2000',75'CURL_DEBUG': 'ssl,tcp,lib-ids,multi'76})77curl = CurlClient(env=env, run_env=run_env)78port = env.port_for(alpn_proto=proto)79url = f'https://{env.domain1}:{port}/data.json?[0-1]'80r = curl.http_download(urls=[url], alpn_proto=proto, with_tcpdump=True, extra_args=[81'--parallel'82])83r.check_response(http_status=200, count=2)84assert r.tcpdump85assert len(r.tcpdump.get_rsts(ports=[port])) == 0, 'Unexpected TCP RST packets'8687# run downloads where the server closes the connection after each request88@pytest.mark.parametrize("proto", ['http/1.1'])89def test_19_03_shutdown_by_server(self, env: Env, httpd, proto):90if not env.curl_is_debug():91pytest.skip('only works for curl debug builds')92if not env.curl_is_verbose():93pytest.skip('only works for curl with verbose strings')94count = 1095curl = CurlClient(env=env, run_env={96'CURL_GRACEFUL_SHUTDOWN': '2000',97'CURL_DEBUG': 'ssl,multi'98})99url = f'https://{env.authority_for(env.domain1, proto)}/curltest/tweak/?'\100f'id=[0-{count-1}]&with_cl&close'101r = curl.http_download(urls=[url], alpn_proto=proto)102r.check_response(http_status=200, count=count)103shutdowns = [line for line in r.trace_lines104if re.match(r'.*\[SHUTDOWN] shutdown, done=1', line)]105assert len(shutdowns) == count, f'{shutdowns}'106107# run downloads with CURLOPT_FORBID_REUSE set, meaning *we* close108# the connection after each request109@pytest.mark.parametrize("proto", ['http/1.1'])110def test_19_04_shutdown_by_curl(self, env: Env, httpd, proto):111if not env.curl_is_debug():112pytest.skip('only works for curl debug builds')113if not env.curl_is_verbose():114pytest.skip('only works for curl with verbose strings')115count = 10116docname = 'data.json'117url = f'https://localhost:{env.https_port}/{docname}'118client = LocalClient(name='cli_hx_download', env=env, run_env={119'CURL_GRACEFUL_SHUTDOWN': '2000',120'CURL_DEBUG': 'ssl,multi'121})122if not client.exists():123pytest.skip(f'example client not built: {client.name}')124r = client.run(args=[125'-n', f'{count}', '-f', '-V', proto, url126])127r.check_exit_code(0)128shutdowns = [line for line in r.trace_lines129if re.match(r'.*SHUTDOWN] shutdown, done=1', line)]130assert len(shutdowns) == count, f'{shutdowns}'131132# run event-based downloads with CURLOPT_FORBID_REUSE set, meaning *we* close133# the connection after each request134@pytest.mark.parametrize("proto", ['http/1.1'])135def test_19_05_event_shutdown_by_server(self, env: Env, httpd, proto):136if not env.curl_is_debug():137pytest.skip('only works for curl debug builds')138if not env.curl_is_verbose():139pytest.skip('only works for curl with verbose strings')140count = 10141run_env = os.environ.copy()142# forbid connection reuse to trigger shutdowns after transfer143run_env['CURL_FORBID_REUSE'] = '1'144# make socket receives block 50% of the time to delay shutdown145run_env['CURL_DBG_SOCK_RBLOCK'] = '50'146run_env['CURL_DEBUG'] = 'ssl,multi,lib-ids'147curl = CurlClient(env=env, run_env=run_env)148url = f'https://{env.authority_for(env.domain1, proto)}/curltest/tweak/?'\149f'id=[0-{count-1}]&with_cl&'150r = curl.http_download(urls=[url], alpn_proto=proto, extra_args=[151'--test-event'152])153r.check_response(http_status=200, count=count)154# check that we closed all connections155closings = [line for line in r.trace_lines156if re.match(r'.*SHUTDOWN] (force )?closing', line)]157assert len(closings) == count, f'{closings}'158# check that all connection sockets were removed from event159removes = [line for line in r.trace_lines160if re.match(r'.*socket cb: socket \d+ REMOVED', line)]161assert len(removes) == count, f'{removes}'162163# check graceful shutdown on multiplexed http164@pytest.mark.parametrize("proto", ['h2', 'h3'])165def test_19_06_check_shutdown(self, env: Env, httpd, nghttpx, proto):166if proto == 'h3' and not env.have_h3():167pytest.skip("h3 not supported")168if not env.curl_is_debug():169pytest.skip('only works for curl debug builds')170if not env.curl_is_verbose():171pytest.skip('only works for curl with verbose strings')172curl = CurlClient(env=env, run_env={173'CURL_GRACEFUL_SHUTDOWN': '2000',174'CURL_DEBUG': 'all'175})176url = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-1]'177r = curl.http_download(urls=[url], alpn_proto=proto, with_tcpdump=True, extra_args=[178'--parallel'179])180r.check_response(http_status=200, count=2)181# check connection cache closings182shutdowns = [line for line in r.trace_lines183if re.match(r'.*SHUTDOWN] shutdown, done=1', line)]184assert len(shutdowns) == 1, f'{shutdowns}'185186# run connection pressure, many small transfers, not reusing connections,187# limited total188@pytest.mark.parametrize("proto", ['http/1.1'])189def test_19_07_shutdown_by_curl(self, env: Env, httpd, proto):190if not env.curl_is_debug():191pytest.skip('only works for curl debug builds')192count = 500193docname = 'data.json'194url = f'https://localhost:{env.https_port}/{docname}'195client = LocalClient(name='cli_hx_download', env=env, run_env={196'CURL_GRACEFUL_SHUTDOWN': '2000',197'CURL_DEBUG': 'ssl,multi'198})199if not client.exists():200pytest.skip(f'example client not built: {client.name}')201r = client.run(args=[202'-n', f'{count}', # that many transfers203'-f', # forbid conn reuse204'-m', '10', # max parallel205'-T', '5', # max total conns at a time206'-V', proto,207url208])209r.check_exit_code(0)210shutdowns = [line for line in r.trace_lines211if re.match(r'.*SHUTDOWN] shutdown, done=1', line)]212# we see less clean shutdowns as total limit forces early closes213assert len(shutdowns) < count, f'{shutdowns}'214215216