pytest_esp_eth.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: CC0-1.0
  3. import contextlib
  4. import logging
  5. import os
  6. import socket
  7. from typing import Iterator
  8. import pytest
  9. from pytest_embedded import Dut
  10. @contextlib.contextmanager
  11. def configure_eth_if() -> Iterator[socket.socket]:
  12. # try to determine which interface to use
  13. netifs = os.listdir('/sys/class/net/')
  14. logging.info('detected interfaces: %s', str(netifs))
  15. target_if = ''
  16. for netif in netifs:
  17. if netif.find('eth') == 0 or netif.find('enp') == 0 or netif.find('eno') == 0:
  18. target_if = netif
  19. break
  20. if target_if == '':
  21. raise Exception('no network interface found')
  22. logging.info('Use %s for testing', target_if)
  23. so = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, 0x2222)
  24. so.bind((target_if, 0))
  25. try:
  26. yield so
  27. finally:
  28. so.close()
  29. def send_eth_packet(mac: bytes) -> None:
  30. with configure_eth_if() as so:
  31. so.settimeout(10)
  32. pkt = bytearray()
  33. pkt += mac # dest
  34. pkt += so.getsockname()[4] # src
  35. pkt += bytes.fromhex('2222') # proto
  36. pkt += bytes(1010) # padding to 1024
  37. for i in range(128, 1024):
  38. pkt[i] = i & 0xff
  39. try:
  40. so.send(pkt)
  41. except Exception as e:
  42. raise e
  43. def actual_test(dut: Dut) -> None:
  44. dut.expect_exact('Press ENTER to see the list of tests')
  45. dut.write('\n')
  46. dut.expect_exact('Enter test for running.')
  47. dut.write('"start_and_stop"')
  48. dut.expect_unity_test_output()
  49. dut.expect_exact("Enter next test, or 'enter' to see menu")
  50. dut.write('"get_set_mac"')
  51. dut.expect_unity_test_output()
  52. dut.expect_exact("Enter next test, or 'enter' to see menu")
  53. with configure_eth_if() as so:
  54. so.settimeout(30)
  55. dut.write('"ethernet_broadcast_transmit"')
  56. pkt = so.recv(1024)
  57. for i in range(128, 1024):
  58. if pkt[i] != i & 0xff:
  59. raise Exception('Packet content mismatch')
  60. dut.expect_unity_test_output()
  61. dut.expect_exact("Enter next test, or 'enter' to see menu")
  62. dut.write('"recv_pkt"')
  63. res = dut.expect(
  64. r'([\s\S]*)'
  65. r'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})'
  66. )
  67. send_eth_packet(bytes.fromhex('ffffffffffff')) # broadcast frame # pylint: disable=no-value-for-parameter
  68. send_eth_packet(bytes.fromhex('010000000000')) # multicast frame # pylint: disable=no-value-for-parameter
  69. send_eth_packet(bytes.fromhex(res.group(2).decode('utf-8').replace(':', ''))) # unicast fram # pylint: disable=no-value-for-parameter, line-too-long # noqa
  70. dut.expect_unity_test_output(extra_before=res.group(1))
  71. @pytest.mark.esp32
  72. @pytest.mark.ip101
  73. @pytest.mark.parametrize('config', [
  74. 'ip101',
  75. ], indirect=True)
  76. @pytest.mark.flaky(reruns=3, reruns_delay=5)
  77. def test_esp_eth_ip101(dut: Dut) -> None:
  78. actual_test(dut)
  79. @pytest.mark.esp32
  80. @pytest.mark.lan8720
  81. @pytest.mark.parametrize('config', [
  82. 'lan8720',
  83. ], indirect=True)
  84. @pytest.mark.flaky(reruns=3, reruns_delay=5)
  85. def test_esp_eth_lan8720(dut: Dut) -> None:
  86. actual_test(dut)