example_test.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. import re
  2. import os
  3. import socket
  4. import http.server
  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. server_cert = "-----BEGIN CERTIFICATE-----\n" \
  12. "MIIDXTCCAkWgAwIBAgIJAP4LF7E72HakMA0GCSqGSIb3DQEBCwUAMEUxCzAJBgNV\n"\
  13. "BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX\n"\
  14. "aWRnaXRzIFB0eSBMdGQwHhcNMTkwNjA3MDk1OTE2WhcNMjAwNjA2MDk1OTE2WjBF\n"\
  15. "MQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50\n"\
  16. "ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB\n"\
  17. "CgKCAQEAlzfCyv3mIv7TlLkObxunKfCdrJ/zgdANrsx0RBtpEPhV560hWJ0fEin0\n"\
  18. "nIOMpJSiF9E6QsPdr6Q+eogH4XnOMU9JE+iG743N1dPfGEzJvRlyct/Ck8SswKPC\n"\
  19. "9+VXsnOdZmUw9y/xtANbURA/TspvPzz3Avv382ffffrJGh7ooOmaZSCZFlSYHLZA\n"\
  20. "w/XlRr0sSRbLpFGY0gXjaAV8iHHiPDYLy4kZOepjV9U51xi+IGsL4w75zuMgsHyF\n"\
  21. "3nJeGYHgtGVBrkL0ZKG5udY0wcBjysjubDJC4iSlNiq2HD3fhs7j6CZddV2v845M\n"\
  22. "lVKNxP0kO4Uj4D8r+5USWC8JKfAwxQIDAQABo1AwTjAdBgNVHQ4EFgQU6OE7ssfY\n"\
  23. "IIPTDThiUoofUpsD5NwwHwYDVR0jBBgwFoAU6OE7ssfYIIPTDThiUoofUpsD5Nww\n"\
  24. "DAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAXIlHS/FJWfmcinUAxyBd\n"\
  25. "/xd5Lu8ykeru6oaUCci+Vk9lyoMMES7lQ+b/00d5x7AcTawkTil9EWpBTPTOTraA\n"\
  26. "lzJMQhNKmSLk0iIoTtAJtSZgUSpIIozqK6lenxQQDsHbXKU6h+u9H6KZE8YcjsFl\n"\
  27. "6vL7sw9BVotw/VxfgjQ5OSGLgoLrdVT0z5C2qOuwOgz1c7jNiJhtMdwN+cOtnJp2\n"\
  28. "fuBgEYyE3eeuWogvkWoDcIA8r17Ixzkpq2oJsdvZcHZPIZShPKW2SHUsl98KDemu\n"\
  29. "y0pQyExmQUbwKE4vbFb9XuWCcL9XaOHQytyszt2DeD67AipvoBwVU7/LBOvqnsmy\n"\
  30. "hA==\n"\
  31. "-----END CERTIFICATE-----\n"
  32. server_key = "-----BEGIN PRIVATE KEY-----\n"\
  33. "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCXN8LK/eYi/tOU\n"\
  34. "uQ5vG6cp8J2sn/OB0A2uzHREG2kQ+FXnrSFYnR8SKfScg4yklKIX0TpCw92vpD56\n"\
  35. "iAfhec4xT0kT6Ibvjc3V098YTMm9GXJy38KTxKzAo8L35Veyc51mZTD3L/G0A1tR\n"\
  36. "ED9Oym8/PPcC+/fzZ999+skaHuig6ZplIJkWVJgctkDD9eVGvSxJFsukUZjSBeNo\n"\
  37. "BXyIceI8NgvLiRk56mNX1TnXGL4gawvjDvnO4yCwfIXecl4ZgeC0ZUGuQvRkobm5\n"\
  38. "1jTBwGPKyO5sMkLiJKU2KrYcPd+GzuPoJl11Xa/zjkyVUo3E/SQ7hSPgPyv7lRJY\n"\
  39. "Lwkp8DDFAgMBAAECggEAfBhAfQE7mUByNbxgAgI5fot9eaqR1Nf+QpJ6X2H3KPwC\n"\
  40. "02sa0HOwieFwYfj6tB1doBoNq7i89mTc+QUlIn4pHgIowHO0OGawomeKz5BEhjCZ\n"\
  41. "4XeLYGSoODary2+kNkf2xY8JTfFEcyvGBpJEwc4S2VyYgRRx+IgnumTSH+N5mIKZ\n"\
  42. "SXWNdZIuHEmkwod+rPRXs6/r+PH0eVW6WfpINEbr4zVAGXJx2zXQwd2cuV1GTJWh\n"\
  43. "cPVOXLu+XJ9im9B370cYN6GqUnR3fui13urYbnWnEf3syvoH/zuZkyrVChauoFf8\n"\
  44. "8EGb74/HhXK7Q2s8NRakx2c7OxQifCbcy03liUMmyQKBgQDFAob5B/66N4Q2cq/N\n"\
  45. "MWPf98kYBYoLaeEOhEJhLQlKk0pIFCTmtpmUbpoEes2kCUbH7RwczpYko8tlKyoB\n"\
  46. "6Fn6RY4zQQ64KZJI6kQVsjkYpcP/ihnOY6rbds+3yyv+4uPX7Eh9sYZwZMggE19M\n"\
  47. "CkFHkwAjiwqhiiSlUxe20sWmowKBgQDEfx4lxuFzA1PBPeZKGVBTxYPQf+DSLCre\n"\
  48. "ZFg3ZmrxbCjRq1O7Lra4FXWD3dmRq7NDk79JofoW50yD8wD7I0B7opdDfXD2idO8\n"\
  49. "0dBnWUKDr2CAXyoLEINce9kJPbx4kFBQRN9PiGF7VkDQxeQ3kfS8CvcErpTKCOdy\n"\
  50. "5wOwBTwJdwKBgDiTFTeGeDv5nVoVbS67tDao7XKchJvqd9q3WGiXikeELJyuTDqE\n"\
  51. "zW22pTwMF+m3UEAxcxVCrhMvhkUzNAkANHaOatuFHzj7lyqhO5QPbh4J3FMR0X9X\n"\
  52. "V8VWRSg+jA/SECP9koOl6zlzd5Tee0tW1pA7QpryXscs6IEhb3ns5R2JAoGAIkzO\n"\
  53. "RmnhEOKTzDex611f2D+yMsMfy5BKK2f4vjLymBH5TiBKDXKqEpgsW0huoi8Gq9Uu\n"\
  54. "nvvXXAgkIyRYF36f0vUe0nkjLuYAQAWgC2pZYgNLJR13iVbol0xHJoXQUHtgiaJ8\n"\
  55. "GLYFzjHQPqFMpSalQe3oELko39uOC1CoJCHFySECgYBeycUnRBikCO2n8DNhY4Eg\n"\
  56. "9Y3oxcssRt6ea5BZwgW2eAYi7/XqKkmxoSoOykUt3MJx9+EkkrL17bxFSpkj1tvL\n"\
  57. "qvxn7egtsKjjgGNAxwXC4MwCvhveyUQQxtQb8AqGrGqo4jEEN0L15cnP38i2x1Uo\n"\
  58. "muhfskWf4MABV0yTUaKcGg==\n"\
  59. "-----END PRIVATE KEY-----\n"
  60. def get_my_ip():
  61. s1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  62. s1.connect(("8.8.8.8", 80))
  63. my_ip = s1.getsockname()[0]
  64. s1.close()
  65. return my_ip
  66. def get_server_status(host_ip, port):
  67. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  68. server_status = sock.connect_ex((host_ip, port))
  69. sock.close()
  70. if server_status == 0:
  71. return True
  72. return False
  73. def create_file(server_file, file_data):
  74. with open(server_file, "w+") as file:
  75. file.write(file_data)
  76. def get_ca_cert(ota_image_dir):
  77. os.chdir(ota_image_dir)
  78. server_file = os.path.join(ota_image_dir, "server_cert.pem")
  79. create_file(server_file, server_cert)
  80. key_file = os.path.join(ota_image_dir, "server_key.pem")
  81. create_file(key_file, server_key)
  82. return server_file, key_file
  83. def https_request_handler():
  84. """
  85. Returns a request handler class that handles broken pipe exception
  86. """
  87. class RequestHandler(http.server.SimpleHTTPRequestHandler):
  88. def finish(self):
  89. try:
  90. if not self.wfile.closed:
  91. self.wfile.flush()
  92. self.wfile.close()
  93. except socket.error:
  94. pass
  95. self.rfile.close()
  96. def handle(self):
  97. try:
  98. http.server.BaseHTTPRequestHandler.handle(self)
  99. except socket.error:
  100. pass
  101. return RequestHandler
  102. def start_https_server(ota_image_dir, server_ip, server_port):
  103. server_file, key_file = get_ca_cert(ota_image_dir)
  104. requestHandler = https_request_handler()
  105. httpd = http.server.HTTPServer((server_ip, server_port), requestHandler)
  106. httpd.socket = ssl.wrap_socket(httpd.socket,
  107. keyfile=key_file,
  108. certfile=server_file, server_side=True)
  109. httpd.serve_forever()
  110. def start_chunked_server(ota_image_dir, server_port):
  111. server_file, key_file = get_ca_cert(ota_image_dir)
  112. chunked_server = subprocess.Popen(["openssl", "s_server", "-WWW", "-key", key_file, "-cert", server_file, "-port", str(server_port)])
  113. return chunked_server
  114. def redirect_handler_factory(url):
  115. """
  116. Returns a request handler class that redirects to supplied `url`
  117. """
  118. class RedirectHandler(http.server.SimpleHTTPRequestHandler):
  119. def do_GET(self):
  120. print("Sending resp, URL: " + url)
  121. self.send_response(301)
  122. self.send_header('Location', url)
  123. self.end_headers()
  124. def handle(self):
  125. try:
  126. http.server.BaseHTTPRequestHandler.handle(self)
  127. except socket.error:
  128. pass
  129. return RedirectHandler
  130. def start_redirect_server(ota_image_dir, server_ip, server_port, redirection_port):
  131. os.chdir(ota_image_dir)
  132. server_file, key_file = get_ca_cert(ota_image_dir)
  133. redirectHandler = redirect_handler_factory("https://" + server_ip + ":" + str(redirection_port) + "/advanced_https_ota.bin")
  134. httpd = http.server.HTTPServer((server_ip, server_port), redirectHandler)
  135. httpd.socket = ssl.wrap_socket(httpd.socket,
  136. keyfile=key_file,
  137. certfile=server_file, server_side=True)
  138. httpd.serve_forever()
  139. @ttfw_idf.idf_example_test(env_tag="Example_WIFI")
  140. def test_examples_protocol_advanced_https_ota_example(env, extra_data):
  141. """
  142. This is a positive test case, which downloads complete binary file multiple number of times.
  143. Number of iterations can be specified in variable iterations.
  144. steps: |
  145. 1. join AP
  146. 2. Fetch OTA image over HTTPS
  147. 3. Reboot with the new OTA image
  148. """
  149. dut1 = env.get_dut("advanced_https_ota_example", "examples/system/ota/advanced_https_ota", dut_class=ttfw_idf.ESP32DUT)
  150. # Number of iterations to validate OTA
  151. iterations = 3
  152. server_port = 8001
  153. # File to be downloaded. This file is generated after compilation
  154. bin_name = "advanced_https_ota.bin"
  155. # check and log bin size
  156. binary_file = os.path.join(dut1.app.binary_path, bin_name)
  157. bin_size = os.path.getsize(binary_file)
  158. ttfw_idf.log_performance("advanced_https_ota_bin_size", "{}KB".format(bin_size // 1024))
  159. ttfw_idf.check_performance("advanced_https_ota_bin_size", bin_size // 1024, dut1.TARGET)
  160. # start test
  161. host_ip = get_my_ip()
  162. if (get_server_status(host_ip, server_port) is False):
  163. thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
  164. thread1.daemon = True
  165. thread1.start()
  166. dut1.start_app()
  167. for i in range(iterations):
  168. dut1.expect("Loaded app from partition at offset", timeout=30)
  169. try:
  170. ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30)
  171. print("Connected to AP with IP: {}".format(ip_address))
  172. except DUT.ExpectTimeout:
  173. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  174. thread1.close()
  175. dut1.expect("Starting Advanced OTA example", timeout=30)
  176. print("writing to device: {}".format("https://" + host_ip + ":" + str(server_port) + "/" + bin_name))
  177. dut1.write("https://" + host_ip + ":" + str(server_port) + "/" + bin_name)
  178. dut1.expect("Loaded app from partition at offset", timeout=60)
  179. dut1.expect("Starting Advanced OTA example", timeout=30)
  180. dut1.reset()
  181. @ttfw_idf.idf_example_test(env_tag="Example_WIFI")
  182. def test_examples_protocol_advanced_https_ota_example_truncated_bin(env, extra_data):
  183. """
  184. Working of OTA if binary file is truncated is validated in this test case.
  185. Application should return with error message in this case.
  186. steps: |
  187. 1. join AP
  188. 2. Generate truncated binary file
  189. 3. Fetch OTA image over HTTPS
  190. 4. Check working of code if bin is truncated
  191. """
  192. dut1 = env.get_dut("advanced_https_ota_example", "examples/system/ota/advanced_https_ota", dut_class=ttfw_idf.ESP32DUT)
  193. server_port = 8001
  194. # Original binary file generated after compilation
  195. bin_name = "advanced_https_ota.bin"
  196. # Truncated binary file to be generated from original binary file
  197. truncated_bin_name = "truncated.bin"
  198. # Size of truncated file to be grnerated. This value can range from 288 bytes (Image header size) to size of original binary file
  199. # truncated_bin_size is set to 64000 to reduce consumed by the test case
  200. truncated_bin_size = 64000
  201. # check and log bin size
  202. binary_file = os.path.join(dut1.app.binary_path, bin_name)
  203. f = open(binary_file, "r+")
  204. fo = open(os.path.join(dut1.app.binary_path, truncated_bin_name), "w+")
  205. fo.write(f.read(truncated_bin_size))
  206. fo.close()
  207. f.close()
  208. binary_file = os.path.join(dut1.app.binary_path, truncated_bin_name)
  209. bin_size = os.path.getsize(binary_file)
  210. ttfw_idf.log_performance("advanced_https_ota_bin_size", "{}KB".format(bin_size // 1024))
  211. ttfw_idf.check_performance("advanced_https_ota_bin_size", bin_size // 1024, dut1.TARGET)
  212. # start test
  213. host_ip = get_my_ip()
  214. if (get_server_status(host_ip, server_port) is False):
  215. thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
  216. thread1.daemon = True
  217. thread1.start()
  218. dut1.start_app()
  219. dut1.expect("Loaded app from partition at offset", timeout=30)
  220. try:
  221. ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30)
  222. print("Connected to AP with IP: {}".format(ip_address))
  223. except DUT.ExpectTimeout:
  224. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  225. dut1.expect("Starting Advanced OTA example", timeout=30)
  226. print("writing to device: {}".format("https://" + host_ip + ":" + str(server_port) + "/" + truncated_bin_name))
  227. dut1.write("https://" + host_ip + ":" + str(server_port) + "/" + truncated_bin_name)
  228. dut1.expect("Image validation failed, image is corrupted", timeout=30)
  229. os.remove(binary_file)
  230. @ttfw_idf.idf_example_test(env_tag="Example_WIFI")
  231. def test_examples_protocol_advanced_https_ota_example_truncated_header(env, extra_data):
  232. """
  233. Working of OTA if headers of binary file are truncated is vaildated in this test case.
  234. Application should return with error message in this case.
  235. steps: |
  236. 1. join AP
  237. 2. Generate binary file with truncated headers
  238. 3. Fetch OTA image over HTTPS
  239. 4. Check working of code if headers are not sent completely
  240. """
  241. dut1 = env.get_dut("advanced_https_ota_example", "examples/system/ota/advanced_https_ota", dut_class=ttfw_idf.ESP32DUT)
  242. server_port = 8001
  243. # Original binary file generated after compilation
  244. bin_name = "advanced_https_ota.bin"
  245. # Truncated binary file to be generated from original binary file
  246. truncated_bin_name = "truncated_header.bin"
  247. # Size of truncated file to be grnerated. This value should be less than 288 bytes (Image header size)
  248. truncated_bin_size = 180
  249. # check and log bin size
  250. binary_file = os.path.join(dut1.app.binary_path, bin_name)
  251. f = open(binary_file, "r+")
  252. fo = open(os.path.join(dut1.app.binary_path, truncated_bin_name), "w+")
  253. fo.write(f.read(truncated_bin_size))
  254. fo.close()
  255. f.close()
  256. binary_file = os.path.join(dut1.app.binary_path, truncated_bin_name)
  257. bin_size = os.path.getsize(binary_file)
  258. ttfw_idf.log_performance("advanced_https_ota_bin_size", "{}KB".format(bin_size // 1024))
  259. ttfw_idf.check_performance("advanced_https_ota_bin_size", bin_size // 1024, dut1.TARGET)
  260. # start test
  261. host_ip = get_my_ip()
  262. if (get_server_status(host_ip, server_port) is False):
  263. thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
  264. thread1.daemon = True
  265. thread1.start()
  266. dut1.start_app()
  267. dut1.expect("Loaded app from partition at offset", timeout=30)
  268. try:
  269. ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30)
  270. print("Connected to AP with IP: {}".format(ip_address))
  271. except DUT.ExpectTimeout:
  272. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  273. dut1.expect("Starting Advanced OTA example", timeout=30)
  274. print("writing to device: {}".format("https://" + host_ip + ":" + str(server_port) + "/" + truncated_bin_name))
  275. dut1.write("https://" + host_ip + ":" + str(server_port) + "/" + truncated_bin_name)
  276. dut1.expect("advanced_https_ota_example: esp_https_ota_read_img_desc failed", timeout=30)
  277. os.remove(binary_file)
  278. @ttfw_idf.idf_example_test(env_tag="Example_WIFI")
  279. def test_examples_protocol_advanced_https_ota_example_random(env, extra_data):
  280. """
  281. Working of OTA if random data is added in binary file are validated in this test case.
  282. Magic byte verification should fail in this case.
  283. steps: |
  284. 1. join AP
  285. 2. Generate random binary image
  286. 3. Fetch OTA image over HTTPS
  287. 4. Check working of code for random binary file
  288. """
  289. dut1 = env.get_dut("advanced_https_ota_example", "examples/system/ota/advanced_https_ota", dut_class=ttfw_idf.ESP32DUT)
  290. server_port = 8001
  291. # Random binary file to be generated
  292. random_bin_name = "random.bin"
  293. # Size of random binary file. 32000 is choosen, to reduce the time required to run the test-case
  294. random_bin_size = 32000
  295. # check and log bin size
  296. binary_file = os.path.join(dut1.app.binary_path, random_bin_name)
  297. fo = open(binary_file, "w+")
  298. # First byte of binary file is always set to zero. If first byte is generated randomly,
  299. # in some cases it may generate 0xE9 which will result in failure of testcase.
  300. fo.write(str(0))
  301. for i in range(random_bin_size - 1):
  302. fo.write(str(random.randrange(0,255,1)))
  303. fo.close()
  304. bin_size = os.path.getsize(binary_file)
  305. ttfw_idf.log_performance("advanced_https_ota_bin_size", "{}KB".format(bin_size // 1024))
  306. ttfw_idf.check_performance("advanced_https_ota_bin_size", bin_size // 1024, dut1.TARGET)
  307. # start test
  308. host_ip = get_my_ip()
  309. if (get_server_status(host_ip, server_port) is False):
  310. thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
  311. thread1.daemon = True
  312. thread1.start()
  313. dut1.start_app()
  314. dut1.expect("Loaded app from partition at offset", timeout=30)
  315. try:
  316. ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30)
  317. print("Connected to AP with IP: {}".format(ip_address))
  318. except DUT.ExpectTimeout:
  319. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  320. dut1.expect("Starting Advanced OTA example", timeout=30)
  321. print("writing to device: {}".format("https://" + host_ip + ":" + str(server_port) + "/" + random_bin_name))
  322. dut1.write("https://" + host_ip + ":" + str(server_port) + "/" + random_bin_name)
  323. dut1.expect("esp_ota_ops: OTA image has invalid magic byte", timeout=10)
  324. os.remove(binary_file)
  325. @ttfw_idf.idf_example_test(env_tag="Example_WIFI")
  326. def test_examples_protocol_advanced_https_ota_example_chunked(env, extra_data):
  327. """
  328. This is a positive test case, which downloads complete binary file multiple number of times.
  329. Number of iterations can be specified in variable iterations.
  330. steps: |
  331. 1. join AP
  332. 2. Fetch OTA image over HTTPS
  333. 3. Reboot with the new OTA image
  334. """
  335. dut1 = env.get_dut("advanced_https_ota_example", "examples/system/ota/advanced_https_ota", dut_class=ttfw_idf.ESP32DUT)
  336. # File to be downloaded. This file is generated after compilation
  337. bin_name = "advanced_https_ota.bin"
  338. # check and log bin size
  339. binary_file = os.path.join(dut1.app.binary_path, bin_name)
  340. bin_size = os.path.getsize(binary_file)
  341. ttfw_idf.log_performance("advanced_https_ota_bin_size", "{}KB".format(bin_size // 1024))
  342. ttfw_idf.check_performance("advanced_https_ota_bin_size", bin_size // 1024, dut1.TARGET)
  343. # start test
  344. host_ip = get_my_ip()
  345. chunked_server = start_chunked_server(dut1.app.binary_path, 8070)
  346. dut1.start_app()
  347. dut1.expect("Loaded app from partition at offset", timeout=30)
  348. try:
  349. ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30)
  350. print("Connected to AP with IP: {}".format(ip_address))
  351. except DUT.ExpectTimeout:
  352. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  353. dut1.expect("Starting Advanced OTA example", timeout=30)
  354. print("writing to device: {}".format("https://" + host_ip + ":8070/" + bin_name))
  355. dut1.write("https://" + host_ip + ":8070/" + bin_name)
  356. dut1.expect("Loaded app from partition at offset", timeout=60)
  357. dut1.expect("Starting Advanced OTA example", timeout=30)
  358. chunked_server.kill()
  359. os.remove(os.path.join(dut1.app.binary_path, "server_cert.pem"))
  360. os.remove(os.path.join(dut1.app.binary_path, "server_key.pem"))
  361. @ttfw_idf.idf_example_test(env_tag="Example_WIFI")
  362. def test_examples_protocol_advanced_https_ota_example_redirect_url(env, extra_data):
  363. """
  364. This is a positive test case, which starts a server and a redirection server.
  365. Redirection server redirects http_request to different port
  366. Number of iterations can be specified in variable iterations.
  367. steps: |
  368. 1. join AP
  369. 2. Fetch OTA image over HTTPS
  370. 3. Reboot with the new OTA image
  371. """
  372. dut1 = env.get_dut("advanced_https_ota_example", "examples/system/ota/advanced_https_ota", dut_class=ttfw_idf.ESP32DUT)
  373. server_port = 8001
  374. # Port to which the request should be redirecetd
  375. redirection_server_port = 8081
  376. # File to be downloaded. This file is generated after compilation
  377. bin_name = "advanced_https_ota.bin"
  378. # check and log bin size
  379. binary_file = os.path.join(dut1.app.binary_path, bin_name)
  380. bin_size = os.path.getsize(binary_file)
  381. ttfw_idf.log_performance("advanced_https_ota_bin_size", "{}KB".format(bin_size // 1024))
  382. ttfw_idf.check_performance("advanced_https_ota_bin_size", bin_size // 1024, dut1.TARGET)
  383. # start test
  384. host_ip = get_my_ip()
  385. if (get_server_status(host_ip, server_port) is False):
  386. thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
  387. thread1.daemon = True
  388. thread1.start()
  389. thread2 = Thread(target=start_redirect_server, args=(dut1.app.binary_path, host_ip, redirection_server_port, server_port))
  390. thread2.daemon = True
  391. thread2.start()
  392. dut1.start_app()
  393. dut1.expect("Loaded app from partition at offset", timeout=30)
  394. try:
  395. ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30)
  396. print("Connected to AP with IP: {}".format(ip_address))
  397. except DUT.ExpectTimeout:
  398. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  399. thread1.close()
  400. thread2.close()
  401. dut1.expect("Starting Advanced OTA example", timeout=30)
  402. print("writing to device: {}".format("https://" + host_ip + ":" + str(redirection_server_port) + "/" + bin_name))
  403. dut1.write("https://" + host_ip + ":" + str(redirection_server_port) + "/" + bin_name)
  404. dut1.expect("Loaded app from partition at offset", timeout=60)
  405. dut1.expect("Starting Advanced OTA example", timeout=30)
  406. dut1.reset()
  407. if __name__ == '__main__':
  408. test_examples_protocol_advanced_https_ota_example()
  409. test_examples_protocol_advanced_https_ota_example_chunked()
  410. test_examples_protocol_advanced_https_ota_example_redirect_url()
  411. test_examples_protocol_advanced_https_ota_example_truncated_bin()
  412. test_examples_protocol_advanced_https_ota_example_truncated_header()
  413. test_examples_protocol_advanced_https_ota_example_random()