asio_tcp_server_test.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import re
  2. import os
  3. import sys
  4. from socket import *
  5. # this is a test case write with tiny-test-fw.
  6. # to run test cases outside tiny-test-fw,
  7. # we need to set environment variable `TEST_FW_PATH`,
  8. # then get and insert `TEST_FW_PATH` to sys path before import FW module
  9. test_fw_path = os.getenv("TEST_FW_PATH")
  10. if test_fw_path and test_fw_path not in sys.path:
  11. sys.path.insert(0, test_fw_path)
  12. import TinyFW
  13. import IDF
  14. @IDF.idf_example_test(env_tag="Example_WIFI")
  15. def test_examples_protocol_asio_tcp_server(env, extra_data):
  16. """
  17. steps: |
  18. 1. join AP
  19. 2. Start server
  20. 3. Test connects to server and sends a test message
  21. 4. Test evaluates received test message from server
  22. 5. Test evaluates received test message on server stdout
  23. """
  24. test_msg=b"echo message from client to server"
  25. dut1 = env.get_dut("tcp_echo_server", "examples/protocols/asio/tcp_echo_server")
  26. # check and log bin size
  27. binary_file = os.path.join(dut1.app.binary_path, "asio_tcp_echo_server.bin")
  28. bin_size = os.path.getsize(binary_file)
  29. IDF.log_performance("asio_tcp_echo_server_bin_size", "{}KB".format(bin_size//1024))
  30. IDF.check_performance("asio_tcp_echo_server_size", bin_size//1024)
  31. # 1. start test
  32. dut1.start_app()
  33. # 2. get the server IP address
  34. data = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30)
  35. # 3. create tcp client and connect to server
  36. cli = socket(AF_INET,SOCK_STREAM)
  37. cli.settimeout(30)
  38. cli.connect((data[0],80))
  39. cli.send(test_msg)
  40. data = cli.recv(1024)
  41. # 4. check the message received back from the server
  42. if (data == test_msg):
  43. print("PASS: Received correct message")
  44. pass
  45. else:
  46. print("Failure!")
  47. raise ValueError('Wrong data received from asi tcp server: {} (expoected:{})'.format(data, test_msg))
  48. # 5. check the client message appears also on server terminal
  49. dut1.expect(test_msg.decode())
  50. if __name__ == '__main__':
  51. test_examples_protocol_asio_tcp_server()