14-dynsec-group-invalid.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. #!/usr/bin/env python3
  2. # Check invalid inputs for group commands
  3. from mosq_test_helper import *
  4. import json
  5. import shutil
  6. def write_config(filename, port):
  7. with open(filename, 'w') as f:
  8. f.write("listener %d\n" % (port))
  9. f.write("allow_anonymous true\n")
  10. f.write("plugin ../../plugins/dynamic-security/mosquitto_dynamic_security.so\n")
  11. f.write("plugin_opt_config_file %d/dynamic-security.json\n" % (port))
  12. def command_check(sock, command_payload, expected_response, msg=""):
  13. command_packet = mosq_test.gen_publish(topic="$CONTROL/dynamic-security/v1", qos=0, payload=json.dumps(command_payload))
  14. sock.send(command_packet)
  15. response = json.loads(mosq_test.read_publish(sock))
  16. if response != expected_response:
  17. print(expected_response)
  18. print(response)
  19. if msg != "":
  20. print(msg)
  21. raise ValueError(response)
  22. port = mosq_test.get_port()
  23. conf_file = os.path.basename(__file__).replace('.py', '.conf')
  24. write_config(conf_file, port)
  25. # Create client for modifying
  26. create_client0_command = { 'commands': [{'command': 'createClient', 'username':'validclient' }] }
  27. create_client0_response = {'responses': [{'command': 'createClient'}]}
  28. # Create group for modifying
  29. create_group0_command = { 'commands': [{'command': 'createGroup', 'groupname':'validgroup' }] }
  30. create_group0_response = {'responses': [{'command': 'createGroup'}]}
  31. # Create role for modifying
  32. create_role0_command = { 'commands': [{'command': 'createRole', 'rolename':'validrole' }] }
  33. create_role0_response = {'responses': [{'command': 'createRole'}]}
  34. # ==========================================================================
  35. # Create group
  36. # ==========================================================================
  37. # No groupname
  38. create_group1_command = { 'commands': [{'command': 'createGroup' }] }
  39. create_group1_response = {'responses': [{'command': 'createGroup', 'error': 'Invalid/missing groupname'}]}
  40. # Groupname not a string
  41. create_group2_command = { 'commands': [{'command': 'createGroup', 'groupname':5 }] }
  42. create_group2_response = {'responses': [{'command': 'createGroup', 'error': 'Invalid/missing groupname'}]}
  43. # Groupname not UTF-8
  44. create_group3_command = { 'commands': [{'command': 'createGroup', 'groupname': '￿LO' }] }
  45. create_group3_response = {'responses': [{'command': 'createGroup', 'error': 'Group name not valid UTF-8'}]}
  46. # textname not a string
  47. create_group4_command = { 'commands': [{'command': 'createGroup', 'groupname':'g', 'textname':5 }] }
  48. create_group4_response = {'responses': [{'command': 'createGroup', 'error': 'Invalid/missing textname'}]}
  49. # textdescription not a string
  50. create_group5_command = { 'commands': [{'command': 'createGroup', 'groupname':'g', 'textdescription':5 }] }
  51. create_group5_response = {'responses': [{'command': 'createGroup', 'error': 'Invalid/missing textdescription'}]}
  52. # Group already exists
  53. create_group6_command = { 'commands': [{'command': 'createGroup', 'groupname': 'validgroup'}]}
  54. create_group6_response = {'responses': [{'command': 'createGroup', 'error': 'Group already exists'}]}
  55. # Role not found
  56. create_group7_command = { 'commands': [{'command': 'createGroup', 'groupname': 'group', 'roles':[{'rolename':'notfound'}]}] }
  57. create_group7_response = {'responses': [{'command': 'createGroup', 'error': 'Role not found'}]}
  58. # ==========================================================================
  59. # Delete group
  60. # ==========================================================================
  61. # No groupname
  62. delete_group1_command = { 'commands': [{'command': 'deleteGroup' }] }
  63. delete_group1_response = {'responses': [{'command': 'deleteGroup', 'error': 'Invalid/missing groupname'}]}
  64. # Groupname not a string
  65. delete_group2_command = { 'commands': [{'command': 'deleteGroup', 'groupname':5 }] }
  66. delete_group2_response = {'responses': [{'command': 'deleteGroup', 'error': 'Invalid/missing groupname'}]}
  67. # Groupname not UTF-8
  68. delete_group3_command = { 'commands': [{'command': 'deleteGroup', 'groupname': '￿LO' }] }
  69. delete_group3_response = {'responses': [{'command': 'deleteGroup', 'error': 'Group name not valid UTF-8'}]}
  70. # Group not found
  71. delete_group4_command = { 'commands': [{'command': 'deleteGroup', 'groupname': 'group'}]}
  72. delete_group4_response = {'responses': [{'command': 'deleteGroup', 'error': 'Group not found'}]}
  73. # ==========================================================================
  74. # Add role
  75. # ==========================================================================
  76. # No groupname
  77. add_role1_command = { 'commands': [{'command': 'addGroupRole' }] }
  78. add_role1_response = {'responses': [{'command': 'addGroupRole', 'error': 'Invalid/missing groupname'}]}
  79. # Groupname not a string
  80. add_role2_command = { 'commands': [{'command': 'addGroupRole', 'groupname':5 }] }
  81. add_role2_response = {'responses': [{'command': 'addGroupRole', 'error': 'Invalid/missing groupname'}]}
  82. # Groupname not UTF-8
  83. add_role3_command = { 'commands': [{'command': 'addGroupRole', 'groupname': '￿LO' }] }
  84. add_role3_response = {'responses': [{'command': 'addGroupRole', 'error': 'Group name not valid UTF-8'}]}
  85. # No rolename
  86. add_role4_command = { 'commands': [{'command': 'addGroupRole', 'groupname':'g' }] }
  87. add_role4_response = {'responses': [{'command': 'addGroupRole', 'error': 'Invalid/missing rolename'}]}
  88. # Rolename not a string
  89. add_role5_command = { 'commands': [{'command': 'addGroupRole', 'groupname':'g', 'rolename':5 }] }
  90. add_role5_response = {'responses': [{'command': 'addGroupRole', 'error': 'Invalid/missing rolename'}]}
  91. # Rolename not UTF-8
  92. add_role6_command = { 'commands': [{'command': 'addGroupRole', 'groupname':'g', 'rolename':'￿LO' }] }
  93. add_role6_response = {'responses': [{'command': 'addGroupRole', 'error': 'Role name not valid UTF-8'}]}
  94. # Group not found
  95. add_role7_command = { 'commands': [{'command': 'addGroupRole', 'groupname':'notfound', 'rolename':'notfound' }] }
  96. add_role7_response = {'responses': [{'command': 'addGroupRole', 'error': 'Group not found'}]}
  97. # Role not found
  98. add_role8_command = { 'commands': [{'command': 'addGroupRole', 'groupname':'validgroup', 'rolename':'notfound' }] }
  99. add_role8_response = {'responses': [{'command': 'addGroupRole', 'error': 'Role not found'}]}
  100. # ==========================================================================
  101. # Remove role
  102. # ==========================================================================
  103. # No groupname
  104. remove_role1_command = { 'commands': [{'command': 'removeGroupRole' }] }
  105. remove_role1_response = {'responses': [{'command': 'removeGroupRole', 'error': 'Invalid/missing groupname'}]}
  106. # Groupname not a string
  107. remove_role2_command = { 'commands': [{'command': 'removeGroupRole', 'groupname':5 }] }
  108. remove_role2_response = {'responses': [{'command': 'removeGroupRole', 'error': 'Invalid/missing groupname'}]}
  109. # Groupname not UTF-8
  110. remove_role3_command = { 'commands': [{'command': 'removeGroupRole', 'groupname': '￿LO' }] }
  111. remove_role3_response = {'responses': [{'command': 'removeGroupRole', 'error': 'Group name not valid UTF-8'}]}
  112. # No rolename
  113. remove_role4_command = { 'commands': [{'command': 'removeGroupRole', 'groupname':'g' }] }
  114. remove_role4_response = {'responses': [{'command': 'removeGroupRole', 'error': 'Invalid/missing rolename'}]}
  115. # Rolename not a string
  116. remove_role5_command = { 'commands': [{'command': 'removeGroupRole', 'groupname':'g', 'rolename':5 }] }
  117. remove_role5_response = {'responses': [{'command': 'removeGroupRole', 'error': 'Invalid/missing rolename'}]}
  118. # Rolename not UTF-8
  119. remove_role6_command = { 'commands': [{'command': 'removeGroupRole', 'groupname': 'g', 'rolename':'￿LO' }] }
  120. remove_role6_response = {'responses': [{'command': 'removeGroupRole', 'error': 'Role name not valid UTF-8'}]}
  121. # Group not found
  122. remove_role7_command = { 'commands': [{'command': 'removeGroupRole', 'groupname':'notfound', 'rolename':'notfound' }] }
  123. remove_role7_response = {'responses': [{'command': 'removeGroupRole', 'error': 'Group not found'}]}
  124. # Role not found
  125. remove_role8_command = { 'commands': [{'command': 'removeGroupRole', 'groupname':'validgroup', 'rolename':'notfound' }] }
  126. remove_role8_response = {'responses': [{'command': 'removeGroupRole', 'error': 'Role not found'}]}
  127. # ==========================================================================
  128. # Add client
  129. # ==========================================================================
  130. # No groupname
  131. add_client1_command = { 'commands': [{'command': 'addGroupClient', 'username':'g' }] }
  132. add_client1_response = {'responses': [{'command': 'addGroupClient', 'error': 'Invalid/missing groupname'}]}
  133. # Groupname not a string
  134. add_client2_command = { 'commands': [{'command': 'addGroupClient', 'groupname':5, 'username':'g' }] }
  135. add_client2_response = {'responses': [{'command': 'addGroupClient', 'error': 'Invalid/missing groupname'}]}
  136. # Groupname not UTF-8
  137. add_client3_command = { 'commands': [{'command': 'addGroupClient', 'groupname': '￿LO', 'username':'g' }] }
  138. add_client3_response = {'responses': [{'command': 'addGroupClient', 'error': 'Group name not valid UTF-8'}]}
  139. # No username
  140. add_client4_command = { 'commands': [{'command': 'addGroupClient', 'groupname':'g' }] }
  141. add_client4_response = {'responses': [{'command': 'addGroupClient', 'error': 'Invalid/missing username'}]}
  142. # Username not a string
  143. add_client5_command = { 'commands': [{'command': 'addGroupClient', 'groupname':'g', 'username':5 }] }
  144. add_client5_response = {'responses': [{'command': 'addGroupClient', 'error': 'Invalid/missing username'}]}
  145. # Username not UTF-8
  146. add_client6_command = { 'commands': [{'command': 'addGroupClient', 'groupname':'g', 'username': '￿LO' }] }
  147. add_client6_response = {'responses': [{'command': 'addGroupClient', 'error': 'Username not valid UTF-8'}]}
  148. # Group not found
  149. add_client7_command = { 'commands': [{'command': 'addGroupClient', 'groupname':'notfound', 'username':'validclient' }] }
  150. add_client7_response = {'responses': [{'command': 'addGroupClient', 'error': 'Group not found'}]}
  151. # Client not found
  152. add_client8_command = { 'commands': [{'command': 'addGroupClient', 'groupname':'validgroup', 'username':'notfound' }] }
  153. add_client8_response = {'responses': [{'command': 'addGroupClient', 'error': 'Client not found'}]}
  154. # ==========================================================================
  155. # Remove client
  156. # ==========================================================================
  157. # No groupname
  158. remove_client1_command = { 'commands': [{'command': 'removeGroupClient', 'username':'g' }] }
  159. remove_client1_response = {'responses': [{'command': 'removeGroupClient', 'error': 'Invalid/missing groupname'}]}
  160. # Groupname not a string
  161. remove_client2_command = { 'commands': [{'command': 'removeGroupClient', 'groupname':5, 'username':'g' }] }
  162. remove_client2_response = {'responses': [{'command': 'removeGroupClient', 'error': 'Invalid/missing groupname'}]}
  163. # Groupname not UTF-8
  164. remove_client3_command = { 'commands': [{'command': 'removeGroupClient', 'groupname':'￿LO', 'username':'g' }] }
  165. remove_client3_response = {'responses': [{'command': 'removeGroupClient', 'error': 'Group name not valid UTF-8'}]}
  166. # No username
  167. remove_client4_command = { 'commands': [{'command': 'removeGroupClient', 'groupname':'g' }] }
  168. remove_client4_response = {'responses': [{'command': 'removeGroupClient', 'error': 'Invalid/missing username'}]}
  169. # Username not a string
  170. remove_client5_command = { 'commands': [{'command': 'removeGroupClient', 'groupname':'g', 'username':5 }] }
  171. remove_client5_response = {'responses': [{'command': 'removeGroupClient', 'error': 'Invalid/missing username'}]}
  172. # Username not UTF-8
  173. remove_client6_command = { 'commands': [{'command': 'removeGroupClient', 'groupname':'g', 'username': '￿LO' }] }
  174. remove_client6_response = {'responses': [{'command': 'removeGroupClient', 'error': 'Username not valid UTF-8'}]}
  175. # Group not found
  176. remove_client7_command = { 'commands': [{'command': 'removeGroupClient', 'groupname':'notfound', 'username':'validclient' }] }
  177. remove_client7_response = {'responses': [{'command': 'removeGroupClient', 'error': 'Group not found'}]}
  178. # Client not found
  179. remove_client8_command = { 'commands': [{'command': 'removeGroupClient', 'groupname':'validgroup', 'username':'notfound' }] }
  180. remove_client8_response = {'responses': [{'command': 'removeGroupClient', 'error': 'Client not found'}]}
  181. # ==========================================================================
  182. # Get group
  183. # ==========================================================================
  184. # No groupname
  185. get_group1_command = { 'commands': [{'command': 'getGroup'}] }
  186. get_group1_response = {'responses': [{'command': 'getGroup', 'error': 'Invalid/missing groupname'}]}
  187. # Groupname not a string
  188. get_group2_command = { 'commands': [{'command': 'getGroup', 'groupname':5}] }
  189. get_group2_response = {'responses': [{'command': 'getGroup', 'error': 'Invalid/missing groupname'}]}
  190. # Groupname not UTF-8
  191. get_group3_command = { 'commands': [{'command': 'getGroup', 'groupname':'￿LO' }] }
  192. get_group3_response = {'responses': [{'command': 'getGroup', 'error': 'Group name not valid UTF-8'}]}
  193. # Group not found
  194. get_group4_command = { 'commands': [{'command': 'getGroup', 'groupname':"missing"}] }
  195. get_group4_response = {'responses': [{'command': 'getGroup', 'error': 'Group not found'}]}
  196. # ==========================================================================
  197. # Set anon group
  198. # ==========================================================================
  199. # No groupname
  200. set_anon_group1_command = { 'commands': [{'command': 'setAnonymousGroup'}] }
  201. set_anon_group1_response = {'responses': [{'command': 'setAnonymousGroup', 'error': 'Invalid/missing groupname'}]}
  202. # Groupname not a string
  203. set_anon_group2_command = { 'commands': [{'command': 'setAnonymousGroup', 'groupname':5}] }
  204. set_anon_group2_response = {'responses': [{'command': 'setAnonymousGroup', 'error': 'Invalid/missing groupname'}]}
  205. # Groupname not UTF-8
  206. set_anon_group3_command = { 'commands': [{'command': 'setAnonymousGroup', 'groupname':'￿LO' }] }
  207. set_anon_group3_response = {'responses': [{'command': 'setAnonymousGroup', 'error': 'Group name not valid UTF-8'}]}
  208. # Group not found
  209. set_anon_group4_command = { 'commands': [{'command': 'setAnonymousGroup', 'groupname':'notfound' }] }
  210. set_anon_group4_response = {'responses': [{'command': 'setAnonymousGroup', 'error': 'Group not found'}]}
  211. # ==========================================================================
  212. # Modify group
  213. # ==========================================================================
  214. # No groupname
  215. modify_group1_command = { 'commands': [{'command': 'modifyGroup'}]}
  216. modify_group1_response = {'responses': [{'command': 'modifyGroup', 'error': 'Invalid/missing groupname'}]}
  217. # Group name not a string
  218. modify_group2_command = { 'commands': [{'command': 'modifyGroup', 'groupname':5}]}
  219. modify_group2_response = {'responses': [{'command': 'modifyGroup', 'error': 'Invalid/missing groupname'}]}
  220. # Group name not UTF-8
  221. modify_group3_command = { 'commands': [{'command': 'modifyGroup', 'groupname':'￿LO' }] }
  222. modify_group3_response = {'responses': [{'command': 'modifyGroup', 'error': 'Group name not valid UTF-8'}]}
  223. # roles not a list
  224. modify_group4_command = { 'commands': [{'command': 'modifyGroup', 'groupname':'validgroup', 'password':'test', 'roles':'string'}]}
  225. modify_group4_response = {'responses': [{'command': 'modifyGroup', 'error': "'roles' not an array or missing/invalid rolename"}]}
  226. # No rolename
  227. modify_group5_command = { 'commands': [{'command': 'modifyGroup', 'groupname':'validgroup', 'roles':[{}]}]}
  228. modify_group5_response = {'responses': [{'command': 'modifyGroup', 'error': "'roles' not an array or missing/invalid rolename"}]}
  229. # rolename not a string
  230. modify_group6_command = { 'commands': [{'command': 'modifyGroup', 'groupname':'validgroup', 'roles':[{'rolename':5}]}]}
  231. modify_group6_response = {'responses': [{'command': 'modifyGroup', 'error': "'roles' not an array or missing/invalid rolename"}]}
  232. # rolename not UTF-8
  233. #modify_group7_command = { 'commands': [{'command': 'modifyGroup', 'groupname':'validgroup','roles':[{'rolename':'￿LO'}] }] }
  234. #modify_group7_response = {'responses': [{'command': 'modifyGroup', 'error': 'Role name not valid UTF-8'}]}
  235. # Group not found
  236. modify_group8_command = { 'commands': [{'command': 'modifyGroup', 'groupname':'notfound', 'rolename':'notfound'}]}
  237. modify_group8_response = {'responses': [{'command': 'modifyGroup', 'error': 'Group not found'}]}
  238. # Role not found
  239. modify_group9_command = { 'commands': [{'command': 'modifyGroup', 'groupname':'validgroup', 'roles':[{'rolename':'notfound'}]}]}
  240. modify_group9_response = {'responses': [{'command': 'modifyGroup', 'error': 'Role not found'}]}
  241. rc = 1
  242. keepalive = 10
  243. connect_packet = mosq_test.gen_connect("ctrl-test", keepalive=keepalive, username="admin", password="admin")
  244. connack_packet = mosq_test.gen_connack(rc=0)
  245. mid = 2
  246. subscribe_packet = mosq_test.gen_subscribe(mid, "$CONTROL/dynamic-security/#", 1)
  247. suback_packet = mosq_test.gen_suback(mid, 1)
  248. try:
  249. os.mkdir(str(port))
  250. shutil.copyfile("dynamic-security-init.json", "%d/dynamic-security.json" % (port))
  251. except FileExistsError:
  252. pass
  253. broker = mosq_test.start_broker(filename=os.path.basename(__file__), use_conf=True, port=port)
  254. try:
  255. sock = mosq_test.do_client_connect(connect_packet, connack_packet, timeout=5, port=port)
  256. mosq_test.do_send_receive(sock, subscribe_packet, suback_packet, "suback")
  257. command_check(sock, create_client0_command, create_client0_response, "0")
  258. command_check(sock, create_group0_command, create_group0_response, "0")
  259. command_check(sock, create_role0_command, create_role0_response, "0")
  260. command_check(sock, create_group1_command, create_group1_response, "1")
  261. command_check(sock, create_group2_command, create_group2_response, "2")
  262. command_check(sock, create_group3_command, create_group3_response, "3")
  263. command_check(sock, create_group4_command, create_group4_response, "4")
  264. command_check(sock, create_group5_command, create_group5_response, "5")
  265. command_check(sock, create_group6_command, create_group6_response, "6")
  266. command_check(sock, create_group7_command, create_group7_response, "7")
  267. command_check(sock, delete_group1_command, delete_group1_response, "1")
  268. command_check(sock, delete_group2_command, delete_group2_response, "2")
  269. command_check(sock, delete_group3_command, delete_group3_response, "3")
  270. command_check(sock, delete_group4_command, delete_group4_response, "4")
  271. command_check(sock, add_role1_command, add_role1_response, "1")
  272. command_check(sock, add_role2_command, add_role2_response, "2")
  273. command_check(sock, add_role3_command, add_role3_response, "3")
  274. command_check(sock, add_role4_command, add_role4_response, "4")
  275. command_check(sock, add_role5_command, add_role5_response, "5")
  276. command_check(sock, add_role6_command, add_role6_response, "6")
  277. command_check(sock, add_role7_command, add_role7_response, "7")
  278. command_check(sock, add_role8_command, add_role8_response, "8")
  279. command_check(sock, remove_role1_command, remove_role1_response, "1")
  280. command_check(sock, remove_role2_command, remove_role2_response, "2")
  281. command_check(sock, remove_role3_command, remove_role3_response, "3")
  282. command_check(sock, remove_role4_command, remove_role4_response, "4")
  283. command_check(sock, remove_role5_command, remove_role5_response, "5")
  284. command_check(sock, remove_role6_command, remove_role6_response, "6")
  285. command_check(sock, remove_role7_command, remove_role7_response, "7")
  286. command_check(sock, remove_role8_command, remove_role8_response, "8")
  287. command_check(sock, add_client1_command, add_client1_response, "1")
  288. command_check(sock, add_client2_command, add_client2_response, "2")
  289. command_check(sock, add_client3_command, add_client3_response, "3")
  290. command_check(sock, add_client4_command, add_client4_response, "4")
  291. command_check(sock, add_client5_command, add_client5_response, "5")
  292. command_check(sock, add_client6_command, add_client6_response, "6")
  293. command_check(sock, add_client7_command, add_client7_response, "7")
  294. command_check(sock, add_client8_command, add_client8_response, "8")
  295. command_check(sock, remove_client1_command, remove_client1_response, "1")
  296. command_check(sock, remove_client2_command, remove_client2_response, "2")
  297. command_check(sock, remove_client3_command, remove_client3_response, "3")
  298. command_check(sock, remove_client4_command, remove_client4_response, "4")
  299. command_check(sock, remove_client5_command, remove_client5_response, "5")
  300. command_check(sock, remove_client6_command, remove_client6_response, "6")
  301. command_check(sock, remove_client7_command, remove_client7_response, "7")
  302. command_check(sock, remove_client8_command, remove_client8_response, "8")
  303. command_check(sock, get_group1_command, get_group1_response, "1")
  304. command_check(sock, get_group2_command, get_group2_response, "2")
  305. command_check(sock, get_group3_command, get_group3_response, "3")
  306. command_check(sock, get_group4_command, get_group4_response, "4")
  307. command_check(sock, set_anon_group1_command, set_anon_group1_response, "1")
  308. command_check(sock, set_anon_group2_command, set_anon_group2_response, "2")
  309. command_check(sock, set_anon_group3_command, set_anon_group3_response, "3")
  310. command_check(sock, set_anon_group4_command, set_anon_group4_response, "4")
  311. command_check(sock, modify_group1_command, modify_group1_response, "1")
  312. command_check(sock, modify_group2_command, modify_group2_response, "2")
  313. command_check(sock, modify_group3_command, modify_group3_response, "3")
  314. command_check(sock, modify_group4_command, modify_group4_response, "4")
  315. command_check(sock, modify_group5_command, modify_group5_response, "5")
  316. command_check(sock, modify_group6_command, modify_group6_response, "6")
  317. #command_check(sock, modify_group7_command, modify_group7_response, "7")
  318. command_check(sock, modify_group8_command, modify_group8_response, "8")
  319. command_check(sock, modify_group9_command, modify_group9_response, "9")
  320. rc = 0
  321. sock.close()
  322. except mosq_test.TestError:
  323. pass
  324. finally:
  325. os.remove(conf_file)
  326. try:
  327. os.remove(f"{port}/dynamic-security.json")
  328. except FileNotFoundError:
  329. pass
  330. os.rmdir(f"{port}")
  331. broker.terminate()
  332. broker.wait()
  333. (stdo, stde) = broker.communicate()
  334. if rc:
  335. print(stde.decode('utf-8'))
  336. exit(rc)