commit d4cfecb34c146faf07c87268abcd202e693257b8 Author: kudrom kudrom@riseup.net Date: Sat Jul 5 15:57:52 2014 +0200
Improved error handling of oonibclient --- .gitignore | 3 ++- Vagrantfile | 2 ++ ooni/errors.py | 19 +++++++++++++++++++ ooni/oonibclient.py | 1 - ooni/tests/test_oonibclient.py | 34 +++++++++++++++++----------------- 5 files changed, 40 insertions(+), 19 deletions(-)
diff --git a/.gitignore b/.gitignore index 5f6f71f..9632014 100644 --- a/.gitignore +++ b/.gitignore @@ -40,4 +40,5 @@ docs/build/*
.vagrant/*
-cover/* \ No newline at end of file +cover/* +ooni_home/* \ No newline at end of file diff --git a/Vagrantfile b/Vagrantfile index 39d9f0f..3d4b81d 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -9,6 +9,8 @@ Vagrant.configure("2") do |config| config.vm.box_url = "http://files.vagrantup.com/precise32.box"
config.vm.synced_folder ".", "/ooni" + # Place the ooni-backend source code in ../ooni-backend to sync it with the vagrant instance + # config.vm.synced_folder "../ooni-backend", "/oonib"
end
diff --git a/ooni/errors.py b/ooni/errors.py index d5673ed..76b56f9 100644 --- a/ooni/errors.py +++ b/ooni/errors.py @@ -1,6 +1,7 @@ from twisted.internet.defer import CancelledError from twisted.internet.defer import TimeoutError as DeferTimeoutError from twisted.web._newclient import ResponseNeverReceived +from twisted.web.error import Error
from twisted.internet.error import ConnectionRefusedError, TCPTimedOutError from twisted.internet.error import DNSLookupError, ConnectError, ConnectionLost @@ -208,6 +209,10 @@ class OONIBError(Exception): pass
+class OONIBInvalidRequest(OONIBError): + pass + + class OONIBReportError(OONIBError): pass
@@ -224,6 +229,14 @@ class OONIBTestDetailsLookupError(OONIBReportError): pass
+class OONIBInputError(OONIBError): + pass + + +class OONIBInputDescriptorNotFound(OONIBInputError): + pass + + class UnableToLoadDeckInput(Exception): pass
@@ -279,5 +292,11 @@ class ReportLogExists(Exception): def get_error(error_key): if error_key == 'test-helpers-key-missing': return CouldNotFindTestHelper + if error_key == 'input-descriptor-not-found': + return OONIBInputDescriptorNotFound + if error_key == 'invalid-request': + return OONIBInvalidRequest + elif isinstance(error_key, int): + return Error("%d" % error_key) else: return OONIBError diff --git a/ooni/oonibclient.py b/ooni/oonibclient.py index 84e12b5..7676b91 100644 --- a/ooni/oonibclient.py +++ b/ooni/oonibclient.py @@ -69,7 +69,6 @@ class OONIBClient(object):
def perform_request(attempts): uri = address + urn - headers = {} d = agent.request(method, uri, bodyProducer=bodyProducer)
@d.addCallback diff --git a/ooni/tests/test_oonibclient.py b/ooni/tests/test_oonibclient.py index 1497105..d80b606 100644 --- a/ooni/tests/test_oonibclient.py +++ b/ooni/tests/test_oonibclient.py @@ -4,6 +4,7 @@ import socket
from twisted.trial import unittest from twisted.internet import defer +from twisted.web import error
from ooni import errors as e from ooni.utils import log @@ -99,23 +100,22 @@ class TestOONIBClient(ConfigTestCase): self.assertTrue(int(helpers['dns']['address'].split('.')[0]))
@defer.inlineCallbacks - def test_invalid_requests(self): - - @defer.inlineCallbacks - def all_requests(path): - for mthd in ['GET', 'POST', 'PUT', 'OPTION']: - try: - yield self.oonibclient.queryBackend(mthd, path) - except: - pass - - for path in ['/policy/input', '/policy/nettest', - '/input', '/input/' + 'a' * 64, '/fooo']: - yield all_requests(path) - - for path in ['/bouncer']: - self.oonibclient.address = 'http://127.0.0.1:8888' - yield all_requests(path) + def test_input_descriptor_not_found(self): + try: + yield self.oonibclient.queryBackend('GET', '/input/' + 'a'*64) + except e.OONIBInputDescriptorNotFound: + pass + else: + assert False + + @defer.inlineCallbacks + def test_http_errors(self): + try: + yield self.oonibclient.queryBackend('PUT', '/policy/input') + except error.Error: + pass + else: + assert False
@defer.inlineCallbacks def test_create_report(self):
tor-commits@lists.torproject.org