[tor-commits] [stem/master] Test index request failures

atagar at torproject.org atagar at torproject.org
Sat Aug 17 20:44:26 UTC 2019


commit d0ec0cdc476151a4e698867a47f1c3ffc4662fdb
Author: Damian Johnson <atagar at torproject.org>
Date:   Mon Dec 25 17:32:22 2017 -0800

    Test index request failures
---
 stem/descriptor/collector.py      | 14 +++++++++++---
 test/unit/descriptor/collector.py | 15 +++++++++++++++
 2 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/stem/descriptor/collector.py b/stem/descriptor/collector.py
index 5d65fd65..2360d827 100644
--- a/stem/descriptor/collector.py
+++ b/stem/descriptor/collector.py
@@ -100,7 +100,11 @@ class Compression(object):
 
     :returns: **bytes** with the decompressed content
 
-    :raises: **ImportError** if this method if decompression is unavalable
+    :raises:
+      If unable to decompress this provide...
+
+      * **IOError** if content isn't compressed with this
+      * **ImportError** if this method if decompression is unavalable
     """
 
     if not self.available:
@@ -190,7 +194,8 @@ class CollecTor(object):
     :raises:
       If unable to retrieve the index this provide...
 
-        * **ValueError** if the index is malformed
+        * **ValueError** if json is malformed
+        * **IOError** if unable to decompress
         * **socket.timeout** if our request timed out
         * **urllib2.URLError** for most request failures
     """
@@ -201,7 +206,10 @@ class CollecTor(object):
       response = urllib.urlopen(url('index', self.compression), timeout = self.timeout).read()
 
       if self.compression:
-        response = self.compression.decompress(response)
+        try:
+          response = self.compression.decompress(response)
+        except Exception as exc:
+          raise IOError('Unable to decompress response as %s: %s' % (self.compression, exc))
 
       self._cached_index = json.loads(stem.util.str_tools._to_unicode(response))
       self._cached_index_at = time.time()
diff --git a/test/unit/descriptor/collector.py b/test/unit/descriptor/collector.py
index 01f8f6ee..a0f8cb2e 100644
--- a/test/unit/descriptor/collector.py
+++ b/test/unit/descriptor/collector.py
@@ -36,3 +36,18 @@ class TestCollector(unittest.TestCase):
 
     collector = CollecTor(compression = None)
     self.assertEqual(expected, collector.index())
+
+  @patch(URL_OPEN, Mock(return_value = io.BytesIO(b'not json')))
+  def test_index_malformed_json(self):
+    collector = CollecTor(compression = None)
+
+    if stem.prereq.is_python_3():
+      self.assertRaisesRegexp(ValueError, 'Expecting value: line 1 column 1', collector.index)
+    else:
+      self.assertRaisesRegexp(ValueError, 'No JSON object could be decoded', collector.index)
+
+  def test_index_malformed_compression(self):
+    for compression in (GZIP, BZ2, LZMA):
+      with patch(URL_OPEN, Mock(return_value = io.BytesIO(b'not compressed'))):
+        collector = CollecTor(compression = compression)
+        self.assertRaisesRegexp(IOError, 'Unable to decompress response as %s' % compression, collector.index)





More information about the tor-commits mailing list