[tor-commits] [stem/master] stem/util/tor_tools.py: Small optimizations to validators.

atagar at torproject.org atagar at torproject.org
Sun Nov 23 00:13:20 UTC 2014


commit 7cd62935b7f6d5dcf41fb2a82da8a13853fe809c
Author: Ossi Herrala <oherrala at gmail.com>
Date:   Fri Nov 21 02:00:42 2014 +0200

    stem/util/tor_tools.py: Small optimizations to validators.
    
     * Remove calls to isinstance() and trust the called functions to throw exception instead.
     * Add more unit tests for cases found while poking around.
---
 stem/util/tor_tools.py      |   35 +++++++++++++++++++++--------------
 test/unit/util/tor_tools.py |   30 ++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+), 14 deletions(-)

diff --git a/stem/util/tor_tools.py b/stem/util/tor_tools.py
index 9eb46a0..54579f6 100644
--- a/stem/util/tor_tools.py
+++ b/stem/util/tor_tools.py
@@ -51,15 +51,16 @@ def is_valid_fingerprint(entry, check_prefix = False):
   :returns: **True** if the string could be a relay fingerprint, **False** otherwise
   """
 
-  if not isinstance(entry, (str, unicode)):
-    return False
-  elif check_prefix:
-    if not entry or entry[0] != '$':
-      return False
+  try:
+    if check_prefix:
+      if not entry or entry[0] != '$':
+        return False
 
-    entry = entry[1:]
+      entry = entry[1:]
 
-  return bool(FINGERPRINT_PATTERN.match(entry))
+    return is_hex_digits(entry, 40)
+  except TypeError:
+    return False
 
 
 def is_valid_nickname(entry):
@@ -71,11 +72,11 @@ def is_valid_nickname(entry):
   :returns: **True** if the string could be a nickname, **False** otherwise
   """
 
-  if not isinstance(entry, (str, unicode)):
+  try:
+    return bool(NICKNAME_PATTERN.match(entry))
+  except TypeError:
     return False
 
-  return bool(NICKNAME_PATTERN.match(entry))
-
 
 def is_valid_circuit_id(entry):
   """
@@ -84,11 +85,11 @@ def is_valid_circuit_id(entry):
   :returns: **True** if the string could be a circuit id, **False** otherwise
   """
 
-  if not isinstance(entry, (str, unicode)):
+  try:
+    return bool(CIRC_ID_PATTERN.match(entry))
+  except TypeError:
     return False
 
-  return bool(CIRC_ID_PATTERN.match(entry))
-
 
 def is_valid_stream_id(entry):
   """
@@ -123,4 +124,10 @@ def is_hex_digits(entry, count):
   :returns: **True** if the string matches this number
   """
 
-  return bool(re.match('^%s{%i}$' % (HEX_DIGIT, count), entry))
+  try:
+    if len(entry) != count:
+      return False
+    int(entry, 16)
+  except (ValueError, TypeError):
+    return False
+  return True
diff --git a/test/unit/util/tor_tools.py b/test/unit/util/tor_tools.py
index 6e3af86..80362f7 100644
--- a/test/unit/util/tor_tools.py
+++ b/test/unit/util/tor_tools.py
@@ -19,7 +19,10 @@ class TestTorTools(unittest.TestCase):
     )
 
     invalid_fingerprints = (
+      None,
       '',
+      5,
+      ['A7569A83B5706AB1B1A9CB52EFF7D2D32E4553EB'],
       'A7569A83B5706AB1B1A9CB52EFF7D2D32E4553EB',
       '$A7569A83B5706AB1B1A9CB52EFF7D2D32E4553E',
       '$A7569A83B5706AB1B1A9CB52EFF7D2D32E4553E33',
@@ -46,6 +49,7 @@ class TestTorTools(unittest.TestCase):
     invalid_nicknames = (
       None,
       '',
+      5,
       'toolongggggggggggggg',
       'bad_character',
     )
@@ -81,3 +85,29 @@ class TestTorTools(unittest.TestCase):
 
     for circuit_id in invalid_circuit_ids:
       self.assertFalse(stem.util.tor_tools.is_valid_circuit_id(circuit_id))
+
+  def test_is_valid_hex_digits(self):
+    """
+    Checks the is_valid_hex_digits function.
+    """
+
+    valid_hex_digits = (
+      "12345",
+      "AbCdE",
+    )
+
+    invalid_hex_digits = (
+      None,
+      '',
+      5,
+      'X',
+      "1234",
+      "ABCDEF",
+      [1, "2", (3, 4)]
+    )
+
+    for hex_digits in valid_hex_digits:
+      self.assertTrue(stem.util.tor_tools.is_hex_digits(hex_digits, 5))
+
+    for hex_digits in invalid_hex_digits:
+      self.assertFalse(stem.util.tor_tools.is_hex_digits(hex_digits, 5))





More information about the tor-commits mailing list