-
Notifications
You must be signed in to change notification settings - Fork 2
/
tls_v5_test.py
355 lines (303 loc) · 11 KB
/
tls_v5_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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/python3
from cgi import test
from curses import termattrs
from operator import is_
from re import S, T
import subprocess
import shlex
import os
import paho.mqtt.client as mqtt
from multiprocessing import Process, Value
import time
import threading
import signal
g_port = 8883
g_addr = "127.0.0.1"
g_cacert = "/home/lee/workspace/nanomq/etc/certs/cacert.pem"
g_cert = "../certs/client-cert.pem"
g_key = "../certs/client-key.pem"
# g_url = " -h {addr} -p {port} ".format(addr = g_addr, port = g_port)
# g_url = " -h {addr} -p {port} --cafile {cacert} --cert {cert} --key {key} --insecure".format(addr = g_addr, port = g_port, cacert = g_cacert, cert = g_cert, key = g_key)
g_url = " -h {addr} -p {port} --cafile {cacert} --insecure".format(addr = g_addr, port = g_port, cacert = g_cacert)
cnt = 0
non_cnt = 0
shared_cnt = 0
lock = threading.Lock()
def clear_subclients():
entries = os.popen("pidof mosquitto_sub")
for line in entries:
for pid in line.split():
os.kill(int(pid), signal.SIGKILL)
def wait_message(process, route):
global cnt
global non_cnt
global shared_cnt
while True:
output = process.stdout.readline()
if output.strip() == 'message':
lock.acquire()
if route == 1:
cnt += 1
elif route == 2:
non_cnt += 1
else:
shared_cnt += 1
lock.release()
def cnt_substr(cmd, n, pid, message):
process = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
universal_newlines=True)
pid.value = process.pid
while True:
output = process.stdout.readline()
if message in output:
n.value += 1
def cnt_message(cmd, n, pid, message):
process = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
universal_newlines=True)
pid.value = process.pid
while True:
output = process.stdout.readline()
if output.strip() == message:
n.value += 1
def test_shared_subscription():
pub_cmd = shlex.split("mosquitto_pub -t topic_share -V 5 -m message {} -d --repeat 10".format(g_url))
sub_cmd = shlex.split("mosquitto_sub -t '$share/a/topic_share' {}".format(g_url))
sub_cmd_shared = shlex.split("mosquitto_sub -t '$share/b/topic_share' {}".format(g_url))
sub_cmd_non_shared = shlex.split("mosquitto_sub -t topic_share {}".format(g_url))
process1 = subprocess.Popen(sub_cmd,
stdout=subprocess.PIPE,
universal_newlines=True)
time.sleep(0.05)
process2 = subprocess.Popen(sub_cmd,
stdout=subprocess.PIPE,
universal_newlines=True)
time.sleep(0.05)
process3 = subprocess.Popen(sub_cmd,
stdout=subprocess.PIPE,
universal_newlines=True)
time.sleep(0.05)
process4 = subprocess.Popen(sub_cmd_non_shared,
stdout=subprocess.PIPE,
universal_newlines=True)
time.sleep(0.05)
process5 = subprocess.Popen(sub_cmd_shared,
stdout=subprocess.PIPE,
universal_newlines=True)
time.sleep(2)
process6 = subprocess.Popen(pub_cmd,
stdout=subprocess.PIPE,
universal_newlines=True)
t1 = threading.Thread(target=wait_message, args=(process1, 1))
t2 = threading.Thread(target=wait_message, args=(process2, 1))
t3 = threading.Thread(target=wait_message, args=(process3, 1))
t4 = threading.Thread(target=wait_message, args=(process4, 2))
t5 = threading.Thread(target=wait_message, args=(process5, 3))
t1.daemon = True
t2.daemon = True
t3.daemon = True
t4.daemon = True
t5.daemon = True
t1.start()
t2.start()
t3.start()
t4.start()
t5.start()
times = 0
while True:
lock.acquire()
if cnt == 10:
lock.release()
process1.terminate()
process2.terminate()
process3.terminate()
break
lock.release()
times += 1
time.sleep(1)
if times == 5:
print("Shared subscription test failed!")
return
times = 0
while True:
lock.acquire()
if non_cnt == 10:
lock.release()
process4.terminate()
break
lock.release()
times += 1
time.sleep(1)
if times == 5:
print("Shared subscription test failed!")
return
times = 0
while True:
lock.acquire()
if shared_cnt == 10:
lock.release()
process5.terminate()
break
lock.release()
times += 1
time.sleep(1)
if times == 5:
print("Shared subscription test failed!")
return
print("Shared subscription test passed!")
def test_topic_alias():
pub_cmd = shlex.split("mosquitto_pub -t topic -V 5 -m message -D Publish topic-alias 10 {} -d --repeat 10".format(g_url))
sub_cmd = shlex.split("mosquitto_sub -t topic {}".format(g_url))
cnt = Value('i', 0)
pid = Value('i', 0)
process1 = Process(target=cnt_message, args=(sub_cmd, cnt, pid, "message"))
process1.start()
time.sleep(1)
process2 = subprocess.Popen(pub_cmd,
stdout=subprocess.PIPE,
universal_newlines=True)
times = 0
while True:
if cnt.value == 10 or times == 5:
break
time.sleep(1)
times += 1
process1.terminate()
os.kill(pid.value, signal.SIGKILL)
if cnt.value == 10:
print("Topic alias test passed!")
else:
print("Topic alias test failed!")
def test_user_property():
pub_cmd = shlex.split("mosquitto_pub -t topic_test {} -m aaaa -V 5 -D Publish user-property user property".format(g_url))
sub_cmd = shlex.split("mosquitto_sub -t topic_test {} -V 5 -F %P".format(g_url))
cnt = Value('i', 0)
pid = Value('i', 0)
process1 = Process(target=cnt_message, args=(sub_cmd, cnt, pid, "user:property"))
process1.start()
time.sleep(1)
process2 = subprocess.Popen(pub_cmd,
stdout=subprocess.PIPE,
universal_newlines=True)
times = 0
while True:
if cnt.value == 1:
process1.terminate()
break
time.sleep(1)
times += 1
if times == 5:
break
process1.terminate()
os.kill(pid.value, signal.SIGKILL)
if times == 5:
print("User property test failed!")
else:
print("User property test passed!")
def test_session_expiry():
pub_cmd = shlex.split("mosquitto_pub -t topic_test {} -m message -V 5 -q 1".format(g_url))
sub_cmd = shlex.split("mosquitto_sub -t 'topic_test' {} --id client -x 6 -c -q 1 -V 5".format(g_url))
process1 = subprocess.Popen(sub_cmd,
stdout=subprocess.PIPE,
universal_newlines=True)
time.sleep(0.5)
process1.terminate()
process2 = subprocess.Popen(pub_cmd,
stdout=subprocess.PIPE,
universal_newlines=True)
time.sleep(0.5)
cnt = Value('i', 0)
pid = Value('i', 0)
process3 = Process(target=cnt_message, args=(sub_cmd, cnt, pid, "message"))
process3.start()
time.sleep(4)
process3.terminate()
os.kill(pid.value, signal.SIGKILL)
if cnt.value != 1:
print("Session expiry interval test failed")
return
process2 = subprocess.Popen(pub_cmd,
stdout=subprocess.PIPE,
universal_newlines=True)
cnt = Value('i', 0)
pid = Value('i', 0)
time.sleep(1)
process3 = Process(target=cnt_message, args=(sub_cmd, cnt, pid, "message"))
process3.start()
time.sleep(2)
process3.terminate()
os.kill(pid.value, signal.SIGKILL)
if cnt.value == 1:
print("Session expiry interval test passed!")
else:
print("Session expiry interval test failed")
def test_message_expiry():
pub_cmd = shlex.split("mosquitto_pub -t topic_test {} -m message -V 5 -q 1 -D publish message-expiry-interval 3 -r".format(g_url))
sub_cmd = shlex.split("mosquitto_sub -t topic_test {} -q 1 -V 5".format(g_url))
process1 = subprocess.Popen(pub_cmd,
stdout=subprocess.PIPE,
universal_newlines=True)
time.sleep(1)
cnt = Value('i', 0)
pid = Value('i', 0)
process2 = Process(target=cnt_message, args=(sub_cmd, cnt, pid, "message"))
process2.start()
time.sleep(2)
process2.terminate()
os.kill(pid.value, signal.SIGKILL)
if cnt.value != 1:
print("Message expiry interval test failed!")
return
time.sleep(3)
pid = Value('i', 0)
process2 = Process(target=cnt_message, args=(sub_cmd, cnt, pid, "message"))
process2.start()
time.sleep(2)
process2.terminate()
os.kill(pid.value, signal.SIGKILL)
if cnt.value == 1:
print("Message expiry interval test passed!")
else:
print("Message expiry interval test failed!")
def test_retain_as_publish():
pub_retain_cmd = shlex.split("mosquitto_pub -t topic {} -V 5 -m retain/as/published -d --retain".format(g_url))
sub_retain_cmd = shlex.split("mosquitto_sub -t topic {} -V 5 --retain-as-published -d".format(g_url))
sub_common_cmd = shlex.split("mosquitto_sub -t topic {} -V 5 -d".format(g_url))
pub_clean_retain_cmd = shlex.split("mosquitto_pub -t topic {} -V 5 -m \"\" -d".format(g_url))
process1 = subprocess.Popen(pub_retain_cmd,
stdout=subprocess.PIPE,
universal_newlines=True)
time.sleep(0.05)
cnt = Value('i', 0)
pid1 = Value('i', 0)
process2 = Process(target=cnt_substr, args=(sub_common_cmd, cnt, pid1, " r0,"))
process2.start()
time.sleep(0.05)
cnt1 = Value('i', 0)
pid2 = Value('i', 0)
process3 = Process(target=cnt_substr, args=(sub_retain_cmd, cnt1, pid2, " r1,"))
process3.start()
time.sleep(1)
if cnt.value != 1 or cnt1.value != 1:
print("Retain As Published test failed!")
else:
print("Retain As Published test passed!")
process4 = subprocess.Popen(pub_clean_retain_cmd,
stdout=subprocess.PIPE,
universal_newlines=True)
process1.terminate()
process2.terminate()
process3.terminate()
process4.terminate()
os.kill(pid1.value, signal.SIGKILL)
os.kill(pid2.value, signal.SIGKILL)
time.sleep(2)
# clear_subclients()
if __name__ == '__main__':
# test_message_expiry()
test_session_expiry()
test_user_property()
test_shared_subscription()
test_topic_alias()
test_retain_as_publish()