[tor-commits] [ooni-probe/master] Get all the unittests to pass.

art at torproject.org art at torproject.org
Tue Aug 27 09:21:51 UTC 2013


commit 553cb673b39fce91b0829ebe71caba94a817eadc
Author: Arturo Filastò <art at fuffa.org>
Date:   Wed Aug 21 23:22:55 2013 +0200

    Get all the unittests to pass.
---
 ooni/oonibclient.py            |   26 +++++++++++++++++++++-----
 ooni/tests/test_oonibclient.py |   22 ++++++++++------------
 ooni/utils/net.py              |    2 +-
 3 files changed, 32 insertions(+), 18 deletions(-)

diff --git a/ooni/oonibclient.py b/ooni/oonibclient.py
index 3f9ed56..1a581c0 100644
--- a/ooni/oonibclient.py
+++ b/ooni/oonibclient.py
@@ -1,9 +1,11 @@
+from hashlib import sha256
+
 import json
 
 from twisted.internet import defer, reactor
 from twisted.web.client import Agent
 
-from ooni.utils.net import BodyReceiver, StringProducer
+from ooni.utils.net import BodyReceiver, StringProducer, Downloader
 
 class InputFile(object):
     def __init__(self, id, name=None, description=None, 
@@ -15,12 +17,20 @@ class InputFile(object):
         self.author = author
         self.date = date
 
-        self._file = None
+        self.file_path = None
+
+    def verify(self):
+        digest = os.path.basename(self.file_path)
+        with open(self.file_path) as f:
+            file_hash = sha256(f.read())
+            return file_hash.hexdigest() == digest
+        return False
 
 class OONIBClient(object):
     def __init__(self, address):
         self.address = address
         self.agent = Agent(reactor)
+        self.input_files = {}
 
     def _request(self, method, urn, genReceiver, bodyProducer=None):
         finished = defer.Deferred()
@@ -56,14 +66,20 @@ class OONIBClient(object):
 
         return self._request('GET', urn, genReceiver)
     
-    def downloadInput(self, input_hash, download_path):
-        return self.download('/input/'+input_hash, download_path)
-
     def getNettestPolicy(self):
         pass
 
     def queryBouncer(self, requested_helpers):
         pass
 
+    def getInput(self, input_hash):
+        return self.queryBackend('GET', '/input/' + input_hash)
+
+    def getInputList(self):
+        return self.queryBackend('GET', '/input')
+
+    def downloadInput(self, input_hash, download_path):
+        return self.download('/input/'+input_hash+'/file', download_path)
+
     def getInputPolicy(self):
         pass
diff --git a/ooni/tests/test_oonibclient.py b/ooni/tests/test_oonibclient.py
index 06008bb..65e9e7e 100644
--- a/ooni/tests/test_oonibclient.py
+++ b/ooni/tests/test_oonibclient.py
@@ -1,42 +1,39 @@
 from twisted.trial import unittest
 from twisted.internet import defer
 
-try:
-    import oonib
-except ImportError:
-    oonib = None
-
 from ooni.oonibclient import OONIBClient
 
-input_id = 'e0611ecd28bead38a7afeb4dda8ae3449d0fc2e1ba53fa7355f2799dce9af290'
+input_id = '37e60e13536f6afe47a830bfb6b371b5cf65da66d7ad65137344679b24fdccd1'
 
 class TestOONIBClient(unittest.TestCase):
     def setUp(self):
         self.oonibclient = OONIBClient('http://127.0.0.1:8888')
-        if not oonib:
-            self.skipTest("OONIB is not running")
     
     @defer.inlineCallbacks
     def test_query(self):
         res = yield self.oonibclient.queryBackend('GET', '/policy/input')
         self.assertTrue(isinstance(res, list))
     
+    @defer.inlineCallbacks
     def test_get_input_list(self):
         input_list = yield self.oonibclient.getInputList()
-        self.assertTrue(isinstance(inputList, list))
+        self.assertTrue(isinstance(input_list, list))
 
+    @defer.inlineCallbacks
     def test_get_input_descriptor(self):
         input_descriptor = yield self.oonibclient.getInput(input_id)
         for key in ['name', 'description', 
                     'version', 'author', 'date']:
             self.assertTrue(key in input_descriptor.keys())
 
+    @defer.inlineCallbacks
     def test_download_input(self):
-        pass
+        yield self.oonibclient.downloadInput(input_id, input_id)
 
+    @defer.inlineCallbacks
     def test_get_deck_list(self):
         input_list = yield self.oonibclient.getInputList()
-        self.assertTrue(isinstance(inputList, list))
+        self.assertTrue(isinstance(input_list, list))
 
     def test_get_deck_descriptor(self):
         pass
@@ -44,9 +41,10 @@ class TestOONIBClient(unittest.TestCase):
     def test_download_deck(self):
         pass
 
+    @defer.inlineCallbacks
     def test_get_nettest_list(self):
         input_list = yield self.oonibclient.getInputList()
-        self.assertTrue(isinstance(inputList, list))
+        self.assertTrue(isinstance(input_list, list))
 
     def test_get_nettest_descriptor(self):
         pass
diff --git a/ooni/utils/net.py b/ooni/utils/net.py
index db2ea18..ab33d00 100644
--- a/ooni/utils/net.py
+++ b/ooni/utils/net.py
@@ -98,7 +98,7 @@ class Downloader(protocol.Protocol):
 
     def connectionLost(self, reason):
         self.fp.close()
-        self.finished.callback(self.download_path)
+        self.finished.callback(None)
 
 def getSystemResolver():
     """





More information about the tor-commits mailing list