03-publish-c2b-qos2.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/env python3
  2. # Test whether a client sends a correct PUBLISH to a topic with QoS 2.
  3. # The client should connect to port 1888 with keepalive=60, clean session set,
  4. # and client id publish-qos2-test
  5. # The test will send a CONNACK message to the client with rc=0. Upon receiving
  6. # the CONNACK the client should verify that rc==0. If not, it should exit with
  7. # return code=1.
  8. # On a successful CONNACK, the client should send a PUBLISH message with topic
  9. # "pub/qos2/test", payload "message" and QoS=2.
  10. # The test will not respond to the first PUBLISH message, so the client must
  11. # resend the PUBLISH message with dup=1. Note that to keep test durations low, a
  12. # message retry timeout of less than 10 seconds is required for this test.
  13. # On receiving the second PUBLISH message, the test will send the correct
  14. # PUBREC response. On receiving the correct PUBREC response, the client should
  15. # send a PUBREL message.
  16. # The test will not respond to the first PUBREL message, so the client must
  17. # resend the PUBREL message with dup=1. On receiving the second PUBREL message,
  18. # the test will send the correct PUBCOMP response. On receiving the correct
  19. # PUBCOMP response, the client should send a DISCONNECT message.
  20. from mosq_test_helper import *
  21. port = mosq_test.get_lib_port()
  22. rc = 1
  23. keepalive = 60
  24. connect_packet = mosq_test.gen_connect("publish-qos2-test", keepalive=keepalive)
  25. connack_packet = mosq_test.gen_connack(rc=0)
  26. disconnect_packet = mosq_test.gen_disconnect()
  27. mid = 1
  28. publish_packet = mosq_test.gen_publish("pub/qos2/test", qos=2, mid=mid, payload="message")
  29. publish_dup_packet = mosq_test.gen_publish("pub/qos2/test", qos=2, mid=mid, payload="message", dup=True)
  30. pubrec_packet = mosq_test.gen_pubrec(mid)
  31. pubrel_packet = mosq_test.gen_pubrel(mid)
  32. pubcomp_packet = mosq_test.gen_pubcomp(mid)
  33. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  34. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  35. sock.settimeout(10)
  36. sock.bind(('', port))
  37. sock.listen(5)
  38. client_args = sys.argv[1:]
  39. env = dict(os.environ)
  40. env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp'
  41. try:
  42. pp = env['PYTHONPATH']
  43. except KeyError:
  44. pp = ''
  45. env['PYTHONPATH'] = '../../lib/python:'+pp
  46. client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port)
  47. try:
  48. (conn, address) = sock.accept()
  49. conn.settimeout(10)
  50. mosq_test.do_receive_send(conn, connect_packet, connack_packet, "connect")
  51. mosq_test.do_receive_send(conn, publish_packet, pubrec_packet, "publish")
  52. mosq_test.do_receive_send(conn, pubrel_packet, pubcomp_packet, "pubrel")
  53. mosq_test.expect_packet(conn, "disconnect", disconnect_packet)
  54. rc = 0
  55. conn.close()
  56. except mosq_test.TestError:
  57. pass
  58. finally:
  59. client.terminate()
  60. client.wait()
  61. sock.close()
  62. exit(rc)