[stem/master] Function to check validity of a port

commit 8fe000d4de00a073fba060b39f42a8fa4a423077 Author: Damian Johnson <atagar@torproject.org> Date: Sat Mar 17 18:20:30 2012 -0700 Function to check validity of a port --- stem/util/connection.py | 35 +++++++++++++++++++++++++++---- test/unit/util/connection.py | 46 ++++++++++++++++++++++++++++++++--------- 2 files changed, 66 insertions(+), 15 deletions(-) diff --git a/stem/util/connection.py b/stem/util/connection.py index 6505637..9c6ba8b 100644 --- a/stem/util/connection.py +++ b/stem/util/connection.py @@ -6,22 +6,22 @@ https://gitweb.torproject.org/arm.git/blob/HEAD:/src/util/connections.py but for now just moving the parts we need. """ -def is_valid_ip_address(address): +def is_valid_ip_address(entry): """ Checks if a string is a valid IPv4 address. Arguments: - address (str) - string to be checked + entry (str) - string to be checked Returns: - True if input is a valid IPv4 address, false otherwise. + True if input is a valid IPv4 address, False otherwise. """ # checks if theres four period separated values - if not address.count(".") == 3: return False + if not entry.count(".") == 3: return False # checks that each value in the octet are decimal values between 0-255 - for entry in address.split("."): + for entry in entry.split("."): if not entry.isdigit() or int(entry) < 0 or int(entry) > 255: return False elif entry[0] == "0" and len(entry) > 1: @@ -29,3 +29,28 @@ def is_valid_ip_address(address): return True +def is_valid_port(entry, allow_zero = False): + """ + Checks if a string or int is a valid port number. + + Arguments: + entry (str or int) - string or integer to be checked + allow_zero (bool) - accept port number of zero (reserved by defintion) + + Returns: + True if input is an integer and within the valid port range, False + otherwise. + """ + + if isinstance(entry, str): + if not entry.isdigit(): + return False + elif entry[0] == "0" and len(entry) > 1: + return False # leading zeros, ex "001" + + entry = int(entry) + + if allow_zero and entry == 0: return True + + return entry > 0 and entry < 65536 + diff --git a/test/unit/util/connection.py b/test/unit/util/connection.py index 58cedf4..01d1c92 100644 --- a/test/unit/util/connection.py +++ b/test/unit/util/connection.py @@ -11,15 +11,41 @@ class TestConnection(unittest.TestCase): Checks the is_valid_ip_address function. """ - self.assertTrue(stem.util.connection.is_valid_ip_address("0.0.0.0")) - self.assertTrue(stem.util.connection.is_valid_ip_address("1.2.3.4")) - self.assertTrue(stem.util.connection.is_valid_ip_address("192.168.0.1")) - self.assertTrue(stem.util.connection.is_valid_ip_address("255.255.255.255")) + valid_addresses = ( + "0.0.0.0", + "1.2.3.4", + "192.168.0.1", + "255.255.255.255", + ) - self.assertFalse(stem.util.connection.is_valid_ip_address("0.0.00.0")) - self.assertFalse(stem.util.connection.is_valid_ip_address("0.0.0")) - self.assertFalse(stem.util.connection.is_valid_ip_address("1.2.3.256")) - self.assertFalse(stem.util.connection.is_valid_ip_address("1.2.3.-1")) - self.assertFalse(stem.util.connection.is_valid_ip_address("0.0.0.a")) - self.assertFalse(stem.util.connection.is_valid_ip_address("a.b.c.d")) + invalid_addresses = ( + "0.0.00.0", + "0.0.0", + "1.2.3.256", + "1.2.3.-1", + "0.0.0.a", + "a.b.c.d", + ) + + for address in valid_addresses: + self.assertTrue(stem.util.connection.is_valid_ip_address(address)) + for address in invalid_addresses: + self.assertFalse(stem.util.connection.is_valid_ip_address(address)) + + def test_is_valid_port(self): + """ + Checks the is_valid_port function. + """ + + valid_ports = (1, "1", 1234, "1234", 65535, "65535") + invalid_ports = (0, "0", 65536, "65536", "abc", "*", " 15", "01") + + for port in valid_ports: + self.assertTrue(stem.util.connection.is_valid_port(port)) + + for port in invalid_ports: + self.assertFalse(stem.util.connection.is_valid_port(port)) + + self.assertTrue(stem.util.connection.is_valid_port(0, allow_zero = True)) + self.assertTrue(stem.util.connection.is_valid_port("0", allow_zero = True))
participants (1)
-
atagar@torproject.org