example_test.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. import http.server
  2. import os
  3. import random
  4. import re
  5. import socket
  6. import ssl
  7. import struct
  8. import subprocess
  9. from threading import Thread
  10. import ttfw_idf
  11. from RangeHTTPServer import RangeRequestHandler
  12. from tiny_test_fw import DUT, Utility
  13. server_cert = '-----BEGIN CERTIFICATE-----\n' \
  14. 'MIIDWDCCAkACCQCbF4+gVh/MLjANBgkqhkiG9w0BAQsFADBuMQswCQYDVQQGEwJJ\n'\
  15. 'TjELMAkGA1UECAwCTUgxDDAKBgNVBAcMA1BVTjEMMAoGA1UECgwDRVNQMQwwCgYD\n'\
  16. 'VQQLDANFU1AxDDAKBgNVBAMMA0VTUDEaMBgGCSqGSIb3DQEJARYLZXNwQGVzcC5j\n'\
  17. 'b20wHhcNMjEwNzEyMTIzNjI3WhcNNDEwNzA3MTIzNjI3WjBuMQswCQYDVQQGEwJJ\n'\
  18. 'TjELMAkGA1UECAwCTUgxDDAKBgNVBAcMA1BVTjEMMAoGA1UECgwDRVNQMQwwCgYD\n'\
  19. 'VQQLDANFU1AxDDAKBgNVBAMMA0VTUDEaMBgGCSqGSIb3DQEJARYLZXNwQGVzcC5j\n'\
  20. 'b20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDhxF/y7bygndxPwiWL\n'\
  21. 'SwS9LY3uBMaJgup0ufNKVhx+FhGQOu44SghuJAaH3KkPUnt6SOM8jC97/yQuc32W\n'\
  22. 'ukI7eBZoA12kargSnzdv5m5rZZpd+NznSSpoDArOAONKVlzr25A1+aZbix2mKRbQ\n'\
  23. 'S5w9o1N2BriQuSzd8gL0Y0zEk3VkOWXEL+0yFUT144HnErnD+xnJtHe11yPO2fEz\n'\
  24. 'YaGiilh0ddL26PXTugXMZN/8fRVHP50P2OG0SvFpC7vghlLp4VFM1/r3UJnvL6Oz\n'\
  25. '3ALc6dhxZEKQucqlpj8l1UegszQToopemtIj0qXTHw2+uUnkUyWIPjPC+wdOAoap\n'\
  26. 'rFTRAgMBAAEwDQYJKoZIhvcNAQELBQADggEBAItw24y565k3C/zENZlxyzto44ud\n'\
  27. 'IYPQXN8Fa2pBlLe1zlSIyuaA/rWQ+i1daS8nPotkCbWZyf5N8DYaTE4B0OfvoUPk\n'\
  28. 'B5uGDmbuk6akvlB5BGiYLfQjWHRsK9/4xjtIqN1H58yf3QNROuKsPAeywWS3Fn32\n'\
  29. '3//OpbWaClQePx6udRYMqAitKR+QxL7/BKZQsX+UyShuq8hjphvXvk0BW8ONzuw9\n'\
  30. 'RcoORxM0FzySYjeQvm4LhzC/P3ZBhEq0xs55aL2a76SJhq5hJy7T/Xz6NFByvlrN\n'\
  31. 'lFJJey33KFrAf5vnV9qcyWFIo7PYy2VsaaEjFeefr7q3sTFSMlJeadexW2Y=\n'\
  32. '-----END CERTIFICATE-----\n'
  33. server_key = '-----BEGIN PRIVATE KEY-----\n'\
  34. 'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDhxF/y7bygndxP\n'\
  35. 'wiWLSwS9LY3uBMaJgup0ufNKVhx+FhGQOu44SghuJAaH3KkPUnt6SOM8jC97/yQu\n'\
  36. 'c32WukI7eBZoA12kargSnzdv5m5rZZpd+NznSSpoDArOAONKVlzr25A1+aZbix2m\n'\
  37. 'KRbQS5w9o1N2BriQuSzd8gL0Y0zEk3VkOWXEL+0yFUT144HnErnD+xnJtHe11yPO\n'\
  38. '2fEzYaGiilh0ddL26PXTugXMZN/8fRVHP50P2OG0SvFpC7vghlLp4VFM1/r3UJnv\n'\
  39. 'L6Oz3ALc6dhxZEKQucqlpj8l1UegszQToopemtIj0qXTHw2+uUnkUyWIPjPC+wdO\n'\
  40. 'AoaprFTRAgMBAAECggEAE0HCxV/N1Q1h+1OeDDGL5+74yjKSFKyb/vTVcaPCrmaH\n'\
  41. 'fPvp0ddOvMZJ4FDMAsiQS6/n4gQ7EKKEnYmwTqj4eUYW8yxGUn3f0YbPHbZT+Mkj\n'\
  42. 'z5woi3nMKi/MxCGDQZX4Ow3xUQlITUqibsfWcFHis8c4mTqdh4qj7xJzehD2PVYF\n'\
  43. 'gNHZsvVj6MltjBDAVwV1IlGoHjuElm6vuzkfX7phxcA1B4ZqdYY17yCXUnvui46z\n'\
  44. 'Xn2kUTOOUCEgfgvGa9E+l4OtdXi5IxjaSraU+dlg2KsE4TpCuN2MEVkeR5Ms3Y7Q\n'\
  45. 'jgJl8vlNFJDQpbFukLcYwG7rO5N5dQ6WWfVia/5XgQKBgQD74at/bXAPrh9NxPmz\n'\
  46. 'i1oqCHMDoM9sz8xIMZLF9YVu3Jf8ux4xVpRSnNy5RU1gl7ZXbpdgeIQ4v04zy5aw\n'\
  47. '8T4tu9K3XnR3UXOy25AK0q+cnnxZg3kFQm+PhtOCKEFjPHrgo2MUfnj+EDddod7N\n'\
  48. 'JQr9q5rEFbqHupFPpWlqCa3QmQKBgQDldWUGokNaEpmgHDMnHxiibXV5LQhzf8Rq\n'\
  49. 'gJIQXb7R9EsTSXEvsDyqTBb7PHp2Ko7rZ5YQfyf8OogGGjGElnPoU/a+Jij1gVFv\n'\
  50. 'kZ064uXAAISBkwHdcuobqc5EbG3ceyH46F+FBFhqM8KcbxJxx08objmh58+83InN\n'\
  51. 'P9Qr25Xw+QKBgEGXMHuMWgQbSZeM1aFFhoMvlBO7yogBTKb4Ecpu9wI5e3Kan3Al\n'\
  52. 'pZYltuyf+VhP6XG3IMBEYdoNJyYhu+nzyEdMg8CwXg+8LC7FMis/Ve+o7aS5scgG\n'\
  53. '1to/N9DK/swCsdTRdzmc/ZDbVC+TuVsebFBGYZTyO5KgqLpezqaIQrTxAoGALFCU\n'\
  54. '10glO9MVyl9H3clap5v+MQ3qcOv/EhaMnw6L2N6WVT481tnxjW4ujgzrFcE4YuxZ\n'\
  55. 'hgwYu9TOCmeqopGwBvGYWLbj+C4mfSahOAs0FfXDoYazuIIGBpuv03UhbpB1Si4O\n'\
  56. 'rJDfRnuCnVWyOTkl54gKJ2OusinhjztBjcrV1XkCgYEA3qNi4uBsPdyz9BZGb/3G\n'\
  57. 'rOMSw0CaT4pEMTLZqURmDP/0hxvTk1polP7O/FYwxVuJnBb6mzDa0xpLFPTpIAnJ\n'\
  58. 'YXB8xpXU69QVh+EBbemdJWOd+zp5UCfXvb2shAeG3Tn/Dz4cBBMEUutbzP+or0nG\n'\
  59. 'vSXnRLaxQhooWm+IuX9SuBQ=\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(RangeRequestHandler):
  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. RangeRequestHandler.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 = http.server.HTTPServer((server_ip, server_port), requestHandler)
  107. httpd.socket = ssl.wrap_socket(httpd.socket,
  108. keyfile=key_file,
  109. certfile=server_file, server_side=True)
  110. httpd.serve_forever()
  111. def start_chunked_server(ota_image_dir, server_port):
  112. server_file, key_file = get_ca_cert(ota_image_dir)
  113. chunked_server = subprocess.Popen(['openssl', 's_server', '-WWW', '-key', key_file, '-cert', server_file, '-port', str(server_port)])
  114. return chunked_server
  115. def redirect_handler_factory(url):
  116. """
  117. Returns a request handler class that redirects to supplied `url`
  118. """
  119. class RedirectHandler(http.server.SimpleHTTPRequestHandler):
  120. def do_GET(self):
  121. print('Sending resp, URL: ' + url)
  122. self.send_response(301)
  123. self.send_header('Location', url)
  124. self.end_headers()
  125. def handle(self):
  126. try:
  127. http.server.BaseHTTPRequestHandler.handle(self)
  128. except socket.error:
  129. pass
  130. return RedirectHandler
  131. def start_redirect_server(ota_image_dir, server_ip, server_port, redirection_port):
  132. os.chdir(ota_image_dir)
  133. server_file, key_file = get_ca_cert(ota_image_dir)
  134. redirectHandler = redirect_handler_factory('https://' + server_ip + ':' + str(redirection_port) + '/advanced_https_ota.bin')
  135. httpd = http.server.HTTPServer((server_ip, server_port), redirectHandler)
  136. httpd.socket = ssl.wrap_socket(httpd.socket,
  137. keyfile=key_file,
  138. certfile=server_file, server_side=True)
  139. httpd.serve_forever()
  140. @ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
  141. def test_examples_protocol_advanced_https_ota_example(env, extra_data):
  142. """
  143. This is a positive test case, which downloads complete binary file multiple number of times.
  144. Number of iterations can be specified in variable iterations.
  145. steps: |
  146. 1. join AP
  147. 2. Fetch OTA image over HTTPS
  148. 3. Reboot with the new OTA image
  149. """
  150. dut1 = env.get_dut('advanced_https_ota_example', 'examples/system/ota/advanced_https_ota', dut_class=ttfw_idf.ESP32DUT)
  151. # Number of iterations to validate OTA
  152. iterations = 3
  153. server_port = 8001
  154. # File to be downloaded. This file is generated after compilation
  155. bin_name = 'advanced_https_ota.bin'
  156. # check and log bin size
  157. binary_file = os.path.join(dut1.app.binary_path, bin_name)
  158. bin_size = os.path.getsize(binary_file)
  159. ttfw_idf.log_performance('advanced_https_ota_bin_size', '{}KB'.format(bin_size // 1024))
  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_OTA')
  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, 'rb+')
  204. fo = open(os.path.join(dut1.app.binary_path, truncated_bin_name), 'wb+')
  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. # start test
  212. host_ip = get_my_ip()
  213. if (get_server_status(host_ip, server_port) is False):
  214. thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
  215. thread1.daemon = True
  216. thread1.start()
  217. dut1.start_app()
  218. dut1.expect('Loaded app from partition at offset', timeout=30)
  219. try:
  220. ip_address = dut1.expect(re.compile(r' sta ip: ([^,]+),'), timeout=30)
  221. print('Connected to AP with IP: {}'.format(ip_address))
  222. except DUT.ExpectTimeout:
  223. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  224. dut1.expect('Starting Advanced OTA example', timeout=30)
  225. print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + truncated_bin_name))
  226. dut1.write('https://' + host_ip + ':' + str(server_port) + '/' + truncated_bin_name)
  227. dut1.expect('Image validation failed, image is corrupted', timeout=30)
  228. os.remove(binary_file)
  229. @ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
  230. def test_examples_protocol_advanced_https_ota_example_truncated_header(env, extra_data):
  231. """
  232. Working of OTA if headers of binary file are truncated is vaildated in this test case.
  233. Application should return with error message in this case.
  234. steps: |
  235. 1. join AP
  236. 2. Generate binary file with truncated headers
  237. 3. Fetch OTA image over HTTPS
  238. 4. Check working of code if headers are not sent completely
  239. """
  240. dut1 = env.get_dut('advanced_https_ota_example', 'examples/system/ota/advanced_https_ota', dut_class=ttfw_idf.ESP32DUT)
  241. server_port = 8001
  242. # Original binary file generated after compilation
  243. bin_name = 'advanced_https_ota.bin'
  244. # Truncated binary file to be generated from original binary file
  245. truncated_bin_name = 'truncated_header.bin'
  246. # Size of truncated file to be grnerated. This value should be less than 288 bytes (Image header size)
  247. truncated_bin_size = 180
  248. # check and log bin size
  249. binary_file = os.path.join(dut1.app.binary_path, bin_name)
  250. f = open(binary_file, 'rb+')
  251. fo = open(os.path.join(dut1.app.binary_path, truncated_bin_name), 'wb+')
  252. fo.write(f.read(truncated_bin_size))
  253. fo.close()
  254. f.close()
  255. binary_file = os.path.join(dut1.app.binary_path, truncated_bin_name)
  256. bin_size = os.path.getsize(binary_file)
  257. ttfw_idf.log_performance('advanced_https_ota_bin_size', '{}KB'.format(bin_size // 1024))
  258. # start test
  259. host_ip = get_my_ip()
  260. if (get_server_status(host_ip, server_port) is False):
  261. thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
  262. thread1.daemon = True
  263. thread1.start()
  264. dut1.start_app()
  265. dut1.expect('Loaded app from partition at offset', timeout=30)
  266. try:
  267. ip_address = dut1.expect(re.compile(r' sta ip: ([^,]+),'), timeout=30)
  268. print('Connected to AP with IP: {}'.format(ip_address))
  269. except DUT.ExpectTimeout:
  270. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  271. dut1.expect('Starting Advanced OTA example', timeout=30)
  272. print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + truncated_bin_name))
  273. dut1.write('https://' + host_ip + ':' + str(server_port) + '/' + truncated_bin_name)
  274. dut1.expect('advanced_https_ota_example: esp_https_ota_read_img_desc failed', timeout=30)
  275. os.remove(binary_file)
  276. @ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
  277. def test_examples_protocol_advanced_https_ota_example_random(env, extra_data):
  278. """
  279. Working of OTA if random data is added in binary file are validated in this test case.
  280. Magic byte verification should fail in this case.
  281. steps: |
  282. 1. join AP
  283. 2. Generate random binary image
  284. 3. Fetch OTA image over HTTPS
  285. 4. Check working of code for random binary file
  286. """
  287. dut1 = env.get_dut('advanced_https_ota_example', 'examples/system/ota/advanced_https_ota', dut_class=ttfw_idf.ESP32DUT)
  288. server_port = 8001
  289. # Random binary file to be generated
  290. random_bin_name = 'random.bin'
  291. # Size of random binary file. 32000 is choosen, to reduce the time required to run the test-case
  292. random_bin_size = 32000
  293. # check and log bin size
  294. binary_file = os.path.join(dut1.app.binary_path, random_bin_name)
  295. fo = open(binary_file, 'wb+')
  296. # First byte of binary file is always set to zero. If first byte is generated randomly,
  297. # in some cases it may generate 0xE9 which will result in failure of testcase.
  298. fo.write(struct.pack('B', 0))
  299. for i in range(random_bin_size - 1):
  300. fo.write(struct.pack('B', random.randrange(0,255,1)))
  301. fo.close()
  302. bin_size = os.path.getsize(binary_file)
  303. ttfw_idf.log_performance('advanced_https_ota_bin_size', '{}KB'.format(bin_size // 1024))
  304. # start test
  305. host_ip = get_my_ip()
  306. if (get_server_status(host_ip, server_port) is False):
  307. thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
  308. thread1.daemon = True
  309. thread1.start()
  310. dut1.start_app()
  311. dut1.expect('Loaded app from partition at offset', timeout=30)
  312. try:
  313. ip_address = dut1.expect(re.compile(r' sta ip: ([^,]+),'), timeout=30)
  314. print('Connected to AP with IP: {}'.format(ip_address))
  315. except DUT.ExpectTimeout:
  316. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  317. dut1.expect('Starting Advanced OTA example', timeout=30)
  318. print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + random_bin_name))
  319. dut1.write('https://' + host_ip + ':' + str(server_port) + '/' + random_bin_name)
  320. dut1.expect('esp_ota_ops: OTA image has invalid magic byte', timeout=10)
  321. os.remove(binary_file)
  322. @ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
  323. def test_examples_protocol_advanced_https_ota_example_chunked(env, extra_data):
  324. """
  325. This is a positive test case, which downloads complete binary file multiple number of times.
  326. Number of iterations can be specified in variable iterations.
  327. steps: |
  328. 1. join AP
  329. 2. Fetch OTA image over HTTPS
  330. 3. Reboot with the new OTA image
  331. """
  332. dut1 = env.get_dut('advanced_https_ota_example', 'examples/system/ota/advanced_https_ota', dut_class=ttfw_idf.ESP32DUT)
  333. # File to be downloaded. This file is generated after compilation
  334. bin_name = 'advanced_https_ota.bin'
  335. # check and log bin size
  336. binary_file = os.path.join(dut1.app.binary_path, bin_name)
  337. bin_size = os.path.getsize(binary_file)
  338. ttfw_idf.log_performance('advanced_https_ota_bin_size', '{}KB'.format(bin_size // 1024))
  339. # start test
  340. host_ip = get_my_ip()
  341. chunked_server = start_chunked_server(dut1.app.binary_path, 8070)
  342. dut1.start_app()
  343. dut1.expect('Loaded app from partition at offset', timeout=30)
  344. try:
  345. ip_address = dut1.expect(re.compile(r' sta ip: ([^,]+),'), timeout=30)
  346. print('Connected to AP with IP: {}'.format(ip_address))
  347. except DUT.ExpectTimeout:
  348. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  349. dut1.expect('Starting Advanced OTA example', timeout=30)
  350. print('writing to device: {}'.format('https://' + host_ip + ':8070/' + bin_name))
  351. dut1.write('https://' + host_ip + ':8070/' + bin_name)
  352. dut1.expect('Loaded app from partition at offset', timeout=60)
  353. dut1.expect('Starting Advanced OTA example', timeout=30)
  354. chunked_server.kill()
  355. os.remove(os.path.join(dut1.app.binary_path, 'server_cert.pem'))
  356. os.remove(os.path.join(dut1.app.binary_path, 'server_key.pem'))
  357. @ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
  358. def test_examples_protocol_advanced_https_ota_example_redirect_url(env, extra_data):
  359. """
  360. This is a positive test case, which starts a server and a redirection server.
  361. Redirection server redirects http_request to different port
  362. Number of iterations can be specified in variable iterations.
  363. steps: |
  364. 1. join AP
  365. 2. Fetch OTA image over HTTPS
  366. 3. Reboot with the new OTA image
  367. """
  368. dut1 = env.get_dut('advanced_https_ota_example', 'examples/system/ota/advanced_https_ota', dut_class=ttfw_idf.ESP32DUT)
  369. server_port = 8001
  370. # Port to which the request should be redirecetd
  371. redirection_server_port = 8081
  372. # File to be downloaded. This file is generated after compilation
  373. bin_name = 'advanced_https_ota.bin'
  374. # check and log bin size
  375. binary_file = os.path.join(dut1.app.binary_path, bin_name)
  376. bin_size = os.path.getsize(binary_file)
  377. ttfw_idf.log_performance('advanced_https_ota_bin_size', '{}KB'.format(bin_size // 1024))
  378. # start test
  379. host_ip = get_my_ip()
  380. if (get_server_status(host_ip, server_port) is False):
  381. thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
  382. thread1.daemon = True
  383. thread1.start()
  384. thread2 = Thread(target=start_redirect_server, args=(dut1.app.binary_path, host_ip, redirection_server_port, server_port))
  385. thread2.daemon = True
  386. thread2.start()
  387. dut1.start_app()
  388. dut1.expect('Loaded app from partition at offset', timeout=30)
  389. try:
  390. ip_address = dut1.expect(re.compile(r' sta ip: ([^,]+),'), timeout=30)
  391. print('Connected to AP with IP: {}'.format(ip_address))
  392. except DUT.ExpectTimeout:
  393. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  394. thread1.close()
  395. thread2.close()
  396. dut1.expect('Starting Advanced OTA example', timeout=30)
  397. print('writing to device: {}'.format('https://' + host_ip + ':' + str(redirection_server_port) + '/' + bin_name))
  398. dut1.write('https://' + host_ip + ':' + str(redirection_server_port) + '/' + bin_name)
  399. dut1.expect('Loaded app from partition at offset', timeout=60)
  400. dut1.expect('Starting Advanced OTA example', timeout=30)
  401. dut1.reset()
  402. @ttfw_idf.idf_example_test(env_tag='Example_8Mflash_Ethernet')
  403. def test_examples_protocol_advanced_https_ota_example_anti_rollback(env, extra_data):
  404. """
  405. Working of OTA when anti_rollback is enabled and security version of new image is less than current one.
  406. Application should return with error message in this case.
  407. steps: |
  408. 1. join AP
  409. 2. Generate binary file with lower security version
  410. 3. Fetch OTA image over HTTPS
  411. 4. Check working of anti_rollback feature
  412. """
  413. dut1 = env.get_dut('advanced_https_ota_example', 'examples/system/ota/advanced_https_ota', dut_class=ttfw_idf.ESP32DUT, app_config_name='anti_rollback')
  414. Utility.console_log('Erasing the flash on the chip')
  415. # erase the flash
  416. dut1.erase_flash()
  417. server_port = 8001
  418. # Original binary file generated after compilation
  419. bin_name = 'advanced_https_ota.bin'
  420. # Modified firmware image to lower security version in its header. This is to enable negative test case
  421. anti_rollback_bin_name = 'advanced_https_ota_lower_sec_version.bin'
  422. # check and log bin size
  423. binary_file = os.path.join(dut1.app.binary_path, bin_name)
  424. file_size = os.path.getsize(binary_file)
  425. f = open(binary_file, 'rb+')
  426. fo = open(os.path.join(dut1.app.binary_path, anti_rollback_bin_name), 'wb+')
  427. fo.write(f.read(file_size))
  428. # Change security_version to 0 for negative test case
  429. fo.seek(36)
  430. fo.write(b'\x00')
  431. fo.close()
  432. f.close()
  433. binary_file = os.path.join(dut1.app.binary_path, anti_rollback_bin_name)
  434. bin_size = os.path.getsize(binary_file)
  435. ttfw_idf.log_performance('advanced_https_ota_bin_size', '{}KB'.format(bin_size // 1024))
  436. # start test
  437. host_ip = get_my_ip()
  438. if (get_server_status(host_ip, server_port) is False):
  439. thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
  440. thread1.daemon = True
  441. thread1.start()
  442. dut1.start_app()
  443. # Positive Case
  444. dut1.expect('Loaded app from partition at offset', timeout=30)
  445. try:
  446. ip_address = dut1.expect(re.compile(r' eth ip: ([^,]+),'), timeout=30)
  447. print('Connected to AP with IP: {}'.format(ip_address))
  448. except DUT.ExpectTimeout:
  449. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  450. dut1.expect('Starting Advanced OTA example', timeout=30)
  451. # Use originally generated image with secure_version=1
  452. print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + bin_name))
  453. dut1.write('https://' + host_ip + ':' + str(server_port) + '/' + bin_name)
  454. dut1.expect('Loaded app from partition at offset', timeout=60)
  455. dut1.expect(re.compile(r' eth ip: ([^,]+),'), timeout=30)
  456. dut1.expect('App is valid, rollback cancelled successfully', 30)
  457. # Negative Case
  458. dut1.expect('Starting Advanced OTA example', timeout=30)
  459. # Use modified image with secure_version=0
  460. print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + anti_rollback_bin_name))
  461. dut1.write('https://' + host_ip + ':' + str(server_port) + '/' + anti_rollback_bin_name)
  462. dut1.expect('New firmware security version is less than eFuse programmed, 0 < 1', timeout=30)
  463. os.remove(anti_rollback_bin_name)
  464. @ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
  465. def test_examples_protocol_advanced_https_ota_example_partial_request(env, extra_data):
  466. """
  467. This is a positive test case, to test OTA workflow with Range HTTP header.
  468. steps: |
  469. 1. join AP
  470. 2. Fetch OTA image over HTTPS
  471. 3. Reboot with the new OTA image
  472. """
  473. dut1 = env.get_dut('advanced_https_ota_example', 'examples/system/ota/advanced_https_ota', dut_class=ttfw_idf.ESP32DUT, app_config_name='partial_download')
  474. server_port = 8001
  475. # Size of partial HTTP request
  476. request_size = 16384
  477. # File to be downloaded. This file is generated after compilation
  478. bin_name = 'advanced_https_ota.bin'
  479. # check and log bin size
  480. binary_file = os.path.join(dut1.app.binary_path, bin_name)
  481. bin_size = os.path.getsize(binary_file)
  482. ttfw_idf.log_performance('advanced_https_ota_bin_size', '{}KB'.format(bin_size // 1024))
  483. http_requests = int((bin_size / request_size) - 1)
  484. # start test
  485. host_ip = get_my_ip()
  486. if (get_server_status(host_ip, server_port) is False):
  487. thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
  488. thread1.daemon = True
  489. thread1.start()
  490. dut1.start_app()
  491. dut1.expect('Loaded app from partition at offset', timeout=30)
  492. try:
  493. ip_address = dut1.expect(re.compile(r' sta ip: ([^,]+),'), timeout=30)
  494. print('Connected to AP with IP: {}'.format(ip_address))
  495. except DUT.ExpectTimeout:
  496. Utility.console_log('ENV_TEST_FAILURE: Cannot connect to AP')
  497. raise
  498. dut1.expect('Starting Advanced OTA example', timeout=30)
  499. print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + bin_name))
  500. dut1.write('https://' + host_ip + ':' + str(server_port) + '/' + bin_name)
  501. for _ in range(http_requests):
  502. dut1.expect('Connection closed', timeout=60)
  503. dut1.expect('Loaded app from partition at offset', timeout=60)
  504. dut1.expect('Starting Advanced OTA example', timeout=30)
  505. dut1.reset()
  506. @ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
  507. def test_examples_protocol_advanced_https_ota_example_nimble_gatts(env, extra_data):
  508. """
  509. Run an OTA image update while a BLE GATT Server is running in background. This GATT server will be using NimBLE Host stack.
  510. steps: |
  511. 1. join AP
  512. 2. Run BLE advertise and then GATT server.
  513. 3. Fetch OTA image over HTTPS
  514. 4. Reboot with the new OTA image
  515. """
  516. dut1 = env.get_dut('advanced_https_ota_example', 'examples/system/ota/advanced_https_ota', dut_class=ttfw_idf.ESP32DUT, app_config_name='nimble')
  517. server_port = 8001
  518. # File to be downloaded. This file is generated after compilation
  519. bin_name = 'advanced_https_ota.bin'
  520. # check and log bin size
  521. binary_file = os.path.join(dut1.app.binary_path, bin_name)
  522. bin_size = os.path.getsize(binary_file)
  523. ttfw_idf.log_performance('advanced_https_ota_bin_size', '{}KB'.format(bin_size // 1024))
  524. # start test
  525. host_ip = get_my_ip()
  526. if (get_server_status(host_ip, server_port) is False):
  527. thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
  528. thread1.daemon = True
  529. thread1.start()
  530. dut1.start_app()
  531. dut1.expect('Loaded app from partition at offset', timeout=30)
  532. try:
  533. ip_address = dut1.expect(re.compile(r' sta ip: ([^,]+),'), timeout=30)
  534. print('Connected to AP with IP: {}'.format(ip_address))
  535. except DUT.ExpectTimeout:
  536. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  537. dut1.expect('Starting Advanced OTA example', timeout=30)
  538. print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + bin_name))
  539. dut1.expect('GAP procedure initiated: advertise', timeout=30)
  540. print('Started GAP advertising.')
  541. dut1.write('https://' + host_ip + ':' + str(server_port) + '/' + bin_name)
  542. dut1.expect('Loaded app from partition at offset', timeout=60)
  543. dut1.expect('Starting Advanced OTA example', timeout=30)
  544. dut1.reset()
  545. @ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
  546. def test_examples_protocol_advanced_https_ota_example_bluedroid_gatts(env, extra_data):
  547. """
  548. Run an OTA image update while a BLE GATT Server is running in background. This GATT server will be using Bluedroid Host stack.
  549. steps: |
  550. 1. join AP
  551. 2. Run BLE advertise and then GATT server.
  552. 3. Fetch OTA image over HTTPS
  553. 4. Reboot with the new OTA image
  554. """
  555. dut1 = env.get_dut('advanced_https_ota_example', 'examples/system/ota/advanced_https_ota', dut_class=ttfw_idf.ESP32DUT, app_config_name='bluedroid')
  556. server_port = 8001
  557. # File to be downloaded. This file is generated after compilation
  558. bin_name = 'advanced_https_ota.bin'
  559. # check and log bin size
  560. binary_file = os.path.join(dut1.app.binary_path, bin_name)
  561. bin_size = os.path.getsize(binary_file)
  562. ttfw_idf.log_performance('advanced_https_ota_bin_size', '{}KB'.format(bin_size // 1024))
  563. # start test
  564. host_ip = get_my_ip()
  565. if (get_server_status(host_ip, server_port) is False):
  566. thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
  567. thread1.daemon = True
  568. thread1.start()
  569. dut1.start_app()
  570. dut1.expect('Loaded app from partition at offset', timeout=30)
  571. try:
  572. ip_address = dut1.expect(re.compile(r' sta ip: ([^,]+),'), timeout=30)
  573. print('Connected to AP with IP: {}'.format(ip_address))
  574. except DUT.ExpectTimeout:
  575. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  576. dut1.expect('Starting Advanced OTA example', timeout=30)
  577. print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + bin_name))
  578. dut1.expect('Started advertising.', timeout=30)
  579. print('Started GAP advertising.')
  580. dut1.write('https://' + host_ip + ':' + str(server_port) + '/' + bin_name)
  581. dut1.expect('Loaded app from partition at offset', timeout=60)
  582. dut1.expect('Starting Advanced OTA example', timeout=30)
  583. dut1.reset()
  584. @ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
  585. def test_examples_protocol_advanced_https_ota_example_openssl_aligned_bin(env, extra_data):
  586. """
  587. This is a test case for esp_http_client_read with binary size multiple of 289 bytes
  588. steps: |
  589. 1. join AP
  590. 2. Fetch OTA image over HTTPS
  591. 3. Reboot with the new OTA image
  592. """
  593. dut1 = env.get_dut('advanced_https_ota_example', 'examples/system/ota/advanced_https_ota', dut_class=ttfw_idf.ESP32DUT)
  594. # Original binary file generated after compilation
  595. bin_name = 'advanced_https_ota.bin'
  596. # Binary file aligned to DEFAULT_OTA_BUF_SIZE(289 bytes) boundary
  597. aligned_bin_name = 'aligned.bin'
  598. # check and log bin size
  599. binary_file = os.path.join(dut1.app.binary_path, bin_name)
  600. # Original binary size
  601. bin_size = os.path.getsize(binary_file)
  602. # Dummy data required to align binary size to 289 bytes boundary
  603. dummy_data_size = 289 - (bin_size % 289)
  604. f = open(binary_file, 'rb+')
  605. fo = open(os.path.join(dut1.app.binary_path, aligned_bin_name), 'wb+')
  606. fo.write(f.read(bin_size))
  607. for _ in range(dummy_data_size):
  608. fo.write(struct.pack('B', random.randrange(0,255,1)))
  609. fo.close()
  610. f.close()
  611. # start test
  612. host_ip = get_my_ip()
  613. chunked_server = start_chunked_server(dut1.app.binary_path, 8070)
  614. dut1.start_app()
  615. dut1.expect('Loaded app from partition at offset', timeout=30)
  616. try:
  617. ip_address = dut1.expect(re.compile(r' sta ip: ([^,]+),'), timeout=30)
  618. print('Connected to AP with IP: {}'.format(ip_address))
  619. except DUT.ExpectTimeout:
  620. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  621. dut1.expect('Starting Advanced OTA example', timeout=30)
  622. print('writing to device: {}'.format('https://' + host_ip + ':8070/' + aligned_bin_name))
  623. dut1.write('https://' + host_ip + ':8070/' + aligned_bin_name)
  624. dut1.expect('Loaded app from partition at offset', timeout=60)
  625. dut1.expect('Starting Advanced OTA example', timeout=30)
  626. chunked_server.kill()
  627. os.remove(os.path.join(dut1.app.binary_path, 'server_cert.pem'))
  628. os.remove(os.path.join(dut1.app.binary_path, 'server_key.pem'))
  629. os.remove(aligned_bin_name)
  630. if __name__ == '__main__':
  631. test_examples_protocol_advanced_https_ota_example()
  632. test_examples_protocol_advanced_https_ota_example_chunked()
  633. test_examples_protocol_advanced_https_ota_example_redirect_url()
  634. test_examples_protocol_advanced_https_ota_example_truncated_bin()
  635. test_examples_protocol_advanced_https_ota_example_truncated_header()
  636. test_examples_protocol_advanced_https_ota_example_random()
  637. test_examples_protocol_advanced_https_ota_example_anti_rollback()
  638. test_examples_protocol_advanced_https_ota_example_partial_request()
  639. test_examples_protocol_advanced_https_ota_example_nimble_gatts()
  640. test_examples_protocol_advanced_https_ota_example_bluedroid_gatts()
  641. test_examples_protocol_advanced_https_ota_example_openssl_aligned_bin()