commit d893ec53b03bda8fa973e0552c65999b5eb5167f Author: Damian Johnson atagar@torproject.org Date: Sun May 6 13:03:24 2018 -0700
Unit test directory equality
Now that we've cleaned up our equality checks it's a natural spot to start testing. Found an interesting bug where 'False' and 'None' values resulted in directories being considered equal. --- stem/util/__init__.py | 8 ++++++++ test/settings.cfg | 1 + test/unit/directory/__init__.py | 1 + test/unit/directory/authority.py | 28 ++++++++++++++++++++++++++++ test/unit/directory/fallback.py | 24 ++++++++++++++++++++++++ 5 files changed, 62 insertions(+)
diff --git a/stem/util/__init__.py b/stem/util/__init__.py index 5ccd921a..711f28a5 100644 --- a/stem/util/__init__.py +++ b/stem/util/__init__.py @@ -31,6 +31,12 @@ else: str_type = unicode int_type = long
+# Python hashes booleans to zero or one. Usually this would be fine, but since +# we use hashes for equality checks we need them to be something less common. + +TRUE_HASH_VALUE = 4813749 +FALSE_HASH_VALUE = 5826450 +
def datetime_to_unix(timestamp): """ @@ -72,6 +78,8 @@ def _hash_attr(obj, *attributes, **kwargs): elif isinstance(attr_value, (list, tuple)): for entry in attr_value: my_hash = (my_hash + hash(entry)) * 1024 + elif isinstance(attr_value, bool): + my_hash += TRUE_HASH_VALUE if attr_value else FALSE_HASH_VALUE else: my_hash += hash(attr_value)
diff --git a/test/settings.cfg b/test/settings.cfg index 2206c7e7..312e3ce9 100644 --- a/test/settings.cfg +++ b/test/settings.cfg @@ -220,6 +220,7 @@ test.unit_tests |test.unit.endpoint.TestEndpoint |test.unit.version.TestVersion |test.unit.manual.TestManual +|test.unit.directory.authority.TestAuthority |test.unit.directory.fallback.TestFallback |test.unit.tutorial.TestTutorial |test.unit.tutorial_examples.TestTutorialExamples diff --git a/test/unit/directory/__init__.py b/test/unit/directory/__init__.py index 78e42d7c..7ca7ea92 100644 --- a/test/unit/directory/__init__.py +++ b/test/unit/directory/__init__.py @@ -3,5 +3,6 @@ Unit tests for stem.directory. """
__all__ = [ + 'authority', 'fallback', ] diff --git a/test/unit/directory/authority.py b/test/unit/directory/authority.py new file mode 100644 index 00000000..4a5f3a12 --- /dev/null +++ b/test/unit/directory/authority.py @@ -0,0 +1,28 @@ +""" +Unit tests for stem.directory.Authority. +""" + +import unittest + +import stem.directory + + +class TestAuthority(unittest.TestCase): + def test_equality(self): + authority_attr = { + 'address': '5.9.110.236', + 'or_port': 9001, + 'dir_port': 9030, + 'fingerprint': '0756B7CD4DFC8182BE23143FAC0642F515182CEB', + 'nickname': 'rueckgrat', + 'v3ident': '23D15D965BC35114467363C165C4F724B64B4F66', + 'is_bandwidth_authority': False, + } + + self.assertEqual(stem.directory.Authority(**authority_attr), stem.directory.Authority(**authority_attr)) + + for attr in authority_attr: + for value in (None, 'something else'): + second_authority = dict(authority_attr) + second_authority[attr] = value + self.assertNotEqual(stem.directory.Authority(**authority_attr), stem.directory.Authority(**second_authority)) diff --git a/test/unit/directory/fallback.py b/test/unit/directory/fallback.py index c95d74b7..cd857b9f 100644 --- a/test/unit/directory/fallback.py +++ b/test/unit/directory/fallback.py @@ -65,6 +65,30 @@ FALLBACK_ENTRY = b"""\
class TestFallback(unittest.TestCase): + def test_equality(self): + fallback_attr = { + 'address': '5.9.110.236', + 'or_port': 9001, + 'dir_port': 9030, + 'fingerprint': '0756B7CD4DFC8182BE23143FAC0642F515182CEB', + 'nickname': 'rueckgrat', + 'has_extrainfo': True, + 'orport_v6': ('2a01:4f8:162:51e2::2', 9001), + 'header': OrderedDict(( + ('type', 'fallback'), + ('version', '2.0.0'), + ('timestamp', '20170526090242'), + )), + } + + self.assertEqual(stem.directory.Fallback(**fallback_attr), stem.directory.Fallback(**fallback_attr)) + + for attr in fallback_attr: + for value in (None, 'something else'): + second_fallback = dict(fallback_attr) + second_fallback[attr] = value + self.assertNotEqual(stem.directory.Fallback(**fallback_attr), stem.directory.Fallback(**second_fallback)) + def test_from_cache(self): # quick sanity test that we can load cached content fallback_directories = stem.directory.Fallback.from_cache()
tor-commits@lists.torproject.org