mqtt_tcp_example_test.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import re
  2. import os
  3. import sys
  4. import socket
  5. from threading import Thread
  6. import struct
  7. import time
  8. from tiny_test_fw import DUT
  9. import ttfw_idf
  10. msgid = -1
  11. def get_my_ip():
  12. s1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  13. s1.connect(("8.8.8.8", 80))
  14. my_ip = s1.getsockname()[0]
  15. s1.close()
  16. return my_ip
  17. def mqqt_server_sketch(my_ip, port):
  18. global msgid
  19. print("Starting the server on {}".format(my_ip))
  20. s = None
  21. try:
  22. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  23. s.settimeout(60)
  24. s.bind((my_ip, port))
  25. s.listen(1)
  26. q,addr = s.accept()
  27. q.settimeout(30)
  28. print("connection accepted")
  29. except Exception:
  30. print("Local server on {}:{} listening/accepting failure: {}"
  31. "Possibly check permissions or firewall settings"
  32. "to accept connections on this address".format(my_ip, port, sys.exc_info()[0]))
  33. raise
  34. data = q.recv(1024)
  35. # check if received initial empty message
  36. print("received from client {}".format(data))
  37. data = bytearray([0x20, 0x02, 0x00, 0x00])
  38. q.send(data)
  39. # try to receive qos1
  40. data = q.recv(1024)
  41. msgid = struct.unpack(">H", data[15:17])[0]
  42. print("received from client {}, msgid: {}".format(data, msgid))
  43. data = bytearray([0x40, 0x02, data[15], data[16]])
  44. q.send(data)
  45. time.sleep(5)
  46. s.close()
  47. print("server closed")
  48. @ttfw_idf.idf_example_test(env_tag="Example_WIFI")
  49. def test_examples_protocol_mqtt_qos1(env, extra_data):
  50. global msgid
  51. """
  52. steps: (QoS1: Happy flow)
  53. 1. start the broker broker (with correctly sending ACK)
  54. 2. DUT client connects to a broker and publishes qos1 message
  55. 3. Test evaluates that qos1 message is queued and removed from queued after ACK received
  56. 4. Test the broker received the same message id evaluated in step 3
  57. """
  58. dut1 = env.get_dut("mqtt_tcp", "examples/protocols/mqtt/tcp", dut_class=ttfw_idf.ESP32DUT)
  59. # check and log bin size
  60. binary_file = os.path.join(dut1.app.binary_path, "mqtt_tcp.bin")
  61. bin_size = os.path.getsize(binary_file)
  62. ttfw_idf.log_performance("mqtt_tcp_bin_size", "{}KB".format(bin_size // 1024))
  63. ttfw_idf.check_performance("mqtt_tcp_size", bin_size // 1024, dut1.TARGET)
  64. # 1. start mqtt broker sketch
  65. host_ip = get_my_ip()
  66. thread1 = Thread(target=mqqt_server_sketch, args=(host_ip,1883))
  67. thread1.start()
  68. # 2. start the dut test and wait till client gets IP address
  69. dut1.start_app()
  70. # waiting for getting the IP address
  71. try:
  72. ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30)
  73. print("Connected to AP with IP: {}".format(ip_address))
  74. except DUT.ExpectTimeout:
  75. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  76. print("writing to device: {}".format("mqtt://" + host_ip + "\n"))
  77. dut1.write("mqtt://" + host_ip + "\n")
  78. thread1.join()
  79. print("Message id received from server: {}".format(msgid))
  80. # 3. check the message id was enqueued and then deleted
  81. msgid_enqueued = dut1.expect(re.compile(r"OUTBOX: ENQUEUE msgid=([0-9]+)"), timeout=30)
  82. msgid_deleted = dut1.expect(re.compile(r"OUTBOX: DELETED msgid=([0-9]+)"), timeout=30)
  83. # 4. check the msgid of received data are the same as that of enqueued and deleted from outbox
  84. if (msgid_enqueued[0] == str(msgid) and msgid_deleted[0] == str(msgid)):
  85. print("PASS: Received correct msg id")
  86. else:
  87. print("Failure!")
  88. raise ValueError('Mismatch of msgid: received: {}, enqueued {}, deleted {}'.format(msgid, msgid_enqueued, msgid_deleted))
  89. if __name__ == '__main__':
  90. test_examples_protocol_mqtt_qos1()