Skip to content

Commit

Permalink
Fix for #36: default SSL context instead of preconfigured SSL context…
Browse files Browse the repository at this point in the history
… is used (#38)

Fixed typing error on SSLContext isinstance
  • Loading branch information
sfaiss authored Aug 27, 2023
1 parent f278a59 commit a42d1dc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion doipclient/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ def _connect(self):
self._udp_sock.bind((self._client_ip_address, 0))

if self._use_secure:
if isinstance(self._use_secure, type(ssl.SSLContext)):
if isinstance(self._use_secure, ssl.SSLContext):
ssl_context = self._use_secure
else:
ssl_context = ssl.create_default_context()
Expand Down
23 changes: 17 additions & 6 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,18 +695,29 @@ def test_exception_from_blocking_ssl_socket(mock_socket, mocker):

def test_use_secure_uses_default_ssl_context(mock_socket, mocker):
"""Wrap socket with default SSL-context when use_secure=True"""
mocked_context = mocker.patch.object(ssl, "SSLContext", autospec=True)
mocked_default_context = mocker.patch.object(ssl, "create_default_context", autospec=True)
sut = DoIPClient(
test_ip, test_logical_address, use_secure=True, activation_type=None
)
mocked_wrap_socket = mocked_context.return_value.wrap_socket
mocked_wrap_socket.assert_called_once_with(mock_socket)
mocked_default_wrap_socket = mocked_default_context.return_value.wrap_socket
mocked_default_wrap_socket.assert_called_once_with(mock_socket)


def test_use_secure_with_external_ssl_context(mock_socket, mocker):
"""Wrap socket with user provided SSL-context when use_secure=ssl_context"""
mocked_context = mocker.patch.object(ssl, "SSLContext", autospec=True)
original_context = ssl.SSLContext
mocked_external_context = mocker.patch.object(ssl, "SSLContext", autospec=True)
mocked_default_context = mocker.patch.object(ssl, "create_default_context", autospec=True)

# Unmock the SSLContext
ssl.SSLContext = original_context

sut = DoIPClient(
test_ip, test_logical_address, use_secure=mocked_context, activation_type=None
test_ip, test_logical_address, use_secure=mocked_external_context, activation_type=None
)
mocked_context.wrap_socket.assert_called_once_with(mock_socket)

mocked_default_wrap_socket = mocked_default_context.return_value.wrap_socket
assert not mocked_default_wrap_socket.called, "Socket should *not* get wrapped using default context."

mocked_external_wrap_socket = mocked_external_context.wrap_socket
mocked_external_wrap_socket.assert_called_once_with(mock_socket)

0 comments on commit a42d1dc

Please sign in to comment.