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

[tor-commits] [bridgedb/master] Deprecate bridgedb.Bridges.is_valid_fingerprint().



commit b4a78c72dc74158b2e2f62f8fd38915cea07add0
Author: Isis Lovecruft <isis@xxxxxxxxxxxxxx>
Date:   Tue Jun 10 21:59:31 2014 +0000

    Deprecate bridgedb.Bridges.is_valid_fingerprint().
    
     * MOVE bridgedb.Bridges.is_valid_fingerprint() to
       bridgedb.test.deprecated module for regression testing.
     * REMOVE bridgedb.Bridges.toHex().
     * REMOVE bridgedb.Bridges.fromHex().
     * REMOVE bridgedb.Bridges.HEX_FP_LEN.
     * ADD bridgedb.parse.fingerprint module. This now contain equivalent
       functionality to the above removed/moved objects.
     * ADD monkeypatcher code for all the above removed/moved code to
       bridgedb.test.test_Tests suite for regression testing.
---
 lib/bridgedb/Bridges.py           |   29 ++++++------------------
 lib/bridgedb/parse/fingerprint.py |   44 +++++++++++++++++++++++++++++++++++++
 lib/bridgedb/test/deprecated.py   |   33 ++++++++++++++++++++++++++++
 lib/bridgedb/test/test_Tests.py   |    6 +++++
 4 files changed, 90 insertions(+), 22 deletions(-)

diff --git a/lib/bridgedb/Bridges.py b/lib/bridgedb/Bridges.py
index 2439130..5b5e678 100644
--- a/lib/bridgedb/Bridges.py
+++ b/lib/bridgedb/Bridges.py
@@ -10,7 +10,6 @@
 them into hashrings for distributors.
 """
 
-import binascii
 import bisect
 import logging
 import re
@@ -26,6 +25,9 @@ import bridgedb.Bucket
 from bridgedb.crypto import getHMACFunc
 from bridgedb.parse import addr
 from bridgedb.parse import networkstatus
+from bridgedb.parse.fingerprint import toHex
+from bridgedb.parse.fingerprint import fromHex
+from bridgedb.parse.fingerprint import isValidFingerprint
 from bridgedb.safelog import logSafely
 
 try:
@@ -34,7 +36,6 @@ except ImportError:
     from io import StringIO
 
 
-HEX_FP_LEN = 40
 ID_LEN = 20
 HEX_DIGEST_LEN = 40
 DIGEST_LEN = 20
@@ -73,22 +74,6 @@ def is_valid_ip(ip):
         return False
     return True
 
-def is_valid_fingerprint(fp):
-    """Return true iff fp in the right format to be a hex fingerprint
-       of a Tor server.
-    """
-    if len(fp) != HEX_FP_LEN:
-        return False
-    try:
-        fromHex(fp)
-    except TypeError:
-        return False
-    else:
-        return True
-
-toHex = binascii.b2a_hex
-fromHex = binascii.a2b_hex
-
 
 class Bridge(object):
     """Holds information for a single bridge, along with any Pluggable
@@ -142,7 +127,7 @@ class Bridge(object):
                 raise TypeError("Bridge with invalid ID")
             self.fingerprint = toHex(id_digest)
         elif fingerprint is not None:
-            if not is_valid_fingerprint(fingerprint):
+            if not isValidFingerprint(fingerprint):
                 raise TypeError("Bridge with invalid fingerprint (%r)"%
                                 fingerprint)
             self.fingerprint = fingerprint.lower()
@@ -248,7 +233,7 @@ class Bridge(object):
 
     def assertOK(self):
         assert is_valid_ip(self.ip)
-        assert is_valid_fingerprint(self.fingerprint)
+        assert isValidFingerprint(self.fingerprint)
         assert 1 <= self.orport <= 65535
         if self.or_addresses:
             for address, portlist in self.or_addresses.items():
@@ -603,7 +588,7 @@ def parseExtraInfoFile(f):
             line = line[11:]
             (nickname, ID) = line.split()
             logging.debug("  Parsed Nickname: %s", nickname)
-            if is_valid_fingerprint(ID):
+            if isValidFingerprint(ID):
                 logging.debug("  Parsed fingerprint: %s", ID)
                 ID = fromHex(ID)
             else:
@@ -720,7 +705,7 @@ def parseCountryBlockFile(f):
         line = line.strip()
         try:
             ID, addrspec, countries = line.split()
-            if is_valid_fingerprint(ID):
+            if isValidFingerprint(ID):
                 ID = fromHex(ID)
                 logging.debug("Parsed ID: %s", ID)
             else:
diff --git a/lib/bridgedb/parse/fingerprint.py b/lib/bridgedb/parse/fingerprint.py
new file mode 100644
index 0000000..17f15c3
--- /dev/null
+++ b/lib/bridgedb/parse/fingerprint.py
@@ -0,0 +1,44 @@
+# -*- coding: utf-8 ; test-case-name: bridgedb.test.test_parse_fingerprint ; -*-
+#_____________________________________________________________________________
+#
+# This file is part of BridgeDB, a Tor bridge distribution system.
+#
+# :authors: Isis Lovecruft 0xA3ADB67A2CDB8B35 <isis@xxxxxxxxxxxxxx>
+#           please also see AUTHORS file
+# :copyright: (c) 2007-2014, The Tor Project, Inc.
+#             (c) 2014, Isis Lovecruft
+# :license: see LICENSE for licensing information
+#_____________________________________________________________________________
+
+
+import binascii
+import logging
+
+
+#: The required length for hexidecimal representations of hash digest of a
+#: Tor relay's public identity key (a.k.a. its fingerprint).
+HEX_FINGERPRINT_LEN = 40
+
+
+#: (callable) Convert a value from binary to hexidecimal representation.
+toHex = binascii.b2a_hex
+
+#: (callable) Convert a value from hexidecimal to binary representation.
+fromHex = binascii.a2b_hex
+
+def isValidFingerprint(fingerprint):
+    """Determine if a Tor relay fingerprint is valid.
+
+    :param str fingerprint: The hex-encoded hash digest of the relay's
+        public identity key, a.k.a. its fingerprint.
+    :rtype: bool
+    :returns: ``True`` if the **fingerprint** was valid, ``False`` otherwise.
+    """
+    if len(fingerprint) == HEX_FINGERPRINT_LEN:
+        try:
+            fromHex(fingerprint)
+        except TypeError:
+            logging.debug("Invalid hex fingerprint: %r" % repr(fingerprint))
+        else:
+            return True
+    return False
diff --git a/lib/bridgedb/test/deprecated.py b/lib/bridgedb/test/deprecated.py
index 8238b53..a972a52 100644
--- a/lib/bridgedb/test/deprecated.py
+++ b/lib/bridgedb/test/deprecated.py
@@ -48,6 +48,24 @@ deprecate.deprecatedModuleAttribute(
     "bridgedb.Bridges",
     "re_ipv6")
 
+deprecate.deprecatedModuleAttribute(
+    Version('bridgedb', 0, 2, 3),
+    ("Removed due to 'bridgedb.Bridges.HEX_FP_LEN' being moved to "
+     "'bridgedb.parse.fingerprint.HEX_FINGERPRINT_LEN."),
+    "bridgedb.Bridges", "HEX_FP_LEN")
+
+deprecate.deprecatedModuleAttribute(
+    Version('bridgedb', 0, 2, 3),
+    ("Removed due to 'bridgedb.Bridges.toHex' being moved to "
+     "'bridgedb.parse.fingerprint.toHex."),
+    "bridgedb.Bridges", "toHex")
+
+deprecate.deprecatedModuleAttribute(
+    Version('bridgedb', 0, 2, 3),
+    ("Removed due to 'bridgedb.Bridges.fromHex' being moved to "
+     "'bridgedb.parse.fingerprint.fromHex."),
+    "bridgedb.Bridges", "fromHex")
+
 
 @deprecate.deprecated(
     Version('bridgedb', 0, 0, 1),
@@ -77,6 +95,21 @@ def parseORAddressLine(line):
     if address and portlist and len(portlist): return address,portlist
     raise ParseORAddressError(line)
 
+@xxxxxxxxxxxxxxxxxxxx(Version('bridgedb', 0, 2, 3),
+                      replacement='bridgedb.parse.fingerprint.isValidFingerprint')
+def is_valid_fingerprint(fp):
+    """Return true iff fp in the right format to be a hex fingerprint
+       of a Tor server.
+    """
+    if len(fp) != HEX_FP_LEN:
+        return False
+    try:
+        fromHex(fp)
+    except TypeError:
+        return False
+    else:
+        return True
+
 
 @deprecate.deprecated(
     Version('bridgedb', 0, 0, 1),
diff --git a/lib/bridgedb/test/test_Tests.py b/lib/bridgedb/test/test_Tests.py
index 00994b5..816cd40 100644
--- a/lib/bridgedb/test/test_Tests.py
+++ b/lib/bridgedb/test/test_Tests.py
@@ -16,6 +16,7 @@ be compatible with the newer :api:`twisted.trial` unittests in this directory.
 from __future__ import print_function
 from __future__ import unicode_literals
 
+import binascii
 import doctest
 import glob
 import logging
@@ -88,6 +89,11 @@ def monkeypatchTests():
     patcher.addPatch(Tests.bridgedb.Bridges, 'PORTSPEC_LEN', 16)
     patcher.addPatch(Tests.bridgedb.Bridges, 're_ipv4', deprecated.re_ipv4)
     patcher.addPatch(Tests.bridgedb.Bridges, 're_ipv6', deprecated.re_ipv6)
+    patcher.addPatch(Tests.bridgedb.Bridges, 'HEX_FP_LEN', 40)
+    patcher.addPatch(Tests.bridgedb.Bridges, 'toHex', binascii.b2a_hex)
+    patcher.addPatch(Tests.bridgedb.Bridges, 'fromHex', binascii.a2b_hex)
+    patcher.addPatch(Tests.bridgedb.Bridges, 'is_valid_fingerprint',
+                     deprecated.is_valid_fingerprint)
     return patcher
 
 



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