|
1 | | -#! /usr/bin/env python |
| 1 | +#! /usr/bin/env python3 |
2 | 2 | # -*- coding: utf-8 -*- |
3 | 3 |
|
4 | | -# Python Test Repo Template |
| 4 | +# Python Multicast Repo |
5 | 5 | # .................................. |
6 | 6 | # Copyright (c) 2017-2024, Mr. Walls |
7 | 7 | # .................................. |
8 | 8 | # Licensed under MIT (the "License"); |
9 | 9 | # you may not use this file except in compliance with the License. |
10 | 10 | # You may obtain a copy of the License at |
11 | 11 | # .......................................... |
12 | | -# http://www.github.com/reactive-firewall/python-repo/LICENSE.md |
| 12 | +# https://www.github.com/reactive-firewall/multicast/LICENSE.md |
13 | 13 | # .......................................... |
14 | 14 | # Unless required by applicable law or agreed to in writing, software |
15 | 15 | # distributed under the License is distributed on an "AS IS" BASIS, |
|
20 | 20 | __module__ = """tests""" |
21 | 21 |
|
22 | 22 | try: |
| 23 | + """Handle imports with CWE-758 mitigation. |
| 24 | +
|
| 25 | + This implementation uses a nested try-except pattern to: |
| 26 | + 1. Attempt direct context import |
| 27 | + 2. Fallback to relative import |
| 28 | + 3. Validate context module integrity |
| 29 | + 4. Import required dependencies |
| 30 | +
|
| 31 | + References: |
| 32 | + - CWE-758: Reliance on Undefined, Unspecified, or Implementation-Defined Behavior |
| 33 | + """ |
23 | 34 | try: |
24 | 35 | import context |
25 | | - except Exception as ImportErr: # pragma: no branch |
26 | | - ImportErr = None |
27 | | - del ImportErr # skipcq - cleanup any error leaks early |
| 36 | + except ImportError as _: # pragma: no branch |
| 37 | + del _ # skipcq - cleanup any error vars early |
28 | 38 | from . import context |
29 | 39 | if context.__name__ is None: |
30 | | - raise ImportError("[CWE-758] Failed to import context") from None |
| 40 | + raise ModuleNotFoundError("[CWE-758] Failed to import context") from None |
31 | 41 | else: |
32 | | - from context import multicast |
| 42 | + from context import multicast # pylint: disable=cyclic-import - skipcq: PYL-R0401 |
33 | 43 | from context import unittest |
34 | 44 | from context import BasicUsageTestSuite |
35 | 45 | except Exception as _cause: # pragma: no branch |
@@ -82,6 +92,52 @@ def test_command_execution_error_with_cause(self): |
82 | 92 | self.assertEqual(error.message, "Test with cause") |
83 | 93 | self.assertEqual(error.exit_code, 77) |
84 | 94 |
|
| 95 | + def test_shutdown_received_error_with_args(self): |
| 96 | + """ |
| 97 | + Test ShutdownCommandReceived initialization with custom message and exit code. |
| 98 | +
|
| 99 | + Verifies that both the message and exit code are correctly assigned when |
| 100 | + explicitly provided during initialization. |
| 101 | + """ |
| 102 | + error = multicast.exceptions.ShutdownCommandReceived("Test Shutdown", 42) |
| 103 | + self.assertEqual(error.message, "Test Shutdown") |
| 104 | + self.assertIsNotEqual(error.exit_code, 42, "Unexpectedly was able to overide exit code!") |
| 105 | + self.assertEqual(error.exit_code, 143) |
| 106 | + |
| 107 | + def test_shutdown_received_error_default_exit_code(self): |
| 108 | + """Test ShutdownCommandReceived initialization with default exit code. |
| 109 | +
|
| 110 | + Verifies that the exit code defaults to 143 when only a message is provided. |
| 111 | + """ |
| 112 | + error = multicast.exceptions.ShutdownCommandReceived("Test Shutdown") |
| 113 | + self.assertEqual(error.exit_code, 143) |
| 114 | + |
| 115 | + def test_shutdown_received_error_GIVEN_no_args(self): |
| 116 | + """Test ShutdownCommandReceived initialization with default exit code. |
| 117 | +
|
| 118 | + Verifies that the exit code defaults to 143 when no arguments are provided. |
| 119 | + Verifies that the message defaults to 'SHUTDOWN' when no arguments are provided. |
| 120 | + """ |
| 121 | + error = multicast.exceptions.ShutdownCommandReceived() |
| 122 | + self.assertEqual(error.message, "SHUTDOWN") |
| 123 | + self.assertEqual(error.exit_code, 143) |
| 124 | + |
| 125 | + def test_shutdown_received_error_with_cause(self): |
| 126 | + """Test ShutdownCommandReceived initialization with a cause. |
| 127 | +
|
| 128 | + Verifies that the error properly chains exceptions when initialized with a |
| 129 | + cause, maintaining both the cause reference and custom attributes. |
| 130 | + """ |
| 131 | + test_cause = RuntimeError("test") |
| 132 | + self.assertIsNotNone(test_cause) |
| 133 | + error = multicast.exceptions.ShutdownCommandReceived(test_cause, "Shutdown with cause", 77) |
| 134 | + self.assertIsNotNone(error) |
| 135 | + self.assertIsNotNone(error.__cause__) |
| 136 | + self.assertEqual(error.__cause__, test_cause) |
| 137 | + self.assertEqual(error.message, "Shutdown with cause") |
| 138 | + self.assertIsNotEqual(error.exit_code, 77, "Unexpectedly was able to overide exit code!") |
| 139 | + self.assertEqual(error.exit_code, 143) |
| 140 | + |
85 | 141 |
|
86 | 142 | # leave this part |
87 | 143 | if __name__ == '__main__': |
|
0 commit comments