Newer
Older
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# Copyright 2019 Dom Sekotill <dom.sekotill@kodo.org.uk>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Test cases for wpa_supplicant.client.base.BaseClient
"""
import unittest
from unittest import mock
import anyio
from tests.unit import anyio as anyio_mock
from wpa_supplicant import errors
from wpa_supplicant.client import base
@mock.patch('wpa_supplicant.util.connect_unix_datagram', new_callable=anyio_mock.AsyncMock)
@mock.patch('wpa_supplicant.client.base.BaseClient.send_message', new_callable=anyio_mock.AsyncMock)
class ConnectTests(unittest.TestCase):
"""
Tests for the connect() method
"""
@anyio_mock.with_anyio()
async def test_connect(self, _, connect_mock):
"""
Check connect() calls socket.connect()
"""
async with base.BaseClient() as client:
await client.connect("foo")
connect_mock.assert_called_once_with("foo")
@anyio_mock.with_anyio()
async def test_connect_timeout_1(self, _, connect_mock):
"""
Check a socket.connect() delay causes TimeoutError to be raised
"""
connect_mock.delay = 2
async with base.BaseClient() as client:
with self.assertRaises(TimeoutError):
await client.connect("foo")
@anyio_mock.with_anyio()
async def test_connect_timeout_2(self, send_mock, _):
"""
Check a send/recv delay causes a TimeoutError to be raised
"""
send_mock.delay = 2
async with base.BaseClient() as client:
with self.assertRaises(TimeoutError):
await client.connect("foo")
class SendMessageTests(unittest.TestCase):
"""
Tests for the send_message() method
"""
def setUp(self):
async def _make_client():
return base.BaseClient()
self.client = client = anyio.run(_make_client)
client.sock = anyio_mock.AsyncMock()
client.sock.send.side_effect = len
@anyio_mock.with_anyio()
async def test_simple(self):
"""
Check that a response is processed after a command
"""
async with self.client as client:
client.sock.recv.return_value = b"OK"
assert await client.send_message("SOME_COMMAND") is None
@anyio_mock.with_anyio()
async def test_simple_expect(self):
"""
Check that an alternate expected response is processed
"""
async with self.client as client:
client.sock.recv.return_value = b"PONG"
assert await client.send_message("PING", expect="PONG") is None
@anyio_mock.with_anyio()
async def test_simple_no_expect(self):
"""
Check that an unexpected response raises an UnexpectedResponseError
"""
async with self.client as client:
client.sock.recv.return_value = b"DING"
with self.assertRaises(errors.UnexpectedResponseError):
await client.send_message("PING")
with self.assertRaises(errors.UnexpectedResponseError):
await client.send_message("PING", expect="PONG")
@anyio_mock.with_anyio()
async def test_simple_convert(self):
"""
Check that a response is passed through a converter if given
"""
async with self.client as client:
client.sock.recv.return_value = b"FOO\nBAR\nBAZ\n"
self.assertListEqual(
await client.send_message(
"SOME_COMMAND",
convert=lambda x: x.splitlines(),
),
["FOO", "BAR", "BAZ"]
)
@anyio_mock.with_anyio()
async def test_simple_convert_over_expect(self):
"""
Check that 'convert' overrides 'expect'
"""
async with self.client as client:
client.sock.recv.return_value = b"FOO\nBAR\nBAZ\n"
self.assertListEqual(
await client.send_message(
"SOME_COMMAND",
convert=lambda x: x.splitlines(),
expect="PONG",
),
["FOO", "BAR", "BAZ"]
)
@anyio_mock.with_anyio()
async def test_simple_fail(self):
"""
Check that a response of 'FAIL' causes CommandFailed to be raised
"""
async with self.client as client:
client.sock.recv.return_value = b"FAIL"
with self.assertRaises(errors.CommandFailed):
await client.send_message("SOME_COMMAND")
@anyio_mock.with_anyio()
async def test_simple_bad_command(self):
"""
Check that a response of 'UNKNOWN COMMAND' causes ValueError to be raised
"""
async with self.client as client:
client.sock.recv.return_value = b"UNKNOWN COMMAND"
with self.assertRaises(ValueError):
await client.send_message("SOME_COMMAND")
@anyio_mock.with_anyio()
async def test_simple_too_large(self):
"""
Check that MessageTooLargeError is raised if send() reports an incomplete send
"""
async with self.client as client:
client.sock.send.side_effect = lambda b: len(b) / 2
with self.assertRaises(errors.MessageTooLargeError):
await client.send_message("SOME_COMMAND")
@anyio_mock.with_anyio()
async def test_interleaved(self):
"""
Check that messages are processed alongside replies
"""
async with self.client as client:
client.sock.recv.side_effect = [
b"<2>SOME-MESSAGE",
b"<1>SOME-OTHER-MESSAGE with|args",
b"OK",
b"<2>SOME-MESSAGE"
]
assert await client.send_message("SOME_COMMAND") is None
class EventTests(unittest.TestCase):
"""
Tests for the event() method
"""
def setUp(self):
async def _make_client():
return base.BaseClient()
self.client = client = anyio.run(_make_client)
client.sock = anyio_mock.AsyncMock()
client.sock.send.side_effect = len
@anyio_mock.with_anyio()
async def test_simple(self):
"""
Check that an awaited message is returned when is arrives
"""
async with self.client as client, anyio.fail_after(2):
client.sock.recv.side_effect = [
b"OK", # Respond to ATTACH
b"<2>EXAMPLE-EVENT",
b"OK", # Respond to DETACH
]
prio, evt, args = await client.event("EXAMPLE-EVENT")
assert prio == 2
assert evt == "EXAMPLE-EVENT"
assert args is None
@anyio_mock.with_anyio()
async def test_multiple(self):
"""
Check that an awaited messages is returned when it arrives between others
"""
async with self.client as client, anyio.fail_after(2):
client.sock.recv.side_effect = [
b"OK", # Respond to ATTACH
b"<1>OTHER-MESSAGE",
b"<2>OTHER-MESSAGE",
b"<4>EXAMPLE-EVENT",
b"OK", # Respond to DETACH
b"<3>OTHER-MESSAGE",
]
prio, evt, args = await client.event("EXAMPLE-EVENT")
assert prio == 4
assert evt == "EXAMPLE-EVENT"
assert args is None
@anyio_mock.with_anyio()
async def test_wait_multiple(self):
"""
Check that the first of several awaited events is returned
"""
async with self.client as client, anyio.fail_after(2):
client.sock.recv.side_effect = [
b"OK", # Respond to ATTACH
b"<1>OTHER-MESSAGE",
b"<2>OTHER-MESSAGE",
b"<4>EXAMPLE-EVENT3",
b"<4>EXAMPLE-EVENT1",
b"OK", # Respond to DETACH
b"<3>OTHER-MESSAGE",
]
prio, evt, args = await client.event("EXAMPLE-EVENT1", "EXAMPLE-EVENT2", "EXAMPLE-EVENT3")
assert prio == 4
assert evt == "EXAMPLE-EVENT3"
assert args is None
@anyio_mock.with_anyio()
async def test_interleaved(self):
"""
Check that messages are processed as well as replies
"""
async with self.client as client, anyio.fail_after(2):
client.sock.recv.side_effect = [
b"<1>OTHER-MESSAGE",
b"OK", # Respond to SOME_COMMAND
b"OK", # Respond to ATTACH
b"<2>OTHER-MESSAGE",
b"<4>EXAMPLE-EVENT",
b"<3>OTHER-MESSAGE",
b"OK", # Respond to DETACH
b"FOO",
]
assert await client.send_message("SOME_COMMAND") is None
prio, evt, args = await client.event("EXAMPLE-EVENT")
assert prio == 4
assert evt == "EXAMPLE-EVENT"
assert args is None
assert await client.send_message("SOME_COMMAND", expect="FOO") is None