webssh

Web based ssh client https://github.com/huashengdun/webssh webssh.huashengdun.org/
git clone http://git.hanabi.in/repos/webssh.git
Log | Files | Refs | README | LICENSE

test_utils.py (4795B)


      1 import unittest
      2 
      3 from webssh.utils import (
      4     is_valid_ip_address, is_valid_port, is_valid_hostname, to_str, to_bytes,
      5     to_int, is_ip_hostname, is_same_primary_domain, parse_origin_from_url
      6 )
      7 
      8 
      9 class TestUitls(unittest.TestCase):
     10 
     11     def test_to_str(self):
     12         b = b'hello'
     13         u = u'hello'
     14         self.assertEqual(to_str(b), u)
     15         self.assertEqual(to_str(u), u)
     16 
     17     def test_to_bytes(self):
     18         b = b'hello'
     19         u = u'hello'
     20         self.assertEqual(to_bytes(b), b)
     21         self.assertEqual(to_bytes(u), b)
     22 
     23     def test_to_int(self):
     24         self.assertEqual(to_int(''), None)
     25         self.assertEqual(to_int(None), None)
     26         self.assertEqual(to_int('22'), 22)
     27         self.assertEqual(to_int(' 22 '), 22)
     28 
     29     def test_is_valid_ip_address(self):
     30         self.assertFalse(is_valid_ip_address('127.0.0'))
     31         self.assertFalse(is_valid_ip_address(b'127.0.0'))
     32         self.assertTrue(is_valid_ip_address('127.0.0.1'))
     33         self.assertTrue(is_valid_ip_address(b'127.0.0.1'))
     34         self.assertFalse(is_valid_ip_address('abc'))
     35         self.assertFalse(is_valid_ip_address(b'abc'))
     36         self.assertTrue(is_valid_ip_address('::1'))
     37         self.assertTrue(is_valid_ip_address(b'::1'))
     38         self.assertTrue(is_valid_ip_address('fe80::1111:2222:3333:4444'))
     39         self.assertTrue(is_valid_ip_address(b'fe80::1111:2222:3333:4444'))
     40         self.assertTrue(is_valid_ip_address('fe80::1111:2222:3333:4444%eth0'))
     41         self.assertTrue(is_valid_ip_address(b'fe80::1111:2222:3333:4444%eth0'))
     42 
     43     def test_is_valid_port(self):
     44         self.assertTrue(is_valid_port(80))
     45         self.assertFalse(is_valid_port(0))
     46         self.assertFalse(is_valid_port(65536))
     47 
     48     def test_is_valid_hostname(self):
     49         self.assertTrue(is_valid_hostname('google.com'))
     50         self.assertTrue(is_valid_hostname('google.com.'))
     51         self.assertTrue(is_valid_hostname('www.google.com'))
     52         self.assertTrue(is_valid_hostname('www.google.com.'))
     53         self.assertFalse(is_valid_hostname('.www.google.com'))
     54         self.assertFalse(is_valid_hostname('http://www.google.com'))
     55         self.assertFalse(is_valid_hostname('https://www.google.com'))
     56         self.assertFalse(is_valid_hostname('127.0.0.1'))
     57         self.assertFalse(is_valid_hostname('::1'))
     58 
     59     def test_is_ip_hostname(self):
     60         self.assertTrue(is_ip_hostname('[::1]'))
     61         self.assertTrue(is_ip_hostname('127.0.0.1'))
     62         self.assertFalse(is_ip_hostname('localhost'))
     63         self.assertFalse(is_ip_hostname('www.google.com'))
     64 
     65     def test_is_same_primary_domain(self):
     66         domain1 = 'localhost'
     67         domain2 = 'localhost'
     68         self.assertTrue(is_same_primary_domain(domain1, domain2))
     69 
     70         domain1 = 'localhost'
     71         domain2 = 'test'
     72         self.assertFalse(is_same_primary_domain(domain1, domain2))
     73 
     74         domain1 = 'com'
     75         domain2 = 'example.com'
     76         self.assertFalse(is_same_primary_domain(domain1, domain2))
     77 
     78         domain1 = 'example.com'
     79         domain2 = 'example.com'
     80         self.assertTrue(is_same_primary_domain(domain1, domain2))
     81 
     82         domain1 = 'www.example.com'
     83         domain2 = 'example.com'
     84         self.assertTrue(is_same_primary_domain(domain1, domain2))
     85 
     86         domain1 = 'wwwexample.com'
     87         domain2 = 'example.com'
     88         self.assertFalse(is_same_primary_domain(domain1, domain2))
     89 
     90         domain1 = 'www.example.com'
     91         domain2 = 'www2.example.com'
     92         self.assertTrue(is_same_primary_domain(domain1, domain2))
     93 
     94         domain1 = 'xxx.www.example.com'
     95         domain2 = 'xxx.www2.example.com'
     96         self.assertTrue(is_same_primary_domain(domain1, domain2))
     97 
     98     def test_parse_origin_from_url(self):
     99         url = ''
    100         self.assertIsNone(parse_origin_from_url(url))
    101 
    102         url = 'www.example.com'
    103         self.assertEqual(parse_origin_from_url(url), 'http://www.example.com')
    104 
    105         url = 'http://www.example.com'
    106         self.assertEqual(parse_origin_from_url(url), 'http://www.example.com')
    107 
    108         url = 'www.example.com:80'
    109         self.assertEqual(parse_origin_from_url(url), 'http://www.example.com')
    110 
    111         url = 'http://www.example.com:80'
    112         self.assertEqual(parse_origin_from_url(url), 'http://www.example.com')
    113 
    114         url = 'www.example.com:443'
    115         self.assertEqual(parse_origin_from_url(url), 'https://www.example.com')
    116 
    117         url = 'https://www.example.com'
    118         self.assertEqual(parse_origin_from_url(url), 'https://www.example.com')
    119 
    120         url = 'https://www.example.com:443'
    121         self.assertEqual(parse_origin_from_url(url), 'https://www.example.com')
    122 
    123         url = 'https://www.example.com:80'
    124         self.assertEqual(parse_origin_from_url(url), url)
    125 
    126         url = 'http://www.example.com:443'
    127         self.assertEqual(parse_origin_from_url(url), url)