commit c2c5c58519f1fc695b0c2111cb47f522510a6a53 Author: Damian Johnson atagar@torproject.org Date: Fri Jan 12 10:02:10 2018 -0800
Load new fallback attributes when persisted --- cache_fallback_directories.py | 2 +- stem/descriptor/remote.py | 16 ++++++++++++++-- test/unit/descriptor/remote.py | 15 ++++++++++++--- 3 files changed, 27 insertions(+), 6 deletions(-)
diff --git a/cache_fallback_directories.py b/cache_fallback_directories.py index 50139f56..990301f4 100755 --- a/cache_fallback_directories.py +++ b/cache_fallback_directories.py @@ -48,7 +48,7 @@ if __name__ == '__main__':
# all fallbacks have the same header metadata, so just picking one
- headers = latest_fallback_directories.values()[0].header if latest_fallback_directories else {} + headers = latest_fallback_directories.values()[0].header if latest_fallback_directories else None
print('Differences detected...\n') print(stem.descriptor.remote._fallback_directory_differences(cached_fallback_directories, latest_fallback_directories)) diff --git a/stem/descriptor/remote.py b/stem/descriptor/remote.py index f4845922..b2fd612c 100644 --- a/stem/descriptor/remote.py +++ b/stem/descriptor/remote.py @@ -92,6 +92,12 @@ import time import zlib
try: + # added in python 2.7 + from collections import OrderedDict +except ImportError: + from stem.util.ordereddict import OrderedDict + +try: # account for urllib's change between python 2.x and 3.x import urllib.request as urllib except ImportError: @@ -966,7 +972,7 @@ class FallbackDirectory(Directory): self.nickname = nickname self.has_extrainfo = has_extrainfo self.orport_v6 = orport_v6 - self.header = header if header else {} + self.header = header if header else OrderedDict()
@staticmethod def from_cache(path = CACHE_PATH): @@ -988,6 +994,7 @@ class FallbackDirectory(Directory):
conf = stem.util.conf.Config() conf.load(path) + headers = OrderedDict([(k.split('.', 1)[1], conf.get(k)) for k in conf.keys() if k.startswith('header.')])
results = {}
@@ -1010,7 +1017,7 @@ class FallbackDirectory(Directory): raise IOError("'%s.or_port' was an invalid port (%s)" % (fingerprint, attr['or_port'])) elif not connection.is_valid_port(attr['dir_port']): raise IOError("'%s.dir_port' was an invalid port (%s)" % (fingerprint, attr['dir_port'])) - elif attr['nickname'] and not connection.is_valid_nickname(attr['nickname']): + elif attr['nickname'] and not tor_tools.is_valid_nickname(attr['nickname']): raise IOError("'%s.nickname' was an invalid nickname (%s)" % (fingerprint, attr['nickname'])) elif attr['orport6_address'] and not connection.is_valid_ipv6_address(attr['orport6_address']): raise IOError("'%s.orport6_address' was an invalid IPv6 address (%s)" % (fingerprint, attr['orport6_address'])) @@ -1027,7 +1034,10 @@ class FallbackDirectory(Directory): or_port = int(attr['or_port']), dir_port = int(attr['dir_port']), fingerprint = fingerprint, + nickname = attr['nickname'], + has_extrainfo = attr['has_extrainfo'] == 'true', orport_v6 = orport_v6, + header = headers, )
return results @@ -1209,6 +1219,8 @@ class FallbackDirectory(Directory): conf.set('%s.address' % fingerprint, directory.address) conf.set('%s.or_port' % fingerprint, str(directory.or_port)) conf.set('%s.dir_port' % fingerprint, str(directory.dir_port)) + conf.set('%s.nickname' % fingerprint, directory.nickname) + conf.set('%s.has_extrainfo' % fingerprint, 'true' if directory.has_extrainfo else 'false')
if directory.orport_v6: conf.set('%s.orport6_address' % fingerprint, str(directory.orport_v6[0])) diff --git a/test/unit/descriptor/remote.py b/test/unit/descriptor/remote.py index c17a1342..9409544c 100644 --- a/test/unit/descriptor/remote.py +++ b/test/unit/descriptor/remote.py @@ -12,6 +12,12 @@ import stem.prereq import stem.util.conf
try: + # added in python 2.7 + from collections import OrderedDict +except ImportError: + from stem.util.ordereddict import OrderedDict + +try: # added in python 3.3 from unittest.mock import patch except ImportError: @@ -211,7 +217,7 @@ class TestDescriptorDownloader(unittest.TestCase): def test_fallback_directories_from_remote(self, urlopen_mock): urlopen_mock.return_value = io.BytesIO(FALLBACK_DIR_CONTENT) fallback_directories = stem.descriptor.remote.FallbackDirectory.from_remote() - header = {'type': 'fallback', 'version': '2.0.0', 'timestamp': '20170526090242'} + header = OrderedDict((('type', 'fallback'), ('version', '2.0.0'), ('timestamp', '20170526090242')))
expected = { '0756B7CD4DFC8182BE23143FAC0642F515182CEB': stem.descriptor.remote.FallbackDirectory( @@ -239,7 +245,7 @@ class TestDescriptorDownloader(unittest.TestCase): self.assertEqual(expected, fallback_directories)
def test_fallback_persistence(self): - header = {'type': 'fallback', 'version': '2.0.0', 'timestamp': '20170526090242'} + header = OrderedDict((('type', 'fallback'), ('version', '2.0.0'), ('timestamp', '20170526090242')))
expected = { '0756B7CD4DFC8182BE23143FAC0642F515182CEB': stem.descriptor.remote.FallbackDirectory( @@ -273,9 +279,12 @@ class TestDescriptorDownloader(unittest.TestCase): '01A9258A46E97FF8B2CAC7910577862C14F2C524.address': ['193.171.202.146'], '01A9258A46E97FF8B2CAC7910577862C14F2C524.or_port': ['9001'], '01A9258A46E97FF8B2CAC7910577862C14F2C524.dir_port': ['9030'], + '01A9258A46E97FF8B2CAC7910577862C14F2C524.has_extrainfo': ['false'], '0756B7CD4DFC8182BE23143FAC0642F515182CEB.address': ['5.9.110.236'], '0756B7CD4DFC8182BE23143FAC0642F515182CEB.or_port': ['9001'], '0756B7CD4DFC8182BE23143FAC0642F515182CEB.dir_port': ['9030'], + '0756B7CD4DFC8182BE23143FAC0642F515182CEB.nickname': ['rueckgrat'], + '0756B7CD4DFC8182BE23143FAC0642F515182CEB.has_extrainfo': ['true'], '0756B7CD4DFC8182BE23143FAC0642F515182CEB.orport6_address': ['2a01:4f8:162:51e2::2'], '0756B7CD4DFC8182BE23143FAC0642F515182CEB.orport6_port': ['9001'], } @@ -287,7 +296,7 @@ class TestDescriptorDownloader(unittest.TestCase): conf.load(tmp.name) self.assertEqual(excepted_config, dict(conf))
- #self.assertEqual(expected, stem.descriptor.remote.FallbackDirectory.from_cache(tmp.name)) + self.assertEqual(expected, stem.descriptor.remote.FallbackDirectory.from_cache(tmp.name))
@patch(URL_OPEN) def test_fallback_directories_from_remote_empty(self, urlopen_mock):