scapy/test/pipetool.uts

298 lines
4.7 KiB
Plaintext
Raw Normal View History

2017-05-02 19:58:57 +00:00
########################
% Pipetool related tests
########################
+ Basic tests
= Test default test case
s = PeriodicSource("hello", 1, name="src")
d1 = Drain(name="d1")
c = ConsoleSink(name="c")
tf = TransformDrain(lambda x:"Got %r" % x)
t = TermSink(name="t", keepterm=False)
s > d1 > c
d1 > tf > t
p = PipeEngine(s)
p.graph(type="png",target="> /tmp/pipe.png")
p.start()
time.sleep(3)
p.stop()
2017-05-08 21:24:12 +00:00
= Test add_pipe
s = AutoSource()
p = PipeEngine(s)
p.add(Pipe())
assert len(p.active_pipes) == 2
x = p.spawn_Pipe()
assert len(p.active_pipes) == 3
assert isinstance(x, Pipe)
2017-05-12 16:20:14 +00:00
= Test exhausted source
s = AutoSource()
s._gen_data("hello")
s.is_exhausted = True
d1 = Drain(name="d1")
c = ConsoleSink(name="c")
s > d1 > c
p = PipeEngine(s)
p.start()
p.wait_and_stop()
= Test add_pipe on running instance
test_val = None
class TestSink(Sink):
def push(self, msg):
global test_val
test_val = msg
p = PipeEngine()
p.start()
s = AutoSource()
s._gen_data("hello")
s.is_exhausted = True
d1 = Drain(name="d1")
c = TestSink(name="c")
s > d1 > c
p.add(s)
2017-06-05 11:25:10 +00:00
2017-05-12 16:20:14 +00:00
p.wait_and_stop()
assert test_val == "hello"
2017-05-08 21:24:12 +00:00
= Test Operators
s = AutoSource()
p = PipeEngine(s)
assert p == p
assert not p < p
assert not p > p
a = AutoSource()
b = AutoSource()
a >> b
assert len(a.high_sinks) == 1
assert len(a.high_sources) == 0
assert len(b.high_sinks) == 0
assert len(b.high_sources) == 1
a
b
a = AutoSource()
b = AutoSource()
a << b
assert len(a.high_sinks) == 0
assert len(a.high_sources) == 1
assert len(b.high_sinks) == 1
assert len(b.high_sources) == 0
a
b
2017-05-12 16:20:14 +00:00
a = AutoSource()
b = AutoSource()
a == b
assert len(a.sinks) == 1
assert len(a.sources) == 1
assert len(b.sinks) == 1
assert len(b.sources) == 1
a = AutoSource()
b = AutoSource()
a//b
assert len(a.high_sinks) == 1
assert len(a.high_sources) == 1
assert len(b.high_sinks) == 1
assert len(b.high_sources) == 1
a = AutoSource()
b = AutoSource()
a^b
assert len(b.trigger_sources) == 1
assert len(a.trigger_sinks) == 1
2017-05-08 21:24:12 +00:00
= Test doc
s = AutoSource()
p = PipeEngine(s)
p.list_pipes()
p.list_pipes_detailed()
2017-05-13 11:13:37 +00:00
= Test RawConsoleSink with CLIFeeder
p = PipeEngine()
s = CLIFeeder()
s.send("hello")
s.is_exhausted = True
r, w = os.pipe()
d1 = Drain(name="d1")
c = RawConsoleSink(name="c")
c._write_pipe = w
s > d1 > c
p.add(s)
2017-06-05 11:25:10 +00:00
p.start()
2017-05-13 11:13:37 +00:00
assert os.read(r, 20) == "hello\n"
2017-06-05 11:25:10 +00:00
p.wait_and_stop()
2017-05-13 11:13:37 +00:00
= Test QueueSink with CLIFeeder
p = PipeEngine()
s = CLIFeeder()
s.send("hello")
s.is_exhausted = True
d1 = Drain(name="d1")
c = QueueSink(name="c")
s > d1 > c
p.add(s)
2017-06-05 11:25:10 +00:00
p.start()
2017-05-13 11:13:37 +00:00
p.wait_and_stop()
assert c.recv() == "hello"
= Test UpDrain
test_val = None
class TestSink(Sink):
def high_push(self, msg):
global test_val
test_val = msg
p = PipeEngine()
s = CLIFeeder()
s.send("hello")
s.is_exhausted = True
d1 = UpDrain(name="d1")
c = TestSink(name="c")
s > d1
d1 >> c
p.add(s)
2017-06-05 11:25:10 +00:00
p.start()
2017-05-13 11:13:37 +00:00
p.wait_and_stop()
assert test_val == "hello"
= Test DownDrain
test_val = None
class TestSink(Sink):
def push(self, msg):
global test_val
test_val = msg
p = PipeEngine()
s = CLIHighFeeder()
s.send("hello")
s.is_exhausted = True
d1 = DownDrain(name="d1")
c = TestSink(name="c")
s >> d1
d1 > c
p.add(s)
2017-06-05 11:25:10 +00:00
p.start()
2017-05-13 11:13:37 +00:00
p.wait_and_stop()
assert test_val == "hello"
2017-09-07 22:49:41 +00:00
+ Advanced ScapyPipes pipetools tests
= Test SniffSource
~ netaccess
p = PipeEngine()
s = SniffSource()
d1 = Drain(name="d1")
c = QueueSink(name="c")
s > d1 > c
p.add(s)
p.start()
sniff(count=3)
p.stop()
assert c.q.get()
= Test RdpcapSource and WrpcapSink
~ needs_root
req = Ether()/IP()/ICMP()
rpy = Ether()/IP('E\x00\x00\x1c\x00\x00\x00\x004\x01\x1d\x04\xd8:\xd0\x83\xc0\xa8\x00w\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
wrpcap("t.pcap", [req, rpy])
p = PipeEngine()
s = RdpcapSource("t.pcap")
d1 = Drain(name="d1")
c = WrpcapSink("t2.pcap", name="c")
s > d1 > c
p.add(s)
p.start()
p.wait_and_stop()
results = rdpcap("t2.pcap")
assert str(results[0]) == str(req)
assert str(results[1]) == str(rpy)
os.unlink("t.pcap")
os.unlink("t2.pcap")
= Test InjectSink and Inject3Sink
~ needs_root
import mock
2017-09-07 22:49:41 +00:00
a = IP(dst="192.168.0.1")/ICMP()
msgs = []
class FakeSocket(object):
def __init__(self, *arg, **karg):
pass
def close(self):
pass
def send(self, msg):
global msgs
msgs.append(msg)
@mock.patch("scapy.scapypipes.conf.L2socket", FakeSocket)
@mock.patch("scapy.scapypipes.conf.L3socket", FakeSocket)
def _inject_sink(i3):
s = CLIFeeder()
s.send(a)
s.is_exhausted = True
d1 = Drain(name="d1")
c = Inject3Sink() if i3 else InjectSink()
s > d1 > c
p = PipeEngine(s)
p.start()
p.wait_and_stop()
_inject_sink(False) # InjectSink
_inject_sink(True) # Inject3Sink
assert msgs == [a,a]