example_test.py 21 KB

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