forked from eclipse-paho/paho.mqtt.python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_client.py
331 lines (248 loc) · 11.3 KB
/
test_client.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
327
328
329
330
331
import inspect
import os
import sys
import time
import unicodedata
import pytest
import paho.mqtt.client as client
# From http://stackoverflow.com/questions/279237/python-import-a-module-from-a-folder
cmd_subfolder = os.path.realpath(
os.path.abspath(
os.path.join(
os.path.split(
inspect.getfile(inspect.currentframe()))[0],
'..', 'test')))
if cmd_subfolder not in sys.path:
sys.path.insert(0, cmd_subfolder)
import paho_test
# Import test fixture
from testsupport.broker import fake_broker
@pytest.mark.parametrize("proto_ver", [
(client.MQTTv31),
(client.MQTTv311),
])
class Test_connect(object):
"""
Tests on connect/disconnect behaviour of the client
"""
def test_01_con_discon_success(self, proto_ver, fake_broker):
mqttc = client.Client(
"01-con-discon-success", protocol=proto_ver)
def on_connect(mqttc, obj, flags, rc):
assert rc == 0
mqttc.disconnect()
mqttc.on_connect = on_connect
mqttc.connect_async("localhost", 1888)
mqttc.loop_start()
try:
fake_broker.start()
connect_packet = paho_test.gen_connect(
"01-con-discon-success", keepalive=60,
proto_ver=proto_ver)
packet_in = fake_broker.receive_packet(1000)
assert packet_in # Check connection was not closed
assert packet_in == connect_packet
connack_packet = paho_test.gen_connack(rc=0)
count = fake_broker.send_packet(connack_packet)
assert count # Check connection was not closed
assert count == len(connack_packet)
disconnect_packet = paho_test.gen_disconnect()
packet_in = fake_broker.receive_packet(1000)
assert packet_in # Check connection was not closed
assert packet_in == disconnect_packet
finally:
mqttc.loop_stop()
packet_in = fake_broker.receive_packet(1)
assert not packet_in # Check connection is closed
def test_01_con_failure_rc(self, proto_ver, fake_broker):
mqttc = client.Client(
"01-con-failure-rc", protocol=proto_ver)
def on_connect(mqttc, obj, flags, rc):
assert rc == 1
mqttc.on_connect = on_connect
mqttc.connect_async("localhost", 1888)
mqttc.loop_start()
try:
fake_broker.start()
connect_packet = paho_test.gen_connect(
"01-con-failure-rc", keepalive=60,
proto_ver=proto_ver)
packet_in = fake_broker.receive_packet(1000)
assert packet_in # Check connection was not closed
assert packet_in == connect_packet
connack_packet = paho_test.gen_connack(rc=1)
count = fake_broker.send_packet(connack_packet)
assert count # Check connection was not closed
assert count == len(connack_packet)
packet_in = fake_broker.receive_packet(1)
assert not packet_in # Check connection is closed
finally:
mqttc.loop_stop()
class TestPublishBroker2Client(object):
def test_invalid_utf8_topic(self, fake_broker):
mqttc = client.Client("client-id")
def on_message(client, userdata, msg):
with pytest.raises(UnicodeDecodeError):
msg.topic
client.disconnect()
mqttc.on_message = on_message
mqttc.connect_async("localhost", 1888)
mqttc.loop_start()
try:
fake_broker.start()
connect_packet = paho_test.gen_connect("client-id")
packet_in = fake_broker.receive_packet(len(connect_packet))
assert packet_in # Check connection was not closed
assert packet_in == connect_packet
connack_packet = paho_test.gen_connack(rc=0)
count = fake_broker.send_packet(connack_packet)
assert count # Check connection was not closed
assert count == len(connack_packet)
publish_packet = paho_test.gen_publish(b"\xff", qos=0)
count = fake_broker.send_packet(publish_packet)
assert count # Check connection was not closed
assert count == len(publish_packet)
disconnect_packet = paho_test.gen_disconnect()
packet_in = fake_broker.receive_packet(len(disconnect_packet))
assert packet_in # Check connection was not closed
assert packet_in == disconnect_packet
finally:
mqttc.loop_stop()
packet_in = fake_broker.receive_packet(1)
assert not packet_in # Check connection is closed
def test_valid_utf8_topic_recv(self, fake_broker):
mqttc = client.Client("client-id")
# It should be non-ascii multi-bytes character
topic = unicodedata.lookup('SNOWMAN')
def on_message(client, userdata, msg):
assert msg.topic == topic
client.disconnect()
mqttc.on_message = on_message
mqttc.connect_async("localhost", 1888)
mqttc.loop_start()
try:
fake_broker.start()
connect_packet = paho_test.gen_connect("client-id")
packet_in = fake_broker.receive_packet(len(connect_packet))
assert packet_in # Check connection was not closed
assert packet_in == connect_packet
connack_packet = paho_test.gen_connack(rc=0)
count = fake_broker.send_packet(connack_packet)
assert count # Check connection was not closed
assert count == len(connack_packet)
publish_packet = paho_test.gen_publish(
topic.encode('utf-8'), qos=0
)
count = fake_broker.send_packet(publish_packet)
assert count # Check connection was not closed
assert count == len(publish_packet)
disconnect_packet = paho_test.gen_disconnect()
packet_in = fake_broker.receive_packet(len(disconnect_packet))
assert packet_in # Check connection was not closed
assert packet_in == disconnect_packet
finally:
mqttc.loop_stop()
packet_in = fake_broker.receive_packet(1)
assert not packet_in # Check connection is closed
def test_valid_utf8_topic_publish(self, fake_broker):
mqttc = client.Client("client-id")
# It should be non-ascii multi-bytes character
topic = unicodedata.lookup('SNOWMAN')
mqttc.connect_async("localhost", 1888)
mqttc.loop_start()
try:
fake_broker.start()
connect_packet = paho_test.gen_connect("client-id")
packet_in = fake_broker.receive_packet(len(connect_packet))
assert packet_in # Check connection was not closed
assert packet_in == connect_packet
connack_packet = paho_test.gen_connack(rc=0)
count = fake_broker.send_packet(connack_packet)
assert count # Check connection was not closed
assert count == len(connack_packet)
mqttc.publish(topic, None, 0)
# Small sleep needed to avoid connection reset.
time.sleep(0.3)
publish_packet = paho_test.gen_publish(
topic.encode('utf-8'), qos=0
)
packet_in = fake_broker.receive_packet(len(publish_packet))
assert packet_in # Check connection was not closed
assert packet_in == publish_packet
mqttc.disconnect()
disconnect_packet = paho_test.gen_disconnect()
packet_in = fake_broker.receive_packet(len(disconnect_packet))
assert packet_in # Check connection was not closed
assert packet_in == disconnect_packet
finally:
mqttc.loop_stop()
packet_in = fake_broker.receive_packet(1)
assert not packet_in # Check connection is closed
def test_message_callback(self, fake_broker):
mqttc = client.Client("client-id")
userdata = {
'on_message': 0,
'callback1': 0,
'callback2': 0,
}
mqttc.user_data_set(userdata)
def on_message(client, userdata, msg):
assert msg.topic == 'topic/value'
userdata['on_message'] += 1
def callback1(client, userdata, msg):
assert msg.topic == 'topic/callback/1'
userdata['callback1'] += 1
def callback2(client, userdata, msg):
assert msg.topic in ('topic/callback/3', 'topic/callback/1')
userdata['callback2'] += 1
mqttc.on_message = on_message
mqttc.message_callback_add('topic/callback/1', callback1)
mqttc.message_callback_add('topic/callback/+', callback2)
mqttc.connect_async("localhost", 1888)
mqttc.loop_start()
try:
fake_broker.start()
connect_packet = paho_test.gen_connect("client-id")
packet_in = fake_broker.receive_packet(len(connect_packet))
assert packet_in # Check connection was not closed
assert packet_in == connect_packet
connack_packet = paho_test.gen_connack(rc=0)
count = fake_broker.send_packet(connack_packet)
assert count # Check connection was not closed
assert count == len(connack_packet)
publish_packet = paho_test.gen_publish(b"topic/value", qos=1, mid=1)
count = fake_broker.send_packet(publish_packet)
assert count # Check connection was not closed
assert count == len(publish_packet)
publish_packet = paho_test.gen_publish(b"topic/callback/1", qos=1, mid=2)
count = fake_broker.send_packet(publish_packet)
assert count # Check connection was not closed
assert count == len(publish_packet)
publish_packet = paho_test.gen_publish(b"topic/callback/3", qos=1, mid=3)
count = fake_broker.send_packet(publish_packet)
assert count # Check connection was not closed
assert count == len(publish_packet)
puback_packet = paho_test.gen_puback(mid=1)
packet_in = fake_broker.receive_packet(len(puback_packet))
assert packet_in # Check connection was not closed
assert packet_in == puback_packet
puback_packet = paho_test.gen_puback(mid=2)
packet_in = fake_broker.receive_packet(len(puback_packet))
assert packet_in # Check connection was not closed
assert packet_in == puback_packet
puback_packet = paho_test.gen_puback(mid=3)
packet_in = fake_broker.receive_packet(len(puback_packet))
assert packet_in # Check connection was not closed
assert packet_in == puback_packet
mqttc.disconnect()
disconnect_packet = paho_test.gen_disconnect()
packet_in = fake_broker.receive_packet(len(disconnect_packet))
assert packet_in # Check connection was not closed
assert packet_in == disconnect_packet
finally:
mqttc.loop_stop()
packet_in = fake_broker.receive_packet(1)
assert not packet_in # Check connection is closed
assert userdata['on_message'] == 1
assert userdata['callback1'] == 1
assert userdata['callback2'] == 2