mqtt_wss_example_test.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. from __future__ import unicode_literals
  2. from __future__ import unicode_literals
  3. from builtins import str
  4. import re
  5. import os
  6. import sys
  7. import ssl
  8. import paho.mqtt.client as mqtt
  9. from threading import Thread, Event
  10. from tiny_test_fw import DUT
  11. import ttfw_idf
  12. event_client_connected = Event()
  13. event_stop_client = Event()
  14. event_client_received_correct = Event()
  15. message_log = ""
  16. # The callback for when the client receives a CONNACK response from the server.
  17. def on_connect(client, userdata, flags, rc):
  18. print("Connected with result code " + str(rc))
  19. event_client_connected.set()
  20. client.subscribe("/topic/qos0")
  21. def mqtt_client_task(client):
  22. while not event_stop_client.is_set():
  23. client.loop()
  24. # The callback for when a PUBLISH message is received from the server.
  25. def on_message(client, userdata, msg):
  26. global message_log
  27. payload = msg.payload.decode()
  28. if not event_client_received_correct.is_set() and payload == "data":
  29. client.publish("/topic/qos0", "data_to_esp32")
  30. if msg.topic == "/topic/qos0" and payload == "data":
  31. event_client_received_correct.set()
  32. message_log += "Received data:" + msg.topic + " " + payload + "\n"
  33. @ttfw_idf.idf_example_test(env_tag="Example_WIFI")
  34. def test_examples_protocol_mqtt_wss(env, extra_data):
  35. broker_url = ""
  36. broker_port = 0
  37. """
  38. steps: |
  39. 1. join AP and connects to wss broker
  40. 2. Test connects a client to the same broker
  41. 3. Test evaluates it received correct qos0 message
  42. 4. Test ESP32 client received correct qos0 message
  43. """
  44. dut1 = env.get_dut("mqtt_websocket_secure", "examples/protocols/mqtt/wss", dut_class=ttfw_idf.ESP32DUT)
  45. # check and log bin size
  46. binary_file = os.path.join(dut1.app.binary_path, "mqtt_websocket_secure.bin")
  47. bin_size = os.path.getsize(binary_file)
  48. ttfw_idf.log_performance("mqtt_websocket_secure_bin_size", "{}KB".format(bin_size // 1024))
  49. ttfw_idf.check_performance("mqtt_websocket_secure_size", bin_size // 1024, dut1.TARGET)
  50. # Look for host:port in sdkconfig
  51. try:
  52. value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut1.app.get_sdkconfig()["CONFIG_BROKER_URI"])
  53. broker_url = value.group(1)
  54. broker_port = int(value.group(2))
  55. except Exception:
  56. print('ENV_TEST_FAILURE: Cannot find broker url in sdkconfig')
  57. raise
  58. client = None
  59. # 1. Test connects to a broker
  60. try:
  61. client = mqtt.Client(transport="websockets")
  62. client.on_connect = on_connect
  63. client.on_message = on_message
  64. client.tls_set(None,
  65. None,
  66. None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None)
  67. print("Connecting...")
  68. client.connect(broker_url, broker_port, 60)
  69. except Exception:
  70. print("ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:".format(broker_url, sys.exc_info()[0]))
  71. raise
  72. # Starting a py-client in a separate thread
  73. thread1 = Thread(target=mqtt_client_task, args=(client,))
  74. thread1.start()
  75. try:
  76. print("Connecting py-client to broker {}:{}...".format(broker_url, broker_port))
  77. if not event_client_connected.wait(timeout=30):
  78. raise ValueError("ENV_TEST_FAILURE: Test script cannot connect to broker: {}".format(broker_url))
  79. dut1.start_app()
  80. try:
  81. ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30)
  82. print("Connected to AP with IP: {}".format(ip_address))
  83. except DUT.ExpectTimeout:
  84. print('ENV_TEST_FAILURE: Cannot connect to AP')
  85. raise
  86. print("Checking py-client received msg published from esp...")
  87. if not event_client_received_correct.wait(timeout=30):
  88. raise ValueError('Wrong data received, msg log: {}'.format(message_log))
  89. print("Checking esp-client received msg published from py-client...")
  90. dut1.expect(re.compile(r"DATA=data_to_esp32"), timeout=30)
  91. finally:
  92. event_stop_client.set()
  93. thread1.join()
  94. if __name__ == '__main__':
  95. test_examples_protocol_mqtt_wss()