|
| 1 | +#! /usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +# Python Test Repo Template |
| 5 | +# .................................. |
| 6 | +# Copyright (c) 2017-2024, Mr. Walls |
| 7 | +# .................................. |
| 8 | +# Licensed under MIT (the "License"); |
| 9 | +# you may not use this file except in compliance with the License. |
| 10 | +# You may obtain a copy of the License at |
| 11 | +# .......................................... |
| 12 | +# http://www.github.com/reactive-firewall/python-repo/LICENSE.md |
| 13 | +# .......................................... |
| 14 | +# Unless required by applicable law or agreed to in writing, software |
| 15 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | +# See the License for the specific language governing permissions and |
| 18 | +# limitations under the License. |
| 19 | + |
| 20 | +__module__ = """tests""" |
| 21 | + |
| 22 | + |
| 23 | +try: |
| 24 | + try: |
| 25 | + import context |
| 26 | + except Exception as _: # pragma: no branch |
| 27 | + del _ # skipcq - cleanup any error vars early |
| 28 | + from . import context |
| 29 | + if context.__name__ is None: |
| 30 | + raise ModuleNotFoundError("[CWE-758] Failed to import context") from None |
| 31 | + else: |
| 32 | + import socket |
| 33 | + from context import multicast # pylint: disable=cyclic-import - skipcq: PYL-R0401 |
| 34 | + from context import unittest |
| 35 | +except Exception as err: |
| 36 | + raise ImportError("[CWE-758] Failed to import test context") from err |
| 37 | + |
| 38 | + |
| 39 | +class McastHearTestSuite(context.BasicUsageTestSuite): |
| 40 | + |
| 41 | + __module__ = """tests.test_hear_server""" |
| 42 | + |
| 43 | + __name__ = """tests.test_hear_server.McastHearTestSuite""" |
| 44 | + |
| 45 | + @staticmethod |
| 46 | + def get_default_ip(): |
| 47 | + """Get the default IP address of the machine. |
| 48 | +
|
| 49 | + Returns: |
| 50 | + str: The IP address of the default network interface. |
| 51 | +
|
| 52 | + Note: |
| 53 | + Uses 203.0.113.1 (TEST-NET-3) for RFC 5737 compliance. |
| 54 | + Port 59095 is chosen as an arbitrary high port number. |
| 55 | + """ |
| 56 | + try: |
| 57 | + # Create a socket connection to an external address |
| 58 | + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 59 | + # Connect to a public non-routable IP |
| 60 | + s.connect(("203.0.113.1", 59095)) |
| 61 | + # Get the IP address of the default interface |
| 62 | + ip = s.getsockname()[0] |
| 63 | + except socket.error as e: |
| 64 | + raise multicast.exceptions.CommandExecutionError("Failed to determine IP", 69) from e |
| 65 | + finally: |
| 66 | + s.close() |
| 67 | + return ip |
| 68 | + |
| 69 | + |
| 70 | +class McastServerTestSuite(McastHearTestSuite): |
| 71 | + |
| 72 | + __module__ = """tests.test_hear_server""" |
| 73 | + |
| 74 | + __name__ = """tests.test_hear_server.McastServerTestSuite""" |
| 75 | + |
| 76 | + def test_handle_error_without_stop_in_request(self): |
| 77 | + """ |
| 78 | + Test McastServer.handle_error with a non-STOP request. |
| 79 | +
|
| 80 | + Verifies that the server properly handles requests without |
| 81 | + the STOP command and cleans up resources. |
| 82 | + """ |
| 83 | + theResult = False |
| 84 | + fail_fixture = str("""Mock(BLAH) --> Handler-HEAR == error""") |
| 85 | + _fixture_port_num = self._the_test_port |
| 86 | + try: |
| 87 | + self.assertIsNotNone(_fixture_port_num) |
| 88 | + self.assertIsInstance(_fixture_port_num, int) |
| 89 | + # Create an instance of McastServer |
| 90 | + server_address = ('224.0.0.1', _fixture_port_num) |
| 91 | + server = multicast.hear.McastServer(server_address, multicast.hear.HearUDPHandler) |
| 92 | + client_address = (self.get_default_ip(), _fixture_port_num) |
| 93 | + # Mock a request not containing "STOP" |
| 94 | + request = (str("Regular message"), multicast.genSocket()) |
| 95 | + try: |
| 96 | + server.handle_error(request, client_address) |
| 97 | + finally: |
| 98 | + # Clean up |
| 99 | + server.server_close() |
| 100 | + theResult = (multicast.endSocket(request[1]) is None) |
| 101 | + self.assertTrue(theResult, "RESOURCE LEAK") |
| 102 | + except Exception as err: |
| 103 | + context.debugtestError(err) |
| 104 | + self.fail(fail_fixture) |
| 105 | + self.assertTrue(theResult, fail_fixture) |
| 106 | + |
| 107 | + def test_handle_error_with_none_request(self): |
| 108 | + theResult = False |
| 109 | + fail_fixture = str("""Mock(EMPTY) --X Handler-HEAR != Safe""") |
| 110 | + _fixture_port_num = self._the_test_port |
| 111 | + try: |
| 112 | + self.assertIsNotNone(_fixture_port_num) |
| 113 | + self.assertIsInstance(_fixture_port_num, int) |
| 114 | + # Create an instance of McastServer |
| 115 | + server_address = ('224.0.0.1', _fixture_port_num) |
| 116 | + server = multicast.hear.McastServer(server_address, multicast.hear.HearUDPHandler) |
| 117 | + client_address = (self.get_default_ip(), _fixture_port_num) |
| 118 | + # Mock None as a request |
| 119 | + request = None |
| 120 | + self.assertIsNone(request, "RESOURCE LEAK") |
| 121 | + try: |
| 122 | + server.handle_error(request, client_address) |
| 123 | + finally: |
| 124 | + # Clean up |
| 125 | + server.server_close() |
| 126 | + theResult = (request is None) |
| 127 | + except Exception as err: |
| 128 | + context.debugtestError(err) |
| 129 | + self.fail(fail_fixture) |
| 130 | + self.assertTrue(theResult, fail_fixture) |
| 131 | + |
| 132 | + |
| 133 | +class HearUDPHandlerTestSuite(McastHearTestSuite): |
| 134 | + |
| 135 | + __module__ = """tests.test_hear_server""" |
| 136 | + |
| 137 | + __name__ = """tests.test_hear_server.HearUDPHandlerTestSuite""" |
| 138 | + |
| 139 | + def test_handle_with_none_data_and_sock(self): |
| 140 | + fail_fixture = str("""Handler(None, None) --> HEAR == error""") |
| 141 | + _fixture_port_num = self._the_test_port |
| 142 | + self.assertIsNotNone(_fixture_port_num) |
| 143 | + self.assertIsInstance(_fixture_port_num, int) |
| 144 | + handler = multicast.hear.HearUDPHandler( |
| 145 | + request=(None, None), |
| 146 | + client_address=(self.get_default_ip(), _fixture_port_num), |
| 147 | + server=None |
| 148 | + ) |
| 149 | + # Should return early without processing |
| 150 | + result = handler.handle() |
| 151 | + self.assertIsNone(result, fail_fixture) |
| 152 | + |
| 153 | + def test_handle_with_data_none_sock(self): |
| 154 | + fail_fixture = str("""Handler(None, None) --> HEAR == error""") |
| 155 | + _fixture_port_num = self._the_test_port |
| 156 | + self.assertIsNotNone(_fixture_port_num) |
| 157 | + self.assertIsInstance(_fixture_port_num, int) |
| 158 | + handler = multicast.hear.HearUDPHandler( |
| 159 | + request=(b"No-Op", None), |
| 160 | + client_address=(self.get_default_ip(), _fixture_port_num), |
| 161 | + server=None |
| 162 | + ) |
| 163 | + # Should return early without processing |
| 164 | + result = handler.handle() |
| 165 | + self.assertIsNone(result, fail_fixture) |
| 166 | + |
| 167 | + def test_handle_with_valid_data_and_sock(self): |
| 168 | + sock = multicast.genSocket() |
| 169 | + fail_fixture = str("""Handler("The Test", sock) --> HEAR == error""") |
| 170 | + _fixture_port_num = self._the_test_port |
| 171 | + try: |
| 172 | + self.assertIsNotNone(_fixture_port_num) |
| 173 | + self.assertIsInstance(_fixture_port_num, int) |
| 174 | + handler = multicast.hear.HearUDPHandler( |
| 175 | + request=(b"The Test", sock), |
| 176 | + client_address=(self.get_default_ip(), _fixture_port_num), |
| 177 | + server=None |
| 178 | + ) |
| 179 | + # Should process the message |
| 180 | + result = handler.handle() |
| 181 | + # Clean up socket |
| 182 | + self.assertIsNone(multicast.endSocket(sock), "RESOURCE LEAK") |
| 183 | + except Exception as err: |
| 184 | + context.debugtestError(err) |
| 185 | + self.fail(fail_fixture) |
| 186 | + self.assertIsNone(result, fail_fixture) |
| 187 | + |
| 188 | + |
| 189 | +if __name__ == '__main__': |
| 190 | + unittest.main() |
0 commit comments