-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathtests.py
326 lines (279 loc) · 11.1 KB
/
tests.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import utime as time
from ubinascii import hexlify
from umqtt.simple2 import MQTTClient as _MQTTClient, MQTTException, pid_gen
def debug_print(data):
print('HEX: %s STR: /*' % hexlify(data).decode('ascii'), end='')
for i, d in enumerate(data):
if type(d) == str:
d = ord(d)
if d > 31 and d < 127:
print(chr(d), end='')
else:
print('.', end='')
print('*/')
def debug_func_name(f_name):
print('FUNC: %s' % f_name)
class MQTTClient(_MQTTClient):
MAX_DBG_LEN = 80
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.newpid = pid_gen(65535 - 1)
def _read(self, n):
out = super()._read(n)
if type(out) == bytes:
print(' R(%3d) - ' % len(out), end='')
debug_print(out[:self.MAX_DBG_LEN])
else:
print(' R(---) - %s' % out)
return out
def _write(self, bytes_wr, *args, **kwargs):
print(' W(%3d,%3s) - ' % (len(bytes_wr), args[0] if args else '---'), end='')
debug_print(bytes_wr[:self.MAX_DBG_LEN])
return super()._write(bytes_wr, *args, **kwargs)
def connect(self, *a, **k):
debug_func_name('connect')
return super().connect(*a, **k)
def disconnect(self, *a, **k):
debug_func_name('disconnect')
return super().disconnect(*a, **k)
def ping(self, *a, **k):
debug_func_name('ping')
return super().ping(*a, **k)
def publish(self, *a, **k):
debug_func_name('publish')
return super().publish(*a, **k)
def subscribe(self, *a, **k):
debug_func_name('subscribe')
return super().subscribe(*a, **k)
def wait_msg(self, *a, **k):
debug_func_name('wait_msg')
return super().wait_msg(*a, **k)
class TestMQTT:
HIDE_SENSITIVE = True
TESTS = [
'test_publish_qos_0',
'test_subscribe_qos_0',
# 'test_subscribe_qos_1', # not implenemted
# 'test_subscribe_qos_2', # not implenemted
'test_subscribe_long_topic',
'test_publish_qos_1',
'test_publish_qos_1_no_puback',
'test_publish_qos_2',
'test_publish_retain',
'test_publish_lastwill',
]
def __init__(self, *args, **kwargs):
self.mqtt_client_args = (args, kwargs)
self.msg_id = args[0]
self.subsctiption_out = {}
self.status_out = {}
self.clients = {}
self.client = None
def init_mqtt_client(self, clientid_postfix='_1'):
args = list(self.mqtt_client_args[0][:])
kwargs = self.mqtt_client_args[1].copy()
if len(args) > 0:
args[0] += clientid_postfix
if 'client_id' in kwargs:
kwargs['client_id'] += clientid_postfix
print_args = args.copy()
print_kwargs = kwargs.copy()
if 'ssl_params' in kwargs:
print_kwargs['ssl_params'] = kwargs['ssl_params'].copy()
hidden_val = '../hidden/..'
if self.HIDE_SENSITIVE:
if 'ssl_params' in print_kwargs:
if 'key' in print_kwargs['ssl_params']:
print_kwargs['ssl_params']['key'] = hidden_val
if 'cert' in print_kwargs['ssl_params']:
print_kwargs['ssl_params']['cert'] = hidden_val
if 'password' in print_kwargs:
print_kwargs['password'] = hidden_val
if len(print_args) >= 5:
print_args[4] = hidden_val # password
if len(print_args) >= 8:
print_args[7] = hidden_val # ssl_params
if 'key' in print_args[7]:
print_args[7]['key'] = hidden_val
if 'cert' in print_args[7]:
print_args[7]['cert'] = hidden_val
print('MQTT connection args:', args, print_kwargs)
client = MQTTClient(*args, **kwargs)
client.set_callback(self.sub_cb_gen(clientid_postfix))
client.set_callback_status(self.stat_cb_gen(clientid_postfix))
self.subsctiption_out[clientid_postfix] = None
self.status_out[clientid_postfix] = None
self.clients[clientid_postfix] = client
return client
def sub_cb_gen(self, clientid_postfix='_1'):
def sub_cb(topic, msg, retained, dup):
print('TOPIC: %s MSG: %s R: %s D: %s' % (topic, msg, retained, dup))
self.subsctiption_out[clientid_postfix] = (topic, msg, retained)
return sub_cb
def stat_cb_gen(self, clientid_postfix='_1'):
def stat_cb(pid, status):
print('PID: %s STATUS: %d' % (pid, status))
self.status_out[clientid_postfix] = (pid, status)
return stat_cb
def get_subscription_out(self, timeout=5, clientid_postfix='_1'):
print('WAIT SUB: timeout=%d' % (timeout,))
client = self.clients[clientid_postfix]
for i in range(timeout):
client.check_msg()
if clientid_postfix in self.subsctiption_out and self.subsctiption_out[clientid_postfix] is not None:
o = self.subsctiption_out[clientid_postfix]
self.subsctiption_out[clientid_postfix] = None
return o
time.sleep(1)
raise Exception('timeout')
def get_status_out(self, timeout=5, pid=None, clientid_postfix='_1'):
print('WAIT STAT: timeout=%d pid=%s' % (timeout, pid))
client = self.clients[clientid_postfix]
for i in range(timeout + 1):
time.sleep(1)
client.check_msg()
if clientid_postfix in self.status_out and self.status_out[clientid_postfix] is not None:
o = self.status_out[clientid_postfix]
self.status_out[clientid_postfix] = None
if pid:
if pid != o[0]:
continue
return o
raise Exception('timeout')
def disable_net(self):
raise RuntimeError('Not implemented method')
def enable_net(self):
raise RuntimeError('Not implemented method')
def network_status(self):
# Display networki information
raise RuntimeError('Not implemented method')
def device_status(self):
from micropython import mem_info, qstr_info
print('DEVICE STATUS:')
mem_info()
qstr_info()
def get_topic(self, test_name):
return '%s/umqtt.simple2/%s/' % (self.msg_id, test_name)
def verbose_tests(self, test_values: dict):
tests_fail=[k for k, v in test_values.items() if not v[0]]
if tests_fail:
print('\nTests fails: %s\n' % ', '.join(tests_fail))
else:
print('All the tests were finished successfully!')
def run(self, verbose=True, tests=None, hide_sensitive=HIDE_SENSITIVE):
self.HIDE_SENSITIVE = hide_sensitive
if not tests:
tests = self.TESTS
tests_values = {}
for test_name in tests:
a = time.ticks_ms()
s = self.run_test(test_name)
b = time.ticks_ms()
tests_values[test_name] = (s, time.ticks_diff(b, a))
if verbose:
self.verbose_tests(tests_values)
return tests_values
def run_test(self, test_name):
try:
if self.client:
self.client.disconnect()
except:
pass
self.client = self.init_mqtt_client()
self.subsctiption_out = {}
self.status_out = {}
test = getattr(self, test_name)
print('RUN [%s]' % test_name)
test_pass = True
self.enable_net()
try:
test(self.get_topic(test_name))
except Exception as e:
from sys import print_exception
self.network_status()
self.device_status()
print_exception(e)
test_pass = False
print('END [%s] %s\n' % (test_name, 'succes' if test_pass else 'FAIL'))
return test_pass
def test_publish_qos_0(self, topic):
self.client.connect()
self.client.publish(topic, 'test QoS 0')
self.client.disconnect()
def test_publish_qos_1(self, topic):
self.client.connect()
pid = self.client.publish(topic, 'test QoS 1', qos=1)
out_pid, status = self.get_status_out(pid=pid)
assert status == 1
self.client.disconnect()
def test_publish_qos_1_no_puback(self, topic):
self.client.connect()
pid = self.client.publish(topic, 'test QoS 1', qos=1)
pid = next(self.client.newpid)
self.client.rcv_pids[pid] = time.ticks_add(time.ticks_ms(), self.client.message_timeout * 1000)
out_pid, status = self.get_status_out(10, pid=pid)
assert status == 0
self.client.disconnect()
def test_publish_qos_2(self, topic):
self.client.connect()
try:
self.client.publish(topic, 'test QoS 2', qos=2)
except AssertionError as e:
print(e)
self.client.disconnect()
def test_publish_retain(self, topic):
self.client.connect()
pid = self.client.publish(topic, 'test retain', qos=1, retain=True)
self.client.disconnect()
self.client.connect()
self.client.subscribe(topic + '#')
t, m, r = self.get_subscription_out()
assert t.decode('ascii') == topic
assert m.decode('ascii') == 'test retain'
assert r == True
self.client.disconnect()
def test_subscribe_qos_0(self, topic):
self.client.connect()
pid = self.client.subscribe(topic + '#')
out_pid, status = self.get_status_out(pid=pid)
assert status == 1
assert pid == 65535
msg_in = 'abc123'
self.client.publish(topic, msg_in, qos=1)
msg_out = self.get_subscription_out()[1]
assert msg_in == msg_out.decode('ascii')
msg_in = ''
self.client.publish(topic, msg_in, qos=1)
msg_out = self.get_subscription_out()[1]
assert msg_in == msg_out.decode('ascii')
self.client.disconnect()
def test_subscribe_qos_1(self, topic):
pass
def test_subscribe_qos_2(self, topic):
pass
def test_subscribe_long_topic(self, topic):
self.client.connect()
topic = topic + '3' * (500 - len(topic))
pid = self.client.subscribe(topic + '/#')
out_pid, status = self.get_status_out(pid=pid)
assert status == 1
msg_in = 'abc123'
self.client.publish(topic, msg_in, qos=1)
msg_out = self.get_subscription_out()[1]
assert msg_in == msg_out.decode('ascii')
self.client.disconnect()
def test_publish_lastwill(self, topic):
self.client.set_last_will(topic, 'offline', True, 1)
self.client.connect()
self.client.publish(topic, 'online', retain=True, qos=1)
self.client.subscribe(topic)
msg_out = self.get_subscription_out()[1]
assert b'online' == msg_out
self.client.sock.close()
time.sleep(1)
client_2 = self.init_mqtt_client('_2')
client_2.connect()
client_2.subscribe(topic)
msg_out = self.get_subscription_out(clientid_postfix='_2')[1]
assert 'offline' == msg_out.decode('ascii')
client_2.disconnect()