asio_udp_server_test.py 2.1 KB

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