ot_ci_function.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. # SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: Unlicense OR CC0-1.0
  3. # !/usr/bin/env python3
  4. # this file defines some functions for testing cli and br under pytest framework
  5. import re
  6. import socket
  7. import struct
  8. import subprocess
  9. import time
  10. from typing import Tuple
  11. import netifaces
  12. import pexpect
  13. from pytest_embedded_idf.dut import IdfDut
  14. class thread_parameter:
  15. def __init__(self, deviceRole:str='', dataset:str='', channel:str='', exaddr:str='', bbr:bool=False):
  16. self.deviceRole = deviceRole
  17. self.dataset = dataset
  18. self.channel = channel
  19. self.exaddr = exaddr
  20. self.bbr = bbr
  21. class wifi_parameter:
  22. def __init__(self, ssid:str='', psk:str='', retry_times:int=10):
  23. self.ssid = ssid
  24. self.psk = psk
  25. self.retry_times = retry_times
  26. def joinThreadNetwork(dut:IdfDut, thread:thread_parameter) -> None:
  27. if thread.dataset != '':
  28. command = 'dataset set active ' + thread.dataset
  29. execute_command(dut, command)
  30. dut.expect('Done', timeout=5)
  31. else:
  32. execute_command(dut, 'dataset init new')
  33. dut.expect('Done', timeout=5)
  34. execute_command(dut, 'dataset commit active')
  35. dut.expect('Done', timeout=5)
  36. if thread.channel != '':
  37. command = 'channel ' + thread.channel
  38. execute_command(dut, command)
  39. dut.expect('Done', timeout=5)
  40. if thread.exaddr != '':
  41. command = 'extaddr ' + thread.exaddr
  42. execute_command(dut, command)
  43. dut.expect('Done', timeout=5)
  44. if thread.bbr:
  45. execute_command(dut, 'bbr enable')
  46. dut.expect('Done', timeout=5)
  47. if thread.deviceRole == 'router':
  48. execute_command(dut, 'routerselectionjitter 1')
  49. dut.expect('Done', timeout=5)
  50. execute_command(dut, 'ifconfig up')
  51. dut.expect('Done', timeout=5)
  52. execute_command(dut, 'thread start')
  53. assert wait_for_join(dut, thread.deviceRole)
  54. def wait_for_join(dut:IdfDut, role:str) -> bool:
  55. for _ in range(1, 30):
  56. if getDeviceRole(dut) == role:
  57. wait(dut, 5)
  58. return True
  59. wait(dut, 1)
  60. return False
  61. def joinWiFiNetwork(dut:IdfDut, wifi:wifi_parameter) -> Tuple[str, int]:
  62. clean_buffer(dut)
  63. ip_address = ''
  64. information = ''
  65. for order in range(1, wifi.retry_times):
  66. command = 'wifi connect -s ' + str(wifi.ssid) + ' -p ' + str(wifi.psk)
  67. tmp = get_ouput_string(dut, command, 10)
  68. if 'sta ip' in str(tmp):
  69. ip_address = re.findall(r'sta ip: (\w+.\w+.\w+.\w+),', str(tmp))[0]
  70. if 'wifi sta' in str(tmp):
  71. information = re.findall(r'wifi sta (\w+ \w+ \w+)\W', str(tmp))[0]
  72. if information == 'is connected successfully':
  73. break
  74. assert information == 'is connected successfully'
  75. return ip_address, order
  76. def getDeviceRole(dut:IdfDut) -> str:
  77. clean_buffer(dut)
  78. execute_command(dut, 'state')
  79. role = dut.expect(r'\W+(\w+)\W+Done', timeout=5)[1].decode()
  80. print(role)
  81. return str(role)
  82. def changeDeviceRole(dut:IdfDut, role:str) -> None:
  83. command = 'state ' + role
  84. execute_command(dut, command)
  85. def getDataset(dut:IdfDut) -> str:
  86. clean_buffer(dut)
  87. execute_command(dut, 'dataset active -x')
  88. dut_data = dut.expect(r'\n(\w{212})\r', timeout=5)[1].decode()
  89. return str(dut_data)
  90. def reset_thread(dut:IdfDut) -> None:
  91. dut.expect('>', timeout=10)
  92. wait(dut, 3)
  93. clean_buffer(dut)
  94. execute_command(dut, 'factoryreset')
  95. dut.expect('OpenThread attached to netif', timeout=20)
  96. dut.expect('>', timeout=10)
  97. wait(dut, 3)
  98. clean_buffer(dut)
  99. # get the mleid address of the thread
  100. def get_mleid_addr(dut:IdfDut) -> str:
  101. dut_adress = ''
  102. clean_buffer(dut)
  103. execute_command(dut, 'ipaddr mleid')
  104. dut_adress = dut.expect(r'\n((?:\w+:){7}\w+)\r', timeout=5)[1].decode()
  105. return dut_adress
  106. # get the rloc address of the thread
  107. def get_rloc_addr(dut:IdfDut) -> str:
  108. dut_adress = ''
  109. clean_buffer(dut)
  110. execute_command(dut, 'ipaddr rloc')
  111. dut_adress = dut.expect(r'\n((?:\w+:){7}\w+)\r', timeout=5)[1].decode()
  112. return dut_adress
  113. # get the linklocal address of the thread
  114. def get_linklocal_addr(dut:IdfDut) -> str:
  115. dut_adress = ''
  116. clean_buffer(dut)
  117. execute_command(dut, 'ipaddr linklocal')
  118. dut_adress = dut.expect(r'\n((?:\w+:){7}\w+)\r', timeout=5)[1].decode()
  119. return dut_adress
  120. # get the global unicast address of the thread:
  121. def get_global_unicast_addr(dut:IdfDut, br:IdfDut) -> str:
  122. dut_adress = ''
  123. clean_buffer(br)
  124. omrprefix = get_omrprefix(br)
  125. clean_buffer(dut)
  126. execute_command(dut, 'ipaddr')
  127. dut_adress = dut.expect(r'(%s(?:\w+:){3}\w+)\r' % str(omrprefix), timeout=5)[1].decode()
  128. return dut_adress
  129. # ping of thread
  130. def ot_ping(dut:IdfDut, target:str, times:int) -> Tuple[int, int]:
  131. command = 'ping ' + str(target) + ' 0 ' + str(times)
  132. execute_command(dut, command)
  133. transmitted = dut.expect(r'(\d+) packets transmitted', timeout=30)[1].decode()
  134. tx_count = int(transmitted)
  135. received = dut.expect(r'(\d+) packets received', timeout=30)[1].decode()
  136. rx_count = int(received)
  137. return tx_count, rx_count
  138. def reset_host_interface() -> None:
  139. interface_name = get_host_interface_name()
  140. flag = False
  141. try:
  142. command = 'ifconfig ' + interface_name + ' down'
  143. subprocess.call(command, shell=True, timeout=5)
  144. time.sleep(1)
  145. command = 'ifconfig ' + interface_name + ' up'
  146. subprocess.call(command, shell=True, timeout=10)
  147. time.sleep(1)
  148. flag = True
  149. finally:
  150. time.sleep(1)
  151. assert flag
  152. def set_interface_sysctl_options() -> None:
  153. interface_name = get_host_interface_name()
  154. flag = False
  155. try:
  156. command = 'sysctl -w net/ipv6/conf/' + interface_name + '/accept_ra=2'
  157. subprocess.call(command, shell=True, timeout=5)
  158. time.sleep(1)
  159. command = 'sysctl -w net/ipv6/conf/' + interface_name + '/accept_ra_rt_info_max_plen=128'
  160. subprocess.call(command, shell=True, timeout=5)
  161. time.sleep(1)
  162. flag = True
  163. finally:
  164. time.sleep(2)
  165. assert flag
  166. def init_interface_ipv6_address() -> None:
  167. interface_name = get_host_interface_name()
  168. flag = False
  169. try:
  170. command = 'ip -6 route | grep ' + interface_name + " | grep ra | awk {'print $1'} | xargs -I {} ip -6 route del {}"
  171. subprocess.call(command, shell=True, timeout=5)
  172. time.sleep(0.5)
  173. subprocess.call(command, shell=True, timeout=5)
  174. time.sleep(1)
  175. command = 'ip -6 address show dev ' + interface_name + \
  176. " scope global | grep 'inet6' | awk {'print $2'} | xargs -I {} ip -6 addr del {} dev " + interface_name
  177. subprocess.call(command, shell=True, timeout=5)
  178. time.sleep(1)
  179. flag = True
  180. finally:
  181. time.sleep(1)
  182. assert flag
  183. def get_host_interface_name() -> str:
  184. interfaces = netifaces.interfaces()
  185. interface_name = [s for s in interfaces if 'wl' in s][0]
  186. return str(interface_name)
  187. def clean_buffer(dut:IdfDut) -> None:
  188. str_length = str(len(dut.expect(pexpect.TIMEOUT, timeout=0.1)))
  189. dut.expect(r'[\s\S]{%s}' % str(str_length), timeout=10)
  190. def check_if_host_receive_ra(br:IdfDut) -> bool:
  191. interface_name = get_host_interface_name()
  192. clean_buffer(br)
  193. omrprefix = get_omrprefix(br)
  194. command = 'ip -6 route | grep ' + str(interface_name)
  195. out_str = subprocess.getoutput(command)
  196. print('br omrprefix: ', str(omrprefix))
  197. print('host route table:\n', str(out_str))
  198. return str(omrprefix) in str(out_str)
  199. def host_connect_wifi() -> None:
  200. command = '. /home/test/wlan_connection_OTTE.sh'
  201. subprocess.call(command, shell=True, timeout=30)
  202. time.sleep(5)
  203. def is_joined_wifi_network(br:IdfDut) -> bool:
  204. return check_if_host_receive_ra(br)
  205. thread_ipv6_group = 'ff04:0:0:0:0:0:0:125'
  206. def check_ipmaddr(dut:IdfDut) -> bool:
  207. info = get_ouput_string(dut, 'ipmaddr', 2)
  208. if thread_ipv6_group in str(info):
  209. return True
  210. return False
  211. def thread_is_joined_group(dut:IdfDut) -> bool:
  212. command = 'mcast join ' + thread_ipv6_group
  213. execute_command(dut, command)
  214. dut.expect('Done', timeout=5)
  215. order = 0
  216. while order < 3:
  217. if check_ipmaddr(dut):
  218. return True
  219. execute_command(dut, command)
  220. wait(dut, 2)
  221. order = order + 1
  222. return False
  223. class udp_parameter:
  224. def __init__(self, udp_type:str='', addr:str='::', port:int=5090, group:str='', init_flag:bool=False, timeout:float=15.0, udp_bytes:bytes=b''):
  225. self.udp_type = udp_type
  226. self.addr = addr
  227. self.port = port
  228. self.group = group
  229. self.init_flag = init_flag
  230. self.timeout = timeout
  231. self.udp_bytes = udp_bytes
  232. def create_host_udp_server(myudp:udp_parameter) -> None:
  233. interface_name = get_host_interface_name()
  234. try:
  235. if myudp.udp_type == 'INET6':
  236. AF_INET = socket.AF_INET6
  237. else:
  238. AF_INET = socket.AF_INET
  239. print('The host start to create udp server!')
  240. if_index = socket.if_nametoindex(interface_name)
  241. sock = socket.socket(AF_INET, socket.SOCK_DGRAM)
  242. sock.bind((myudp.addr, myudp.port))
  243. if myudp.udp_type == 'INET6' and myudp.group != '':
  244. sock.setsockopt(
  245. socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP,
  246. struct.pack('16si', socket.inet_pton(socket.AF_INET6, myudp.group),
  247. if_index))
  248. sock.settimeout(myudp.timeout)
  249. myudp.init_flag = True
  250. print('The host start to receive message!')
  251. myudp.udp_bytes = (sock.recvfrom(1024))[0]
  252. print('The host has received message: ', myudp.udp_bytes)
  253. except socket.error:
  254. print('The host did not receive message!')
  255. finally:
  256. print('Close the socket.')
  257. sock.close()
  258. def wait(dut:IdfDut, wait_time:float) -> None:
  259. dut.expect(pexpect.TIMEOUT, timeout=wait_time)
  260. def get_host_ipv4_address() -> str:
  261. interface_name = get_host_interface_name()
  262. command = 'ifconfig ' + interface_name + " | grep -w 'inet' | awk '{print $2}'"
  263. out_bytes = subprocess.check_output(command, shell=True, timeout=5)
  264. out_str = out_bytes.decode('utf-8')
  265. host_ipv4_address = ''
  266. host_ipv4_address = re.findall(r'((?:\d+.){3}\d+)', str(out_str))[0]
  267. return host_ipv4_address
  268. def start_avahi() -> None:
  269. time.sleep(1)
  270. command = '/etc/init.d/dbus start'
  271. subprocess.Popen(command, shell=True)
  272. time.sleep(5)
  273. command = 'avahi-daemon'
  274. subprocess.Popen(command, shell=True)
  275. time.sleep(5)
  276. def host_publish_service() -> None:
  277. command = 'avahi-publish-service testxxx _testxxx._udp 12347 test=1235 dn="for_ci_br_test"'
  278. subprocess.Popen(command, shell=True)
  279. time.sleep(2)
  280. def host_close_service() -> None:
  281. command = "ps | grep avahi-publish-s | awk '{print $1}'"
  282. out_bytes = subprocess.check_output(command, shell=True, timeout=5)
  283. out_str = out_bytes.decode('utf-8')
  284. the_pid = re.findall(r'(\d+)\n', str(out_str))
  285. for pid in the_pid:
  286. command = 'kill -9 ' + pid
  287. subprocess.call(command, shell=True, timeout=5)
  288. time.sleep(1)
  289. def close_host_interface() -> None:
  290. interface_name = get_host_interface_name()
  291. flag = False
  292. try:
  293. command = 'ifconfig ' + interface_name + ' down'
  294. subprocess.call(command, shell=True, timeout=5)
  295. time.sleep(1)
  296. flag = True
  297. finally:
  298. time.sleep(1)
  299. assert flag
  300. def open_host_interface() -> None:
  301. interface_name = get_host_interface_name()
  302. flag = False
  303. try:
  304. command = 'ifconfig ' + interface_name + ' up'
  305. subprocess.call(command, shell=True, timeout=5)
  306. time.sleep(1)
  307. flag = True
  308. finally:
  309. time.sleep(1)
  310. assert flag
  311. def get_domain() -> str:
  312. hostname = socket.gethostname()
  313. print('hostname is: ', hostname)
  314. command = 'ps -aux | grep avahi-daemon | grep running'
  315. out_str = subprocess.getoutput(command)
  316. print('avahi status:\n', out_str)
  317. role = re.findall(r'\[([\w\W]+)\.local\]', str(out_str))[0]
  318. print('active host is: ', role)
  319. return str(role)
  320. class tcp_parameter:
  321. def __init__(self, tcp_type:str='', addr:str='::', port:int=12345, listen_flag:bool=False, recv_flag:bool=False, timeout:float=15.0, tcp_bytes:bytes=b''):
  322. self.tcp_type = tcp_type
  323. self.addr = addr
  324. self.port = port
  325. self.listen_flag = listen_flag
  326. self.recv_flag = recv_flag
  327. self.timeout = timeout
  328. self.tcp_bytes = tcp_bytes
  329. def create_host_tcp_server(mytcp:tcp_parameter) -> None:
  330. try:
  331. if mytcp.tcp_type == 'INET6':
  332. AF_INET = socket.AF_INET6
  333. else:
  334. AF_INET = socket.AF_INET
  335. print('The host start to create a tcp server!')
  336. sock = socket.socket(AF_INET, socket.SOCK_STREAM)
  337. sock.bind((mytcp.addr, mytcp.port))
  338. sock.listen(5)
  339. mytcp.listen_flag = True
  340. print('The tcp server is waiting for connection!')
  341. sock.settimeout(mytcp.timeout)
  342. connfd,addr = sock.accept()
  343. print('The tcp server connected with ',addr)
  344. mytcp.recv_flag = True
  345. mytcp.tcp_bytes = connfd.recv(1024)
  346. print('The tcp server has received message: ', mytcp.tcp_bytes)
  347. except socket.error:
  348. if mytcp.recv_flag:
  349. print('The tcp server did not receive message!')
  350. else:
  351. print('The tcp server fail to connect!')
  352. finally:
  353. print('Close the socket.')
  354. sock.close()
  355. def get_ipv6_from_ipv4(ipv4_address:str, br:IdfDut) -> str:
  356. clean_buffer(br)
  357. nat64prefix = get_nat64prefix(br)
  358. ipv4_find = re.findall(r'\d+', ipv4_address)
  359. ipv6_16_1 = decimal_to_hex(ipv4_find[0]) + decimal_to_hex(ipv4_find[1])
  360. ipv6_16_2 = decimal_to_hex(ipv4_find[2]) + decimal_to_hex(ipv4_find[3])
  361. ipv6_get_from_ipv4 = nat64prefix + ':' + ipv6_16_1 + ':' + ipv6_16_2
  362. return str(ipv6_get_from_ipv4)
  363. def decimal_to_hex(decimal_str:str) -> str:
  364. decimal_int = int(decimal_str)
  365. hex_str = hex(decimal_int)[2:]
  366. return hex_str
  367. def get_omrprefix(br:IdfDut) -> str:
  368. clean_buffer(br)
  369. execute_command(br, 'br omrprefix')
  370. omrprefix = br.expect(r'Local: ((?:\w+:){4}):/\d+\r', timeout=5)[1].decode()
  371. return str(omrprefix)
  372. def get_onlinkprefix(br:IdfDut) -> str:
  373. clean_buffer(br)
  374. execute_command(br, 'br onlinkprefix')
  375. onlinkprefix = br.expect(r'Local: ((?:\w+:){4}):/\d+\r', timeout=5)[1].decode()
  376. return str(onlinkprefix)
  377. def get_nat64prefix(br:IdfDut) -> str:
  378. clean_buffer(br)
  379. execute_command(br, 'br nat64prefix')
  380. nat64prefix = br.expect(r'Local: ((?:\w+:){6}):/\d+', timeout=5)[1].decode()
  381. return str(nat64prefix)
  382. def execute_command(dut:IdfDut, command:str) -> None:
  383. dut.write(command)
  384. dut.expect(command, timeout=3)
  385. def get_ouput_string(dut:IdfDut, command:str, wait_time:int) -> str:
  386. clean_buffer(dut)
  387. execute_command(dut, command)
  388. tmp = dut.expect(pexpect.TIMEOUT, timeout=wait_time)
  389. clean_buffer(dut)
  390. return str(tmp)