example_test.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. import re
  2. import os
  3. import struct
  4. import socket
  5. from threading import Thread
  6. import ssl
  7. from tiny_test_fw import DUT
  8. import ttfw_idf
  9. import random
  10. import subprocess
  11. try:
  12. import BaseHTTPServer
  13. from SimpleHTTPServer import SimpleHTTPRequestHandler
  14. except ImportError:
  15. import http.server as BaseHTTPServer
  16. from http.server import SimpleHTTPRequestHandler
  17. server_cert = '-----BEGIN CERTIFICATE-----\n' \
  18. 'MIIDWDCCAkACCQCbF4+gVh/MLjANBgkqhkiG9w0BAQsFADBuMQswCQYDVQQGEwJJ\n'\
  19. 'TjELMAkGA1UECAwCTUgxDDAKBgNVBAcMA1BVTjEMMAoGA1UECgwDRVNQMQwwCgYD\n'\
  20. 'VQQLDANFU1AxDDAKBgNVBAMMA0VTUDEaMBgGCSqGSIb3DQEJARYLZXNwQGVzcC5j\n'\
  21. 'b20wHhcNMjEwNzEyMTIzNjI3WhcNNDEwNzA3MTIzNjI3WjBuMQswCQYDVQQGEwJJ\n'\
  22. 'TjELMAkGA1UECAwCTUgxDDAKBgNVBAcMA1BVTjEMMAoGA1UECgwDRVNQMQwwCgYD\n'\
  23. 'VQQLDANFU1AxDDAKBgNVBAMMA0VTUDEaMBgGCSqGSIb3DQEJARYLZXNwQGVzcC5j\n'\
  24. 'b20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDhxF/y7bygndxPwiWL\n'\
  25. 'SwS9LY3uBMaJgup0ufNKVhx+FhGQOu44SghuJAaH3KkPUnt6SOM8jC97/yQuc32W\n'\
  26. 'ukI7eBZoA12kargSnzdv5m5rZZpd+NznSSpoDArOAONKVlzr25A1+aZbix2mKRbQ\n'\
  27. 'S5w9o1N2BriQuSzd8gL0Y0zEk3VkOWXEL+0yFUT144HnErnD+xnJtHe11yPO2fEz\n'\
  28. 'YaGiilh0ddL26PXTugXMZN/8fRVHP50P2OG0SvFpC7vghlLp4VFM1/r3UJnvL6Oz\n'\
  29. '3ALc6dhxZEKQucqlpj8l1UegszQToopemtIj0qXTHw2+uUnkUyWIPjPC+wdOAoap\n'\
  30. 'rFTRAgMBAAEwDQYJKoZIhvcNAQELBQADggEBAItw24y565k3C/zENZlxyzto44ud\n'\
  31. 'IYPQXN8Fa2pBlLe1zlSIyuaA/rWQ+i1daS8nPotkCbWZyf5N8DYaTE4B0OfvoUPk\n'\
  32. 'B5uGDmbuk6akvlB5BGiYLfQjWHRsK9/4xjtIqN1H58yf3QNROuKsPAeywWS3Fn32\n'\
  33. '3//OpbWaClQePx6udRYMqAitKR+QxL7/BKZQsX+UyShuq8hjphvXvk0BW8ONzuw9\n'\
  34. 'RcoORxM0FzySYjeQvm4LhzC/P3ZBhEq0xs55aL2a76SJhq5hJy7T/Xz6NFByvlrN\n'\
  35. 'lFJJey33KFrAf5vnV9qcyWFIo7PYy2VsaaEjFeefr7q3sTFSMlJeadexW2Y=\n'\
  36. '-----END CERTIFICATE-----\n'
  37. server_key = '-----BEGIN PRIVATE KEY-----\n'\
  38. 'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDhxF/y7bygndxP\n'\
  39. 'wiWLSwS9LY3uBMaJgup0ufNKVhx+FhGQOu44SghuJAaH3KkPUnt6SOM8jC97/yQu\n'\
  40. 'c32WukI7eBZoA12kargSnzdv5m5rZZpd+NznSSpoDArOAONKVlzr25A1+aZbix2m\n'\
  41. 'KRbQS5w9o1N2BriQuSzd8gL0Y0zEk3VkOWXEL+0yFUT144HnErnD+xnJtHe11yPO\n'\
  42. '2fEzYaGiilh0ddL26PXTugXMZN/8fRVHP50P2OG0SvFpC7vghlLp4VFM1/r3UJnv\n'\
  43. 'L6Oz3ALc6dhxZEKQucqlpj8l1UegszQToopemtIj0qXTHw2+uUnkUyWIPjPC+wdO\n'\
  44. 'AoaprFTRAgMBAAECggEAE0HCxV/N1Q1h+1OeDDGL5+74yjKSFKyb/vTVcaPCrmaH\n'\
  45. 'fPvp0ddOvMZJ4FDMAsiQS6/n4gQ7EKKEnYmwTqj4eUYW8yxGUn3f0YbPHbZT+Mkj\n'\
  46. 'z5woi3nMKi/MxCGDQZX4Ow3xUQlITUqibsfWcFHis8c4mTqdh4qj7xJzehD2PVYF\n'\
  47. 'gNHZsvVj6MltjBDAVwV1IlGoHjuElm6vuzkfX7phxcA1B4ZqdYY17yCXUnvui46z\n'\
  48. 'Xn2kUTOOUCEgfgvGa9E+l4OtdXi5IxjaSraU+dlg2KsE4TpCuN2MEVkeR5Ms3Y7Q\n'\
  49. 'jgJl8vlNFJDQpbFukLcYwG7rO5N5dQ6WWfVia/5XgQKBgQD74at/bXAPrh9NxPmz\n'\
  50. 'i1oqCHMDoM9sz8xIMZLF9YVu3Jf8ux4xVpRSnNy5RU1gl7ZXbpdgeIQ4v04zy5aw\n'\
  51. '8T4tu9K3XnR3UXOy25AK0q+cnnxZg3kFQm+PhtOCKEFjPHrgo2MUfnj+EDddod7N\n'\
  52. 'JQr9q5rEFbqHupFPpWlqCa3QmQKBgQDldWUGokNaEpmgHDMnHxiibXV5LQhzf8Rq\n'\
  53. 'gJIQXb7R9EsTSXEvsDyqTBb7PHp2Ko7rZ5YQfyf8OogGGjGElnPoU/a+Jij1gVFv\n'\
  54. 'kZ064uXAAISBkwHdcuobqc5EbG3ceyH46F+FBFhqM8KcbxJxx08objmh58+83InN\n'\
  55. 'P9Qr25Xw+QKBgEGXMHuMWgQbSZeM1aFFhoMvlBO7yogBTKb4Ecpu9wI5e3Kan3Al\n'\
  56. 'pZYltuyf+VhP6XG3IMBEYdoNJyYhu+nzyEdMg8CwXg+8LC7FMis/Ve+o7aS5scgG\n'\
  57. '1to/N9DK/swCsdTRdzmc/ZDbVC+TuVsebFBGYZTyO5KgqLpezqaIQrTxAoGALFCU\n'\
  58. '10glO9MVyl9H3clap5v+MQ3qcOv/EhaMnw6L2N6WVT481tnxjW4ujgzrFcE4YuxZ\n'\
  59. 'hgwYu9TOCmeqopGwBvGYWLbj+C4mfSahOAs0FfXDoYazuIIGBpuv03UhbpB1Si4O\n'\
  60. 'rJDfRnuCnVWyOTkl54gKJ2OusinhjztBjcrV1XkCgYEA3qNi4uBsPdyz9BZGb/3G\n'\
  61. 'rOMSw0CaT4pEMTLZqURmDP/0hxvTk1polP7O/FYwxVuJnBb6mzDa0xpLFPTpIAnJ\n'\
  62. 'YXB8xpXU69QVh+EBbemdJWOd+zp5UCfXvb2shAeG3Tn/Dz4cBBMEUutbzP+or0nG\n'\
  63. 'vSXnRLaxQhooWm+IuX9SuBQ=\n'\
  64. '-----END PRIVATE KEY-----\n'
  65. def get_my_ip():
  66. s1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  67. s1.connect(("8.8.8.8", 80))
  68. my_ip = s1.getsockname()[0]
  69. s1.close()
  70. return my_ip
  71. def get_server_status(host_ip, port):
  72. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  73. server_status = sock.connect_ex((host_ip, port))
  74. sock.close()
  75. if server_status == 0:
  76. return True
  77. return False
  78. def create_file(server_file, file_data):
  79. with open(server_file, "w+") as file:
  80. file.write(file_data)
  81. def get_ca_cert(ota_image_dir):
  82. os.chdir(ota_image_dir)
  83. server_file = os.path.join(ota_image_dir, "server_cert.pem")
  84. create_file(server_file, server_cert)
  85. key_file = os.path.join(ota_image_dir, "server_key.pem")
  86. create_file(key_file, server_key)
  87. return server_file, key_file
  88. def https_request_handler():
  89. """
  90. Returns a request handler class that handles broken pipe exception
  91. """
  92. class RequestHandler(SimpleHTTPRequestHandler):
  93. def finish(self):
  94. try:
  95. if not self.wfile.closed:
  96. self.wfile.flush()
  97. self.wfile.close()
  98. except socket.error:
  99. pass
  100. self.rfile.close()
  101. def handle(self):
  102. try:
  103. BaseHTTPServer.BaseHTTPRequestHandler.handle(self)
  104. except socket.error:
  105. pass
  106. return RequestHandler
  107. def start_https_server(ota_image_dir, server_ip, server_port):
  108. server_file, key_file = get_ca_cert(ota_image_dir)
  109. requestHandler = https_request_handler()
  110. httpd = BaseHTTPServer.HTTPServer((server_ip, server_port), requestHandler)
  111. httpd.socket = ssl.wrap_socket(httpd.socket,
  112. keyfile=key_file,
  113. certfile=server_file, server_side=True)
  114. httpd.serve_forever()
  115. def start_chunked_server(ota_image_dir, server_port):
  116. server_file, key_file = get_ca_cert(ota_image_dir)
  117. chunked_server = subprocess.Popen(["openssl", "s_server", "-WWW", "-key", key_file, "-cert", server_file, "-port", str(server_port)])
  118. return chunked_server
  119. def redirect_handler_factory(url):
  120. """
  121. Returns a request handler class that redirects to supplied `url`
  122. """
  123. class RedirectHandler(SimpleHTTPRequestHandler):
  124. def do_GET(self):
  125. print("Sending resp, URL: " + url)
  126. self.send_response(301)
  127. self.send_header('Location', url)
  128. self.end_headers()
  129. def handle(self):
  130. try:
  131. BaseHTTPServer.BaseHTTPRequestHandler.handle(self)
  132. except socket.error:
  133. pass
  134. return RedirectHandler
  135. def start_redirect_server(ota_image_dir, server_ip, server_port, redirection_port):
  136. os.chdir(ota_image_dir)
  137. server_file, key_file = get_ca_cert(ota_image_dir)
  138. redirectHandler = redirect_handler_factory("https://" + server_ip + ":" + str(redirection_port) + "/advanced_https_ota.bin")
  139. httpd = BaseHTTPServer.HTTPServer((server_ip, server_port),
  140. redirectHandler)
  141. httpd.socket = ssl.wrap_socket(httpd.socket,
  142. keyfile=key_file,
  143. certfile=server_file, server_side=True)
  144. httpd.serve_forever()
  145. @ttfw_idf.idf_example_test(env_tag="Example_WIFI")
  146. def test_examples_protocol_advanced_https_ota_example(env, extra_data):
  147. """
  148. This is a positive test case, which downloads complete binary file multiple number of times.
  149. Number of iterations can be specified in variable iterations.
  150. steps: |
  151. 1. join AP
  152. 2. Fetch OTA image over HTTPS
  153. 3. Reboot with the new OTA image
  154. """
  155. dut1 = env.get_dut("advanced_https_ota_example", "examples/system/ota/advanced_https_ota", dut_class=ttfw_idf.ESP32DUT)
  156. # Number of iterations to validate OTA
  157. iterations = 3
  158. server_port = 8001
  159. # File to be downloaded. This file is generated after compilation
  160. bin_name = "advanced_https_ota.bin"
  161. # check and log bin size
  162. binary_file = os.path.join(dut1.app.binary_path, bin_name)
  163. bin_size = os.path.getsize(binary_file)
  164. ttfw_idf.log_performance("advanced_https_ota_bin_size", "{}KB".format(bin_size // 1024))
  165. ttfw_idf.check_performance("advanced_https_ota_bin_size", bin_size // 1024, dut1.TARGET)
  166. # start test
  167. host_ip = get_my_ip()
  168. if (get_server_status(host_ip, server_port) is False):
  169. thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
  170. thread1.daemon = True
  171. thread1.start()
  172. dut1.start_app()
  173. for i in range(iterations):
  174. dut1.expect("Loaded app from partition at offset", timeout=30)
  175. try:
  176. ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30)
  177. print("Connected to AP with IP: {}".format(ip_address))
  178. except DUT.ExpectTimeout:
  179. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  180. thread1.close()
  181. dut1.expect("Starting Advanced OTA example", timeout=30)
  182. print("writing to device: {}".format("https://" + host_ip + ":" + str(server_port) + "/" + bin_name))
  183. dut1.write("https://" + host_ip + ":" + str(server_port) + "/" + bin_name)
  184. dut1.expect("Loaded app from partition at offset", timeout=60)
  185. dut1.expect("Starting Advanced OTA example", timeout=30)
  186. dut1.reset()
  187. @ttfw_idf.idf_example_test(env_tag="Example_WIFI")
  188. def test_examples_protocol_advanced_https_ota_example_truncated_bin(env, extra_data):
  189. """
  190. Working of OTA if binary file is truncated is validated in this test case.
  191. Application should return with error message in this case.
  192. steps: |
  193. 1. join AP
  194. 2. Generate truncated binary file
  195. 3. Fetch OTA image over HTTPS
  196. 4. Check working of code if bin is truncated
  197. """
  198. dut1 = env.get_dut("advanced_https_ota_example", "examples/system/ota/advanced_https_ota", dut_class=ttfw_idf.ESP32DUT)
  199. server_port = 8001
  200. # Original binary file generated after compilation
  201. bin_name = "advanced_https_ota.bin"
  202. # Truncated binary file to be generated from original binary file
  203. truncated_bin_name = "truncated.bin"
  204. # Size of truncated file to be grnerated. This value can range from 288 bytes (Image header size) to size of original binary file
  205. # truncated_bin_size is set to 64000 to reduce consumed by the test case
  206. truncated_bin_size = 64000
  207. # check and log bin size
  208. binary_file = os.path.join(dut1.app.binary_path, bin_name)
  209. f = open(binary_file, "rb+")
  210. fo = open(os.path.join(dut1.app.binary_path, truncated_bin_name), "wb+")
  211. fo.write(f.read(truncated_bin_size))
  212. fo.close()
  213. f.close()
  214. binary_file = os.path.join(dut1.app.binary_path, truncated_bin_name)
  215. bin_size = os.path.getsize(binary_file)
  216. ttfw_idf.log_performance("advanced_https_ota_bin_size", "{}KB".format(bin_size // 1024))
  217. ttfw_idf.check_performance("advanced_https_ota_bin_size", bin_size // 1024, dut1.TARGET)
  218. # start test
  219. host_ip = get_my_ip()
  220. if (get_server_status(host_ip, server_port) is False):
  221. thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
  222. thread1.daemon = True
  223. thread1.start()
  224. dut1.start_app()
  225. dut1.expect("Loaded app from partition at offset", timeout=30)
  226. try:
  227. ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30)
  228. print("Connected to AP with IP: {}".format(ip_address))
  229. except DUT.ExpectTimeout:
  230. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  231. dut1.expect("Starting Advanced OTA example", timeout=30)
  232. print("writing to device: {}".format("https://" + host_ip + ":" + str(server_port) + "/" + truncated_bin_name))
  233. dut1.write("https://" + host_ip + ":" + str(server_port) + "/" + truncated_bin_name)
  234. dut1.expect("Image validation failed, image is corrupted", timeout=30)
  235. os.remove(binary_file)
  236. @ttfw_idf.idf_example_test(env_tag="Example_WIFI")
  237. def test_examples_protocol_advanced_https_ota_example_truncated_header(env, extra_data):
  238. """
  239. Working of OTA if headers of binary file are truncated is vaildated in this test case.
  240. Application should return with error message in this case.
  241. steps: |
  242. 1. join AP
  243. 2. Generate binary file with truncated headers
  244. 3. Fetch OTA image over HTTPS
  245. 4. Check working of code if headers are not sent completely
  246. """
  247. dut1 = env.get_dut("advanced_https_ota_example", "examples/system/ota/advanced_https_ota", dut_class=ttfw_idf.ESP32DUT)
  248. server_port = 8001
  249. # Original binary file generated after compilation
  250. bin_name = "advanced_https_ota.bin"
  251. # Truncated binary file to be generated from original binary file
  252. truncated_bin_name = "truncated_header.bin"
  253. # Size of truncated file to be grnerated. This value should be less than 288 bytes (Image header size)
  254. truncated_bin_size = 180
  255. # check and log bin size
  256. binary_file = os.path.join(dut1.app.binary_path, bin_name)
  257. f = open(binary_file, "rb+")
  258. fo = open(os.path.join(dut1.app.binary_path, truncated_bin_name), "wb+")
  259. fo.write(f.read(truncated_bin_size))
  260. fo.close()
  261. f.close()
  262. binary_file = os.path.join(dut1.app.binary_path, truncated_bin_name)
  263. bin_size = os.path.getsize(binary_file)
  264. ttfw_idf.log_performance("advanced_https_ota_bin_size", "{}KB".format(bin_size // 1024))
  265. ttfw_idf.check_performance("advanced_https_ota_bin_size", bin_size // 1024, dut1.TARGET)
  266. # start test
  267. host_ip = get_my_ip()
  268. if (get_server_status(host_ip, server_port) is False):
  269. thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
  270. thread1.daemon = True
  271. thread1.start()
  272. dut1.start_app()
  273. dut1.expect("Loaded app from partition at offset", timeout=30)
  274. try:
  275. ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30)
  276. print("Connected to AP with IP: {}".format(ip_address))
  277. except DUT.ExpectTimeout:
  278. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  279. dut1.expect("Starting Advanced OTA example", timeout=30)
  280. print("writing to device: {}".format("https://" + host_ip + ":" + str(server_port) + "/" + truncated_bin_name))
  281. dut1.write("https://" + host_ip + ":" + str(server_port) + "/" + truncated_bin_name)
  282. dut1.expect("advanced_https_ota_example: esp_https_ota_read_img_desc failed", timeout=30)
  283. os.remove(binary_file)
  284. @ttfw_idf.idf_example_test(env_tag="Example_WIFI")
  285. def test_examples_protocol_advanced_https_ota_example_random(env, extra_data):
  286. """
  287. Working of OTA if random data is added in binary file are validated in this test case.
  288. Magic byte verification should fail in this case.
  289. steps: |
  290. 1. join AP
  291. 2. Generate random binary image
  292. 3. Fetch OTA image over HTTPS
  293. 4. Check working of code for random binary file
  294. """
  295. dut1 = env.get_dut("advanced_https_ota_example", "examples/system/ota/advanced_https_ota", dut_class=ttfw_idf.ESP32DUT)
  296. server_port = 8001
  297. # Random binary file to be generated
  298. random_bin_name = "random.bin"
  299. # Size of random binary file. 32000 is choosen, to reduce the time required to run the test-case
  300. random_bin_size = 32000
  301. # check and log bin size
  302. binary_file = os.path.join(dut1.app.binary_path, random_bin_name)
  303. fo = open(binary_file, "wb+")
  304. # First byte of binary file is always set to zero. If first byte is generated randomly,
  305. # in some cases it may generate 0xE9 which will result in failure of testcase.
  306. fo.write(struct.pack("B", 0))
  307. for i in range(random_bin_size - 1):
  308. fo.write(struct.pack("B", random.randrange(0,255,1)))
  309. fo.close()
  310. bin_size = os.path.getsize(binary_file)
  311. ttfw_idf.log_performance("advanced_https_ota_bin_size", "{}KB".format(bin_size // 1024))
  312. ttfw_idf.check_performance("advanced_https_ota_bin_size", bin_size // 1024, dut1.TARGET)
  313. # start test
  314. host_ip = get_my_ip()
  315. if (get_server_status(host_ip, server_port) is False):
  316. thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
  317. thread1.daemon = True
  318. thread1.start()
  319. dut1.start_app()
  320. dut1.expect("Loaded app from partition at offset", timeout=30)
  321. try:
  322. ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30)
  323. print("Connected to AP with IP: {}".format(ip_address))
  324. except DUT.ExpectTimeout:
  325. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  326. dut1.expect("Starting Advanced OTA example", timeout=30)
  327. print("writing to device: {}".format("https://" + host_ip + ":" + str(server_port) + "/" + random_bin_name))
  328. dut1.write("https://" + host_ip + ":" + str(server_port) + "/" + random_bin_name)
  329. dut1.expect("esp_ota_ops: OTA image has invalid magic byte", timeout=10)
  330. os.remove(binary_file)
  331. @ttfw_idf.idf_example_test(env_tag="Example_WIFI")
  332. def test_examples_protocol_advanced_https_ota_example_chunked(env, extra_data):
  333. """
  334. This is a positive test case, which downloads complete binary file multiple number of times.
  335. Number of iterations can be specified in variable iterations.
  336. steps: |
  337. 1. join AP
  338. 2. Fetch OTA image over HTTPS
  339. 3. Reboot with the new OTA image
  340. """
  341. dut1 = env.get_dut("advanced_https_ota_example", "examples/system/ota/advanced_https_ota", dut_class=ttfw_idf.ESP32DUT)
  342. # File to be downloaded. This file is generated after compilation
  343. bin_name = "advanced_https_ota.bin"
  344. # check and log bin size
  345. binary_file = os.path.join(dut1.app.binary_path, bin_name)
  346. bin_size = os.path.getsize(binary_file)
  347. ttfw_idf.log_performance("advanced_https_ota_bin_size", "{}KB".format(bin_size // 1024))
  348. ttfw_idf.check_performance("advanced_https_ota_bin_size", bin_size // 1024, dut1.TARGET)
  349. # start test
  350. host_ip = get_my_ip()
  351. chunked_server = start_chunked_server(dut1.app.binary_path, 8070)
  352. dut1.start_app()
  353. dut1.expect("Loaded app from partition at offset", timeout=30)
  354. try:
  355. ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30)
  356. print("Connected to AP with IP: {}".format(ip_address))
  357. except DUT.ExpectTimeout:
  358. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  359. dut1.expect("Starting Advanced OTA example", timeout=30)
  360. print("writing to device: {}".format("https://" + host_ip + ":8070/" + bin_name))
  361. dut1.write("https://" + host_ip + ":8070/" + bin_name)
  362. dut1.expect("Loaded app from partition at offset", timeout=60)
  363. dut1.expect("Starting Advanced OTA example", timeout=30)
  364. chunked_server.kill()
  365. os.remove(os.path.join(dut1.app.binary_path, "server_cert.pem"))
  366. os.remove(os.path.join(dut1.app.binary_path, "server_key.pem"))
  367. @ttfw_idf.idf_example_test(env_tag="Example_WIFI")
  368. def test_examples_protocol_advanced_https_ota_example_redirect_url(env, extra_data):
  369. """
  370. This is a positive test case, which starts a server and a redirection server.
  371. Redirection server redirects http_request to different port
  372. Number of iterations can be specified in variable iterations.
  373. steps: |
  374. 1. join AP
  375. 2. Fetch OTA image over HTTPS
  376. 3. Reboot with the new OTA image
  377. """
  378. dut1 = env.get_dut("advanced_https_ota_example", "examples/system/ota/advanced_https_ota", dut_class=ttfw_idf.ESP32DUT)
  379. server_port = 8001
  380. # Port to which the request should be redirecetd
  381. redirection_server_port = 8081
  382. # File to be downloaded. This file is generated after compilation
  383. bin_name = "advanced_https_ota.bin"
  384. # check and log bin size
  385. binary_file = os.path.join(dut1.app.binary_path, bin_name)
  386. bin_size = os.path.getsize(binary_file)
  387. ttfw_idf.log_performance("advanced_https_ota_bin_size", "{}KB".format(bin_size // 1024))
  388. ttfw_idf.check_performance("advanced_https_ota_bin_size", bin_size // 1024, dut1.TARGET)
  389. # start test
  390. host_ip = get_my_ip()
  391. if (get_server_status(host_ip, server_port) is False):
  392. thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
  393. thread1.daemon = True
  394. thread1.start()
  395. thread2 = Thread(target=start_redirect_server, args=(dut1.app.binary_path, host_ip, redirection_server_port, server_port))
  396. thread2.daemon = True
  397. thread2.start()
  398. dut1.start_app()
  399. dut1.expect("Loaded app from partition at offset", timeout=30)
  400. try:
  401. ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30)
  402. print("Connected to AP with IP: {}".format(ip_address))
  403. except DUT.ExpectTimeout:
  404. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  405. thread1.close()
  406. thread2.close()
  407. dut1.expect("Starting Advanced OTA example", timeout=30)
  408. print("writing to device: {}".format("https://" + host_ip + ":" + str(redirection_server_port) + "/" + bin_name))
  409. dut1.write("https://" + host_ip + ":" + str(redirection_server_port) + "/" + bin_name)
  410. dut1.expect("Loaded app from partition at offset", timeout=60)
  411. dut1.expect("Starting Advanced OTA example", timeout=30)
  412. dut1.reset()
  413. if __name__ == '__main__':
  414. test_examples_protocol_advanced_https_ota_example()
  415. test_examples_protocol_advanced_https_ota_example_chunked()
  416. test_examples_protocol_advanced_https_ota_example_redirect_url()
  417. test_examples_protocol_advanced_https_ota_example_truncated_bin()
  418. test_examples_protocol_advanced_https_ota_example_truncated_header()
  419. test_examples_protocol_advanced_https_ota_example_random()