mdns_example_test.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import os
  2. import re
  3. import select
  4. import socket
  5. import struct
  6. import subprocess
  7. import time
  8. from threading import Event, Thread
  9. import dpkt
  10. import dpkt.dns
  11. import ttfw_idf
  12. from tiny_test_fw import DUT
  13. from tiny_test_fw.Utility import console_log
  14. stop_mdns_server = Event()
  15. esp_answered = Event()
  16. esp_delegated_answered = Event()
  17. def get_dns_query_for_esp(esp_host):
  18. dns = dpkt.dns.DNS(b'\x00\x00\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x01')
  19. dns.qd[0].name = esp_host + u'.local'
  20. console_log('Created query for esp host: {} '.format(dns.__repr__()))
  21. return dns.pack()
  22. def get_dns_answer_to_mdns(tester_host):
  23. dns = dpkt.dns.DNS(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
  24. dns.op = dpkt.dns.DNS_QR | dpkt.dns.DNS_AA
  25. dns.rcode = dpkt.dns.DNS_RCODE_NOERR
  26. arr = dpkt.dns.DNS.RR()
  27. arr.cls = dpkt.dns.DNS_IN
  28. arr.type = dpkt.dns.DNS_A
  29. arr.name = tester_host
  30. arr.ip = socket.inet_aton('127.0.0.1')
  31. dns. an.append(arr)
  32. console_log('Created answer to mdns query: {} '.format(dns.__repr__()))
  33. return dns.pack()
  34. def get_dns_answer_to_mdns_lwip(tester_host, id):
  35. dns = dpkt.dns.DNS(b'\x5e\x39\x84\x00\x00\x01\x00\x01\x00\x00\x00\x00\x0a\x64\x61\x76\x69\x64'
  36. b'\x2d\x63\x6f\x6d\x70\x05\x6c\x6f\x63\x61\x6c\x00\x00\x01\x00\x01\xc0\x0c'
  37. b'\x00\x01\x00\x01\x00\x00\x00\x0a\x00\x04\xc0\xa8\x0a\x6c')
  38. dns.qd[0].name = tester_host
  39. dns.an[0].name = tester_host
  40. dns.an[0].ip = socket.inet_aton('127.0.0.1')
  41. dns.an[0].rdata = socket.inet_aton('127.0.0.1')
  42. dns.id = id
  43. print('Created answer to mdns (lwip) query: {} '.format(dns.__repr__()))
  44. return dns.pack()
  45. def mdns_server(esp_host):
  46. global esp_answered
  47. UDP_IP = '0.0.0.0'
  48. UDP_PORT = 5353
  49. MCAST_GRP = '224.0.0.251'
  50. TESTER_NAME = u'tinytester.local'
  51. TESTER_NAME_LWIP = u'tinytester-lwip.local'
  52. QUERY_TIMEOUT = 0.2
  53. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  54. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  55. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
  56. sock.setblocking(False)
  57. sock.bind((UDP_IP,UDP_PORT))
  58. mreq = struct.pack('4sl', socket.inet_aton(MCAST_GRP), socket.INADDR_ANY)
  59. sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
  60. last_query_timepoint = time.time()
  61. while not stop_mdns_server.is_set():
  62. try:
  63. current_time = time.time()
  64. if current_time - last_query_timepoint > QUERY_TIMEOUT:
  65. last_query_timepoint = current_time
  66. if not esp_answered.is_set():
  67. sock.sendto(get_dns_query_for_esp(esp_host), (MCAST_GRP,UDP_PORT))
  68. if not esp_delegated_answered.is_set():
  69. sock.sendto(get_dns_query_for_esp(esp_host + '-delegated'), (MCAST_GRP,UDP_PORT))
  70. timeout = max(0, QUERY_TIMEOUT - (current_time - last_query_timepoint))
  71. read_socks, _, _ = select.select([sock], [], [], timeout)
  72. if not read_socks:
  73. continue
  74. data, addr = sock.recvfrom(1024)
  75. dns = dpkt.dns.DNS(data)
  76. if len(dns.qd) > 0 and dns.qd[0].type == dpkt.dns.DNS_A:
  77. if dns.qd[0].name == TESTER_NAME:
  78. console_log('Received query: {} '.format(dns.__repr__()))
  79. sock.sendto(get_dns_answer_to_mdns(TESTER_NAME), (MCAST_GRP,UDP_PORT))
  80. elif dns.qd[0].name == TESTER_NAME_LWIP:
  81. console_log('Received query: {} '.format(dns.__repr__()))
  82. sock.sendto(get_dns_answer_to_mdns_lwip(TESTER_NAME_LWIP, dns.id), addr)
  83. if len(dns.an) > 0 and dns.an[0].type == dpkt.dns.DNS_A:
  84. if dns.an[0].name == esp_host + u'.local':
  85. console_log('Received answer to esp32-mdns query: {}'.format(dns.__repr__()))
  86. esp_answered.set()
  87. if dns.an[0].name == esp_host + u'-delegated.local':
  88. console_log('Received answer to esp32-mdns-delegate query: {}'.format(dns.__repr__()))
  89. esp_delegated_answered.set()
  90. except socket.timeout:
  91. break
  92. except dpkt.UnpackError:
  93. continue
  94. @ttfw_idf.idf_example_test(env_tag='Example_WIFI_Protocols')
  95. def test_examples_protocol_mdns(env, extra_data):
  96. global stop_mdns_server
  97. """
  98. steps: |
  99. 1. join AP + init mdns example
  100. 2. get the dut host name (and IP address)
  101. 3. check the mdns name is accessible
  102. 4. check DUT output if mdns advertized host is resolved
  103. """
  104. dut1 = env.get_dut('mdns-test', 'examples/protocols/mdns', dut_class=ttfw_idf.ESP32DUT)
  105. # check and log bin size
  106. binary_file = os.path.join(dut1.app.binary_path, 'mdns_test.bin')
  107. bin_size = os.path.getsize(binary_file)
  108. ttfw_idf.log_performance('mdns-test_bin_size', '{}KB'.format(bin_size // 1024))
  109. # 1. start mdns application
  110. dut1.start_app()
  111. # 2. get the dut host name (and IP address)
  112. specific_host = dut1.expect(re.compile(r'mdns hostname set to: \[([^\]]+)\]'), timeout=30)[0]
  113. mdns_responder = Thread(target=mdns_server, args=(str(specific_host),))
  114. try:
  115. ip_address = dut1.expect(re.compile(r' sta ip: ([^,]+),'), timeout=30)[0]
  116. console_log('Connected to AP with IP: {}'.format(ip_address))
  117. except DUT.ExpectTimeout:
  118. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  119. try:
  120. # 3. check the mdns name is accessible
  121. mdns_responder.start()
  122. if not esp_answered.wait(timeout=30):
  123. raise ValueError('Test has failed: did not receive mdns answer within timeout')
  124. if not esp_delegated_answered.wait(timeout=30):
  125. raise ValueError('Test has failed: did not receive mdns answer for delegated host within timeout')
  126. # 4. check DUT output if mdns advertized host is resolved
  127. dut1.expect(re.compile(r'mdns-test: Query A: tinytester.local resolved to: 127.0.0.1'), timeout=30)
  128. dut1.expect(re.compile(r'mdns-test: gethostbyname: tinytester-lwip.local resolved to: 127.0.0.1'), timeout=30)
  129. dut1.expect(re.compile(r'mdns-test: getaddrinfo: tinytester-lwip.local resolved to: 127.0.0.1'), timeout=30)
  130. # 5. check the DUT answers to `dig` command
  131. dig_output = subprocess.check_output(['dig', '+short', '-p', '5353', '@224.0.0.251',
  132. '{}.local'.format(specific_host)])
  133. console_log('Resolving {} using "dig" succeeded with:\n{}'.format(specific_host, dig_output))
  134. if not ip_address.encode('utf-8') in dig_output:
  135. raise ValueError('Test has failed: Incorrectly resolved DUT hostname using dig'
  136. "Output should've contained DUT's IP address:{}".format(ip_address))
  137. finally:
  138. stop_mdns_server.set()
  139. mdns_responder.join()
  140. if __name__ == '__main__':
  141. test_examples_protocol_mdns()