-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwlan.test.py
206 lines (151 loc) · 6.29 KB
/
wlan.test.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
from microtest import test, run, expect, mock_module, observe
from mocks import MockNetwork, MockWlan
import uasyncio
mock_module('network', MockNetwork())
from wlan import Wlan, TimeoutException, InvalidPasswordException, AccessPointUnreachableException, ConnectionFailedException, STAT_NO_IP
import network
@test
async def it_should_activate_the_wlan_interface_on_connect():
(wlan, mock_wlan) = default_wlan_harness()
mock_wlan.isconnected.returns(True)
await wlan.connect()
expect(mock_wlan.active).to_have_been_called_with(True)
@test
async def it_should_timeout_after_configured_period():
mock_wlan = MockWlan()
wlan = Wlan(ssid = 'ssid', password = 'pass', retry_count = 1, timeout = 1, wlan = mock_wlan)
mock_wlan.status.returns(network.STAT_CONNECTING)
async def connect():
await wlan.connect()
await expect(connect).to_throw(TimeoutException)
@test
async def it_should_throw_an_invalid_password_error_immediately():
mock_wlan = MockWlan()
wlan = Wlan(ssid = 'ssid', password = 'pass', retry_count = 5, timeout = 1, wlan = mock_wlan)
mock_wlan.status.returns(network.STAT_WRONG_PASSWORD)
async def connect():
await wlan.connect()
await expect(connect).to_throw(InvalidPasswordException)
expect(mock_wlan.connect).to_have_been_called_times(1)
@test
async def it_should_throw_an_access_point_unreachable_error():
mock_wlan = MockWlan()
wlan = Wlan(ssid = 'ssid', password = 'pass', retry_count = 1, timeout = 1, wlan = mock_wlan)
mock_wlan.status.returns(network.STAT_NO_AP_FOUND)
async def connect():
await wlan.connect()
await expect(connect).to_throw(AccessPointUnreachableException)
@test
async def it_should_throw_a_connection_failed_error():
mock_wlan = MockWlan()
wlan = Wlan(ssid = 'ssid', password = 'pass', retry_count = 1, timeout = 1, wlan = mock_wlan)
mock_wlan.status.returns(network.STAT_CONNECT_FAIL)
async def connect():
await wlan.connect()
await expect(connect).to_throw(ConnectionFailedException)
@test
async def it_should_throw_an_os_error_when_an_unknown_status_is_received():
mock_wlan = MockWlan()
wlan = Wlan(ssid = 'ssid', password = 'pass', retry_count = 1, timeout = 1, wlan = mock_wlan)
mock_wlan.status.returns(38924)
async def connect():
await wlan.connect()
await expect(connect).to_throw(OSError)
@test
async def it_should_retry_while_an_ip_address_is_being_assigned():
mock_wlan = MockWlan()
wlan = Wlan(ssid = 'ssid', password = 'pass', retry_count = 5, timeout = 1, wlan = mock_wlan)
mock_wlan.status.returns(STAT_NO_IP)
mock_wlan.isconnected.define_returns(False, True)
await wlan.connect()
expect(mock_wlan.connect).to_have_been_called_times(2)
@test
async def it_should_retry_while_connecting_is_in_progress():
mock_wlan = MockWlan()
wlan = Wlan(ssid = 'ssid', password = 'pass', retry_count = 5, timeout = 1, wlan = mock_wlan)
mock_wlan.status.returns(network.STAT_CONNECTING)
mock_wlan.isconnected.define_returns(False, True)
await wlan.connect()
expect(mock_wlan.connect).to_have_been_called_times(2)
@test
async def it_should_disable_power_saving_mode_on_connect():
(wlan, mock_wlan) = default_wlan_harness()
mock_wlan.isconnected.returns(True)
await wlan.connect()
expect(mock_wlan.config).to_have_been_called_with(pm = 0xA11140)
@test
async def it_should_trigger_the_on_connected_event_on_initial_connection():
(wlan, mock_wlan) = default_wlan_harness()
observer = observe(wlan.on_connected)
mock_wlan.status.returns(network.STAT_CONNECTING)
mock_wlan.isconnected.define_returns(False, True)
await wlan.connect()
await observer.wait()
expect(observer).to_have_been_triggered()
@test
async def it_should_try_reconnecting_on_connection_loss():
(wlan, mock_wlan) = default_wlan_harness()
mock_wlan.status.returns(network.STAT_CONNECTING)
mock_wlan.isconnected.define_returns(True, False)
mock_wlan.isconnected.returns(True)
await wlan.connect()
await uasyncio.sleep(3)
expect(mock_wlan.connect).to_have_been_called_times(2)
expect(mock_wlan.isconnected).to_have_been_called_times(3)
@test
async def it_should_trigger_the_disconnected_event_on_connection_loss():
(wlan, mock_wlan) = default_wlan_harness()
disconnect_observer = observe(wlan.on_disconnected)
mock_wlan.status.returns(network.STAT_CONNECTING)
mock_wlan.isconnected.define_returns(True, False)
await wlan.connect()
await disconnect_observer.wait()
expect(disconnect_observer).to_have_been_triggered()
@test
async def it_should_trigger_the_connected_event_on_reconnection():
(wlan, mock_wlan) = default_wlan_harness()
mock_wlan.status.returns(network.STAT_CONNECTING)
mock_wlan.isconnected.define_returns(True, True)
mock_wlan.isconnected.returns(False)
await wlan.connect()
await uasyncio.sleep(3)
wlan.on_connected.clear()
reconnected_observer = observe(wlan.on_connected)
mock_wlan.isconnected.define_returns(True) # Simulate reconnect
await reconnected_observer.wait()
expect(reconnected_observer).to_have_been_triggered()
@test
async def it_should_return_the_connection_status():
(wlan, mock_wlan) = default_wlan_harness()
mock_wlan.isconnected.define_returns(True, False)
expect(wlan.is_connected()).to_be(True)
expect(wlan.is_connected()).to_be(False)
@test
async def it_should_get_the_mac_address():
(wlan, mock_wlan) = default_wlan_harness()
mock_wlan.config.returns(b'\x00\x11\x22\x33\x44\x55')
expect(wlan.get_mac_address()).to_be('00:11:22:33:44:55')
@test
async def it_should_get_the_ip_address():
(wlan, mock_wlan) = default_wlan_harness()
mock_wlan.ifconfig.returns(['192.168.1.123', '255.255.255.0', '192.168.1.1', '192.168.1.1'])
expect(wlan.get_ip_address()).to_be('192.168.1.123')
@test
async def it_should_disconnect_the_wlan_interface():
(wlan, mock_wlan) = default_wlan_harness()
wlan.disconnect()
expect(mock_wlan.disconnect).to_have_been_called()
expect(mock_wlan.active).to_have_been_called_with(False)
@test
async def it_should_trigger_the_on_disconnected_event_on_manual_disconnect():
(wlan, _) = default_wlan_harness()
observer = observe(wlan.on_disconnected)
wlan.disconnect()
await observer.wait()
expect(observer).to_have_been_triggered()
def default_wlan_harness():
mock_wlan = MockWlan()
wlan = Wlan(ssid = 'ssid', password = 'pass', wlan = mock_wlan)
mock_wlan.isconnected.returns(False)
return (wlan, mock_wlan)
run()