diff --git a/adafruit_connection_manager.py b/adafruit_connection_manager.py index cc70f3f..cd8d892 100644 --- a/adafruit_connection_manager.py +++ b/adafruit_connection_manager.py @@ -128,8 +128,21 @@ def get_radio_socketpool(radio): elif class_name == "WIZNET5K": import adafruit_wiznet5k.adafruit_wiznet5k_socket as pool # pylint: disable=import-outside-toplevel - # Note: SSL/TLS connections are not supported by the Wiznet5k library at this time - ssl_context = create_fake_ssl_context(pool, radio) + # Note: At this time, SSL/TLS connections are not supported by older + # versions of the Wiznet5k library or on boards withouut the ssl module + # see https://docs.circuitpython.org/en/latest/shared-bindings/support_matrix.html + ssl_context = None + cp_version = sys.implementation[1] + if pool.SOCK_STREAM == 1 and cp_version[0] >= 9 and cp_version[1] >= 1: + try: + import ssl # pylint: disable=import-outside-toplevel + + ssl_context = ssl.create_default_context() + except ImportError: + """SSL not on board default to fake_ssl_context""" + + if ssl_context is None: + ssl_context = create_fake_ssl_context(pool, radio) else: raise AttributeError(f"Unsupported radio class: {class_name}") diff --git a/tests/conftest.py b/tests/conftest.py index 2d9bb0a..e1b2426 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -7,6 +7,7 @@ import sys import mocket +import pytest # pylint: disable=unused-argument @@ -27,5 +28,18 @@ def set_interface(iface): wiznet5k_module = type(sys)("adafruit_wiznet5k") wiznet5k_socket_module = type(sys)("adafruit_wiznet5k_socket") wiznet5k_socket_module.set_interface = set_interface +wiznet5k_socket_module.SOCK_STREAM = 0x21 sys.modules["adafruit_wiznet5k"] = wiznet5k_module sys.modules["adafruit_wiznet5k.adafruit_wiznet5k_socket"] = wiznet5k_socket_module + + +@pytest.fixture(autouse=True) +def reset_connection_manager(monkeypatch): + monkeypatch.setattr( + "adafruit_connection_manager._global_socketpool", + {}, + ) + monkeypatch.setattr( + "adafruit_connection_manager._global_ssl_contexts", + {}, + ) diff --git a/tests/fake_ssl_context_test.py b/tests/fake_ssl_context_test.py index fc566ea..80887d8 100644 --- a/tests/fake_ssl_context_test.py +++ b/tests/fake_ssl_context_test.py @@ -4,6 +4,8 @@ """ FakeSLLSocket Tests """ +from unittest import mock + import mocket import pytest @@ -34,7 +36,8 @@ def test_connect_https_not_supported(): mock_pool.socket.return_value = mock_socket_1 radio = mocket.MockRadio.WIZNET5K() - ssl_context = adafruit_connection_manager.get_radio_ssl_context(radio) + with mock.patch("sys.implementation", return_value=[9, 0, 0]): + ssl_context = adafruit_connection_manager.get_radio_ssl_context(radio) connection_manager = adafruit_connection_manager.ConnectionManager(mock_pool) # verify a HTTPS call for a board without built in WiFi and SSL support errors diff --git a/tests/get_radio_test.py b/tests/get_radio_test.py index ea80f7e..2925f56 100644 --- a/tests/get_radio_test.py +++ b/tests/get_radio_test.py @@ -5,6 +5,7 @@ """ Get socketpool and ssl_context Tests """ import ssl +from unittest import mock import mocket import pytest @@ -26,7 +27,8 @@ def test_get_radio_socketpool_esp32spi(): def test_get_radio_socketpool_wiznet5k(): radio = mocket.MockRadio.WIZNET5K() - socket_pool = adafruit_connection_manager.get_radio_socketpool(radio) + with mock.patch("sys.implementation", return_value=[9, 0, 0]): + socket_pool = adafruit_connection_manager.get_radio_socketpool(radio) assert socket_pool.__name__ == "adafruit_wiznet5k_socket" @@ -58,7 +60,8 @@ def test_get_radio_ssl_context_esp32spi(): def test_get_radio_ssl_context_wiznet5k(): radio = mocket.MockRadio.WIZNET5K() - ssl_contexts = adafruit_connection_manager.get_radio_ssl_context(radio) + with mock.patch("sys.implementation", return_value=[9, 0, 0]): + ssl_contexts = adafruit_connection_manager.get_radio_ssl_context(radio) assert isinstance(ssl_contexts, adafruit_connection_manager._FakeSSLContext)