commit d990698937d91fa068fa11fbebc7b01a5e465cea Author: Damian Johnson atagar@torproject.org Date: Sun Jun 8 16:59:12 2014 -0700
Always skip _verify_digest() when making mock descriptors
When making mock descriptors for our tests we pretty much never want to run its _verify_digest() since that makes our tests balk on the fake content. We were mocking out _verify_digest() calls in each test, but it's a lot simpler to just do this in the funciton that provides us the mock data. --- test/mocking.py | 11 ++++++++++- test/unit/descriptor/export.py | 12 ------------ test/unit/descriptor/server_descriptor.py | 8 -------- test/unit/tutorial.py | 1 - 4 files changed, 10 insertions(+), 22 deletions(-)
diff --git a/test/mocking.py b/test/mocking.py index f139548..057541d 100644 --- a/test/mocking.py +++ b/test/mocking.py @@ -49,6 +49,12 @@ import stem.prereq import stem.response import stem.util.str_tools
+try: + # added in python 3.3 + from unittest.mock import Mock, patch +except ImportError: + from mock import Mock, patch + CRYPTO_BLOB = """ MIGJAoGBAJv5IIWQ+WDWYUdyA/0L8qbIkEVH/cwryZWoIaPAzINfrw1WfNZGtBmg skFtXhOHHqTRN4GPPrZsAIUOQGzQtGb66IQgT4tO/pj+P6QmSCCdTfhvGfgTCsC+ @@ -347,7 +353,10 @@ def get_relay_server_descriptor(attr = None, exclude = (), content = False, sign if sign_content: desc_content = sign_descriptor_content(desc_content)
- return stem.descriptor.server_descriptor.RelayDescriptor(desc_content, validate = True) + with patch('stem.descriptor.server_descriptor.RelayDescriptor._verify_digest', Mock()): + desc = stem.descriptor.server_descriptor.RelayDescriptor(desc_content, validate = True) + + return desc
def get_bridge_server_descriptor(attr = None, exclude = (), content = False): diff --git a/test/unit/descriptor/export.py b/test/unit/descriptor/export.py index 68b4f29..55429fd 100644 --- a/test/unit/descriptor/export.py +++ b/test/unit/descriptor/export.py @@ -13,15 +13,8 @@ from stem.descriptor.export import export_csv, export_csv_file from test.mocking import get_relay_server_descriptor, \ get_bridge_server_descriptor
-try: - # added in python 3.3 - from unittest.mock import Mock, patch -except ImportError: - from mock import Mock, patch -
class TestExport(unittest.TestCase): - @patch('stem.descriptor.server_descriptor.RelayDescriptor._verify_digest', Mock()) def test_minimal_descriptor(self): """ Exports a single minimal tor server descriptor. @@ -42,7 +35,6 @@ class TestExport(unittest.TestCase): expected = 'nickname,address,published\n' + expected self.assertEquals(expected, desc_csv)
- @patch('stem.descriptor.server_descriptor.RelayDescriptor._verify_digest', Mock()) def test_multiple_descriptors(self): """ Exports multiple descriptors, making sure that we get them back in the same @@ -59,7 +51,6 @@ class TestExport(unittest.TestCase): expected = '\n'.join(nicknames) + '\n' self.assertEqual(expected, export_csv(descriptors, included_fields = ('nickname',), header = False))
- @patch('stem.descriptor.server_descriptor.RelayDescriptor._verify_digest', Mock()) def test_file_output(self): """ Basic test for the export_csv_file() function, checking that it provides @@ -74,7 +65,6 @@ class TestExport(unittest.TestCase):
self.assertEqual(desc_csv, csv_buffer.getvalue())
- @patch('stem.descriptor.server_descriptor.RelayDescriptor._verify_digest', Mock()) def test_excludes_private_attr(self): """ Checks that the default attributes for our csv output doesn't include private fields. @@ -99,7 +89,6 @@ class TestExport(unittest.TestCase):
self.assertEquals('', export_csv([]))
- @patch('stem.descriptor.server_descriptor.RelayDescriptor._verify_digest', Mock()) def test_invalid_attributes(self): """ Attempts to make a csv with attributes that don't exist. @@ -108,7 +97,6 @@ class TestExport(unittest.TestCase): desc = get_relay_server_descriptor() self.assertRaises(ValueError, export_csv, desc, ('nickname', 'blarg!'))
- @patch('stem.descriptor.server_descriptor.RelayDescriptor._verify_digest', Mock()) def test_multiple_descriptor_types(self): """ Attempts to make a csv with multiple descriptor types. diff --git a/test/unit/descriptor/server_descriptor.py b/test/unit/descriptor/server_descriptor.py index c51de06..4d01219 100644 --- a/test/unit/descriptor/server_descriptor.py +++ b/test/unit/descriptor/server_descriptor.py @@ -26,7 +26,6 @@ except ImportError:
class TestServerDescriptor(unittest.TestCase): - @patch('stem.descriptor.server_descriptor.RelayDescriptor._verify_digest', Mock()) def test_minimal_relay_descriptor(self): """ Basic sanity check that we can parse a relay server descriptor with minimal @@ -40,7 +39,6 @@ class TestServerDescriptor(unittest.TestCase): self.assertEquals(None, desc.fingerprint) self.assertTrue(CRYPTO_BLOB in desc.onion_key)
- @patch('stem.descriptor.server_descriptor.RelayDescriptor._verify_digest', Mock()) def test_with_opt(self): """ Includes an 'opt <keyword> <value>' entry. @@ -49,7 +47,6 @@ class TestServerDescriptor(unittest.TestCase): desc = get_relay_server_descriptor({'opt': 'contact www.atagar.com/contact/'}) self.assertEquals(b'www.atagar.com/contact/', desc.contact)
- @patch('stem.descriptor.server_descriptor.RelayDescriptor._verify_digest', Mock()) def test_unrecognized_line(self): """ Includes unrecognized content in the descriptor. @@ -144,7 +141,6 @@ class TestServerDescriptor(unittest.TestCase): desc = RelayDescriptor(desc_text, validate = False) self.assertEquals(b'', desc.platform)
- @patch('stem.descriptor.server_descriptor.RelayDescriptor._verify_digest', Mock()) def test_platform_for_node_tor(self): """ Parse a platform line belonging to a node-Tor relay. @@ -185,7 +181,6 @@ class TestServerDescriptor(unittest.TestCase): desc_text = get_relay_server_descriptor({'published': '2012-01-01'}, content = True) self._expect_invalid_attr(desc_text, 'published')
- @patch('stem.descriptor.server_descriptor.RelayDescriptor._verify_digest', Mock()) def test_read_and_write_history(self): """ Parses a read-history and write-history entry. This is now a deprecated @@ -209,7 +204,6 @@ class TestServerDescriptor(unittest.TestCase): self.assertEquals(900, attr[1]) self.assertEquals(expected_values, attr[2])
- @patch('stem.descriptor.server_descriptor.RelayDescriptor._verify_digest', Mock()) def test_read_history_empty(self): """ Parses a read-history with an empty value. @@ -288,7 +282,6 @@ class TestServerDescriptor(unittest.TestCase): desc_text = get_relay_server_descriptor({'opt fingerprint': fingerprint}, content = True) self._expect_invalid_attr(desc_text, 'fingerprint', fingerprint.replace(' ', ''))
- @patch('stem.descriptor.server_descriptor.RelayDescriptor._verify_digest', Mock()) def test_ipv6_policy(self): """ Checks a 'ipv6-policy' line. @@ -298,7 +291,6 @@ class TestServerDescriptor(unittest.TestCase): desc = get_relay_server_descriptor({'ipv6-policy': 'accept 22-23,53,80,110'}) self.assertEquals(expected, desc.exit_policy_v6)
- @patch('stem.descriptor.server_descriptor.RelayDescriptor._verify_digest', Mock()) def test_ntor_onion_key(self): """ Checks a 'ntor-onion-key' line. diff --git a/test/unit/tutorial.py b/test/unit/tutorial.py index f666d4c..1c853c0 100644 --- a/test/unit/tutorial.py +++ b/test/unit/tutorial.py @@ -107,7 +107,6 @@ class TestTutorial(unittest.TestCase):
@patch('sys.stdout', new_callable = StringIO.StringIO) @patch('stem.descriptor.reader.DescriptorReader', spec = DescriptorReader) - @patch('stem.descriptor.server_descriptor.RelayDescriptor._verify_digest', Mock()) def test_mirror_mirror_on_the_wall_4(self, reader_mock, stdout_mock): def tutorial_example(): from stem.descriptor.reader import DescriptorReader