2
0

test_08_caddy.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 difflib
  28. import filecmp
  29. import logging
  30. import os
  31. import re
  32. import pytest
  33. from testenv import Env, CurlClient, Caddy, LocalClient
  34. log = logging.getLogger(__name__)
  35. @pytest.mark.skipif(condition=not Env.has_caddy(), reason="missing caddy")
  36. @pytest.mark.skipif(condition=not Env.have_ssl_curl(), reason="curl without SSL")
  37. class TestCaddy:
  38. @pytest.fixture(autouse=True, scope='class')
  39. def caddy(self, env):
  40. caddy = Caddy(env=env)
  41. assert caddy.start()
  42. yield caddy
  43. caddy.stop()
  44. def _make_docs_file(self, docs_dir: str, fname: str, fsize: int):
  45. fpath = os.path.join(docs_dir, fname)
  46. data1k = 1024*'x'
  47. flen = 0
  48. with open(fpath, 'w') as fd:
  49. while flen < fsize:
  50. fd.write(data1k)
  51. flen += len(data1k)
  52. return flen
  53. @pytest.fixture(autouse=True, scope='class')
  54. def _class_scope(self, env, caddy):
  55. self._make_docs_file(docs_dir=caddy.docs_dir, fname='data10k.data', fsize=10*1024)
  56. self._make_docs_file(docs_dir=caddy.docs_dir, fname='data1.data', fsize=1024*1024)
  57. self._make_docs_file(docs_dir=caddy.docs_dir, fname='data5.data', fsize=5*1024*1024)
  58. self._make_docs_file(docs_dir=caddy.docs_dir, fname='data10.data', fsize=10*1024*1024)
  59. self._make_docs_file(docs_dir=caddy.docs_dir, fname='data100.data', fsize=100*1024*1024)
  60. env.make_data_file(indir=env.gen_dir, fname="data-10m", fsize=10*1024*1024)
  61. # download 1 file
  62. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  63. def test_08_01_download_1(self, env: Env, caddy: Caddy, repeat, proto):
  64. if proto == 'h3' and not env.have_h3_curl():
  65. pytest.skip("h3 not supported in curl")
  66. if proto == 'h3' and env.curl_uses_lib('msh3'):
  67. pytest.skip("msh3 itself crashes")
  68. curl = CurlClient(env=env)
  69. url = f'https://{env.domain1}:{caddy.port}/data.json'
  70. r = curl.http_download(urls=[url], alpn_proto=proto)
  71. r.check_response(count=1, http_status=200)
  72. # download 1MB files sequentially
  73. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  74. def test_08_02_download_1mb_sequential(self, env: Env, caddy: Caddy,
  75. repeat, proto):
  76. if proto == 'h3' and not env.have_h3_curl():
  77. pytest.skip("h3 not supported in curl")
  78. if proto == 'h3' and env.curl_uses_lib('msh3'):
  79. pytest.skip("msh3 itself crashes")
  80. count = 50
  81. curl = CurlClient(env=env)
  82. urln = f'https://{env.domain1}:{caddy.port}/data1.data?[0-{count-1}]'
  83. r = curl.http_download(urls=[urln], alpn_proto=proto)
  84. r.check_response(count=count, http_status=200, connect_count=1)
  85. # download 1MB files parallel
  86. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  87. def test_08_03_download_1mb_parallel(self, env: Env, caddy: Caddy,
  88. repeat, proto):
  89. if proto == 'h3' and not env.have_h3_curl():
  90. pytest.skip("h3 not supported in curl")
  91. if proto == 'h3' and env.curl_uses_lib('msh3'):
  92. pytest.skip("msh3 itself crashes")
  93. count = 20
  94. curl = CurlClient(env=env)
  95. urln = f'https://{env.domain1}:{caddy.port}/data1.data?[0-{count-1}]'
  96. r = curl.http_download(urls=[urln], alpn_proto=proto, extra_args=[
  97. '--parallel'
  98. ])
  99. r.check_response(count=count, http_status=200)
  100. if proto == 'http/1.1':
  101. # http/1.1 parallel transfers will open multiple connections
  102. assert r.total_connects > 1, r.dump_logs()
  103. else:
  104. assert r.total_connects == 1, r.dump_logs()
  105. # download 5MB files sequentially
  106. @pytest.mark.skipif(condition=Env().slow_network, reason="not suitable for slow network tests")
  107. @pytest.mark.skipif(condition=Env().ci_run, reason="not suitable for CI runs")
  108. @pytest.mark.parametrize("proto", ['h2', 'h3'])
  109. def test_08_04a_download_10mb_sequential(self, env: Env, caddy: Caddy,
  110. repeat, proto):
  111. if proto == 'h3' and not env.have_h3_curl():
  112. pytest.skip("h3 not supported in curl")
  113. if proto == 'h3' and env.curl_uses_lib('msh3'):
  114. pytest.skip("msh3 itself crashes")
  115. count = 40
  116. curl = CurlClient(env=env)
  117. urln = f'https://{env.domain1}:{caddy.port}/data5.data?[0-{count-1}]'
  118. r = curl.http_download(urls=[urln], alpn_proto=proto)
  119. r.check_response(count=count, http_status=200, connect_count=1)
  120. # download 10MB files sequentially
  121. @pytest.mark.skipif(condition=Env().slow_network, reason="not suitable for slow network tests")
  122. @pytest.mark.skipif(condition=Env().ci_run, reason="not suitable for CI runs")
  123. @pytest.mark.parametrize("proto", ['h2', 'h3'])
  124. def test_08_04b_download_10mb_sequential(self, env: Env, caddy: Caddy,
  125. repeat, proto):
  126. if proto == 'h3' and not env.have_h3_curl():
  127. pytest.skip("h3 not supported in curl")
  128. if proto == 'h3' and env.curl_uses_lib('msh3'):
  129. pytest.skip("msh3 itself crashes")
  130. count = 20
  131. curl = CurlClient(env=env)
  132. urln = f'https://{env.domain1}:{caddy.port}/data10.data?[0-{count-1}]'
  133. r = curl.http_download(urls=[urln], alpn_proto=proto)
  134. r.check_response(count=count, http_status=200, connect_count=1)
  135. # download 10MB files parallel
  136. @pytest.mark.skipif(condition=Env().slow_network, reason="not suitable for slow network tests")
  137. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  138. @pytest.mark.skipif(condition=Env().ci_run, reason="not suitable for CI runs")
  139. def test_08_05_download_1mb_parallel(self, env: Env, caddy: Caddy,
  140. repeat, proto):
  141. if proto == 'h3' and not env.have_h3_curl():
  142. pytest.skip("h3 not supported in curl")
  143. if proto == 'h3' and env.curl_uses_lib('msh3'):
  144. pytest.skip("msh3 itself crashes")
  145. if proto == 'http/1.1' and env.curl_uses_lib('mbedtls'):
  146. pytest.skip("mbedtls 3.6.0 fails on 50 connections with: "\
  147. "ssl_handshake returned: (-0x7F00) SSL - Memory allocation failed")
  148. count = 50
  149. curl = CurlClient(env=env)
  150. urln = f'https://{env.domain1}:{caddy.port}/data10.data?[0-{count-1}]'
  151. r = curl.http_download(urls=[urln], alpn_proto=proto, extra_args=[
  152. '--parallel'
  153. ])
  154. r.check_response(count=count, http_status=200)
  155. if proto == 'http/1.1':
  156. # http/1.1 parallel transfers will open multiple connections
  157. assert r.total_connects > 1, r.dump_logs()
  158. else:
  159. assert r.total_connects == 1, r.dump_logs()
  160. # post data parallel, check that they were echoed
  161. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  162. def test_08_06_post_parallel(self, env: Env, httpd, caddy, repeat, proto):
  163. if proto == 'h3' and not env.have_h3():
  164. pytest.skip("h3 not supported")
  165. if proto == 'h3' and env.curl_uses_lib('msh3'):
  166. pytest.skip("msh3 stalls here")
  167. # limit since we use a separate connection in h1
  168. count = 20
  169. data = '0123456789'
  170. curl = CurlClient(env=env)
  171. url = f'https://{env.domain2}:{caddy.port}/curltest/echo?id=[0-{count-1}]'
  172. r = curl.http_upload(urls=[url], data=data, alpn_proto=proto,
  173. extra_args=['--parallel'])
  174. r.check_stats(count=count, http_status=200, exitcode=0)
  175. for i in range(count):
  176. respdata = open(curl.response_file(i)).readlines()
  177. assert respdata == [data]
  178. # put large file, check that they length were echoed
  179. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  180. def test_08_07_put_large(self, env: Env, httpd, caddy, repeat, proto):
  181. if proto == 'h3' and not env.have_h3():
  182. pytest.skip("h3 not supported")
  183. if proto == 'h3' and env.curl_uses_lib('msh3'):
  184. pytest.skip("msh3 stalls here")
  185. # limit since we use a separate connection in h1<
  186. count = 1
  187. fdata = os.path.join(env.gen_dir, 'data-10m')
  188. curl = CurlClient(env=env)
  189. url = f'https://{env.domain2}:{caddy.port}/curltest/put?id=[0-{count-1}]'
  190. r = curl.http_put(urls=[url], fdata=fdata, alpn_proto=proto)
  191. exp_data = [f'{os.path.getsize(fdata)}']
  192. r.check_response(count=count, http_status=200)
  193. for i in range(count):
  194. respdata = open(curl.response_file(i)).readlines()
  195. assert respdata == exp_data
  196. @pytest.mark.parametrize("proto", ['http/1.1', 'h2'])
  197. def test_08_08_earlydata(self, env: Env, httpd, caddy, proto):
  198. count = 2
  199. docname = 'data10k.data'
  200. url = f'https://{env.domain1}:{caddy.port}/{docname}'
  201. client = LocalClient(name='hx-download', env=env)
  202. if not client.exists():
  203. pytest.skip(f'example client not built: {client.name}')
  204. r = client.run(args=[
  205. '-n', f'{count}',
  206. '-e', # use TLS earlydata
  207. '-f', # forbid reuse of connections
  208. '-r', f'{env.domain1}:{caddy.port}:127.0.0.1',
  209. '-V', proto, url
  210. ])
  211. r.check_exit_code(0)
  212. srcfile = os.path.join(caddy.docs_dir, docname)
  213. self.check_downloads(client, srcfile, count)
  214. earlydata = {}
  215. for line in r.trace_lines:
  216. m = re.match(r'^\[t-(\d+)] EarlyData: (\d+)', line)
  217. if m:
  218. earlydata[int(m.group(1))] = int(m.group(2))
  219. # Caddy does not support early data
  220. assert earlydata[0] == 0, f'{earlydata}'
  221. assert earlydata[1] == 0, f'{earlydata}'
  222. def check_downloads(self, client, srcfile: str, count: int,
  223. complete: bool = True):
  224. for i in range(count):
  225. dfile = client.download_file(i)
  226. assert os.path.exists(dfile)
  227. if complete and not filecmp.cmp(srcfile, dfile, shallow=False):
  228. diff = "".join(difflib.unified_diff(a=open(srcfile).readlines(),
  229. b=open(dfile).readlines(),
  230. fromfile=srcfile,
  231. tofile=dfile,
  232. n=1))
  233. assert False, f'download {dfile} differs:\n{diff}'