2
0

test_02_download.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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 math
  31. import os
  32. import re
  33. from datetime import timedelta
  34. import pytest
  35. from testenv import Env, CurlClient, LocalClient
  36. log = logging.getLogger(__name__)
  37. class TestDownload:
  38. @pytest.fixture(autouse=True, scope='class')
  39. def _class_scope(self, env, httpd, nghttpx):
  40. if env.have_h3():
  41. nghttpx.start_if_needed()
  42. httpd.clear_extra_configs()
  43. httpd.reload()
  44. @pytest.fixture(autouse=True, scope='class')
  45. def _class_scope(self, env, httpd):
  46. indir = httpd.docs_dir
  47. env.make_data_file(indir=indir, fname="data-10k", fsize=10*1024)
  48. env.make_data_file(indir=indir, fname="data-100k", fsize=100*1024)
  49. env.make_data_file(indir=indir, fname="data-1m", fsize=1024*1024)
  50. env.make_data_file(indir=indir, fname="data-10m", fsize=10*1024*1024)
  51. env.make_data_file(indir=indir, fname="data-50m", fsize=50*1024*1024)
  52. # download 1 file
  53. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  54. def test_02_01_download_1(self, env: Env, httpd, nghttpx, repeat, proto):
  55. if proto == 'h3' and not env.have_h3():
  56. pytest.skip("h3 not supported")
  57. curl = CurlClient(env=env)
  58. url = f'https://{env.authority_for(env.domain1, proto)}/data.json'
  59. r = curl.http_download(urls=[url], alpn_proto=proto)
  60. r.check_response(http_status=200)
  61. # download 2 files
  62. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  63. def test_02_02_download_2(self, env: Env, httpd, nghttpx, repeat, proto):
  64. if proto == 'h3' and not env.have_h3():
  65. pytest.skip("h3 not supported")
  66. curl = CurlClient(env=env)
  67. url = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-1]'
  68. r = curl.http_download(urls=[url], alpn_proto=proto)
  69. r.check_response(http_status=200, count=2)
  70. # download 100 files sequentially
  71. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  72. def test_02_03_download_sequential(self, env: Env,
  73. httpd, nghttpx, repeat, proto):
  74. if proto == 'h3' and not env.have_h3():
  75. pytest.skip("h3 not supported")
  76. count = 10
  77. curl = CurlClient(env=env)
  78. urln = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-{count-1}]'
  79. r = curl.http_download(urls=[urln], alpn_proto=proto)
  80. r.check_response(http_status=200, count=count, connect_count=1)
  81. # download 100 files parallel
  82. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  83. def test_02_04_download_parallel(self, env: Env,
  84. httpd, nghttpx, repeat, proto):
  85. if proto == 'h3' and not env.have_h3():
  86. pytest.skip("h3 not supported")
  87. count = 10
  88. max_parallel = 5
  89. curl = CurlClient(env=env)
  90. urln = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-{count-1}]'
  91. r = curl.http_download(urls=[urln], alpn_proto=proto, extra_args=[
  92. '--parallel', '--parallel-max', f'{max_parallel}'
  93. ])
  94. r.check_response(http_status=200, count=count)
  95. if proto == 'http/1.1':
  96. # http/1.1 parallel transfers will open multiple connections
  97. assert r.total_connects > 1, r.dump_logs()
  98. else:
  99. # http2 parallel transfers will use one connection (common limit is 100)
  100. assert r.total_connects == 1, r.dump_logs()
  101. # download 500 files sequential
  102. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  103. def test_02_05_download_many_sequential(self, env: Env,
  104. httpd, nghttpx, repeat, proto):
  105. if proto == 'h3' and not env.have_h3():
  106. pytest.skip("h3 not supported")
  107. if proto == 'h3' and env.curl_uses_lib('msh3'):
  108. pytest.skip("msh3 shaky here")
  109. count = 200
  110. curl = CurlClient(env=env)
  111. urln = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-{count-1}]'
  112. r = curl.http_download(urls=[urln], alpn_proto=proto)
  113. r.check_response(http_status=200, count=count)
  114. if proto == 'http/1.1':
  115. # http/1.1 parallel transfers will open multiple connections
  116. assert r.total_connects > 1, r.dump_logs()
  117. else:
  118. # http2 parallel transfers will use one connection (common limit is 100)
  119. assert r.total_connects == 1, r.dump_logs()
  120. # download 500 files parallel
  121. @pytest.mark.parametrize("proto", ['h2', 'h3'])
  122. def test_02_06_download_many_parallel(self, env: Env,
  123. httpd, nghttpx, repeat, proto):
  124. if proto == 'h3' and not env.have_h3():
  125. pytest.skip("h3 not supported")
  126. count = 200
  127. max_parallel = 50
  128. curl = CurlClient(env=env)
  129. urln = f'https://{env.authority_for(env.domain1, proto)}/data.json?[000-{count-1}]'
  130. r = curl.http_download(urls=[urln], alpn_proto=proto, extra_args=[
  131. '--parallel', '--parallel-max', f'{max_parallel}'
  132. ])
  133. r.check_response(http_status=200, count=count, connect_count=1)
  134. # download files parallel, check connection reuse/multiplex
  135. @pytest.mark.parametrize("proto", ['h2', 'h3'])
  136. def test_02_07_download_reuse(self, env: Env,
  137. httpd, nghttpx, repeat, proto):
  138. if proto == 'h3' and not env.have_h3():
  139. pytest.skip("h3 not supported")
  140. count = 200
  141. curl = CurlClient(env=env)
  142. urln = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-{count-1}]'
  143. r = curl.http_download(urls=[urln], alpn_proto=proto,
  144. with_stats=True, extra_args=[
  145. '--parallel', '--parallel-max', '200'
  146. ])
  147. r.check_response(http_status=200, count=count)
  148. # should have used at most 2 connections only (test servers allow 100 req/conn)
  149. # it may be just 1 on slow systems where request are answered faster than
  150. # curl can exhaust the capacity or if curl runs with address-sanitizer speed
  151. assert r.total_connects <= 2, "h2 should use fewer connections here"
  152. # download files parallel with http/1.1, check connection not reused
  153. @pytest.mark.parametrize("proto", ['http/1.1'])
  154. def test_02_07b_download_reuse(self, env: Env,
  155. httpd, nghttpx, repeat, proto):
  156. count = 6
  157. curl = CurlClient(env=env)
  158. urln = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-{count-1}]'
  159. r = curl.http_download(urls=[urln], alpn_proto=proto,
  160. with_stats=True, extra_args=[
  161. '--parallel'
  162. ])
  163. r.check_response(count=count, http_status=200)
  164. # http/1.1 should have used count connections
  165. assert r.total_connects == count, "http/1.1 should use this many connections"
  166. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  167. def test_02_08_1MB_serial(self, env: Env,
  168. httpd, nghttpx, repeat, proto):
  169. if proto == 'h3' and not env.have_h3():
  170. pytest.skip("h3 not supported")
  171. count = 5
  172. urln = f'https://{env.authority_for(env.domain1, proto)}/data-1m?[0-{count-1}]'
  173. curl = CurlClient(env=env)
  174. r = curl.http_download(urls=[urln], alpn_proto=proto)
  175. r.check_response(count=count, http_status=200)
  176. @pytest.mark.parametrize("proto", ['h2', 'h3'])
  177. def test_02_09_1MB_parallel(self, env: Env,
  178. httpd, nghttpx, repeat, proto):
  179. if proto == 'h3' and not env.have_h3():
  180. pytest.skip("h3 not supported")
  181. count = 5
  182. urln = f'https://{env.authority_for(env.domain1, proto)}/data-1m?[0-{count-1}]'
  183. curl = CurlClient(env=env)
  184. r = curl.http_download(urls=[urln], alpn_proto=proto, extra_args=[
  185. '--parallel'
  186. ])
  187. r.check_response(count=count, http_status=200)
  188. @pytest.mark.skipif(condition=Env().slow_network, reason="not suitable for slow network tests")
  189. @pytest.mark.skipif(condition=Env().ci_run, reason="not suitable for CI runs")
  190. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  191. def test_02_10_10MB_serial(self, env: Env,
  192. httpd, nghttpx, repeat, proto):
  193. if proto == 'h3' and not env.have_h3():
  194. pytest.skip("h3 not supported")
  195. count = 3
  196. urln = f'https://{env.authority_for(env.domain1, proto)}/data-10m?[0-{count-1}]'
  197. curl = CurlClient(env=env)
  198. r = curl.http_download(urls=[urln], alpn_proto=proto)
  199. r.check_response(count=count, http_status=200)
  200. @pytest.mark.skipif(condition=Env().slow_network, reason="not suitable for slow network tests")
  201. @pytest.mark.skipif(condition=Env().ci_run, reason="not suitable for CI runs")
  202. @pytest.mark.parametrize("proto", ['h2', 'h3'])
  203. def test_02_11_10MB_parallel(self, env: Env,
  204. httpd, nghttpx, repeat, proto):
  205. if proto == 'h3' and not env.have_h3():
  206. pytest.skip("h3 not supported")
  207. if proto == 'h3' and env.curl_uses_lib('msh3'):
  208. pytest.skip("msh3 stalls here")
  209. count = 3
  210. urln = f'https://{env.authority_for(env.domain1, proto)}/data-10m?[0-{count-1}]'
  211. curl = CurlClient(env=env)
  212. r = curl.http_download(urls=[urln], alpn_proto=proto, extra_args=[
  213. '--parallel'
  214. ])
  215. r.check_response(count=count, http_status=200)
  216. @pytest.mark.parametrize("proto", ['h2', 'h3'])
  217. def test_02_12_head_serial_https(self, env: Env,
  218. httpd, nghttpx, repeat, proto):
  219. if proto == 'h3' and not env.have_h3():
  220. pytest.skip("h3 not supported")
  221. count = 5
  222. urln = f'https://{env.authority_for(env.domain1, proto)}/data-10m?[0-{count-1}]'
  223. curl = CurlClient(env=env)
  224. r = curl.http_download(urls=[urln], alpn_proto=proto, extra_args=[
  225. '--head'
  226. ])
  227. r.check_response(count=count, http_status=200)
  228. @pytest.mark.parametrize("proto", ['h2'])
  229. def test_02_13_head_serial_h2c(self, env: Env,
  230. httpd, nghttpx, repeat, proto):
  231. if proto == 'h3' and not env.have_h3():
  232. pytest.skip("h3 not supported")
  233. count = 5
  234. urln = f'http://{env.domain1}:{env.http_port}/data-10m?[0-{count-1}]'
  235. curl = CurlClient(env=env)
  236. r = curl.http_download(urls=[urln], alpn_proto=proto, extra_args=[
  237. '--head', '--http2-prior-knowledge', '--fail-early'
  238. ])
  239. r.check_response(count=count, http_status=200)
  240. @pytest.mark.parametrize("proto", ['h2', 'h3'])
  241. def test_02_14_not_found(self, env: Env, httpd, nghttpx, repeat, proto):
  242. if proto == 'h3' and not env.have_h3():
  243. pytest.skip("h3 not supported")
  244. if proto == 'h3' and env.curl_uses_lib('msh3'):
  245. pytest.skip("msh3 stalls here")
  246. count = 5
  247. urln = f'https://{env.authority_for(env.domain1, proto)}/not-found?[0-{count-1}]'
  248. curl = CurlClient(env=env)
  249. r = curl.http_download(urls=[urln], alpn_proto=proto, extra_args=[
  250. '--parallel'
  251. ])
  252. r.check_stats(count=count, http_status=404, exitcode=0,
  253. remote_port=env.port_for(alpn_proto=proto),
  254. remote_ip='127.0.0.1')
  255. @pytest.mark.parametrize("proto", ['h2', 'h3'])
  256. def test_02_15_fail_not_found(self, env: Env, httpd, nghttpx, repeat, proto):
  257. if proto == 'h3' and not env.have_h3():
  258. pytest.skip("h3 not supported")
  259. if proto == 'h3' and env.curl_uses_lib('msh3'):
  260. pytest.skip("msh3 stalls here")
  261. count = 5
  262. urln = f'https://{env.authority_for(env.domain1, proto)}/not-found?[0-{count-1}]'
  263. curl = CurlClient(env=env)
  264. r = curl.http_download(urls=[urln], alpn_proto=proto, extra_args=[
  265. '--fail'
  266. ])
  267. r.check_stats(count=count, http_status=404, exitcode=22,
  268. remote_port=env.port_for(alpn_proto=proto),
  269. remote_ip='127.0.0.1')
  270. @pytest.mark.skipif(condition=Env().slow_network, reason="not suitable for slow network tests")
  271. def test_02_20_h2_small_frames(self, env: Env, httpd, repeat):
  272. # Test case to reproduce content corruption as observed in
  273. # https://github.com/curl/curl/issues/10525
  274. # To reliably reproduce, we need an Apache httpd that supports
  275. # setting smaller frame sizes. This is not released yet, we
  276. # test if it works and back out if not.
  277. httpd.set_extra_config(env.domain1, lines=[
  278. 'H2MaxDataFrameLen 1024',
  279. ])
  280. assert httpd.stop()
  281. if not httpd.start():
  282. # no, not supported, bail out
  283. httpd.set_extra_config(env.domain1, lines=None)
  284. assert httpd.start()
  285. pytest.skip('H2MaxDataFrameLen not supported')
  286. # ok, make 100 downloads with 2 parallel running and they
  287. # are expected to stumble into the issue when using `lib/http2.c`
  288. # from curl 7.88.0
  289. count = 5
  290. urln = f'https://{env.authority_for(env.domain1, "h2")}/data-1m?[0-{count-1}]'
  291. curl = CurlClient(env=env)
  292. r = curl.http_download(urls=[urln], alpn_proto="h2", extra_args=[
  293. '--parallel', '--parallel-max', '2'
  294. ])
  295. r.check_response(count=count, http_status=200)
  296. srcfile = os.path.join(httpd.docs_dir, 'data-1m')
  297. self.check_downloads(curl, srcfile, count)
  298. # restore httpd defaults
  299. httpd.set_extra_config(env.domain1, lines=None)
  300. assert httpd.stop()
  301. assert httpd.start()
  302. # download via lib client, 1 at a time, pause/resume at different offsets
  303. @pytest.mark.parametrize("pause_offset", [0, 10*1024, 100*1023, 640000])
  304. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  305. def test_02_21_lib_serial(self, env: Env, httpd, nghttpx, proto, pause_offset, repeat):
  306. if proto == 'h3' and not env.have_h3():
  307. pytest.skip("h3 not supported")
  308. count = 2
  309. docname = 'data-10m'
  310. url = f'https://localhost:{env.https_port}/{docname}'
  311. client = LocalClient(name='hx-download', env=env)
  312. if not client.exists():
  313. pytest.skip(f'example client not built: {client.name}')
  314. r = client.run(args=[
  315. '-n', f'{count}', '-P', f'{pause_offset}', '-V', proto, url
  316. ])
  317. r.check_exit_code(0)
  318. srcfile = os.path.join(httpd.docs_dir, docname)
  319. self.check_downloads(client, srcfile, count)
  320. # download via lib client, several at a time, pause/resume
  321. @pytest.mark.parametrize("pause_offset", [100*1023])
  322. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  323. def test_02_22_lib_parallel_resume(self, env: Env, httpd, nghttpx, proto, pause_offset, repeat):
  324. if proto == 'h3' and not env.have_h3():
  325. pytest.skip("h3 not supported")
  326. count = 2
  327. max_parallel = 5
  328. docname = 'data-10m'
  329. url = f'https://localhost:{env.https_port}/{docname}'
  330. client = LocalClient(name='hx-download', env=env)
  331. if not client.exists():
  332. pytest.skip(f'example client not built: {client.name}')
  333. r = client.run(args=[
  334. '-n', f'{count}', '-m', f'{max_parallel}',
  335. '-P', f'{pause_offset}', '-V', proto, url
  336. ])
  337. r.check_exit_code(0)
  338. srcfile = os.path.join(httpd.docs_dir, docname)
  339. self.check_downloads(client, srcfile, count)
  340. # download, several at a time, pause and abort paused
  341. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  342. def test_02_23a_lib_abort_paused(self, env: Env, httpd, nghttpx, proto, repeat):
  343. if proto == 'h3' and not env.have_h3():
  344. pytest.skip("h3 not supported")
  345. if proto == 'h3' and env.curl_uses_ossl_quic():
  346. pytest.skip('OpenSSL QUIC fails here')
  347. if proto == 'h3' and env.ci_run and env.curl_uses_lib('quiche'):
  348. pytest.skip("fails in CI, but works locally for unknown reasons")
  349. count = 10
  350. max_parallel = 5
  351. if proto in ['h2', 'h3']:
  352. pause_offset = 64 * 1024
  353. else:
  354. pause_offset = 12 * 1024
  355. docname = 'data-1m'
  356. url = f'https://localhost:{env.https_port}/{docname}'
  357. client = LocalClient(name='hx-download', env=env)
  358. if not client.exists():
  359. pytest.skip(f'example client not built: {client.name}')
  360. r = client.run(args=[
  361. '-n', f'{count}', '-m', f'{max_parallel}', '-a',
  362. '-P', f'{pause_offset}', '-V', proto, url
  363. ])
  364. r.check_exit_code(0)
  365. srcfile = os.path.join(httpd.docs_dir, docname)
  366. # downloads should be there, but not necessarily complete
  367. self.check_downloads(client, srcfile, count, complete=False)
  368. # download, several at a time, abort after n bytes
  369. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  370. def test_02_23b_lib_abort_offset(self, env: Env, httpd, nghttpx, proto, repeat):
  371. if proto == 'h3' and not env.have_h3():
  372. pytest.skip("h3 not supported")
  373. if proto == 'h3' and env.curl_uses_ossl_quic():
  374. pytest.skip('OpenSSL QUIC fails here')
  375. if proto == 'h3' and env.ci_run and env.curl_uses_lib('quiche'):
  376. pytest.skip("fails in CI, but works locally for unknown reasons")
  377. count = 10
  378. max_parallel = 5
  379. if proto in ['h2', 'h3']:
  380. abort_offset = 64 * 1024
  381. else:
  382. abort_offset = 12 * 1024
  383. docname = 'data-1m'
  384. url = f'https://localhost:{env.https_port}/{docname}'
  385. client = LocalClient(name='hx-download', env=env)
  386. if not client.exists():
  387. pytest.skip(f'example client not built: {client.name}')
  388. r = client.run(args=[
  389. '-n', f'{count}', '-m', f'{max_parallel}', '-a',
  390. '-A', f'{abort_offset}', '-V', proto, url
  391. ])
  392. r.check_exit_code(0)
  393. srcfile = os.path.join(httpd.docs_dir, docname)
  394. # downloads should be there, but not necessarily complete
  395. self.check_downloads(client, srcfile, count, complete=False)
  396. # download, several at a time, abort after n bytes
  397. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  398. def test_02_23c_lib_fail_offset(self, env: Env, httpd, nghttpx, proto, repeat):
  399. if proto == 'h3' and not env.have_h3():
  400. pytest.skip("h3 not supported")
  401. if proto == 'h3' and env.curl_uses_ossl_quic():
  402. pytest.skip('OpenSSL QUIC fails here')
  403. if proto == 'h3' and env.ci_run and env.curl_uses_lib('quiche'):
  404. pytest.skip("fails in CI, but works locally for unknown reasons")
  405. count = 10
  406. max_parallel = 5
  407. if proto in ['h2', 'h3']:
  408. fail_offset = 64 * 1024
  409. else:
  410. fail_offset = 12 * 1024
  411. docname = 'data-1m'
  412. url = f'https://localhost:{env.https_port}/{docname}'
  413. client = LocalClient(name='hx-download', env=env)
  414. if not client.exists():
  415. pytest.skip(f'example client not built: {client.name}')
  416. r = client.run(args=[
  417. '-n', f'{count}', '-m', f'{max_parallel}', '-a',
  418. '-F', f'{fail_offset}', '-V', proto, url
  419. ])
  420. r.check_exit_code(0)
  421. srcfile = os.path.join(httpd.docs_dir, docname)
  422. # downloads should be there, but not necessarily complete
  423. self.check_downloads(client, srcfile, count, complete=False)
  424. # speed limited download
  425. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  426. def test_02_24_speed_limit(self, env: Env, httpd, nghttpx, proto, repeat):
  427. if proto == 'h3' and not env.have_h3():
  428. pytest.skip("h3 not supported")
  429. count = 1
  430. url = f'https://{env.authority_for(env.domain1, proto)}/data-1m'
  431. curl = CurlClient(env=env)
  432. speed_limit = 384 * 1024
  433. min_duration = math.floor((1024 * 1024)/speed_limit)
  434. r = curl.http_download(urls=[url], alpn_proto=proto, extra_args=[
  435. '--limit-rate', f'{speed_limit}'
  436. ])
  437. r.check_response(count=count, http_status=200)
  438. assert r.duration > timedelta(seconds=min_duration), \
  439. f'rate limited transfer should take more than {min_duration}s, '\
  440. f'not {r.duration}'
  441. # make extreme parallel h2 upgrades, check invalid conn reuse
  442. # before protocol switch has happened
  443. def test_02_25_h2_upgrade_x(self, env: Env, httpd, repeat):
  444. url = f'http://localhost:{env.http_port}/data-100k'
  445. client = LocalClient(name='h2-upgrade-extreme', env=env, timeout=15)
  446. if not client.exists():
  447. pytest.skip(f'example client not built: {client.name}')
  448. r = client.run(args=[url])
  449. assert r.exit_code == 0, f'{client.dump_logs()}'
  450. # Special client that tests TLS session reuse in parallel transfers
  451. # TODO: just uses a single connection for h2/h3. Not sure how to prevent that
  452. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  453. def test_02_26_session_shared_reuse(self, env: Env, proto, httpd, nghttpx, repeat):
  454. url = f'https://{env.authority_for(env.domain1, proto)}/data-100k'
  455. client = LocalClient(name='tls-session-reuse', env=env)
  456. if not client.exists():
  457. pytest.skip(f'example client not built: {client.name}')
  458. r = client.run(args=[proto, url])
  459. r.check_exit_code(0)
  460. # test on paused transfers, based on issue #11982
  461. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  462. def test_02_27a_paused_no_cl(self, env: Env, httpd, nghttpx, proto, repeat):
  463. url = f'https://{env.authority_for(env.domain1, proto)}' \
  464. '/curltest/tweak/?&chunks=6&chunk_size=8000'
  465. client = LocalClient(env=env, name='h2-pausing')
  466. r = client.run(args=['-V', proto, url])
  467. r.check_exit_code(0)
  468. # test on paused transfers, based on issue #11982
  469. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  470. def test_02_27b_paused_no_cl(self, env: Env, httpd, nghttpx, proto, repeat):
  471. url = f'https://{env.authority_for(env.domain1, proto)}' \
  472. '/curltest/tweak/?error=502'
  473. client = LocalClient(env=env, name='h2-pausing')
  474. r = client.run(args=['-V', proto, url])
  475. r.check_exit_code(0)
  476. # test on paused transfers, based on issue #11982
  477. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  478. def test_02_27c_paused_no_cl(self, env: Env, httpd, nghttpx, proto, repeat):
  479. url = f'https://{env.authority_for(env.domain1, proto)}' \
  480. '/curltest/tweak/?status=200&chunks=1&chunk_size=100'
  481. client = LocalClient(env=env, name='h2-pausing')
  482. r = client.run(args=['-V', proto, url])
  483. r.check_exit_code(0)
  484. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  485. def test_02_28_get_compressed(self, env: Env, httpd, nghttpx, repeat, proto):
  486. if proto == 'h3' and not env.have_h3():
  487. pytest.skip("h3 not supported")
  488. count = 1
  489. urln = f'https://{env.authority_for(env.domain1brotli, proto)}/data-100k?[0-{count-1}]'
  490. curl = CurlClient(env=env)
  491. r = curl.http_download(urls=[urln], alpn_proto=proto, extra_args=[
  492. '--compressed'
  493. ])
  494. r.check_exit_code(code=0)
  495. r.check_response(count=count, http_status=200)
  496. def check_downloads(self, client, srcfile: str, count: int,
  497. complete: bool = True):
  498. for i in range(count):
  499. dfile = client.download_file(i)
  500. assert os.path.exists(dfile)
  501. if complete and not filecmp.cmp(srcfile, dfile, shallow=False):
  502. diff = "".join(difflib.unified_diff(a=open(srcfile).readlines(),
  503. b=open(dfile).readlines(),
  504. fromfile=srcfile,
  505. tofile=dfile,
  506. n=1))
  507. assert False, f'download {dfile} differs:\n{diff}'
  508. # download via lib client, 1 at a time, pause/resume at different offsets
  509. @pytest.mark.parametrize("pause_offset", [0, 10*1024, 100*1023, 640000])
  510. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  511. def test_02_29_h2_lib_serial(self, env: Env, httpd, nghttpx, proto, pause_offset, repeat):
  512. count = 2
  513. docname = 'data-10m'
  514. url = f'https://localhost:{env.https_port}/{docname}'
  515. client = LocalClient(name='hx-download', env=env)
  516. if not client.exists():
  517. pytest.skip(f'example client not built: {client.name}')
  518. r = client.run(args=[
  519. '-n', f'{count}', '-P', f'{pause_offset}', '-V', proto, url
  520. ])
  521. r.check_exit_code(0)
  522. srcfile = os.path.join(httpd.docs_dir, docname)
  523. self.check_downloads(client, srcfile, count)
  524. # download parallel with prior knowledge
  525. def test_02_30_parallel_prior_knowledge(self, env: Env, httpd):
  526. count = 3
  527. curl = CurlClient(env=env)
  528. urln = f'http://{env.domain1}:{env.http_port}/data.json?[0-{count-1}]'
  529. r = curl.http_download(urls=[urln], extra_args=[
  530. '--parallel', '--http2-prior-knowledge'
  531. ])
  532. r.check_response(http_status=200, count=count)
  533. assert r.total_connects == 1, r.dump_logs()
  534. # download parallel with h2 "Upgrade:"
  535. def test_02_31_parallel_upgrade(self, env: Env, httpd, nghttpx):
  536. count = 3
  537. curl = CurlClient(env=env)
  538. urln = f'http://{env.domain1}:{env.http_port}/data.json?[0-{count-1}]'
  539. r = curl.http_download(urls=[urln], extra_args=[
  540. '--parallel', '--http2'
  541. ])
  542. r.check_response(http_status=200, count=count)
  543. # we see 3 connections, because Apache only every serves a single
  544. # request via Upgrade: and then closed the connection.
  545. assert r.total_connects == 3, r.dump_logs()
  546. # nghttpx is the only server we have that supports TLS early data
  547. @pytest.mark.skipif(condition=not Env.have_nghttpx(), reason="no nghttpx")
  548. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  549. def test_02_32_earlydata(self, env: Env, httpd, nghttpx, proto):
  550. if not env.curl_uses_lib('gnutls'):
  551. pytest.skip('TLS earlydata only implemented in GnuTLS')
  552. if proto == 'h3' and not env.have_h3():
  553. pytest.skip("h3 not supported")
  554. count = 2
  555. docname = 'data-10k'
  556. # we want this test to always connect to nghttpx, since it is
  557. # the only server we have that supports TLS earlydata
  558. port = env.port_for(proto)
  559. if proto != 'h3':
  560. port = env.nghttpx_https_port
  561. url = f'https://{env.domain1}:{port}/{docname}'
  562. client = LocalClient(name='hx-download', env=env)
  563. if not client.exists():
  564. pytest.skip(f'example client not built: {client.name}')
  565. r = client.run(args=[
  566. '-n', f'{count}',
  567. '-e', # use TLS earlydata
  568. '-f', # forbid reuse of connections
  569. '-r', f'{env.domain1}:{port}:127.0.0.1',
  570. '-V', proto, url
  571. ])
  572. r.check_exit_code(0)
  573. srcfile = os.path.join(httpd.docs_dir, docname)
  574. self.check_downloads(client, srcfile, count)
  575. # check that TLS earlydata worked as expected
  576. earlydata = {}
  577. reused_session = False
  578. for line in r.trace_lines:
  579. m = re.match(r'^\[t-(\d+)] EarlyData: (\d+)', line)
  580. if m:
  581. earlydata[int(m.group(1))] = int(m.group(2))
  582. continue
  583. m = re.match(r'\[1-1] \* SSL reusing session.*', line)
  584. if m:
  585. reused_session = True
  586. assert reused_session, 'session was not reused for 2nd transfer'
  587. assert earlydata[0] == 0, f'{earlydata}'
  588. if proto == 'http/1.1':
  589. assert earlydata[1] == 69, f'{earlydata}'
  590. elif proto == 'h2':
  591. assert earlydata[1] == 107, f'{earlydata}'
  592. elif proto == 'h3':
  593. # not implemented
  594. assert earlydata[1] == 0, f'{earlydata}'