[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] BridgeDescriptor creation



commit ab765d3233090d0014b939b4f0d3e89e70d96c89
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Mon May 1 10:39:50 2017 -0700

    BridgeDescriptor creation
---
 stem/descriptor/server_descriptor.py      | 12 +++++++++++
 test/mocking.py                           | 31 ---------------------------
 test/unit/descriptor/export.py            | 10 ++-------
 test/unit/descriptor/server_descriptor.py | 35 ++++++++++++++-----------------
 4 files changed, 30 insertions(+), 58 deletions(-)

diff --git a/stem/descriptor/server_descriptor.py b/stem/descriptor/server_descriptor.py
index b4dcd23..d764588 100644
--- a/stem/descriptor/server_descriptor.py
+++ b/stem/descriptor/server_descriptor.py
@@ -125,6 +125,14 @@ RELAY_SERVER_FOOTER = (
   ('router-signature', '\n-----BEGIN SIGNATURE-----%s-----END SIGNATURE-----' % CRYPTO_BLOB),
 )
 
+BRIDGE_SERVER_HEADER = (
+  ('router', 'Unnamed 10.45.227.253 9001 0 0'),
+  ('router-digest', '006FD96BA35E7785A6A3B8B75FE2E2435A13BDB4'),
+  ('published', '2012-03-22 17:34:38'),
+  ('bandwidth', '409600 819200 5120'),
+  ('reject', '*:*'),
+)
+
 
 def _parse_file(descriptor_file, is_bridge = False, validate = False, **kwargs):
   """
@@ -900,6 +908,10 @@ class BridgeDescriptor(ServerDescriptor):
     'router-digest': _parse_router_digest_line,
   })
 
+  @classmethod
+  def content(cls, attr = None, exclude = ()):
+    return _descriptor_content(attr, exclude, BRIDGE_SERVER_HEADER)
+
   def digest(self):
     return self._digest
 
diff --git a/test/mocking.py b/test/mocking.py
index 990aa89..caf7d44 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.server_descriptor
-      get_bridge_server_descriptor - BridgeDescriptor
-
     stem.descriptor.microdescriptor
       get_microdescriptor - Microdescriptor
 
@@ -79,14 +76,6 @@ DOC_SIG = stem.descriptor.networkstatus.DocumentSignature(
   'BF112F1C6D5543CFD0A32215ACABD4197B5279AD',
   '-----BEGIN SIGNATURE-----%s-----END SIGNATURE-----' % CRYPTO_BLOB)
 
-BRIDGE_SERVER_HEADER = (
-  ('router', 'Unnamed 10.45.227.253 9001 0 0'),
-  ('router-digest', '006FD96BA35E7785A6A3B8B75FE2E2435A13BDB4'),
-  ('published', '2012-03-22 17:34:38'),
-  ('bandwidth', '409600 819200 5120'),
-  ('reject', '*:*'),
-)
-
 RELAY_EXTRAINFO_HEADER = (
   ('extra-info', 'ninja B2289C3EAB83ECD6EB916A2F481A02E6B76A0A48'),
   ('published', '2012-05-05 17:03:50'),
@@ -352,26 +341,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_server_descriptor(attr = None, exclude = (), content = False):
-  """
-  Provides the descriptor content for...
-  stem.descriptor.server_descriptor.BridgeDescriptor
-
-  :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: BridgeDescriptor for the requested descriptor content
-  """
-
-  desc_content = _get_descriptor_content(attr, exclude, BRIDGE_SERVER_HEADER)
-
-  if content:
-    return desc_content
-  else:
-    return stem.descriptor.server_descriptor.BridgeDescriptor(desc_content, validate = True)
-
-
 def get_relay_extrainfo_descriptor(attr = None, exclude = (), content = False):
   """
   Provides the descriptor content for...
diff --git a/test/unit/descriptor/export.py b/test/unit/descriptor/export.py
index 009c43d..cd005d1 100644
--- a/test/unit/descriptor/export.py
+++ b/test/unit/descriptor/export.py
@@ -11,13 +11,9 @@ except ImportError:
 
 import stem.prereq
 
-from stem.descriptor.server_descriptor import RelayDescriptor
+from stem.descriptor.server_descriptor import RelayDescriptor, BridgeDescriptor
 from stem.descriptor.export import export_csv, export_csv_file
 
-from test.mocking import (
-  get_bridge_server_descriptor,
-)
-
 
 class TestExport(unittest.TestCase):
   def test_minimal_descriptor(self):
@@ -104,6 +100,4 @@ class TestExport(unittest.TestCase):
     Attempts to make a csv with multiple descriptor types.
     """
 
-    server_desc = RelayDescriptor.create()
-    bridge_desc = get_bridge_server_descriptor()
-    self.assertRaises(ValueError, export_csv, (server_desc, bridge_desc))
+    self.assertRaises(ValueError, export_csv, (RelayDescriptor.create(), BridgeDescriptor.create()))
diff --git a/test/unit/descriptor/server_descriptor.py b/test/unit/descriptor/server_descriptor.py
index 1b754bf..f30fed6 100644
--- a/test/unit/descriptor/server_descriptor.py
+++ b/test/unit/descriptor/server_descriptor.py
@@ -20,11 +20,6 @@ from stem.util import str_type
 from stem.descriptor.certificate import CertType, ExtensionType
 from stem.descriptor.server_descriptor import RelayDescriptor, BridgeDescriptor
 
-from test.mocking import (
-  get_bridge_server_descriptor,
-  CRYPTO_BLOB,
-)
-
 from test.unit.descriptor import get_resource
 
 try:
@@ -436,7 +431,7 @@ Qlx9HNCqCY877ztFRC624ja2ql6A2hBcuoYMbkHjcQ4=
     self.assertEqual('caerSidi', desc.nickname)
     self.assertEqual('71.35.133.197', desc.address)
     self.assertEqual(None, desc.fingerprint)
-    self.assertTrue(CRYPTO_BLOB in desc.onion_key)
+    self.assertTrue(stem.descriptor.CRYPTO_BLOB in desc.onion_key)
 
   def test_with_opt(self):
     """
@@ -722,7 +717,7 @@ Qlx9HNCqCY877ztFRC624ja2ql6A2hBcuoYMbkHjcQ4=
     Basic sanity check that we can parse a descriptor with minimal attributes.
     """
 
-    desc = get_bridge_server_descriptor()
+    desc = BridgeDescriptor.create()
 
     self.assertEqual('Unnamed', desc.nickname)
     self.assertEqual('10.45.227.253', desc.address)
@@ -744,13 +739,13 @@ Qlx9HNCqCY877ztFRC624ja2ql6A2hBcuoYMbkHjcQ4=
       {'contact': 'Damian'},
       {'or-address': '71.35.133.197:9001'},
       {'or-address': '[12ab:2e19:3bcf::02:9970]:9001'},
-      {'onion-key': '\n-----BEGIN RSA PUBLIC KEY-----%s-----END RSA PUBLIC KEY-----' % CRYPTO_BLOB},
-      {'signing-key': '\n-----BEGIN RSA PUBLIC KEY-----%s-----END RSA PUBLIC KEY-----' % CRYPTO_BLOB},
-      {'router-signature': '\n-----BEGIN SIGNATURE-----%s-----END SIGNATURE-----' % CRYPTO_BLOB},
+      {'onion-key': '\n-----BEGIN RSA PUBLIC KEY-----%s-----END RSA PUBLIC KEY-----' % stem.descriptor.CRYPTO_BLOB},
+      {'signing-key': '\n-----BEGIN RSA PUBLIC KEY-----%s-----END RSA PUBLIC KEY-----' % stem.descriptor.CRYPTO_BLOB},
+      {'router-signature': '\n-----BEGIN SIGNATURE-----%s-----END SIGNATURE-----' % stem.descriptor.CRYPTO_BLOB},
     ]
 
     for attr in unsanitized_attr:
-      desc = get_bridge_server_descriptor(attr)
+      desc = BridgeDescriptor.create(attr)
       self.assertFalse(desc.is_scrubbed())
 
   def test_bridge_unsanitized_relay(self):
@@ -771,12 +766,12 @@ Qlx9HNCqCY877ztFRC624ja2ql6A2hBcuoYMbkHjcQ4=
     # checks with valid content
 
     router_digest = '068A2E28D4C934D9490303B7A645BA068DCA0504'
-    desc = get_bridge_server_descriptor({'router-digest': router_digest})
+    desc = BridgeDescriptor.create({'router-digest': router_digest})
     self.assertEqual(router_digest, desc.digest())
 
     # checks when missing
 
-    desc_text = get_bridge_server_descriptor(exclude = ['router-digest'], content = True)
+    desc_text = BridgeDescriptor.content(exclude = ['router-digest'])
     self.assertRaises(ValueError, BridgeDescriptor, desc_text, True)
 
     # check that we can still construct it without validation
@@ -793,7 +788,7 @@ Qlx9HNCqCY877ztFRC624ja2ql6A2hBcuoYMbkHjcQ4=
     )
 
     for value in test_values:
-      desc_text = get_bridge_server_descriptor({'router-digest': value}, content = True)
+      desc_text = BridgeDescriptor.content({'router-digest': value})
       self.assertRaises(ValueError, BridgeDescriptor, desc_text, True)
 
       desc = BridgeDescriptor(desc_text, validate = False)
@@ -804,7 +799,7 @@ Qlx9HNCqCY877ztFRC624ja2ql6A2hBcuoYMbkHjcQ4=
     Constructs a bridge descriptor with a sanatized IPv4 or-address entry.
     """
 
-    desc = get_bridge_server_descriptor({'or-address': '10.45.227.253:9001'})
+    desc = BridgeDescriptor.create({'or-address': '10.45.227.253:9001'})
     self.assertEqual([('10.45.227.253', 9001, False)], desc.or_addresses)
 
   def test_or_address_v6(self):
@@ -812,7 +807,7 @@ Qlx9HNCqCY877ztFRC624ja2ql6A2hBcuoYMbkHjcQ4=
     Constructs a bridge descriptor with a sanatized IPv6 or-address entry.
     """
 
-    desc = get_bridge_server_descriptor({'or-address': '[fd9f:2e19:3bcf::02:9970]:9001'})
+    desc = BridgeDescriptor.create({'or-address': '[fd9f:2e19:3bcf::02:9970]:9001'})
     self.assertEqual([('fd9f:2e19:3bcf::02:9970', 9001, True)], desc.or_addresses)
 
   def test_or_address_multiple(self):
@@ -820,9 +815,11 @@ Qlx9HNCqCY877ztFRC624ja2ql6A2hBcuoYMbkHjcQ4=
     Constructs a bridge descriptor with multiple or-address entries and multiple ports.
     """
 
-    desc_text = b'\n'.join((get_bridge_server_descriptor(content = True),
-                            b'or-address 10.45.227.253:9001',
-                            b'or-address [fd9f:2e19:3bcf::02:9970]:443'))
+    desc_text = b'\n'.join((
+      BridgeDescriptor.content(),
+      b'or-address 10.45.227.253:9001',
+      b'or-address [fd9f:2e19:3bcf::02:9970]:443',
+    ))
 
     expected_or_addresses = [
       ('10.45.227.253', 9001, False),



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits