[stem/master] Funcions to check validity of nicknames and fingerprints

commit 28fb4b1d55952ec905762d4cb400d997e117a0ea Author: Damian Johnson <atagar@torproject.org> Date: Sat Mar 17 17:59:33 2012 -0700 Funcions to check validity of nicknames and fingerprints Couple functions to check if a string is a valid nickname or fingerprint, plus tests. I needed to guess at the definition of a HEXDIG but otherwise nothing very interesting. --- run_tests.py | 2 + stem/util/__init__.py | 2 +- stem/util/tor_tools.py | 44 ++++++++++++++++++++++++++++++++++ test/unit/util/__init__.py | 2 +- test/unit/util/tor_tools.py | 55 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 103 insertions(+), 2 deletions(-) diff --git a/run_tests.py b/run_tests.py index 4918881..e0574d3 100755 --- a/run_tests.py +++ b/run_tests.py @@ -22,6 +22,7 @@ import test.unit.util.conf import test.unit.util.connection import test.unit.util.enum import test.unit.util.system +import test.unit.util.tor_tools import test.unit.version import test.integ.connection.authentication import test.integ.connection.connect @@ -84,6 +85,7 @@ UNIT_TESTS = ( test.unit.util.connection.TestConnection, test.unit.util.conf.TestConf, test.unit.util.system.TestSystem, + test.unit.util.tor_tools.TestTorTools, test.unit.descriptor.reader.TestDescriptorReader, test.unit.version.TestVersion, test.unit.socket.control_message.TestControlMessage, diff --git a/stem/util/__init__.py b/stem/util/__init__.py index bbf4df7..5f6bb24 100644 --- a/stem/util/__init__.py +++ b/stem/util/__init__.py @@ -2,5 +2,5 @@ Utility functions used by the stem library. """ -__all__ = ["conf", "connection", "enum", "log", "proc", "system", "term"] +__all__ = ["conf", "connection", "enum", "log", "proc", "system", "term", "tor_tools"] diff --git a/stem/util/tor_tools.py b/stem/util/tor_tools.py new file mode 100644 index 0000000..5e468c3 --- /dev/null +++ b/stem/util/tor_tools.py @@ -0,0 +1,44 @@ +""" +Miscellaneous utility functions for working with tor. +""" + +import re + +# The control-spec defines the following as... +# Fingerprint = "$" 40*HEXDIG +# NicknameChar = "a"-"z" / "A"-"Z" / "0" - "9" +# Nickname = 1*19 NicknameChar +# +# HEXDIG is defined in RFC 5234 as being uppercase and used in RFC 5987 as +# case insensitive. Tor doesn't define this in the spec so flipping a coin +# and going with case insensitive. + +FINGERPRINT_PATTERN = re.compile("^\$[0-9a-fA-F]{40}$") +NICKNAME_PATTERN = re.compile("^[a-zA-Z0-9]{1,19}$") + +def is_valid_fingerprint(entry): + """ + Checks if a string is a properly formatted relay fingerprint. + + Arguments: + entry (str) - string to be checked + + Returns: + True if the string could be a relay fingerprint, False otherwise. + """ + + return bool(FINGERPRINT_PATTERN.match(entry)) + +def is_valid_nickname(entry): + """ + Checks if a string is a valid format for being a nickname. + + Arguments: + entry (str) - string to be checked + + Returns: + True if the string could be a nickname, False otherwise. + """ + + return bool(NICKNAME_PATTERN.match(entry)) + diff --git a/test/unit/util/__init__.py b/test/unit/util/__init__.py index 74b758a..17a4deb 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", "connection", "enum", "system"] +__all__ = ["conf", "connection", "enum", "system", "tor_tools"] diff --git a/test/unit/util/tor_tools.py b/test/unit/util/tor_tools.py new file mode 100644 index 0000000..5dd1abb --- /dev/null +++ b/test/unit/util/tor_tools.py @@ -0,0 +1,55 @@ +""" +Unit tests for the stem.util.tor_tools functions. +""" + +import unittest +import stem.util.tor_tools + +class TestTorTools(unittest.TestCase): + def test_is_valid_fingerprint(self): + """ + Checks the is_valid_fingerprint function. + """ + + valid_fingerprints = ( + "$A7569A83B5706AB1B1A9CB52EFF7D2D32E4553EB", + "$a7569a83b5706ab1b1a9cb52eff7d2d32e4553eb", + ) + + invalid_fingerprints = ( + "", + "A7569A83B5706AB1B1A9CB52EFF7D2D32E4553EB", + "$A7569A83B5706AB1B1A9CB52EFF7D2D32E4553E", + "$A7569A83B5706AB1B1A9CB52EFF7D2D32E4553E33", + "$A7569A83B5706AB1B1A9CB52EFF7D2D32E4553EG", + ) + + for fingerprint in valid_fingerprints: + self.assertTrue(stem.util.tor_tools.is_valid_fingerprint(fingerprint)) + + for fingerprint in invalid_fingerprints: + self.assertFalse(stem.util.tor_tools.is_valid_fingerprint(fingerprint)) + + def test_is_valid_nickname(self): + """ + Checks the is_valid_nickname function. + """ + + valid_nicknames = ( + "caerSidi", + "a", + "abcABC123", + ) + + invalid_nicknames = ( + "", + "toolongggggggggggggg", + "bad_character", + ) + + for nickname in valid_nicknames: + self.assertTrue(stem.util.tor_tools.is_valid_nickname(nickname)) + + for nickname in invalid_nicknames: + self.assertFalse(stem.util.tor_tools.is_valid_nickname(nickname)) +
participants (1)
-
atagar@torproject.org