| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import os |
| 4 |
from unittest import mock |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
try: |
| 9 |
from cryptography import x509 |
| 10 |
from OpenSSL.crypto import FILETYPE_PEM, load_certificate |
| 11 |
|
| 12 |
from urllib3.contrib.pyopenssl import _dnsname_to_stdlib, get_subj_alt_name |
| 13 |
except ImportError: |
| 14 |
pass |
| 15 |
|
| 16 |
|
| 17 |
def setup_module() -> None: |
| 18 |
try: |
| 19 |
from urllib3.contrib.pyopenssl import inject_into_urllib3 |
| 20 |
|
| 21 |
inject_into_urllib3() |
| 22 |
except ImportError as e: |
| 23 |
pytest.skip(f"Could not import PyOpenSSL: {e!r}") |
| 24 |
|
| 25 |
|
| 26 |
def teardown_module() -> None: |
| 27 |
try: |
| 28 |
from urllib3.contrib.pyopenssl import extract_from_urllib3 |
| 29 |
|
| 30 |
extract_from_urllib3() |
| 31 |
except ImportError: |
| 32 |
pass |
| 33 |
|
| 34 |
|
| 35 |
from ..test_ssl import TestSSL # noqa: E402, F401 |
| 36 |
from ..test_util import TestUtilSSL # noqa: E402, F401 |
| 37 |
from ..with_dummyserver.test_https import ( # noqa: E402, F401 |
| 38 |
TestHTTPS_IPV4SAN, |
| 39 |
TestHTTPS_IPV6SAN, |
| 40 |
TestHTTPS_TLSv1, |
| 41 |
TestHTTPS_TLSv1_1, |
| 42 |
TestHTTPS_TLSv1_2, |
| 43 |
TestHTTPS_TLSv1_3, |
| 44 |
) |
| 45 |
|
| 46 |
from ..with_dummyserver.test_socketlevel import ( # noqa: E402, F401 # isort: skip |
| 47 |
TestClientCerts, |
| 48 |
TestSNI, |
| 49 |
TestSSL as TestSocketSSL, |
| 50 |
TestSocketClosing, |
| 51 |
) |
| 52 |
|
| 53 |
|
| 54 |
class TestPyOpenSSLHelpers: |
| 55 |
""" |
| 56 |
Tests for PyOpenSSL helper functions. |
| 57 |
""" |
| 58 |
|
| 59 |
def test_dnsname_to_stdlib_simple(self) -> None: |
| 60 |
""" |
| 61 |
We can convert a dnsname to a native string when the domain is simple. |
| 62 |
""" |
| 63 |
name = "उदाहरण.परीक" |
| 64 |
expected_result = "xn--p1b6ci4b4b3a.xn--11b5bs8d" |
| 65 |
|
| 66 |
assert _dnsname_to_stdlib(name) == expected_result |
| 67 |
|
| 68 |
def test_dnsname_to_stdlib_leading_period(self) -> None: |
| 69 |
""" |
| 70 |
If there is a . in front of the domain name we correctly encode it. |
| 71 |
""" |
| 72 |
name = ".उदाहरण.परीक" |
| 73 |
expected_result = ".xn--p1b6ci4b4b3a.xn--11b5bs8d" |
| 74 |
|
| 75 |
assert _dnsname_to_stdlib(name) == expected_result |
| 76 |
|
| 77 |
def test_dnsname_to_stdlib_leading_splat(self) -> None: |
| 78 |
""" |
| 79 |
If there's a wildcard character in the front of the string we handle it |
| 80 |
appropriately. |
| 81 |
""" |
| 82 |
name = "*.उदाहरण.परीक" |
| 83 |
expected_result = "*.xn--p1b6ci4b4b3a.xn--11b5bs8d" |
| 84 |
|
| 85 |
assert _dnsname_to_stdlib(name) == expected_result |
| 86 |
|
| 87 |
@mock.patch("urllib3.contrib.pyopenssl.log.warning") |
| 88 |
def test_get_subj_alt_name(self, mock_warning: mock.MagicMock) -> None: |
| 89 |
""" |
| 90 |
If a certificate has two subject alternative names, cryptography raises |
| 91 |
an x509.DuplicateExtension exception. |
| 92 |
""" |
| 93 |
path = os.path.join(os.path.dirname(__file__), "duplicate_san.pem") |
| 94 |
with open(path, "rb") as fp: |
| 95 |
cert = load_certificate(FILETYPE_PEM, fp.read()) |
| 96 |
|
| 97 |
assert get_subj_alt_name(cert) == [] |
| 98 |
|
| 99 |
assert mock_warning.call_count == 1 |
| 100 |
assert isinstance(mock_warning.call_args[0][1], x509.DuplicateExtension) |