09-plugin-auth-acl-sub-denied.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env python3
  2. # Test topic subscription. All SUBSCRIBE requests are denied. Check this
  3. # produces the correct response, and check the client isn't disconnected (ref:
  4. # issue #1016).
  5. from mosq_test_helper import *
  6. def write_config(filename, port):
  7. with open(filename, 'w') as f:
  8. f.write("port %d\n" % (port))
  9. f.write("auth_plugin c/auth_plugin_acl_sub_denied.so\n")
  10. f.write("allow_anonymous false\n")
  11. port = mosq_test.get_port()
  12. conf_file = os.path.basename(__file__).replace('.py', '.conf')
  13. write_config(conf_file, port)
  14. rc = 1
  15. keepalive = 10
  16. connect_packet = mosq_test.gen_connect("sub-denied-test", keepalive=keepalive, username="denied")
  17. connack_packet = mosq_test.gen_connack(rc=0)
  18. mid = 53
  19. subscribe_packet = mosq_test.gen_subscribe(mid, "qos0/test", 0)
  20. suback_packet = mosq_test.gen_suback(mid, 128)
  21. mid_pub = 54
  22. publish_packet = mosq_test.gen_publish("topic", qos=1, payload="test", mid=mid_pub)
  23. puback_packet = mosq_test.gen_puback(mid_pub)
  24. broker = mosq_test.start_broker(filename=os.path.basename(__file__), use_conf=True, port=port)
  25. try:
  26. sock = mosq_test.do_client_connect(connect_packet, connack_packet, timeout=20, port=port)
  27. mosq_test.do_send_receive(sock, subscribe_packet, suback_packet, "suback")
  28. mosq_test.do_send_receive(sock, publish_packet, puback_packet, "puback")
  29. rc = 0
  30. sock.close()
  31. except mosq_test.TestError:
  32. pass
  33. finally:
  34. os.remove(conf_file)
  35. broker.terminate()
  36. broker.wait()
  37. (stdo, stde) = broker.communicate()
  38. if rc:
  39. print(stde.decode('utf-8'))
  40. exit(rc)