mqtt_ssl_example_test.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import re
  2. import os
  3. import sys
  4. import time
  5. import ssl
  6. import paho.mqtt.client as mqtt
  7. try:
  8. import IDF
  9. except ImportError:
  10. # this is a test case write with tiny-test-fw.
  11. # to run test cases outside tiny-test-fw,
  12. # we need to set environment variable `TEST_FW_PATH`,
  13. # then get and insert `TEST_FW_PATH` to sys path before import FW module
  14. test_fw_path = os.getenv("TEST_FW_PATH")
  15. if test_fw_path and test_fw_path not in sys.path:
  16. sys.path.insert(0, test_fw_path)
  17. import IDF
  18. import DUT
  19. g_recv_data = ""
  20. g_recv_topic = ""
  21. g_broker_connected = 0
  22. # The callback for when the client receives a CONNACK response from the server.
  23. def on_connect(client, userdata, flags, rc):
  24. global g_broker_connected
  25. print("Connected with result code " + str(rc))
  26. g_broker_connected = 1
  27. client.subscribe("/topic/qos0")
  28. # The callback for when a PUBLISH message is received from the server.
  29. def on_message(client, userdata, msg):
  30. global g_recv_topic
  31. global g_recv_data
  32. payload = msg.payload.decode()
  33. if g_recv_data == "" and payload == "data":
  34. client.publish("/topic/qos0", "data_to_esp32")
  35. g_recv_topic = msg.topic
  36. g_recv_data = payload
  37. print(msg.topic + " " + str(payload))
  38. @IDF.idf_example_test(env_tag="Example_WIFI")
  39. def test_examples_protocol_mqtt_ssl(env, extra_data):
  40. global g_recv_topic
  41. global g_recv_data
  42. global g_broker_connected
  43. broker_url = "iot.eclipse.org"
  44. """
  45. steps: |
  46. 1. join AP and connects to ssl broker
  47. 2. Test connects a client to the same broker
  48. 3. Test evaluates python client received correct qos0 message
  49. 4. Test ESP32 client received correct qos0 message
  50. """
  51. dut1 = env.get_dut("mqtt_ssl", "examples/protocols/mqtt/ssl")
  52. # check and log bin size
  53. binary_file = os.path.join(dut1.app.binary_path, "mqtt_ssl.bin")
  54. bin_size = os.path.getsize(binary_file)
  55. IDF.log_performance("mqtt_ssl_bin_size", "{}KB".format(bin_size // 1024))
  56. IDF.check_performance("mqtt_ssl_size", bin_size // 1024)
  57. # 1. start test (and check the environment is healthy)
  58. dut1.start_app()
  59. client = None
  60. # 2. Test connects to a broker
  61. try:
  62. ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30)
  63. print("Connected to AP with IP: {}".format(ip_address))
  64. client = mqtt.Client()
  65. client.on_connect = on_connect
  66. client.on_message = on_message
  67. client.tls_set(None,
  68. None,
  69. None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1, ciphers=None)
  70. client.tls_insecure_set(True)
  71. print("Connecting...")
  72. client.connect(broker_url, 8883, 60)
  73. print("...done")
  74. except DUT.ExpectTimeout:
  75. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  76. except Exception:
  77. print("ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:".format(broker_url, sys.exc_info()[0]))
  78. raise
  79. print("Start Looping...")
  80. start = time.time()
  81. while (time.time() - start) <= 20:
  82. client.loop()
  83. print("...done")
  84. if g_broker_connected == 0:
  85. raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_url))
  86. # 3. check the message received back from the server
  87. if g_recv_topic == "/topic/qos0" and g_recv_data == "data":
  88. print("PASS: Received correct message")
  89. else:
  90. print("Failure!")
  91. raise ValueError('Wrong data received topic: {}, data:{}'.format(g_recv_topic, g_recv_data))
  92. # 4. check that the esp32 client received data sent by this python client
  93. dut1.expect(re.compile(r"DATA=data_to_esp32"), timeout=30)
  94. if __name__ == '__main__':
  95. test_examples_protocol_mqtt_ssl()