example_test.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import http.server
  2. import multiprocessing
  3. import os
  4. import re
  5. import socket
  6. import ssl
  7. import ttfw_idf
  8. from RangeHTTPServer import RangeRequestHandler
  9. from tiny_test_fw import DUT, Utility
  10. def get_my_ip():
  11. s1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  12. s1.connect(('8.8.8.8', 80))
  13. my_ip = s1.getsockname()[0]
  14. s1.close()
  15. return my_ip
  16. def get_server_status(host_ip, port):
  17. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  18. server_status = sock.connect_ex((host_ip, port))
  19. sock.close()
  20. if server_status == 0:
  21. return True
  22. return False
  23. def https_request_handler():
  24. """
  25. Returns a request handler class that handles broken pipe exception
  26. """
  27. class RequestHandler(RangeRequestHandler):
  28. protocol_version = 'HTTP/1.1'
  29. def finish(self):
  30. try:
  31. if not self.wfile.closed:
  32. self.wfile.flush()
  33. self.wfile.close()
  34. except socket.error:
  35. pass
  36. self.rfile.close()
  37. def handle(self):
  38. try:
  39. RangeRequestHandler.handle(self)
  40. except socket.error:
  41. pass
  42. def do_GET(self):
  43. self.close_connection = True
  44. self.send_response(200)
  45. self.end_headers()
  46. return RequestHandler
  47. def start_https_server(server_file, key_file, server_ip, server_port):
  48. requestHandler = https_request_handler()
  49. httpd = http.server.HTTPServer((server_ip, server_port), requestHandler)
  50. httpd.socket = ssl.wrap_socket(httpd.socket, keyfile=key_file,
  51. certfile=server_file, server_side=True)
  52. httpd.serve_forever()
  53. @ttfw_idf.idf_example_test(env_tag='Example_EthKitV1')
  54. def test_examples_protocol_https_request_cli_session_tickets(env, extra_data):
  55. Utility.console_log("Testing for \"esp_tls client session tickets\"")
  56. dut1 = env.get_dut('https_request_ses_tkt', 'examples/protocols/https_request', dut_class=ttfw_idf.ESP32DUT, app_config_name='cli_ses_tkt')
  57. Utility.console_log('[app_config_name] - {}'.format(dut1.app.config_name))
  58. # check and log bin size
  59. binary_file = os.path.join(dut1.app.binary_path, 'https_request.bin')
  60. bin_size = os.path.getsize(binary_file)
  61. ttfw_idf.log_performance('https_request_bin_size', '{}KB'.format(bin_size // 1024))
  62. # start test
  63. host_ip = get_my_ip()
  64. server_port = 8070
  65. server_file = os.path.join(os.path.dirname(__file__), 'main', 'local_server_cert.pem')
  66. key_file = os.path.join(os.path.dirname(__file__), 'main', 'local_server_key.pem')
  67. if (get_server_status(host_ip, server_port) is False):
  68. thread1 = multiprocessing.Process(target=start_https_server, args=(server_file, key_file, host_ip, server_port))
  69. thread1.daemon = True
  70. thread1.start()
  71. Utility.console_log('The server started on {}:{}'.format(host_ip, server_port))
  72. dut1.start_app()
  73. dut1.expect('Loaded app from partition at offset', timeout=30)
  74. try:
  75. ip_address = dut1.expect(re.compile(r' (sta|eth) ip: ([^,]+),'), timeout=60)
  76. print('Connected to AP with IP: {}'.format(ip_address))
  77. except DUT.ExpectTimeout:
  78. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  79. dut1.expect('Start https_request example', timeout=30)
  80. print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port)))
  81. dut1.write('https://' + host_ip + ':' + str(server_port))
  82. Utility.console_log("Testing for \"https_request using saved session\"")
  83. # Check for connection using already saved client session
  84. try:
  85. dut1.expect(re.compile('https_request to local server'), timeout=30)
  86. dut1.expect_all('Connection established...',
  87. 'Reading HTTP response...',
  88. 'HTTP/1.1 200 OK',
  89. re.compile('connection closed'))
  90. except Exception:
  91. Utility.console_log("Failed to connect to local https server\"")
  92. raise
  93. try:
  94. dut1.expect(re.compile('https_request using saved client session'), timeout=20)
  95. dut1.expect_all('Connection established...',
  96. 'Reading HTTP response...',
  97. 'HTTP/1.1 200 OK',
  98. re.compile('connection closed'))
  99. except Exception:
  100. Utility.console_log("Failed the test for \"https_request using saved client session\"")
  101. raise
  102. Utility.console_log("Passed the test for \"https_request using saved client session\"")
  103. thread1.terminate()
  104. env.close_dut('https_request_ses_tkt')
  105. @ttfw_idf.idf_example_test(env_tag='Example_EthKitV1')
  106. def test_examples_protocol_https_request_dynamic_buffers(env, extra_data):
  107. # Check for connection using crt bundle with mbedtls dynamic resource enabled
  108. dut1 = env.get_dut('https_request_ssldyn', 'examples/protocols/https_request', dut_class=ttfw_idf.ESP32DUT, app_config_name='ssldyn')
  109. # check and log bin size
  110. Utility.console_log('[app_config_name] - {}'.format(dut1.app.config_name))
  111. binary_file = os.path.join(dut1.app.binary_path, 'https_request.bin')
  112. bin_size = os.path.getsize(binary_file)
  113. ttfw_idf.log_performance('https_request_bin_size', '{}KB'.format(bin_size // 1024))
  114. # start test
  115. dut1.start_app()
  116. dut1.expect('Loaded app from partition at offset', timeout=30)
  117. try:
  118. ip_address = dut1.expect(re.compile(r' (sta|eth) ip: ([^,]+),'), timeout=60)
  119. print('Connected to AP with IP: {}'.format(ip_address))
  120. except DUT.ExpectTimeout:
  121. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  122. # only check if one connection is established
  123. Utility.console_log("Testing for \"https_request using crt bundle\" with mbedtls dynamic resource enabled")
  124. try:
  125. dut1.expect(re.compile('https_request using crt bundle'), timeout=30)
  126. dut1.expect_all('Connection established...',
  127. 'Reading HTTP response...',
  128. 'HTTP/1.1 200 OK',
  129. re.compile('connection closed'))
  130. except Exception:
  131. Utility.console_log("Failed the test for \"https_request using crt bundle\" when mbedtls dynamic resource was enabled")
  132. raise
  133. Utility.console_log("Passed the test for \"https_request using crt bundle\" when mbedtls dynamic resource was enabled")
  134. # Read free heap size
  135. res = dut1.expect(ttfw_idf.MINIMUM_FREE_HEAP_SIZE_RE,timeout=20)
  136. if not res:
  137. raise ValueError('Maximum heap size info not found')
  138. ttfw_idf.print_heap_size('https_request', dut1.app.config_name, dut1.TARGET, res[0])
  139. env.close_dut('https_request_ssldyn')
  140. @ttfw_idf.idf_example_test(env_tag='Example_EthKitV1')
  141. def test_examples_protocol_https_request(env, extra_data):
  142. """
  143. steps: |
  144. 1. join AP
  145. 2. establish TLS connection to www.howsmyssl.com:443 with multiple
  146. certificate verification options
  147. 3. send http request
  148. """
  149. dut1 = env.get_dut('https_request', 'examples/protocols/https_request', dut_class=ttfw_idf.ESP32DUT)
  150. # check and log bin size
  151. Utility.console_log('[app_config_name] - {}'.format(dut1.app.config_name))
  152. binary_file = os.path.join(dut1.app.binary_path, 'https_request.bin')
  153. bin_size = os.path.getsize(binary_file)
  154. ttfw_idf.log_performance('https_request_bin_size', '{}KB'.format(bin_size // 1024))
  155. # start tes
  156. Utility.console_log('Starting https_request simple test app')
  157. dut1.start_app()
  158. dut1.expect('Loaded app from partition at offset', timeout=30)
  159. try:
  160. ip_address = dut1.expect(re.compile(r' (sta|eth) ip: ([^,]+),'), timeout=60)
  161. print('Connected to AP with IP: {}'.format(ip_address))
  162. except DUT.ExpectTimeout:
  163. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  164. # Check for connection using crt bundle
  165. Utility.console_log("Testing for \"https_request using crt bundle\"")
  166. try:
  167. dut1.expect(re.compile('https_request using crt bundle'), timeout=30)
  168. dut1.expect_all('Certificate validated',
  169. 'Connection established...',
  170. 'Reading HTTP response...',
  171. 'HTTP/1.1 200 OK',
  172. re.compile('connection closed'))
  173. except Exception:
  174. Utility.console_log("Failed the test for \"https_request using crt bundle\"")
  175. raise
  176. Utility.console_log("Passed the test for \"https_request using crt bundle\"")
  177. # Read free heap size
  178. res = dut1.expect(ttfw_idf.MINIMUM_FREE_HEAP_SIZE_RE,timeout=20)
  179. if not res:
  180. raise ValueError('Maximum heap size info not found')
  181. ttfw_idf.print_heap_size('https_request', dut1.app.config_name, dut1.TARGET, res[0])
  182. # Check for connection using cacert_buf
  183. Utility.console_log("Testing for \"https_request using cacert_buf\"")
  184. try:
  185. dut1.expect(re.compile('https_request using cacert_buf'), timeout=20)
  186. dut1.expect_all('Connection established...',
  187. 'Reading HTTP response...',
  188. 'HTTP/1.1 200 OK',
  189. re.compile('connection closed'))
  190. except Exception:
  191. Utility.console_log("Passed the test for \"https_request using cacert_buf\"")
  192. raise
  193. Utility.console_log("Passed the test for \"https_request using cacert_buf\"")
  194. # Check for connection using global ca_store
  195. Utility.console_log("Testing for \"https_request using global ca_store\"")
  196. try:
  197. dut1.expect(re.compile('https_request using global ca_store'), timeout=20)
  198. dut1.expect_all('Connection established...',
  199. 'Reading HTTP response...',
  200. 'HTTP/1.1 200 OK',
  201. re.compile('connection closed'))
  202. except Exception:
  203. Utility.console_log("Failed the test for \"https_request using global ca_store\"")
  204. raise
  205. Utility.console_log("Passed the test for \"https_request using global ca_store\"")
  206. env.close_dut('https_request')
  207. if __name__ == '__main__':
  208. test_examples_protocol_https_request()
  209. test_examples_protocol_https_request_cli_session_tickets()
  210. test_examples_protocol_https_request_dynamic_buffers()