[tor-commits] [ooni-probe/master] Write unit tests for all the OONI plugins

art at torproject.org art at torproject.org
Tue Jul 10 15:30:00 UTC 2012


commit 6aa7ca883a500eb2b909b21386d03f9bd29e5384
Author: Arturo Filastò <art at torproject.org>
Date:   Tue Jul 10 09:50:33 2012 +0200

    Write unit tests for all the OONI plugins
    * Refactor some code
---
 ooni/log.py                |    5 ++++-
 ooni/ooniprobe.py          |   31 +++++++++++++++++++++----------
 ooni/plugins/blocking.py   |    7 +++++--
 ooni/plugins/tcpconnect.py |    3 +--
 ooni/plugoo/tests.py       |   12 +++---------
 ooni/protocols/http.py     |    5 +----
 tests/assets/ipports.txt   |    2 ++
 tests/assets/urllist.txt   |    3 +++
 tests/test_plugins.py      |   40 ++++++++++++++++++++++++++++++++++++++++
 tests/test_tests.py        |   17 +++++++++--------
 10 files changed, 89 insertions(+), 36 deletions(-)

diff --git a/ooni/log.py b/ooni/log.py
index f1b7839..3dc52dd 100644
--- a/ooni/log.py
+++ b/ooni/log.py
@@ -34,7 +34,10 @@ def start(logfile=None, loglevel=None, logstdout=True):
         msg("Started OONI")
 
 def msg(message, level=INFO, **kw):
-    log.msg(message, **kw)
+    log.msg(message, logLevel=level, **kw)
 
 def err(message, **kw):
     log.err(message, **kw)
+
+def debug(message, **kw):
+    log.msg(message, logLevel=DEBUG, **kw)
diff --git a/ooni/ooniprobe.py b/ooni/ooniprobe.py
index 3eb70f6..22299ac 100755
--- a/ooni/ooniprobe.py
+++ b/ooni/ooniprobe.py
@@ -55,8 +55,17 @@ def retrieve_plugoo():
 
 plugoo = retrieve_plugoo()
 
-def runTest(test, options, global_options):
+def runTest(test, options, global_options, reactor=None):
+    """
+    Run an OONI probe test by name.
+
+    @param test: a string specifying the test name as specified inside of
+                 shortName.
 
+    @param options: the local options to be passed to the test.
+
+    @param global_options: the global options for OONI
+    """
     parallelism = int(global_options['parallelism'])
     worker = work.Worker(parallelism)
     test_class = plugoo[test].__class__
@@ -64,6 +73,8 @@ def runTest(test, options, global_options):
 
     log.start(global_options['log'], 1)
     resume = 0
+    if not options:
+        options = {}
     if 'resume' in options:
         resume = options['resume']
 
@@ -74,7 +85,6 @@ def runTest(test, options, global_options):
     for x in wgen:
         worker.push(x)
 
-    reactor.run()
 
 class Options(usage.Options):
     tests = plugoo.keys()
@@ -111,14 +121,15 @@ class Options(usage.Options):
         return getlogo() + '\n' + self.getSynopsis() + '\n' + \
                self.getUsage(width=None).replace("Commands:", "Tests:")
 
-config = Options()
-config.parseOptions()
-
-if not config.subCommand:
-    print "Error! No Test Specified."
-    config.opt_help()
-    sys.exit(1)
+if __name__ == "__main__":
+    config = Options()
+    config.parseOptions()
 
-runTest(config.subCommand, config.subOptions, config)
+    if not config.subCommand:
+        print "Error! No Test Specified."
+        config.opt_help()
+        sys.exit(1)
 
+    runTest(config.subCommand, config.subOptions, config)
+    reactor.run()
 
diff --git a/ooni/plugins/blocking.py b/ooni/plugins/blocking.py
index 6ac3c91..6ddb5a4 100644
--- a/ooni/plugins/blocking.py
+++ b/ooni/plugins/blocking.py
@@ -27,8 +27,11 @@ class BlockingTest(OONITest):
 
     def experiment(self, args):
         import urllib
-        url = 'https://torproject.org/' if not 'asset' in args else args['asset']
-        req = urllib.urlopen(url)
+        url = 'http://torproject.org/' if not 'asset' in args else args['asset']
+        try:
+            req = urllib.urlopen(url)
+        except:
+            return {'error': 'Connection failed!'}
 
         return {'page': req.readlines()}
 
diff --git a/ooni/plugins/tcpconnect.py b/ooni/plugins/tcpconnect.py
index 27df08d..7c04994 100644
--- a/ooni/plugins/tcpconnect.py
+++ b/ooni/plugins/tcpconnect.py
@@ -6,7 +6,6 @@ Safe hacking :).
 from zope.interface import implements
 from twisted.python import usage
 from twisted.plugin import IPlugin
-from twisted.internet import reactor
 from twisted.internet.protocol import Factory, Protocol
 from twisted.internet.endpoints import TCP4ClientEndpoint
 
@@ -48,7 +47,7 @@ class tcpconnectTest(OONITest):
             return {'result': False, 'target': [host, port]}
 
         # What you return here gets handed as input to control
-        point = TCP4ClientEndpoint(reactor, host, int(port))
+        point = TCP4ClientEndpoint(self.reactor, host, int(port))
         d = point.connect(DummyFactory())
         d.addCallback(gotProtocol)
         d.addErrback(gotError)
diff --git a/ooni/plugoo/tests.py b/ooni/plugoo/tests.py
index 47bb135..42294d6 100644
--- a/ooni/plugoo/tests.py
+++ b/ooni/plugoo/tests.py
@@ -79,6 +79,8 @@ class OONITest(object):
         not we expect it to return a Deferred.
 
         @param args: the asset line(s) that we are working on.
+
+        returns a deferred.
         """
         if self.blocking:
             self.d = threads.deferToThread(self.experiment, args)
@@ -98,15 +100,7 @@ class OONITest(object):
         @param args: the asset(s) lines that we are working on.
         """
         log.msg("Doing control")
-
-        if self.blocking:
-            return result
-
-        def end(cb):
-            return result
-        d = defer.Deferred()
-        d.addCallback(end)
-        return d
+        return result
 
     def experiment(self, args):
         """
diff --git a/ooni/protocols/http.py b/ooni/protocols/http.py
index 835735b..1dd2261 100644
--- a/ooni/protocols/http.py
+++ b/ooni/protocols/http.py
@@ -41,13 +41,10 @@ class HTTPTest(OONITest):
     """
     randomize_ua = True
 
-    def initialize(self, reactor=None):
+    def initialize(self):
         from twisted.web.client import Agent
         import yaml
 
-        if not self.reactor:
-            from twisted.internet import reactor
-            self.reactor = reactor
         self.agent = Agent(self.reactor)
         self.request = {}
         self.response = {}
diff --git a/tests/assets/ipports.txt b/tests/assets/ipports.txt
new file mode 100644
index 0000000..ade757f
--- /dev/null
+++ b/tests/assets/ipports.txt
@@ -0,0 +1,2 @@
+127.0.0.1:80
+8.8.8.8:53
diff --git a/tests/assets/urllist.txt b/tests/assets/urllist.txt
new file mode 100644
index 0000000..dc38e22
--- /dev/null
+++ b/tests/assets/urllist.txt
@@ -0,0 +1,3 @@
+http://google.com/
+http://127.0.0.1/
+http://dio.it/
diff --git a/tests/test_plugins.py b/tests/test_plugins.py
new file mode 100644
index 0000000..26bee51
--- /dev/null
+++ b/tests/test_plugins.py
@@ -0,0 +1,40 @@
+from twisted.internet import defer, reactor
+from twisted.trial import unittest
+
+from ooni.ooniprobe import retrieve_plugoo, runTest, Options
+from ooni.plugoo import work, tests
+
+def asset_file(filename):
+    import os
+    file_dir = os.path.normpath(os.path.join(__file__, '..'))
+    return os.path.join(file_dir, 'assets', filename)
+
+class PluginsTestCase(unittest.TestCase):
+    def test_plugin_blocking(self):
+        suboptions = {'asset': asset_file('urllist.txt')}
+        runTest('blocking', suboptions, Options(), reactor)
+        return
+
+    def test_plugin_tcpconnect(self):
+        suboptions = {'asset': asset_file('ipports.txt')}
+        runTest('tcpconnect', suboptions, Options(), reactor)
+        return
+
+
+    def test_plugin_captivep(self):
+        runTest('blocking', None, Options(), reactor)
+        return
+
+
+    def test_plugin_httphost(self):
+        suboptions = {'asset': asset_file('urllist.txt')}
+        runTest('httphost', suboptions, Options(), reactor)
+        return
+
+
+    def test_plugin_httpt(self):
+        suboptions = {'urls': asset_file('urllist.txt')}
+        runTest('httpt', suboptions, Options(), reactor)
+        return
+
+
diff --git a/tests/test_tests.py b/tests/test_tests.py
index bed5922..d0cc88c 100644
--- a/tests/test_tests.py
+++ b/tests/test_tests.py
@@ -13,9 +13,7 @@ class TestsTestCase(unittest.TestCase):
         self.errbackResults = None
 
     def _callback(self, *args, **kw):
-        print "BBB"
-        print args, kw
-        print "CCCC"
+        #print args, kw
         self.callbackResults = args, kw
 
     def _errback(self, *args, **kw):
@@ -31,14 +29,17 @@ class TestsTestCase(unittest.TestCase):
         class DummyTest(tests.OONITest):
             blocking = False
             def experiment(self, args):
-                def cb(aaa):
+                def bla(a):
+                    print a
                     return test_dict
-                d = defer.Deferred()
-                d.addCallback(cb)
-                d.callback(None)
-                return d
+                d2 = defer.Deferred()
+                d2.addCallback(bla)
+                from twisted.internet import reactor
+                reactor.callLater(0.1, d2.callback, None)
+                return d2
 
         test = DummyTest(None, None, self.dummyreport)
         yield test.startTest(None).addCallback(self._callback)
         self.assertEqual(self.callbackResults[0][0]['control'], test_dict)
+        return
 





More information about the tor-commits mailing list