asio_chat_client_test.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import re
  2. import os
  3. import sys
  4. import socket
  5. from threading import Thread
  6. import time
  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. global g_client_response
  19. global g_msg_to_client
  20. g_client_response = b""
  21. g_msg_to_client = b" 3XYZ"
  22. def get_my_ip():
  23. s1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  24. s1.connect(("8.8.8.8", 80))
  25. my_ip = s1.getsockname()[0]
  26. s1.close()
  27. return my_ip
  28. def chat_server_sketch(my_ip):
  29. global g_client_response
  30. print("Starting the server on {}".format(my_ip))
  31. port = 2222
  32. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  33. s.settimeout(600)
  34. s.bind((my_ip, port))
  35. s.listen(1)
  36. q,addr = s.accept()
  37. print("connection accepted")
  38. q.settimeout(30)
  39. q.send(g_msg_to_client)
  40. data = q.recv(1024)
  41. # check if received initial empty message
  42. if (len(data) > 4):
  43. g_client_response = data
  44. else:
  45. g_client_response = q.recv(1024)
  46. print("received from client {}".format(g_client_response))
  47. s.close()
  48. print("server closed")
  49. @IDF.idf_example_test(env_tag="Example_WIFI")
  50. def test_examples_protocol_asio_chat_client(env, extra_data):
  51. """
  52. steps: |
  53. 1. Test to start simple tcp server
  54. 2. `dut1` joins AP
  55. 3. Test injects server IP to `dut1`via stdin
  56. 4. Test evaluates `dut1` receives a message server placed
  57. 5. Test injects a message to `dut1` to be sent as chat_client message
  58. 6. Test evaluates received test message in host server
  59. """
  60. global g_client_response
  61. global g_msg_to_client
  62. test_msg = "ABC"
  63. dut1 = env.get_dut("chat_client", "examples/protocols/asio/chat_client")
  64. # check and log bin size
  65. binary_file = os.path.join(dut1.app.binary_path, "asio_chat_client.bin")
  66. bin_size = os.path.getsize(binary_file)
  67. IDF.log_performance("asio_chat_client_size", "{}KB".format(bin_size // 1024))
  68. IDF.check_performance("asio_chat_client_size", bin_size // 1024)
  69. # 1. start a tcp server on the host
  70. host_ip = get_my_ip()
  71. thread1 = Thread(target=chat_server_sketch, args=(host_ip,))
  72. thread1.start()
  73. # 2. start the dut test and wait till client gets IP address
  74. dut1.start_app()
  75. dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30)
  76. # 3. send host's IP to the client i.e. the `dut1`
  77. dut1.write(host_ip)
  78. # 4. client `dut1` should receive a message
  79. dut1.expect(g_msg_to_client[4:].decode()) # Strip out the front 4 bytes of message len (see chat_message protocol)
  80. # 5. write test message from `dut1` chat_client to the server
  81. dut1.write(test_msg)
  82. while len(g_client_response) == 0:
  83. time.sleep(1)
  84. g_client_response = g_client_response.decode()
  85. print(g_client_response)
  86. # 6. evaluate host_server received this message
  87. if (g_client_response[4:7] == test_msg):
  88. print("PASS: Received correct message")
  89. pass
  90. else:
  91. print("Failure!")
  92. raise ValueError('Wrong data received from asi tcp server: {} (expected:{})'.format(g_client_response[4:7], test_msg))
  93. thread1.join()
  94. if __name__ == '__main__':
  95. test_examples_protocol_asio_chat_client()