01-connect-zero-length-id.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/env python3
  2. # Test whether a CONNECT with a zero length client id results in the correct behaviour.
  3. # MQTT v3.1.1 - zero length is allowed, unless allow_zero_length_clientid is false, and unless clean_start is False.
  4. # MQTT v5.0 - zero length is allowed, unless allow_zero_length_clientid is false
  5. from mosq_test_helper import *
  6. def write_config(filename, port1, port2, per_listener, allow_zero):
  7. with open(filename, 'w') as f:
  8. f.write("per_listener_settings %s\n" % (per_listener))
  9. f.write("listener %d\n" % (port2))
  10. f.write("allow_anonymous true\n")
  11. if allow_zero != "":
  12. f.write("allow_zero_length_clientid %s\n" % (allow_zero))
  13. f.write("listener %d\n" % (port1))
  14. f.write("allow_anonymous true\n")
  15. if allow_zero != "":
  16. f.write("allow_zero_length_clientid %s\n" % (allow_zero))
  17. def do_test(per_listener, proto_ver, clean_start, allow_zero, client_port, expect_fail):
  18. conf_file = os.path.basename(__file__).replace('.py', '.conf')
  19. write_config(conf_file, port1, port2, per_listener, allow_zero)
  20. rc = 1
  21. keepalive = 10
  22. connect_packet = mosq_test.gen_connect("", keepalive=keepalive, proto_ver=proto_ver, clean_session=clean_start)
  23. if proto_ver == 4:
  24. if expect_fail == True:
  25. connack_packet = mosq_test.gen_connack(rc=2, proto_ver=proto_ver)
  26. else:
  27. connack_packet = mosq_test.gen_connack(rc=0, proto_ver=proto_ver)
  28. else:
  29. if expect_fail == True:
  30. connack_packet = mosq_test.gen_connack(rc=128, proto_ver=proto_ver, properties=None)
  31. else:
  32. props = mqtt5_props.gen_string_prop(mqtt5_props.PROP_ASSIGNED_CLIENT_IDENTIFIER, "auto-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")
  33. connack_packet = mosq_test.gen_connack(rc=0, proto_ver=proto_ver, properties=props)
  34. # Remove the "xxxx" part - this means the front part of the packet
  35. # is correct (so remaining length etc. is correct), but we don't
  36. # need to match against the random id.
  37. connack_packet = connack_packet[:-39]
  38. broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port1, use_conf=True)
  39. try:
  40. sock = mosq_test.do_client_connect(connect_packet, connack_packet, port=client_port)
  41. sock.close()
  42. rc = 0
  43. except mosq_test.TestError:
  44. pass
  45. finally:
  46. broker.terminate()
  47. broker.wait()
  48. (stdo, stde) = broker.communicate()
  49. os.remove(conf_file)
  50. if rc:
  51. print(stde.decode('utf-8'))
  52. print("per_listener:%s proto_ver:%d client_port:%d clean_start:%d allow_zero:%s" % (per_listener, proto_ver, client_port, clean_start, allow_zero))
  53. print("port1:%d port2:%d" % (port1, port2))
  54. exit(rc)
  55. (port1, port2) = mosq_test.get_port(2)
  56. test_v4 = True
  57. test_v5 = True
  58. if test_v4 == True:
  59. do_test(per_listener="false", proto_ver=4, client_port=port1, clean_start=True, allow_zero="true", expect_fail=False)
  60. do_test(per_listener="false", proto_ver=4, client_port=port1, clean_start=True, allow_zero="false", expect_fail=True)
  61. do_test(per_listener="false", proto_ver=4, client_port=port1, clean_start=False, allow_zero="true", expect_fail=True)
  62. do_test(per_listener="false", proto_ver=4, client_port=port1, clean_start=False, allow_zero="false", expect_fail=True)
  63. do_test(per_listener="true", proto_ver=4, client_port=port1, clean_start=True, allow_zero="true", expect_fail=False)
  64. do_test(per_listener="true", proto_ver=4, client_port=port1, clean_start=True, allow_zero="false", expect_fail=True)
  65. do_test(per_listener="true", proto_ver=4, client_port=port1, clean_start=False, allow_zero="true", expect_fail=True)
  66. do_test(per_listener="true", proto_ver=4, client_port=port1, clean_start=False, allow_zero="false", expect_fail=True)
  67. do_test(per_listener="false", proto_ver=4, client_port=port2, clean_start=True, allow_zero="true", expect_fail=False)
  68. do_test(per_listener="false", proto_ver=4, client_port=port2, clean_start=True, allow_zero="false", expect_fail=True)
  69. do_test(per_listener="false", proto_ver=4, client_port=port2, clean_start=False, allow_zero="true", expect_fail=True)
  70. do_test(per_listener="false", proto_ver=4, client_port=port2, clean_start=False, allow_zero="false", expect_fail=True)
  71. do_test(per_listener="true", proto_ver=4, client_port=port2, clean_start=True, allow_zero="true", expect_fail=False)
  72. do_test(per_listener="true", proto_ver=4, client_port=port2, clean_start=True, allow_zero="false", expect_fail=True)
  73. do_test(per_listener="true", proto_ver=4, client_port=port2, clean_start=False, allow_zero="true", expect_fail=True)
  74. do_test(per_listener="true", proto_ver=4, client_port=port2, clean_start=False, allow_zero="false", expect_fail=True)
  75. do_test(per_listener="false", proto_ver=4, client_port=port1, clean_start=True, allow_zero="", expect_fail=False)
  76. do_test(per_listener="false", proto_ver=4, client_port=port1, clean_start=False, allow_zero="", expect_fail=True)
  77. do_test(per_listener="true", proto_ver=4, client_port=port1, clean_start=True, allow_zero="", expect_fail=False)
  78. do_test(per_listener="true", proto_ver=4, client_port=port1, clean_start=False, allow_zero="", expect_fail=True)
  79. do_test(per_listener="false", proto_ver=4, client_port=port2, clean_start=True, allow_zero="", expect_fail=False)
  80. do_test(per_listener="false", proto_ver=4, client_port=port2, clean_start=False, allow_zero="", expect_fail=True)
  81. do_test(per_listener="true", proto_ver=4, client_port=port2, clean_start=True, allow_zero="", expect_fail=False)
  82. do_test(per_listener="true", proto_ver=4, client_port=port2, clean_start=False, allow_zero="", expect_fail=True)
  83. if test_v5 == True:
  84. do_test(per_listener="false", proto_ver=5, client_port=port1, clean_start=True, allow_zero="true", expect_fail=False)
  85. do_test(per_listener="false", proto_ver=5, client_port=port1, clean_start=True, allow_zero="false", expect_fail=True)
  86. do_test(per_listener="false", proto_ver=5, client_port=port1, clean_start=False, allow_zero="true", expect_fail=False)
  87. do_test(per_listener="false", proto_ver=5, client_port=port1, clean_start=False, allow_zero="false", expect_fail=True)
  88. do_test(per_listener="true", proto_ver=5, client_port=port1, clean_start=True, allow_zero="true", expect_fail=False)
  89. do_test(per_listener="true", proto_ver=5, client_port=port1, clean_start=True, allow_zero="false", expect_fail=True)
  90. do_test(per_listener="true", proto_ver=5, client_port=port1, clean_start=False, allow_zero="true", expect_fail=False)
  91. do_test(per_listener="true", proto_ver=5, client_port=port1, clean_start=False, allow_zero="false", expect_fail=True)
  92. do_test(per_listener="false", proto_ver=5, client_port=port2, clean_start=True, allow_zero="true", expect_fail=False)
  93. do_test(per_listener="false", proto_ver=5, client_port=port2, clean_start=True, allow_zero="false", expect_fail=True)
  94. do_test(per_listener="false", proto_ver=5, client_port=port2, clean_start=False, allow_zero="true", expect_fail=False)
  95. do_test(per_listener="false", proto_ver=5, client_port=port2, clean_start=False, allow_zero="false", expect_fail=True)
  96. do_test(per_listener="true", proto_ver=5, client_port=port2, clean_start=True, allow_zero="true", expect_fail=False)
  97. do_test(per_listener="true", proto_ver=5, client_port=port2, clean_start=True, allow_zero="false", expect_fail=True)
  98. do_test(per_listener="true", proto_ver=5, client_port=port2, clean_start=False, allow_zero="true", expect_fail=False)
  99. do_test(per_listener="true", proto_ver=5, client_port=port2, clean_start=False, allow_zero="false", expect_fail=True)
  100. do_test(per_listener="false", proto_ver=5, client_port=port1, clean_start=True, allow_zero="", expect_fail=False)
  101. do_test(per_listener="false", proto_ver=5, client_port=port1, clean_start=False, allow_zero="", expect_fail=False)
  102. do_test(per_listener="true", proto_ver=5, client_port=port1, clean_start=True, allow_zero="", expect_fail=False)
  103. do_test(per_listener="true", proto_ver=5, client_port=port1, clean_start=False, allow_zero="", expect_fail=False)
  104. do_test(per_listener="false", proto_ver=5, client_port=port2, clean_start=True, allow_zero="", expect_fail=False)
  105. do_test(per_listener="false", proto_ver=5, client_port=port2, clean_start=False, allow_zero="", expect_fail=False)
  106. do_test(per_listener="true", proto_ver=5, client_port=port2, clean_start=True, allow_zero="", expect_fail=False)
  107. do_test(per_listener="true", proto_ver=5, client_port=port2, clean_start=False, allow_zero="", expect_fail=False)
  108. exit(0)