asio_chat_client_test.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import os
  2. import re
  3. import socket
  4. import time
  5. from threading import Thread
  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', dut_class=ttfw_idf.ESP32DUT)
  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. # 1. start a tcp server on the host
  58. host_ip = get_my_ip()
  59. thread1 = Thread(target=chat_server_sketch, args=(host_ip,))
  60. thread1.start()
  61. # 2. start the dut test and wait till client gets IP address
  62. dut1.start_app()
  63. dut1.expect(re.compile(r' IPv4 address: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)'), timeout=30)
  64. # 3. send host's IP to the client i.e. the `dut1`
  65. dut1.write(host_ip)
  66. # 4. client `dut1` should receive a message
  67. dut1.expect(g_msg_to_client[4:].decode()) # Strip out the front 4 bytes of message len (see chat_message protocol)
  68. # 5. write test message from `dut1` chat_client to the server
  69. dut1.write(test_msg)
  70. while len(g_client_response) == 0:
  71. time.sleep(1)
  72. g_client_response = g_client_response.decode()
  73. print(g_client_response)
  74. # 6. evaluate host_server received this message
  75. if (g_client_response[4:7] == test_msg):
  76. print('PASS: Received correct message')
  77. pass
  78. else:
  79. print('Failure!')
  80. raise ValueError('Wrong data received from asi tcp server: {} (expected:{})'.format(g_client_response[4:7], test_msg))
  81. thread1.join()
  82. if __name__ == '__main__':
  83. test_examples_protocol_asio_chat_client()