asio_chat_client_test.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import re
  2. import os
  3. import socket
  4. from threading import Thread
  5. import time
  6. import ttfw_idf
  7. global g_client_response
  8. global g_msg_to_client
  9. g_client_response = b""
  10. g_msg_to_client = b" 3XYZ"
  11. def get_my_ip():
  12. s1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  13. s1.connect(("8.8.8.8", 80))
  14. my_ip = s1.getsockname()[0]
  15. s1.close()
  16. return my_ip
  17. def chat_server_sketch(my_ip):
  18. global g_client_response
  19. print("Starting the server on {}".format(my_ip))
  20. port = 2222
  21. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  22. s.settimeout(600)
  23. s.bind((my_ip, port))
  24. s.listen(1)
  25. q,addr = s.accept()
  26. print("connection accepted")
  27. q.settimeout(30)
  28. q.send(g_msg_to_client)
  29. data = q.recv(1024)
  30. # check if received initial empty message
  31. if (len(data) > 4):
  32. g_client_response = data
  33. else:
  34. g_client_response = q.recv(1024)
  35. print("received from client {}".format(g_client_response))
  36. s.close()
  37. print("server closed")
  38. @ttfw_idf.idf_example_test(env_tag="Example_WIFI")
  39. def test_examples_protocol_asio_chat_client(env, extra_data):
  40. """
  41. steps: |
  42. 1. Test to start simple tcp server
  43. 2. `dut1` joins AP
  44. 3. Test injects server IP to `dut1`via stdin
  45. 4. Test evaluates `dut1` receives a message server placed
  46. 5. Test injects a message to `dut1` to be sent as chat_client message
  47. 6. Test evaluates received test message in host server
  48. """
  49. global g_client_response
  50. global g_msg_to_client
  51. test_msg = "ABC"
  52. dut1 = env.get_dut("chat_client", "examples/protocols/asio/chat_client")
  53. # check and log bin size
  54. binary_file = os.path.join(dut1.app.binary_path, "asio_chat_client.bin")
  55. bin_size = os.path.getsize(binary_file)
  56. ttfw_idf.log_performance("asio_chat_client_size", "{}KB".format(bin_size // 1024))
  57. ttfw_idf.check_performance("asio_chat_client_size", bin_size // 1024)
  58. # 1. start a tcp server on the host
  59. host_ip = get_my_ip()
  60. thread1 = Thread(target=chat_server_sketch, args=(host_ip,))
  61. thread1.start()
  62. # 2. start the dut test and wait till client gets IP address
  63. dut1.start_app()
  64. dut1.expect(re.compile(r" IPv4 address: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)"), timeout=30)
  65. # 3. send host's IP to the client i.e. the `dut1`
  66. dut1.write(host_ip)
  67. # 4. client `dut1` should receive a message
  68. dut1.expect(g_msg_to_client[4:].decode()) # Strip out the front 4 bytes of message len (see chat_message protocol)
  69. # 5. write test message from `dut1` chat_client to the server
  70. dut1.write(test_msg)
  71. while len(g_client_response) == 0:
  72. time.sleep(1)
  73. g_client_response = g_client_response.decode()
  74. print(g_client_response)
  75. # 6. evaluate host_server received this message
  76. if (g_client_response[4:7] == test_msg):
  77. print("PASS: Received correct message")
  78. pass
  79. else:
  80. print("Failure!")
  81. raise ValueError('Wrong data received from asi tcp server: {} (expected:{})'.format(g_client_response[4:7], test_msg))
  82. thread1.join()
  83. if __name__ == '__main__':
  84. test_examples_protocol_asio_chat_client()