def test_sniff_loop(self): def cbk(ts, pkt, *args): assert pkt.ip.src == "1.2.3.6" assert pkt.tcp.srcport == 99 assert ts > 0 assert len(args) == 2 if args[0] > args[1]: raise UMPASniffingException("test") th = SendPacket(umit.umpa.Packet(IP(src="1.2.3.6"), TCP(srcport=99)), 2) th.start() umit.umpa.sniffing.sniff_loop(1, filter="src 1.2.3.6", device='any', callback=cbk, callback_args=[1, 2]) th.join() th = SendPacket(umit.umpa.Packet(IP(src="1.2.3.6"), TCP(srcport=99))) th.start() py.test.raises(UMPASniffingException, umit.umpa.sniffing.sniff_loop, 1, filter="src 1.2.3.6", device='any', callback=cbk, callback_args=[2, 1]) th.join() py.test.raises(UMPASniffingException, umit.umpa.sniffing.sniff_loop, 1)
def test_to_file(self): dump_file = tempfile.NamedTemporaryFile(mode="w") amount = 5 th = SendPacket(umit.umpa.Packet(IP(src="1.2.3.4"), TCP(srcport=99)), amount) th.start() try: umit.umpa.sniffing.to_file(dump_file.name, amount, "src host 1.2.3.4 and src port 99", "any") except UMPASniffingException: py.test.skip("no suitable devices for sniffing found. " "propably not sufficent priviliges.") finally: th.join() result = umit.umpa.sniffing.from_file(dump_file.name) assert len(result) == amount for packet in result: assert packet.ip.src == "1.2.3.4" assert packet.tcp.srcport == 99 result = umit.umpa.sniffing.from_file(dump_file.name, 2) assert len(result) == 2 for packet in result: assert packet.ip.src == "1.2.3.4" assert packet.tcp.srcport == 99
def test_dump(self): amount = 5 dump_file = tempfile.NamedTemporaryFile(mode="w") th = SendPacket(umit.umpa.Packet(IP(src="1.2.3.4"), TCP(srcport=99)), amount) th.start() try: p = pypcap.open_pcap("any", to_ms=100) p.setfilter("src host 1.2.3.4 and src port 99") d = pypcap.dumper() d.open(p, dump_file.name) pkts = [] for i in xrange(amount): pkts.append(p.next()) d.dump() except UMPASniffingException: py.test.skip("no suitable devices for sniffing found. " "propably not sufficent priviliges.") finally: th.join() d.close() p = pypcap.open_pcap(dump_file.name) p.setfilter("src host 1.2.3.4 and src port 99") for i, pkt in enumerate(p): assert pkt[0] == pkts[i][0] # not sure if the below should be equivalent assert str(pkt[1]) == str(pkts[i][1])
def test_sniff_next(self): th = SendPacket(umit.umpa.Packet(IP(src="1.2.3.4"), TCP(srcport=99))) th.start() result = umit.umpa.sniffing.sniff_next(device='any', filter="src port 99") th.join() assert result.ip.src == '1.2.3.4' assert result.tcp.srcport == 99 # send more, sniff one th = SendPacket(umit.umpa.Packet(IP(src="1.2.3.4"), TCP(srcport=99)), 5) th.start() result = umit.umpa.sniffing.sniff_next(device='any', filter="src port 99") th.join() assert result.ip.src == '1.2.3.4' assert result.tcp.srcport == 99
def test_sniff(self): th = SendPacket(umit.umpa.Packet(IP(src="1.2.3.4"), TCP(srcport=99))) th.start() result = umit.umpa.sniffing.sniff(1, device='any', filter="src port 99") th.join() assert len(result) == 1 assert result[0].ip.src == '1.2.3.4' assert result[0].tcp.srcport == 99
def test_sndout(self): packet = umit.umpa.Packet(IP(src="1.2.3.4", dst="67.205.14.183"), TCP(srcport=81, dstport=80)) th = SendPacket(packet) th.start() received = umit.umpa.sniffing.sniff_next(filter="dst 67.205.14.183", device="any") th.join() assert received.ip.src == packet.ip.src assert received.ip.dst == packet.ip.dst assert received.tcp.srcport == packet.tcp.srcport assert received.tcp.dstport == packet.tcp.dstport
def test_loop_and_filter(self): # XXX it would rather looped than failing def cbk(timestamp, pkt, *args): assert args[0] == "foobar" # stupid isn't it? :) try: p = pypcap.open_pcap("any", to_ms=100) p.setfilter("src host 1.2.3.4 and src port 99") th = SendPacket(umit.umpa.Packet(IP(src="1.2.3.4"), TCP(srcport=99))) th.start() p.loop(1, cbk, "foobar") th.join() th = SendPacket(umit.umpa.Packet(IP(src="1.2.3.4"), TCP(srcport=99)), 5) th.start() p.loop(5, cbk, "foobar") th.join() except UMPASniffingException: py.test.skip("no suitable devices for sniffing found. " "propably not sufficent priviliges.")
def test_sndrcv_loopback(self): packet = umit.umpa.Packet(IP(src="1.2.3.4", dst="127.0.0.1"), TCP(srcport=99), Payload(data="foo bar")) th = SendPacket(packet) th.start() received = umit.umpa.sniffing.sniff_next(filter="src 1.2.3.4", device="lo") th.join() assert received.ip.src == packet.ip.src assert received.ip.dst == packet.ip.dst assert received.tcp.srcport == packet.tcp.srcport assert received.tcp.dstport == packet.tcp.dstport assert received.payload.data == packet.payload.data
def test_next(self): # can't test iterable of the object # because pypcap doesn't raise StopIteration amount = 5 th = SendPacket(umit.umpa.Packet(IP(src="1.2.3.4"), TCP(srcport=99)), amount) th.start() try: p = pypcap.open_pcap("any", to_ms=100) p.setfilter("src host 1.2.3.4 and src port 99") for i in xrange(amount): packet = p.next() except UMPASniffingException: py.test.skip("no suitable devices for sniffing found. " "propably not sufficent priviliges.") finally: th.join()
def test_from_file_loop(self): global idx idx = 0 def cbk(ts, pkt, *args): global idx assert pkt.ip.src == "1.2.3.6" assert pkt.tcp.srcport == 99 assert ts > 0 assert len(args) == 1 idx += 1 dump_file = tempfile.NamedTemporaryFile(mode="w") th = SendPacket(umit.umpa.Packet(IP(src="1.2.3.6"), TCP(srcport=99)), 3) th.start() umit.umpa.sniffing.sniff(3, device="any", dump=dump_file.name, filter="src host 1.2.3.6 and src port 99") th.join() idx = 0 amount = 3 umit.umpa.sniffing.from_file_loop(dump_file.name, callback=cbk, callback_args=[ amount, ]) assert idx == amount idx = 0 amount = 2 umit.umpa.sniffing.from_file_loop(dump_file.name, 2, callback=cbk, callback_args=[ amount, ]) assert idx == amount
def test_from_file(self): dump_file = tempfile.NamedTemporaryFile(mode="w") th = SendPacket(umit.umpa.Packet(IP(src="1.2.3.6"), TCP(srcport=99)), 3) th.start() umit.umpa.sniffing.sniff(3, device="any", dump=dump_file.name, filter="src host 1.2.3.6 and src port 99") th.join() result = umit.umpa.sniffing.from_file(dump_file.name) assert len(result) == 3 for packet in result: assert packet.ip.src == "1.2.3.6" assert packet.tcp.srcport == 99 result = umit.umpa.sniffing.from_file(dump_file.name, 2) assert len(result) == 2 for packet in result: assert packet.ip.src == "1.2.3.6" assert packet.tcp.srcport == 99