test_16_info.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 os
  29. import pytest
  30. from testenv import Env, CurlClient
  31. log = logging.getLogger(__name__)
  32. class TestInfo:
  33. @pytest.fixture(autouse=True, scope='class')
  34. def _class_scope(self, env, httpd, nghttpx):
  35. if env.have_h3():
  36. nghttpx.start_if_needed()
  37. httpd.clear_extra_configs()
  38. httpd.reload()
  39. @pytest.fixture(autouse=True, scope='class')
  40. def _class_scope(self, env, httpd):
  41. indir = httpd.docs_dir
  42. env.make_data_file(indir=indir, fname="data-10k", fsize=10*1024)
  43. env.make_data_file(indir=indir, fname="data-100k", fsize=100*1024)
  44. env.make_data_file(indir=indir, fname="data-1m", fsize=1024*1024)
  45. # download plain file
  46. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  47. def test_16_01_info_download(self, env: Env, httpd, nghttpx, repeat, proto):
  48. if proto == 'h3' and not env.have_h3():
  49. pytest.skip("h3 not supported")
  50. count = 2
  51. curl = CurlClient(env=env)
  52. url = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-{count-1}]'
  53. r = curl.http_download(urls=[url], alpn_proto=proto, with_stats=True)
  54. r.check_stats(count=count, http_status=200, exitcode=0,
  55. remote_port=env.port_for(alpn_proto=proto),
  56. remote_ip='127.0.0.1')
  57. for idx, s in enumerate(r.stats):
  58. self.check_stat(idx, s, r, dl_size=30, ul_size=0)
  59. # download plain file with a 302 redirect
  60. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  61. def test_16_02_info_302_download(self, env: Env, httpd, nghttpx, repeat, proto):
  62. if proto == 'h3' and not env.have_h3():
  63. pytest.skip("h3 not supported")
  64. count = 2
  65. curl = CurlClient(env=env)
  66. url = f'https://{env.authority_for(env.domain1, proto)}/data.json.302?[0-{count-1}]'
  67. r = curl.http_download(urls=[url], alpn_proto=proto, with_stats=True, extra_args=[
  68. '--location'
  69. ])
  70. r.check_stats(count=count, http_status=200, exitcode=0,
  71. remote_port=env.port_for(alpn_proto=proto),
  72. remote_ip='127.0.0.1')
  73. for idx, s in enumerate(r.stats):
  74. self.check_stat(idx, s, r, dl_size=30, ul_size=0)
  75. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  76. def test_16_03_info_upload(self, env: Env, httpd, nghttpx, proto, repeat):
  77. if proto == 'h3' and not env.have_h3():
  78. pytest.skip("h3 not supported")
  79. count = 2
  80. fdata = os.path.join(env.gen_dir, 'data-100k')
  81. fsize = 100 * 1024
  82. curl = CurlClient(env=env)
  83. url = f'https://{env.authority_for(env.domain1, proto)}/curltest/echo?id=[0-{count-1}]'
  84. r = curl.http_upload(urls=[url], data=f'@{fdata}', alpn_proto=proto,
  85. with_headers=True, extra_args=[
  86. '--trace-config', 'http/2,http/3'
  87. ])
  88. r.check_response(count=count, http_status=200)
  89. r.check_stats(count=count, http_status=200, exitcode=0,
  90. remote_port=env.port_for(alpn_proto=proto),
  91. remote_ip='127.0.0.1')
  92. for idx, s in enumerate(r.stats):
  93. self.check_stat(idx, s, r, dl_size=fsize, ul_size=fsize)
  94. # download plain file via http: ('time_appconnect' is 0)
  95. @pytest.mark.parametrize("proto", ['http/1.1'])
  96. def test_16_04_info_http_download(self, env: Env, httpd, nghttpx, repeat, proto):
  97. count = 2
  98. curl = CurlClient(env=env)
  99. url = f'http://{env.domain1}:{env.http_port}/data.json?[0-{count-1}]'
  100. r = curl.http_download(urls=[url], alpn_proto=proto, with_stats=True)
  101. r.check_stats(count=count, http_status=200, exitcode=0,
  102. remote_port=env.http_port, remote_ip='127.0.0.1')
  103. for idx, s in enumerate(r.stats):
  104. self.check_stat(idx, s, r, dl_size=30, ul_size=0)
  105. def check_stat(self, idx, s, r, dl_size=None, ul_size=None):
  106. self.check_stat_times(s, idx)
  107. # we always send something
  108. self.check_stat_positive(s, idx, 'size_request')
  109. # we always receive response headers
  110. self.check_stat_positive(s, idx, 'size_header')
  111. if ul_size is not None:
  112. assert s['size_upload'] == ul_size, f'stat #{idx}\n{r.dump_logs()}' # the file we sent
  113. assert s['size_request'] >= s['size_upload'], \
  114. f'stat #{idx}, "size_request" smaller than "size_upload", {s}\n{r.dump_logs()}'
  115. if dl_size is not None:
  116. assert s['size_download'] == dl_size, f'stat #{idx}\n{r.dump_logs()}' # the file we received
  117. def check_stat_positive(self, s, idx, key):
  118. assert key in s, f'stat #{idx} "{key}" missing: {s}'
  119. assert s[key] > 0, f'stat #{idx} "{key}" not positive: {s}'
  120. def check_stat_zero(self, s, key):
  121. assert key in s, f'stat "{key}" missing: {s}'
  122. assert s[key] == 0, f'stat "{key}" not zero: {s}'
  123. def check_stat_times(self, s, idx):
  124. # check timings reported on a transfer for consistency
  125. url = s['url_effective']
  126. # all stat keys which reporting timings
  127. all_keys = {
  128. 'time_appconnect', 'time_connect', 'time_redirect',
  129. 'time_pretransfer', 'time_starttransfer', 'time_total'
  130. }
  131. # stat keys where we expect a positive value
  132. pos_keys = {'time_pretransfer', 'time_starttransfer', 'time_total'}
  133. if s['num_connects'] > 0:
  134. pos_keys.add('time_connect')
  135. if url.startswith('https:'):
  136. pos_keys.add('time_appconnect')
  137. if s['num_redirects'] > 0:
  138. pos_keys.add('time_redirect')
  139. zero_keys = all_keys - pos_keys
  140. # assert all zeros are zeros and the others are positive
  141. for key in zero_keys:
  142. self.check_stat_zero(s, key)
  143. for key in pos_keys:
  144. self.check_stat_positive(s, idx, key)
  145. # assert that all timers before "time_pretransfer" are less or equal
  146. for key in ['time_appconnect', 'time_connect', 'time_namelookup']:
  147. assert s[key] < s['time_pretransfer'], f'time "{key}" larger than' \
  148. f'"time_pretransfer": {s}'
  149. # assert transfer start is after pretransfer
  150. assert s['time_pretransfer'] <= s['time_starttransfer'], f'"time_pretransfer" '\
  151. f'greater than "time_starttransfer", {s}'
  152. # assert that transfer start is before total
  153. assert s['time_starttransfer'] <= s['time_total'], f'"time_starttransfer" '\
  154. f'greater than "time_total", {s}'