example_test.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 udp_client(address, payload):
  17. for res in socket.getaddrinfo(address, PORT, socket.AF_UNSPEC,
  18. socket.SOCK_DGRAM, 0, socket.AI_PASSIVE):
  19. family_addr, socktype, proto, canonname, addr = res
  20. try:
  21. sock = socket.socket(family_addr, socket.SOCK_DGRAM)
  22. sock.settimeout(20.0)
  23. except socket.error as msg:
  24. print('Could not create socket')
  25. print(os.strerror(msg.errno))
  26. raise
  27. try:
  28. sock.sendto(payload.encode(), addr)
  29. reply, addr = sock.recvfrom(128)
  30. if not reply:
  31. return
  32. print('Reply[' + addr[0] + ':' + str(addr[1]) + '] - ' + str(reply))
  33. except socket.timeout:
  34. print('Socket operation timeout')
  35. return str(None)
  36. except socket.error as msg:
  37. print('Error while sending or receiving data from the socket')
  38. print(os.strerror(msg.errno))
  39. sock.close()
  40. raise
  41. return reply.decode()
  42. @ttfw_idf.idf_example_test(env_tag='wifi_router')
  43. def test_examples_protocol_socket_udpserver(env, extra_data):
  44. MESSAGE = 'Data to ESP'
  45. MAX_RETRIES = 3
  46. """
  47. steps:
  48. 1. join AP
  49. 2. have the board connect to the server
  50. 3. send and receive data
  51. """
  52. dut1 = env.get_dut('udp_server', 'examples/protocols/sockets/udp_server', dut_class=ttfw_idf.ESP32DUT)
  53. # check and log bin size
  54. binary_file = os.path.join(dut1.app.binary_path, 'udp_server.bin')
  55. bin_size = os.path.getsize(binary_file)
  56. ttfw_idf.log_performance('udp_server_bin_size', '{}KB'.format(bin_size // 1024))
  57. # start test
  58. dut1.start_app()
  59. if dut1.app.get_sdkconfig_config_value('CONFIG_EXAMPLE_WIFI_SSID_PWD_FROM_STDIN'):
  60. dut1.expect('Please input ssid password:')
  61. env_name = 'wifi_router'
  62. ap_ssid = get_env_config_variable(env_name, 'ap_ssid')
  63. ap_password = get_env_config_variable(env_name, 'ap_password')
  64. dut1.write(f'{ap_ssid} {ap_password}')
  65. ipv4 = dut1.expect(re.compile(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]'), timeout=30)[0]
  66. 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)
  67. ipv6 = dut1.expect(re.compile(r' IPv6 address: ({})'.format(ipv6_r)), timeout=30)[0]
  68. print('Connected with IPv4={} and IPv6={}'.format(ipv4, ipv6))
  69. dut1.expect(re.compile(r'Waiting for data'), timeout=10)
  70. interface = get_my_interface_by_dest_ip(ipv4)
  71. # test IPv4
  72. for _ in range(MAX_RETRIES):
  73. print('Testing UDP on IPv4...')
  74. received = udp_client(ipv4, MESSAGE)
  75. if received == MESSAGE:
  76. print('OK')
  77. break
  78. else:
  79. raise ValueError('IPv4: Did not receive UDP message after {} retries'.format(MAX_RETRIES))
  80. dut1.expect(MESSAGE)
  81. # test IPv6
  82. for _ in range(MAX_RETRIES):
  83. print('Testing UDP on IPv6...')
  84. received = udp_client('{}%{}'.format(ipv6, interface), MESSAGE)
  85. if received == MESSAGE:
  86. print('OK')
  87. break
  88. else:
  89. raise ValueError('IPv6: Did not receive UDP message after {} retries'.format(MAX_RETRIES))
  90. dut1.expect(MESSAGE)
  91. if __name__ == '__main__':
  92. if sys.argv[2:]: # if two arguments provided:
  93. # Usage: example_test.py <server_address> <message_to_send_to_server>
  94. udp_client(sys.argv[1], sys.argv[2])
  95. else: # otherwise run standard example test as in the CI
  96. test_examples_protocol_socket_udpserver()