pytest_tcp_client.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: Apache-2.0
  3. import logging
  4. import socket
  5. import pytest
  6. from common_test_methods import (get_env_config_variable, get_host_ip4_by_dest_ip, get_host_ip6_by_dest_ip,
  7. get_my_interface_by_dest_ip)
  8. from pytest_embedded import Dut
  9. try:
  10. from run_tcp_server import TcpServer
  11. except ImportError:
  12. import os
  13. import sys
  14. sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'scripts')))
  15. from run_tcp_server import TcpServer
  16. PORT = 3333
  17. @pytest.mark.esp32
  18. @pytest.mark.esp32s2
  19. @pytest.mark.esp32c2
  20. @pytest.mark.esp32c3
  21. @pytest.mark.esp32s3
  22. @pytest.mark.esp32c6
  23. @pytest.mark.wifi_router
  24. def test_examples_tcp_client_ipv4(dut: Dut) -> None:
  25. # Parse IP address of STA
  26. logging.info('Waiting to connect with AP')
  27. if dut.app.sdkconfig.get('EXAMPLE_WIFI_SSID_PWD_FROM_STDIN') is True:
  28. dut.expect('Please input ssid password:')
  29. env_name = 'wifi_router'
  30. ap_ssid = get_env_config_variable(env_name, 'ap_ssid')
  31. ap_password = get_env_config_variable(env_name, 'ap_password')
  32. dut.write(f'{ap_ssid} {ap_password}')
  33. ipv4 = dut.expect(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]', timeout=30)[1].decode()
  34. print(f'Connected with IPv4={ipv4}')
  35. # test IPv4
  36. with TcpServer(PORT, socket.AF_INET):
  37. server_ip = get_host_ip4_by_dest_ip(ipv4)
  38. print('Connect tcp client to server IP={}'.format(server_ip))
  39. dut.write(server_ip)
  40. dut.expect('OK: Message from ESP32')
  41. @pytest.mark.esp32
  42. @pytest.mark.esp32s2
  43. @pytest.mark.esp32c2
  44. @pytest.mark.esp32c3
  45. @pytest.mark.esp32s3
  46. @pytest.mark.esp32c6
  47. @pytest.mark.wifi_router
  48. def test_examples_tcp_client_ipv6(dut: Dut) -> None:
  49. # Parse IP address of STA
  50. logging.info('Waiting to connect with AP')
  51. if dut.app.sdkconfig.get('EXAMPLE_WIFI_SSID_PWD_FROM_STDIN') is True:
  52. dut.expect('Please input ssid password:')
  53. env_name = 'wifi_router'
  54. ap_ssid = get_env_config_variable(env_name, 'ap_ssid')
  55. ap_password = get_env_config_variable(env_name, 'ap_password')
  56. dut.write(f'{ap_ssid} {ap_password}')
  57. ipv4 = dut.expect(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]', timeout=30)[1].decode()
  58. # expect all 8 octets from IPv6 (assumes it's printed in the long form)
  59. ipv6_r = r':'.join((r'[0-9a-fA-F]{4}',) * 8)
  60. ipv6 = dut.expect(ipv6_r, timeout=30)[0].decode()
  61. print('Connected with IPv4={} and IPv6={}'.format(ipv4, ipv6))
  62. # test IPv6
  63. my_interface = get_my_interface_by_dest_ip(ipv4)
  64. with TcpServer(PORT, socket.AF_INET6):
  65. server_ip = get_host_ip6_by_dest_ip(ipv6, my_interface)
  66. print('Connect tcp client to server IP={}'.format(server_ip))
  67. dut.write(server_ip)
  68. dut.expect('OK: Message from ESP32')