mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
128 lines
4.6 KiB
Python
128 lines
4.6 KiB
Python
# SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
import logging
|
|
import socket
|
|
from threading import Event, Thread
|
|
|
|
import pytest
|
|
from common_test_methods import (get_env_config_variable, get_host_ip4_by_dest_ip, get_host_ip6_by_dest_ip,
|
|
get_my_interface_by_dest_ip)
|
|
from pexpect.exceptions import TIMEOUT
|
|
from pytest_embedded import Dut
|
|
|
|
PORT = 3333
|
|
MAX_RETRIES = 3
|
|
|
|
|
|
class UdpServer:
|
|
|
|
def __init__(self, port, family_addr, persist=False): # type: ignore
|
|
self.port = port
|
|
self.family_addr = family_addr
|
|
self.socket = socket.socket(family_addr, socket.SOCK_DGRAM)
|
|
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
self.socket.settimeout(60.0)
|
|
self.shutdown = Event()
|
|
self.persist = persist
|
|
|
|
def __enter__(self): # type: ignore
|
|
try:
|
|
self.socket.bind(('', self.port))
|
|
except socket.error as e:
|
|
print('Bind failed:{}'.format(e))
|
|
raise
|
|
|
|
print('Starting server on port={} family_addr={}'.format(self.port, self.family_addr))
|
|
self.server_thread = Thread(target=self.run_server)
|
|
self.server_thread.start()
|
|
return self
|
|
|
|
def __exit__(self, exc_type, exc_value, traceback): # type: ignore
|
|
if self.persist:
|
|
sock = socket.socket(self.family_addr, socket.SOCK_DGRAM)
|
|
sock.sendto(b'Stop', ('localhost', self.port))
|
|
sock.close()
|
|
self.shutdown.set()
|
|
self.server_thread.join()
|
|
self.socket.close()
|
|
|
|
def run_server(self) -> None:
|
|
while not self.shutdown.is_set():
|
|
try:
|
|
data, addr = self.socket.recvfrom(1024)
|
|
print(addr)
|
|
if not data:
|
|
return
|
|
data = data.decode()
|
|
print('Reply[' + addr[0] + ':' + str(addr[1]) + '] - ' + data)
|
|
reply = 'OK: ' + data
|
|
self.socket.sendto(reply.encode(), addr)
|
|
except socket.error as e:
|
|
print('Running server failed:{}'.format(e))
|
|
raise
|
|
if not self.persist:
|
|
break
|
|
|
|
|
|
@pytest.mark.esp32
|
|
@pytest.mark.wifi_router
|
|
def test_examples_udp_client_ipv4(dut: Dut) -> None:
|
|
# Parse IP address of STA
|
|
logging.info('Waiting to connect with AP')
|
|
if dut.app.sdkconfig.get('EXAMPLE_WIFI_SSID_PWD_FROM_STDIN') is True:
|
|
dut.expect('Please input ssid password:')
|
|
env_name = 'wifi_router'
|
|
ap_ssid = get_env_config_variable(env_name, 'ap_ssid')
|
|
ap_password = get_env_config_variable(env_name, 'ap_password')
|
|
dut.write(f'{ap_ssid} {ap_password}')
|
|
ipv4 = dut.expect(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]', timeout=30)[1].decode()
|
|
print(f'Connected with IPv4={ipv4}')
|
|
|
|
# test IPv4
|
|
with UdpServer(PORT, socket.AF_INET):
|
|
server_ip = get_host_ip4_by_dest_ip(ipv4)
|
|
print('Connect udp client to server IP={}'.format(server_ip))
|
|
for _ in range(MAX_RETRIES):
|
|
try:
|
|
dut.write(server_ip)
|
|
dut.expect('OK: Message from ESP32')
|
|
break
|
|
except TIMEOUT:
|
|
pass
|
|
else:
|
|
raise ValueError('Failed to send/recv udp packets.')
|
|
|
|
|
|
@pytest.mark.esp32
|
|
@pytest.mark.wifi_router
|
|
def test_examples_udp_client_ipv6(dut: Dut) -> None:
|
|
# Parse IP address of STA
|
|
logging.info('Waiting to connect with AP')
|
|
if dut.app.sdkconfig.get('EXAMPLE_WIFI_SSID_PWD_FROM_STDIN') is True:
|
|
dut.expect('Please input ssid password:')
|
|
env_name = 'wifi_router'
|
|
ap_ssid = get_env_config_variable(env_name, 'ap_ssid')
|
|
ap_password = get_env_config_variable(env_name, 'ap_password')
|
|
dut.write(f'{ap_ssid} {ap_password}')
|
|
ipv4 = dut.expect(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]', timeout=30)[1].decode()
|
|
# expect all 8 octets from IPv6 (assumes it's printed in the long form)
|
|
ipv6_r = r':'.join((r'[0-9a-fA-F]{4}',) * 8)
|
|
ipv6 = dut.expect(ipv6_r, timeout=30)[0].decode()
|
|
print(f'Connected with IPv4={ipv4} and IPv6={ipv6}')
|
|
|
|
interface = get_my_interface_by_dest_ip(ipv4)
|
|
# test IPv6
|
|
with UdpServer(PORT, socket.AF_INET6):
|
|
server_ip = get_host_ip6_by_dest_ip(ipv6, interface)
|
|
print('Connect udp client to server IP={}'.format(server_ip))
|
|
for _ in range(MAX_RETRIES):
|
|
try:
|
|
dut.write(server_ip)
|
|
dut.expect('OK: Message from ESP32')
|
|
break
|
|
except TIMEOUT:
|
|
pass
|
|
else:
|
|
raise ValueError('Failed to send/recv udpv6 packets.')
|