mqtt_wss_example_test.py 3.9 KB

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