09-plugin-auth-msg-params.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/env python3
  2. # Test whether message parameters are passed to the plugin acl check function.
  3. from mosq_test_helper import *
  4. def write_config(filename, port):
  5. with open(filename, 'w') as f:
  6. f.write("port %d\n" % (port))
  7. f.write("auth_plugin c/auth_plugin_msg_params.so\n")
  8. f.write("allow_anonymous true\n")
  9. port = mosq_test.get_port()
  10. conf_file = os.path.basename(__file__).replace('.py', '.conf')
  11. write_config(conf_file, port)
  12. rc = 1
  13. keepalive = 10
  14. connect_packet = mosq_test.gen_connect("msg-param-test", keepalive=keepalive)
  15. connack_packet = mosq_test.gen_connack(rc=0)
  16. mid = 2
  17. subscribe_packet = mosq_test.gen_subscribe(mid, "param/topic", 1)
  18. suback_packet = mosq_test.gen_suback(mid, 1)
  19. mid = 3
  20. publish_packet = mosq_test.gen_publish(topic="param/topic", qos=1, payload="payload contents", retain=1, mid=mid)
  21. puback_packet = mosq_test.gen_puback(mid)
  22. mid = 1
  23. publish_packet_recv = mosq_test.gen_publish(topic="param/topic", qos=1, payload="payload contents", retain=0, mid=mid)
  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. sock.send(publish_packet)
  29. mosq_test.receive_unordered(sock, puback_packet, publish_packet_recv, "puback/publish_receive")
  30. rc = 0
  31. sock.close()
  32. except mosq_test.TestError:
  33. pass
  34. finally:
  35. os.remove(conf_file)
  36. broker.terminate()
  37. broker.wait()
  38. (stdo, stde) = broker.communicate()
  39. if rc:
  40. print(stde.decode('utf-8'))
  41. exit(rc)