[tor-commits] [stem/master] Integ test to exercise each of the authorities

atagar at torproject.org atagar at torproject.org
Mon Jul 22 03:10:17 UTC 2013


commit dc6632b5afba37111b9b2d6fb0c316cca1d9e046
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun Jul 14 11:44:57 2013 -0700

    Integ test to exercise each of the authorities
    
    Adding an integration test that fetches moria1's descriptor from each of the
    directory authorities. This obviously requires network activity so it's run via
    the ONLINE target.
    
    This uncovered a couple interesting finds...
    
    * This is our first time actually using urllib2 (it was mocked previously), and
      its 'file like object' is incomplete. It lacks tell() and seek() which means
      that our parsers don't have random access. As such we need to read this all
      into memory and use a BytesIO.... damn.
    
    * The moria1 authority has an extra newline in its descriptor content. Why?
      Haven't a clue. Maybe it's running a different version of tor or maybe this
      is an artifact of fetching its own descriptor. Whatever it is, it's the only
      authority to do so. Simply stripping the content to avoid having our parser
      complain about this.
---
 stem/descriptor/remote.py       |    8 +++++++
 test/integ/descriptor/remote.py |   44 +++++++++++++++++++++++++++++++++++++++
 test/settings.cfg               |    1 +
 3 files changed, 53 insertions(+)

diff --git a/stem/descriptor/remote.py b/stem/descriptor/remote.py
index ed86eda..3db72ea 100644
--- a/stem/descriptor/remote.py
+++ b/stem/descriptor/remote.py
@@ -39,6 +39,7 @@ itself...
       print "  %s (%s)" % (desc.nickname, desc.fingerprint)
 """
 
+import io
 import sys
 import threading
 import time
@@ -178,6 +179,13 @@ class Query(object):
       response = urllib2.urlopen(self.get_url(), timeout = self.timeout)
       self.runtime = time.time() - self.start_time
 
+      # This sucks. We need to read the full response into memory before
+      # processing the content. This is because urllib2 returns a 'file like'
+      # object that lacks tell() or seek(). Hence we need to read it into our
+      # own buffer that does support these.
+
+      response = io.BytesIO(response.read().strip())
+
       self._results = stem.descriptor.parse_file(response, self.descriptor_type)
     except:
       self.error = sys.exc_info()[1]
diff --git a/test/integ/descriptor/remote.py b/test/integ/descriptor/remote.py
new file mode 100644
index 0000000..7ac6757
--- /dev/null
+++ b/test/integ/descriptor/remote.py
@@ -0,0 +1,44 @@
+"""
+Integration tests for stem.descriptor.remote.
+"""
+
+import unittest
+
+import stem.descriptor.remote
+import test.runner
+
+# Required to prevent unmarshal error when running this test alone.
+
+import stem.descriptor.networkstatus
+
+class TestDescriptorReader(unittest.TestCase):
+  def test_using_authorities(self):
+    """
+    Fetches a descriptor from each of the directory authorities. This is
+    intended to check that DIRECTORY_AUTHORITIES is still up to date (that
+    addresses and ports haven't changed).
+
+    This is hardcoded to fetch moria1's descriptor. If its fingerprint changes
+    then this test will need to be updated.
+    """
+
+    if test.runner.require_online(self):
+      return
+
+    for authority, (address, dirport) in stem.descriptor.remote.DIRECTORY_AUTHORITIES.items():
+      query = stem.descriptor.remote.Query(
+        address,
+        dirport,
+        '/tor/server/fp/9695DFC35FFEB861329B9F1AB04C46397020CE31',
+        'server-descriptor 1.0',
+        30,
+      )
+
+      try:
+        descriptors = list(query.run())
+      except Exception, exc:
+        self.fail("Unable to use %s (%s:%s, %s): %s" % (authority, address, dirport, type(exc), exc))
+
+      self.assertEqual(1, len(descriptors))
+      self.assertEqual('moria1', descriptors[0].nickname)
+
diff --git a/test/settings.cfg b/test/settings.cfg
index 68b120d..d0fd5b0 100644
--- a/test/settings.cfg
+++ b/test/settings.cfg
@@ -187,6 +187,7 @@ test.integ_tests
 |test.integ.util.proc.TestProc
 |test.integ.util.system.TestSystem
 |test.integ.descriptor.reader.TestDescriptorReader
+|test.integ.descriptor.remote.TestDescriptorReader
 |test.integ.descriptor.server_descriptor.TestServerDescriptor
 |test.integ.descriptor.extrainfo_descriptor.TestExtraInfoDescriptor
 |test.integ.descriptor.microdescriptor.TestMicrodescriptor





More information about the tor-commits mailing list