[tor-commits] [ooni-probe/master] Fixed bug when a deck was missing some argument of a test. Also fixed some various quirks

art at torproject.org art at torproject.org
Mon Jul 7 11:07:37 UTC 2014


commit a9a24464b4b61800fe5787497db9f5ca11ce4855
Author: kudrom <kudrom at riseup.net>
Date:   Sat Jun 28 00:16:43 2014 +0200

    Fixed bug when a deck was missing some argument of a test. Also fixed some various quirks
---
 ooni/deck.py                                       |    2 +-
 ooni/errors.py                                     |   12 ++++++-
 ooni/nettest.py                                    |    9 +++--
 .../manipulation/http_header_field_manipulation.py |   12 +++----
 ooni/oonicli.py                                    |    7 ++--
 ooni/tests/test_nettest.py                         |   36 +++++++++-----------
 6 files changed, 45 insertions(+), 33 deletions(-)

diff --git a/ooni/deck.py b/ooni/deck.py
index f38b098..adf1d69 100644
--- a/ooni/deck.py
+++ b/ooni/deck.py
@@ -144,7 +144,7 @@ class Deck(InputFile):
             net_test_loader.checkOptions()
             if net_test_loader.requiresTor:
                 self.requiresTor = True
-        except e.MissingRequiredOption, missing_options:
+        except e.MissingRequiredOption as missing_options:
             if not self.bouncer:
                 raise
             for missing_option in missing_options.message:
diff --git a/ooni/errors.py b/ooni/errors.py
index d5673ed..d8c0911 100644
--- a/ooni/errors.py
+++ b/ooni/errors.py
@@ -6,6 +6,8 @@ from twisted.internet.error import ConnectionRefusedError, TCPTimedOutError
 from twisted.internet.error import DNSLookupError, ConnectError, ConnectionLost
 from twisted.internet.error import TimeoutError as GenericTimeoutError
 
+from twisted.python import usage
+
 from txsocksx.errors import SOCKSError
 from txsocksx.errors import MethodsNotAcceptedError, AddressNotSupported
 from txsocksx.errors import ConnectionError, NetworkUnreachable
@@ -241,8 +243,16 @@ class NetTestNotFound(Exception):
 
 
 class MissingRequiredOption(Exception):
-    pass
+    def __init__(self, message, net_test_loader):
+        super(MissingRequiredOption, self).__init__()
+        self.net_test_loader = net_test_loader
+        self.message = message
+
 
+class OONIUsageError(usage.UsageError):
+    def __init__(self, net_test_loader):
+        super(OONIUsageError, self).__init__()
+        self.net_test_loader = net_test_loader
 
 class FailureToLoadNetTest(Exception):
     pass
diff --git a/ooni/nettest.py b/ooni/nettest.py
index 1547617..eccde78 100644
--- a/ooni/nettest.py
+++ b/ooni/nettest.py
@@ -1,6 +1,7 @@
 import os
 import re
 import time
+import sys
 from hashlib import sha256
 
 from twisted.internet import defer
@@ -342,7 +343,11 @@ class NetTestLoader(object):
         """
         for klass in self.testClasses:
             options = self.usageOptions()
-            options.parseOptions(self.options)
+            try:
+                options.parseOptions(self.options)
+            except usage.UsageError:
+                tb = sys.exc_info()[2]
+                raise e.OONIUsageError(self), None, tb
 
             if options:
                 klass.localOptions = options
@@ -764,7 +769,7 @@ class NetTestCase(object):
                     self.localOptions[required_option] is None:
                 missing_options.append(required_option)
         if missing_options:
-            raise e.MissingRequiredOption(missing_options)
+            raise e.MissingRequiredOption(missing_options, self)
 
     def __repr__(self):
         return "<%s inputs=%s>" % (self.__class__, self.inputs)
diff --git a/ooni/nettests/manipulation/http_header_field_manipulation.py b/ooni/nettests/manipulation/http_header_field_manipulation.py
index 8378924..4e47040 100644
--- a/ooni/nettests/manipulation/http_header_field_manipulation.py
+++ b/ooni/nettests/manipulation/http_header_field_manipulation.py
@@ -50,7 +50,7 @@ class HTTPHeaderFieldManipulation(httpt.HTTPTest):
     description = "Checks if the HTTP request the server " \
                   "sees is the same as the one that the client has created."
     author = "Arturo Filastò"
-    version = "0.1.4"
+    version = "0.1.5"
 
     randomizeUA = False
     usageOptions = UsageOptions
@@ -60,6 +60,10 @@ class HTTPHeaderFieldManipulation(httpt.HTTPTest):
     requiresTor = False
     requiresRoot = False
 
+    def setUp(self):
+        super(HTTPHeaderFieldManipulation, self).setUp()
+        self.url = self.localOptions['backend']
+
     def get_headers(self):
         headers = {}
         if self.localOptions['headers']:
@@ -96,12 +100,6 @@ class HTTPHeaderFieldManipulation(httpt.HTTPTest):
             headers[new_key] = v
         return headers
 
-    def processInputs(self):
-        if self.localOptions['backend']:
-            self.url = self.localOptions['backend']
-        else:
-            raise Exception("No backend specified")
-
     def processResponseBody(self, data):
         self.check_for_tampering(data)
 
diff --git a/ooni/oonicli.py b/ooni/oonicli.py
index f84ac2d..8313822 100644
--- a/ooni/oonicli.py
+++ b/ooni/oonicli.py
@@ -183,14 +183,15 @@ def runWithDirector(logging=True, start_tor=True):
             deck.insert(net_test_loader)
     except errors.MissingRequiredOption as option_name:
         log.err('Missing required option: "%s"' % option_name)
-        print net_test_loader.usageOptions().getUsage()
+        incomplete_net_test_loader = option_name.net_test_loader
+        print incomplete_net_test_loader.usageOptions().getUsage()
         sys.exit(2)
     except errors.NetTestNotFound as path:
         log.err('Requested NetTest file not found (%s)' % path)
         sys.exit(3)
-    except usage.UsageError as e:
+    except errors.OONIUsageError as e:
         log.err(e)
-        print net_test_loader.usageOptions().getUsage()
+        print e.net_test_loader.usageOptions().getUsage()
         sys.exit(4)
     except Exception as e:
         log.err(e)
diff --git a/ooni/tests/test_nettest.py b/ooni/tests/test_nettest.py
index 94db4b3..7828c4b 100644
--- a/ooni/tests/test_nettest.py
+++ b/ooni/tests/test_nettest.py
@@ -6,7 +6,7 @@ from twisted.internet import defer, reactor
 from twisted.python.usage import UsageError
 
 from ooni.settings import config
-from ooni.errors import MissingRequiredOption
+from ooni.errors import MissingRequiredOption, OONIUsageError
 from ooni.nettest import NetTest, NetTestLoader
 
 from ooni.director import Director
@@ -67,6 +67,7 @@ class UsageOptions(usage.Options):
 class DummyTestCase(NetTestCase):
     inputFile = ['file', 'f', None, 'The input File']
 
+    requiredOptions = ['foo', 'bar']
     usageOptions = UsageOptions
 
     def test_a(self):
@@ -74,8 +75,6 @@ class DummyTestCase(NetTestCase):
 
     def test_b(self):
         self.report['foo'] = 'foo'
-
-    requiredOptions = ['foo', 'bar']
 """
 
 http_net_test = """
@@ -115,12 +114,15 @@ class TestNetTest(unittest.TestCase):
     timeout = 1
 
     def setUp(self):
+        self.filename = ""
         with open(dummyInputFile, 'w') as f:
             for i in range(10):
                 f.write("%s\n" % i)
 
     def tearDown(self):
         os.remove(dummyInputFile)
+        if self.filename != "":
+            os.remove(self.filename)
 
     def assertCallable(self, thing):
         self.assertIn('__call__', dir(thing))
@@ -181,14 +183,10 @@ class TestNetTest(unittest.TestCase):
                 self.assertIn(option, test_klass.usageOptions())
 
     def test_load_with_invalid_option(self):
-        try:
-            ntl = NetTestLoader(dummyInvalidArgs)
-            ntl.loadNetTestString(net_test_string)
-
-            ntl.checkOptions()
-            raise Exception
-        except UsageError:
-            pass
+        ntl = NetTestLoader(dummyInvalidArgs)
+        ntl.loadNetTestString(net_test_string)
+        self.assertRaises(UsageError, ntl.checkOptions)
+        self.assertRaises(OONIUsageError, ntl.checkOptions)
 
     def test_load_with_required_option(self):
         ntl = NetTestLoader(dummyArgsWithRequiredOptions)
@@ -197,12 +195,9 @@ class TestNetTest(unittest.TestCase):
         self.assertIsInstance(ntl, NetTestLoader)
 
     def test_load_with_missing_required_option(self):
-        try:
-            ntl = NetTestLoader(dummyArgs)
-            ntl.loadNetTestString(net_test_string_with_required_option)
-
-        except MissingRequiredOption:
-            pass
+        ntl = NetTestLoader(dummyArgs)
+        ntl.loadNetTestString(net_test_string_with_required_option)
+        self.assertRaises(MissingRequiredOption, ntl.checkOptions)
 
     def test_net_test_inputs(self):
         ntl = NetTestLoader(dummyArgsWithFile)
@@ -247,7 +242,8 @@ class TestNetTest(unittest.TestCase):
         ntl.checkOptions()
         director = Director()
 
-        d = director.startNetTest(ntl, 'dummy_report.yaml')
+        self.filename = 'dummy_report.yaml'
+        d = director.startNetTest(ntl, self.filename)
 
         @d.addCallback
         def complete(result):
@@ -301,6 +297,7 @@ class TestNettestTimeout(ConfigTestCase):
         super(TestNettestTimeout, self).tearDown()
         self.factory.stopFactory()
         self.port.stopListening()
+        os.remove(self.filename)
 
     def test_nettest_timeout(self):
         ntl = NetTestLoader(('-u', 'http://localhost:8007/'))
@@ -309,7 +306,8 @@ class TestNettestTimeout(ConfigTestCase):
         ntl.checkOptions()
         director = Director()
 
-        d = director.startNetTest(ntl, 'dummy_report.yaml')
+        self.filename = 'dummy_report.yaml'
+        d = director.startNetTest(ntl, self.filename)
 
         @d.addCallback
         def complete(result):





More information about the tor-commits mailing list