03-publish-qos0-no-payload.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env python3
  2. # Test whether a client sends a correct PUBLISH to a topic with QoS 0 and no payload.
  3. # The client should connect to port 1888 with keepalive=60, clean session set,
  4. # and client id publish-qos0-test-np
  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/no-payload/test" with zero length payload and QoS=0. If
  8. # rc!=0, the 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("publish-qos0-test-np", keepalive=keepalive)
  15. connack_packet = mosq_test.gen_connack(rc=0)
  16. publish_packet = mosq_test.gen_publish("pub/qos0/no-payload/test", qos=0)
  17. disconnect_packet = mosq_test.gen_disconnect()
  18. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  19. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  20. sock.settimeout(10)
  21. sock.bind(('', port))
  22. sock.listen(5)
  23. client_args = sys.argv[1:]
  24. env = dict(os.environ)
  25. env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp'
  26. try:
  27. pp = env['PYTHONPATH']
  28. except KeyError:
  29. pp = ''
  30. env['PYTHONPATH'] = '../../lib/python:'+pp
  31. client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port)
  32. try:
  33. (conn, address) = sock.accept()
  34. conn.settimeout(10)
  35. mosq_test.do_receive_send(conn, connect_packet, connack_packet, "connect")
  36. mosq_test.expect_packet(conn, "publish", publish_packet)
  37. mosq_test.expect_packet(conn, "disconnect", disconnect_packet)
  38. rc = 0
  39. conn.close()
  40. except mosq_test.TestError:
  41. pass
  42. finally:
  43. client.terminate()
  44. client.wait()
  45. sock.close()
  46. exit(rc)