asio_chat_client_test.py 3.3 KB

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