pytest_tcp_server.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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.esp32c3
  19. @pytest.mark.esp32s3
  20. @pytest.mark.wifi_router
  21. def test_examples_tcp_server_ipv4(dut: Dut) -> None:
  22. # Parse IP address of STA
  23. logging.info('Waiting to connect with AP')
  24. if dut.app.sdkconfig.get('EXAMPLE_WIFI_SSID_PWD_FROM_STDIN') is True:
  25. dut.expect('Please input ssid password:')
  26. env_name = 'wifi_router'
  27. ap_ssid = get_env_config_variable(env_name, 'ap_ssid')
  28. ap_password = get_env_config_variable(env_name, 'ap_password')
  29. dut.write(f'{ap_ssid} {ap_password}')
  30. ipv4 = dut.expect(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]', timeout=30)[1].decode()
  31. print(f'Connected with IPv4={ipv4}')
  32. time.sleep(1)
  33. # test IPv4
  34. received = tcp_client(ipv4, PORT, MESSAGE)
  35. if not received == MESSAGE:
  36. raise
  37. dut.expect(MESSAGE)
  38. @pytest.mark.esp32
  39. @pytest.mark.esp32c3
  40. @pytest.mark.esp32s3
  41. @pytest.mark.wifi_router
  42. def test_examples_tcp_server_ipv6(dut: Dut) -> None:
  43. # Parse IP address of STA
  44. logging.info('Waiting to connect with AP')
  45. if dut.app.sdkconfig.get('EXAMPLE_WIFI_SSID_PWD_FROM_STDIN') is True:
  46. dut.expect('Please input ssid password:')
  47. env_name = 'wifi_router'
  48. ap_ssid = get_env_config_variable(env_name, 'ap_ssid')
  49. ap_password = get_env_config_variable(env_name, 'ap_password')
  50. dut.write(f'{ap_ssid} {ap_password}')
  51. ipv4 = dut.expect(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]', timeout=30)[1].decode()
  52. # expect all 8 octets from IPv6 (assumes it's printed in the long form)
  53. ipv6_r = r':'.join((r'[0-9a-fA-F]{4}',) * 8)
  54. ipv6 = dut.expect(ipv6_r, timeout=30)[0].decode()
  55. print(f'Connected with IPv4={ipv4} and IPv6={ipv6}')
  56. time.sleep(1)
  57. interface = get_my_interface_by_dest_ip(ipv4)
  58. # test IPv6
  59. received = tcp_client('{}%{}'.format(ipv6, interface), PORT, MESSAGE)
  60. if not received == MESSAGE:
  61. raise
  62. dut.expect(MESSAGE)