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_blank_header_creation(self):
"""Verifies it generates the correct header (no checksum) given packet data"""
packet = icmp.ICMP(icmp.Types.EchoRequest, payload='random text goes here', identifier=16)
self.assertEqual(packet._header(), b'\x08\x00\x00\x00\x10\x00\x01\x00',
'Blank header creation failed (without checksum)')
packet = icmp.ICMP(icmp.Types.EchoReply, payload='foo', identifier=11)
self.assertEqual(packet._header(), b'\x00\x00\x00\x00\x0b\x00\x01\x00',
'Blank header creation failed (without checksum)')
def craft_response_of_type(response_type):
"""Generates an executor.Response from an icmp.Types
:param response_type: Type of response
:type response_type: Union[icmp.Type, tuple]
:return: The crafted response
:rtype: executor.Response"""
return executor.Response(executor.Message('', icmp.ICMP(response_type), '127.0.0.1'), 0.1)
def time_elapsed(self):
"""Verifies the time elapsed is presented correctly"""
self.assertEqual(executor.Response(None, 1).time_elapsed_ms, 1000, 'Bad ms representation for 1 second')
self.assertEqual(executor.Response(None, 0.001).time_elapsed_ms, 1, 'Bad ms representation for 1 ms')
self.assertEqual(executor.Response(None, 0.01).time_elapsed_ms, 10, 'Bad ms representation for 10 ms')
import collections
import unittest
from pythonping import executor
from pythonping import icmp
class SuccessfulResponseMock(executor.Response):
"""Mock a successful response to a ping"""
@property
def success(self):
return True
class FailingResponseMock(executor.Response):
"""Mock a failed response to a ping"""
@property
def success(self):
return False
class ExecutorUtilsTestCase(unittest.TestCase):
"""Tests for standalone functions in pythonping.executor"""
def test_represent_seconds_in_ms(self):
"""Verifies conversion from seconds to milliseconds works correctly"""
self.assertEqual(executor.represent_seconds_in_ms(4), 4000, 'Failed to convert seconds to milliseconds')
self.assertEqual(executor.represent_seconds_in_ms(0), 0, 'Failed to convert seconds to milliseconds')
self.assertEqual(executor.represent_seconds_in_ms(0.001), 1, 'Failed to convert seconds to milliseconds')
self.assertEqual(executor.represent_seconds_in_ms(0.0001), 0.1, 'Failed to convert seconds to milliseconds')
self.assertEqual(executor.represent_seconds_in_ms(0.00001), 0.01, 'Failed to convert seconds to milliseconds')
def responses_from_times(times):
"""Generates a list of empty responses from a list of time elapsed
:param times: List of time elapsed for each response
:type times: list
:return: List of responses
:rtype: executor.ResponseList"""
return executor.ResponseList([executor.Response(None, _) for _ in times])
def time_elapsed(self):
"""Verifies the time elapsed is presented correctly"""
self.assertEqual(executor.Response(None, 1).time_elapsed_ms, 1000, 'Bad ms representation for 1 second')
self.assertEqual(executor.Response(None, 0.001).time_elapsed_ms, 1, 'Bad ms representation for 1 ms')
self.assertEqual(executor.Response(None, 0.01).time_elapsed_ms, 10, 'Bad ms representation for 10 ms')
import collections
import unittest
from pythonping import executor
from pythonping import icmp
class SuccessfulResponseMock(executor.Response):
"""Mock a successful response to a ping"""
@property
def success(self):
return True
class FailingResponseMock(executor.Response):
"""Mock a failed response to a ping"""
@property
def success(self):
return False
class ExecutorUtilsTestCase(unittest.TestCase):
"""Tests for standalone functions in pythonping.executor"""
def test_success(self):
"""Verifies the if the Response can indicate a success to a request correctly"""
self.assertTrue(self.craft_response_of_type(icmp.Types.EchoReply).success,
'Unable to validate a successful response')
self.assertFalse(self.craft_response_of_type(icmp.Types.DestinationUnreachable).success,
'Unable to validate Destination Unreachable')
self.assertFalse(self.craft_response_of_type(icmp.Types.BadIPHeader).success,
'Unable to validate Bad IP Header')
self.assertFalse(executor.Response(None, 0.1).success, 'Unable to validate timeout (no payload)')
def test_checksum(self):
"""Verifies that checksum calculation is correct"""
self.assertEqual(
icmp.checksum(b'\x08\x00\x00\x00\x00*\x01\x009TPM'),
13421,
"Checksum validation failed (even length)"
)
self.assertEqual(
icmp.checksum(b'\x08\x00\x00\x00\x01*\x01\x001PJ2'),
21370,
"Checksum validation failed (even length)"
)
self.assertEqual(
icmp.checksum(b'\x08\x00\x00\x00\x02*\x01\x006K3J'),
16523,
"Checksum validation failed (even length)"
)
self.assertEqual(
icmp.checksum(b'\x08\x00\x00\x00\x03*\x01\x00COSA'),
17757,
def test_success(self):
"""Verifies the if the Response can indicate a success to a request correctly"""
self.assertTrue(self.craft_response_of_type(icmp.Types.EchoReply).success,
'Unable to validate a successful response')
self.assertFalse(self.craft_response_of_type(icmp.Types.DestinationUnreachable).success,
'Unable to validate Destination Unreachable')
self.assertFalse(self.craft_response_of_type(icmp.Types.BadIPHeader).success,
'Unable to validate Bad IP Header')
self.assertFalse(executor.Response(None, 0.1).success, 'Unable to validate timeout (no payload)')