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

[tor-commits] [ooni-probe/master] Add support for setting the url and expected-body in tests



commit c17f564789b85bc9baa9dc1c64e77d8ff070aebd
Author: Arturo Filastò <arturo@xxxxxxxxxxx>
Date:   Wed Mar 16 15:06:59 2016 +0100

    Add support for setting the url and expected-body in tests
    
    * When these are changed from their default values a flag is set to false
---
 docs/source/tests/lantern.rst        |  6 ++++++
 docs/source/tests/psiphon.rst        |  7 +++++++
 ooni/nettests/third_party/lantern.py | 30 ++++++++++++++++++++++++++----
 ooni/nettests/third_party/psiphon.py | 29 +++++++++++++++++++----------
 ooni/utils/net.py                    |  3 +++
 5 files changed, 61 insertions(+), 14 deletions(-)

diff --git a/docs/source/tests/lantern.rst b/docs/source/tests/lantern.rst
index 9f3af2f..9f3ee09 100644
--- a/docs/source/tests/lantern.rst
+++ b/docs/source/tests/lantern.rst
@@ -31,6 +31,11 @@ How to run the test
 
 `ooniprobe nettests/third_party/lantern.py`
 
+For advanced usages you may also configure a different URL and expected body
+for the response with the `--url` and `--expected-body` command line options.
+
+`ooniprobe nettests/third_party/lantern.py --url http://www.github.com/humans.txt --expected-body '/* TEAM */'`
+
 Sample report
 =============
 
@@ -55,6 +60,7 @@ test_version: 0.0.1
 ---
 body: "Google is built by a large team of engineers, designers, researchers, robots, and others in many different sites across the globe. It is updated continuously, and built with more tools and technologies than we can shake a stick at. If you'd like to help us out, see google.com/careers."
 bootstrapped: true
+default_configuration: true
 input: null
 lantern --headless: {exit_reason: process_done, stderr: '', stdout: ''}
 ```
diff --git a/docs/source/tests/psiphon.rst b/docs/source/tests/psiphon.rst
index 3934bcb..3499501 100644
--- a/docs/source/tests/psiphon.rst
+++ b/docs/source/tests/psiphon.rst
@@ -35,6 +35,12 @@ To test Psiphon when it is installed in a different path other than the user hom
 
 `ooniprobe third_party/psiphon -p <path to Psiphon repository>`
 
+For advanced usages you may also configure a different URL and expected body
+for the response with the `--url` and `--expected-body` command line options.
+
+`ooniprobe third_party/psiphon --url http://www.github.com/humans.txt --expected-body '/* TEAM */'`
+
+
 How to install Psiphon
 ===================
 
@@ -80,6 +86,7 @@ Sample report
     agent: agent
     input: null
     psiphon_installed: true
+    default_configuration: true
     requests:
     - request:
         body: null
diff --git a/ooni/nettests/third_party/lantern.py b/ooni/nettests/third_party/lantern.py
index 187e210..da19566 100644
--- a/ooni/nettests/third_party/lantern.py
+++ b/ooni/nettests/third_party/lantern.py
@@ -1,11 +1,22 @@
 import os
+import distutils.spawn
+
 from twisted.internet import defer, reactor
 from twisted.internet.endpoints import TCP4ClientEndpoint
 from twisted.web.client import ProxyAgent, readBody
+from twisted.python import usage
+
 from ooni.templates.process import ProcessTest, ProcessDirector
-from ooni.utils import log
+from ooni.utils import log, net
 from ooni.errors import handleAllFailures
-import distutils.spawn
+
+class UsageOptions(usage.Options):
+    optParameters = [
+        ['url', 'u', net.GOOGLE_HUMANS[0],
+            'Specify the URL to fetch over lantern (default: http://www.google.com/humans.txt).'],
+        ['expected-body', 'e', net.GOOGLE_HUMANS[1],
+            'Specify the beginning of the expected body in the response (default: ' + net.GOOGLE_HUMANS[1] + ')']
+    ]
 
 class LanternTest(ProcessTest):
     """
@@ -26,10 +37,21 @@ class LanternTest(ProcessTest):
     timeout = 120
 
     def setUp(self):
+        self.report['body'] = None
+        self.report['failure'] = None
+        self.report['success'] = None
+        self.report['default_configuration'] = True
+
         self.command = [distutils.spawn.find_executable("lantern"), "--headless"]
         self.bootstrapped = defer.Deferred()
         self.exited = False
-        self.url = 'http://www.google.com/humans.txt'
+
+        self.url = self.localOptions['url']
+        if self.url != net.GOOGLE_HUMANS[0]:
+            self.report['default_configuration'] = False
+
+        if self.localOptions['expected-body'] != net.GOOGLE_HUMANS[1]:
+            self.report['default_configuration'] = False
 
     def stop(self, reason=None):
         if not self.exited:
@@ -49,7 +71,7 @@ class LanternTest(ProcessTest):
     def test_lantern_circumvent(self):
         def addResultToReport(result):
             self.report['body'] = result
-            if result.startswith('Google is built by a large'):
+            if result.startswith(self.localOptions['expected-body']):
                 log.msg("Got the HTTP response body I expected!")
                 self.report['success'] = True
             else:
diff --git a/ooni/nettests/third_party/psiphon.py b/ooni/nettests/third_party/psiphon.py
index 575002b..2b18b18 100644
--- a/ooni/nettests/third_party/psiphon.py
+++ b/ooni/nettests/third_party/psiphon.py
@@ -6,17 +6,19 @@ from twisted.internet import defer, reactor
 from twisted.internet.error import ProcessExitedAlready
 from twisted.python import usage
 
-from ooni.utils import log
+from ooni.utils import log, net
 from ooni.templates import process, httpt
 
 
 class UsageOptions(usage.Options):
-    log.debug("UsageOptions")
     optParameters = [
-        ['psiphonpath', 'p', None, 'Specify psiphon python client path.']
+        ['psiphonpath', 'p', None, 'Specify psiphon python client path.'],
+        ['url', 'u', net.GOOGLE_HUMANS[0],
+            'Specify the URL to fetch over psiphon (default: http://www.google.com/humans.txt).'],
+        ['expected-body', 'e', net.GOOGLE_HUMANS[1],
+            'Specify the beginning of the expected body in the response (default: ' + net.GOOGLE_HUMANS[1] + ')']
     ]
 
-
 class PsiphonTest(httpt.HTTPTest,  process.ProcessTest):
 
     """
@@ -43,8 +45,19 @@ class PsiphonTest(httpt.HTTPTest,  process.ProcessTest):
     def setUp(self):
         log.debug('PsiphonTest.setUp')
 
+        self.report['bootstrapped_success'] = None
+        self.report['request_success'] = None
+        self.report['psiphon_found'] = None
+        self.report['default_configuration'] = True
+
         self.bootstrapped = defer.Deferred()
-        self.url = 'http://www.google.com/humans.txt'
+        self.url = self.localOptions['url']
+
+        if self.localOptions['url'] != net.GOOGLE_HUMANS[0]:
+            self.report['default_configuration'] = False
+
+        if self.localOptions['expected-body'] != net.GOOGLE_HUMANS[1]:
+            self.report['default_configuration'] = False
 
         if self.localOptions['psiphonpath']:
             self.psiphonpath = self.localOptions['psiphonpath']
@@ -83,10 +96,6 @@ connect(False)
     def test_psiphon(self):
         log.debug('PsiphonTest.test_psiphon')
         self.createCommand()
-
-        self.report['bootstrapped_success'] = None
-        self.report['request_success'] = None
-        self.report['psiphon_found'] = None
         if not os.path.exists(self.psiphonpath):
             log.err('psiphon path does not exists, is it installed?')
             self.report['psiphon_found'] = False
@@ -121,7 +130,7 @@ connect(False)
             d = self.doRequest(self.url)
             def addSuccessToReport(res):
                 log.debug("PsiphonTest.callDoRequest.addSuccessToReport")
-                if res.body.startswith('Google is built by a large'):
+                if res.body.startswith(self.localOptions['expected-body']):
                     self.report['request_success'] = True
                 else:
                     self.report['request_success'] = False
diff --git a/ooni/utils/net.py b/ooni/utils/net.py
index 96d62a1..1f87101 100644
--- a/ooni/utils/net.py
+++ b/ooni/utils/net.py
@@ -46,6 +46,9 @@ PLATFORMS = {'LINUX': sys.platform.startswith("linux"),
              'SOLARIS': sys.platform.startswith("sunos"),
              'WINDOWS': sys.platform.startswith("win32")}
 
+# This is used as a default for checking if we get the expected result when
+# fetching URLs over some proxy.
+GOOGLE_HUMANS = ('http://www.google.com/humans.txt', 'Google is built by a large')
 
 class StringProducer(object):
     implements(IBodyProducer)



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