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

[tor-commits] [stem/master] Test check_digests example



commit 941c408c3a5c608e8272e629c61387f80ff1e2d4
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Tue Sep 22 18:42:15 2020 -0700

    Test check_digests example
    
    Yay! Caught our second example bug, this time a minor python 3 error with
    regard to iterating over a filter object.
---
 docs/_static/example/check_digests.py | 11 +++++--
 test/unit/examples.py                 | 54 ++++++++++++++++++++++++++++++++++-
 2 files changed, 61 insertions(+), 4 deletions(-)

diff --git a/docs/_static/example/check_digests.py b/docs/_static/example/check_digests.py
index 69f509cf..93b037c0 100644
--- a/docs/_static/example/check_digests.py
+++ b/docs/_static/example/check_digests.py
@@ -16,7 +16,7 @@ def download_descriptors(fingerprint):
   server_desc_query = stem.descriptor.remote.get_server_descriptors(fingerprint)
   extrainfo_query = stem.descriptor.remote.get_extrainfo_descriptors(fingerprint)
 
-  router_status_entries = filter(lambda desc: desc.fingerprint == fingerprint, conensus_query.run())
+  router_status_entries = list(filter(lambda desc: desc.fingerprint == fingerprint, conensus_query.run()))
 
   if len(router_status_entries) != 1:
     raise OSError("Unable to find relay '%s' in the consensus" % fingerprint)
@@ -27,8 +27,8 @@ def download_descriptors(fingerprint):
     extrainfo_query.run()[0],
   )
 
-if __name__ == '__main__':
-  fingerprint = raw_input("What relay fingerprint would you like to validate?\n")
+
+def validate_relay(fingerprint):
   print('')  # blank line
 
   if not stem.util.tor_tools.is_valid_fingerprint(fingerprint):
@@ -50,3 +50,8 @@ if __name__ == '__main__':
     print("Extrainfo descriptor digest is correct")
   else:
     print("Extrainfo descriptor digest invalid, expected %s but is %s" % (server_desc.extra_info_digest, extrainfo_desc.digest()))
+
+
+if __name__ == '__main__':
+  fingerprint = raw_input("What relay fingerprint would you like to validate?\n")
+  validate_relay(fingerprint)
diff --git a/test/unit/examples.py b/test/unit/examples.py
index 2f664fd3..ba75a98c 100644
--- a/test/unit/examples.py
+++ b/test/unit/examples.py
@@ -2,6 +2,8 @@
 Exercise the code in our examples directory.
 """
 
+import base64
+import binascii
 import importlib
 import io
 import os
@@ -11,9 +13,13 @@ import unittest
 import stem.socket
 import stem.util.system
 import test
+import test.require
 
 from stem.control import Controller
 from stem.descriptor.bandwidth_file import BandwidthFile
+from stem.descriptor.extrainfo_descriptor import RelayExtraInfoDescriptor
+from stem.descriptor.router_status_entry import RouterStatusEntryV3
+from stem.descriptor.server_descriptor import RelayDescriptor
 from stem.response import ControlMessage
 from unittest.mock import Mock, patch
 
@@ -69,6 +75,16 @@ Finished measure_fraction_relays_exit_80_microdescriptors('%s')
   Time per microdescriptor:
 """.rstrip()
 
+EXPECTED_CHECK_DIGESTS_OK = """
+Server descriptor digest is correct
+Extrainfo descriptor digest is correct
+"""
+
+EXPECTED_CHECK_DIGESTS_BAD = """
+Server descriptor digest invalid, expected A106452D87BD7B803B6CE916291ED368DC5BD091 but is %s
+Extrainfo descriptor digest is correct
+"""
+
 
 def import_example(module_name):
   """
@@ -174,8 +190,44 @@ class TestExamples(unittest.TestCase):
 
     self.assertEqual('start of broken_handler\n', stdout_mock.getvalue())
 
+  @test.require.cryptography
   def test_check_digests(self):
-    pass
+    def download_of(desc):
+      query = Mock()
+      query.run.return_value = [desc]
+      return Mock(return_value = query)
+
+    module = import_example('check_digests')
+    fingerprint = 'A7569A83B5706AB1B1A9CB52EFF7D2D32E4553EB'
+
+    extrainfo_desc = RelayExtraInfoDescriptor.create()
+    server_desc = RelayDescriptor.create({'extra-info-digest': extrainfo_desc.digest()}, sign = True)
+
+    encoded_digest = base64.b64encode(binascii.unhexlify(server_desc.digest())).rstrip(b'=')
+
+    consensus_desc = RouterStatusEntryV3.create({
+      'r': 'caerSidi p1aag7VwarGxqctS7/fS0y5FU+s %s 2012-08-06 11:19:31 71.35.150.29 9001 0' % encoded_digest.decode('utf-8'),
+    })
+
+    bad_consensus_desc = RouterStatusEntryV3.create({
+      'r': 'caerSidi p1aag7VwarGxqctS7/fS0y5FU+s oQZFLYe9e4A7bOkWKR7TaNxb0JE 2012-08-06 11:19:31 71.35.150.29 9001 0',
+    })
+
+    with patch('stem.descriptor.remote.get_server_descriptors', download_of(server_desc)):
+      with patch('stem.descriptor.remote.get_extrainfo_descriptors', download_of(extrainfo_desc)):
+        # correctly signed descriptors
+
+        with patch('stem.descriptor.remote.get_consensus', download_of(consensus_desc)):
+          with patch('sys.stdout', new_callable = io.StringIO) as stdout_mock:
+            module.validate_relay(fingerprint)
+            self.assertEqual(EXPECTED_CHECK_DIGESTS_OK, stdout_mock.getvalue())
+
+        # incorrect server descriptor digest
+
+        with patch('stem.descriptor.remote.get_consensus', download_of(bad_consensus_desc)):
+          with patch('sys.stdout', new_callable = io.StringIO) as stdout_mock:
+            module.validate_relay(fingerprint)
+            self.assertEqual(EXPECTED_CHECK_DIGESTS_BAD % server_desc.digest(), stdout_mock.getvalue())
 
   def test_client_usage_using_pycurl(self):
     pass



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