example_test.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # This example code is in the Public Domain (or CC0 licensed, at your option.)
  2. # Unless required by applicable law or agreed to in writing, this
  3. # software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  4. # CONDITIONS OF ANY KIND, either express or implied.
  5. # -*- coding: utf-8 -*-
  6. from __future__ import print_function, unicode_literals
  7. import os
  8. import re
  9. import socket
  10. import sys
  11. import ttfw_idf
  12. from common_test_methods import get_env_config_variable, get_my_interface_by_dest_ip
  13. # ----------- Config ----------
  14. PORT = 3333
  15. # -------------------------------
  16. def tcp_client(address, payload):
  17. for res in socket.getaddrinfo(address, PORT, socket.AF_UNSPEC,
  18. socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
  19. family_addr, socktype, proto, canonname, addr = res
  20. try:
  21. sock = socket.socket(family_addr, socket.SOCK_STREAM)
  22. sock.settimeout(60.0)
  23. except socket.error as msg:
  24. print('Could not create socket: ' + str(msg[0]) + ': ' + msg[1])
  25. raise
  26. try:
  27. sock.connect(addr)
  28. except socket.error as msg:
  29. print('Could not open socket: ', msg)
  30. sock.close()
  31. raise
  32. sock.sendall(payload.encode())
  33. data = sock.recv(1024)
  34. if not data:
  35. return
  36. print('Reply : ' + data.decode())
  37. sock.close()
  38. return data.decode()
  39. @ttfw_idf.idf_example_test(env_tag='wifi_router')
  40. def test_examples_protocol_socket_tcpserver(env, extra_data):
  41. MESSAGE = 'Data to ESP'
  42. """
  43. steps:
  44. 1. join AP
  45. 2. have the board connect to the server
  46. 3. send and receive data
  47. """
  48. dut1 = env.get_dut('tcp_client', 'examples/protocols/sockets/tcp_server', dut_class=ttfw_idf.ESP32DUT)
  49. # check and log bin size
  50. binary_file = os.path.join(dut1.app.binary_path, 'tcp_server.bin')
  51. bin_size = os.path.getsize(binary_file)
  52. ttfw_idf.log_performance('tcp_server_bin_size', '{}KB'.format(bin_size // 1024))
  53. # start test
  54. dut1.start_app()
  55. if dut1.app.get_sdkconfig_config_value('CONFIG_EXAMPLE_WIFI_SSID_PWD_FROM_STDIN'):
  56. dut1.expect('Please input ssid password:')
  57. env_name = 'wifi_router'
  58. ap_ssid = get_env_config_variable(env_name, 'ap_ssid')
  59. ap_password = get_env_config_variable(env_name, 'ap_password')
  60. dut1.write(f'{ap_ssid} {ap_password}')
  61. ipv4 = dut1.expect(re.compile(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]'), timeout=30)[0]
  62. ipv6_r = r':'.join((r'[0-9a-fA-F]{4}',) * 8) # expect all 8 octets from IPv6 (assumes it's printed in the long form)
  63. ipv6 = dut1.expect(re.compile(r' IPv6 address: ({})'.format(ipv6_r)), timeout=30)[0]
  64. print('Connected with IPv4={} and IPv6={}'.format(ipv4, ipv6))
  65. interface = get_my_interface_by_dest_ip(ipv4)
  66. # test IPv4
  67. received = tcp_client(ipv4, MESSAGE)
  68. if not received == MESSAGE:
  69. raise
  70. dut1.expect(MESSAGE)
  71. # test IPv6
  72. received = tcp_client('{}%{}'.format(ipv6, interface), MESSAGE)
  73. if not received == MESSAGE:
  74. raise
  75. dut1.expect(MESSAGE)
  76. if __name__ == '__main__':
  77. if sys.argv[2:]: # if two arguments provided:
  78. # Usage: example_test.py <server_address> <message_to_send_to_server>
  79. tcp_client(sys.argv[1], sys.argv[2])
  80. else: # otherwise run standard example test as in the CI
  81. test_examples_protocol_socket_tcpserver()