commit f873cd780a37451810b04526065fbe90d6250ba7 Author: Arturo Filastò art@fuffa.org Date: Wed Nov 21 18:59:56 2012 +0100
Implement TCP payload sending test template * Example test based on this test template --- nettests/examples/example_tcpt.py | 8 +++++ ooni/templates/tcpt.py | 58 +++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 0 deletions(-)
diff --git a/nettests/examples/example_tcpt.py b/nettests/examples/example_tcpt.py new file mode 100644 index 0000000..ccb3077 --- /dev/null +++ b/nettests/examples/example_tcpt.py @@ -0,0 +1,8 @@ +from ooni.templates import tcpt + +class ExampleTCPT(tcpt.TCPTest): + def test_hello_world(self): + self.address = "127.0.0.1" + self.port = 57002 + payload = "Hello World!\n\r" + return self.sendPayload(payload) diff --git a/ooni/templates/tcpt.py b/ooni/templates/tcpt.py new file mode 100644 index 0000000..3312cbe --- /dev/null +++ b/ooni/templates/tcpt.py @@ -0,0 +1,58 @@ +from twisted.internet import protocol, defer, reactor +from twisted.internet.error import ConnectionDone +from twisted.internet.endpoints import TCP4ClientEndpoint + +from ooni.nettest import NetTestCase +from ooni.utils import log + +class TCPSender(protocol.Protocol): + report = None + def dataReceived(self, data): + self.report['received'].append(data) + + def sendPayload(self, payload): + self.report['sent'].append(payload) + self.transport.write(payload) + +class TCPSenderFactory(protocol.Factory): + def buildProtocol(self, addr): + return TCPSender() + +class TCPTest(NetTestCase): + name = "Base TCP Test" + version = "0.1" + + requiresRoot = False + timeout = 2 + address = None + port = None + + def _setUp(self): + self.report['sent'] = [] + self.report['received'] = [] + + def sendPayload(self, payload): + d1 = defer.Deferred() + + def closeConnection(p): + p.transport.loseConnection() + log.debug("Closing connection") + d1.callback(None) + + def errback(failure): + self.report['error'] = str(failure) + log.exception(failure) + + def connected(p): + log.debug("Connected to %s:%s" % (self.address, self.port)) + p.report = self.report + p.sendPayload(payload) + reactor.callLater(self.timeout, closeConnection, p) + + point = TCP4ClientEndpoint(reactor, self.address, self.port) + d2 = point.connect(TCPSenderFactory()) + d2.addCallback(connected) + d2.addErrback(errback) + return d1 + +
tor-commits@lists.torproject.org