commit f4a065da96af966e01126be4c3013780da1b7579 Author: Arturo Filastò arturo@filasto.net Date: Wed Nov 7 19:49:08 2012 +0100
Refactor the Scapy test template. * Now is it much more simple For the time being the txscapy send and receiver will be disabled in sake of KISS principle. --- nettests/examples/example_scapyt.py | 25 ++------- ooni/templates/scapyt.py | 103 ++++++++++++++++++----------------- 2 files changed, 56 insertions(+), 72 deletions(-)
diff --git a/nettests/examples/example_scapyt.py b/nettests/examples/example_scapyt.py index ce32ac3..51ffd02 100644 --- a/nettests/examples/example_scapyt.py +++ b/nettests/examples/example_scapyt.py @@ -5,30 +5,13 @@
from ooni.utils import log from ooni.templates import scapyt -from scapy.all import * +from scapy.all import IP, TCP
-class ExampleScapy(scapyt.ScapyTest): +class ExampleBasicScapy(scapyt.BaseScapyTest): name = "Example Scapy Test" author = "Arturo Filastò" version = 0.1
- inputs = [IP(dst="8.8.8.8")/TCP(dport=31337), - IP(dst="ooni.nu")/TCP(dport=31337)] - - requiresRoot = True - - def test_sendReceive(self): + def test_send_raw_ip_frame(self): log.msg("Running send receive") - if self.receive: - log.msg("Sending and receiving packets.") - d = self.sendReceivePackets(self.buildPackets()) - else: - log.msg("Sending packets.") - d = self.sendPackets(self.buildPackets()) - - def finished(data): - log.msg("Finished sending") - return data - - d.addCallback(finished) - return + ans, unans = self.sr(IP(dst='8.8.8.8')/TCP(), timeout=1) diff --git a/ooni/templates/scapyt.py b/ooni/templates/scapyt.py index 9e18fbd..8683a8e 100644 --- a/ooni/templates/scapyt.py +++ b/ooni/templates/scapyt.py @@ -9,14 +9,61 @@ from twisted.python import usage from twisted.plugin import IPlugin from twisted.internet import protocol, defer, threads
-from scapy.all import IP, TCP, send, sr +from scapy.all import send, sr, IP, TCP
from ooni.nettest import NetTestCase from ooni.utils import log
from ooni.lib.txscapy import TXScapy
-class ScapyTest(NetTestCase): +def createPacketReport(packet_list): + """ + Takes as input a packet a list containing a dict with the packet + summary and the raw packet. + """ + report = [] + for packet in packet_list: + report.append({'raw_packet': str(packet), + 'summary': str(packet.summary())}) + return report + +class BaseScapyTest(NetTestCase): + """ + The report of a test run with scapy looks like this: + + report: + sent_packets: [{'raw_packet': BASE64Encoding of packet, + 'summary': 'IP / TCP 192.168.2.66:ftp_data > 8.8.8.8:http S'] + answered_packets: [] + + """ + name = "Base Scapy Test" + version = 0.1 + + requiresRoot = True + + sentPackets = [] + answeredPackets = [] + + def sr(self, *arg, **kw): + """ + Wrapper around scapy.sendrecv.sr for sending and receiving of packets + at layer 3. + """ + answered_packets, sent_packets = sr(*arg, **kw) + self.report['answered_packets'] = createPacketReport(answered_packets) + self.report['sent_packets'] = createPacketReport(sent_packets) + return (answered_packets, sent_packets) + + def send(self, *arg, **kw): + """ + Wrapper around scapy.sendrecv.send for sending of packets at layer 3 + """ + sent_packets = send(*arg, **kw) + self.report['sent_packets'] = createPacketReport(sent_packets) + return sent_packets + +class TXScapyTest(BaseScapyTest): """ A utility class for writing scapy driven OONI tests.
@@ -25,8 +72,10 @@ class ScapyTest(NetTestCase): * timeout: timeout in ms of when we should stop waiting to receive packets
* receive: if we should also receive packets and not just send + + XXX This is currently not working """ - name = "Scapy Test" + name = "TX Scapy Test" version = 0.1
receive = True @@ -38,15 +87,6 @@ class ScapyTest(NetTestCase): answered = None unanswered = None
- - def setUp(self): - if not self.reactor: - from twisted.internet import reactor - self.reactor = reactor - self.questions = [] - self.answers = [] - self.processInputs() - def processInputs(self): """ Place here the logic for validating and processing of inputs and @@ -105,42 +145,3 @@ class ScapyTest(NetTestCase): """ pass
-class BlockingScapyTest(ScapyTest): - """ - This is a very basic Scapy Test template that does not do all the - multithreading kung-fu of txscapy, but maintains the same API. - - This will allow tests implemented using the BlockingScapyTest API to easily - migrate to the new API. - """ - name = "Blocking Scapy Test" - version = 0.1 - - timeout = None - - answered = None - unanswered = None - - def sendReceivePackets(self): - packets = self.buildPackets() - - log.debug("Sending and receiving %s" % packets) - - self.answered, self.unanswered = sr(packets, timeout=self.timeout) - - log.debug("%s %s" % (ans, unans)) - - def sendPackets(self): - packets = self.buildPackets() - - log.debug("Sending packets %s" % packets) - - send(packets) - - - def buildPackets(self): - """ - Override this method to build scapy packets. - """ - pass -