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_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_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_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_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_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_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))