Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for WIZNET5K ssl #9

Merged
merged 7 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions adafruit_connection_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import errno
import sys

WIZNET5K_SSL_SUPPORT_VERSION = (9, 1)

# typing


Expand Down Expand Up @@ -128,8 +130,22 @@ 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 >= WIZNET5K_SSL_SUPPORT_VERSION:
try:
import ssl # pylint: disable=import-outside-toplevel

ssl_context = ssl.create_default_context()
pool.set_interface(radio)
except ImportError:
"""SSL not on board default to fake_ssl_context"""
justmobilize marked this conversation as resolved.
Show resolved Hide resolved

if ssl_context is None:
ssl_context = create_fake_ssl_context(pool, radio)

else:
raise AttributeError(f"Unsupported radio class: {class_name}")
Expand Down
13 changes: 13 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ def adafruit_wiznet5k_socket_module():
del sys.modules["adafruit_wiznet5k.adafruit_wiznet5k_socket"]


@pytest.fixture
def adafruit_wiznet5k_with_ssl_socket_module():
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 = 1
sys.modules["adafruit_wiznet5k"] = wiznet5k_module
sys.modules["adafruit_wiznet5k.adafruit_wiznet5k_socket"] = wiznet5k_socket_module
yield
del sys.modules["adafruit_wiznet5k"]
del sys.modules["adafruit_wiznet5k.adafruit_wiznet5k_socket"]


@pytest.fixture(autouse=True)
def reset_connection_manager(monkeypatch):
monkeypatch.setattr(
Expand Down
15 changes: 14 additions & 1 deletion tests/ssl_context_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
""" SLL Context Tests """

import ssl
from unittest import mock

import mocket
import pytest

import adafruit_connection_manager
from adafruit_connection_manager import WIZNET5K_SSL_SUPPORT_VERSION


def test_connect_esp32spi_https( # pylint: disable=unused-argument
Expand Down Expand Up @@ -50,7 +52,9 @@ def test_connect_wiznet5k_https_not_supported( # pylint: disable=unused-argumen
):
mock_pool = mocket.MocketPool()
radio = mocket.MockRadio.WIZNET5K()
ssl_context = adafruit_connection_manager.get_radio_ssl_context(radio)
old_version = (WIZNET5K_SSL_SUPPORT_VERSION[0] - 1, 0, 0)
with mock.patch("sys.implementation", (None, old_version)):
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
Expand All @@ -59,3 +63,12 @@ def test_connect_wiznet5k_https_not_supported( # pylint: disable=unused-argumen
mocket.MOCK_HOST_1, 443, "https:", ssl_context=ssl_context
)
assert "This radio does not support TLS/HTTPS" in str(context)


def test_connect_wiznet5k_https_supported( # pylint: disable=unused-argument
adafruit_wiznet5k_with_ssl_socket_module,
):
radio = mocket.MockRadio.WIZNET5K()
with mock.patch("sys.implementation", (None, WIZNET5K_SSL_SUPPORT_VERSION)):
ssl_context = adafruit_connection_manager.get_radio_ssl_context(radio)
assert isinstance(ssl_context, ssl.SSLContext)
Loading