Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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 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)')
:param timeout: How long to listen for the specified packet, in seconds
:type timeout: float
:return: The response to the request with the specified packet_id
:rtype: Response"""
time_left = timeout
response = icmp.ICMP()
while time_left > 0:
# Keep listening until a packet arrives
raw_packet, source_socket, time_left = self.socket.receive(time_left)
# If we actually received something
if raw_packet != b'':
response.unpack(raw_packet)
# Ensure we have not unpacked the packet we sent (RHEL will also listen to outgoing packets)
if response.id == packet_id and response.message_type != icmp.Types.EchoRequest.type_id:
return Response(Message('', response, source_socket[0]), timeout-time_left)
return Response(None, timeout)