[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Implementing and testing is_valid_ip_address()



commit 31cd9b9aa5ea0b5139d3d32ee3a424d9af1e26ca
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sat Mar 17 17:07:52 2012 -0700

    Implementing and testing is_valid_ip_address()
    
    Moving over an arm function for testing if a string is a valid IPv4 address.
    Also throwing in some unit tests and a fix for entries with leading zeros (for
    instance "1.2.3.01").
---
 run_tests.py                 |    2 ++
 stem/util/__init__.py        |    2 +-
 stem/util/connection.py      |   31 +++++++++++++++++++++++++++++++
 test/unit/util/__init__.py   |    2 +-
 test/unit/util/connection.py |   25 +++++++++++++++++++++++++
 5 files changed, 60 insertions(+), 2 deletions(-)

diff --git a/run_tests.py b/run_tests.py
index 60118af..4918881 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -19,6 +19,7 @@ import test.unit.socket.control_line
 import test.unit.socket.control_message
 import test.unit.descriptor.reader
 import test.unit.util.conf
+import test.unit.util.connection
 import test.unit.util.enum
 import test.unit.util.system
 import test.unit.version
@@ -80,6 +81,7 @@ ERROR_ATTR = (term.Color.RED, term.Attr.BOLD)
 
 UNIT_TESTS = (
   test.unit.util.enum.TestEnum,
+  test.unit.util.connection.TestConnection,
   test.unit.util.conf.TestConf,
   test.unit.util.system.TestSystem,
   test.unit.descriptor.reader.TestDescriptorReader,
diff --git a/stem/util/__init__.py b/stem/util/__init__.py
index 2e6b479..bbf4df7 100644
--- a/stem/util/__init__.py
+++ b/stem/util/__init__.py
@@ -2,5 +2,5 @@
 Utility functions used by the stem library.
 """
 
-__all__ = ["conf", "enum", "log", "proc", "system", "term"]
+__all__ = ["conf", "connection", "enum", "log", "proc", "system", "term"]
 
diff --git a/stem/util/connection.py b/stem/util/connection.py
new file mode 100644
index 0000000..6505637
--- /dev/null
+++ b/stem/util/connection.py
@@ -0,0 +1,31 @@
+"""
+Connection and networking based utility functions. This will likely be expanded
+later to have all of arm's functions...
+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):
+  """
+  Checks if a string is a valid IPv4 address.
+  
+  Arguments:
+    address (str) - string to be checked
+  
+  Returns:
+    True if input is a valid IPv4 address, false otherwise.
+  """
+  
+  # checks if theres four period separated values
+  if not address.count(".") == 3: return False
+  
+  # checks that each value in the octet are decimal values between 0-255
+  for entry in address.split("."):
+    if not entry.isdigit() or int(entry) < 0 or int(entry) > 255:
+      return False
+    elif entry[0] == "0" and len(entry) > 1:
+      return False # leading zeros, for instance in "1.2.3.001"
+  
+  return True
+
diff --git a/test/unit/util/__init__.py b/test/unit/util/__init__.py
index 7c8d12b..74b758a 100644
--- a/test/unit/util/__init__.py
+++ b/test/unit/util/__init__.py
@@ -2,5 +2,5 @@
 Unit tests for stem.util.* contents.
 """
 
-__all__ = ["conf", "enum", "system"]
+__all__ = ["conf", "connection", "enum", "system"]
 
diff --git a/test/unit/util/connection.py b/test/unit/util/connection.py
new file mode 100644
index 0000000..58cedf4
--- /dev/null
+++ b/test/unit/util/connection.py
@@ -0,0 +1,25 @@
+"""
+Unit tests for the stem.util.connection functions.
+"""
+
+import unittest
+import stem.util.connection
+
+class TestConnection(unittest.TestCase):
+  def test_is_valid_ip_address(self):
+    """
+    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"))
+    
+    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"))
+



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits