pytest_otbr.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. # SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: Unlicense OR CC0-1.0
  3. # !/usr/bin/env python3
  4. import os.path
  5. import re
  6. import socket
  7. import struct
  8. import subprocess
  9. import time
  10. from typing import Tuple
  11. import ot_ci_function as ocf
  12. import pytest
  13. from pytest_embedded_idf.dut import IdfDut
  14. # This file contains the test scripts for Thread:
  15. # Case 1: Thread network formation and attaching
  16. # A Thread Border Router forms a Thread network, a Thread device attaches to it, then test ping connection between them.
  17. # Case 2: Bidirectional IPv6 connectivity
  18. # Test IPv6 ping connection between Thread device and Linux Host (via Thread Border Router).
  19. # Case 3: Multicast forwarding from Wi-Fi to Thread network
  20. # Thread device joins the multicast group, then test group communication from Wi-Fi to Thread network.
  21. # Case 4: Multicast forwarding from Thread to Wi-Fi network
  22. # Linux Host joins the multicast group, test group communication from Thread to Wi-Fi network.
  23. @pytest.fixture(name='Init_interface')
  24. def fixture_Init_interface() -> bool:
  25. ocf.init_interface_ipv6_address()
  26. ocf.reset_host_interface()
  27. ocf.set_interface_sysctl_options()
  28. return True
  29. # Case 1: Thread network formation and attaching
  30. @pytest.mark.esp32s3
  31. @pytest.mark.esp32h2
  32. @pytest.mark.i154_multi_dut
  33. @pytest.mark.flaky(reruns=2, reruns_delay=10)
  34. @pytest.mark.parametrize(
  35. 'port, config, count, app_path, beta_target, target', [
  36. ('/dev/USB_BR|/dev/USB_CLI|/dev/USB_RCP', 'br|cli|rcp', 3,
  37. f'{os.path.join(os.path.dirname(__file__), "ot_br")}'
  38. f'|{os.path.join(os.path.dirname(__file__), "ot_cli")}'
  39. f'|{os.path.join(os.path.dirname(__file__), "ot_rcp")}',
  40. 'esp32s3|esp32h2beta2|esp32h2beta2', 'esp32s3|esp32h2|esp32h2'),
  41. ],
  42. indirect=True,
  43. )
  44. def test_thread_connect(dut:Tuple[IdfDut, IdfDut]) -> None:
  45. br = dut[0]
  46. cli = dut[1]
  47. dataset = '-1'
  48. ocf.form_network_using_manual_configuration(br, cli, 'br', 'random', dataset, br, 'OTCITE', '0000')
  49. time.sleep(1)
  50. flag = False
  51. try:
  52. cli_mleid_addr = ocf.get_mleid_addr(cli)
  53. br_mleid_addr = ocf.get_mleid_addr(br)
  54. rx_nums = ocf.ot_ping(cli, br_mleid_addr, 5)[1]
  55. assert rx_nums != 0
  56. rx_nums = ocf.ot_ping(br, cli_mleid_addr, 5)[1]
  57. assert rx_nums != 0
  58. flag = True
  59. finally:
  60. br.write('factoryreset')
  61. cli.write('factoryreset')
  62. time.sleep(3)
  63. assert flag
  64. # Case 2: Bidirectional IPv6 connectivity
  65. @pytest.mark.esp32s3
  66. @pytest.mark.esp32h2
  67. @pytest.mark.i154_multi_dut
  68. @pytest.mark.flaky(reruns=5, reruns_delay=10)
  69. @pytest.mark.parametrize(
  70. 'port, config, count, app_path, beta_target, target', [
  71. ('/dev/USB_BR|/dev/USB_CLI|/dev/USB_RCP', 'br|cli|rcp', 3,
  72. f'{os.path.join(os.path.dirname(__file__), "ot_br")}'
  73. f'|{os.path.join(os.path.dirname(__file__), "ot_cli")}'
  74. f'|{os.path.join(os.path.dirname(__file__), "ot_rcp")}',
  75. 'esp32s3|esp32h2beta2|esp32h2beta2', 'esp32s3|esp32h2|esp32h2'),
  76. ],
  77. indirect=True,
  78. )
  79. def test_Bidirectional_IPv6_connectivity(Init_interface:bool, dut: Tuple[IdfDut, IdfDut]) -> None:
  80. br = dut[0]
  81. cli = dut[1]
  82. assert Init_interface
  83. dataset = '-1'
  84. ocf.form_network_using_manual_configuration(br, cli, 'br', 'random', dataset, br, 'OTCITE', 'otcitest888')
  85. time.sleep(5)
  86. cli_global_unicast_addr = ocf.get_global_unicast_addr(cli, br)
  87. flag = False
  88. try:
  89. command = 'ping ' + str(cli_global_unicast_addr) + ' -c 10'
  90. out_bytes = subprocess.check_output(command, shell=True, timeout=60)
  91. out_str = out_bytes.decode('utf-8')
  92. role = re.findall(r' (\d+)%', str(out_str))[0]
  93. assert role != '100'
  94. interface_name = ocf.get_host_interface_name()
  95. command = 'ifconfig ' + interface_name
  96. out_bytes = subprocess.check_output(command, shell=True, timeout=5)
  97. out_str = out_bytes.decode('utf-8')
  98. host_global_unicast_addr = re.findall(r'inet6 ((?:\w+:){7}\w+) prefixlen 64 scopeid 0x0<global>', str(out_str))
  99. rx_nums = 0
  100. for ip_addr in host_global_unicast_addr:
  101. txrx_nums = ocf.ot_ping(cli, str(ip_addr), 5)
  102. rx_nums = rx_nums + int(txrx_nums[1])
  103. assert rx_nums != 0
  104. flag = True
  105. finally:
  106. br.write('factoryreset')
  107. cli.write('factoryreset')
  108. time.sleep(3)
  109. assert flag
  110. # Case 3: Multicast forwarding from Wi-Fi to Thread network
  111. @pytest.mark.esp32s3
  112. @pytest.mark.esp32h2
  113. @pytest.mark.i154_multi_dut
  114. @pytest.mark.flaky(reruns=5, reruns_delay=10)
  115. @pytest.mark.parametrize(
  116. 'port, config, count, app_path, beta_target, target', [
  117. ('/dev/USB_BR|/dev/USB_CLI|/dev/USB_RCP', 'br|cli|rcp', 3,
  118. f'{os.path.join(os.path.dirname(__file__), "ot_br")}'
  119. f'|{os.path.join(os.path.dirname(__file__), "ot_cli")}'
  120. f'|{os.path.join(os.path.dirname(__file__), "ot_rcp")}',
  121. 'esp32s3|esp32h2beta2|esp32h2beta2', 'esp32s3|esp32h2|esp32h2'),
  122. ],
  123. indirect=True,
  124. )
  125. def test_multicast_forwarding_A(Init_interface:bool, dut: Tuple[IdfDut, IdfDut]) -> None:
  126. br = dut[0]
  127. cli = dut[1]
  128. assert Init_interface
  129. dataset = '-1'
  130. ocf.form_network_using_manual_configuration(br, cli, 'br', 'random', dataset, br, 'OTCITE', 'otcitest888')
  131. time.sleep(5)
  132. flag = False
  133. try:
  134. br.write('bbr')
  135. br.expect('server16', timeout=2)
  136. cli.write('mcast join ff04::125')
  137. cli.expect('Done', timeout=2)
  138. time.sleep(1)
  139. interface_name = ocf.get_host_interface_name()
  140. command = 'ping -I ' + str(interface_name) + ' -t 64 ff04::125 -c 10'
  141. out_bytes = subprocess.check_output(command, shell=True, timeout=60)
  142. out_str = out_bytes.decode('utf-8')
  143. role = re.findall(r' (\d+)%', str(out_str))[0]
  144. assert role != '100'
  145. flag = True
  146. finally:
  147. br.write('factoryreset')
  148. cli.write('factoryreset')
  149. time.sleep(3)
  150. assert flag
  151. # Case 4: Multicast forwarding from Thread to Wi-Fi network
  152. @pytest.mark.esp32s3
  153. @pytest.mark.esp32h2
  154. @pytest.mark.i154_multi_dut
  155. @pytest.mark.flaky(reruns=5, reruns_delay=5)
  156. @pytest.mark.parametrize(
  157. 'port, config, count, app_path, beta_target, target', [
  158. ('/dev/USB_BR|/dev/USB_CLI|/dev/USB_RCP', 'br|cli|rcp', 3,
  159. f'{os.path.join(os.path.dirname(__file__), "ot_br")}'
  160. f'|{os.path.join(os.path.dirname(__file__), "ot_cli")}'
  161. f'|{os.path.join(os.path.dirname(__file__), "ot_rcp")}',
  162. 'esp32s3|esp32h2beta2|esp32h2beta2', 'esp32s3|esp32h2|esp32h2'),
  163. ],
  164. indirect=True,
  165. )
  166. def test_multicast_forwarding_B(Init_interface:bool, dut: Tuple[IdfDut, IdfDut]) -> None:
  167. br = dut[0]
  168. cli = dut[1]
  169. assert Init_interface
  170. dataset = '-1'
  171. ocf.form_network_using_manual_configuration(br, cli, 'br', 'random', dataset, br, 'OTCITE', 'otcitest888')
  172. time.sleep(5)
  173. br.write('bbr')
  174. br.expect('server16', timeout=2)
  175. interface_name = ocf.get_host_interface_name()
  176. if_index = socket.if_nametoindex(interface_name)
  177. sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
  178. sock.bind(('::', 5090))
  179. sock.setsockopt(
  180. socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP,
  181. struct.pack('16si', socket.inet_pton(socket.AF_INET6, 'ff04::125'),
  182. if_index))
  183. time.sleep(1)
  184. cli.write('udp open')
  185. cli.expect('Done', timeout=2)
  186. cli.write('udp send ff04::125 5090 hello')
  187. cli.expect('Done', timeout=2)
  188. data = b''
  189. try:
  190. print('The host start to receive message!')
  191. sock.settimeout(5)
  192. data = (sock.recvfrom(1024))[0]
  193. print('The host has received message!')
  194. except socket.error:
  195. print('The host did not received message!')
  196. finally:
  197. sock.close()
  198. br.write('factoryreset')
  199. cli.write('factoryreset')
  200. time.sleep(3)
  201. assert data == b'hello'