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 1 commit
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
24 changes: 22 additions & 2 deletions adafruit_connection_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,28 @@ 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
try_ssl = False
cp_version = sys.implementation[1]
if pool.SOCK_STREAM == 1 and cp_version[0] >= 9:
justmobilize marked this conversation as resolved.
Show resolved Hide resolved
if cp_version[0] > 9:
try_ssl = True
elif cp_version[0] == 9 and cp_version[1] >= 1:
try_ssl = True

if try_ssl:
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}")
Expand Down
14 changes: 14 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import sys

import mocket
import pytest


# pylint: disable=unused-argument
Expand All @@ -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",
{},
)
5 changes: 4 additions & 1 deletion tests/fake_ssl_context_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

""" FakeSLLSocket Tests """

from unittest import mock

import mocket
import pytest

Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions tests/get_radio_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
""" Get socketpool and ssl_context Tests """

import ssl
from unittest import mock

import mocket
import pytest
Expand All @@ -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"


Expand Down Expand Up @@ -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)


Expand Down
Loading