asio_chat_server_test.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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_chat_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. """
  23. test_msg=" 4ABC\n"
  24. dut1 = env.get_dut("chat_server", "examples/protocols/asio/chat_server")
  25. # check and log bin size
  26. binary_file = os.path.join(dut1.app.binary_path, "asio_chatserver.bin")
  27. bin_size = os.path.getsize(binary_file)
  28. IDF.log_performance("asio_chatserver_bin_size", "{}KB".format(bin_size//1024))
  29. IDF.check_performance("asio_chatserver_size", bin_size//1024)
  30. # 1. start test
  31. dut1.start_app()
  32. # 2. get the server IP address
  33. data = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30)
  34. # 3. create tcp client and connect to server
  35. cli = socket(AF_INET,SOCK_STREAM)
  36. cli.settimeout(30)
  37. cli.connect((data[0],80))
  38. cli.send(test_msg)
  39. data = cli.recv(1024)
  40. # 4. check the message received back from the server
  41. if (data == test_msg):
  42. print("PASS: Received correct message {}".format(data))
  43. pass
  44. else:
  45. print("Failure!")
  46. raise ValueError('Wrong data received from asi tcp server: {} (expoected:{})'.format(data, test_msg))
  47. if __name__ == '__main__':
  48. test_examples_protocol_asio_chat_server()