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_pack(self):
"""Verifies that creates the correct pack"""
self.assertEqual(
icmp.ICMP(icmp.Types.EchoReply, payload='banana', identifier=19700).packet,
b'\x00\x00\xcb\x8e\xf4L\x01\x00banana',
"Fail to pack ICMP structure to packet"
)
self.assertEqual(
icmp.ICMP(icmp.Types.EchoReply, payload='random text goes here', identifier=12436).packet,
b'\x00\x00h\xd1\x940\x01\x00random text goes here',
"Fail to pack ICMP structure to packet"
)
self.assertEqual(
icmp.ICMP(icmp.Types.EchoRequest, payload='random text goes here', identifier=18676).packet,
b'\x08\x00\x00\xb9\xf4H\x01\x00random text goes here',
"Fail to unpack ICMP structure to packet"
)
def test_checksum_creation(self):
"""Verifies it generates the correct checksum, given packet data"""
packet = icmp.ICMP(icmp.Types.EchoRequest, payload='random text goes here', identifier=16)
self.assertEqual(packet.expected_checksum, 485, 'Checksum creation failed')
packet = icmp.ICMP(icmp.Types.EchoReply, payload='foobar', identifier=11)
self.assertEqual(packet.expected_checksum, 48060, 'Checksum creation failed')
def test_error_message(self):
"""Verifies error messages are presented correctly"""
self.assertEqual(self.craft_response_of_type(icmp.Types.EchoReply).error_message, None,
'Generated error message when response was correct')
self.assertEqual(self.craft_response_of_type(icmp.Types.DestinationUnreachable).error_message,
'Network Unreachable',
'Unable to classify a Network Unreachable error correctly')
self.assertEqual(executor.Response(None, 0.1).error_message, 'No response',
'Unable to generate correct message when response is not received')
pass
def __init__(self, message_type=Types.EchoReply, payload=None, identifier=None):
"""Creates an ICMP packet
:param message_type: Type of ICMP message to send
:type message_type: Union[ICMPType, (int, int), int]
:param payload: utf8 string or bytes payload
:type payload: Union[str, bytes]
:param identifier: ID of this ICMP packet
:type identifier: int"""
self.message_code = 0
if issubclass(message_type, ICMPType):
self.message_type = message_type.type_id
elif isinstance(message_type, tuple):
self.message_type = message_type[0]
self.message_code = message_type[1]
elif isinstance(message_type, int):
self.message_type = message_type