commit ceeec7989007371508b024d779fc12fd62a68bd6 Author: Damian Johnson atagar@torproject.org Date: Mon May 1 11:11:18 2017 -0700
BridgeExtraInfoDescriptor creation --- stem/descriptor/extrainfo_descriptor.py | 13 +++++++++++ test/mocking.py | 32 ---------------------------- test/unit/descriptor/extrainfo_descriptor.py | 27 +++++++++++++---------- 3 files changed, 29 insertions(+), 43 deletions(-)
diff --git a/stem/descriptor/extrainfo_descriptor.py b/stem/descriptor/extrainfo_descriptor.py index 7a2e7b1..848ba3e 100644 --- a/stem/descriptor/extrainfo_descriptor.py +++ b/stem/descriptor/extrainfo_descriptor.py @@ -166,6 +166,15 @@ RELAY_EXTRAINFO_FOOTER = ( ('router-signature', '\n-----BEGIN SIGNATURE-----%s-----END SIGNATURE-----' % CRYPTO_BLOB), )
+BRIDGE_EXTRAINFO_HEADER = ( + ('extra-info', 'ec2bridgereaac65a3 1EC248422B57D9C0BD751892FE787585407479A4'), + ('published', '2012-05-05 17:03:50'), +) + +BRIDGE_EXTRAINFO_FOOTER = ( + ('router-digest', '006FD96BA35E7785A6A3B8B75FE2E2435A13BDB4'), +) +
_timestamp_re = re.compile('^(.*) (([0-9]+) s)( .*)?$') _locale_re = re.compile('^[a-zA-Z0-9?]{2}$') @@ -961,6 +970,10 @@ class BridgeExtraInfoDescriptor(ExtraInfoDescriptor): 'router-digest': _parse_router_digest_line, })
+ @classmethod + def content(cls, attr = None, exclude = ()): + return _descriptor_content(attr, exclude, BRIDGE_EXTRAINFO_HEADER, BRIDGE_EXTRAINFO_FOOTER) + def digest(self): return self._digest
diff --git a/test/mocking.py b/test/mocking.py index dc08a61..ce9a458 100644 --- a/test/mocking.py +++ b/test/mocking.py @@ -16,9 +16,6 @@ Helper functions for creating mock objects. stem.descriptor.microdescriptor get_microdescriptor - Microdescriptor
- stem.descriptor.extrainfo_descriptor - get_bridge_extrainfo_descriptor - BridgeExtraInfoDescriptor - stem.descriptor.networkstatus get_directory_authority - DirectoryAuthority get_key_certificate - KeyCertificate @@ -75,15 +72,6 @@ DOC_SIG = stem.descriptor.networkstatus.DocumentSignature( 'BF112F1C6D5543CFD0A32215ACABD4197B5279AD', '-----BEGIN SIGNATURE-----%s-----END SIGNATURE-----' % CRYPTO_BLOB)
-BRIDGE_EXTRAINFO_HEADER = ( - ('extra-info', 'ec2bridgereaac65a3 1EC248422B57D9C0BD751892FE787585407479A4'), - ('published', '2012-05-05 17:03:50'), -) - -BRIDGE_EXTRAINFO_FOOTER = ( - ('router-digest', '006FD96BA35E7785A6A3B8B75FE2E2435A13BDB4'), -) - MICRODESCRIPTOR = ( ('onion-key', '\n-----BEGIN RSA PUBLIC KEY-----%s-----END RSA PUBLIC KEY-----' % CRYPTO_BLOB), ) @@ -331,26 +319,6 @@ def _get_descriptor_content(attr = None, exclude = (), header_template = (), foo return stem.util.str_tools._to_bytes('\n'.join(header_content + remainder + footer_content))
-def get_bridge_extrainfo_descriptor(attr = None, exclude = (), content = False): - """ - Provides the descriptor content for... - stem.descriptor.extrainfo_descriptor.BridgeExtraInfoDescriptor - - :param dict attr: keyword/value mappings to be included in the descriptor - :param list exclude: mandatory keywords to exclude from the descriptor - :param bool content: provides the str content of the descriptor rather than the class if True - - :returns: BridgeExtraInfoDescriptor for the requested descriptor content - """ - - desc_content = _get_descriptor_content(attr, exclude, BRIDGE_EXTRAINFO_HEADER, BRIDGE_EXTRAINFO_FOOTER) - - if content: - return desc_content - else: - return stem.descriptor.extrainfo_descriptor.BridgeExtraInfoDescriptor(desc_content, validate = True) - - def get_microdescriptor(attr = None, exclude = (), content = False): """ Provides the descriptor content for... diff --git a/test/unit/descriptor/extrainfo_descriptor.py b/test/unit/descriptor/extrainfo_descriptor.py index 61f5ee7..2269000 100644 --- a/test/unit/descriptor/extrainfo_descriptor.py +++ b/test/unit/descriptor/extrainfo_descriptor.py @@ -8,9 +8,14 @@ import unittest
import stem.descriptor
-from stem.descriptor.extrainfo_descriptor import RelayExtraInfoDescriptor, DirResponse, DirStat +from stem.descriptor.extrainfo_descriptor import ( + RelayExtraInfoDescriptor, + BridgeExtraInfoDescriptor, + DirResponse, + DirStat, +)
-from test.mocking import get_bridge_extrainfo_descriptor, CRYPTO_BLOB +from test.mocking import CRYPTO_BLOB
from test.unit.descriptor import get_resource
@@ -677,7 +682,7 @@ k0d2aofcVbHr4fPQOSST0LXDrhFl5Fqo5um296zpJGvRUeO6S44U/EfJAGShtqWw Basic sanity check that we can parse a descriptor with minimal attributes. """
- desc = get_bridge_extrainfo_descriptor() + desc = BridgeExtraInfoDescriptor.create()
self.assertEqual('ec2bridgereaac65a3', desc.nickname) self.assertEqual('1EC248422B57D9C0BD751892FE787585407479A4', desc.fingerprint) @@ -692,13 +697,13 @@ k0d2aofcVbHr4fPQOSST0LXDrhFl5Fqo5um296zpJGvRUeO6S44U/EfJAGShtqWw Parses the 'bridge-ip-versions' line, which only appears in bridges. """
- desc = get_bridge_extrainfo_descriptor({'bridge-ip-versions': 'v4=16,v6=40'}) + desc = BridgeExtraInfoDescriptor.create({'bridge-ip-versions': 'v4=16,v6=40'}) self.assertEqual({'v4': 16, 'v6': 40}, desc.ip_versions)
- desc = get_bridge_extrainfo_descriptor({'bridge-ip-versions': ''}) + desc = BridgeExtraInfoDescriptor.create({'bridge-ip-versions': ''}) self.assertEqual({}, desc.ip_versions)
- desc_text = get_bridge_extrainfo_descriptor({'bridge-ip-versions': 'v4=24.5'}, content = True) + desc_text = BridgeExtraInfoDescriptor.content({'bridge-ip-versions': 'v4=24.5'}) self.assertRaises(ValueError, RelayExtraInfoDescriptor, desc_text, True)
def test_bridge_ip_transports_line(self): @@ -706,13 +711,13 @@ k0d2aofcVbHr4fPQOSST0LXDrhFl5Fqo5um296zpJGvRUeO6S44U/EfJAGShtqWw Parses the 'bridge-ip-transports' line, which only appears in bridges. """
- desc = get_bridge_extrainfo_descriptor({'bridge-ip-transports': '<OR>=16,<??>=40'}) + desc = BridgeExtraInfoDescriptor.create({'bridge-ip-transports': '<OR>=16,<??>=40'}) self.assertEqual({'<OR>': 16, '<??>': 40}, desc.ip_transports)
- desc = get_bridge_extrainfo_descriptor({'bridge-ip-transports': ''}) + desc = BridgeExtraInfoDescriptor.create({'bridge-ip-transports': ''}) self.assertEqual({}, desc.ip_transports)
- desc_text = get_bridge_extrainfo_descriptor({'bridge-ip-transports': '<OR>=24.5'}, content = True) + desc_text = BridgeExtraInfoDescriptor.content({'bridge-ip-transports': '<OR>=24.5'}) self.assertRaises(ValueError, RelayExtraInfoDescriptor, desc_text, True)
def test_transport_line(self): @@ -720,7 +725,7 @@ k0d2aofcVbHr4fPQOSST0LXDrhFl5Fqo5um296zpJGvRUeO6S44U/EfJAGShtqWw Basic exercise for both a bridge and relay's transport entry. """
- desc = get_bridge_extrainfo_descriptor({'transport': 'obfs3'}) + desc = BridgeExtraInfoDescriptor.create({'transport': 'obfs3'}) self.assertEqual({'obfs3': (None, None, None)}, desc.transport) self.assertEqual([], desc.get_unrecognized_lines())
@@ -729,7 +734,7 @@ k0d2aofcVbHr4fPQOSST0LXDrhFl5Fqo5um296zpJGvRUeO6S44U/EfJAGShtqWw self.assertEqual([], desc.get_unrecognized_lines())
# multiple transport lines - desc = get_bridge_extrainfo_descriptor({'transport': 'obfs3\ntransport obfs4'}) + desc = BridgeExtraInfoDescriptor.create({'transport': 'obfs3\ntransport obfs4'}) self.assertEqual({'obfs3': (None, None, None), 'obfs4': (None, None, None)}, desc.transport) self.assertEqual([], desc.get_unrecognized_lines())