example_test.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 tiny_test_fw import DUT
  12. server_cert = '-----BEGIN CERTIFICATE-----\n' \
  13. 'MIIDWDCCAkACCQCbF4+gVh/MLjANBgkqhkiG9w0BAQsFADBuMQswCQYDVQQGEwJJ\n'\
  14. 'TjELMAkGA1UECAwCTUgxDDAKBgNVBAcMA1BVTjEMMAoGA1UECgwDRVNQMQwwCgYD\n'\
  15. 'VQQLDANFU1AxDDAKBgNVBAMMA0VTUDEaMBgGCSqGSIb3DQEJARYLZXNwQGVzcC5j\n'\
  16. 'b20wHhcNMjEwNzEyMTIzNjI3WhcNNDEwNzA3MTIzNjI3WjBuMQswCQYDVQQGEwJJ\n'\
  17. 'TjELMAkGA1UECAwCTUgxDDAKBgNVBAcMA1BVTjEMMAoGA1UECgwDRVNQMQwwCgYD\n'\
  18. 'VQQLDANFU1AxDDAKBgNVBAMMA0VTUDEaMBgGCSqGSIb3DQEJARYLZXNwQGVzcC5j\n'\
  19. 'b20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDhxF/y7bygndxPwiWL\n'\
  20. 'SwS9LY3uBMaJgup0ufNKVhx+FhGQOu44SghuJAaH3KkPUnt6SOM8jC97/yQuc32W\n'\
  21. 'ukI7eBZoA12kargSnzdv5m5rZZpd+NznSSpoDArOAONKVlzr25A1+aZbix2mKRbQ\n'\
  22. 'S5w9o1N2BriQuSzd8gL0Y0zEk3VkOWXEL+0yFUT144HnErnD+xnJtHe11yPO2fEz\n'\
  23. 'YaGiilh0ddL26PXTugXMZN/8fRVHP50P2OG0SvFpC7vghlLp4VFM1/r3UJnvL6Oz\n'\
  24. '3ALc6dhxZEKQucqlpj8l1UegszQToopemtIj0qXTHw2+uUnkUyWIPjPC+wdOAoap\n'\
  25. 'rFTRAgMBAAEwDQYJKoZIhvcNAQELBQADggEBAItw24y565k3C/zENZlxyzto44ud\n'\
  26. 'IYPQXN8Fa2pBlLe1zlSIyuaA/rWQ+i1daS8nPotkCbWZyf5N8DYaTE4B0OfvoUPk\n'\
  27. 'B5uGDmbuk6akvlB5BGiYLfQjWHRsK9/4xjtIqN1H58yf3QNROuKsPAeywWS3Fn32\n'\
  28. '3//OpbWaClQePx6udRYMqAitKR+QxL7/BKZQsX+UyShuq8hjphvXvk0BW8ONzuw9\n'\
  29. 'RcoORxM0FzySYjeQvm4LhzC/P3ZBhEq0xs55aL2a76SJhq5hJy7T/Xz6NFByvlrN\n'\
  30. 'lFJJey33KFrAf5vnV9qcyWFIo7PYy2VsaaEjFeefr7q3sTFSMlJeadexW2Y=\n'\
  31. '-----END CERTIFICATE-----\n'
  32. server_key = '-----BEGIN PRIVATE KEY-----\n'\
  33. 'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDhxF/y7bygndxP\n'\
  34. 'wiWLSwS9LY3uBMaJgup0ufNKVhx+FhGQOu44SghuJAaH3KkPUnt6SOM8jC97/yQu\n'\
  35. 'c32WukI7eBZoA12kargSnzdv5m5rZZpd+NznSSpoDArOAONKVlzr25A1+aZbix2m\n'\
  36. 'KRbQS5w9o1N2BriQuSzd8gL0Y0zEk3VkOWXEL+0yFUT144HnErnD+xnJtHe11yP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6XG3IMBEYdoNJyYhu+nzyEdMg8CwXg+8LC7FMis/Ve+o7aS5scgG\n'\
  52. '1to/N9DK/swCsdTRdzmc/ZDbVC+TuVsebFBGYZTyO5KgqLpezqaIQrTxAoGALFCU\n'\
  53. '10glO9MVyl9H3clap5v+MQ3qcOv/EhaMnw6L2N6WVT481tnxjW4ujgzrFcE4YuxZ\n'\
  54. 'hgwYu9TOCmeqopGwBvGYWLbj+C4mfSahOAs0FfXDoYazuIIGBpuv03UhbpB1Si4O\n'\
  55. 'rJDfRnuCnVWyOTkl54gKJ2OusinhjztBjcrV1XkCgYEA3qNi4uBsPdyz9BZGb/3G\n'\
  56. 'rOMSw0CaT4pEMTLZqURmDP/0hxvTk1polP7O/FYwxVuJnBb6mzDa0xpLFPTpIAnJ\n'\
  57. 'YXB8xpXU69QVh+EBbemdJWOd+zp5UCfXvb2shAeG3Tn/Dz4cBBMEUutbzP+or0nG\n'\
  58. 'vSXnRLaxQhooWm+IuX9SuBQ=\n'\
  59. '-----END PRIVATE KEY-----\n'
  60. def get_my_ip():
  61. s1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  62. s1.connect(('8.8.8.8', 80))
  63. my_ip = s1.getsockname()[0]
  64. s1.close()
  65. return my_ip
  66. def get_server_status(host_ip, port):
  67. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  68. server_status = sock.connect_ex((host_ip, port))
  69. sock.close()
  70. if server_status == 0:
  71. return True
  72. return False
  73. def create_file(server_file, file_data):
  74. with open(server_file, 'w+') as file:
  75. file.write(file_data)
  76. def get_ca_cert(ota_image_dir):
  77. os.chdir(ota_image_dir)
  78. server_file = os.path.join(ota_image_dir, 'server_cert.pem')
  79. create_file(server_file, server_cert)
  80. key_file = os.path.join(ota_image_dir, 'server_key.pem')
  81. create_file(key_file, server_key)
  82. return server_file, key_file
  83. def https_request_handler():
  84. """
  85. Returns a request handler class that handles broken pipe exception
  86. """
  87. class RequestHandler(http.server.SimpleHTTPRequestHandler):
  88. def finish(self):
  89. try:
  90. if not self.wfile.closed:
  91. self.wfile.flush()
  92. self.wfile.close()
  93. except socket.error:
  94. pass
  95. self.rfile.close()
  96. def handle(self):
  97. try:
  98. http.server.BaseHTTPRequestHandler.handle(self)
  99. except socket.error:
  100. pass
  101. return RequestHandler
  102. def start_https_server(ota_image_dir, server_ip, server_port):
  103. server_file, key_file = get_ca_cert(ota_image_dir)
  104. requestHandler = https_request_handler()
  105. httpd = http.server.HTTPServer((server_ip, server_port), requestHandler)
  106. httpd.socket = ssl.wrap_socket(httpd.socket,
  107. keyfile=key_file,
  108. certfile=server_file, server_side=True)
  109. httpd.serve_forever()
  110. def start_chunked_server(ota_image_dir, server_port):
  111. server_file, key_file = get_ca_cert(ota_image_dir)
  112. chunked_server = subprocess.Popen(['openssl', 's_server', '-WWW', '-key', key_file, '-cert', server_file, '-port', str(server_port)])
  113. return chunked_server
  114. @ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
  115. def test_examples_protocol_native_ota_example(env, extra_data):
  116. """
  117. This is a positive test case, which downloads complete binary file multiple number of times.
  118. Number of iterations can be specified in variable iterations.
  119. steps: |
  120. 1. join AP
  121. 2. Fetch OTA image over HTTPS
  122. 3. Reboot with the new OTA image
  123. """
  124. dut1 = env.get_dut('native_ota_example', 'examples/system/ota/native_ota_example', dut_class=ttfw_idf.ESP32DUT)
  125. server_port = 8002
  126. # No. of times working of application to be validated
  127. iterations = 3
  128. # File to be downloaded. This file is generated after compilation
  129. bin_name = 'native_ota.bin'
  130. # check and log bin size
  131. binary_file = os.path.join(dut1.app.binary_path, bin_name)
  132. bin_size = os.path.getsize(binary_file)
  133. ttfw_idf.log_performance('native_ota_bin_size', '{}KB'.format(bin_size // 1024))
  134. # start test
  135. host_ip = get_my_ip()
  136. if (get_server_status(host_ip, server_port) is False):
  137. thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
  138. thread1.daemon = True
  139. thread1.start()
  140. dut1.start_app()
  141. for i in range(iterations):
  142. dut1.expect('Loaded app from partition at offset', timeout=30)
  143. try:
  144. ip_address = dut1.expect(re.compile(r' sta ip: ([^,]+),'), timeout=30)
  145. print('Connected to AP with IP: {}'.format(ip_address))
  146. except DUT.ExpectTimeout:
  147. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  148. thread1.close()
  149. dut1.expect('Starting OTA example', timeout=30)
  150. print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + bin_name))
  151. dut1.write('https://' + host_ip + ':' + str(server_port) + '/' + bin_name)
  152. dut1.expect('Loaded app from partition at offset', timeout=60)
  153. dut1.expect('Starting OTA example', timeout=30)
  154. dut1.reset()
  155. @ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
  156. def test_examples_protocol_native_ota_example_truncated_bin(env, extra_data):
  157. """
  158. Working of OTA if binary file is truncated is validated in this test case.
  159. Application should return with error message in this case.
  160. steps: |
  161. 1. join AP
  162. 2. Generate truncated binary file
  163. 3. Fetch OTA image over HTTPS
  164. 4. Check working of code if bin is truncated
  165. """
  166. dut1 = env.get_dut('native_ota_example', 'examples/system/ota/native_ota_example', dut_class=ttfw_idf.ESP32DUT)
  167. server_port = 8002
  168. # Original binary file generated after compilation
  169. bin_name = 'native_ota.bin'
  170. # Truncated binary file to be generated from original binary file
  171. truncated_bin_name = 'truncated.bin'
  172. # Size of truncated file to be grnerated. This value can range from 288 bytes (Image header size) to size of original binary file
  173. # truncated_bin_size is set to 64000 to reduce consumed by the test case
  174. truncated_bin_size = 64000
  175. # check and log bin size
  176. binary_file = os.path.join(dut1.app.binary_path, bin_name)
  177. f = open(binary_file, 'rb+')
  178. fo = open(os.path.join(dut1.app.binary_path, truncated_bin_name), 'wb+')
  179. fo.write(f.read(truncated_bin_size))
  180. fo.close()
  181. f.close()
  182. binary_file = os.path.join(dut1.app.binary_path, truncated_bin_name)
  183. bin_size = os.path.getsize(binary_file)
  184. ttfw_idf.log_performance('native_ota_bin_size', '{}KB'.format(bin_size // 1024))
  185. # start test
  186. host_ip = get_my_ip()
  187. if (get_server_status(host_ip, server_port) is False):
  188. thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
  189. thread1.daemon = True
  190. thread1.start()
  191. dut1.start_app()
  192. dut1.expect('Loaded app from partition at offset', timeout=30)
  193. try:
  194. ip_address = dut1.expect(re.compile(r' sta ip: ([^,]+),'), timeout=60)
  195. print('Connected to AP with IP: {}'.format(ip_address))
  196. except DUT.ExpectTimeout:
  197. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  198. dut1.expect('Starting OTA example', timeout=30)
  199. print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + truncated_bin_name))
  200. dut1.write('https://' + host_ip + ':' + str(server_port) + '/' + truncated_bin_name)
  201. dut1.expect('native_ota_example: Image validation failed, image is corrupted', timeout=20)
  202. os.remove(binary_file)
  203. @ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
  204. def test_examples_protocol_native_ota_example_truncated_header(env, extra_data):
  205. """
  206. Working of OTA if headers of binary file are truncated is vaildated in this test case.
  207. Application should return with error message in this case.
  208. steps: |
  209. 1. join AP
  210. 2. Generate binary file with truncated headers
  211. 3. Fetch OTA image over HTTPS
  212. 4. Check working of code if headers are not sent completely
  213. """
  214. dut1 = env.get_dut('native_ota_example', 'examples/system/ota/native_ota_example', dut_class=ttfw_idf.ESP32DUT)
  215. server_port = 8002
  216. # Original binary file generated after compilation
  217. bin_name = 'native_ota.bin'
  218. # Truncated binary file to be generated from original binary file
  219. truncated_bin_name = 'truncated_header.bin'
  220. # Size of truncated file to be grnerated. This value should be less than 288 bytes (Image header size)
  221. truncated_bin_size = 180
  222. # check and log bin size
  223. binary_file = os.path.join(dut1.app.binary_path, bin_name)
  224. f = open(binary_file, 'rb+')
  225. fo = open(os.path.join(dut1.app.binary_path, truncated_bin_name), 'wb+')
  226. fo.write(f.read(truncated_bin_size))
  227. fo.close()
  228. f.close()
  229. binary_file = os.path.join(dut1.app.binary_path, truncated_bin_name)
  230. bin_size = os.path.getsize(binary_file)
  231. ttfw_idf.log_performance('native_ota_bin_size', '{}KB'.format(bin_size // 1024))
  232. # start test
  233. host_ip = get_my_ip()
  234. if (get_server_status(host_ip, server_port) is False):
  235. thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
  236. thread1.daemon = True
  237. thread1.start()
  238. dut1.start_app()
  239. dut1.expect('Loaded app from partition at offset', timeout=30)
  240. try:
  241. ip_address = dut1.expect(re.compile(r' sta ip: ([^,]+),'), timeout=60)
  242. print('Connected to AP with IP: {}'.format(ip_address))
  243. except DUT.ExpectTimeout:
  244. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  245. dut1.expect('Starting OTA example', timeout=30)
  246. print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + truncated_bin_name))
  247. dut1.write('https://' + host_ip + ':' + str(server_port) + '/' + truncated_bin_name)
  248. dut1.expect('native_ota_example: received package is not fit len', timeout=20)
  249. os.remove(binary_file)
  250. @ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
  251. def test_examples_protocol_native_ota_example_random(env, extra_data):
  252. """
  253. Working of OTA if random data is added in binary file are validated in this test case.
  254. Magic byte verification should fail in this case.
  255. steps: |
  256. 1. join AP
  257. 2. Generate random binary image
  258. 3. Fetch OTA image over HTTPS
  259. 4. Check working of code for random binary file
  260. """
  261. dut1 = env.get_dut('native_ota_example', 'examples/system/ota/native_ota_example', dut_class=ttfw_idf.ESP32DUT)
  262. server_port = 8002
  263. # Random binary file to be generated
  264. random_bin_name = 'random.bin'
  265. # Size of random binary file. 32000 is choosen, to reduce the time required to run the test-case
  266. random_bin_size = 32000
  267. # check and log bin size
  268. binary_file = os.path.join(dut1.app.binary_path, random_bin_name)
  269. fo = open(binary_file, 'wb+')
  270. # First byte of binary file is always set to zero. If first byte is generated randomly,
  271. # in some cases it may generate 0xE9 which will result in failure of testcase.
  272. fo.write(struct.pack('B', 0))
  273. for i in range(random_bin_size - 1):
  274. fo.write(struct.pack('B', random.randrange(0,255,1)))
  275. fo.close()
  276. bin_size = os.path.getsize(binary_file)
  277. ttfw_idf.log_performance('native_ota_bin_size', '{}KB'.format(bin_size // 1024))
  278. # start test
  279. host_ip = get_my_ip()
  280. if (get_server_status(host_ip, server_port) is False):
  281. thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
  282. thread1.daemon = True
  283. thread1.start()
  284. dut1.start_app()
  285. dut1.expect('Loaded app from partition at offset', timeout=30)
  286. try:
  287. ip_address = dut1.expect(re.compile(r' sta ip: ([^,]+),'), timeout=60)
  288. print('Connected to AP with IP: {}'.format(ip_address))
  289. except DUT.ExpectTimeout:
  290. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  291. dut1.expect('Starting OTA example', timeout=30)
  292. print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + random_bin_name))
  293. dut1.write('https://' + host_ip + ':' + str(server_port) + '/' + random_bin_name)
  294. dut1.expect('esp_ota_ops: OTA image has invalid magic byte', timeout=20)
  295. os.remove(binary_file)
  296. @ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
  297. def test_examples_protocol_native_ota_example_chunked(env, extra_data):
  298. """
  299. This is a positive test case, which downloads complete binary file multiple number of times.
  300. Number of iterations can be specified in variable iterations.
  301. steps: |
  302. 1. join AP
  303. 2. Fetch OTA image over HTTPS
  304. 3. Reboot with the new OTA image
  305. """
  306. dut1 = env.get_dut('native_ota_example', 'examples/system/ota/native_ota_example', dut_class=ttfw_idf.ESP32DUT)
  307. # File to be downloaded. This file is generated after compilation
  308. bin_name = 'native_ota.bin'
  309. # check and log bin size
  310. binary_file = os.path.join(dut1.app.binary_path, bin_name)
  311. bin_size = os.path.getsize(binary_file)
  312. ttfw_idf.log_performance('native_ota_bin_size', '{}KB'.format(bin_size // 1024))
  313. # start test
  314. host_ip = get_my_ip()
  315. chunked_server = start_chunked_server(dut1.app.binary_path, 8070)
  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 OTA example', timeout=30)
  324. print('writing to device: {}'.format('https://' + host_ip + ':8070/' + bin_name))
  325. dut1.write('https://' + host_ip + ':8070/' + bin_name)
  326. dut1.expect('Loaded app from partition at offset', timeout=60)
  327. dut1.expect('Starting OTA example', timeout=30)
  328. chunked_server.kill()
  329. os.remove(os.path.join(dut1.app.binary_path, 'server_cert.pem'))
  330. os.remove(os.path.join(dut1.app.binary_path, 'server_key.pem'))
  331. if __name__ == '__main__':
  332. test_examples_protocol_native_ota_example()
  333. test_examples_protocol_native_ota_example_chunked()
  334. test_examples_protocol_native_ota_example_truncated_bin()
  335. test_examples_protocol_native_ota_example_truncated_header()
  336. test_examples_protocol_native_ota_example_random()