Path: blob/main/external/curl/tests/http/test_19_shutdown.py
2066 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')92count = 1093curl = CurlClient(env=env, run_env={94'CURL_GRACEFUL_SHUTDOWN': '2000',95'CURL_DEBUG': 'ssl,multi'96})97url = f'https://{env.authority_for(env.domain1, proto)}/curltest/tweak/?'\98f'id=[0-{count-1}]&with_cl&close'99r = curl.http_download(urls=[url], alpn_proto=proto)100r.check_response(http_status=200, count=count)101shutdowns = [line for line in r.trace_lines102if re.match(r'.*\[SHUTDOWN] shutdown, done=1', line)]103assert len(shutdowns) == count, f'{shutdowns}'104105# run downloads with CURLOPT_FORBID_REUSE set, meaning *we* close106# the connection after each request107@pytest.mark.parametrize("proto", ['http/1.1'])108def test_19_04_shutdown_by_curl(self, env: Env, httpd, proto):109if not env.curl_is_debug():110pytest.skip('only works for curl debug builds')111count = 10112docname = 'data.json'113url = f'https://localhost:{env.https_port}/{docname}'114client = LocalClient(name='hx-download', env=env, run_env={115'CURL_GRACEFUL_SHUTDOWN': '2000',116'CURL_DEBUG': 'ssl,multi'117})118if not client.exists():119pytest.skip(f'example client not built: {client.name}')120r = client.run(args=[121'-n', f'{count}', '-f', '-V', proto, url122])123r.check_exit_code(0)124shutdowns = [line for line in r.trace_lines125if re.match(r'.*SHUTDOWN] shutdown, done=1', line)]126assert len(shutdowns) == count, f'{shutdowns}'127128# run event-based downloads with CURLOPT_FORBID_REUSE set, meaning *we* close129# the connection after each request130@pytest.mark.parametrize("proto", ['http/1.1'])131def test_19_05_event_shutdown_by_server(self, env: Env, httpd, proto):132if not env.curl_is_debug():133pytest.skip('only works for curl debug builds')134count = 10135run_env = os.environ.copy()136# forbid connection reuse to trigger shutdowns after transfer137run_env['CURL_FORBID_REUSE'] = '1'138# make socket receives block 50% of the time to delay shutdown139run_env['CURL_DBG_SOCK_RBLOCK'] = '50'140run_env['CURL_DEBUG'] = 'ssl,multi,lib-ids'141curl = CurlClient(env=env, run_env=run_env)142url = f'https://{env.authority_for(env.domain1, proto)}/curltest/tweak/?'\143f'id=[0-{count-1}]&with_cl&'144r = curl.http_download(urls=[url], alpn_proto=proto, extra_args=[145'--test-event'146])147r.check_response(http_status=200, count=count)148# check that we closed all connections149closings = [line for line in r.trace_lines150if re.match(r'.*SHUTDOWN] (force )?closing', line)]151assert len(closings) == count, f'{closings}'152# check that all connection sockets were removed from event153removes = [line for line in r.trace_lines154if re.match(r'.*socket cb: socket \d+ REMOVED', line)]155assert len(removes) == count, f'{removes}'156157# check graceful shutdown on multiplexed http158@pytest.mark.parametrize("proto", ['h2', 'h3'])159def test_19_06_check_shutdown(self, env: Env, httpd, nghttpx, proto):160if proto == 'h3' and not env.have_h3():161pytest.skip("h3 not supported")162if not env.curl_is_debug():163pytest.skip('only works for curl debug builds')164curl = CurlClient(env=env, run_env={165'CURL_GRACEFUL_SHUTDOWN': '2000',166'CURL_DEBUG': 'all'167})168url = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-1]'169r = curl.http_download(urls=[url], alpn_proto=proto, with_tcpdump=True, extra_args=[170'--parallel'171])172r.check_response(http_status=200, count=2)173# check connection cache closings174shutdowns = [line for line in r.trace_lines175if re.match(r'.*SHUTDOWN] shutdown, done=1', line)]176assert len(shutdowns) == 1, f'{shutdowns}'177178# run connection pressure, many small transfers, not reusing connections,179# limited total180@pytest.mark.parametrize("proto", ['http/1.1'])181def test_19_07_shutdown_by_curl(self, env: Env, httpd, proto):182if not env.curl_is_debug():183pytest.skip('only works for curl debug builds')184count = 500185docname = 'data.json'186url = f'https://localhost:{env.https_port}/{docname}'187client = LocalClient(name='hx-download', env=env, run_env={188'CURL_GRACEFUL_SHUTDOWN': '2000',189'CURL_DEBUG': 'ssl,multi'190})191if not client.exists():192pytest.skip(f'example client not built: {client.name}')193r = client.run(args=[194'-n', f'{count}', # that many transfers195'-f', # forbid conn reuse196'-m', '10', # max parallel197'-T', '5', # max total conns at a time198'-V', proto,199url200])201r.check_exit_code(0)202shutdowns = [line for line in r.trace_lines203if re.match(r'.*SHUTDOWN] shutdown, done=1', line)]204# we see less clean shutdowns as total limit forces early closes205assert len(shutdowns) < count, f'{shutdowns}'206207208