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

[tor-commits] [stem/master] Unit test descriptor compression



commit 522422427ec2b8427f9eb7baefb6261941772e85
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sat Mar 31 12:00:54 2018 -0700

    Unit test descriptor compression
    
    Mocking download responses so we can add unit test coverage of our compression
    support.
---
 test/unit/descriptor/__init__.py              |   9 +++
 test/unit/descriptor/data/compressed_gzip     | Bin 0 -> 1543 bytes
 test/unit/descriptor/data/compressed_identity |  52 +++++++++++++++++
 test/unit/descriptor/data/compressed_lzma     |   0
 test/unit/descriptor/data/compressed_zstd     | Bin 0 -> 1550 bytes
 test/unit/descriptor/remote.py                |  77 +++++++++++++++++++++++++-
 6 files changed, 136 insertions(+), 2 deletions(-)

diff --git a/test/unit/descriptor/__init__.py b/test/unit/descriptor/__init__.py
index bf0e8619..bce8d3d0 100644
--- a/test/unit/descriptor/__init__.py
+++ b/test/unit/descriptor/__init__.py
@@ -25,6 +25,15 @@ def get_resource(filename):
   return os.path.join(DESCRIPTOR_TEST_DATA, filename)
 
 
+def read_resource(filename):
+  """
+  Provides test data.
+  """
+
+  with open(get_resource(filename), 'rb') as resource_file:
+    return resource_file.read()
+
+
 def base_expect_invalid_attr(cls, default_attr, default_value, test, desc_attrs, attr = None, expected_value = None):
   return base_expect_invalid_attr_for_text(cls, default_attr, default_value, test, cls.content(desc_attrs), attr, expected_value)
 
diff --git a/test/unit/descriptor/data/compressed_gzip b/test/unit/descriptor/data/compressed_gzip
new file mode 100644
index 00000000..2b2dc642
Binary files /dev/null and b/test/unit/descriptor/data/compressed_gzip differ
diff --git a/test/unit/descriptor/data/compressed_identity b/test/unit/descriptor/data/compressed_identity
new file mode 100644
index 00000000..cd5b56b4
--- /dev/null
+++ b/test/unit/descriptor/data/compressed_identity
@@ -0,0 +1,52 @@
+router moria1 128.31.0.34 9101 0 9131
+identity-ed25519
+-----BEGIN ED25519 CERT-----
+AQQABnxNAQS9ja600v/ZodOUiu7NepTkbPIOrFPgEVQE+03rGBtPAQAgBADKnR/C
+2nhpr9UzJkkbPy83sqbfNh63VgFnCpkSTULAcq52z8xM7raRDCiTJTu/FK/BJGgE
+dJcFQ8MgZJOuYgFKcMVyQ6j2FGbhDI0zQTK1+TAPNRG4ixiF7h7wqDT9Ugw=
+-----END ED25519 CERT-----
+master-key-ed25519 yp0fwtp4aa/VMyZJGz8vN7Km3zYet1YBZwqZEk1CwHI
+platform Tor 0.3.4.0-alpha-dev on Linux
+proto Cons=1-2 Desc=1-2 DirCache=1-2 HSDir=1-2 HSIntro=3-4 HSRend=1-2 Link=1-5 LinkAuth=1,3 Microdesc=1-2 Relay=1-2
+published 2018-03-31 04:17:41
+fingerprint 9695 DFC3 5FFE B861 329B 9F1A B04C 4639 7020 CE31
+uptime 295196
+bandwidth 512000 62914560 3403447
+extra-info-digest 393861CB4D9A0480E5A58A3005A88DD7F09271E3 CCbwxMembtsEKAVkU2bqCiBhKaokRlJv6077uy5kI4Q
+caches-extra-info
+onion-key
+-----BEGIN RSA PUBLIC KEY-----
+MIGJAoGBAKsNybH/i/nMLDwSr6WYd77EV6IgiuhfD7UBbsifsDywTe+YrZU3Z4O+
+DY9jemMPRA7wYioJrmXyMVTsbktjWUtWFge1oUj2xsxO3cufCavLmmkmqpkHL3Wn
+xRJraupgfLK72/UN8wXB0duSAU/DH9hTg7WRhkFZdRJK7rostJvZAgMBAAE=
+-----END RSA PUBLIC KEY-----
+signing-key
+-----BEGIN RSA PUBLIC KEY-----
+MIGJAoGBALtJ9uD7cD7iHjqNA3AgsX9prES5QN+yFQyr2uOkxzhvunnaf6SNhzWW
+bkfylnMrRm/qCz/czcjZO6N6EKHcXmypehvP566B7gAQ9vDsb+l7VZVWgXvzNc2s
+tl3P7qpC08rgyJh1GqmtQTCesIDqkEyWxwToympCt09ZQRq+fIttAgMBAAE=
+-----END RSA PUBLIC KEY-----
+onion-key-crosscert
+-----BEGIN CROSSCERT-----
+A9x30r1LTl6rRUe4/irB6/cdNGmd95+bOuArjrmuwSI8Crerx3uNbbN8/iRlRSec
+fBqElxb8SvEGONvtyjGVxp+t5QQS381Bah7DRMz7MfRvOgD8UoZ4DEcpMBnJnA+O
+5OnAoBVOProdfUvFfEnBWJY+ELB3ShG4A3TL4c8QrwA=
+-----END CROSSCERT-----
+ntor-onion-key-crosscert 0
+-----BEGIN ED25519 CERT-----
+AQoABnadAcqdH8LaeGmv1TMmSRs/Lzeypt82HrdWAWcKmRJNQsByAEnIlLlEtELa
+EZA/y3Q5wXxA20upqmSKoCt1s8a6cXHqnsbha61MVbRLVCgWlWmKNjBSu+GiXeXC
+IJ9Tr+aYawM=
+-----END ED25519 CERT-----
+hidden-service-dir
+contact 1024D/28988BF5 arma mit edu
+ntor-onion-key a3r4k2huGP4YdHTQ8ldJhZkm5O+1tLaKE//o5DSibU0=
+reject *:*
+tunnelled-dir-server
+router-sig-ed25519 FkY6h3SMPWPt1fQlU8jsFllGWIy+lw7SGTjTV2FZVNlrHYHcS1F2L7bMGu38ljZ7J+VIBssRZnaiQVEXbI9uDQ
+router-signature
+-----BEGIN SIGNATURE-----
+UzHGCcQK1XGntL3MVvcgcj0kpCsEMf5UIEj4eWOtCjWyriOX7nqwayc5vWOubZLN
+pAZ27MjVmMbVGgtS0xFZ/UQrGEeznITaZwjLNd1IOliadKPdOp4w4OFcjbkCytk6
+MdAEyxwnZzxUwCex0Kj+3FWXQTr09VnXMg5/+vdbCUY=
+-----END SIGNATURE-----
diff --git a/test/unit/descriptor/data/compressed_lzma b/test/unit/descriptor/data/compressed_lzma
new file mode 100644
index 00000000..e69de29b
diff --git a/test/unit/descriptor/data/compressed_zstd b/test/unit/descriptor/data/compressed_zstd
new file mode 100644
index 00000000..b3c3269b
Binary files /dev/null and b/test/unit/descriptor/data/compressed_zstd differ
diff --git a/test/unit/descriptor/remote.py b/test/unit/descriptor/remote.py
index 41585fc2..05cf1d57 100644
--- a/test/unit/descriptor/remote.py
+++ b/test/unit/descriptor/remote.py
@@ -11,6 +11,7 @@ import stem.prereq
 import stem.util.conf
 
 from stem.descriptor.remote import Compression
+from test.unit.descriptor import read_resource
 
 try:
   # added in python 2.7
@@ -110,14 +111,18 @@ FALLBACK_ENTRY = b"""\
 """
 
 
-def _urlopen_mock(data):
+def _urlopen_mock(data, encoding = 'identity'):
   urlopen_mock = Mock()
   urlopen_mock().read.return_value = data
-  urlopen_mock().info().getheader.return_value = 'identity'
+  urlopen_mock().info().getheader.return_value = encoding
   return urlopen_mock
 
 
 class TestDescriptorDownloader(unittest.TestCase):
+  def tearDown(self):
+    # prevent our mocks from impacting other tests
+    stem.descriptor.remote.SINGLETON_DOWNLOADER = None
+
   def test_gzip_url_override(self):
     query = stem.descriptor.remote.Query(TEST_RESOURCE, start = False)
     self.assertEqual([Compression.PLAINTEXT], query.compression)
@@ -145,6 +150,74 @@ class TestDescriptorDownloader(unittest.TestCase):
       query = stem.descriptor.remote.Query(TEST_RESOURCE, compression = Compression.LZMA, start = False)
       self.assertEqual([Compression.PLAINTEXT], query.compression)
 
+  @patch(URL_OPEN, _urlopen_mock(read_resource('compressed_identity'), encoding = 'identity'))
+  def test_compression_plaintext(self):
+    """
+    Download a plaintext descriptor.
+    """
+
+    descriptors = list(stem.descriptor.remote.get_server_descriptors(
+      '9695DFC35FFEB861329B9F1AB04C46397020CE31',
+      compression = Compression.PLAINTEXT,
+      validate = True,
+    ))
+
+    self.assertEqual(1, len(descriptors))
+    self.assertEqual('moria1', descriptors[0].nickname)
+
+  @patch(URL_OPEN, _urlopen_mock(read_resource('compressed_gzip'), encoding = 'gzip'))
+  def test_compression_gzip(self):
+    """
+    Download a gip compressed descriptor.
+    """
+
+    descriptors = list(stem.descriptor.remote.get_server_descriptors(
+      '9695DFC35FFEB861329B9F1AB04C46397020CE31',
+      compression = Compression.GZIP,
+      validate = True,
+    ))
+
+    self.assertEqual(1, len(descriptors))
+    self.assertEqual('moria1', descriptors[0].nickname)
+
+  @patch(URL_OPEN, _urlopen_mock(read_resource('compressed_zstd'), encoding = 'x-zstd'))
+  def test_compression_zstd(self):
+    """
+    Download a zstd compressed descriptor.
+    """
+
+    if not stem.descriptor.remote.ZSTD_SUPPORTED:
+      self.skipTest('(requires zstd module)')
+      return
+
+    descriptors = list(stem.descriptor.remote.get_server_descriptors(
+      '9695DFC35FFEB861329B9F1AB04C46397020CE31',
+      compression = Compression.ZSTD,
+      validate = True,
+    ))
+
+    self.assertEqual(1, len(descriptors))
+    self.assertEqual('moria1', descriptors[0].nickname)
+
+  @patch(URL_OPEN, _urlopen_mock(read_resource('compressed_lzma'), encoding = 'x-tor-lzma'))
+  def test_compression_lzma(self):
+    """
+    Download a lzma compressed descriptor.
+    """
+
+    if not stem.descriptor.remote.LZMA_SUPPORTED:
+      self.skipTest('(requires lzma module)')
+      return
+
+    descriptors = list(stem.descriptor.remote.get_server_descriptors(
+      '9695DFC35FFEB861329B9F1AB04C46397020CE31',
+      compression = Compression.LZMA,
+      validate = True,
+    ))
+
+    self.assertEqual(1, len(descriptors))
+    self.assertEqual('moria1', descriptors[0].nickname)
+
   @patch(URL_OPEN, _urlopen_mock(TEST_DESCRIPTOR))
   def test_query_download(self):
     """



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