asio_chat_server_test.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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: ([^,]+),"))
  34. # 3. create tcp client and connect to server
  35. cli = socket(AF_INET,SOCK_STREAM)
  36. cli.connect((data[0],80))
  37. cli.send(test_msg)
  38. data = cli.recv(1024)
  39. # 4. check the message received back from the server
  40. if (data == test_msg):
  41. print("PASS: Received correct message {}".format(data))
  42. pass
  43. else:
  44. print("Failure!")
  45. raise ValueError('Wrong data received from asi tcp server: {} (expoected:{})'.format(data, test_msg))
  46. if __name__ == '__main__':
  47. test_examples_protocol_asio_chat_server()