From 9e0e882ecce99df6e81ad63714b9dc3bd3126f27 Mon Sep 17 00:00:00 2001 From: Pierre LALET <pierre.lalet@cea.fr> Date: Wed, 11 Jan 2017 11:28:53 +0100 Subject: [PATCH] Add new IP-related tests --- scapy/layers/inet.py | 2 +- test/regression.uts | 46 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/scapy/layers/inet.py b/scapy/layers/inet.py index 760e0675..54fffc48 100644 --- a/scapy/layers/inet.py +++ b/scapy/layers/inet.py @@ -42,7 +42,7 @@ class IPTools(object): t.sort() return t[t.index(self.ttl)+1] def hops(self): - return self.ottl()-self.ttl-1 + return self.ottl() - self.ttl _ip_options_names = { 0: "end_of_list", diff --git a/test/regression.uts b/test/regression.uts index 10d09fcc..29c3e01a 100644 --- a/test/regression.uts +++ b/test/regression.uts @@ -622,6 +622,8 @@ assert(x.PDU.varbindlist[2].value == 1) ~ netaccess IP ICMP x=sr1(IP(dst="www.google.com")/ICMP(),timeout=3) x +assert x[IP].ottl() in [32, 64, 128, 255] +assert 0 <= x[IP].hops() <= 126 x is not None and ICMP in x and x[ICMP].type == 0 = DNS request @@ -631,6 +633,10 @@ x is not None and ICMP in x and x[ICMP].type == 0 dns_ans = sr1(IP(dst="resolver1.opendns.com")/UDP()/DNS(rd=1,qd=DNSQR(qname="www.slashdot.com")),timeout=5) DNS in dns_ans += Whois request +~ netaccess IP +IP(src="8.8.8.8").whois() + ############ ############ @@ -4877,6 +4883,46 @@ assert len(defrags) == 1 * which should be the same as pkt reconstructed assert defrags[0] == IP(str(pkt)) += Packet().fragment() +payloadlen, fragsize = 100, 8 +assert fragsize % 8 == 0 +fragcount = (payloadlen / fragsize) + bool(payloadlen % fragsize) +* create the packet +pkt = IP() / ("X" * payloadlen) +* create the fragments +frags = pkt.fragment(fragsize) +* count the fragments +assert len(frags) == fragcount +* each fragment except the last one should have MF set +assert all(p.flags == 1 for p in frags[:-1]) +assert frags[-1].flags == 0 +* each fragment except the last one should have a payload of fragsize bytes +assert all(len(p.payload) == 8 for p in frags[:-1]) +assert len(frags[-1].payload) == ((payloadlen % fragsize) or fragsize) + += Packet().fragment() and overloaded_fields +pkt1 = Ether() / IP() / UDP() +pkt2 = pkt1.fragment()[0] +pkt3 = pkt2.__class__(str(pkt2)) +assert pkt1[IP].proto == pkt2[IP].proto == pkt3[IP].proto + += Packet().fragment() already fragmented packets +payloadlen = 1480 * 3 +ffrags = (IP() / ("X" * payloadlen)).fragment(1480) +ffrags = reduce(lambda x, y: x + y, (pkt.fragment(1400) for pkt in ffrags)) +len(ffrags) == 6 +* each fragment except the last one should have MF set +assert all(p.flags == 1 for p in ffrags[:-1]) +assert ffrags[-1].flags == 0 +* fragment offset should be well computed +plen = 0 +for p in ffrags: + assert p.frag == plen / 8 + plen += len(p.payload) + +assert plen == payloadlen + + ############ ############ + TCP/IP tests -- GitLab