test_16_info.py 7.2 KB

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