test_05_errors.py 6.1 KB

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