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_ping_normal(self):
delay = ping3.ping("example.com")
self.assertIsInstance(delay, float)
def test_ping_ttl(self):
delay = ping3.ping("example.com", ttl=64)
self.assertIsInstance(delay, float)
delay = ping3.ping("example.com", ttl=1)
self.assertIsNone(delay)
def test_ping_seq(self):
delay = ping3.ping("example.com", seq=199)
self.assertIsInstance(delay, float)
def test_ping_bind(self):
my_ip = socket.gethostbyname(socket.gethostname())
dest_addr = "example.com"
if my_ip == "127.0.0.1" or my_ip == "127.0.1.1": # This may caused by /etc/hosts settings.
dest_addr = my_ip # only localhost can send and receive from 127.0.0.1 (or 127.0.1.1 on Ubuntu).
delay = ping3.ping(dest_addr, src_addr=my_ip)
self.assertIsInstance(delay, float)
def test_ping_ttl(self):
delay = ping3.ping("example.com", ttl=64)
self.assertIsInstance(delay, float)
delay = ping3.ping("example.com", ttl=1)
self.assertIsNone(delay)
def test_DEBUG(self):
with patch("sys.stdout", new=io.StringIO()) as fake_out:
with patch("ping3.DEBUG", True):
delay = ping3.ping("example.com")
self.assertTrue("[DEBUG]" in fake_out.getvalue())
def test_ping_hostunknown(self):
not_exist_url = "not-exist.com"
with patch("sys.stdout", new=io.StringIO()) as fake_out:
self.assertFalse(ping3.ping(not_exist_url))
def test_ping_size(self):
delay = ping3.ping("example.com", size=100)
self.assertIsInstance(delay, float)
with self.assertRaises(OSError):
ping3.ping("example.com", size=99999) # most router has 1480 MTU, which is IP_Header(20) + ICMP_Header(8) + ICMP_Payload(1452)
def test_ping_timeout_exception(self):
with patch("ping3.EXCEPTIONS", True):
with self.assertRaises(errors.Timeout):
ping3.ping("example.com", timeout=0.0001)
def test_verbose_ping_normal(self):
with patch("sys.stdout", new=io.StringIO()) as fake_out:
ping3.verbose_ping("example.com")
self.assertRegex(fake_out.getvalue(), r".*[0-9]+ms.*")