Skip to content

Commit 513cb12

Browse files
authored
Merge pull request #9 from justmobilize/wiznet5k-ssl
Add support for WIZNET5K ssl
2 parents 9322a01 + 7ffe4bd commit 513cb12

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

adafruit_connection_manager.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import errno
3030
import sys
3131

32+
WIZNET5K_SSL_SUPPORT_VERSION = (9, 1)
33+
3234
# typing
3335

3436

@@ -128,8 +130,23 @@ def get_radio_socketpool(radio):
128130
elif class_name == "WIZNET5K":
129131
import adafruit_wiznet5k.adafruit_wiznet5k_socket as pool # pylint: disable=import-outside-toplevel
130132

131-
# Note: SSL/TLS connections are not supported by the Wiznet5k library at this time
132-
ssl_context = create_fake_ssl_context(pool, radio)
133+
# Note: At this time, SSL/TLS connections are not supported by older
134+
# versions of the Wiznet5k library or on boards withouut the ssl module
135+
# see https://docs.circuitpython.org/en/latest/shared-bindings/support_matrix.html
136+
ssl_context = None
137+
cp_version = sys.implementation[1]
138+
if pool.SOCK_STREAM == 1 and cp_version >= WIZNET5K_SSL_SUPPORT_VERSION:
139+
try:
140+
import ssl # pylint: disable=import-outside-toplevel
141+
142+
ssl_context = ssl.create_default_context()
143+
pool.set_interface(radio)
144+
except ImportError:
145+
# if SSL not on board, default to fake_ssl_context
146+
pass
147+
148+
if ssl_context is None:
149+
ssl_context = create_fake_ssl_context(pool, radio)
133150

134151
else:
135152
raise AttributeError(f"Unsupported radio class: {class_name}")

tests/conftest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ def adafruit_wiznet5k_socket_module():
4949
del sys.modules["adafruit_wiznet5k.adafruit_wiznet5k_socket"]
5050

5151

52+
@pytest.fixture
53+
def adafruit_wiznet5k_with_ssl_socket_module():
54+
wiznet5k_module = type(sys)("adafruit_wiznet5k")
55+
wiznet5k_socket_module = type(sys)("adafruit_wiznet5k_socket")
56+
wiznet5k_socket_module.set_interface = set_interface
57+
wiznet5k_socket_module.SOCK_STREAM = 1
58+
sys.modules["adafruit_wiznet5k"] = wiznet5k_module
59+
sys.modules["adafruit_wiznet5k.adafruit_wiznet5k_socket"] = wiznet5k_socket_module
60+
yield
61+
del sys.modules["adafruit_wiznet5k"]
62+
del sys.modules["adafruit_wiznet5k.adafruit_wiznet5k_socket"]
63+
64+
5265
@pytest.fixture(autouse=True)
5366
def reset_connection_manager(monkeypatch):
5467
monkeypatch.setattr(

tests/ssl_context_test.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
""" SLL Context Tests """
66

77
import ssl
8+
from unittest import mock
89

910
import mocket
1011
import pytest
1112

1213
import adafruit_connection_manager
14+
from adafruit_connection_manager import WIZNET5K_SSL_SUPPORT_VERSION
1315

1416

1517
def test_connect_esp32spi_https( # pylint: disable=unused-argument
@@ -50,7 +52,9 @@ def test_connect_wiznet5k_https_not_supported( # pylint: disable=unused-argumen
5052
):
5153
mock_pool = mocket.MocketPool()
5254
radio = mocket.MockRadio.WIZNET5K()
53-
ssl_context = adafruit_connection_manager.get_radio_ssl_context(radio)
55+
old_version = (WIZNET5K_SSL_SUPPORT_VERSION[0] - 1, 0, 0)
56+
with mock.patch("sys.implementation", (None, old_version)):
57+
ssl_context = adafruit_connection_manager.get_radio_ssl_context(radio)
5458
connection_manager = adafruit_connection_manager.ConnectionManager(mock_pool)
5559

5660
# verify a HTTPS call for a board without built in WiFi and SSL support errors
@@ -59,3 +63,12 @@ def test_connect_wiznet5k_https_not_supported( # pylint: disable=unused-argumen
5963
mocket.MOCK_HOST_1, 443, "https:", ssl_context=ssl_context
6064
)
6165
assert "This radio does not support TLS/HTTPS" in str(context)
66+
67+
68+
def test_connect_wiznet5k_https_supported( # pylint: disable=unused-argument
69+
adafruit_wiznet5k_with_ssl_socket_module,
70+
):
71+
radio = mocket.MockRadio.WIZNET5K()
72+
with mock.patch("sys.implementation", (None, WIZNET5K_SSL_SUPPORT_VERSION)):
73+
ssl_context = adafruit_connection_manager.get_radio_ssl_context(radio)
74+
assert isinstance(ssl_context, ssl.SSLContext)

0 commit comments

Comments
 (0)