Repository navigation
Expand file tree
/
Copy pathmesh_endpoint_connectivity_tests.py
More file actions
315 lines (261 loc) · 12.5 KB
/
Copy pathmesh_endpoint_connectivity_tests.py
File metadata and controls
315 lines (261 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import itertools
import socket
from _socket import gaierror
from ssl import SSLCertVerificationError
from urllib.parse import urlparse
import pytest
from requests.exceptions import ConnectionError as RequestsConnectionError
from requests.exceptions import HTTPError, SSLError
from urllib3.exceptions import ProxyError
import mesh_client
from mesh_client import DEPRECATED_HSCN_INT_ENDPOINT, Endpoint, MeshClient
from tests.helpers import MOCK_CERT, MOCK_KEY, temp_env_vars
def _host_resolves(endpoint: Endpoint):
parsed = urlparse(endpoint.url)
try:
socket.gethostbyname(str(parsed.hostname))
except gaierror as err:
if err.args[1] == "Name or service not known":
return False
raise
return True
_ENDPOINTS = [(name, endpoint) for name, endpoint in mesh_client.ENDPOINTS if not name.startswith("LOCAL_")]
_INTERNET_ENDPOINTS = [(name, endpoint) for name, endpoint in _ENDPOINTS if not name.startswith("DEPRECATED_HSCN_")]
_HSCN_ENDPOINTS = [(name, endpoint) for name, endpoint in _ENDPOINTS if name.startswith("DEPRECATED_HSCN_")]
CONNECTION_ABORTED_ERROR = "Connection aborted."
REMOTE_END_CLOSED_CONNECTION = "Remote end closed connection without response"
SSL_CERTIFICATE_ERROR = "SSL certificate error"
LOCAL_HTTPS_PROXY_URL = "http://localhost:8019"
@pytest.mark.parametrize(("name", "endpoint"), _HSCN_ENDPOINTS)
@pytest.mark.skipif(not _host_resolves(DEPRECATED_HSCN_INT_ENDPOINT), reason="these hosts will only resolve on HSCN")
def test_hscn_endpoints(name: str, endpoint: Endpoint):
with (
pytest.raises((HTTPError, SSLError)) as err,
MeshClient(endpoint, "BADUSERNAME", "BADPASSWORD", cert=(MOCK_CERT, MOCK_KEY)) as client,
):
client.ping()
if err.type == SSLError:
assert err.value.args[0].reason.args[0].reason == "CERTIFICATE_VERIFY_FAILED"
else:
assert err.value.response is not None
assert err.value.response.status_code == 400
@pytest.mark.parametrize(("name", "endpoint"), _HSCN_ENDPOINTS)
@pytest.mark.skipif(not _host_resolves(DEPRECATED_HSCN_INT_ENDPOINT), reason="these hosts will only resolve on HSCN")
def test_hscn_endpoints_verify_false(name: str, endpoint: Endpoint):
with (
pytest.raises((HTTPError, SSLError)) as err,
MeshClient(endpoint.url, "BADUSERNAME", "BADPASSWORD", cert=(MOCK_CERT, MOCK_KEY), verify=False) as client,
):
client.ping()
if err.type == SSLError:
assert err.value.args[0].reason.args[0].reason == "CERTIFICATE_VERIFY_FAILED"
else:
assert err.value.response is not None
assert err.value.response.status_code == 400
@pytest.mark.parametrize(("name", "endpoint"), _HSCN_ENDPOINTS)
@pytest.mark.skipif(not _host_resolves(DEPRECATED_HSCN_INT_ENDPOINT), reason="these hosts will only resolve on HSCN")
def test_hscn_endpoints_defaults_from_hostname(name: str, endpoint: Endpoint):
with (
pytest.raises((HTTPError, SSLError)) as err,
MeshClient(endpoint.url, "BADUSERNAME", "BADPASSWORD", cert=(MOCK_CERT, MOCK_KEY)) as client,
):
client.ping()
if err.type == SSLError:
assert err.value.args[0].reason.args[0].reason == "CERTIFICATE_VERIFY_FAILED"
else:
assert err.value.response is not None
assert err.value.response.status_code == 400
assert err.value.args[0] == f"400 Client Error: Bad Request for url: {endpoint.url}/messageexchange/_ping"
assert SSL_CERTIFICATE_ERROR in err.value.response.text
@pytest.mark.parametrize(("name", "endpoint"), _HSCN_ENDPOINTS)
@pytest.mark.skipif(not _host_resolves(DEPRECATED_HSCN_INT_ENDPOINT), reason="these hosts will only resolve on HSCN")
def test_dep_hscn_endpoint_common_name_check_false(name: str, endpoint: Endpoint):
with (
pytest.raises((SSLError, HTTPError)) as err,
MeshClient(
endpoint, "BADUSERNAME", "BADPASSWORD", cert=(MOCK_CERT, MOCK_KEY), hostname_checks_common_name=False
) as client,
):
client.ping()
if err.type == SSLError:
assert err.value.args[0].reason.args[0].reason == "CERTIFICATE_VERIFY_FAILED"
else:
assert err.value.response is not None
assert err.value.args[0] == f"400 Client Error: Bad Request for url: {endpoint.url}/messageexchange/_ping"
assert SSL_CERTIFICATE_ERROR in err.value.response.text
assert err.value.response.status_code == 400
@pytest.mark.parametrize(("name", "endpoint"), _INTERNET_ENDPOINTS)
def test_internet_endpoints(name: str, endpoint: Endpoint):
with (
pytest.raises((RequestsConnectionError, SSLError)) as err,
MeshClient(endpoint, "BADUSERNAME", "BADPASSWORD", cert=(MOCK_CERT, MOCK_KEY)) as client,
):
client.ping()
if err.type == SSLError:
assert isinstance(err.value.args[0].reason.args[0], SSLCertVerificationError)
else:
assert isinstance(err.value.args[0].reason.args[1], ConnectionResetError)
@pytest.mark.parametrize(("name", "endpoint"), _INTERNET_ENDPOINTS)
def test_internet_endpoints_common_name_check_false(name: str, endpoint: Endpoint):
with (
pytest.raises((RequestsConnectionError, SSLError)) as err,
MeshClient(
endpoint, "BADUSERNAME", "BADPASSWORD", cert=(MOCK_CERT, MOCK_KEY), hostname_checks_common_name=False
) as client,
):
client.ping()
if err.type == SSLError:
assert isinstance(err.value.args[0].reason.args[0], SSLCertVerificationError)
else:
assert isinstance(err.value.args[0].reason.args[1], ConnectionResetError)
@pytest.mark.parametrize(("name", "endpoint"), _INTERNET_ENDPOINTS)
def test_internet_endpoints_verify_false(name: str, endpoint: Endpoint):
with (
pytest.raises((RequestsConnectionError, SSLError)) as err,
MeshClient(endpoint.url, "BADUSERNAME", "BADPASSWORD", cert=(MOCK_CERT, MOCK_KEY), verify=False) as client,
):
client.ping()
if err.type == SSLError:
assert isinstance(err.value.args[0].reason.args[0], SSLCertVerificationError)
else:
assert isinstance(err.value.args[0].reason.args[1], ConnectionResetError)
@pytest.mark.parametrize(("name", "endpoint"), _INTERNET_ENDPOINTS)
def test_internet_endpoints_defaults_from_hostname(name: str, endpoint: Endpoint):
with (
pytest.raises((RequestsConnectionError, SSLError)) as err,
MeshClient(endpoint.url, "BADUSERNAME", "BADPASSWORD", cert=(MOCK_CERT, MOCK_KEY), verify=None) as client,
):
client.ping()
if err.type == SSLError:
assert isinstance(err.value.args[0].reason.args[0], SSLCertVerificationError)
else:
assert isinstance(err.value.args[0].reason.args[1], ConnectionResetError)
@pytest.mark.parametrize(("name", "endpoint"), _INTERNET_ENDPOINTS)
def test_internet_endpoints_with_port_defaults_from_hostname(name: str, endpoint: Endpoint):
with (
pytest.raises((RequestsConnectionError, SSLError)) as err,
MeshClient(
f"{endpoint.url}:443", "BADUSERNAME", "BADPASSWORD", cert=(MOCK_CERT, MOCK_KEY), verify=None
) as client,
):
client.ping()
if err.type == SSLError:
assert isinstance(err.value.args[0].reason.args[0], SSLCertVerificationError)
else:
assert isinstance(err.value.args[0].reason.args[1], ConnectionResetError)
@pytest.mark.parametrize(
("name", "endpoint", "check_hostname"),
[(ep[0], ep[1], check_hostname) for check_hostname, ep in itertools.product([True, False, None], _HSCN_ENDPOINTS)],
)
@pytest.mark.skipif(not _host_resolves(DEPRECATED_HSCN_INT_ENDPOINT), reason="these hosts will only resolve on HSCN")
def test_hscn_endpoints_check_hostname(name: str, endpoint: Endpoint, check_hostname: bool):
with (
pytest.raises((HTTPError, SSLError)) as err,
MeshClient(
endpoint.url,
"BADUSERNAME",
"BADPASSWORD",
cert=(MOCK_CERT, MOCK_KEY),
verify=endpoint.verify,
check_hostname=check_hostname,
) as client,
):
client.ping()
if err.type == SSLError:
assert isinstance(err.value.args[0].reason.args[0], SSLCertVerificationError)
else:
assert err.value.response is not None
assert err.value.args[0] == f"400 Client Error: Bad Request for url: {endpoint.url}/messageexchange/_ping"
assert SSL_CERTIFICATE_ERROR in err.value.response.text
assert err.value.response.status_code == 400
@pytest.mark.parametrize(
("name", "endpoint", "check_hostname"),
[(ep[0], ep[1], check_hostname) for check_hostname, ep in itertools.product([True, False], _INTERNET_ENDPOINTS)],
)
def test_internet_endpoints_check_hostname(name: str, endpoint: Endpoint, check_hostname: bool):
with (
pytest.raises((RequestsConnectionError, SSLError)) as err,
MeshClient(
endpoint.url,
"BADUSERNAME",
"BADPASSWORD",
cert=(MOCK_CERT, MOCK_KEY),
verify=endpoint.verify,
check_hostname=check_hostname,
) as client,
):
client.ping()
if err.type == SSLError:
assert isinstance(err.value.args[0].reason.args[0], SSLCertVerificationError)
else:
assert err.value.response is None
assert isinstance(err.value.args[0].reason.args[1], ConnectionResetError)
@pytest.mark.parametrize(("name", "endpoint"), _HSCN_ENDPOINTS)
@pytest.mark.skipif(not _host_resolves(DEPRECATED_HSCN_INT_ENDPOINT), reason="these hosts will only resolve on HSCN")
def test_hscn_endpoints_via_an_explicit_proxy(name: str, endpoint: Endpoint):
with (
pytest.raises((HTTPError, SSLError)) as err,
MeshClient(
endpoint,
"BADUSERNAME",
"BADPASSWORD",
cert=(MOCK_CERT, MOCK_KEY),
proxies={"https": LOCAL_HTTPS_PROXY_URL},
timeout=10,
) as client,
):
client.ping()
if err.type == SSLError:
assert isinstance(err.value.args[0].reason.args[0], SSLCertVerificationError)
else:
assert err.value.response is not None
assert err.value.args[0] == f"400 Client Error: Bad Request for url: {endpoint.url}/messageexchange/_ping"
assert SSL_CERTIFICATE_ERROR in err.value.response.text
assert err.value.response.status_code == 400
@pytest.mark.parametrize(("name", "endpoint"), _HSCN_ENDPOINTS)
@pytest.mark.skipif(not _host_resolves(DEPRECATED_HSCN_INT_ENDPOINT), reason="these hosts will only resolve on HSCN")
def test_hscn_endpoints_via_an_ambient_proxy(name: str, endpoint: Endpoint):
with (
temp_env_vars(HTTPS_PROXY=LOCAL_HTTPS_PROXY_URL),
pytest.raises((HTTPError, SSLError)) as err,
MeshClient(endpoint, "BADUSERNAME", "BADPASSWORD", cert=(MOCK_CERT, MOCK_KEY)) as client,
):
client.ping()
if err.type == SSLError:
assert isinstance(err.value.args[0].reason.args[0], SSLCertVerificationError)
else:
assert err.value.response is not None
assert err.value.args[0] == f"400 Client Error: Bad Request for url: {endpoint.url}/messageexchange/_ping"
assert SSL_CERTIFICATE_ERROR in err.value.response.text
assert err.value.response.status_code == 400
@pytest.mark.parametrize(("name", "endpoint"), _INTERNET_ENDPOINTS)
def test_internet_endpoints_via_explicit_proxy(name: str, endpoint: Endpoint):
with (
pytest.raises((RequestsConnectionError, SSLError)) as err,
MeshClient(
endpoint,
"BADUSERNAME",
"BADPASSWORD",
cert=(MOCK_CERT, MOCK_KEY),
proxies={"https": LOCAL_HTTPS_PROXY_URL},
) as client,
):
client.handshake()
if err.type == SSLError:
assert isinstance(err.value.args[0].reason.args[0], SSLCertVerificationError)
else:
assert isinstance(err.value.args[0].reason, ProxyError)
assert str(err.value.args[0].reason.args[1]) == REMOTE_END_CLOSED_CONNECTION
@pytest.mark.parametrize(("name", "endpoint"), _INTERNET_ENDPOINTS)
def test_internet_endpoints_via_ambient_proxy(name: str, endpoint: Endpoint):
with (
temp_env_vars(HTTPS_PROXY=LOCAL_HTTPS_PROXY_URL),
pytest.raises((RequestsConnectionError, SSLError)) as err,
MeshClient(endpoint, "BADUSERNAME", "BADPASSWORD", cert=(MOCK_CERT, MOCK_KEY)) as client,
):
client.handshake()
if err.type == SSLError:
assert isinstance(err.value.args[0].reason.args[0], SSLCertVerificationError)
else:
assert isinstance(err.value.args[0].reason, ProxyError)
assert str(err.value.args[0].reason.args[1]) == REMOTE_END_CLOSED_CONNECTION