example_test.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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
  7. from __future__ import unicode_literals
  8. import os
  9. import sys
  10. import re
  11. import socket
  12. import ttfw_idf
  13. # ----------- Config ----------
  14. PORT = 3333
  15. INTERFACE = 'eth0'
  16. # -------------------------------
  17. def tcp_client(address, payload):
  18. for res in socket.getaddrinfo(address, PORT, socket.AF_UNSPEC,
  19. socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
  20. family_addr, socktype, proto, canonname, addr = res
  21. try:
  22. sock = socket.socket(family_addr, socket.SOCK_STREAM)
  23. sock.settimeout(60.0)
  24. except socket.error as msg:
  25. print('Could not create socket: ' + str(msg[0]) + ': ' + msg[1])
  26. raise
  27. try:
  28. sock.connect(addr)
  29. except socket.error as msg:
  30. print('Could not open socket: ', msg)
  31. sock.close()
  32. raise
  33. sock.sendall(payload.encode())
  34. data = sock.recv(1024)
  35. if not data:
  36. return
  37. print('Reply : ' + data.decode())
  38. sock.close()
  39. return data.decode()
  40. @ttfw_idf.idf_example_test(env_tag="Example_WIFI")
  41. def test_examples_protocol_socket_tcpserver(env, extra_data):
  42. MESSAGE = "Data to ESP"
  43. """
  44. steps:
  45. 1. join AP
  46. 2. have the board connect to the server
  47. 3. send and receive data
  48. """
  49. dut1 = env.get_dut("tcp_client", "examples/protocols/sockets/tcp_server", dut_class=ttfw_idf.ESP32DUT)
  50. # check and log bin size
  51. binary_file = os.path.join(dut1.app.binary_path, "tcp_server.bin")
  52. bin_size = os.path.getsize(binary_file)
  53. ttfw_idf.log_performance("tcp_server_bin_size", "{}KB".format(bin_size // 1024))
  54. ttfw_idf.check_performance("tcp_server_bin_size", bin_size // 1024, dut1.TARGET)
  55. # start test
  56. dut1.start_app()
  57. ipv4 = dut1.expect(re.compile(r" IPv4 address: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)"), timeout=30)[0]
  58. 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)
  59. ipv6 = dut1.expect(re.compile(r' IPv6 address: ({})'.format(ipv6_r)), timeout=30)[0]
  60. print("Connected with IPv4={} and IPv6={}".format(ipv4, ipv6))
  61. # test IPv4
  62. received = tcp_client(ipv4, MESSAGE)
  63. if not received == MESSAGE:
  64. raise
  65. dut1.expect(MESSAGE)
  66. # test IPv6
  67. received = tcp_client("{}%{}".format(ipv6, INTERFACE), MESSAGE)
  68. if not received == MESSAGE:
  69. raise
  70. dut1.expect(MESSAGE)
  71. if __name__ == '__main__':
  72. if sys.argv[2:]: # if two arguments provided:
  73. # Usage: example_test.py <server_address> <message_to_send_to_server>
  74. tcp_client(sys.argv[1], sys.argv[2])
  75. else: # otherwise run standard example test as in the CI
  76. test_examples_protocol_socket_tcpserver()