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