asio_chat_server_test.py 1.9 KB

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