test_13_proxy_auth.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 filecmp
  28. import logging
  29. import os
  30. import re
  31. import time
  32. import pytest
  33. from testenv import Env, CurlClient, ExecResult
  34. log = logging.getLogger(__name__)
  35. @pytest.mark.skipif(condition=Env.setup_incomplete(),
  36. reason=f"missing: {Env.incomplete_reason()}")
  37. class TestProxyAuth:
  38. @pytest.fixture(autouse=True, scope='class')
  39. def _class_scope(self, env, httpd, nghttpx_fwd):
  40. if env.have_nghttpx():
  41. nghttpx_fwd.start_if_needed()
  42. httpd.clear_extra_configs()
  43. httpd.set_proxy_auth(True)
  44. httpd.reload()
  45. yield
  46. httpd.set_proxy_auth(False)
  47. httpd.reload()
  48. def get_tunnel_proto_used(self, r: ExecResult):
  49. for line in r.trace_lines:
  50. m = re.match(r'.* CONNECT tunnel: (\S+) negotiated$', line)
  51. if m:
  52. return m.group(1)
  53. assert False, f'tunnel protocol not found in:\n{"".join(r.trace_lines)}'
  54. return None
  55. # download via http: proxy (no tunnel), no auth
  56. def test_13_01_proxy_no_auth(self, env: Env, httpd, repeat):
  57. curl = CurlClient(env=env)
  58. url = f'http://localhost:{env.http_port}/data.json'
  59. r = curl.http_download(urls=[url], alpn_proto='http/1.1', with_stats=True,
  60. extra_args=curl.get_proxy_args(proxys=False))
  61. r.check_response(count=1, http_status=407)
  62. # download via http: proxy (no tunnel), auth
  63. def test_13_02_proxy_auth(self, env: Env, httpd, repeat):
  64. curl = CurlClient(env=env)
  65. url = f'http://localhost:{env.http_port}/data.json'
  66. xargs = curl.get_proxy_args(proxys=False)
  67. xargs.extend(['--proxy-user', 'proxy:proxy'])
  68. r = curl.http_download(urls=[url], alpn_proto='http/1.1', with_stats=True,
  69. extra_args=xargs)
  70. r.check_response(count=1, http_status=200)
  71. @pytest.mark.skipif(condition=not Env.curl_has_feature('HTTPS-proxy'),
  72. reason='curl lacks HTTPS-proxy support')
  73. @pytest.mark.skipif(condition=not Env.have_nghttpx(), reason="no nghttpx available")
  74. def test_13_03_proxys_no_auth(self, env: Env, httpd, nghttpx_fwd, repeat):
  75. curl = CurlClient(env=env)
  76. url = f'http://localhost:{env.http_port}/data.json'
  77. xargs = curl.get_proxy_args(proxys=True)
  78. r = curl.http_download(urls=[url], alpn_proto='http/1.1', with_stats=True,
  79. extra_args=xargs)
  80. r.check_response(count=1, http_status=407)
  81. @pytest.mark.skipif(condition=not Env.curl_has_feature('HTTPS-proxy'),
  82. reason='curl lacks HTTPS-proxy support')
  83. @pytest.mark.skipif(condition=not Env.have_nghttpx(), reason="no nghttpx available")
  84. def test_13_04_proxys_auth(self, env: Env, httpd, nghttpx_fwd, repeat):
  85. curl = CurlClient(env=env)
  86. url = f'http://localhost:{env.http_port}/data.json'
  87. xargs = curl.get_proxy_args(proxys=True)
  88. xargs.extend(['--proxy-user', 'proxy:proxy'])
  89. r = curl.http_download(urls=[url], alpn_proto='http/1.1', with_stats=True,
  90. extra_args=xargs)
  91. r.check_response(count=1, http_status=200)
  92. def test_13_05_tunnel_http_no_auth(self, env: Env, httpd, repeat):
  93. curl = CurlClient(env=env)
  94. url = f'http://localhost:{env.http_port}/data.json'
  95. xargs = curl.get_proxy_args(proxys=False, tunnel=True)
  96. r = curl.http_download(urls=[url], alpn_proto='http/1.1', with_stats=True,
  97. extra_args=xargs)
  98. # expect "COULD_NOT_CONNECT"
  99. r.check_response(exitcode=56, http_status=None)
  100. def test_13_06_tunnel_http_auth(self, env: Env, httpd, repeat):
  101. curl = CurlClient(env=env)
  102. url = f'http://localhost:{env.http_port}/data.json'
  103. xargs = curl.get_proxy_args(proxys=False, tunnel=True)
  104. xargs.extend(['--proxy-user', 'proxy:proxy'])
  105. r = curl.http_download(urls=[url], alpn_proto='http/1.1', with_stats=True,
  106. extra_args=xargs)
  107. r.check_response(count=1, http_status=200)
  108. @pytest.mark.skipif(condition=not Env.have_nghttpx(), reason="no nghttpx available")
  109. @pytest.mark.skipif(condition=not Env.curl_has_feature('HTTPS-proxy'),
  110. reason='curl lacks HTTPS-proxy support')
  111. @pytest.mark.parametrize("proto", ['http/1.1', 'h2'])
  112. @pytest.mark.parametrize("tunnel", ['http/1.1', 'h2'])
  113. def test_13_07_tunnels_no_auth(self, env: Env, httpd, proto, tunnel, repeat):
  114. if tunnel == 'h2' and not env.curl_uses_lib('nghttp2'):
  115. pytest.skip('only supported with nghttp2')
  116. curl = CurlClient(env=env)
  117. url = f'https://localhost:{env.https_port}/data.json'
  118. xargs = curl.get_proxy_args(proxys=True, tunnel=True, proto=tunnel)
  119. r = curl.http_download(urls=[url], alpn_proto=proto, with_stats=True,
  120. extra_args=xargs)
  121. # expect "COULD_NOT_CONNECT"
  122. r.check_response(exitcode=56, http_status=None)
  123. assert self.get_tunnel_proto_used(r) == 'HTTP/2' \
  124. if tunnel == 'h2' else 'HTTP/1.1'
  125. @pytest.mark.skipif(condition=not Env.have_nghttpx(), reason="no nghttpx available")
  126. @pytest.mark.skipif(condition=not Env.curl_has_feature('HTTPS-proxy'),
  127. reason='curl lacks HTTPS-proxy support')
  128. @pytest.mark.parametrize("proto", ['http/1.1', 'h2'])
  129. @pytest.mark.parametrize("tunnel", ['http/1.1', 'h2'])
  130. def test_13_08_tunnels_auth(self, env: Env, httpd, proto, tunnel, repeat):
  131. if tunnel == 'h2' and not env.curl_uses_lib('nghttp2'):
  132. pytest.skip('only supported with nghttp2')
  133. curl = CurlClient(env=env)
  134. url = f'https://localhost:{env.https_port}/data.json'
  135. xargs = curl.get_proxy_args(proxys=True, tunnel=True, proto=tunnel)
  136. xargs.extend(['--proxy-user', 'proxy:proxy'])
  137. r = curl.http_download(urls=[url], alpn_proto=proto, with_stats=True,
  138. extra_args=xargs)
  139. r.check_response(count=1, http_status=200,
  140. protocol='HTTP/2' if proto == 'h2' else 'HTTP/1.1')
  141. assert self.get_tunnel_proto_used(r) == 'HTTP/2' \
  142. if tunnel == 'h2' else 'HTTP/1.1'