1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3#*************************************************************************** 4# _ _ ____ _ 5# Project ___| | | | _ \| | 6# / __| | | | |_) | | 7# | (__| |_| | _ <| |___ 8# \___|\___/|_| \_\_____| 9# 10# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, 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# 27import difflib 28import filecmp 29import logging 30import os 31import re 32from datetime import timedelta 33import pytest 34 35from testenv import Env, CurlClient, LocalClient 36 37 38log = logging.getLogger(__name__) 39 40 41class TestShutdown: 42 43 @pytest.fixture(autouse=True, scope='class') 44 def _class_scope(self, env, httpd, nghttpx): 45 if env.have_h3(): 46 nghttpx.start_if_needed() 47 httpd.clear_extra_configs() 48 httpd.reload() 49 50 @pytest.fixture(autouse=True, scope='class') 51 def _class_scope(self, env, httpd): 52 indir = httpd.docs_dir 53 env.make_data_file(indir=indir, fname="data-10k", fsize=10*1024) 54 env.make_data_file(indir=indir, fname="data-100k", fsize=100*1024) 55 env.make_data_file(indir=indir, fname="data-1m", fsize=1024*1024) 56 57 # check with `tcpdump` that we see curl TCP RST packets 58 @pytest.mark.skipif(condition=not Env.tcpdump(), reason="tcpdump not available") 59 @pytest.mark.parametrize("proto", ['http/1.1']) 60 def test_19_01_check_tcp_rst(self, env: Env, httpd, repeat, proto): 61 if env.ci_run: 62 pytest.skip("seems not to work in CI") 63 curl = CurlClient(env=env) 64 url = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-1]' 65 r = curl.http_download(urls=[url], alpn_proto=proto, with_tcpdump=True, extra_args=[ 66 '--parallel' 67 ]) 68 r.check_response(http_status=200, count=2) 69 assert r.tcpdump 70 assert len(r.tcpdump.stats) != 0, f'Expected TCP RSTs packets: {r.tcpdump.stderr}' 71 72 # check with `tcpdump` that we do NOT see TCP RST when CURL_GRACEFUL_SHUTDOWN set 73 @pytest.mark.skipif(condition=not Env.tcpdump(), reason="tcpdump not available") 74 @pytest.mark.parametrize("proto", ['http/1.1', 'h2']) 75 def test_19_02_check_shutdown(self, env: Env, httpd, repeat, proto): 76 if not env.curl_is_debug(): 77 pytest.skip('only works for curl debug builds') 78 curl = CurlClient(env=env, run_env={ 79 'CURL_GRACEFUL_SHUTDOWN': '2000', 80 'CURL_DEBUG': 'ssl' 81 }) 82 url = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-1]' 83 r = curl.http_download(urls=[url], alpn_proto=proto, with_tcpdump=True, extra_args=[ 84 '--parallel' 85 ]) 86 r.check_response(http_status=200, count=2) 87 assert r.tcpdump 88 assert len(r.tcpdump.stats) == 0, f'Unexpected TCP RSTs packets' 89 90 # run downloads where the server closes the connection after each request 91 @pytest.mark.parametrize("proto", ['http/1.1']) 92 def test_19_03_shutdown_by_server(self, env: Env, httpd, repeat, proto): 93 if not env.curl_is_debug(): 94 pytest.skip('only works for curl debug builds') 95 count = 10 96 curl = CurlClient(env=env, run_env={ 97 'CURL_GRACEFUL_SHUTDOWN': '2000', 98 'CURL_DEBUG': 'ssl' 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 = [l for l in r.trace_lines if re.match(r'.*CCACHE\] shutdown #\d+, done=1', l)] 105 assert len(shutdowns) == count, f'{shutdowns}' 106 107 # run downloads with CURLOPT_FORBID_REUSE set, meaning *we* close 108 # the connection after each request 109 @pytest.mark.parametrize("proto", ['http/1.1']) 110 def test_19_04_shutdown_by_curl(self, env: Env, httpd, proto, repeat): 111 if not env.curl_is_debug(): 112 pytest.skip('only works for curl debug builds') 113 count = 10 114 docname = 'data.json' 115 url = f'https://localhost:{env.https_port}/{docname}' 116 client = LocalClient(name='hx-download', env=env, run_env={ 117 'CURL_GRACEFUL_SHUTDOWN': '2000', 118 'CURL_DEBUG': 'ssl' 119 }) 120 if not client.exists(): 121 pytest.skip(f'example client not built: {client.name}') 122 r = client.run(args=[ 123 '-n', f'{count}', '-f', '-V', proto, url 124 ]) 125 r.check_exit_code(0) 126 shutdowns = [l for l in r.trace_lines if re.match(r'.*CCACHE\] shutdown #\d+, done=1', l)] 127 assert len(shutdowns) == count, f'{shutdowns}' 128 129 # run event-based downloads with CURLOPT_FORBID_REUSE set, meaning *we* close 130 # the connection after each request 131 @pytest.mark.parametrize("proto", ['http/1.1']) 132 def test_19_05_event_shutdown_by_server(self, env: Env, httpd, proto, repeat): 133 if not env.curl_is_debug(): 134 pytest.skip('only works for curl debug builds') 135 count = 10 136 curl = CurlClient(env=env, run_env={ 137 # forbid connection reuse to trigger shutdowns after transfer 138 'CURL_FORBID_REUSE': '1', 139 # make socket receives block 50% of the time to delay shutdown 140 'CURL_DBG_SOCK_RBLOCK': '50', 141 'CURL_DEBUG': 'ssl' 142 }) 143 url = f'https://{env.authority_for(env.domain1, proto)}/curltest/tweak/?'\ 144 f'id=[0-{count-1}]&with_cl&' 145 r = curl.http_download(urls=[url], alpn_proto=proto, extra_args=[ 146 '--test-event' 147 ]) 148 r.check_response(http_status=200, count=count) 149 # check that we closed all connections 150 closings = [l for l in r.trace_lines if re.match(r'.*CCACHE\] closing #\d+', l)] 151 assert len(closings) == count, f'{closings}' 152 # check that all connection sockets were removed from event 153 removes = [l for l in r.trace_lines if re.match(r'.*socket cb: socket \d+ REMOVED', l)] 154 assert len(removes) == count, f'{removes}' 155 156 # check graceful shutdown on multiplexed http 157 @pytest.mark.parametrize("proto", ['h2', 'h3']) 158 def test_19_06_check_shutdown(self, env: Env, httpd, nghttpx, repeat, proto): 159 if proto == 'h3' and not env.have_h3(): 160 pytest.skip("h3 not supported") 161 if not env.curl_is_debug(): 162 pytest.skip('only works for curl debug builds') 163 curl = CurlClient(env=env, run_env={ 164 'CURL_GRACEFUL_SHUTDOWN': '2000', 165 'CURL_DEBUG': 'all' 166 }) 167 url = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-1]' 168 r = curl.http_download(urls=[url], alpn_proto=proto, with_tcpdump=True, extra_args=[ 169 '--parallel' 170 ]) 171 r.check_response(http_status=200, count=2) 172 # check connection cache closings 173 shutdowns = [l for l in r.trace_lines if re.match(r'.*CCACHE\] shutdown #\d+, done=1', l)] 174 assert len(shutdowns) == 1, f'{shutdowns}' 175