diff --git a/adafruit_connection_manager.py b/adafruit_connection_manager.py index 658f338..58dcb0c 100644 --- a/adafruit_connection_manager.py +++ b/adafruit_connection_manager.py @@ -97,6 +97,10 @@ def create_fake_ssl_context( return _FakeSSLContext(iface) +class CPythonNetwork: # pylint: disable=too-few-public-methods + """Radio object to use when using ConnectionManager in CPython.""" + + _global_connection_managers = {} _global_key_by_socketpool = {} _global_socketpools = {} @@ -158,6 +162,12 @@ def get_radio_socketpool(radio): if ssl_context is None: ssl_context = create_fake_ssl_context(pool, radio) + elif class_name == "CPythonNetwork": + import socket as pool # pylint: disable=import-outside-toplevel + import ssl # pylint: disable=import-outside-toplevel + + ssl_context = ssl.create_default_context() + else: raise ValueError(f"Unsupported radio class: {class_name}") diff --git a/tests/get_radio_test.py b/tests/get_radio_test.py index 5631bdb..022aecd 100644 --- a/tests/get_radio_test.py +++ b/tests/get_radio_test.py @@ -53,6 +53,13 @@ def test_get_radio_socketpool_wiznet5k( # pylint: disable=unused-argument assert socket_pool in adafruit_connection_manager._global_socketpools.values() +def test_get_radio_socketpool_cpython(): + radio = adafruit_connection_manager.CPythonNetwork() + socket_pool = adafruit_connection_manager.get_radio_socketpool(radio) + assert socket_pool.__name__ == "socket" + assert socket_pool in adafruit_connection_manager._global_socketpools.values() + + def test_get_radio_socketpool_unsupported(): radio = mocket.MockRadio.Unsupported() with pytest.raises(ValueError) as context: @@ -98,6 +105,13 @@ def test_get_radio_ssl_context_wiznet5k( # pylint: disable=unused-argument assert ssl_context in adafruit_connection_manager._global_ssl_contexts.values() +def test_get_radio_ssl_context_cpython(): + radio = adafruit_connection_manager.CPythonNetwork() + ssl_context = adafruit_connection_manager.get_radio_ssl_context(radio) + assert isinstance(ssl_context, ssl.SSLContext) + assert ssl_context in adafruit_connection_manager._global_ssl_contexts.values() + + def test_get_radio_ssl_context_unsupported(): radio = mocket.MockRadio.Unsupported() with pytest.raises(ValueError) as context: