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

[tor-commits] [ooni-probe/master] Add NetTestCase option validation



commit 54d2aa3fb57f733ab9285df2eab685fa2e0e12fe
Author: aagbsn <aagbsn@xxxxxxxx>
Date:   Sun Jan 13 12:18:47 2013 +0000

    Add NetTestCase option validation
    
    Options that are passed to the NetTest constructor must be valid
    options defined in the NetTestCase UsageOptions with optParameters.
    
    Also handle the case where inputFile is *not* a required option and not
    supplied gracefully.
---
 ooni/nettest.py |   38 ++++++++++++++++++++++++--------------
 1 files changed, 24 insertions(+), 14 deletions(-)

diff --git a/ooni/nettest.py b/ooni/nettest.py
index c9e2ed3..0fb5038 100644
--- a/ooni/nettest.py
+++ b/ooni/nettest.py
@@ -142,6 +142,7 @@ class NetTest(object):
             if test_instance.requiresRoot:
                 checkForRoot()
             test_instance._checkRequiredOptions()
+            test_instance._checkValidOptions()
 
             klass.inputs = test_instance.getInputProcessor()
 
@@ -269,31 +270,40 @@ class NetTestCase(object):
 
     def getInputProcessor(self):
         """
-        This method must be called afterr
+        This method must be called after all options are validated by
+        _checkValidOptions and _checkRequiredOptions, which ensure that
+        if the inputFile is a required option it will be present.
         """
         if self.inputFile:
-            self.inputFilename = self.localOptions[self.inputFile[0]]
+            if self.inputFile[0] in self.localOptions:
+                self.inputFilename = self.localOptions[self.inputFile[0]]
 
-            inputProcessor = self.inputProcessor
-            inputFilename = self.inputFilename
+                inputProcessor = self.inputProcessor
+                inputFilename = self.inputFilename
 
-            class inputProcessorIterator(object):
-                """
-                Here we convert the input processor generator into an iterator
-                so that we can run it twice.
-                """
-                def __iter__(self):
-                    return inputProcessor(inputFilename)
+                class inputProcessorIterator(object):
+                    """
+                    Here we convert the input processor generator into an iterator
+                    so that we can run it twice.
+                    """
+                    def __iter__(self):
+                        return inputProcessor(inputFilename)
 
-            return inputProcessorIterator()
+                return inputProcessorIterator()
 
         return iter(())
 
+    def _checkValidOptions(self):
+        for option in self.localOptions:
+            if option not in self.usageOptions():
+                if not self.inputFile or option not in self.inputFile:
+                    raise InvalidOption
+
     def _checkRequiredOptions(self):
         for required_option in self.requiredOptions:
             log.debug("Checking if %s is present" % required_option)
-            if not self.localOptions[required_option]:
-                raise usage.UsageError("%s not specified!" % required_option)
+            if required_option not in self.localOptions:
+               raise MissingRequiredOption
 
     def __repr__(self):
         return "<%s inputs=%s>" % (self.__class__, self.inputs)



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