
commit 1ca2705d208febeea2e8cc400929c170fd875327 Author: Arturo Filastò <art@fuffa.org> Date: Mon Nov 12 09:48:08 2012 +0100 Progress on porting daph3 to new API. The client component --- nettests/core/daphn3.py | 122 ++++++++++++++++++++++------------------------- 1 files changed, 57 insertions(+), 65 deletions(-) diff --git a/nettests/core/daphn3.py b/nettests/core/daphn3.py index ed65963..cc4803c 100644 --- a/nettests/core/daphn3.py +++ b/nettests/core/daphn3.py @@ -1,47 +1,33 @@ +# -*- encoding: utf-8 -*- from twisted.python import usage from twisted.internet import protocol, endpoints, reactor +from ooni import nettest from ooni.kit import daphn3 from ooni.utils import log -class Daphn3ClientProtocol(daphn3.Daphn3Protocol): - def connectionMade(self): - self.next_state() - class Daphn3ClientFactory(protocol.ClientFactory): - protocol = Daphn3ClientProtocol - mutator = None - steps = None - test = None + protocol = daphn3.Daphn3Protocol + def __init__(self, steps): + self.steps = steps def buildProtocol(self, addr): - p = self.protocol() + p = self.protocol(steps=self.steps) p.factory = self - p.test = self.test - - if self.steps: - p.steps = self.steps - - if not self.mutator: - self.mutator = daphn3.Mutator(p.steps) + return p - else: - print "Moving on to next mutation" - self.mutator.next() + def startedConnecting(self, connector): + print "Started connecting %s" % connector - p.mutator = self.mutator - p.current_state = self.mutator.state() - return p + def clientConnectionFailed(self, reason, connector): + log.err("We failed connecting the the OONIB") + log.err("Cannot perform test. Perhaps it got blocked?") + log.err("Please report this to tor-assistants@torproject.org") - def clientConnectionFailed(self, reason): - print "We failed connecting the the OONIB" - print "Cannot perform test. Perhaps it got blocked?" - print "Please report this to tor-assistants@torproject.org" - self.test.result['error'] = ('Failed in connecting to OONIB', reason) - self.test.end(d) + def clientConnectionLost(self, reason, connector): + log.err("Daphn3 client connection lost") + print reason - def clientConnectionLost(self, reason): - print "Connection Lost." class daphn3Args(usage.Options): optParameters = [ @@ -58,57 +44,63 @@ class daphn3Test(nettest.NetTestCase): inputFile = ['file', 'f', None, 'Specify the pcap or YAML file to be used as input to the test'] - requiredOptions = ['file'] + #requiredOptions = ['file'] steps = None def inputProcessor(self, fp): - if self.localOptions['pcap']: - self.steps = daphn3.read_pcap(self.localOptions['pcap']) - else: - self.steps = daphn3.read_yaml(self.localOptions['yaml']) + """ + step_idx is the step in the packet exchange + ex. + [.X.] are packets sent by a client or a server - def setUp(self): - self.factory = Daphn3ClientFactory() - self.factory.test = self + client: [.1.] [.3.] [.4.] + server: [.2.] [.5.] + mutation_idx: is the sub index of the packet as in the byte of the + packet at the step_idx that is to be mutated + + """ if self.localOptions['pcap']: - self.steps = daphn3.read_pcap(self.localOptions['pcap']) + daphn3Steps = daphn3.read_pcap(self.localOptions['pcap']) elif self.localOptions['yaml']: - self.steps = daphn3.read_yaml(self.localOptions['yaml']) + daphn3Steps = daphn3.read_yaml(self.localOptions['yaml']) else: - raise usage.UsageError("You must specify either --pcap or --yaml") - - mutations = 0 - for x in self.steps: - mutations += len(x['data']) - return {'mutation': range(mutations)} + daphn3Steps = [{'client': 'testing'}, {'server': 'antani'}] + + for idx, step in enumerate(daphn3Steps): + current_packet = step.values()[0] + for mutation_idx in range(len(current_packet)): + if step.keys()[0] == "client": + mutated_step = daphn3.daphn3Mutate(daphn3Steps, + idx, mutation_idx) + yield mutated_step + else: + yield daphn3Steps - def control(self, exp_res, args): - try: - mutation = self.factory.mutator.get(0) - self.result['censored'] = False - except: - mutation = None + def setUp(self): + self.factory = Daphn3ClientFactory(self.input) + self.factory.report = self.report + print "Just set the factory to %s with %s" % (self.factory, + self.input) - return {'mutation_number': args['mutation'], - 'value': mutation} + def test_daphn3(self): + host = self.localOptions['host'] + port = int(self.localOptions['port']) - def _failure(self, *argc, **kw): - self.report['censored'] = True - self.report['mutation'] = - self.report['error'] = ('Failed in connecting', (argc, kw)) + def failure(failure): + log.msg("Failed to connect") + self.report['censored'] = True + self.report['mutation'] = 0 - def test_daphn3(self): - log.msg("Doing mutation %s" % args['mutation']) - self.factory.steps = self.steps + def success(protocol): + log.msg("Successfully connected") + protocol.sendMutation() - host = self.local_options['host'] - port = int(self.local_options['port']) log.msg("Connecting to %s:%s" % (host, port)) - endpoint = endpoints.TCP4ClientEndpoint(reactor, host, port) d = endpoint.connect(self.factory) - d.addErrback(self._failure) + d.addErrback(failure) + d.addCallback(success) return d