forked from OpenBazaar/OpenBazaar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_network_util.py
149 lines (124 loc) · 5.1 KB
/
test_network_util.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import mock
import unittest
import requests
import stun
from node import network_util
class TestNodeNetworkUtil(unittest.TestCase):
def test_set_stun_servers(self):
new_stun_servers = (
'stun.openbazaar1.com',
'stun.openbazaar2.com'
)
network_util.set_stun_servers(servers=new_stun_servers)
self.assertItemsEqual(new_stun_servers, stun.stun_servers_list)
@mock.patch.object(stun, 'get_ip_info')
def test_get_nat_status(self, method_mock):
stun_response = ('Symmetric NAT', '123.45.67.89', '12345')
method_mock.return_value = stun_response
keys = ('nat_type', 'external_ip', 'external_port')
dict_response = {key: value for key, value in zip(keys, stun_response)}
self.assertEqual(dict_response, network_util.get_nat_status())
method_mock.assert_called_once_with(source_port=0, stun_host=None, stun_port=19302)
def test_is_loopback_addr(self):
self.assertTrue(network_util.is_loopback_addr("127.0.0.1"))
self.assertTrue(network_util.is_loopback_addr("localhost"))
self.assertFalse(network_util.is_loopback_addr("10.0.0.1"))
self.assertFalse(network_util.is_loopback_addr("192.168.0.1"))
def test_is_private_ip_address(self):
self.assertTrue(network_util.is_private_ip_address('localhost'))
self.assertTrue(network_util.is_private_ip_address('127.0.0.1'))
self.assertTrue(network_util.is_private_ip_address('192.168.1.1'))
self.assertTrue(network_util.is_private_ip_address('172.16.1.1'))
self.assertTrue(network_util.is_private_ip_address('10.1.1.1'))
self.assertFalse(network_util.is_private_ip_address('8.8.8.8'))
@mock.patch.object(requests, 'get')
def test_get_my_ip_from_dflt_site(self, mock_method):
stub_ip = '123.45.67.89'
response_mock = mock.NonCallableMock()
response_mock.text = stub_ip
mock_method.return_value = response_mock
self.assertEqual(stub_ip, network_util.get_my_ip())
mock_method.assert_called_once_with(network_util.IP_DETECT_SITE)
@mock.patch.object(requests, 'get')
def test_get_my_ip_from_othr_site(self, mock_method):
stub_ip = '123.45.67.89'
response_mock = mock.NonCallableMock()
response_mock.text = stub_ip
mock_method.return_value = response_mock
ip_site = 'http://ip.stub.org'
self.assertEqual(stub_ip, network_util.get_my_ip(ip_site=ip_site))
mock_method.assert_called_once_with(ip_site)
@mock.patch.object(requests, 'get', side_effect=requests.RequestException)
def test_get_my_ip_failed_request(self, mock_method):
self.assertIsNone(network_util.get_my_ip())
@mock.patch.object(requests, 'get')
def test_get_my_ip_bad_response(self, mock_method):
mock_method.return_value = None
self.assertIsNone(network_util.get_my_ip())
def test_is_ipv6_address(self):
self.assertTrue(network_util.is_ipv6_address('2a00::'))
self.assertFalse(network_util.is_ipv6_address('8.8.8.8'))
def test_get_peer_url(self):
self.assertEqual(
network_util.get_peer_url('8.8.8.8', 1234),
'tcp://8.8.8.8:1234'
)
self.assertEqual(
network_util.get_peer_url('8.8.8.8', 1234, protocol='udp'),
'udp://8.8.8.8:1234'
)
self.assertEqual(
network_util.get_peer_url('2a00::', 1234),
'tcp://[2a00::]:1234'
)
self.assertEqual(
network_util.get_peer_url('2a00::', 1234, protocol='udp'),
'udp://[2a00::]:1234'
)
self.assertEqual(
network_util.get_peer_url('www.openbazaar.com', 1234),
'tcp://www.openbazaar.com:1234'
)
def test_valid_uri(self):
self.assertTrue(
network_util.is_valid_uri('tcp://localhost:12345')
)
self.assertFalse(
network_util.is_valid_uri('udp://localhost:12345')
)
self.assertFalse(
network_util.is_valid_uri('inproc://localhost:12345')
)
self.assertFalse(
network_util.is_valid_uri('localhost:12345')
)
self.assertFalse(
network_util.is_valid_uri('localhost')
)
self.assertFalse(
network_util.is_valid_uri('@#FADSFJSK@#RKFSAJASDJKF@#lkdafj')
)
self.assertFalse(
network_util.is_valid_uri('tcp://192.33..23.1:12345')
)
self.assertTrue(
network_util.is_valid_uri('tcp://sub.domain.com:12345')
)
def test_is_valid_hostname(self):
self.assertTrue(
network_util.is_valid_hostname('192.168.1.1')
)
self.assertTrue(
network_util.is_valid_hostname('sub.domain.com')
)
self.assertTrue(
network_util.is_valid_hostname('domain.com')
)
self.assertTrue(
network_util.is_valid_hostname('domain')
)
self.assertFalse(
network_util.is_valid_hostname('@#FADSFJSK@#RKFSAJASDJKF@#lkdafj')
)
if __name__ == '__main__':
unittest.main()