Skip to content

[TESTING] Improved testing of HEAR handler slightly #350

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

Merged
Merged
Changes from all 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
31 changes: 23 additions & 8 deletions tests/test_hear_data_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@
@context.markWithMetaTag("mat", "hear")
class RecvDataProcessingTestSuite(context.BasicUsageTestSuite):
"""
A test suite that checks empty data with the multicast sender and receiver.
A test suite that validates the multicast sender and receiver's handling of empty data.

Test cases:
- Sending empty binary data.
- Sending empty data followed by a stop command.
"""

__module__ = "tests.test_hear_data_processing"
Expand All @@ -55,16 +58,18 @@ def test_multicast_sender_with_no_data(self) -> None:
theResult = False
fail_fixture = "SAY -X] RECV? != error"
_fixture_port_num = self._always_generate_random_port_WHEN_called()
_fixture_mcast_addr = "224.0.0.1"
try:
self.assertIsNotNone(_fixture_port_num)
self.assertIsInstance(_fixture_port_num, int)
self.assertIsNotNone(_fixture_mcast_addr)
_fixture_HEAR_args = [
"--port",
str(_fixture_port_num),
"--groups",
"'224.0.0.1'",
f"'{_fixture_mcast_addr}'",
"--group",
"'224.0.0.1'",
f"'{_fixture_mcast_addr}'",
]
p = Process(
target=multicast.__main__.main, name="RECV", args=(
Expand All @@ -77,7 +82,7 @@ def test_multicast_sender_with_no_data(self) -> None:
try:
sender = multicast.send.McastSAY()
self.assertIsNotNone(sender)
sender(group='224.0.0.1', port=_fixture_port_num, ttl=1, data=b'')
sender(group=_fixture_mcast_addr, port=_fixture_port_num, ttl=1, data=b'')
self.assertIsNotNone(p)
self.assertTrue(p.is_alive(), fail_fixture)
except Exception as _cause:
Expand Down Expand Up @@ -105,16 +110,17 @@ def test_multicast_sender_with_no_data_before_follow_by_stop(self) -> None:
theResult = False
fail_fixture = "SAY -X] HEAR? != error"
_fixture_port_num = self._always_generate_random_port_WHEN_called()
_fixture_mcast_addr = "224.0.0.1"
try:
self.assertIsNotNone(_fixture_port_num)
self.assertIsInstance(_fixture_port_num, int)
_fixture_HEAR_args = [
"--port",
str(_fixture_port_num),
"--groups",
"'224.0.0.1'",
f"'{_fixture_mcast_addr}'",
"--group",
"'224.0.0.1'",
f"'{_fixture_mcast_addr}'",
]
p = Process(
target=multicast.__main__.main,
Expand All @@ -130,11 +136,11 @@ def test_multicast_sender_with_no_data_before_follow_by_stop(self) -> None:
try:
sender = multicast.send.McastSAY()
self.assertIsNotNone(sender)
sender(group='224.0.0.1', port=_fixture_port_num, ttl=1, data=b'')
sender(group=_fixture_mcast_addr, port=_fixture_port_num, ttl=1, data=b'')
self.assertIsNotNone(p)
self.assertTrue(p.is_alive(), fail_fixture)
while p.is_alive():
sender(group="224.0.0.1", port=_fixture_port_num, data=["STOP"])
sender(group=_fixture_mcast_addr, port=_fixture_port_num, data=["STOP"])
p.join(1)
self.assertFalse(p.is_alive(), "HEAR ignored STOP")
except Exception as _cause:
Expand Down Expand Up @@ -208,11 +214,20 @@ def test_handle_with_invalid_utf8_data(self) -> None:
request=(data, sock), client_address=_fixture_client_addr, server=None
)
try:
# Mock the processing method
handler._process = MagicMock()
# Should silently ignore invalid UTF-8 data
handler.handle() # If no exception is raised, the test passes
# Verify handler state after processing invalid data
self.assertIsNone(handler.server) # Server should remain None
self.assertEqual(handler.client_address, _fixture_client_addr)
# Verify no data was processed
handler._process.assert_not_called()
# Test with different invalid UTF-8 sequences
for invalid_data in [b'\xff', b'\xfe\xff', b'\xff\xff\xff']:
handler.request = (invalid_data, sock)
handler.handle()
handler._process.assert_not_called()
except Exception as e:
self.fail(f"Handler raised an unexpected exception: {e}")
finally:
Expand Down
Loading