pytest_tcp_server.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 netifaces
  6. import pytest
  7. from common_test_methods import get_env_config_variable, get_my_interface_by_dest_ip
  8. from pytest_embedded import Dut
  9. try:
  10. from run_tcp_client import tcp_client
  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_client import tcp_client
  16. PORT = 3333
  17. MESSAGE = 'Data to ESP'
  18. @pytest.mark.esp32
  19. @pytest.mark.esp32s2
  20. @pytest.mark.esp32c2
  21. @pytest.mark.esp32c3
  22. @pytest.mark.esp32s3
  23. @pytest.mark.esp32c6
  24. @pytest.mark.wifi_router
  25. def test_examples_tcp_server_ipv4(dut: Dut) -> None:
  26. # Parse IP address of STA
  27. logging.info('Waiting to connect with AP')
  28. if dut.app.sdkconfig.get('EXAMPLE_WIFI_SSID_PWD_FROM_STDIN') is True:
  29. dut.expect('Please input ssid password:')
  30. env_name = 'wifi_router'
  31. ap_ssid = get_env_config_variable(env_name, 'ap_ssid')
  32. ap_password = get_env_config_variable(env_name, 'ap_password')
  33. dut.write(f'{ap_ssid} {ap_password}')
  34. ipv4 = dut.expect(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]', timeout=30)[1].decode()
  35. logging.info(f'Connected with IPv4={ipv4}')
  36. time.sleep(1)
  37. # test IPv4
  38. received = tcp_client(ipv4, PORT, MESSAGE)
  39. if not received == MESSAGE:
  40. raise
  41. dut.expect(MESSAGE)
  42. @pytest.mark.esp32c2
  43. @pytest.mark.wifi_router
  44. @pytest.mark.xtal_26mhz
  45. @pytest.mark.parametrize(
  46. 'config, baud', [
  47. ('c2_xtal26m', '74880'),
  48. ], indirect=True
  49. )
  50. def test_examples_tcp_server_ipv4_esp32c2_26mhz(dut: Dut) -> None:
  51. # Parse IP address of STA
  52. logging.info('Waiting to connect with AP')
  53. if dut.app.sdkconfig.get('EXAMPLE_WIFI_SSID_PWD_FROM_STDIN') is True:
  54. dut.expect('Please input ssid password:')
  55. env_name = 'wifi_router'
  56. ap_ssid = get_env_config_variable(env_name, 'ap_ssid')
  57. ap_password = get_env_config_variable(env_name, 'ap_password')
  58. dut.write(f'{ap_ssid} {ap_password}')
  59. ipv4 = dut.expect(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]', timeout=30)[1].decode()
  60. logging.info(f'Connected with IPv4={ipv4}')
  61. time.sleep(1)
  62. # test IPv4
  63. received = tcp_client(ipv4, PORT, MESSAGE)
  64. if not received == MESSAGE:
  65. raise
  66. dut.expect(MESSAGE)
  67. @pytest.mark.esp32
  68. @pytest.mark.esp32s2
  69. @pytest.mark.esp32c2
  70. @pytest.mark.esp32c3
  71. @pytest.mark.esp32s3
  72. @pytest.mark.esp32c6
  73. @pytest.mark.wifi_router
  74. def test_examples_tcp_server_ipv6(dut: Dut) -> None:
  75. # Parse IP address of STA
  76. logging.info('Waiting to connect with AP')
  77. if dut.app.sdkconfig.get('EXAMPLE_WIFI_SSID_PWD_FROM_STDIN') is True:
  78. dut.expect('Please input ssid password:')
  79. env_name = 'wifi_router'
  80. ap_ssid = get_env_config_variable(env_name, 'ap_ssid')
  81. ap_password = get_env_config_variable(env_name, 'ap_password')
  82. dut.write(f'{ap_ssid} {ap_password}')
  83. ipv4 = dut.expect(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]', timeout=30)[1].decode()
  84. # expect all 8 octets from IPv6 (assumes it's printed in the long form)
  85. ipv6_r = r':'.join((r'[0-9a-fA-F]{4}',) * 8)
  86. ipv6 = dut.expect(ipv6_r, timeout=30)[0].decode()
  87. logging.info(f'Connected with IPv4={ipv4} and IPv6={ipv6}')
  88. time.sleep(1)
  89. interface = get_my_interface_by_dest_ip(ipv4)
  90. # test IPv6
  91. received = tcp_client('{}%{}'.format(ipv6, interface), PORT, MESSAGE)
  92. if not received == MESSAGE:
  93. raise
  94. dut.expect(MESSAGE)
  95. @pytest.mark.esp32
  96. @pytest.mark.esp32s2
  97. @pytest.mark.esp32c2
  98. @pytest.mark.esp32c3
  99. @pytest.mark.esp32s3
  100. @pytest.mark.esp32c6
  101. @pytest.mark.wifi_router
  102. def test_examples_tcp_server_ipv6_only(dut: Dut) -> None:
  103. # Parse IP address of STA
  104. logging.info('Waiting to connect with AP')
  105. if dut.app.sdkconfig.get('EXAMPLE_WIFI_SSID_PWD_FROM_STDIN') is True:
  106. dut.expect('Please input ssid password:')
  107. env_name = 'wifi_router'
  108. ap_ssid = get_env_config_variable(env_name, 'ap_ssid')
  109. ap_password = get_env_config_variable(env_name, 'ap_password')
  110. dut.write(f'{ap_ssid} {ap_password}')
  111. # expect all 8 octets from IPv6 (assumes it's printed in the long form)
  112. ipv6_r = r':'.join((r'[0-9a-fA-F]{4}',) * 8)
  113. ipv6 = dut.expect(ipv6_r, timeout=30)[0].decode()
  114. logging.info(f'Connected AP with IPv6={ipv6}')
  115. time.sleep(5)
  116. # test IPv6, try all interfaces
  117. for interface in netifaces.interfaces():
  118. try:
  119. logging.info(f'[{interface}] Connect to tcp_server {ipv6}%{interface}')
  120. received = tcp_client(f'{ipv6}%{interface}', PORT, MESSAGE)
  121. except Exception:
  122. # wrong interface
  123. continue
  124. if not received == MESSAGE:
  125. raise AssertionError(f'Wrong data received ({received}/{MESSAGE})')
  126. dut.expect(MESSAGE)
  127. break
  128. else:
  129. raise AssertionError(f'Can not connect to tcp_server: {ipv6}')