09-plugin-auth-v2-unpwd-fail.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env python3
  2. # Test whether a connection is successful with correct username and password
  3. # when using a simple auth_plugin.
  4. from mosq_test_helper import *
  5. def write_config(filename, port):
  6. with open(filename, 'w') as f:
  7. f.write("port %d\n" % (port))
  8. f.write("auth_plugin c/auth_plugin_v2.so\n")
  9. f.write("allow_anonymous false\n")
  10. port = mosq_test.get_port()
  11. conf_file = os.path.basename(__file__).replace('.py', '.conf')
  12. write_config(conf_file, port)
  13. rc = 1
  14. keepalive = 10
  15. connect_packet = mosq_test.gen_connect("connect-uname-pwd-test", keepalive=keepalive, username="test-username", password="wrong")
  16. connack_packet = mosq_test.gen_connack(rc=5)
  17. broker = mosq_test.start_broker(filename=os.path.basename(__file__), use_conf=True, port=port)
  18. try:
  19. sock = mosq_test.do_client_connect(connect_packet, connack_packet, timeout=20, port=port)
  20. rc = 0
  21. sock.close()
  22. except mosq_test.TestError:
  23. pass
  24. finally:
  25. os.remove(conf_file)
  26. broker.terminate()
  27. broker.wait()
  28. (stdo, stde) = broker.communicate()
  29. if rc:
  30. print(stde.decode('utf-8'))
  31. exit(rc)