11-prop-send-payload-format.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env python3
  2. # Test whether a client sends a correct PUBLISH to a topic with QoS 0.
  3. # The client should connect to port 1888 with keepalive=60, clean session set,
  4. # and client id publish-qos0-test
  5. # The test will send a CONNACK message to the client with rc=0. Upon receiving
  6. # the CONNACK and verifying that rc=0, the client should send a PUBLISH message
  7. # to topic "pub/qos0/test" with payload "message" and QoS=0. If rc!=0, the
  8. # client should exit with an error.
  9. # After sending the PUBLISH message, the client should send a DISCONNECT message.
  10. from mosq_test_helper import *
  11. port = mosq_test.get_lib_port()
  12. rc = 1
  13. keepalive = 60
  14. connect_packet = mosq_test.gen_connect("prop-test", keepalive=keepalive, proto_ver=5)
  15. connack_packet = mosq_test.gen_connack(rc=0, proto_ver=5)
  16. props = mqtt5_props.gen_byte_prop(mqtt5_props.PROP_PAYLOAD_FORMAT_INDICATOR, 0x01)
  17. publish_packet = mosq_test.gen_publish("prop/qos0", qos=0, payload="message", proto_ver=5, properties=props)
  18. disconnect_packet = mosq_test.gen_disconnect(proto_ver=5)
  19. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  20. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  21. sock.settimeout(10)
  22. sock.bind(('', port))
  23. sock.listen(5)
  24. client_args = sys.argv[1:]
  25. env = dict(os.environ)
  26. env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp'
  27. try:
  28. pp = env['PYTHONPATH']
  29. except KeyError:
  30. pp = ''
  31. env['PYTHONPATH'] = '../../lib/python:'+pp
  32. client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port)
  33. try:
  34. (conn, address) = sock.accept()
  35. conn.settimeout(10)
  36. mosq_test.do_receive_send(conn, connect_packet, connack_packet, "connect")
  37. mosq_test.expect_packet(conn, "publish", publish_packet)
  38. mosq_test.expect_packet(conn, "disconnect", disconnect_packet)
  39. rc = 0
  40. conn.close()
  41. except mosq_test.TestError:
  42. pass
  43. finally:
  44. client.terminate()
  45. client.wait()
  46. if rc:
  47. (stdo, stde) = client.communicate()
  48. print(stde)
  49. sock.close()
  50. exit(rc)