01-connect-allow-anonymous.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env python3
  2. # Test whether an anonymous connection is correctly denied.
  3. from mosq_test_helper import *
  4. def write_config1(filename, port):
  5. with open(filename, 'w') as f:
  6. f.write("max_connections 10\n") # So the file isn't completely empty
  7. def write_config2(filename, port):
  8. with open(filename, 'w') as f:
  9. f.write("port %d\n" % (port))
  10. def write_config3(filename, port):
  11. with open(filename, 'w') as f:
  12. f.write("listener %d\n" % (port))
  13. def write_config4(filename, port):
  14. with open(filename, 'w') as f:
  15. f.write("port %d\n" % (port))
  16. f.write("allow_anonymous true\n")
  17. def write_config5(filename, port):
  18. with open(filename, 'w') as f:
  19. f.write("listener %d\n" % (port))
  20. f.write("allow_anonymous true\n")
  21. def do_test(use_conf, write_config, expect_success):
  22. port = mosq_test.get_port()
  23. if write_config is not None:
  24. conf_file = os.path.basename(__file__).replace('.py', '.conf')
  25. write_config(conf_file, port)
  26. broker = mosq_test.start_broker(filename=os.path.basename(__file__), use_conf=use_conf, port=port)
  27. try:
  28. for proto_ver in [4, 5]:
  29. rc = 1
  30. keepalive = 10
  31. connect_packet = mosq_test.gen_connect("connect-anon-test-%d" % (proto_ver), keepalive=keepalive, proto_ver=proto_ver)
  32. if proto_ver == 5:
  33. if expect_success == True:
  34. connack_packet = mosq_test.gen_connack(rc=0, proto_ver=proto_ver)
  35. else:
  36. connack_packet = mosq_test.gen_connack(rc=mqtt5_rc.MQTT_RC_NOT_AUTHORIZED, proto_ver=proto_ver, properties=None)
  37. else:
  38. if expect_success == True:
  39. connack_packet = mosq_test.gen_connack(rc=0, proto_ver=proto_ver)
  40. else:
  41. connack_packet = mosq_test.gen_connack(rc=5, proto_ver=proto_ver)
  42. sock = mosq_test.do_client_connect(connect_packet, connack_packet, port=port)
  43. sock.close()
  44. rc = 0
  45. except mosq_test.TestError:
  46. pass
  47. finally:
  48. if write_config is not None:
  49. os.remove(conf_file)
  50. broker.terminate()
  51. broker.wait()
  52. (stdo, stde) = broker.communicate()
  53. if rc:
  54. print(stde.decode('utf-8'))
  55. print("proto_ver=%d" % (proto_ver))
  56. exit(rc)
  57. # No config file - allow_anonymous should be true
  58. do_test(use_conf=False, write_config=None, expect_success=True)
  59. # Config file but no listener - allow_anonymous should be true
  60. # Not possible right now because the test doesn't allow us to use a config file and -p at the same time.
  61. #do_test(use_conf=True, write_config=write_config1, expect_success=True)
  62. # Config file with "port" - allow_anonymous should be false
  63. do_test(use_conf=True, write_config=write_config2, expect_success=False)
  64. # Config file with "listener" - allow_anonymous should be false
  65. do_test(use_conf=True, write_config=write_config3, expect_success=False)
  66. # Config file with "port" - allow_anonymous explicitly true
  67. do_test(use_conf=True, write_config=write_config4, expect_success=True)
  68. # Config file with "listener" - allow_anonymous explicitly true
  69. do_test(use_conf=True, write_config=write_config5, expect_success=True)
  70. exit(0)