[ooni-probe/master] Clean up some code

commit f7ea3343c0e148c2fd180b746cb651b9f664687a Author: Arturo Filastò <arturo@filasto.net> Date: Fri Sep 28 22:28:44 2012 +0000 Clean up some code * Fix a bug in txscapy --- nettests/example_httpt.py | 6 +++++- nettests/example_scapyt.py | 4 ++++ ooni/lib/txscapy.py | 7 ++++--- ooni/nettest.py | 2 +- ooni/runner.py | 8 ++++---- ooni/templates/httpt.py | 3 +++ ooni/templates/scapyt.py | 18 +++++++++++------- ooni/utils/log.py | 11 ++++++----- 8 files changed, 38 insertions(+), 21 deletions(-) diff --git a/nettests/example_httpt.py b/nettests/example_httpt.py index 1814a90..6501ca7 100644 --- a/nettests/example_httpt.py +++ b/nettests/example_httpt.py @@ -4,7 +4,11 @@ # :licence: see LICENSE from ooni.templates import httpt -class Example(httpt.HTTPTest): +class ExampleHTTP(httpt.HTTPTest): + name = "Example HTTP Test" + author = "Arturo Filastò" + version = 0.1 + inputs = ['http://google.com/', 'http://wikileaks.org/', 'http://torproject.org/'] diff --git a/nettests/example_scapyt.py b/nettests/example_scapyt.py index 4c31567..54fce9a 100644 --- a/nettests/example_scapyt.py +++ b/nettests/example_scapyt.py @@ -6,4 +6,8 @@ from ooni.templates import scapyt from scapy.all import * class ExampleScapy(scapyt.ScapyTest): + name = "Example Scapy Test" + author = "Arturo Filastò" + version = 0.1 + inputs = [IP(dst='8.8.8.8')/UDP(), IP()/TCP()] diff --git a/ooni/lib/txscapy.py b/ooni/lib/txscapy.py index 4d83dce..f67daa3 100644 --- a/ooni/lib/txscapy.py +++ b/ooni/lib/txscapy.py @@ -90,8 +90,7 @@ class Scapy(object): recv = False def __init__(self, pkts=None, maxPacketSize=8192, reactor=None, filter=None, - iface=None, nofilter=None, pcapfile=None): - + iface=None, nofilter=None, pcapfile=None, *arg, **kw): if self.debug: log.startLogging(sys.stdout) @@ -201,7 +200,7 @@ class Scapy(object): @param pkt: the packet that has been received. """ - #pkt.show() + pkt.show() def doRead(self): @@ -210,6 +209,8 @@ class Scapy(object): received packet. """ pkt = self.socket.recv() + if not pkt: + return if self.pcapwriter and not self.write_only_answers: self.pcapwriter.write(pkt) self.processPacket(pkt) diff --git a/ooni/nettest.py b/ooni/nettest.py index c0c7406..403a022 100644 --- a/ooni/nettest.py +++ b/ooni/nettest.py @@ -69,7 +69,7 @@ class TestCase(unittest.TestCase): * version: is the version string of the test. """ - name = "IDidNotChangeTheName" + name = "I Did Not Change The Name" author = "John Doe <foo@example.com>" version = "0" diff --git a/ooni/runner.py b/ooni/runner.py index 1a9b4ad..13fdda2 100644 --- a/ooni/runner.py +++ b/ooni/runner.py @@ -6,7 +6,7 @@ import inspect from twisted.internet import defer, reactor from twisted.python import reflect, failure -from twisted.python import log +#from twisted.python import log from twisted.trial import unittest from twisted.trial.runner import TrialRunner, TestLoader from twisted.trial.runner import isPackage, isTestCase, ErrorHolder @@ -16,7 +16,7 @@ from ooni.reporter import ReporterFactory from ooni.input import InputUnitFactory from ooni.nettest import InputTestSuite from ooni import nettest -#from ooni.utils import log +from ooni.utils import log from ooni.plugoo import tests as oonitests def isTestCase(thing): @@ -183,8 +183,8 @@ class ORunner(object): result.done() def run(self): - log.startLogging(sys.stdout) - #log.start(True) + #log.startLogging(sys.stdout) + log.start(True) self.reporterFactory.writeHeader() for inputUnit in InputUnitFactory(self.inputs): diff --git a/ooni/templates/httpt.py b/ooni/templates/httpt.py index 222e44c..117a77a 100644 --- a/ooni/templates/httpt.py +++ b/ooni/templates/httpt.py @@ -45,6 +45,9 @@ class HTTPTest(TestCase): processResponseHeader that are invoked once the headers have been received and once the request body has been received. """ + name = "HTTP Test" + version = 0.1 + randomizeUA = True followRedirects = False diff --git a/ooni/templates/scapyt.py b/ooni/templates/scapyt.py index 894a476..f762ae5 100644 --- a/ooni/templates/scapyt.py +++ b/ooni/templates/scapyt.py @@ -9,12 +9,13 @@ from twisted.python import usage from twisted.plugin import IPlugin from twisted.internet import protocol, defer +from scapy.all import * + from ooni.nettest import TestCase from ooni.utils import log from ooni.lib.txscapy import txsr, txsend -from scapy.all import * class ScapyTest(TestCase): """ A utility class for writing scapy driven OONI tests. @@ -25,29 +26,33 @@ class ScapyTest(TestCase): * receive: if we should also receive packets and not just send """ + name = "Scapy Test" + version = 0.1 receive = True - timeout = None + timeout = 1 pcapfile = 'scapytest.pcap' input = IP()/TCP() + reactor = None def setUp(self): - if not self.reactor: from twisted.internet import reactor self.reactor = reactor - self.request = {} self.response = {} + def tearDown(self): + self.reactor.stop() + def test_sendReceive(self): log.msg("Running send receive") if self.receive: log.msg("Sending and receiving packets.") d = txsr(self.buildPackets(), pcapfile=self.pcapfile, - timeout=self.timeout) + timeout=self.timeout, reactor=self.reactor) else: log.msg("Sending packets.") - d = txsend(self.buildPackets()) + d = txsend(self.buildPackets(), reactor=self.reactor) def finished(data): log.msg("Finished sending") @@ -62,4 +67,3 @@ class ScapyTest(TestCase): """ return self.input - diff --git a/ooni/utils/log.py b/ooni/utils/log.py index dd5cf13..f79f4b6 100644 --- a/ooni/utils/log.py +++ b/ooni/utils/log.py @@ -17,7 +17,7 @@ def _get_log_level(level): else: ve = "Unknown log level: %s\n" % level ve += "Allowed levels: %s\n" % [word for word in english] - + if type(level) is int: if 0 <= level <= 4: return level @@ -31,16 +31,16 @@ def _get_log_level(level): class OONITestFailure(Failure): """ - For handling Exceptions asynchronously. + For handling Exceptions asynchronously. Can be given an Exception as an argument, else will use the most recent Exception from the current stack frame. """ - def __init__(self, exception=None, _type=None, + def __init__(self, exception=None, _type=None, _traceback=None, _capture=False): Failure.__init__(self, exc_value=exception, exc_type=_type, exc_tb=_traceback, captureVars=_capture) - + class OONILogObserver(log.FileLogObserver): """ Supports logging level verbosity. @@ -66,7 +66,7 @@ class OONILogObserver(log.FileLogObserver): return timeStr = self.formatTime(eventDict['time']) - fmtDict = {'system': eventDict['system'], + fmtDict = {'system': eventDict['system'], 'text': text.replace('\n','\n\t')} msgStr = log._safeFormat("[%(system)s] %(text)s\n", fmtDict) @@ -91,6 +91,7 @@ def debug(message, level="debug", **kw): log.msg(message, logLevel=level, **kw) def msg(message, level="info", **kw): + print "Msg %s" % message log.msg(message, logLevel=level, **kw) def err(message, level="err", **kw):
participants (1)
-
isis@torproject.org