-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_ip.py
64 lines (42 loc) · 1.59 KB
/
test_ip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
import unittest
from ip import IP_Address
class TestIPConversions(unittest.TestCase):
def test_hexadecimal(self): # Test that a Hexadecimal input converts to all other values
hex_ip = '0x62d2ed4b'
str_ip = '98.210.237.75'
int_ip = 1657990475
ip = IP_Address()
ip.hexa_ip = hex_ip
self.assertEqual(str_ip, ip.string_ip) # Test conversion to string IP from Hexadecimal
self.assertEqual(hex_ip, ip.hexa_ip) # Test that input remains unchanged
self.assertEqual(int_ip, ip.int_ip) # Test conversion to INT IP from Hexadecimal
def test_integer(self): # Test that a Integer input converts to all other values
hex_ip = '0x62d2ed4b'
str_ip = '98.210.237.75'
int_ip = 1657990475
ip = IP_Address()
ip.int_ip = int_ip
self.assertEqual(str_ip, ip.string_ip)
self.assertEqual(hex_ip, ip.hexa_ip)
self.assertEqual(int_ip, ip.int_ip)
def test_string(self): # Test that a String input converts to all other values
hex_ip = '0x62d2ed4b'
str_ip = '98.210.237.75'
int_ip = 1657990475
ip = IP_Address()
ip.string_ip = str_ip
self.assertEqual(str_ip, ip.string_ip)
self.assertEqual(hex_ip, ip.hexa_ip)
self.assertEqual(int_ip, ip.int_ip)
def test_cidr_check(self): # Test that IP Input correctly evalutaes if in CIDR range
ip = IP_Address()
ip.hexa_ip = '0x62d2ed4b'
ip.cidr = '98.210.237.0/24'
self.assertEqual(True, ip.in_cidr)
def test_typecheck(self): # Test non-IP's fail type evaluation
ip = IP_Address()
with self.assertRaises(ValueError):
ip.string_ip = '98A.210.237.75'
if __name__ == '__main__':
unittest.main()