def test_tcpv4_compare(self): """Test the underlying __compare__ functionality of the packet. Two packets constructed from the same bytes should be equal and two that are not should not be equal.""" file = PcapConnector("wwwtcp.out") packet = file.read() ip = ipv4(packet[file.dloff:len(packet)]) tcp1 = tcp(ip.data.bytes) tcp2 = tcp(ip.data.bytes) assert (tcp1 != None) assert (tcp2 != None) #hd = hexdumper() #print hd.dump(tcp1.bytes) #print hd.dump(tcp2.bytes) # tcp1 should not equal tcp2, they are different instances, # and will therefore have different timestamps -- unless # we end up racing the system clock. self.assertNotEqual(tcp1, tcp2, "instances SHOULD be equal") self.assertEqual(tcp1.bytes, tcp2.bytes, "packet data SHOULD be equal") tcp1.dport = 0 self.assertNotEqual(tcp1.bytes, tcp2.bytes, "packet data SHOULD NOT be equal")
def test_tcpv4_read(self): """This test reads from a pre-stored pcap file generated with tcpdump.""" file = PcapConnector("wwwtcp.out") packet = file.read() ip = ipv4(packet[file.dloff:len(packet)]) assert (ip != None) tcppacket = tcp(ip.data.bytes) self.assertEqual(tcppacket.sport, 53678, "source port not equal %d" % tcppacket.sport) self.assertEqual(tcppacket.dport, 80, "destination port not equal %d" % tcppacket.dport) self.assertEqual(tcppacket.sequence, 1351059655, "sequence number not equal %d" % tcppacket.sequence) self.assertEqual(tcppacket.ack_number, 0, "ack number not equal %d" % tcppacket.ack_number) self.assertEqual(tcppacket.offset, 11, "offset not equal %d" % tcppacket.offset) self.assertEqual(tcppacket.reserved, 0, "reserved not equal %d" % tcppacket.reserved) self.assertEqual(tcppacket.urgent, 0, "urgent not equal %d" % tcppacket.urgent) self.assertEqual(tcppacket.ack, 0, "ack not equal %d" % tcppacket.ack) self.assertEqual(tcppacket.push, 0, "push not equal %d" % tcppacket.push) self.assertEqual(tcppacket.reset, 0, "reset not equal %d" % tcppacket.reset) self.assertEqual(tcppacket.syn, 1, "syn not equal %d" % tcppacket.syn) self.assertEqual(tcppacket.fin, 0, "fin not equal %d" % tcppacket.fin) self.assertEqual(tcppacket.window, 65535, "window not equal %d" % tcppacket.window) self.assertEqual(tcppacket.checksum, 15295, "checksum not equal %d" % tcppacket.checksum)
def test_ipv4_time(self): """Test the timestamp setting facility.""" import time file = PcapConnector("loopping.out") packet = file.readpkt() ip = packet.data self.assertEqual(packet.timestamp, ip.timestamp, "lower and upper layer timestamps are different but should not be")
def test_pcap_read(self): """This test reads from a pre-stored pcap file generated with tcpdump and ping on the loopback interface.""" file = PcapConnector("etherping.out") ether = file.readpkt() assert (ether != None) self.assertEqual(ether.dst, "\x00\x10\xdb\x3a\x3a\x77", "dst not equal %s" % ether.src) self.assertEqual(ether.src, "\x00\x0d\x93\x44\xfa\x62", "src not equal %s" % ether.dst) self.assertEqual(ether.type, 0x800, "type not equal %d" % ether.type)
def test_ipv4_time(self): """Test the timestamp setting facility.""" import time file = PcapConnector("loopping.out") packet = file.readpkt() ip = packet.data self.assertEqual( packet.timestamp, ip.timestamp, "lower and upper layer timestamps are different but should not be")
def test_ipv4_compare(self): """Test the underlying __compare__ functionality of the packet. Two packets constructed from the same bytes should be equal and two that are not should not be equal.""" file = PcapConnector("loopping.out") packet = file.readpkt() ip1 = packet.data ip2 = ipv4(packet.data.bytes) assert (ip1 != None) assert (ip2 != None) self.assertEqual(ip1, ip2, "packets should be equal but are not") ip1.dst = 0xffffffffL self.assertNotEqual(ip1, ip2, "packets compare equal but should not")
def test_ethernet_compare(self): """Test the underlying __compare__ functionality of the packet. Two packets constructed from the same bytes should be equal and two that are not should not be equal.""" file = PcapConnector("etherping.out") packet = file.read() ether1 = ethernet(packet[0:file.dloff]) ether2 = ethernet(packet[0:file.dloff]) assert (ether1 != None) assert (ether2 != None) self.assertEqual(ether1, ether2, "packets should be equal but are not") ether1.dst = "\xff\xff\xff\xff\xff\xff" self.assertNotEqual(ether1, ether2, "packets compare equal but should not")
def test_tcpv4_println(self): """Test the println method.""" file = PcapConnector("wwwtcp.out") packet = file.read() ip = ipv4(packet[file.dloff:len(packet)]) assert (ip != None) tcppacket = tcp(ip.data.bytes) assert (tcppacket) # pre tcp options: #expected = "<TCP: sport: 53678, dport: 80, sequence: 1351059655, ack_number: 0, offset: 11, reserved: 0, urgent: 0, ack: 0, push: 0, reset: 0, syn: 1, fin: 0, window: 65535, checksum: 15295, urg_pointer: 0>" # post tcp options: # XXX println() uses __repr__(), not __str__(). the rules for the # game "python" say we have to preserve the structure of # objects returned by __repr__(). expected = "<TCP: sport: 53678, dport: 80, sequence: 1351059655, " \ "ack_number: 0, offset: 11, reserved: 0, " \ "ns: 0, cwr: 0, ece: 0, urgent: 0, " \ "ack: 0, push: 0, reset: 0, syn: 1, fin: 0, " \ "window: 65535, checksum: 15295, urg_pointer: 0, " \ "options: [" \ "[Field: mss, Value: " \ "<pcs.Field name v, 16 bits, " \ "default 1460, discriminator 0>], " \ "[Field: nop, Value: 1], " \ "[Field: wscale, Value: " \ "<pcs.Field name v, 8 bits, " \ "default 0, discriminator 0>], " \ "[Field: nop, Value: 1], " \ "[Field: nop, Value: 1], " \ "[Field: tstamp, Value: " \ "<pcs.Field name v, 64 bits, " \ "default 0, discriminator 0>], " \ "[Field: sackok, Value: " \ "<pcs.Field name v, 0 bits, " \ "default 0, discriminator 0>], " \ "[Field: end, Value: 0], " \ "[Field: end, Value: 0]" \ "]>" # unusual naming to make it easier to spot deltas in an # 80 column display. gotttted = tcppacket.println() self.assertEqual( expected, gotttted, "strings are not equal \nexpected %s \ngotttted %s " % (expected, gotttted))
def test_sctp_read(self): """Read a packet from a pcap file.""" file = PcapConnector("sctp.pcap") packet = file.readpkt() sctp = packet.data.data assert (sctp != None) expected = "<SCTP common header class: sport: 16384, dport: 2944, " \ "tag: 93962, checksum: 1840257154>" got = sctp.println() self.assertEqual(expected, got, \ "strings are not equal \nexpected %s \ngot %s " % \ (expected, got))
def test_ipv6_compare(self): """Test the underlying __compare__ functionality of the packet. Two packets constructed from the same bytes should be equal and two that are not should not be equal.""" file = PcapConnector("loopping6.out") packet = file.read() ip1 = ipv6(packet[file.dloff:len(packet)]) ip2 = ipv6(packet[file.dloff:len(packet)]) assert (ip1 != None) assert (ip2 != None) self.assertEqual(ip1, ip2, "packets should be equal but are not") ip1.dst = inet_pton(AF_INET6, "2001:ffff::1") self.assertNotEqual(ip1, ip2, "packets compare equal but should not")
def test_dhcpv4_decode(self): """This test reads from a pre-stored pcap file.""" file = PcapConnector("dhcp_minimal.pcap") packet = file.readpkt() chain = packet.chain() #print chain ether = chain.packets[0] assert (ether != None) ip = chain.packets[1] assert (ip != None) udp = chain.packets[2] assert (udp != None) dhcp = chain.packets[3] assert (dhcp != None) self.assertEqual(dhcp.op, 1, "op not equal") self.assertEqual(dhcp.xid, 0xffff0001, "xid not equal") self.assertEqual(dhcp.secs, 42848, "secs not equal") self.assertEqual(dhcp.flags, 0x8000, "flags not equal") mac = ether_atob("52:54:00:12:34:56") #print ether_btoa(dhcp.chaddr) self.assertEqual(dhcp.chaddr[:dhcp.hlen], mac, "mac not equal") self.assertEqual( len(dhcp.options), 6, "wrong option field count %d should be %d" % (len(dhcp.options), 6)) # Not a tlv field self.assertEqual(dhcp.options[0], pcs.packets.dhcpv4_options.DHCP_OPTIONS_COOKIE, "invalid cookie value") # TLV fields self.assertEqual(dhcp.options[1].value, 1460, "invalid maximum message size value") self.assertEqual(dhcp.options[2].value, "FreeBSD:i386:6.3-RELEASE", \ "invalid vendor class") self.assertEqual(dhcp.options[3].value, pcs.packets.dhcpv4_options.DHCPDISCOVER, "invalud dhcp message type") # Not a tlv field self.assertEqual(dhcp.options[4], pcs.packets.dhcpv4_options.DHO_END, "invalid end value")
def test_tcpv4_println(self): """Test the println method.""" file = PcapConnector("wwwtcp.out") packet = file.read() ip = ipv4(packet[file.dloff:len(packet)]) assert (ip != None) tcppacket = tcp(ip.data.bytes) assert (tcppacket) # pre tcp options: #expected = "<TCP: sport: 53678, dport: 80, sequence: 1351059655, ack_number: 0, offset: 11, reserved: 0, urgent: 0, ack: 0, push: 0, reset: 0, syn: 1, fin: 0, window: 65535, checksum: 15295, urg_pointer: 0>" # post tcp options: # XXX println() uses __repr__(), not __str__(). the rules for the # game "python" say we have to preserve the structure of # objects returned by __repr__(). expected = "<TCP: sport: 53678, dport: 80, sequence: 1351059655, " \ "ack_number: 0, offset: 11, reserved: 0, " \ "ns: 0, cwr: 0, ece: 0, urgent: 0, " \ "ack: 0, push: 0, reset: 0, syn: 1, fin: 0, " \ "window: 65535, checksum: 15295, urg_pointer: 0, " \ "options: [" \ "[Field: mss, Value: " \ "<pcs.Field name v, 16 bits, " \ "default 1460, discriminator 0>], " \ "[Field: nop, Value: 1], " \ "[Field: wscale, Value: " \ "<pcs.Field name v, 8 bits, " \ "default 0, discriminator 0>], " \ "[Field: nop, Value: 1], " \ "[Field: nop, Value: 1], " \ "[Field: tstamp, Value: " \ "<pcs.Field name v, 64 bits, " \ "default 0, discriminator 0>], " \ "[Field: sackok, Value: " \ "<pcs.Field name v, 0 bits, " \ "default 0, discriminator 0>], " \ "[Field: end, Value: 0], " \ "[Field: end, Value: 0]" \ "]>" # unusual naming to make it easier to spot deltas in an # 80 column display. gotttted = tcppacket.println() self.assertEqual(expected, gotttted, "strings are not equal \nexpected %s \ngotttted %s " % (expected, gotttted))
def test_ipv6_compare(self): """Test the underlying __compare__ functionality of the packet. Two packets constructed from the same bytes should be equal and two that are not should not be equal.""" file = PcapConnector("loopping6.out") packet = file.read() ip1 = ipv6(packet[file.dloff:len(packet)]) ip2 = ipv6(packet[file.dloff:len(packet)]) assert (ip1 != None) assert (ip2 != None) self.assertEqual(ip1, ip2, "packets should be equal but are not") ip1.dst = inet_pton(AF_INET6, "2001:ffff::1"); self.assertNotEqual(ip1, ip2, "packets compare equal but should not")
def test_ipv6_println(self): """This test reads from a pre-stored pcap file generated with tcpdump and ping on the loopback interface and tests the __str__ method to make sure the correct values are printed.""" file = PcapConnector("loopping6.out") packet = file.read() ip = ipv6(packet[file.dloff:len(packet)]) assert (ip != None) test_string = "<IPv6: version: 6, traffic_class: 0, flow: 0, length: 16, next_header: 58, hop: 64, src: \'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x01\', dst: \'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x01\'>" string = ip.println() self.assertEqual(string, test_string, "strings are not equal \nexpected %s \ngot %s " % (test_string, string))
def test_ipv6_print(self): """This test reads from a pre-stored pcap file generated with tcpdump and ping on the loopback interface and tests the __str__ method to make sure the correct values are printed.""" file = PcapConnector("loopping6.out") packet = file.read() ip = ipv6(packet[file.dloff:len(packet)]) assert (ip != None) test_string = "version 6\ntraffic_class 0\nflow 0\nlength 16\nnext_header 58\nhop 64\nsrc ::1\ndst ::1\n" string = ip.__str__() self.assertEqual(string, test_string, "strings are not equal \nexpected %s \ngot %s " % (test_string, string))
def test_ethernet_dump(self): """This test dumps a fake ethernet packet, with timetamp, to a dump file.""" from pcs.pcap import DLT_EN10MB file = PcapDumpConnector("pcapdump2.out", DLT_EN10MB) # create one packet, copy its bytes, then compare their fields ether = ethernet() assert (ethernet != None) ether.src = "\x00\x00\x00\x00\x00\x00" ether.dst = "\xff\xff\xff\xff\xff\xff" ether.type = 2048 class header: sec = 0 usec = 0 caplen = 0 header.sec = 69 header.usec = 69 header.caplen = len(ether.bytes) file.sendto(ether.bytes, header) file.close() # Re read what we just wrote. file = PcapConnector("pcapdump2.out", DLT_EN10MB) ether = file.readpkt() assert (ether.timestamp == 69.000069)
def test_ethernet_println(self): """This test reads from a pre-stored pcap file generated with tcpdump and ping on an ethernet interface and tests the println method to make sure the correct values are printed.""" file = PcapConnector("etherping.out") packet = file.read() ether = ethernet(packet[0:file.dloff]) assert (ether != None) string = ether.println() test_string = "<Ethernet: dst: 0:10:db:3a:3a:77 src: 0:d:93:44:fa:62 type: 0x800>" self.assertEqual(string, test_string, "strings are not equal \nexpected %s \ngot %s " % (test_string, string))
def test_ethernet_println(self): """This test reads from a pre-stored pcap file generated with tcpdump and ping on an ethernet interface and tests the __str__ method to make sure the correct values are printed.""" file = PcapConnector("etherping.out") packet = file.read() ether = ethernet(packet[0:file.dloff]) assert (ether != None) test_string = "<Ethernet: dst: 0:10:db:3a:3a:77 src: 0:d:93:44:fa:62 type: 0x800>" string = ether.println() self.assertEqual(string, test_string, "strings are not equal \nexpected %s \ngot %s " % (test_string, string))
def test_ipv6_println(self): """This test reads from a pre-stored pcap file generated with tcpdump and ping on the loopback interface and tests the __str__ method to make sure the correct values are printed.""" file = PcapConnector("loopping6.out") packet = file.read() ip = ipv6(packet[file.dloff:len(packet)]) assert (ip != None) test_string = "<IPv6: version: 6, traffic_class: 0, flow: 0, length: 16, next_header: 58, hop: 64, src: \'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x01\', dst: \'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x01\'>" string = ip.println() self.assertEqual( string, test_string, "strings are not equal \nexpected %s \ngot %s " % (test_string, string))
def test_ipv6_print(self): """This test reads from a pre-stored pcap file generated with tcpdump and ping on the loopback interface and tests the __str__ method to make sure the correct values are printed.""" file = PcapConnector("loopping6.out") packet = file.read() ip = ipv6(packet[file.dloff:len(packet)]) assert (ip != None) test_string = "version 6\ntraffic_class 0\nflow 0\nlength 16\nnext_header 58\nhop 64\nsrc ::1\ndst ::1\n" string = ip.__str__() self.assertEqual( string, test_string, "strings are not equal \nexpected %s \ngot %s " % (test_string, string))
def test_ipv4_println(self): """This test reads from a pre-stored pcap file generated with tcpdump and ping on the loopback interface and tests the __str__ method to make sure the correct values are printed.""" file = PcapConnector("loopping.out") packet = file.read() ip = ipv4(packet[file.dloff:len(packet)]) assert (ip != None) expected = "<IPv4: version: 4, hlen: 5, tos: 0, length: 84, " \ "id: 59067, flags: 0, offset: 0, ttl: 64, protocol: 1, " \ "checksum: 0, src: 2130706433, dst: 2130706433, options: []>" gotttted = ip.println() self.assertEqual(expected, gotttted, "strings are not equal \nexpected %s \ngotttted %s " % (expected, gotttted))
def test_ipv4_println(self): """This test reads from a pre-stored pcap file generated with tcpdump and ping on the loopback interface and tests the __str__ method to make sure the correct values are printed.""" file = PcapConnector("loopping.out") packet = file.read() ip = ipv4(packet[file.dloff:len(packet)]) assert (ip != None) expected = "<IPv4: version: 4, hlen: 5, tos: 0, length: 84, " \ "id: 59067, flags: 0, offset: 0, ttl: 64, protocol: 1, " \ "checksum: 0, src: 2130706433, dst: 2130706433, options: []>" gotttted = ip.println() self.assertEqual( expected, gotttted, "strings are not equal \nexpected %s \ngotttted %s " % (expected, gotttted))
def test_ipv6_read(self): """This test reads from a pre-stored pcap file generated with tcpdump and ping on the loopback interface.""" file = PcapConnector("loopping6.out") packet = file.read() ip = ipv6(packet[file.dloff:len(packet)]) assert (ip != None) self.assertEqual(ip.version, 6, "version not equal %d" % ip.version) self.assertEqual(ip.traffic_class, 0, "traffic_class not equal %d" % ip.traffic_class) self.assertEqual(ip.flow, 0, "flow not equal %d" % ip.flow) self.assertEqual(ip.length, 16, "length not equal %d" % ip.length) self.assertEqual(ip.next_header, 58, "next_header not equal %d" % ip.next_header) self.assertEqual(ip.hop, 64, "hop not equal %d" % ip.hop) self.assertEqual(ip.src, inet_pton(AF_INET6, "::1"), "src not equal %s" % ip.src) self.assertEqual(ip.dst, inet_pton(AF_INET6, "::1"), "dst not equal %s" % ip.dst)
def test_ipv4_print(self): """This test reads from a pre-stored pcap file generated with tcpdump and ping on the loopback interface and tests the __str__ method to make sure the correct values are printed.""" file = PcapConnector("loopping.out") packet = file.read() ip = ipv4(packet[file.dloff:len(packet)]) assert (ip != None) expected = "IPv4\nversion 4\nhlen 5\ntos 0\nlength 84\n" \ "id 59067\nflags 0\noffset 0\nttl 64\nprotocol 1\n" \ "checksum 0\nsrc 127.0.0.1\ndst 127.0.0.1\n" \ "options []\n" gotttted = ip.__str__() self.assertEqual(expected, gotttted, "strings are not equal \nexpected %s \ngotttted %s " % (expected, gotttted))
def test_ipv4_read(self): """This test reads from a pre-stored pcap file generated with tcpdump and ping on the loopback interface.""" file = PcapConnector("loopping.out") packet = file.read() ip = ipv4(packet[file.dloff:len(packet)]) assert (ip != None) self.assertEqual(ip.version, 4, "version not equal %d" % ip.version) self.assertEqual(ip.hlen, 5, "hlen not equal %d" % ip.hlen) self.assertEqual(ip.tos, 0, "tos not equal %d" % ip.tos) self.assertEqual(ip.length, 84, "length not equal %d" % ip.length) self.assertEqual(ip.id, 59067, "id not equal %d" % ip.id) self.assertEqual(ip.flags, 0, "flags not equal %d" % ip.flags) self.assertEqual(ip.offset, 0, "offset not equal %d" % ip.offset) self.assertEqual(ip.ttl, 64, "ttl not equal %d" % ip.ttl) self.assertEqual(ip.protocol, 1, "protocol not equal %d" % ip.protocol) self.assertEqual(ip.src, inet_atol("127.0.0.1"), "src not equal %d" % ip.src) self.assertEqual(ip.dst, inet_atol("127.0.0.1"), "dst not equal %d" % ip.dst)
def test_ipv4_print(self): """This test reads from a pre-stored pcap file generated with tcpdump and ping on the loopback interface and tests the __str__ method to make sure the correct values are printed.""" file = PcapConnector("loopping.out") packet = file.read() ip = ipv4(packet[file.dloff:len(packet)]) assert (ip != None) expected = "IPv4\nversion 4\nhlen 5\ntos 0\nlength 84\n" \ "id 59067\nflags 0\noffset 0\nttl 64\nprotocol 1\n" \ "checksum 0\nsrc 127.0.0.1\ndst 127.0.0.1\n" \ "options []\n" gotttted = ip.__str__() self.assertEqual( expected, gotttted, "strings are not equal \nexpected %s \ngotttted %s " % (expected, gotttted))
def test_tcpv4_str(self): """Test the ___str__ method to make sure the correct values are printed.""" file = PcapConnector("wwwtcp.out") packet = file.read() ip = ipv4(packet[file.dloff:len(packet)]) assert (ip != None) tcppacket = tcp(ip.data.bytes) assert (tcppacket) # pre tcp options: #expected = "TCP\nsport 53678\ndport 80\nsequence 1351059655\nack_number 0\noffset 11\nreserved 0\nns 0\ncwr 0\nece 0\nurgent 0\nack 0\npush 0\nreset 0\nsyn 1\nfin 0\nwindow 65535\nchecksum 15295\nurg_pointer 0\n" # post tcp options: expected = "TCP\nsport 53678\ndport 80\nsequence 1351059655\nack_number 0\noffset 11\nreserved 0\nns 0\ncwr 0\nece 0\nurgent 0\nack 0\npush 0\nreset 0\nsyn 1\nfin 0\nwindow 65535\nchecksum 15295\nurg_pointer 0\n" \ "options [" \ "[Field: mss, Value: " \ "<pcs.Field name v, 16 bits, " \ "default 1460, discriminator 0>], " \ "[Field: nop, Value: 1], " \ "[Field: wscale, Value: " \ "<pcs.Field name v, 8 bits, " \ "default 0, discriminator 0>], " \ "[Field: nop, Value: 1], " \ "[Field: nop, Value: 1], " \ "[Field: tstamp, Value: " \ "<pcs.Field name v, 64 bits, " \ "default 0, discriminator 0>], " \ "[Field: sackok, Value: " \ "<pcs.Field name v, 0 bits, " \ "default 0, discriminator 0>], " \ "[Field: end, Value: 0], " \ "[Field: end, Value: 0]" \ "]\n" gotttted = tcppacket.__str__() self.assertEqual( expected, gotttted, "strings are not equal \nexpected %s \ngotttted %s " % (expected, gotttted))
def test_dhcpv4_decode(self): """This test reads from a pre-stored pcap file.""" file = PcapConnector("dhcp_minimal.pcap") packet = file.readpkt() chain = packet.chain() #print chain ether = chain.packets[0] assert (ether != None) ip = chain.packets[1] assert (ip != None) udp = chain.packets[2] assert (udp != None) dhcp = chain.packets[3] assert (dhcp != None) self.assertEqual(dhcp.op, 1, "op not equal") self.assertEqual(dhcp.xid, 0xffff0001, "xid not equal") self.assertEqual(dhcp.secs, 42848, "secs not equal") self.assertEqual(dhcp.flags, 0x8000, "flags not equal") mac = ether_atob("52:54:00:12:34:56") #print ether_btoa(dhcp.chaddr) self.assertEqual(dhcp.chaddr[:dhcp.hlen], mac, "mac not equal") self.assertEqual(len(dhcp.options), 6, "wrong option field count %d should be %d" % (len(dhcp.options), 6)) # Not a tlv field self.assertEqual(dhcp.options[0], pcs.packets.dhcpv4_options.DHCP_OPTIONS_COOKIE, "invalid cookie value") # TLV fields self.assertEqual(dhcp.options[1].value, 1460, "invalid maximum message size value") self.assertEqual(dhcp.options[2].value, "FreeBSD:i386:6.3-RELEASE", \ "invalid vendor class") self.assertEqual(dhcp.options[3].value, pcs.packets.dhcpv4_options.DHCPDISCOVER, "invalud dhcp message type") # Not a tlv field self.assertEqual(dhcp.options[4], pcs.packets.dhcpv4_options.DHO_END, "invalid end value")
def test_tcpv4_str(self): """Test the ___str__ method to make sure the correct values are printed.""" file = PcapConnector("wwwtcp.out") packet = file.read() ip = ipv4(packet[file.dloff:len(packet)]) assert (ip != None) tcppacket = tcp(ip.data.bytes) assert (tcppacket) # pre tcp options: #expected = "TCP\nsport 53678\ndport 80\nsequence 1351059655\nack_number 0\noffset 11\nreserved 0\nns 0\ncwr 0\nece 0\nurgent 0\nack 0\npush 0\nreset 0\nsyn 1\nfin 0\nwindow 65535\nchecksum 15295\nurg_pointer 0\n" # post tcp options: expected = "TCP\nsport 53678\ndport 80\nsequence 1351059655\nack_number 0\noffset 11\nreserved 0\nns 0\ncwr 0\nece 0\nurgent 0\nack 0\npush 0\nreset 0\nsyn 1\nfin 0\nwindow 65535\nchecksum 15295\nurg_pointer 0\n" \ "options [" \ "[Field: mss, Value: " \ "<pcs.Field name v, 16 bits, " \ "default 1460, discriminator 0>], " \ "[Field: nop, Value: 1], " \ "[Field: wscale, Value: " \ "<pcs.Field name v, 8 bits, " \ "default 0, discriminator 0>], " \ "[Field: nop, Value: 1], " \ "[Field: nop, Value: 1], " \ "[Field: tstamp, Value: " \ "<pcs.Field name v, 64 bits, " \ "default 0, discriminator 0>], " \ "[Field: sackok, Value: " \ "<pcs.Field name v, 0 bits, " \ "default 0, discriminator 0>], " \ "[Field: end, Value: 0], " \ "[Field: end, Value: 0]" \ "]\n" gotttted = tcppacket.__str__() self.assertEqual(expected, gotttted, "strings are not equal \nexpected %s \ngotttted %s " % (expected, gotttted))
def test_map(self): # We're going to replace the higher layer protocol with the NULL # protocol. (Actually we're replacing it with Folger's Crystals.) """This test reads from a pre-stored pcap file generated with tcpdump and ping on an ethernet interface and tests the __str__ method to make sure the correct values are printed.""" file = PcapConnector("etherping.out") packet = file.readpkt() self.assertEqual(type(packet.data), ipv4) file.close # Replace the mapping of IPv4 ethernet_map.map[ethernet_map.ETHERTYPE_IP] = null file = PcapConnector("etherping.out") packet = file.readpkt() self.assertEqual(type(packet.data), null) file.close
def main(): from pcs import PcapConnector, TimeoutError, LimitReachedError from pcs.packets.ethernet import ethernet from pcs.packets.ipv4 import ipv4 from pcs.packets.icmpv4 import icmpv4 from pcs.packets.icmpv4 import icmpv4echo from pcs.packets.icmpv4 import ICMP_ECHO #from pcs.packets.icmpv4 import ICMP_ECHOREPLY fxp0 = PcapConnector("fxp0") filter = ethernet() / ipv4() / icmpv4(type=ICMP_ECHO) / icmpv4echo() #from pcs.bpf import program #bp = fxp0.make_bpf_program(filter) #for lp in bp.disassemble(): # print lp #fxp0.setfilter('icmp') #fxp0.set_bpf_program(bp) print "Expecting at least 1 ICMP echo request within 10 seconds." try: fxp0.expect([filter], 10) except LimitReachedError: print "Limit reached." sys.exit(1) except TimeoutError: print "Timed out." sys.exit(1) nmatches = 0 if fxp0.matches is not None: nmatches = len(fxp0.matches) print "Matched", nmatches, "chain(s)." sys.exit(0)