xref: /curl/tests/http/test_05_errors.py (revision 3a35901a)
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 logging
28import pytest
29
30from testenv import Env, CurlClient
31
32
33log = logging.getLogger(__name__)
34
35
36@pytest.mark.skipif(condition=not Env.httpd_is_at_least('2.4.55'),
37                    reason=f"httpd version too old for this: {Env.httpd_version()}")
38class TestErrors:
39
40    @pytest.fixture(autouse=True, scope='class')
41    def _class_scope(self, env, httpd, nghttpx):
42        if env.have_h3():
43            nghttpx.start_if_needed()
44        httpd.clear_extra_configs()
45        httpd.reload()
46
47    # download 1 file, check that we get CURLE_PARTIAL_FILE
48    @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
49    def test_05_01_partial_1(self, env: Env, httpd, nghttpx, repeat,
50                              proto):
51        if proto == 'h3' and not env.have_h3():
52            pytest.skip("h3 not supported")
53        if proto == 'h3' and env.curl_uses_lib('msh3'):
54            pytest.skip("msh3 stalls here")
55        count = 1
56        curl = CurlClient(env=env)
57        urln = f'https://{env.authority_for(env.domain1, proto)}' \
58               f'/curltest/tweak?id=[0-{count - 1}]'\
59               '&chunks=3&chunk_size=16000&body_error=reset'
60        r = curl.http_download(urls=[urln], alpn_proto=proto, extra_args=[
61            '--retry', '0'
62        ])
63        r.check_exit_code(False)
64        invalid_stats = []
65        for idx, s in enumerate(r.stats):
66            if 'exitcode' not in s or s['exitcode'] not in [18, 56, 92, 95]:
67                invalid_stats.append(f'request {idx} exit with {s["exitcode"]}')
68        assert len(invalid_stats) == 0, f'failed: {invalid_stats}'
69
70    # download files, check that we get CURLE_PARTIAL_FILE for all
71    @pytest.mark.parametrize("proto", ['h2', 'h3'])
72    def test_05_02_partial_20(self, env: Env, httpd, nghttpx, repeat,
73                              proto):
74        if proto == 'h3' and not env.have_h3():
75            pytest.skip("h3 not supported")
76        if proto == 'h3' and env.curl_uses_lib('msh3'):
77            pytest.skip("msh3 stalls here")
78        count = 20
79        curl = CurlClient(env=env)
80        urln = f'https://{env.authority_for(env.domain1, proto)}' \
81               f'/curltest/tweak?id=[0-{count - 1}]'\
82               '&chunks=5&chunk_size=16000&body_error=reset'
83        r = curl.http_download(urls=[urln], alpn_proto=proto, extra_args=[
84            '--retry', '0', '--parallel',
85        ])
86        r.check_exit_code(False)
87        assert len(r.stats) == count, f'did not get all stats: {r}'
88        invalid_stats = []
89        for idx, s in enumerate(r.stats):
90            if 'exitcode' not in s or s['exitcode'] not in [18, 55, 56, 92, 95]:
91                invalid_stats.append(f'request {idx} exit with {s["exitcode"]}\n{s}')
92        assert len(invalid_stats) == 0, f'failed: {invalid_stats}'
93
94    # access a resource that, on h2, RST the stream with HTTP_1_1_REQUIRED
95    def test_05_03_required(self, env: Env, httpd, nghttpx, repeat):
96        curl = CurlClient(env=env)
97        proto = 'http/1.1'
98        urln = f'https://{env.authority_for(env.domain1, proto)}/curltest/1_1'
99        r = curl.http_download(urls=[urln], alpn_proto=proto)
100        r.check_exit_code(0)
101        r.check_response(http_status=200, count=1)
102        proto = 'h2'
103        urln = f'https://{env.authority_for(env.domain1, proto)}/curltest/1_1'
104        r = curl.http_download(urls=[urln], alpn_proto=proto)
105        r.check_exit_code(0)
106        r.check_response(http_status=200, count=1)
107        # check that we did a downgrade
108        assert r.stats[0]['http_version'] == '1.1', r.dump_logs()
109
110    # On the URL used here, Apache is doing an "unclean" TLS shutdown,
111    # meaning it sends no shutdown notice and just closes TCP.
112    # The HTTP response delivers a body without Content-Length. We expect:
113    # - http/1.0 to fail since it relies on a clean connection close to
114    #   detect the end of the body
115    # - http/1.1 to work since it will used "chunked" transfer encoding
116    #   and stop receiving when that signals the end
117    # - h2 to work since it will signal the end of the response before
118    #   and not see the "unclean" close either
119    @pytest.mark.parametrize("proto", ['http/1.0', 'http/1.1', 'h2'])
120    def test_05_04_unclean_tls_shutdown(self, env: Env, httpd, nghttpx, repeat, proto):
121        if proto == 'h3' and not env.have_h3():
122            pytest.skip("h3 not supported")
123        count = 10 if proto == 'h2' else 1
124        curl = CurlClient(env=env)
125        url = f'https://{env.authority_for(env.domain1, proto)}'\
126                f'/curltest/shutdown_unclean?id=[0-{count-1}]&chunks=4'
127        r = curl.http_download(urls=[url], alpn_proto=proto, extra_args=[
128            '--parallel',
129        ])
130        if proto == 'http/1.0' and not env.curl_uses_lib('wolfssl') and \
131                (env.curl_is_debug() or not env.curl_uses_lib('openssl')):
132            # we are inconsistent if we fail or not in missing TLS shutdown
133            # openssl code ignore such errors intentionally in non-debug builds
134            r.check_exit_code(56)
135        else:
136            r.check_exit_code(0)
137            r.check_response(http_status=200, count=count)
138