asio_chat_client_test.py 3.2 KB

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