commit 72b90a84f8bd9557c201be7c45c79a1ea73e1bed Author: Damian Johnson atagar@torproject.org Date: Mon May 1 12:15:13 2017 -0700
Microdescriptor creation --- stem/descriptor/microdescriptor.py | 10 ++++++++ test/mocking.py | 27 ---------------------- test/unit/descriptor/microdescriptor.py | 41 ++++++++++++++++----------------- 3 files changed, 30 insertions(+), 48 deletions(-)
diff --git a/stem/descriptor/microdescriptor.py b/stem/descriptor/microdescriptor.py index a1e5346..fb3c1e6 100644 --- a/stem/descriptor/microdescriptor.py +++ b/stem/descriptor/microdescriptor.py @@ -69,7 +69,9 @@ import hashlib import stem.exit_policy
from stem.descriptor import ( + CRYPTO_BLOB, Descriptor, + _descriptor_content, _descriptor_components, _read_until_keywords, _values, @@ -102,6 +104,10 @@ SINGLE_FIELDS = ( 'pr', )
+MICRODESCRIPTOR = ( + ('onion-key', '\n-----BEGIN RSA PUBLIC KEY-----%s-----END RSA PUBLIC KEY-----' % CRYPTO_BLOB), +) +
def _parse_file(descriptor_file, validate = False, **kwargs): """ @@ -256,6 +262,10 @@ class Microdescriptor(Descriptor): 'id': _parse_id_line, }
+ @classmethod + def content(cls, attr = None, exclude = ()): + return _descriptor_content(attr, exclude, MICRODESCRIPTOR) + def __init__(self, raw_contents, validate = False, annotations = None): super(Microdescriptor, self).__init__(raw_contents, lazy_load = not validate) self._annotation_lines = annotations if annotations else [] diff --git a/test/mocking.py b/test/mocking.py index ce9a458..f1d859e 100644 --- a/test/mocking.py +++ b/test/mocking.py @@ -13,9 +13,6 @@ Helper functions for creating mock objects. get_message - stem.response.ControlMessage get_protocolinfo_response - stem.response.protocolinfo.ProtocolInfoResponse
- stem.descriptor.microdescriptor - get_microdescriptor - Microdescriptor - stem.descriptor.networkstatus get_directory_authority - DirectoryAuthority get_key_certificate - KeyCertificate @@ -72,10 +69,6 @@ DOC_SIG = stem.descriptor.networkstatus.DocumentSignature( 'BF112F1C6D5543CFD0A32215ACABD4197B5279AD', '-----BEGIN SIGNATURE-----%s-----END SIGNATURE-----' % CRYPTO_BLOB)
-MICRODESCRIPTOR = ( - ('onion-key', '\n-----BEGIN RSA PUBLIC KEY-----%s-----END RSA PUBLIC KEY-----' % CRYPTO_BLOB), -) - ROUTER_STATUS_ENTRY_V2_HEADER = ( ('r', 'caerSidi p1aag7VwarGxqctS7/fS0y5FU+s oQZFLYe9e4A7bOkWKR7TaNxb0JE 2012-08-06 11:19:31 71.35.150.29 9001 0'), ) @@ -319,26 +312,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_microdescriptor(attr = None, exclude = (), content = False): - """ - Provides the descriptor content for... - stem.descriptor.microdescriptor.Microdescriptor - - :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: Microdescriptor for the requested descriptor content - """ - - desc_content = _get_descriptor_content(attr, exclude, MICRODESCRIPTOR) - - if content: - return desc_content - else: - return stem.descriptor.microdescriptor.Microdescriptor(desc_content, validate = True) - - def get_router_status_entry_v2(attr = None, exclude = (), content = False): """ Provides the descriptor content for... diff --git a/test/unit/descriptor/microdescriptor.py b/test/unit/descriptor/microdescriptor.py index f9350d2..35b8de0 100644 --- a/test/unit/descriptor/microdescriptor.py +++ b/test/unit/descriptor/microdescriptor.py @@ -11,11 +11,6 @@ import stem.descriptor from stem.util import str_type from stem.descriptor.microdescriptor import Microdescriptor
-from test.mocking import ( - get_microdescriptor, - CRYPTO_BLOB, -) - from test.unit.descriptor import get_resource
FIRST_ONION_KEY = """\ @@ -87,9 +82,9 @@ class TestMicrodescriptor(unittest.TestCase): attributes. """
- desc = get_microdescriptor() + desc = Microdescriptor.create()
- self.assertTrue(CRYPTO_BLOB in desc.onion_key) + self.assertTrue(stem.descriptor.CRYPTO_BLOB in desc.onion_key) self.assertEqual(None, desc.ntor_onion_key) self.assertEqual([], desc.or_addresses) self.assertEqual([], desc.family) @@ -106,7 +101,7 @@ class TestMicrodescriptor(unittest.TestCase): Includes unrecognized content in the descriptor. """
- desc = get_microdescriptor({'pepperjack': 'is oh so tasty!'}) + desc = Microdescriptor.create({'pepperjack': 'is oh so tasty!'}) self.assertEqual(['pepperjack is oh so tasty!'], desc.get_unrecognized_lines())
def test_proceeding_line(self): @@ -114,7 +109,7 @@ class TestMicrodescriptor(unittest.TestCase): Includes a line prior to the 'onion-key' entry. """
- desc_text = b'family Amunet1\n' + get_microdescriptor(content = True) + desc_text = b'family Amunet1\n' + Microdescriptor.content() self.assertRaises(ValueError, Microdescriptor, desc_text, True)
desc = Microdescriptor(desc_text, validate = False) @@ -125,7 +120,7 @@ class TestMicrodescriptor(unittest.TestCase): Sanity test with both an IPv4 and IPv6 address. """
- desc_text = get_microdescriptor(content = True) + desc_text = Microdescriptor.content() desc_text += b'\na 10.45.227.253:9001' desc_text += b'\na [fd9f:2e19:3bcf::02:9970]:9001'
@@ -142,12 +137,12 @@ class TestMicrodescriptor(unittest.TestCase): Check the family line. """
- desc = get_microdescriptor({'family': 'Amunet1 Amunet2 Amunet3'}) + desc = Microdescriptor.create({'family': 'Amunet1 Amunet2 Amunet3'}) self.assertEqual(['Amunet1', 'Amunet2', 'Amunet3'], desc.family)
# try multiple family lines
- desc_text = get_microdescriptor(content = True) + desc_text = Microdescriptor.content() desc_text += b'\nfamily Amunet1' desc_text += b'\nfamily Amunet2'
@@ -163,7 +158,7 @@ class TestMicrodescriptor(unittest.TestCase): field so we're not investing much effort here. """
- desc = get_microdescriptor({'p': 'accept 80,110,143,443'}) + desc = Microdescriptor.create({'p': 'accept 80,110,143,443'}) self.assertEqual(stem.exit_policy.MicroExitPolicy('accept 80,110,143,443'), desc.exit_policy)
def test_protocols(self): @@ -171,7 +166,7 @@ class TestMicrodescriptor(unittest.TestCase): Basic check for 'pr' lines. """
- desc = get_microdescriptor({'pr': 'Cons=1 Desc=1 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 Link=1-4 LinkAuth=1 Microdesc=1 Relay=1-2'}) + desc = Microdescriptor.create({'pr': 'Cons=1 Desc=1 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 Link=1-4 LinkAuth=1 Microdesc=1 Relay=1-2'}) self.assertEqual(10, len(desc.protocols))
def test_identifier(self): @@ -179,16 +174,18 @@ class TestMicrodescriptor(unittest.TestCase): Basic check for 'id' lines. """
- desc = get_microdescriptor({'id': 'rsa1024 Cd47okjCHD83YGzThGBDptXs9Z4'}) + desc = Microdescriptor.create({'id': 'rsa1024 Cd47okjCHD83YGzThGBDptXs9Z4'}) self.assertEqual({'rsa1024': 'Cd47okjCHD83YGzThGBDptXs9Z4'}, desc.identifiers) self.assertEqual('rsa1024', desc.identifier_type) self.assertEqual('Cd47okjCHD83YGzThGBDptXs9Z4', desc.identifier)
# check when there's multiple key types
- desc_text = b'\n'.join((get_microdescriptor(content = True), - b'id rsa1024 Cd47okjCHD83YGzThGBDptXs9Z4', - b'id ed25519 50f6ddbecdc848dcc6b818b14d1')) + desc_text = b'\n'.join(( + Microdescriptor.content(), + b'id rsa1024 Cd47okjCHD83YGzThGBDptXs9Z4', + b'id ed25519 50f6ddbecdc848dcc6b818b14d1', + ))
desc = Microdescriptor(desc_text, validate = True) self.assertEqual({'rsa1024': 'Cd47okjCHD83YGzThGBDptXs9Z4', 'ed25519': '50f6ddbecdc848dcc6b818b14d1'}, desc.identifiers) @@ -197,9 +194,11 @@ class TestMicrodescriptor(unittest.TestCase):
# check when there's conflicting keys
- desc_text = b'\n'.join((get_microdescriptor(content = True), - b'id rsa1024 Cd47okjCHD83YGzThGBDptXs9Z4', - b'id rsa1024 50f6ddbecdc848dcc6b818b14d1')) + desc_text = b'\n'.join(( + Microdescriptor.content(), + b'id rsa1024 Cd47okjCHD83YGzThGBDptXs9Z4', + b'id rsa1024 50f6ddbecdc848dcc6b818b14d1', + ))
desc = Microdescriptor(desc_text) self.assertEqual({}, desc.identifiers)