| 1 |
# Copyright 2025, the TUF contributors |
| 2 |
# SPDX-License-Identifier: MIT OR Apache-2.0 |
| 3 |
|
| 4 |
"""Test ngclient ProxyEnvironment""" |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
import sys |
| 9 |
import unittest |
| 10 |
from unittest.mock import Mock, patch |
| 11 |
|
| 12 |
from urllib3 import PoolManager, ProxyManager |
| 13 |
|
| 14 |
from tests import utils |
| 15 |
from tuf.ngclient._internal.proxy import ProxyEnvironment |
| 16 |
|
| 17 |
|
| 18 |
class TestProxyEnvironment(unittest.TestCase): |
| 19 |
"""Test ngclient ProxyEnvironment implementation |
| 20 |
|
| 21 |
These tests use the ProxyEnvironment.get_pool_manager() endpoint and then |
| 22 |
look at the ProxyEnvironment._poolmanagers dict keys to decide if the result |
| 23 |
is correct. |
| 24 |
|
| 25 |
The test environment is changed via mocking getproxies(): this is a urllib |
| 26 |
method that returns a dict with the proxy environment variable contents. |
| 27 |
|
| 28 |
Testing ProxyEnvironment.request() would possibly be better but far more |
| 29 |
difficult: the current test implementation does not require actually setting up |
| 30 |
all of the different proxies. |
| 31 |
""" |
| 32 |
|
| 33 |
def assert_pool_managers( |
| 34 |
self, env: ProxyEnvironment, expected: list[str | None] |
| 35 |
) -> None: |
| 36 |
# Pool managers have the expected proxy urls |
| 37 |
self.assertEqual(list(env._pool_managers.keys()), expected) |
| 38 |
|
| 39 |
# Pool manager types are as expected |
| 40 |
for proxy_url, pool_manager in env._pool_managers.items(): |
| 41 |
self.assertIsInstance(pool_manager, PoolManager) |
| 42 |
if proxy_url is not None: |
| 43 |
self.assertIsInstance(pool_manager, ProxyManager) |
| 44 |
|
| 45 |
@patch("tuf.ngclient._internal.proxy.getproxies") |
| 46 |
def test_no_variables(self, mock_getproxies: Mock) -> None: |
| 47 |
mock_getproxies.return_value = {} |
| 48 |
|
| 49 |
env = ProxyEnvironment() |
| 50 |
env.get_pool_manager("http", "example.com") |
| 51 |
env.get_pool_manager("https", "example.com") |
| 52 |
env.get_pool_manager("https", "example.com") |
| 53 |
env.get_pool_manager("https", "subdomain.example.com") |
| 54 |
env.get_pool_manager("https", "differentsite.com") |
| 55 |
|
| 56 |
# There is a single pool manager (no proxies) |
| 57 |
self.assert_pool_managers(env, [None]) |
| 58 |
|
| 59 |
@patch("tuf.ngclient._internal.proxy.getproxies") |
| 60 |
def test_proxy_set(self, mock_getproxies: Mock) -> None: |
| 61 |
mock_getproxies.return_value = { |
| 62 |
"https": "http://localhost:8888", |
| 63 |
} |
| 64 |
|
| 65 |
env = ProxyEnvironment() |
| 66 |
env.get_pool_manager("http", "example.com") |
| 67 |
env.get_pool_manager("https", "example.com") |
| 68 |
env.get_pool_manager("https", "example.com") |
| 69 |
env.get_pool_manager("https", "differentsite.com") |
| 70 |
|
| 71 |
# There are two pool managers: A plain poolmanager and https proxymanager |
| 72 |
self.assert_pool_managers(env, [None, "http://localhost:8888"]) |
| 73 |
|
| 74 |
@patch("tuf.ngclient._internal.proxy.getproxies") |
| 75 |
def test_proxies_set(self, mock_getproxies: Mock) -> None: |
| 76 |
mock_getproxies.return_value = { |
| 77 |
"http": "http://localhost:8888", |
| 78 |
"https": "http://localhost:9999", |
| 79 |
} |
| 80 |
|
| 81 |
env = ProxyEnvironment() |
| 82 |
env.get_pool_manager("http", "example.com") |
| 83 |
env.get_pool_manager("https", "example.com") |
| 84 |
env.get_pool_manager("https", "example.com") |
| 85 |
env.get_pool_manager("https", "subdomain.example.com") |
| 86 |
env.get_pool_manager("https", "differentsite.com") |
| 87 |
|
| 88 |
# There are two pool managers: A http proxymanager and https proxymanager |
| 89 |
self.assert_pool_managers( |
| 90 |
env, ["http://localhost:8888", "http://localhost:9999"] |
| 91 |
) |
| 92 |
|
| 93 |
@patch("tuf.ngclient._internal.proxy.getproxies") |
| 94 |
def test_no_proxy_set(self, mock_getproxies: Mock) -> None: |
| 95 |
mock_getproxies.return_value = { |
| 96 |
"http": "http://localhost:8888", |
| 97 |
"https": "http://localhost:9999", |
| 98 |
"no": "somesite.com, example.com, another.site.com", |
| 99 |
} |
| 100 |
|
| 101 |
env = ProxyEnvironment() |
| 102 |
env.get_pool_manager("http", "example.com") |
| 103 |
env.get_pool_manager("https", "example.com") |
| 104 |
env.get_pool_manager("https", "example.com") |
| 105 |
|
| 106 |
# There is a single pool manager (no proxies) |
| 107 |
self.assert_pool_managers(env, [None]) |
| 108 |
|
| 109 |
env.get_pool_manager("http", "differentsite.com") |
| 110 |
env.get_pool_manager("https", "differentsite.com") |
| 111 |
|
| 112 |
# There are three pool managers: plain poolmanager for no_proxy domains, |
| 113 |
# http proxymanager and https proxymanager |
| 114 |
self.assert_pool_managers( |
| 115 |
env, [None, "http://localhost:8888", "http://localhost:9999"] |
| 116 |
) |
| 117 |
|
| 118 |
@patch("tuf.ngclient._internal.proxy.getproxies") |
| 119 |
def test_no_proxy_subdomain_match(self, mock_getproxies: Mock) -> None: |
| 120 |
mock_getproxies.return_value = { |
| 121 |
"https": "http://localhost:9999", |
| 122 |
"no": "somesite.com, example.com, another.site.com", |
| 123 |
} |
| 124 |
|
| 125 |
env = ProxyEnvironment() |
| 126 |
|
| 127 |
# this should match example.com in no_proxy |
| 128 |
env.get_pool_manager("https", "subdomain.example.com") |
| 129 |
|
| 130 |
# There is a single pool manager (no proxies) |
| 131 |
self.assert_pool_managers(env, [None]) |
| 132 |
|
| 133 |
# this should not match example.com in no_proxy |
| 134 |
env.get_pool_manager("https", "xexample.com") |
| 135 |
|
| 136 |
# There are two pool managers: plain poolmanager for no_proxy domains, |
| 137 |
# and a https proxymanager |
| 138 |
self.assert_pool_managers(env, [None, "http://localhost:9999"]) |
| 139 |
|
| 140 |
@patch("tuf.ngclient._internal.proxy.getproxies") |
| 141 |
def test_no_proxy_wildcard(self, mock_getproxies: Mock) -> None: |
| 142 |
mock_getproxies.return_value = { |
| 143 |
"https": "http://localhost:8888", |
| 144 |
"no": "*", |
| 145 |
} |
| 146 |
|
| 147 |
env = ProxyEnvironment() |
| 148 |
env.get_pool_manager("https", "example.com") |
| 149 |
env.get_pool_manager("https", "differentsite.com") |
| 150 |
env.get_pool_manager("https", "subdomain.example.com") |
| 151 |
|
| 152 |
# There is a single pool manager, no proxies |
| 153 |
self.assert_pool_managers(env, [None]) |
| 154 |
|
| 155 |
@patch("tuf.ngclient._internal.proxy.getproxies") |
| 156 |
def test_no_proxy_leading_dot(self, mock_getproxies: Mock) -> None: |
| 157 |
mock_getproxies.return_value = { |
| 158 |
"https": "http://localhost:8888", |
| 159 |
"no": ".example.com", |
| 160 |
} |
| 161 |
|
| 162 |
env = ProxyEnvironment() |
| 163 |
env.get_pool_manager("https", "example.com") |
| 164 |
env.get_pool_manager("https", "subdomain.example.com") |
| 165 |
|
| 166 |
# There is a single pool manager, no proxies |
| 167 |
self.assert_pool_managers(env, [None]) |
| 168 |
|
| 169 |
@patch("tuf.ngclient._internal.proxy.getproxies") |
| 170 |
def test_all_proxy_set(self, mock_getproxies: Mock) -> None: |
| 171 |
mock_getproxies.return_value = { |
| 172 |
"all": "http://localhost:8888", |
| 173 |
} |
| 174 |
|
| 175 |
env = ProxyEnvironment() |
| 176 |
env.get_pool_manager("http", "example.com") |
| 177 |
env.get_pool_manager("https", "example.com") |
| 178 |
env.get_pool_manager("https", "example.com") |
| 179 |
env.get_pool_manager("https", "subdomain.example.com") |
| 180 |
env.get_pool_manager("https", "differentsite.com") |
| 181 |
|
| 182 |
# There is a single proxy manager |
| 183 |
self.assert_pool_managers(env, ["http://localhost:8888"]) |
| 184 |
|
| 185 |
# This urllib3 currently only handles http and https but let's test anyway |
| 186 |
env.get_pool_manager("file", None) |
| 187 |
|
| 188 |
# proxy manager and a plain pool manager |
| 189 |
self.assert_pool_managers(env, ["http://localhost:8888", None]) |
| 190 |
|
| 191 |
@patch("tuf.ngclient._internal.proxy.getproxies") |
| 192 |
def test_all_proxy_and_no_proxy_set(self, mock_getproxies: Mock) -> None: |
| 193 |
mock_getproxies.return_value = { |
| 194 |
"all": "http://localhost:8888", |
| 195 |
"no": "somesite.com, example.com, another.site.com", |
| 196 |
} |
| 197 |
|
| 198 |
env = ProxyEnvironment() |
| 199 |
env.get_pool_manager("http", "example.com") |
| 200 |
env.get_pool_manager("https", "example.com") |
| 201 |
env.get_pool_manager("https", "example.com") |
| 202 |
env.get_pool_manager("https", "subdomain.example.com") |
| 203 |
|
| 204 |
# There is a single pool manager (no proxies) |
| 205 |
self.assert_pool_managers(env, [None]) |
| 206 |
|
| 207 |
env.get_pool_manager("http", "differentsite.com") |
| 208 |
env.get_pool_manager("https", "differentsite.com") |
| 209 |
|
| 210 |
# There are two pool managers: plain poolmanager for no_proxy domains and |
| 211 |
# one proxymanager |
| 212 |
self.assert_pool_managers(env, [None, "http://localhost:8888"]) |
| 213 |
|
| 214 |
|
| 215 |
if __name__ == "__main__": |
| 216 |
utils.configure_test_logging(sys.argv) |
| 217 |
unittest.main() |