Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_is_valid(self):
"""Verifies that understands if receives a packet with valid or invalid checksum"""
ip_header_offset = b''.join([b'0' for _ in range(20)])
# Following two packets have a good checksum
packet = icmp.ICMP.generate_from_raw(ip_header_offset + b'\x00\x00\xbe\xdb\x01\x00\x01\x00banana')
self.assertEqual(packet.received_checksum, packet.expected_checksum, 'Checksum validation failed')
packet = icmp.ICMP.generate_from_raw(ip_header_offset + b'\x08\x00@\xca\xb47\x01\x00random text goes here')
self.assertEqual(packet.received_checksum, packet.expected_checksum, 'Checksum validation failed')
# Packet 'does' instead of 'goes'
packet = icmp.ICMP.generate_from_raw(ip_header_offset + b'\x08\x00\x9c\xd0X1\x01\x00random text does here')
self.assertNotEqual(packet.received_checksum, packet.expected_checksum, 'Checksum validation failed')
def test_unpack(self):
"""Verifies that reads data correctly from a packet"""
ip_header_offset = b''.join([b'0' for _ in range(20)])
packet = icmp.ICMP.generate_from_raw(ip_header_offset + b'\x00\x00\xbe\xdb\x01\x00\x01\x00banana')
self.assertEqual(packet.message_type, 0, 'Failed to extract message type')
self.assertEqual(packet.message_code, 0, 'Failed to extract message code')
self.assertEqual(packet.id, 1, 'Failed to extract id')
self.assertEqual(packet.payload, b'banana', 'Failed to extract payload')
packet = icmp.ICMP.generate_from_raw(ip_header_offset + b'\x00\x00Q\x9d\x10\x00\x01\x00random text goes here')
self.assertEqual(packet.message_type, 0, 'Failed to extract message type')
self.assertEqual(packet.message_code, 0, 'Failed to extract message code')
self.assertEqual(packet.id, 16, 'Failed to extract id')
self.assertEqual(packet.payload, b'random text goes here', 'Failed to extract payload')
packet = icmp.ICMP.generate_from_raw(ip_header_offset + b'\x08\x00I\x9d\x10\x00\x01\x00random text goes here')
self.assertEqual(packet.message_type, 8, 'Failed to extract message type')
self.assertEqual(packet.message_code, 0, 'Failed to extract message code')
self.assertEqual(packet.id, 16, 'Failed to extract id')
self.assertEqual(packet.payload, b'random text goes here', 'Failed to extract payload')