example_test.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. import http.server
  2. import multiprocessing
  3. import os
  4. import random
  5. import re
  6. import socket
  7. import ssl
  8. import struct
  9. import subprocess
  10. import ttfw_idf
  11. from RangeHTTPServer import RangeRequestHandler
  12. from tiny_test_fw import DUT, Utility
  13. server_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'test_certs/server_cert.pem')
  14. key_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'test_certs/server_key.pem')
  15. def get_my_ip():
  16. s1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  17. s1.connect(('8.8.8.8', 80))
  18. my_ip = s1.getsockname()[0]
  19. s1.close()
  20. return my_ip
  21. def get_server_status(host_ip, port):
  22. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  23. server_status = sock.connect_ex((host_ip, port))
  24. sock.close()
  25. if server_status == 0:
  26. return True
  27. return False
  28. def https_request_handler():
  29. """
  30. Returns a request handler class that handles broken pipe exception
  31. """
  32. class RequestHandler(RangeRequestHandler):
  33. def finish(self):
  34. try:
  35. if not self.wfile.closed:
  36. self.wfile.flush()
  37. self.wfile.close()
  38. except socket.error:
  39. pass
  40. self.rfile.close()
  41. def handle(self):
  42. try:
  43. RangeRequestHandler.handle(self)
  44. except socket.error:
  45. pass
  46. return RequestHandler
  47. def start_https_server(ota_image_dir, server_ip, server_port):
  48. os.chdir(ota_image_dir)
  49. requestHandler = https_request_handler()
  50. httpd = http.server.HTTPServer((server_ip, server_port), requestHandler)
  51. httpd.socket = ssl.wrap_socket(httpd.socket,
  52. keyfile=key_file,
  53. certfile=server_file, server_side=True)
  54. httpd.serve_forever()
  55. def start_chunked_server(ota_image_dir, server_port):
  56. os.chdir(ota_image_dir)
  57. chunked_server = subprocess.Popen(['openssl', 's_server', '-WWW', '-key', key_file, '-cert', server_file, '-port', str(server_port)])
  58. return chunked_server
  59. def redirect_handler_factory(url):
  60. """
  61. Returns a request handler class that redirects to supplied `url`
  62. """
  63. class RedirectHandler(http.server.SimpleHTTPRequestHandler):
  64. def do_GET(self):
  65. print('Sending resp, URL: ' + url)
  66. self.send_response(301)
  67. self.send_header('Location', url)
  68. self.end_headers()
  69. def handle(self):
  70. try:
  71. http.server.BaseHTTPRequestHandler.handle(self)
  72. except socket.error:
  73. pass
  74. return RedirectHandler
  75. def start_redirect_server(ota_image_dir, server_ip, server_port, redirection_port):
  76. os.chdir(ota_image_dir)
  77. redirectHandler = redirect_handler_factory('https://' + server_ip + ':' + str(redirection_port) + '/advanced_https_ota.bin')
  78. httpd = http.server.HTTPServer((server_ip, server_port), redirectHandler)
  79. httpd.socket = ssl.wrap_socket(httpd.socket,
  80. keyfile=key_file,
  81. certfile=server_file, server_side=True)
  82. httpd.serve_forever()
  83. @ttfw_idf.idf_example_test(env_tag='EXAMPLE_ETH_OTA')
  84. def test_examples_protocol_advanced_https_ota_example(env, extra_data):
  85. """
  86. This is a positive test case, which downloads complete binary file multiple number of times.
  87. Number of iterations can be specified in variable iterations.
  88. steps: |
  89. 1. join AP
  90. 2. Fetch OTA image over HTTPS
  91. 3. Reboot with the new OTA image
  92. """
  93. dut1 = env.get_dut('advanced_https_ota_example', 'examples/system/ota/advanced_https_ota', dut_class=ttfw_idf.ESP32DUT)
  94. # Number of iterations to validate OTA
  95. iterations = 3
  96. server_port = 8001
  97. # File to be downloaded. This file is generated after compilation
  98. bin_name = 'advanced_https_ota.bin'
  99. # check and log bin size
  100. binary_file = os.path.join(dut1.app.binary_path, bin_name)
  101. bin_size = os.path.getsize(binary_file)
  102. ttfw_idf.log_performance('advanced_https_ota_bin_size', '{}KB'.format(bin_size // 1024))
  103. # start test
  104. host_ip = get_my_ip()
  105. if (get_server_status(host_ip, server_port) is False):
  106. thread1 = multiprocessing.Process(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
  107. thread1.daemon = True
  108. thread1.start()
  109. dut1.start_app()
  110. for i in range(iterations):
  111. dut1.expect('Loaded app from partition at offset', timeout=30)
  112. try:
  113. ip_address = dut1.expect(re.compile(r' (sta|eth) ip: ([^,]+),'), timeout=30)
  114. print('Connected to AP with IP: {}'.format(ip_address))
  115. except DUT.ExpectTimeout:
  116. thread1.terminate()
  117. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  118. dut1.expect('Starting Advanced OTA example', timeout=30)
  119. print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + bin_name))
  120. dut1.write('https://' + host_ip + ':' + str(server_port) + '/' + bin_name)
  121. dut1.expect('Loaded app from partition at offset', timeout=60)
  122. dut1.expect('Starting Advanced OTA example', timeout=30)
  123. dut1.reset()
  124. thread1.terminate()
  125. @ttfw_idf.idf_example_test(env_tag='EXAMPLE_ETH_OTA')
  126. def test_examples_protocol_advanced_https_ota_example_truncated_bin(env, extra_data):
  127. """
  128. Working of OTA if binary file is truncated is validated in this test case.
  129. Application should return with error message in this case.
  130. steps: |
  131. 1. join AP
  132. 2. Generate truncated binary file
  133. 3. Fetch OTA image over HTTPS
  134. 4. Check working of code if bin is truncated
  135. """
  136. dut1 = env.get_dut('advanced_https_ota_example', 'examples/system/ota/advanced_https_ota', dut_class=ttfw_idf.ESP32DUT)
  137. server_port = 8001
  138. # Original binary file generated after compilation
  139. bin_name = 'advanced_https_ota.bin'
  140. # Truncated binary file to be generated from original binary file
  141. truncated_bin_name = 'truncated.bin'
  142. # Size of truncated file to be grnerated. This value can range from 288 bytes (Image header size) to size of original binary file
  143. # truncated_bin_size is set to 64000 to reduce consumed by the test case
  144. truncated_bin_size = 64000
  145. # check and log bin size
  146. binary_file = os.path.join(dut1.app.binary_path, bin_name)
  147. with open(binary_file, 'rb+') as f:
  148. with open(os.path.join(dut1.app.binary_path, truncated_bin_name), 'wb+') as fo:
  149. fo.write(f.read(truncated_bin_size))
  150. binary_file = os.path.join(dut1.app.binary_path, truncated_bin_name)
  151. bin_size = os.path.getsize(binary_file)
  152. ttfw_idf.log_performance('advanced_https_ota_bin_size', '{}KB'.format(bin_size // 1024))
  153. # start test
  154. host_ip = get_my_ip()
  155. if (get_server_status(host_ip, server_port) is False):
  156. thread1 = multiprocessing.Process(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
  157. thread1.daemon = True
  158. thread1.start()
  159. dut1.start_app()
  160. dut1.expect('Loaded app from partition at offset', timeout=30)
  161. try:
  162. ip_address = dut1.expect(re.compile(r' (sta|eth) ip: ([^,]+),'), timeout=30)
  163. print('Connected to AP with IP: {}'.format(ip_address))
  164. except DUT.ExpectTimeout:
  165. thread1.terminate()
  166. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  167. dut1.expect('Starting Advanced OTA example', timeout=30)
  168. print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + truncated_bin_name))
  169. dut1.write('https://' + host_ip + ':' + str(server_port) + '/' + truncated_bin_name)
  170. dut1.expect('Image validation failed, image is corrupted', timeout=30)
  171. try:
  172. os.remove(binary_file)
  173. except OSError:
  174. pass
  175. thread1.terminate()
  176. @ttfw_idf.idf_example_test(env_tag='EXAMPLE_ETH_OTA')
  177. def test_examples_protocol_advanced_https_ota_example_truncated_header(env, extra_data):
  178. """
  179. Working of OTA if headers of binary file are truncated is vaildated in this test case.
  180. Application should return with error message in this case.
  181. steps: |
  182. 1. join AP
  183. 2. Generate binary file with truncated headers
  184. 3. Fetch OTA image over HTTPS
  185. 4. Check working of code if headers are not sent completely
  186. """
  187. dut1 = env.get_dut('advanced_https_ota_example', 'examples/system/ota/advanced_https_ota', dut_class=ttfw_idf.ESP32DUT)
  188. server_port = 8001
  189. # Original binary file generated after compilation
  190. bin_name = 'advanced_https_ota.bin'
  191. # Truncated binary file to be generated from original binary file
  192. truncated_bin_name = 'truncated_header.bin'
  193. # Size of truncated file to be grnerated. This value should be less than 288 bytes (Image header size)
  194. truncated_bin_size = 180
  195. # check and log bin size
  196. binary_file = os.path.join(dut1.app.binary_path, bin_name)
  197. with open(binary_file, 'rb+') as f:
  198. with open(os.path.join(dut1.app.binary_path, truncated_bin_name), 'wb+') as fo:
  199. fo.write(f.read(truncated_bin_size))
  200. binary_file = os.path.join(dut1.app.binary_path, truncated_bin_name)
  201. bin_size = os.path.getsize(binary_file)
  202. ttfw_idf.log_performance('advanced_https_ota_bin_size', '{}KB'.format(bin_size // 1024))
  203. # start test
  204. host_ip = get_my_ip()
  205. if (get_server_status(host_ip, server_port) is False):
  206. thread1 = multiprocessing.Process(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
  207. thread1.daemon = True
  208. thread1.start()
  209. dut1.start_app()
  210. dut1.expect('Loaded app from partition at offset', timeout=30)
  211. try:
  212. ip_address = dut1.expect(re.compile(r' (sta|eth) ip: ([^,]+),'), timeout=30)
  213. print('Connected to AP with IP: {}'.format(ip_address))
  214. except DUT.ExpectTimeout:
  215. thread1.terminate()
  216. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  217. dut1.expect('Starting Advanced OTA example', timeout=30)
  218. print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + truncated_bin_name))
  219. dut1.write('https://' + host_ip + ':' + str(server_port) + '/' + truncated_bin_name)
  220. dut1.expect('advanced_https_ota_example: esp_https_ota_read_img_desc failed', timeout=30)
  221. try:
  222. os.remove(binary_file)
  223. except OSError:
  224. pass
  225. thread1.terminate()
  226. @ttfw_idf.idf_example_test(env_tag='EXAMPLE_ETH_OTA')
  227. def test_examples_protocol_advanced_https_ota_example_random(env, extra_data):
  228. """
  229. Working of OTA if random data is added in binary file are validated in this test case.
  230. Magic byte verification should fail in this case.
  231. steps: |
  232. 1. join AP
  233. 2. Generate random binary image
  234. 3. Fetch OTA image over HTTPS
  235. 4. Check working of code for random binary file
  236. """
  237. dut1 = env.get_dut('advanced_https_ota_example', 'examples/system/ota/advanced_https_ota', dut_class=ttfw_idf.ESP32DUT)
  238. server_port = 8001
  239. # Random binary file to be generated
  240. random_bin_name = 'random.bin'
  241. # Size of random binary file. 32000 is choosen, to reduce the time required to run the test-case
  242. random_bin_size = 32000
  243. # check and log bin size
  244. binary_file = os.path.join(dut1.app.binary_path, random_bin_name)
  245. with open(binary_file, 'wb+') as fo:
  246. # First byte of binary file is always set to zero. If first byte is generated randomly,
  247. # in some cases it may generate 0xE9 which will result in failure of testcase.
  248. fo.write(struct.pack('B', 0))
  249. for i in range(random_bin_size - 1):
  250. fo.write(struct.pack('B', random.randrange(0,255,1)))
  251. bin_size = os.path.getsize(binary_file)
  252. ttfw_idf.log_performance('advanced_https_ota_bin_size', '{}KB'.format(bin_size // 1024))
  253. # start test
  254. host_ip = get_my_ip()
  255. if (get_server_status(host_ip, server_port) is False):
  256. thread1 = multiprocessing.Process(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
  257. thread1.daemon = True
  258. thread1.start()
  259. dut1.start_app()
  260. dut1.expect('Loaded app from partition at offset', timeout=30)
  261. try:
  262. ip_address = dut1.expect(re.compile(r' (sta|eth) ip: ([^,]+),'), timeout=30)
  263. print('Connected to AP with IP: {}'.format(ip_address))
  264. except DUT.ExpectTimeout:
  265. thread1.terminate()
  266. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  267. dut1.expect('Starting Advanced OTA example', timeout=30)
  268. print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + random_bin_name))
  269. dut1.write('https://' + host_ip + ':' + str(server_port) + '/' + random_bin_name)
  270. dut1.expect(re.compile(r'esp_https_ota: Incorrect app descriptor magic'), timeout=10)
  271. try:
  272. os.remove(binary_file)
  273. except OSError:
  274. pass
  275. thread1.terminate()
  276. @ttfw_idf.idf_example_test(env_tag='EXAMPLE_ETH_OTA')
  277. def test_examples_protocol_advanced_https_ota_example_invalid_chip_id(env, extra_data):
  278. """
  279. Working of OTA if binary file have invalid chip id is validated in this test case.
  280. Chip id verification should fail in this case.
  281. steps: |
  282. 1. join AP
  283. 2. Generate binary image with invalid chip id
  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. bin_name = 'advanced_https_ota.bin'
  290. # Random binary file to be generated
  291. random_bin_name = 'random.bin'
  292. random_binary_file = os.path.join(dut1.app.binary_path, random_bin_name)
  293. # Size of random binary file. 2000 is choosen, to reduce the time required to run the test-case
  294. random_bin_size = 2000
  295. binary_file = os.path.join(dut1.app.binary_path, bin_name)
  296. with open(binary_file, 'rb+') as f:
  297. data = list(f.read(random_bin_size))
  298. # Changing Chip id
  299. data[13] = 0xfe
  300. with open(random_binary_file, 'wb+') as fo:
  301. fo.write(bytearray(data))
  302. # start test
  303. host_ip = get_my_ip()
  304. if (get_server_status(host_ip, server_port) is False):
  305. thread1 = multiprocessing.Process(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
  306. thread1.daemon = True
  307. thread1.start()
  308. dut1.start_app()
  309. dut1.expect('Loaded app from partition at offset', timeout=30)
  310. try:
  311. ip_address = dut1.expect(re.compile(r' (sta|eth) ip: ([^,]+),'), timeout=30)
  312. print('Connected to AP with IP: {}'.format(ip_address))
  313. except DUT.ExpectTimeout:
  314. thread1.terminate()
  315. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  316. dut1.expect('Starting Advanced OTA example', timeout=30)
  317. print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + random_bin_name))
  318. dut1.write('https://' + host_ip + ':' + str(server_port) + '/' + random_bin_name)
  319. dut1.expect(re.compile(r'esp_https_ota: Mismatch chip id, expected 0, found \d'), timeout=10)
  320. try:
  321. os.remove(random_binary_file)
  322. except OSError:
  323. pass
  324. thread1.terminate()
  325. @ttfw_idf.idf_example_test(env_tag='EXAMPLE_ETH_OTA')
  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. # start test
  343. host_ip = get_my_ip()
  344. chunked_server = start_chunked_server(dut1.app.binary_path, 8070)
  345. dut1.start_app()
  346. dut1.expect('Loaded app from partition at offset', timeout=30)
  347. try:
  348. ip_address = dut1.expect(re.compile(r' (sta|eth) ip: ([^,]+),'), timeout=30)
  349. print('Connected to AP with IP: {}'.format(ip_address))
  350. except DUT.ExpectTimeout:
  351. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  352. dut1.expect('Starting Advanced OTA example', timeout=30)
  353. print('writing to device: {}'.format('https://' + host_ip + ':8070/' + bin_name))
  354. dut1.write('https://' + host_ip + ':8070/' + bin_name)
  355. dut1.expect('Loaded app from partition at offset', timeout=60)
  356. dut1.expect('Starting Advanced OTA example', timeout=30)
  357. chunked_server.kill()
  358. @ttfw_idf.idf_example_test(env_tag='EXAMPLE_ETH_OTA')
  359. def test_examples_protocol_advanced_https_ota_example_redirect_url(env, extra_data):
  360. """
  361. This is a positive test case, which starts a server and a redirection server.
  362. Redirection server redirects http_request to different port
  363. Number of iterations can be specified in variable iterations.
  364. steps: |
  365. 1. join AP
  366. 2. Fetch OTA image over HTTPS
  367. 3. Reboot with the new OTA image
  368. """
  369. dut1 = env.get_dut('advanced_https_ota_example', 'examples/system/ota/advanced_https_ota', dut_class=ttfw_idf.ESP32DUT)
  370. server_port = 8001
  371. # Port to which the request should be redirected
  372. redirection_server_port = 8081
  373. redirection_server_port1 = 8082
  374. # File to be downloaded. This file is generated after compilation
  375. bin_name = 'advanced_https_ota.bin'
  376. # check and log bin size
  377. binary_file = os.path.join(dut1.app.binary_path, bin_name)
  378. bin_size = os.path.getsize(binary_file)
  379. ttfw_idf.log_performance('advanced_https_ota_bin_size', '{}KB'.format(bin_size // 1024))
  380. # start test
  381. host_ip = get_my_ip()
  382. if (get_server_status(host_ip, server_port) is False):
  383. thread1 = multiprocessing.Process(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
  384. thread1.daemon = True
  385. thread1.start()
  386. thread2 = multiprocessing.Process(target=start_redirect_server, args=(dut1.app.binary_path, host_ip, redirection_server_port, redirection_server_port1))
  387. thread2.daemon = True
  388. thread2.start()
  389. thread3 = multiprocessing.Process(target=start_redirect_server, args=(dut1.app.binary_path, host_ip, redirection_server_port1, server_port))
  390. thread3.daemon = True
  391. thread3.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|eth) ip: ([^,]+),'), timeout=30)
  396. print('Connected to AP with IP: {}'.format(ip_address))
  397. except DUT.ExpectTimeout:
  398. thread1.terminate()
  399. thread2.terminate()
  400. thread3.terminate()
  401. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  402. dut1.expect('Starting Advanced OTA example', timeout=30)
  403. print('writing to device: {}'.format('https://' + host_ip + ':' + str(redirection_server_port) + '/' + bin_name))
  404. dut1.write('https://' + host_ip + ':' + str(redirection_server_port) + '/' + bin_name)
  405. dut1.expect('Loaded app from partition at offset', timeout=60)
  406. dut1.expect('Starting Advanced OTA example', timeout=30)
  407. dut1.reset()
  408. thread1.terminate()
  409. thread2.terminate()
  410. thread3.terminate()
  411. @ttfw_idf.idf_example_test(env_tag='Example_8Mflash_Ethernet')
  412. def test_examples_protocol_advanced_https_ota_example_anti_rollback(env, extra_data):
  413. """
  414. Working of OTA when anti_rollback is enabled and security version of new image is less than current one.
  415. Application should return with error message in this case.
  416. steps: |
  417. 1. join AP
  418. 2. Generate binary file with lower security version
  419. 3. Fetch OTA image over HTTPS
  420. 4. Check working of anti_rollback feature
  421. """
  422. dut1 = env.get_dut('advanced_https_ota_example', 'examples/system/ota/advanced_https_ota', dut_class=ttfw_idf.ESP32DUT, app_config_name='anti_rollback')
  423. Utility.console_log('Erasing the flash on the chip')
  424. # erase the flash
  425. dut1.erase_flash()
  426. server_port = 8001
  427. # Original binary file generated after compilation
  428. bin_name = 'advanced_https_ota.bin'
  429. # Modified firmware image to lower security version in its header. This is to enable negative test case
  430. anti_rollback_bin_name = 'advanced_https_ota_lower_sec_version.bin'
  431. # check and log bin size
  432. binary_file = os.path.join(dut1.app.binary_path, bin_name)
  433. file_size = os.path.getsize(binary_file)
  434. with open(binary_file, 'rb+') as f:
  435. with open(os.path.join(dut1.app.binary_path, anti_rollback_bin_name), 'wb+') as fo:
  436. fo.write(f.read(file_size))
  437. # Change security_version to 0 for negative test case
  438. fo.seek(36)
  439. fo.write(b'\x00')
  440. binary_file = os.path.join(dut1.app.binary_path, anti_rollback_bin_name)
  441. bin_size = os.path.getsize(binary_file)
  442. ttfw_idf.log_performance('advanced_https_ota_bin_size', '{}KB'.format(bin_size // 1024))
  443. # start test
  444. host_ip = get_my_ip()
  445. if (get_server_status(host_ip, server_port) is False):
  446. thread1 = multiprocessing.Process(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
  447. thread1.daemon = True
  448. thread1.start()
  449. dut1.start_app()
  450. # Positive Case
  451. dut1.expect('Loaded app from partition at offset', timeout=30)
  452. try:
  453. ip_address = dut1.expect(re.compile(r' (sta|eth) ip: ([^,]+),'), timeout=30)
  454. print('Connected to AP with IP: {}'.format(ip_address))
  455. except DUT.ExpectTimeout:
  456. thread1.terminate()
  457. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  458. dut1.expect('Starting Advanced OTA example', timeout=30)
  459. # Use originally generated image with secure_version=1
  460. print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + bin_name))
  461. dut1.write('https://' + host_ip + ':' + str(server_port) + '/' + bin_name)
  462. dut1.expect('Loaded app from partition at offset', timeout=60)
  463. dut1.expect(re.compile(r' (sta|eth) ip: ([^,]+),'), timeout=30)
  464. dut1.expect('App is valid, rollback cancelled successfully', 30)
  465. # Negative Case
  466. dut1.expect('Starting Advanced OTA example', timeout=30)
  467. # Use modified image with secure_version=0
  468. print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + anti_rollback_bin_name))
  469. dut1.write('https://' + host_ip + ':' + str(server_port) + '/' + anti_rollback_bin_name)
  470. dut1.expect('New firmware security version is less than eFuse programmed, 0 < 1', timeout=30)
  471. try:
  472. os.remove(binary_file)
  473. except OSError:
  474. pass
  475. thread1.terminate()
  476. @ttfw_idf.idf_example_test(env_tag='EXAMPLE_ETH_OTA')
  477. def test_examples_protocol_advanced_https_ota_example_partial_request(env, extra_data):
  478. """
  479. This is a positive test case, to test OTA workflow with Range HTTP header.
  480. steps: |
  481. 1. join AP
  482. 2. Fetch OTA image over HTTPS
  483. 3. Reboot with the new OTA image
  484. """
  485. dut1 = env.get_dut('advanced_https_ota_example', 'examples/system/ota/advanced_https_ota', dut_class=ttfw_idf.ESP32DUT, app_config_name='partial_download')
  486. server_port = 8001
  487. # Size of partial HTTP request
  488. request_size = 16384
  489. # File to be downloaded. This file is generated after compilation
  490. bin_name = 'advanced_https_ota.bin'
  491. # check and log bin size
  492. binary_file = os.path.join(dut1.app.binary_path, bin_name)
  493. bin_size = os.path.getsize(binary_file)
  494. ttfw_idf.log_performance('advanced_https_ota_bin_size', '{}KB'.format(bin_size // 1024))
  495. http_requests = int((bin_size / request_size) - 1)
  496. # start test
  497. host_ip = get_my_ip()
  498. if (get_server_status(host_ip, server_port) is False):
  499. thread1 = multiprocessing.Process(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
  500. thread1.daemon = True
  501. thread1.start()
  502. dut1.start_app()
  503. dut1.expect('Loaded app from partition at offset', timeout=30)
  504. try:
  505. ip_address = dut1.expect(re.compile(r' (sta|eth) ip: ([^,]+),'), timeout=30)
  506. print('Connected to AP with IP: {}'.format(ip_address))
  507. except DUT.ExpectTimeout:
  508. Utility.console_log('ENV_TEST_FAILURE: Cannot connect to AP')
  509. thread1.terminate()
  510. raise
  511. dut1.expect('Starting Advanced OTA example', timeout=30)
  512. print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + bin_name))
  513. dut1.write('https://' + host_ip + ':' + str(server_port) + '/' + bin_name)
  514. for _ in range(http_requests):
  515. dut1.expect('Connection closed', timeout=60)
  516. dut1.expect('Loaded app from partition at offset', timeout=60)
  517. dut1.expect('Starting Advanced OTA example', timeout=30)
  518. dut1.reset()
  519. thread1.terminate()
  520. @ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA', nightly_run=True)
  521. def test_examples_protocol_advanced_https_ota_example_nimble_gatts(env, extra_data):
  522. """
  523. Run an OTA image update while a BLE GATT Server is running in background. This GATT server will be using NimBLE Host stack.
  524. steps: |
  525. 1. join AP
  526. 2. Run BLE advertise and then GATT server.
  527. 3. Fetch OTA image over HTTPS
  528. 4. Reboot with the new OTA image
  529. """
  530. dut1 = env.get_dut('advanced_https_ota_example', 'examples/system/ota/advanced_https_ota', dut_class=ttfw_idf.ESP32DUT, app_config_name='nimble')
  531. server_port = 8001
  532. # File to be downloaded. This file is generated after compilation
  533. bin_name = 'advanced_https_ota.bin'
  534. # check and log bin size
  535. binary_file = os.path.join(dut1.app.binary_path, bin_name)
  536. bin_size = os.path.getsize(binary_file)
  537. ttfw_idf.log_performance('advanced_https_ota_bin_size', '{}KB'.format(bin_size // 1024))
  538. # start test
  539. host_ip = get_my_ip()
  540. if (get_server_status(host_ip, server_port) is False):
  541. thread1 = multiprocessing.Process(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
  542. thread1.daemon = True
  543. thread1.start()
  544. dut1.start_app()
  545. dut1.expect('Loaded app from partition at offset', timeout=30)
  546. try:
  547. ip_address = dut1.expect(re.compile(r' sta ip: ([^,]+),'), timeout=30)
  548. print('Connected to AP with IP: {}'.format(ip_address))
  549. except DUT.ExpectTimeout:
  550. thread1.terminate()
  551. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  552. dut1.expect('Starting Advanced OTA example', timeout=30)
  553. print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + bin_name))
  554. print('Started GAP advertising.')
  555. dut1.write('https://' + host_ip + ':' + str(server_port) + '/' + bin_name)
  556. dut1.expect('Loaded app from partition at offset', timeout=60)
  557. dut1.expect('Starting Advanced OTA example', timeout=30)
  558. dut1.reset()
  559. thread1.terminate()
  560. @ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA', nightly_run=True)
  561. def test_examples_protocol_advanced_https_ota_example_bluedroid_gatts(env, extra_data):
  562. """
  563. Run an OTA image update while a BLE GATT Server is running in background. This GATT server will be using Bluedroid Host stack.
  564. steps: |
  565. 1. join AP
  566. 2. Run BLE advertise and then GATT server.
  567. 3. Fetch OTA image over HTTPS
  568. 4. Reboot with the new OTA image
  569. """
  570. dut1 = env.get_dut('advanced_https_ota_example', 'examples/system/ota/advanced_https_ota', dut_class=ttfw_idf.ESP32DUT, app_config_name='bluedroid')
  571. server_port = 8001
  572. # File to be downloaded. This file is generated after compilation
  573. bin_name = 'advanced_https_ota.bin'
  574. # check and log bin size
  575. binary_file = os.path.join(dut1.app.binary_path, bin_name)
  576. bin_size = os.path.getsize(binary_file)
  577. ttfw_idf.log_performance('advanced_https_ota_bin_size', '{}KB'.format(bin_size // 1024))
  578. # start test
  579. host_ip = get_my_ip()
  580. if (get_server_status(host_ip, server_port) is False):
  581. thread1 = multiprocessing.Process(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
  582. thread1.daemon = True
  583. thread1.start()
  584. dut1.start_app()
  585. dut1.expect('Loaded app from partition at offset', timeout=30)
  586. try:
  587. ip_address = dut1.expect(re.compile(r' sta ip: ([^,]+),'), timeout=30)
  588. print('Connected to AP with IP: {}'.format(ip_address))
  589. except DUT.ExpectTimeout:
  590. thread1.terminate()
  591. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  592. dut1.expect('Starting Advanced OTA example', timeout=30)
  593. print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + bin_name))
  594. dut1.expect('Started advertising.', timeout=30)
  595. print('Started GAP advertising.')
  596. dut1.write('https://' + host_ip + ':' + str(server_port) + '/' + bin_name)
  597. dut1.expect('Loaded app from partition at offset', timeout=60)
  598. dut1.expect('Starting Advanced OTA example', timeout=30)
  599. dut1.reset()
  600. thread1.terminate()
  601. @ttfw_idf.idf_example_test(env_tag='EXAMPLE_ETH_OTA')
  602. def test_examples_protocol_advanced_https_ota_example_openssl_aligned_bin(env, extra_data):
  603. """
  604. This is a test case for esp_http_client_read with binary size multiple of 289 bytes
  605. steps: |
  606. 1. join AP
  607. 2. Fetch OTA image over HTTPS
  608. 3. Reboot with the new OTA image
  609. """
  610. dut1 = env.get_dut('advanced_https_ota_example', 'examples/system/ota/advanced_https_ota', dut_class=ttfw_idf.ESP32DUT)
  611. # Original binary file generated after compilation
  612. bin_name = 'advanced_https_ota.bin'
  613. # Binary file aligned to DEFAULT_OTA_BUF_SIZE(289 bytes) boundary
  614. aligned_bin_name = 'aligned.bin'
  615. # check and log bin size
  616. binary_file = os.path.join(dut1.app.binary_path, bin_name)
  617. # Original binary size
  618. bin_size = os.path.getsize(binary_file)
  619. # Dummy data required to align binary size to 289 bytes boundary
  620. dummy_data_size = 289 - (bin_size % 289)
  621. with open(binary_file, 'rb+') as f:
  622. with open(os.path.join(dut1.app.binary_path, aligned_bin_name), 'wb+') as fo:
  623. fo.write(f.read(bin_size))
  624. for _ in range(dummy_data_size):
  625. fo.write(struct.pack('B', random.randrange(0,255,1)))
  626. # start test
  627. host_ip = get_my_ip()
  628. chunked_server = start_chunked_server(dut1.app.binary_path, 8070)
  629. dut1.start_app()
  630. dut1.expect('Loaded app from partition at offset', timeout=30)
  631. try:
  632. ip_address = dut1.expect(re.compile(r' (sta|eth) ip: ([^,]+),'), timeout=30)
  633. print('Connected to AP with IP: {}'.format(ip_address))
  634. except DUT.ExpectTimeout:
  635. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  636. dut1.expect('Starting Advanced OTA example', timeout=30)
  637. print('writing to device: {}'.format('https://' + host_ip + ':8070/' + aligned_bin_name))
  638. dut1.write('https://' + host_ip + ':8070/' + aligned_bin_name)
  639. dut1.expect('Loaded app from partition at offset', timeout=60)
  640. dut1.expect('Starting Advanced OTA example', timeout=30)
  641. chunked_server.kill()
  642. try:
  643. os.remove(aligned_bin_name)
  644. except OSError:
  645. pass
  646. if __name__ == '__main__':
  647. test_examples_protocol_advanced_https_ota_example()
  648. test_examples_protocol_advanced_https_ota_example_chunked()
  649. test_examples_protocol_advanced_https_ota_example_redirect_url()
  650. test_examples_protocol_advanced_https_ota_example_truncated_bin()
  651. test_examples_protocol_advanced_https_ota_example_truncated_header()
  652. test_examples_protocol_advanced_https_ota_example_random()
  653. test_examples_protocol_advanced_https_ota_example_invalid_chip_id()
  654. test_examples_protocol_advanced_https_ota_example_anti_rollback()
  655. test_examples_protocol_advanced_https_ota_example_partial_request()
  656. test_examples_protocol_advanced_https_ota_example_nimble_gatts()
  657. test_examples_protocol_advanced_https_ota_example_bluedroid_gatts()
  658. test_examples_protocol_advanced_https_ota_example_openssl_aligned_bin()