commit ff2a352a8229e583ca5e0537f4f963a8fc1dff3a Author: Arturo Filastò art@fuffa.org Date: Thu Mar 27 16:08:12 2014 +0100
Iteration based on feedback from asn.
* Check that tor is installed before running the test (adding of new API method requirements used for doing checks before a test runs). * Add description to bridge_reachability test. * Consider the config.advanced.tor_binary option when running tor. * Add command line option -v for extra verbosity (equivalent to debug: true) --- ooni/nettest.py | 16 ++++++++++------ ooni/nettests/blocking/bridge_reachability.py | 7 +++++++ ooni/oonicli.py | 10 ++++++++-- ooni/utils/log.py | 2 ++ ooni/utils/onion.py | 10 +++++++++- 5 files changed, 36 insertions(+), 9 deletions(-)
diff --git a/ooni/nettest.py b/ooni/nettest.py index aba768b..074f0bd 100644 --- a/ooni/nettest.py +++ b/ooni/nettest.py @@ -138,7 +138,7 @@ class NetTestLoader(object): requiresTor = False
def __init__(self, options, test_file=None, test_string=None): - self.onionInputRegex = re.compile("(httpo://[a-z0-9]{16}.onion)/input/([a-z0-9]{64})$") + self.onionInputRegex = re.compile("(httpo://[a-z0-9]{16}.onion)/input/([a-z0-9]{64})$") self.options = options self.testCases = []
@@ -293,7 +293,7 @@ class NetTestLoader(object):
if not test_cases: raise e.NoTestCasesFound - + self.setupTestCases(test_cases)
def setupTestCases(self, test_cases): @@ -329,10 +329,6 @@ class NetTestLoader(object): """ Call processTest and processOptions methods of each NetTestCase """ - test_classes = set([]) - for test_class, test_method in self.testCases: - test_classes.add(test_class) - for klass in self.testClasses: options = self.usageOptions() options.parseOptions(self.options) @@ -345,6 +341,7 @@ class NetTestLoader(object): checkForRoot() if test_instance.requiresTor: self.requiresTor = True + test_instance.requirements() test_instance._checkRequiredOptions() test_instance._checkValidOptions()
@@ -615,6 +612,13 @@ class NetTestCase(object): """ self.report = {} self.inputs = None + + def requirements(self): + """ + Place in here logic that will be executed before the test is to be run. + If some condition is not met then you should raise an exception. + """ + pass
def setUp(self): """ diff --git a/ooni/nettests/blocking/bridge_reachability.py b/ooni/nettests/blocking/bridge_reachability.py index 54bce73..5a12a1f 100644 --- a/ooni/nettests/blocking/bridge_reachability.py +++ b/ooni/nettests/blocking/bridge_reachability.py @@ -12,6 +12,8 @@ import txtorcon from ooni.utils import log, onion from ooni import nettest
+class TorIsNotInstalled(Exception): pass + class UsageOptions(usage.Options): optParameters = [['timeout', 't', 120, 'Specify the timeout after which to consider the Tor bootstrapping process to have failed'], @@ -19,6 +21,7 @@ class UsageOptions(usage.Options):
class BridgeReachability(nettest.NetTestCase): name = "Bridge Reachability" + description = "A test for checking if bridges are reachable from a given location." author = "Arturo Filastò" version = "0.1"
@@ -30,6 +33,10 @@ class BridgeReachability(nettest.NetTestCase): 'TransportType IP:ORPort (ex. obfs2 127.0.0.1:443)']
requiredOptions = ['file'] + + def requirements(self): + if not onion.find_tor_binary(): + raise TorIsNotInstalled("For instructions on installing Tor see: https://www.torproject.org/download/download")
def setUp(self): self.tor_progress = 0 diff --git a/ooni/oonicli.py b/ooni/oonicli.py index 6f4ebe0..98c3ee6 100644 --- a/ooni/oonicli.py +++ b/ooni/oonicli.py @@ -32,7 +32,8 @@ class Options(usage.Options): ["resume", "r"], ["no-collector", "n"], ["list", "s"], - ["printdeck", "p"] + ["printdeck", "p"], + ["verbose", "v"] ]
optParameters = [["reportfile", "o", None, "report file name"], @@ -101,6 +102,8 @@ def runWithDirector(logging=True, start_tor=True): config.global_options = global_options config.set_paths() config.read_config_file() + if global_options['verbose']: + config.advanced.debug = True if not start_tor: config.advanced.start_tor = False
@@ -166,7 +169,10 @@ def runWithDirector(logging=True, start_tor=True): except usage.UsageError, e: log.err(e) print net_test_loader.usageOptions().getUsage() - sys.exit(2) + sys.exit(4) + except Exception as e: + log.err(e) + sys.exit(5)
d = director.start(start_tor=start_tor)
diff --git a/ooni/utils/log.py b/ooni/utils/log.py index cbad136..7fafcfb 100644 --- a/ooni/utils/log.py +++ b/ooni/utils/log.py @@ -71,6 +71,8 @@ def debug(msg, *arg, **kw): def err(msg, *arg, **kw): from ooni.settings import config if config.logging: + if isinstance(msg, Exception): + msg = "%s: %s" % (msg.__class__.__name__, msg) print "[!] %s" % msg
def exception(error): diff --git a/ooni/utils/onion.py b/ooni/utils/onion.py index 91d8a2d..0c54242 100644 --- a/ooni/utils/onion.py +++ b/ooni/utils/onion.py @@ -2,11 +2,19 @@ import string import subprocess from distutils.version import LooseVersion
-from txtorcon.util import find_tor_binary +from txtorcon.util import find_tor_binary as tx_find_tor_binary + +from ooni.settings import config
class TorVersion(LooseVersion): pass
+ +def find_tor_binary(): + if config.advanced.tor_binary: + return config.advanced.tor_binary + return tx_find_tor_binary() + def tor_version(): tor_binary = find_tor_binary() if not tor_binary: