From 5fc4d52d9ff27fb0f6a8453b976125d0b516e42a Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Fri, 30 Sep 2022 09:12:25 -0400 Subject: [PATCH] Common: Allow 0 for NetworkPort While TCP port 0 is reserved and you're not supposed to use it, it is a valid port. --- monkey/common/types.py | 2 +- .../unit_tests/common/test_socket_address.py | 17 +++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/monkey/common/types.py b/monkey/common/types.py index 89794b942..8c5048a48 100644 --- a/monkey/common/types.py +++ b/monkey/common/types.py @@ -26,7 +26,7 @@ class NetworkPort(ConstrainedInt): port: NetworkPort = typing.cast(NetworkPort, 1000) """ - ge = 1 + ge = 0 le = 65535 diff --git a/monkey/tests/unit_tests/common/test_socket_address.py b/monkey/tests/unit_tests/common/test_socket_address.py index 7d8b20e3f..a521aca62 100644 --- a/monkey/tests/unit_tests/common/test_socket_address.py +++ b/monkey/tests/unit_tests/common/test_socket_address.py @@ -4,14 +4,15 @@ from common.types import SocketAddress GOOD_IP = "192.168.1.1" BAD_IP = "192.168.1.999" -GOOD_PORT = 1234 -BAD_PORTS = [99999, 0, -1] +GOOD_PORTS = [0, 1, 1234, 65535] +BAD_PORTS = [99999, 65536, -1] -def test_socket_address__from_string(): - expected = SocketAddress(ip=GOOD_IP, port=GOOD_PORT) +@pytest.mark.parametrize("good_port", GOOD_PORTS) +def test_socket_address__from_string(good_port): + expected = SocketAddress(ip=GOOD_IP, port=good_port) - address = SocketAddress.from_string(f"{GOOD_IP}:{GOOD_PORT}") + address = SocketAddress.from_string(f"{GOOD_IP}:{good_port}") assert address == expected @@ -22,10 +23,10 @@ def test_socket_address__from_string(): "not an address", ":", GOOD_IP, - str(GOOD_PORT), f"{GOOD_IP}:", - f":{GOOD_PORT}", - f"{BAD_IP}:{GOOD_PORT}", + "25", + ":22", + *[f"{BAD_IP}:{p}" for p in GOOD_PORTS], *[f"{GOOD_IP}:{bad_port}" for bad_port in BAD_PORTS], ], )