You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
webssh/tests/test_utils.py

115 lines
4.4 KiB

import unittest
from webssh.utils import (
is_valid_ip_address, is_valid_port, is_valid_hostname, to_str, to_bytes,
to_int, on_public_network_interface, get_ips_by_name, is_ip_hostname,
is_name_open_to_public, is_same_primary_domain
)
class TestUitls(unittest.TestCase):
def test_to_str(self):
b = b'hello'
u = u'hello'
self.assertEqual(to_str(b), u)
self.assertEqual(to_str(u), u)
def test_to_bytes(self):
b = b'hello'
u = u'hello'
self.assertEqual(to_bytes(b), b)
self.assertEqual(to_bytes(u), b)
def test_to_int(self):
self.assertEqual(to_int(''), None)
self.assertEqual(to_int(None), None)
self.assertEqual(to_int('22'), 22)
self.assertEqual(to_int(' 22 '), 22)
def test_is_valid_ip_address(self):
self.assertFalse(is_valid_ip_address('127.0.0'))
self.assertFalse(is_valid_ip_address(b'127.0.0'))
self.assertTrue(is_valid_ip_address('127.0.0.1'))
self.assertTrue(is_valid_ip_address(b'127.0.0.1'))
self.assertFalse(is_valid_ip_address('abc'))
self.assertFalse(is_valid_ip_address(b'abc'))
self.assertTrue(is_valid_ip_address('::1'))
self.assertTrue(is_valid_ip_address(b'::1'))
def test_is_valid_port(self):
self.assertTrue(is_valid_port(80))
self.assertFalse(is_valid_port(0))
self.assertFalse(is_valid_port(65536))
def test_is_valid_hostname(self):
self.assertTrue(is_valid_hostname('google.com'))
self.assertTrue(is_valid_hostname('google.com.'))
self.assertTrue(is_valid_hostname('www.google.com'))
self.assertTrue(is_valid_hostname('www.google.com.'))
self.assertFalse(is_valid_hostname('.www.google.com'))
self.assertFalse(is_valid_hostname('http://www.google.com'))
self.assertFalse(is_valid_hostname('https://www.google.com'))
self.assertFalse(is_valid_hostname('127.0.0.1'))
self.assertFalse(is_valid_hostname('::1'))
def test_get_ips_by_name(self):
self.assertTrue(get_ips_by_name(''), {'0.0.0.0', '::'})
self.assertTrue(get_ips_by_name('localhost'), {'127.0.0.1'})
self.assertTrue(get_ips_by_name('192.68.1.1'), {'192.168.1.1'})
self.assertTrue(get_ips_by_name('2.2.2.2'), {'2.2.2.2'})
def test_on_public_network_interface(self):
self.assertTrue(on_public_network_interface('0.0.0.0'))
self.assertTrue(on_public_network_interface('::'))
self.assertTrue(on_public_network_interface('0:0:0:0:0:0:0:0'))
self.assertTrue(on_public_network_interface('2.2.2.2'))
self.assertTrue(on_public_network_interface('2:2:2:2:2:2:2:2'))
self.assertIsNone(on_public_network_interface('127.0.0.1'))
def test_is_name_open_to_public(self):
self.assertTrue(is_name_open_to_public('0.0.0.0'))
self.assertTrue(is_name_open_to_public('::'))
self.assertIsNone(is_name_open_to_public('192.168.1.1'))
self.assertIsNone(is_name_open_to_public('127.0.0.1'))
self.assertIsNone(is_name_open_to_public('localhost'))
def test_is_ip_hostname(self):
self.assertTrue(is_ip_hostname('[::1]'))
self.assertTrue(is_ip_hostname('127.0.0.1'))
self.assertFalse(is_ip_hostname('localhost'))
self.assertFalse(is_ip_hostname('www.google.com'))
def test_is_same_primary_domain(self):
domain1 = 'localhost'
domain2 = 'localhost'
self.assertTrue(is_same_primary_domain(domain1, domain2))
domain1 = 'localhost'
domain2 = 'test'
self.assertFalse(is_same_primary_domain(domain1, domain2))
domain1 = 'com'
domain2 = 'example.com'
self.assertFalse(is_same_primary_domain(domain1, domain2))
domain1 = 'example.com'
domain2 = 'example.com'
self.assertTrue(is_same_primary_domain(domain1, domain2))
domain1 = 'www.example.com'
domain2 = 'example.com'
self.assertTrue(is_same_primary_domain(domain1, domain2))
domain1 = 'wwwexample.com'
domain2 = 'example.com'
self.assertFalse(is_same_primary_domain(domain1, domain2))
domain1 = 'www.example.com'
domain2 = 'www2.example.com'
self.assertTrue(is_same_primary_domain(domain1, domain2))
domain1 = 'xxx.www.example.com'
domain2 = 'xxx.www2.example.com'
self.assertTrue(is_same_primary_domain(domain1, domain2))