test_13_proxy_auth.py 7.2 KB

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