2021-07-14 03:37:47 -04:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import socket
|
|
|
|
|
|
|
|
import tiny_test_fw
|
|
|
|
import ttfw_idf
|
2021-12-09 01:37:26 -05:00
|
|
|
from tiny_test_fw import Utility
|
2021-07-14 03:37:47 -04:00
|
|
|
from ttfw_idf import TestFormat
|
|
|
|
|
|
|
|
try:
|
|
|
|
import typing # noqa: F401 # pylint: disable=unused-import
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def configure_eth_if(func): # type: (typing.Any) -> typing.Any
|
|
|
|
def inner(*args, **kwargs): # type: (typing.Any, typing.Any) -> typing.Any
|
|
|
|
# try to determine which interface to use
|
|
|
|
netifs = os.listdir('/sys/class/net/')
|
|
|
|
target_if = ''
|
2021-12-09 01:37:26 -05:00
|
|
|
Utility.console_log('detected interfaces: ' + str(netifs))
|
2021-07-14 03:37:47 -04:00
|
|
|
for netif in netifs:
|
|
|
|
if netif.find('eth') == 0 or netif.find('enp') == 0 or netif.find('eno') == 0:
|
|
|
|
target_if = netif
|
|
|
|
break
|
|
|
|
if target_if == '':
|
|
|
|
raise Exception('no network interface found')
|
2021-12-09 01:37:26 -05:00
|
|
|
Utility.console_log('Use ' + target_if + ' for testing')
|
2021-07-14 03:37:47 -04:00
|
|
|
so = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, 0x2222)
|
|
|
|
so.bind((target_if, 0))
|
|
|
|
|
|
|
|
func(so, *args, **kwargs)
|
|
|
|
|
|
|
|
so.close()
|
|
|
|
|
|
|
|
return inner
|
|
|
|
|
|
|
|
|
|
|
|
@configure_eth_if
|
2021-12-09 10:27:37 -05:00
|
|
|
def check_eth_recv_packet(so, before_recv=None): # type: (socket.socket, typing.Any) -> None
|
2021-07-14 03:37:47 -04:00
|
|
|
so.settimeout(10)
|
2021-12-09 10:27:37 -05:00
|
|
|
if before_recv is not None:
|
|
|
|
before_recv() # If configured, execute user function just before sock recv
|
2021-07-14 03:37:47 -04:00
|
|
|
try:
|
|
|
|
pkt = so.recv(1024)
|
|
|
|
for i in range(128, 1024):
|
|
|
|
if pkt[i] != i & 0xff:
|
|
|
|
raise Exception('Packet content mismatch')
|
|
|
|
except Exception as e:
|
|
|
|
raise e
|
|
|
|
|
|
|
|
|
|
|
|
@configure_eth_if
|
|
|
|
def send_eth_packet(so, mac): # type: (socket.socket, bytes) -> None
|
|
|
|
so.settimeout(10)
|
|
|
|
pkt = bytearray()
|
|
|
|
pkt += mac # dest
|
|
|
|
pkt += so.getsockname()[4] # src
|
|
|
|
pkt += bytes.fromhex('2222') # proto
|
|
|
|
pkt += bytes(1010) # padding to 1024
|
|
|
|
for i in range(128, 1024):
|
|
|
|
pkt[i] = i & 0xff
|
|
|
|
try:
|
|
|
|
so.send(pkt)
|
|
|
|
except Exception as e:
|
|
|
|
raise e
|
|
|
|
|
|
|
|
|
|
|
|
def test_component_ut_esp_eth(env, appname): # type: (tiny_test_fw.Env, str) -> None
|
|
|
|
dut = env.get_dut('esp_eth', 'components/esp_eth/test_apps', app_config_name=appname)
|
|
|
|
dut.start_app()
|
|
|
|
stdout = dut.expect('Press ENTER to see the list of tests', full_stdout=True)
|
2021-12-09 10:27:37 -05:00
|
|
|
|
|
|
|
Utility.console_log('Running test case: start_and_stop')
|
2021-07-14 03:37:47 -04:00
|
|
|
dut.write('"start_and_stop"')
|
|
|
|
stdout += dut.expect("Enter next test, or 'enter' to see menu", full_stdout=True)
|
|
|
|
ttfw_idf.ComponentUTResult.parse_result(stdout, test_format=TestFormat.UNITY_BASIC)
|
2021-12-09 10:27:37 -05:00
|
|
|
|
|
|
|
Utility.console_log('Running test case: get_set_mac')
|
2021-07-14 03:37:47 -04:00
|
|
|
dut.write('"get_set_mac"')
|
|
|
|
stdout = dut.expect("Enter next test, or 'enter' to see menu", full_stdout=True)
|
|
|
|
ttfw_idf.ComponentUTResult.parse_result(stdout, test_format=TestFormat.UNITY_BASIC)
|
2021-12-09 10:27:37 -05:00
|
|
|
|
|
|
|
Utility.console_log('Running test case: ethernet_broadcast_transmit')
|
|
|
|
check_eth_recv_packet(dut.write('"ethernet_broadcast_transmit"')) # Need to start the test after the socket is bound
|
2021-07-14 03:37:47 -04:00
|
|
|
stdout = dut.expect("Enter next test, or 'enter' to see menu", full_stdout=True)
|
|
|
|
ttfw_idf.ComponentUTResult.parse_result(stdout, test_format=TestFormat.UNITY_BASIC)
|
2021-12-09 10:27:37 -05:00
|
|
|
|
|
|
|
Utility.console_log('Running test case: recv_pkt')
|
2021-07-14 03:37:47 -04:00
|
|
|
dut.write('"recv_pkt"')
|
2021-12-09 01:17:36 -05:00
|
|
|
expect_result = dut.expect(re.compile(r'([\s\S]*)DUT MAC: ([0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2})'),
|
|
|
|
timeout=10)
|
2021-07-14 03:37:47 -04:00
|
|
|
stdout = expect_result[0]
|
2021-12-09 01:37:26 -05:00
|
|
|
Utility.console_log('DUTs MAC address: {}'.format(expect_result[1]))
|
2021-07-14 03:37:47 -04:00
|
|
|
send_eth_packet(bytes.fromhex('ffffffffffff')) # broadcast frame
|
|
|
|
send_eth_packet(bytes.fromhex('010000000000')) # multicast frame
|
|
|
|
send_eth_packet(bytes.fromhex(expect_result[1].replace(':', ''))) # unicast frame
|
|
|
|
stdout += dut.expect("Enter next test, or 'enter' to see menu", full_stdout=True)
|
|
|
|
ttfw_idf.ComponentUTResult.parse_result(stdout, test_format=TestFormat.UNITY_BASIC)
|
|
|
|
|
|
|
|
|
|
|
|
@ttfw_idf.idf_component_unit_test(env_tag='COMPONENT_UT_IP101', target=['esp32'])
|
|
|
|
def test_component_ut_esp_eth_ip101(env, _): # type: (tiny_test_fw.Env, typing.Any) -> None
|
|
|
|
test_component_ut_esp_eth(env, 'ip101')
|
|
|
|
|
|
|
|
|
|
|
|
@ttfw_idf.idf_component_unit_test(env_tag='COMPONENT_UT_LAN8720', target=['esp32'])
|
|
|
|
def test_component_ut_esp_eth_lan8720(env, _): # type: (tiny_test_fw.Env, typing.Any) -> None
|
|
|
|
test_component_ut_esp_eth(env, 'lan8720')
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test_component_ut_esp_eth_ip101()
|
|
|
|
test_component_ut_esp_eth_lan8720()
|