pf tests: Accelerate tests

Make the tests run slightly faster by having pft_ping.py end the capture
of packets as soon as it sees the expected packet, rather than
continuing to sniff.

MFC after:	2 weeks
This commit is contained in:
Kristof Provost 2019-03-07 11:09:29 +00:00
parent fdca34e4a5
commit 16b56c7f4e
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=344876

View file

@ -8,26 +8,38 @@
PAYLOAD_MAGIC = 0x42c0ffee
class Sniffer(threading.Thread):
def __init__(self, recvif):
def __init__(self, args, check_function):
threading.Thread.__init__(self)
self._recvif = recvif
self._args = args
self._recvif = args.recvif[0]
self._check_function = check_function
self.foundCorrectPacket = False
self.start()
def _checkPacket(self, packet):
ret = self._check_function(self._args, packet)
if ret:
self.foundCorrectPacket = True
return ret
def run(self):
self.packets = sp.sniff(iface=self._recvif, timeout=3)
self.packets = sp.sniff(iface=self._recvif,
stop_filter=self._checkPacket, timeout=3)
def check_ping_request(packet, dst_ip, args):
def check_ping_request(args, packet):
if args.ip6:
return check_ping6_request(packet, dst_ip, args)
return check_ping6_request(args, packet)
else:
return check_ping4_request(packet, dst_ip, args)
return check_ping4_request(args, packet)
def check_ping4_request(packet, dst_ip, args):
def check_ping4_request(args, packet):
"""
Verify that the packet matches what we'd have sent
"""
dst_ip = args.to[0]
ip = packet.getlayer(sp.IP)
if not ip:
return False
@ -54,13 +66,14 @@ def check_ping4_request(packet, dst_ip, args):
% (ip.tos, args.expect_tos[0])
return False
return True
def check_ping6_request(packet, dst_ip, args):
def check_ping6_request(args, packet):
"""
Verify that the packet matches what we'd have sent
"""
dst_ip = args.to[0]
ip = packet.getlayer(sp.IPv6)
if not ip:
return False
@ -124,7 +137,7 @@ def main():
sniffer = None
if not args.recvif is None:
sniffer = Sniffer(args.recvif[0])
sniffer = Sniffer(args, check_ping_request)
if args.ip6:
ping6(args.sendif[0], args.to[0], args)
@ -134,12 +147,10 @@ def main():
if sniffer:
sniffer.join()
for packet in sniffer.packets:
if check_ping_request(packet, args.to[0], args):
sys.exit(0)
# We did not get the packet we expected
sys.exit(1)
if sniffer.foundCorrectPacket:
sys.exit(0)
else:
sys.exit(1)
if __name__ == '__main__':
main()