Skip to content

Commit 5f2e875

Browse files
committed
Add integration and unit tests for DTLS functionality in CoAP and Golioth clients
1 parent dfea887 commit 5f2e875

3 files changed

Lines changed: 56 additions & 31 deletions

File tree

tests/modules/coap/test_dtls_integration.py renamed to modules/ribbit/coap/dtls_integration_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ def stop(self):
110110
if self.socket:
111111
self.socket.close()
112112

113+
# Run the test
113114
if __name__ == "__main__":
114115
# Run the client test
115116
asyncio.run(test_coap_dtls_connection())
Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
2-
Test the TLS module usage in CoAP and Golioth clients.
2+
Test the TLS module usage in CoAP clients.
33
4-
This test verifies that the CoAP and Golioth modules correctly use
4+
This test verifies that the CoAP module correctly uses
55
the TLS module for DTLS functionality after the upstream MicroPython
66
PR #15764 moved DTLS from the ssl module to the tls module.
77
"""
@@ -14,7 +14,6 @@
1414
# Import modules we need to test
1515
import tls
1616
from ribbit.coap import Coap
17-
import ribbit.golioth as golioth
1817

1918
class DummySocket(io.IOBase):
2019
def __init__(self):
@@ -91,33 +90,8 @@ def test_coap_uses_tls_module(self, mock_socket, mock_ssl_context):
9190
mock_context.wrap_socket.assert_called()
9291

9392

94-
@patch('tls.SSLContext')
95-
def test_golioth_uses_tls_module(self, mock_ssl_context):
96-
"""Test that Golioth client uses the TLS module for DTLS."""
97-
# Setup mock objects
98-
mock_context = MagicMock()
99-
mock_ssl_context.return_value = mock_context
100-
101-
# Create config mock
102-
mock_config = MagicMock()
103-
mock_watch = MagicMock()
104-
mock_config.watch.return_value = mock_watch
105-
mock_watch.__enter__.return_value = mock_watch
106-
mock_watch.get.return_value = (True, "example.org", 5684, "user", "password", True)
107-
108-
# Create OTA manager mock
109-
mock_ota_manager = MagicMock()
110-
111-
# Create a Golioth client
112-
client = golioth.Golioth(config=mock_config, ota_manager=mock_ota_manager)
113-
114-
# Verify that the TLS SSLContext was created with DTLS client mode
115-
mock_ssl_context.assert_called_with(tls.PROTOCOL_DTLS_CLIENT)
116-
117-
# Verify PSK was set
118-
mock_context.set_ciphers.assert_called_with(["TLS-PSK-WITH-AES-128-CBC-SHA256"])
119-
mock_context.set_psk.assert_called_with("user", "password")
120-
93+
def run_tests():
94+
unittest.main()
12195

12296
if __name__ == '__main__':
123-
unittest.main()
97+
run_tests()
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
Test the TLS module usage in Golioth client.
3+
4+
This test verifies that the Golioth client correctly uses
5+
the TLS module for DTLS functionality after the upstream MicroPython
6+
PR #15764 moved DTLS from the ssl module to the tls module.
7+
"""
8+
9+
import unittest
10+
from unittest.mock import patch, MagicMock
11+
12+
# Import modules we need to test
13+
import tls
14+
import ribbit.golioth as golioth
15+
16+
class TestGoliothDTLS(unittest.TestCase):
17+
18+
@patch('tls.SSLContext')
19+
def test_golioth_uses_tls_module(self, mock_ssl_context):
20+
"""Test that Golioth client uses the TLS module for DTLS."""
21+
# Setup mock objects
22+
mock_context = MagicMock()
23+
mock_ssl_context.return_value = mock_context
24+
25+
# Create config mock
26+
mock_config = MagicMock()
27+
mock_watch = MagicMock()
28+
mock_config.watch.return_value = mock_watch
29+
mock_watch.__enter__.return_value = mock_watch
30+
mock_watch.get.return_value = (True, "example.org", 5684, "user", "password", True)
31+
32+
# Create OTA manager mock
33+
mock_ota_manager = MagicMock()
34+
35+
# Create a Golioth client
36+
client = golioth.Golioth(config=mock_config, ota_manager=mock_ota_manager)
37+
38+
# Verify that the TLS SSLContext was created with DTLS client mode
39+
mock_ssl_context.assert_called_with(tls.PROTOCOL_DTLS_CLIENT)
40+
41+
# Verify PSK was set
42+
mock_context.set_ciphers.assert_called_with(["TLS-PSK-WITH-AES-128-CBC-SHA256"])
43+
mock_context.set_psk.assert_called_with("user", "password")
44+
45+
46+
def run_tests():
47+
unittest.main()
48+
49+
if __name__ == '__main__':
50+
run_tests()

0 commit comments

Comments
 (0)