def test_udpv4_raw(self): # Create a packet for raw injection and verify it meets criteria. from pcs import inet_atol from pcs.packets.payload import payload c = ethernet(src="\x01\x02\x03\x04\x05\x06", \ dst="\xff\xff\xff\xff\xff\xff") / \ ipv4(src=inet_atol("192.168.123.17"), \ dst=inet_atol("192.0.2.2"), id=5235) / \ udp(sport=67, dport=68) / \ payload("foobar\n") c.calc_lengths() c.calc_checksums() c.encode() expected = \ "\xFF\xFF\xFF\xFF\xFF\xFF\x01\x02" \ "\x03\x04\x05\x06\x08\x00\x45\x00" \ "\x00\x23\x14\x73\x00\x00\x40\x11" \ "\x68\x9B\xC0\xA8\x7B\x11\xC0\x00" \ "\x02\x02\x00\x43\x00\x44\x00\x0F" \ "\xC0\x48\x66\x6F\x6F\x62\x61\x72" \ "\x0A" gotttted = c.bytes self.assertEqual(expected, gotttted, "test raw encoding")
def test_udpv4_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 = pcs.PcapConnector("dns.out") packet = file.readpkt() ip = packet.data assert (ip != None) packet1 = udp(ip.data.bytes) packet2 = udp(ip.data.bytes) assert (packet1 != None) assert (packet2 != None) self.assertEqual(packet1, packet2, "packets should be equal but are not") packet1.dport = 0xffff self.assertNotEqual(packet1, packet2, "packets compare equal but should not\ngot %sexpect %s" % (packet1, packet2))
def test_udpv4(self): # create one packet, copy its bytes, then compare their fields packet = udp() assert (packet != None) packet.sport = 67 packet.dport = 68 packet.length = 64 packet.checksum = 0 # Create a packet to compare against new_packet = udp() new_packet.decode(packet.bytes) self.assertEqual(packet.bytes, new_packet.bytes, "bytes not equal") for field in packet._fieldnames: self.assertEqual(getattr(packet, field), getattr(new_packet, field), ("%s not equal" % field))
def test_udpv4_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 = pcs.PcapConnector("dns.out") packet = file.readpkt() ip = packet.data assert (ip != None) packet1 = udp(ip.data.bytes) packet2 = udp(ip.data.bytes) assert (packet1 != None) assert (packet2 != None) self.assertEqual(packet1, packet2, "packets should be equal but are not") packet1.dport = 0xffff self.assertNotEqual( packet1, packet2, "packets compare equal but should not\ngot %sexpect %s" % (packet1, packet2))