05-session-expiry-v5.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/env python3
  2. # MQTT v5. Test whether session expiry interval works correctly.
  3. from mosq_test_helper import *
  4. rc = 1
  5. keepalive = 60
  6. # This client exists to test possible fixed size int overflow and sorting of the session intervals
  7. # https://github.com/eclipse/mosquitto/issues/1525
  8. props = mqtt5_props.gen_uint32_prop(mqtt5_props.PROP_SESSION_EXPIRY_INTERVAL, 4294967294)
  9. connect0_packet = mosq_test.gen_connect("overflow", keepalive=keepalive, clean_session=False, proto_ver=5, properties=props)
  10. connack0_packet = mosq_test.gen_connack(flags=0, rc=0, proto_ver=5)
  11. props = mqtt5_props.gen_uint32_prop(mqtt5_props.PROP_SESSION_EXPIRY_INTERVAL, 1)
  12. connect_packet = mosq_test.gen_connect("clean-qos2-test", keepalive=keepalive, clean_session=False, proto_ver=5, properties=props)
  13. connack1_packet = mosq_test.gen_connack(flags=0, rc=0, proto_ver=5)
  14. connack2_packet = mosq_test.gen_connack(flags=1, rc=0, proto_ver=5)
  15. props = mqtt5_props.gen_uint32_prop(mqtt5_props.PROP_SESSION_EXPIRY_INTERVAL, 3)
  16. disconnect_packet = mosq_test.gen_disconnect(proto_ver=5, properties=props)
  17. props = mqtt5_props.gen_uint32_prop(mqtt5_props.PROP_SESSION_EXPIRY_INTERVAL, 0)
  18. disconnect2_packet = mosq_test.gen_disconnect(proto_ver=5, properties=props)
  19. port = mosq_test.get_port()
  20. broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port)
  21. try:
  22. # Connect client with wildly different session expiry, this should impact
  23. # on the test if all is well
  24. sock0 = mosq_test.do_client_connect(connect0_packet, connack0_packet, port=port, connack_error="connack 0")
  25. # Immediately disconnect, this should now be queued to expire
  26. sock0.close()
  27. # First connect, clean start is false, we expect a normal connack
  28. sock = mosq_test.do_client_connect(connect_packet, connack1_packet, port=port, connack_error="connack 1")
  29. # Forceful disconnect
  30. sock.close()
  31. # Immediate second connect, clean start is false, we expect a connack with
  32. # previous state
  33. sock = mosq_test.do_client_connect(connect_packet, connack2_packet, port=port, connack_error="connack 2")
  34. sock.close()
  35. # Session should expire in one second, so sleep longer
  36. time.sleep(2)
  37. # Third connect, clean start is false, session should have expired so we
  38. # expect a normal connack
  39. sock = mosq_test.do_client_connect(connect_packet, connack1_packet, port=port, connack_error="connack 3")
  40. # Send DISCONNECT with new session expiry, then close
  41. sock.send(disconnect_packet)
  42. sock.close()
  43. # Immediate reconnect, clean start is false, we expect a connack with
  44. # previous state
  45. sock = mosq_test.do_client_connect(connect_packet, connack2_packet, port=port, connack_error="connack 4")
  46. # Send DISCONNECT with new session expiry, then close
  47. sock.send(disconnect_packet)
  48. sock.close()
  49. # Session should expire in three seconds if it has been updated, sleep for
  50. # 2 to check it is updated from 1 second.
  51. time.sleep(2)
  52. # Immediate reconnect, clean start is false, we expect a connack with
  53. # previous state
  54. sock = mosq_test.do_client_connect(connect_packet, connack2_packet, port=port, connack_error="connack 5")
  55. # Send DISCONNECT with new session expiry, then close
  56. sock.send(disconnect_packet)
  57. sock.close()
  58. # Session should expire in three seconds, so sleep longer
  59. time.sleep(4)
  60. # Third connect, clean start is false, session should have expired so we
  61. # expect a normal connack
  62. sock = mosq_test.do_client_connect(connect_packet, connack1_packet, port=port, connack_error="connack 6")
  63. # Send DISCONNECT with 0 session expiry, then close
  64. sock.send(disconnect2_packet)
  65. sock.close()
  66. # Immediate reconnect, session should have been removed.
  67. sock = mosq_test.do_client_connect(connect_packet, connack1_packet, port=port, connack_error="connack 7")
  68. sock.close()
  69. rc = 0
  70. sock.close()
  71. except mosq_test.TestError:
  72. pass
  73. finally:
  74. broker.terminate()
  75. broker.wait()
  76. (stdo, stde) = broker.communicate()
  77. if rc:
  78. print(stde.decode('utf-8'))
  79. exit(rc)