example_test.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2021 Espressif Systems (Shanghai) PTE LTD
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. from __future__ import print_function
  17. import http.client
  18. import os
  19. import re
  20. import socket
  21. import sys
  22. import tiny_test_fw
  23. import ttfw_idf
  24. from tiny_test_fw import Utility
  25. try:
  26. import wifi_tools
  27. except ImportError:
  28. wifi_tools_path = str(os.getenv('IDF_PATH')) + '/examples/provisioning/softap_prov/utils'
  29. if wifi_tools_path and wifi_tools_path not in sys.path:
  30. sys.path.insert(0, wifi_tools_path)
  31. import wifi_tools
  32. def test_redirect(ip, port): # type: (str, str) -> str # pylint: disable=unused-argument
  33. # Establish HTTP connection
  34. sess = http.client.HTTPConnection(ip + ':' + port, timeout=15)
  35. uri = '/test'
  36. # GET response
  37. sess.request('GET', url=uri)
  38. resp = sess.getresponse()
  39. resp_hdrs = resp.getheaders()
  40. if resp.status != 302:
  41. raise RuntimeError('Redirect failed, response status: {}'.format(resp.status))
  42. for hdr in resp_hdrs:
  43. if hdr[0] == 'location':
  44. uri = hdr[1]
  45. print('Redirected to uri: {}'.format(uri))
  46. # Close HTTP connection
  47. sess.close()
  48. return uri
  49. def test_captive_page(ip, port, uri): # type: (str, str, str) -> bool # pylint: disable=unused-argument
  50. # Establish HTTP connection
  51. sess = http.client.HTTPConnection(ip + ':' + port, timeout=15)
  52. # GET response
  53. sess.request('GET', url=uri)
  54. resp = sess.getresponse()
  55. resp_data = resp.read().decode()
  56. search_str = 'Redirect to the captive portal'
  57. if search_str not in resp_data:
  58. raise RuntimeError('Failed to match {} with data from captive portal: {}'.format(search_str, resp_data))
  59. # Close HTTP connection
  60. sess.close()
  61. return True
  62. @ttfw_idf.idf_example_test(env_tag='Example_WIFI_BT', ignore=True)
  63. def test_example_captive_portal(env, extra_data): # type: (tiny_test_fw.Env.Env, None) -> None # pylint: disable=unused-argument
  64. # Acquire DUT
  65. dut1 = env.get_dut('captive_portal', 'examples/protocols/http_server/captive_portal', dut_class=ttfw_idf.ESP32DUT)
  66. # Get binary file
  67. binary_file = os.path.join(dut1.app.binary_path, 'captive_portal.bin')
  68. bin_size = os.path.getsize(binary_file)
  69. ttfw_idf.log_performance('captive_portal_bin_size', '{}KB'.format(bin_size // 1024))
  70. # Upload binary and start testing
  71. dut1.start_app()
  72. # Parse IP address of STA
  73. Utility.console_log('Waiting to connect with softAP')
  74. ap_ip = dut1.expect(re.compile(r'Set up softAP with IP: (\d+.\d+.\d+.\d+)'), timeout=60)[0]
  75. [ssid, password] = dut1.expect(re.compile(r"wifi_init_softap finished. SSID:'(\S+)' password:'(\S+)'"), timeout=30)
  76. port = dut1.expect(re.compile(r"(?:[\s\S]*)Starting server on port: '(\d+)'"), timeout=30)[0]
  77. iface = wifi_tools.get_wiface_name()
  78. if iface is None:
  79. raise RuntimeError('Failed to get Wi-Fi interface on host')
  80. print('Interface name : ' + iface)
  81. print('SoftAP SSID : ' + ssid)
  82. print('SoftAP Password : ' + password)
  83. try:
  84. ctrl = wifi_tools.wpa_cli(iface, reset_on_exit=True)
  85. print('Connecting to DUT SoftAP...')
  86. try:
  87. ip = ctrl.connect(ssid, password)
  88. except RuntimeError as err:
  89. Utility.console_log('error: {}'.format(err))
  90. try:
  91. got_ip = dut1.expect(re.compile(r'DHCP server assigned IP to a station, IP is: (\d+.\d+.\d+.\d+)'), timeout=60)
  92. Utility.console_log('got_ip: {}'.format(got_ip))
  93. got_ip = got_ip[0]
  94. if ip != got_ip:
  95. raise RuntimeError('SoftAP connected to another host! {} != {}'.format(ip, got_ip))
  96. except tiny_test_fw.DUT.ExpectTimeout:
  97. # print what is happening on DUT side
  98. Utility.console_log('in exception tiny_test_fw.DUT.ExpectTimeout')
  99. Utility.console_log(dut1.read())
  100. raise
  101. print('Connected to DUT SoftAP')
  102. host_name = 'www.google.com'
  103. host = socket.gethostbyname(host_name)
  104. print('hostname: {} resolved to: {}'.format(host_name, host))
  105. if host != ap_ip:
  106. raise RuntimeError("DNS server failed to redirect question to the softAP's IP")
  107. uri = test_redirect(ap_ip, port)
  108. test_captive_page(ap_ip, port, uri)
  109. finally:
  110. ctrl.reset()
  111. if __name__ == '__main__':
  112. test_example_captive_portal() # pylint: disable=no-value-for-parameter