123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439 |
- from mosq_test_helper import *
- import json
- import shutil
- def write_config(filename, port):
- with open(filename, 'w') as f:
- f.write("listener %d\n" % (port))
- f.write("allow_anonymous true\n")
- f.write("plugin ../../plugins/dynamic-security/mosquitto_dynamic_security.so\n")
- f.write("plugin_opt_config_file %d/dynamic-security.json\n" % (port))
- def command_check(sock, command_payload, expected_response, msg=""):
- command_packet = mosq_test.gen_publish(topic="$CONTROL/dynamic-security/v1", qos=0, payload=json.dumps(command_payload))
- sock.send(command_packet)
- response = json.loads(mosq_test.read_publish(sock))
- if response != expected_response:
- print(expected_response)
- print(response)
- if msg != "":
- print(msg)
- raise ValueError(response)
- port = mosq_test.get_port()
- conf_file = os.path.basename(__file__).replace('.py', '.conf')
- write_config(conf_file, port)
- create_client0_command = { 'commands': [{'command': 'createClient', 'username':'validclient' }] }
- create_client0_response = {'responses': [{'command': 'createClient'}]}
- create_group0_command = { 'commands': [{'command': 'createGroup', 'groupname':'validgroup' }] }
- create_group0_response = {'responses': [{'command': 'createGroup'}]}
- create_role0_command = { 'commands': [{'command': 'createRole', 'rolename':'validrole' }] }
- create_role0_response = {'responses': [{'command': 'createRole'}]}
- create_group1_command = { 'commands': [{'command': 'createGroup' }] }
- create_group1_response = {'responses': [{'command': 'createGroup', 'error': 'Invalid/missing groupname'}]}
- create_group2_command = { 'commands': [{'command': 'createGroup', 'groupname':5 }] }
- create_group2_response = {'responses': [{'command': 'createGroup', 'error': 'Invalid/missing groupname'}]}
- create_group3_command = { 'commands': [{'command': 'createGroup', 'groupname': 'LO' }] }
- create_group3_response = {'responses': [{'command': 'createGroup', 'error': 'Group name not valid UTF-8'}]}
- create_group4_command = { 'commands': [{'command': 'createGroup', 'groupname':'g', 'textname':5 }] }
- create_group4_response = {'responses': [{'command': 'createGroup', 'error': 'Invalid/missing textname'}]}
- create_group5_command = { 'commands': [{'command': 'createGroup', 'groupname':'g', 'textdescription':5 }] }
- create_group5_response = {'responses': [{'command': 'createGroup', 'error': 'Invalid/missing textdescription'}]}
- create_group6_command = { 'commands': [{'command': 'createGroup', 'groupname': 'validgroup'}]}
- create_group6_response = {'responses': [{'command': 'createGroup', 'error': 'Group already exists'}]}
- create_group7_command = { 'commands': [{'command': 'createGroup', 'groupname': 'group', 'roles':[{'rolename':'notfound'}]}] }
- create_group7_response = {'responses': [{'command': 'createGroup', 'error': 'Role not found'}]}
- delete_group1_command = { 'commands': [{'command': 'deleteGroup' }] }
- delete_group1_response = {'responses': [{'command': 'deleteGroup', 'error': 'Invalid/missing groupname'}]}
- delete_group2_command = { 'commands': [{'command': 'deleteGroup', 'groupname':5 }] }
- delete_group2_response = {'responses': [{'command': 'deleteGroup', 'error': 'Invalid/missing groupname'}]}
- delete_group3_command = { 'commands': [{'command': 'deleteGroup', 'groupname': 'LO' }] }
- delete_group3_response = {'responses': [{'command': 'deleteGroup', 'error': 'Group name not valid UTF-8'}]}
- delete_group4_command = { 'commands': [{'command': 'deleteGroup', 'groupname': 'group'}]}
- delete_group4_response = {'responses': [{'command': 'deleteGroup', 'error': 'Group not found'}]}
- add_role1_command = { 'commands': [{'command': 'addGroupRole' }] }
- add_role1_response = {'responses': [{'command': 'addGroupRole', 'error': 'Invalid/missing groupname'}]}
- add_role2_command = { 'commands': [{'command': 'addGroupRole', 'groupname':5 }] }
- add_role2_response = {'responses': [{'command': 'addGroupRole', 'error': 'Invalid/missing groupname'}]}
- add_role3_command = { 'commands': [{'command': 'addGroupRole', 'groupname': 'LO' }] }
- add_role3_response = {'responses': [{'command': 'addGroupRole', 'error': 'Group name not valid UTF-8'}]}
- add_role4_command = { 'commands': [{'command': 'addGroupRole', 'groupname':'g' }] }
- add_role4_response = {'responses': [{'command': 'addGroupRole', 'error': 'Invalid/missing rolename'}]}
- add_role5_command = { 'commands': [{'command': 'addGroupRole', 'groupname':'g', 'rolename':5 }] }
- add_role5_response = {'responses': [{'command': 'addGroupRole', 'error': 'Invalid/missing rolename'}]}
- add_role6_command = { 'commands': [{'command': 'addGroupRole', 'groupname':'g', 'rolename':'LO' }] }
- add_role6_response = {'responses': [{'command': 'addGroupRole', 'error': 'Role name not valid UTF-8'}]}
- add_role7_command = { 'commands': [{'command': 'addGroupRole', 'groupname':'notfound', 'rolename':'notfound' }] }
- add_role7_response = {'responses': [{'command': 'addGroupRole', 'error': 'Group not found'}]}
- add_role8_command = { 'commands': [{'command': 'addGroupRole', 'groupname':'validgroup', 'rolename':'notfound' }] }
- add_role8_response = {'responses': [{'command': 'addGroupRole', 'error': 'Role not found'}]}
- remove_role1_command = { 'commands': [{'command': 'removeGroupRole' }] }
- remove_role1_response = {'responses': [{'command': 'removeGroupRole', 'error': 'Invalid/missing groupname'}]}
- remove_role2_command = { 'commands': [{'command': 'removeGroupRole', 'groupname':5 }] }
- remove_role2_response = {'responses': [{'command': 'removeGroupRole', 'error': 'Invalid/missing groupname'}]}
- remove_role3_command = { 'commands': [{'command': 'removeGroupRole', 'groupname': 'LO' }] }
- remove_role3_response = {'responses': [{'command': 'removeGroupRole', 'error': 'Group name not valid UTF-8'}]}
- remove_role4_command = { 'commands': [{'command': 'removeGroupRole', 'groupname':'g' }] }
- remove_role4_response = {'responses': [{'command': 'removeGroupRole', 'error': 'Invalid/missing rolename'}]}
- remove_role5_command = { 'commands': [{'command': 'removeGroupRole', 'groupname':'g', 'rolename':5 }] }
- remove_role5_response = {'responses': [{'command': 'removeGroupRole', 'error': 'Invalid/missing rolename'}]}
- remove_role6_command = { 'commands': [{'command': 'removeGroupRole', 'groupname': 'g', 'rolename':'LO' }] }
- remove_role6_response = {'responses': [{'command': 'removeGroupRole', 'error': 'Role name not valid UTF-8'}]}
- remove_role7_command = { 'commands': [{'command': 'removeGroupRole', 'groupname':'notfound', 'rolename':'notfound' }] }
- remove_role7_response = {'responses': [{'command': 'removeGroupRole', 'error': 'Group not found'}]}
- remove_role8_command = { 'commands': [{'command': 'removeGroupRole', 'groupname':'validgroup', 'rolename':'notfound' }] }
- remove_role8_response = {'responses': [{'command': 'removeGroupRole', 'error': 'Role not found'}]}
- add_client1_command = { 'commands': [{'command': 'addGroupClient', 'username':'g' }] }
- add_client1_response = {'responses': [{'command': 'addGroupClient', 'error': 'Invalid/missing groupname'}]}
- add_client2_command = { 'commands': [{'command': 'addGroupClient', 'groupname':5, 'username':'g' }] }
- add_client2_response = {'responses': [{'command': 'addGroupClient', 'error': 'Invalid/missing groupname'}]}
- add_client3_command = { 'commands': [{'command': 'addGroupClient', 'groupname': 'LO', 'username':'g' }] }
- add_client3_response = {'responses': [{'command': 'addGroupClient', 'error': 'Group name not valid UTF-8'}]}
- add_client4_command = { 'commands': [{'command': 'addGroupClient', 'groupname':'g' }] }
- add_client4_response = {'responses': [{'command': 'addGroupClient', 'error': 'Invalid/missing username'}]}
- add_client5_command = { 'commands': [{'command': 'addGroupClient', 'groupname':'g', 'username':5 }] }
- add_client5_response = {'responses': [{'command': 'addGroupClient', 'error': 'Invalid/missing username'}]}
- add_client6_command = { 'commands': [{'command': 'addGroupClient', 'groupname':'g', 'username': 'LO' }] }
- add_client6_response = {'responses': [{'command': 'addGroupClient', 'error': 'Username not valid UTF-8'}]}
- add_client7_command = { 'commands': [{'command': 'addGroupClient', 'groupname':'notfound', 'username':'validclient' }] }
- add_client7_response = {'responses': [{'command': 'addGroupClient', 'error': 'Group not found'}]}
- add_client8_command = { 'commands': [{'command': 'addGroupClient', 'groupname':'validgroup', 'username':'notfound' }] }
- add_client8_response = {'responses': [{'command': 'addGroupClient', 'error': 'Client not found'}]}
- remove_client1_command = { 'commands': [{'command': 'removeGroupClient', 'username':'g' }] }
- remove_client1_response = {'responses': [{'command': 'removeGroupClient', 'error': 'Invalid/missing groupname'}]}
- remove_client2_command = { 'commands': [{'command': 'removeGroupClient', 'groupname':5, 'username':'g' }] }
- remove_client2_response = {'responses': [{'command': 'removeGroupClient', 'error': 'Invalid/missing groupname'}]}
- remove_client3_command = { 'commands': [{'command': 'removeGroupClient', 'groupname':'LO', 'username':'g' }] }
- remove_client3_response = {'responses': [{'command': 'removeGroupClient', 'error': 'Group name not valid UTF-8'}]}
- remove_client4_command = { 'commands': [{'command': 'removeGroupClient', 'groupname':'g' }] }
- remove_client4_response = {'responses': [{'command': 'removeGroupClient', 'error': 'Invalid/missing username'}]}
- remove_client5_command = { 'commands': [{'command': 'removeGroupClient', 'groupname':'g', 'username':5 }] }
- remove_client5_response = {'responses': [{'command': 'removeGroupClient', 'error': 'Invalid/missing username'}]}
- remove_client6_command = { 'commands': [{'command': 'removeGroupClient', 'groupname':'g', 'username': 'LO' }] }
- remove_client6_response = {'responses': [{'command': 'removeGroupClient', 'error': 'Username not valid UTF-8'}]}
- remove_client7_command = { 'commands': [{'command': 'removeGroupClient', 'groupname':'notfound', 'username':'validclient' }] }
- remove_client7_response = {'responses': [{'command': 'removeGroupClient', 'error': 'Group not found'}]}
- remove_client8_command = { 'commands': [{'command': 'removeGroupClient', 'groupname':'validgroup', 'username':'notfound' }] }
- remove_client8_response = {'responses': [{'command': 'removeGroupClient', 'error': 'Client not found'}]}
- get_group1_command = { 'commands': [{'command': 'getGroup'}] }
- get_group1_response = {'responses': [{'command': 'getGroup', 'error': 'Invalid/missing groupname'}]}
- get_group2_command = { 'commands': [{'command': 'getGroup', 'groupname':5}] }
- get_group2_response = {'responses': [{'command': 'getGroup', 'error': 'Invalid/missing groupname'}]}
- get_group3_command = { 'commands': [{'command': 'getGroup', 'groupname':'LO' }] }
- get_group3_response = {'responses': [{'command': 'getGroup', 'error': 'Group name not valid UTF-8'}]}
- get_group4_command = { 'commands': [{'command': 'getGroup', 'groupname':"missing"}] }
- get_group4_response = {'responses': [{'command': 'getGroup', 'error': 'Group not found'}]}
- set_anon_group1_command = { 'commands': [{'command': 'setAnonymousGroup'}] }
- set_anon_group1_response = {'responses': [{'command': 'setAnonymousGroup', 'error': 'Invalid/missing groupname'}]}
- set_anon_group2_command = { 'commands': [{'command': 'setAnonymousGroup', 'groupname':5}] }
- set_anon_group2_response = {'responses': [{'command': 'setAnonymousGroup', 'error': 'Invalid/missing groupname'}]}
- set_anon_group3_command = { 'commands': [{'command': 'setAnonymousGroup', 'groupname':'LO' }] }
- set_anon_group3_response = {'responses': [{'command': 'setAnonymousGroup', 'error': 'Group name not valid UTF-8'}]}
- set_anon_group4_command = { 'commands': [{'command': 'setAnonymousGroup', 'groupname':'notfound' }] }
- set_anon_group4_response = {'responses': [{'command': 'setAnonymousGroup', 'error': 'Group not found'}]}
- modify_group1_command = { 'commands': [{'command': 'modifyGroup'}]}
- modify_group1_response = {'responses': [{'command': 'modifyGroup', 'error': 'Invalid/missing groupname'}]}
- modify_group2_command = { 'commands': [{'command': 'modifyGroup', 'groupname':5}]}
- modify_group2_response = {'responses': [{'command': 'modifyGroup', 'error': 'Invalid/missing groupname'}]}
- modify_group3_command = { 'commands': [{'command': 'modifyGroup', 'groupname':'LO' }] }
- modify_group3_response = {'responses': [{'command': 'modifyGroup', 'error': 'Group name not valid UTF-8'}]}
- modify_group4_command = { 'commands': [{'command': 'modifyGroup', 'groupname':'validgroup', 'password':'test', 'roles':'string'}]}
- modify_group4_response = {'responses': [{'command': 'modifyGroup', 'error': "'roles' not an array or missing/invalid rolename"}]}
- modify_group5_command = { 'commands': [{'command': 'modifyGroup', 'groupname':'validgroup', 'roles':[{}]}]}
- modify_group5_response = {'responses': [{'command': 'modifyGroup', 'error': "'roles' not an array or missing/invalid rolename"}]}
- modify_group6_command = { 'commands': [{'command': 'modifyGroup', 'groupname':'validgroup', 'roles':[{'rolename':5}]}]}
- modify_group6_response = {'responses': [{'command': 'modifyGroup', 'error': "'roles' not an array or missing/invalid rolename"}]}
- modify_group8_command = { 'commands': [{'command': 'modifyGroup', 'groupname':'notfound', 'rolename':'notfound'}]}
- modify_group8_response = {'responses': [{'command': 'modifyGroup', 'error': 'Group not found'}]}
- modify_group9_command = { 'commands': [{'command': 'modifyGroup', 'groupname':'validgroup', 'roles':[{'rolename':'notfound'}]}]}
- modify_group9_response = {'responses': [{'command': 'modifyGroup', 'error': 'Role not found'}]}
- rc = 1
- keepalive = 10
- connect_packet = mosq_test.gen_connect("ctrl-test", keepalive=keepalive, username="admin", password="admin")
- connack_packet = mosq_test.gen_connack(rc=0)
- mid = 2
- subscribe_packet = mosq_test.gen_subscribe(mid, "$CONTROL/dynamic-security/#", 1)
- suback_packet = mosq_test.gen_suback(mid, 1)
- try:
- os.mkdir(str(port))
- shutil.copyfile("dynamic-security-init.json", "%d/dynamic-security.json" % (port))
- except FileExistsError:
- pass
- broker = mosq_test.start_broker(filename=os.path.basename(__file__), use_conf=True, port=port)
- try:
- sock = mosq_test.do_client_connect(connect_packet, connack_packet, timeout=5, port=port)
- mosq_test.do_send_receive(sock, subscribe_packet, suback_packet, "suback")
- command_check(sock, create_client0_command, create_client0_response, "0")
- command_check(sock, create_group0_command, create_group0_response, "0")
- command_check(sock, create_role0_command, create_role0_response, "0")
- command_check(sock, create_group1_command, create_group1_response, "1")
- command_check(sock, create_group2_command, create_group2_response, "2")
- command_check(sock, create_group3_command, create_group3_response, "3")
- command_check(sock, create_group4_command, create_group4_response, "4")
- command_check(sock, create_group5_command, create_group5_response, "5")
- command_check(sock, create_group6_command, create_group6_response, "6")
- command_check(sock, create_group7_command, create_group7_response, "7")
- command_check(sock, delete_group1_command, delete_group1_response, "1")
- command_check(sock, delete_group2_command, delete_group2_response, "2")
- command_check(sock, delete_group3_command, delete_group3_response, "3")
- command_check(sock, delete_group4_command, delete_group4_response, "4")
- command_check(sock, add_role1_command, add_role1_response, "1")
- command_check(sock, add_role2_command, add_role2_response, "2")
- command_check(sock, add_role3_command, add_role3_response, "3")
- command_check(sock, add_role4_command, add_role4_response, "4")
- command_check(sock, add_role5_command, add_role5_response, "5")
- command_check(sock, add_role6_command, add_role6_response, "6")
- command_check(sock, add_role7_command, add_role7_response, "7")
- command_check(sock, add_role8_command, add_role8_response, "8")
- command_check(sock, remove_role1_command, remove_role1_response, "1")
- command_check(sock, remove_role2_command, remove_role2_response, "2")
- command_check(sock, remove_role3_command, remove_role3_response, "3")
- command_check(sock, remove_role4_command, remove_role4_response, "4")
- command_check(sock, remove_role5_command, remove_role5_response, "5")
- command_check(sock, remove_role6_command, remove_role6_response, "6")
- command_check(sock, remove_role7_command, remove_role7_response, "7")
- command_check(sock, remove_role8_command, remove_role8_response, "8")
- command_check(sock, add_client1_command, add_client1_response, "1")
- command_check(sock, add_client2_command, add_client2_response, "2")
- command_check(sock, add_client3_command, add_client3_response, "3")
- command_check(sock, add_client4_command, add_client4_response, "4")
- command_check(sock, add_client5_command, add_client5_response, "5")
- command_check(sock, add_client6_command, add_client6_response, "6")
- command_check(sock, add_client7_command, add_client7_response, "7")
- command_check(sock, add_client8_command, add_client8_response, "8")
- command_check(sock, remove_client1_command, remove_client1_response, "1")
- command_check(sock, remove_client2_command, remove_client2_response, "2")
- command_check(sock, remove_client3_command, remove_client3_response, "3")
- command_check(sock, remove_client4_command, remove_client4_response, "4")
- command_check(sock, remove_client5_command, remove_client5_response, "5")
- command_check(sock, remove_client6_command, remove_client6_response, "6")
- command_check(sock, remove_client7_command, remove_client7_response, "7")
- command_check(sock, remove_client8_command, remove_client8_response, "8")
- command_check(sock, get_group1_command, get_group1_response, "1")
- command_check(sock, get_group2_command, get_group2_response, "2")
- command_check(sock, get_group3_command, get_group3_response, "3")
- command_check(sock, get_group4_command, get_group4_response, "4")
- command_check(sock, set_anon_group1_command, set_anon_group1_response, "1")
- command_check(sock, set_anon_group2_command, set_anon_group2_response, "2")
- command_check(sock, set_anon_group3_command, set_anon_group3_response, "3")
- command_check(sock, set_anon_group4_command, set_anon_group4_response, "4")
- command_check(sock, modify_group1_command, modify_group1_response, "1")
- command_check(sock, modify_group2_command, modify_group2_response, "2")
- command_check(sock, modify_group3_command, modify_group3_response, "3")
- command_check(sock, modify_group4_command, modify_group4_response, "4")
- command_check(sock, modify_group5_command, modify_group5_response, "5")
- command_check(sock, modify_group6_command, modify_group6_response, "6")
-
- command_check(sock, modify_group8_command, modify_group8_response, "8")
- command_check(sock, modify_group9_command, modify_group9_response, "9")
- rc = 0
- sock.close()
- except mosq_test.TestError:
- pass
- finally:
- os.remove(conf_file)
- try:
- os.remove(f"{port}/dynamic-security.json")
- except FileNotFoundError:
- pass
- os.rmdir(f"{port}")
- broker.terminate()
- broker.wait()
- (stdo, stde) = broker.communicate()
- if rc:
- print(stde.decode('utf-8'))
- exit(rc)
|