pytest_tcp_server.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: Apache-2.0
  3. import logging
  4. import time
  5. import pytest
  6. from common_test_methods import get_env_config_variable, get_my_interface_by_dest_ip
  7. from pytest_embedded import Dut
  8. try:
  9. from run_tcp_client import tcp_client
  10. except ImportError:
  11. import os
  12. import sys
  13. sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'scripts')))
  14. from run_tcp_client import tcp_client
  15. PORT = 3333
  16. MESSAGE = 'Data to ESP'
  17. @pytest.mark.esp32
  18. @pytest.mark.wifi_router
  19. def test_examples_tcp_server_ipv4(dut: Dut) -> None:
  20. # Parse IP address of STA
  21. logging.info('Waiting to connect with AP')
  22. if dut.app.sdkconfig.get('EXAMPLE_WIFI_SSID_PWD_FROM_STDIN') is True:
  23. dut.expect('Please input ssid password:')
  24. env_name = 'wifi_router'
  25. ap_ssid = get_env_config_variable(env_name, 'ap_ssid')
  26. ap_password = get_env_config_variable(env_name, 'ap_password')
  27. dut.write(f'{ap_ssid} {ap_password}')
  28. ipv4 = dut.expect(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]', timeout=30)[1].decode()
  29. print(f'Connected with IPv4={ipv4}')
  30. time.sleep(1)
  31. # test IPv4
  32. received = tcp_client(ipv4, PORT, MESSAGE)
  33. if not received == MESSAGE:
  34. raise
  35. dut.expect(MESSAGE)
  36. @pytest.mark.esp32
  37. @pytest.mark.wifi_router
  38. def test_examples_tcp_server_ipv6(dut: Dut) -> None:
  39. # Parse IP address of STA
  40. logging.info('Waiting to connect with AP')
  41. if dut.app.sdkconfig.get('EXAMPLE_WIFI_SSID_PWD_FROM_STDIN') is True:
  42. dut.expect('Please input ssid password:')
  43. env_name = 'wifi_router'
  44. ap_ssid = get_env_config_variable(env_name, 'ap_ssid')
  45. ap_password = get_env_config_variable(env_name, 'ap_password')
  46. dut.write(f'{ap_ssid} {ap_password}')
  47. ipv4 = dut.expect(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]', timeout=30)[1].decode()
  48. # expect all 8 octets from IPv6 (assumes it's printed in the long form)
  49. ipv6_r = r':'.join((r'[0-9a-fA-F]{4}',) * 8)
  50. ipv6 = dut.expect(ipv6_r, timeout=30)[0].decode()
  51. print(f'Connected with IPv4={ipv4} and IPv6={ipv6}')
  52. time.sleep(1)
  53. interface = get_my_interface_by_dest_ip(ipv4)
  54. # test IPv6
  55. received = tcp_client('{}%{}'.format(ipv6, interface), PORT, MESSAGE)
  56. if not received == MESSAGE:
  57. raise
  58. dut.expect(MESSAGE)