component_ut_test.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import os
  2. import re
  3. import socket
  4. import tiny_test_fw
  5. import ttfw_idf
  6. from tiny_test_fw import Utility
  7. from ttfw_idf import TestFormat
  8. try:
  9. import typing # noqa: F401 # pylint: disable=unused-import
  10. except ImportError:
  11. pass
  12. def configure_eth_if(func): # type: (typing.Any) -> typing.Any
  13. def inner(*args, **kwargs): # type: (typing.Any, typing.Any) -> typing.Any
  14. # try to determine which interface to use
  15. netifs = os.listdir('/sys/class/net/')
  16. target_if = ''
  17. Utility.console_log('detected interfaces: ' + str(netifs))
  18. for netif in netifs:
  19. if netif.find('eth') == 0 or netif.find('enp') == 0 or netif.find('eno') == 0:
  20. target_if = netif
  21. break
  22. if target_if == '':
  23. raise Exception('no network interface found')
  24. Utility.console_log('Use ' + target_if + ' for testing')
  25. so = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, 0x2222)
  26. so.bind((target_if, 0))
  27. func(so, *args, **kwargs)
  28. so.close()
  29. return inner
  30. @configure_eth_if
  31. def check_eth_recv_packet(so, before_recv=None): # type: (socket.socket, typing.Any) -> None
  32. so.settimeout(10)
  33. if before_recv is not None:
  34. before_recv() # If configured, execute user function just before sock recv
  35. try:
  36. pkt = so.recv(1024)
  37. for i in range(128, 1024):
  38. if pkt[i] != i & 0xff:
  39. raise Exception('Packet content mismatch')
  40. except Exception as e:
  41. raise e
  42. @configure_eth_if
  43. def send_eth_packet(so, mac): # type: (socket.socket, bytes) -> None
  44. so.settimeout(10)
  45. pkt = bytearray()
  46. pkt += mac # dest
  47. pkt += so.getsockname()[4] # src
  48. pkt += bytes.fromhex('2222') # proto
  49. pkt += bytes(1010) # padding to 1024
  50. for i in range(128, 1024):
  51. pkt[i] = i & 0xff
  52. try:
  53. so.send(pkt)
  54. except Exception as e:
  55. raise e
  56. def test_component_ut_esp_eth(env, appname): # type: (tiny_test_fw.Env, str) -> None
  57. dut = env.get_dut('esp_eth', 'components/esp_eth/test_apps', app_config_name=appname)
  58. dut.start_app()
  59. stdout = dut.expect('Press ENTER to see the list of tests', full_stdout=True)
  60. Utility.console_log('Running test case: start_and_stop')
  61. dut.write('"start_and_stop"')
  62. stdout += dut.expect("Enter next test, or 'enter' to see menu", full_stdout=True)
  63. ttfw_idf.ComponentUTResult.parse_result(stdout, test_format=TestFormat.UNITY_BASIC)
  64. Utility.console_log('Running test case: get_set_mac')
  65. dut.write('"get_set_mac"')
  66. stdout = dut.expect("Enter next test, or 'enter' to see menu", full_stdout=True)
  67. ttfw_idf.ComponentUTResult.parse_result(stdout, test_format=TestFormat.UNITY_BASIC)
  68. Utility.console_log('Running test case: ethernet_broadcast_transmit')
  69. check_eth_recv_packet(dut.write('"ethernet_broadcast_transmit"')) # Need to start the test after the socket is bound
  70. stdout = dut.expect("Enter next test, or 'enter' to see menu", full_stdout=True)
  71. ttfw_idf.ComponentUTResult.parse_result(stdout, test_format=TestFormat.UNITY_BASIC)
  72. Utility.console_log('Running test case: recv_pkt')
  73. dut.write('"recv_pkt"')
  74. 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})'),
  75. timeout=10)
  76. stdout = expect_result[0]
  77. Utility.console_log('DUTs MAC address: {}'.format(expect_result[1]))
  78. send_eth_packet(bytes.fromhex('ffffffffffff')) # broadcast frame
  79. send_eth_packet(bytes.fromhex('010000000000')) # multicast frame
  80. send_eth_packet(bytes.fromhex(expect_result[1].replace(':', ''))) # unicast frame
  81. stdout += dut.expect("Enter next test, or 'enter' to see menu", full_stdout=True)
  82. ttfw_idf.ComponentUTResult.parse_result(stdout, test_format=TestFormat.UNITY_BASIC)
  83. @ttfw_idf.idf_component_unit_test(env_tag='COMPONENT_UT_IP101', target=['esp32'])
  84. def test_component_ut_esp_eth_ip101(env, _): # type: (tiny_test_fw.Env, typing.Any) -> None
  85. test_component_ut_esp_eth(env, 'ip101')
  86. @ttfw_idf.idf_component_unit_test(env_tag='COMPONENT_UT_LAN8720', target=['esp32'])
  87. def test_component_ut_esp_eth_lan8720(env, _): # type: (tiny_test_fw.Env, typing.Any) -> None
  88. test_component_ut_esp_eth(env, 'lan8720')
  89. if __name__ == '__main__':
  90. test_component_ut_esp_eth_ip101()
  91. test_component_ut_esp_eth_lan8720()