example_test.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import http.server
  2. import os
  3. import re
  4. import socket
  5. import ssl
  6. import sys
  7. from threading import Thread
  8. import ttfw_idf
  9. from tiny_test_fw import DUT
  10. server_cert = '-----BEGIN CERTIFICATE-----\n' \
  11. 'MIIDXTCCAkWgAwIBAgIJAP4LF7E72HakMA0GCSqGSIb3DQEBCwUAMEUxCzAJBgNV\n'\
  12. 'BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX\n'\
  13. 'aWRnaXRzIFB0eSBMdGQwHhcNMTkwNjA3MDk1OTE2WhcNMjAwNjA2MDk1OTE2WjBF\n'\
  14. 'MQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50\n'\
  15. 'ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB\n'\
  16. 'CgKCAQEAlzfCyv3mIv7TlLkObxunKfCdrJ/zgdANrsx0RBtpEPhV560hWJ0fEin0\n'\
  17. 'nIOMpJSiF9E6QsPdr6Q+eogH4XnOMU9JE+iG743N1dPfGEzJvRlyct/Ck8SswKPC\n'\
  18. '9+VXsnOdZmUw9y/xtANbURA/TspvPzz3Avv382ffffrJGh7ooOmaZSCZFlSYHLZA\n'\
  19. 'w/XlRr0sSRbLpFGY0gXjaAV8iHHiPDYLy4kZOepjV9U51xi+IGsL4w75zuMgsHyF\n'\
  20. '3nJeGYHgtGVBrkL0ZKG5udY0wcBjysjubDJC4iSlNiq2HD3fhs7j6CZddV2v845M\n'\
  21. 'lVKNxP0kO4Uj4D8r+5USWC8JKfAwxQIDAQABo1AwTjAdBgNVHQ4EFgQU6OE7ssfY\n'\
  22. 'IIPTDThiUoofUpsD5NwwHwYDVR0jBBgwFoAU6OE7ssfYIIPTDThiUoofUpsD5Nww\n'\
  23. 'DAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAXIlHS/FJWfmcinUAxyBd\n'\
  24. '/xd5Lu8ykeru6oaUCci+Vk9lyoMMES7lQ+b/00d5x7AcTawkTil9EWpBTPTOTraA\n'\
  25. 'lzJMQhNKmSLk0iIoTtAJtSZgUSpIIozqK6lenxQQDsHbXKU6h+u9H6KZE8YcjsFl\n'\
  26. '6vL7sw9BVotw/VxfgjQ5OSGLgoLrdVT0z5C2qOuwOgz1c7jNiJhtMdwN+cOtnJp2\n'\
  27. 'fuBgEYyE3eeuWogvkWoDcIA8r17Ixzkpq2oJsdvZcHZPIZShPKW2SHUsl98KDemu\n'\
  28. 'y0pQyExmQUbwKE4vbFb9XuWCcL9XaOHQytyszt2DeD67AipvoBwVU7/LBOvqnsmy\n'\
  29. 'hA==\n'\
  30. '-----END CERTIFICATE-----\n'
  31. server_key = '-----BEGIN PRIVATE KEY-----\n'\
  32. 'MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCXN8LK/eYi/tOU\n'\
  33. 'uQ5vG6cp8J2sn/OB0A2uzHREG2kQ+FXnrSFYnR8SKfScg4yklKIX0TpCw92vpD56\n'\
  34. 'iAfhec4xT0kT6Ibvjc3V098YTMm9GXJy38KTxKzAo8L35Veyc51mZTD3L/G0A1tR\n'\
  35. 'ED9Oym8/PPcC+/fzZ999+skaHuig6ZplIJkWVJgctkDD9eVGvSxJFsukUZjSBeNo\n'\
  36. 'BXyIceI8NgvLiRk56mNX1TnXGL4gawvjDvnO4yCwfIXecl4ZgeC0ZUGuQvRkobm5\n'\
  37. '1jTBwGPKy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5R2JAoGAIkzO\n'\
  52. 'RmnhEOKTzDex611f2D+yMsMfy5BKK2f4vjLymBH5TiBKDXKqEpgsW0huoi8Gq9Uu\n'\
  53. 'nvvXXAgkIyRYF36f0vUe0nkjLuYAQAWgC2pZYgNLJR13iVbol0xHJoXQUHtgiaJ8\n'\
  54. 'GLYFzjHQPqFMpSalQe3oELko39uOC1CoJCHFySECgYBeycUnRBikCO2n8DNhY4Eg\n'\
  55. '9Y3oxcssRt6ea5BZwgW2eAYi7/XqKkmxoSoOykUt3MJx9+EkkrL17bxFSpkj1tvL\n'\
  56. 'qvxn7egtsKjjgGNAxwXC4MwCvhveyUQQxtQb8AqGrGqo4jEEN0L15cnP38i2x1Uo\n'\
  57. 'muhfskWf4MABV0yTUaKcGg==\n'\
  58. '-----END PRIVATE KEY-----\n'
  59. def get_my_ip():
  60. s1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  61. s1.connect(('8.8.8.8', 80))
  62. my_ip = s1.getsockname()[0]
  63. s1.close()
  64. return my_ip
  65. def start_https_server(ota_image_dir, server_ip, server_port, server_file=None, key_file=None):
  66. os.chdir(ota_image_dir)
  67. if server_file is None:
  68. server_file = os.path.join(ota_image_dir, 'server_cert.pem')
  69. cert_file_handle = open(server_file, 'w+')
  70. cert_file_handle.write(server_cert)
  71. cert_file_handle.close()
  72. if key_file is None:
  73. key_file = os.path.join(ota_image_dir, 'server_key.pem')
  74. key_file_handle = open('server_key.pem', 'w+')
  75. key_file_handle.write(server_key)
  76. key_file_handle.close()
  77. httpd = http.server.HTTPServer((server_ip, server_port), http.server.SimpleHTTPRequestHandler)
  78. httpd.socket = ssl.wrap_socket(httpd.socket,
  79. keyfile=key_file,
  80. certfile=server_file, server_side=True)
  81. httpd.serve_forever()
  82. @ttfw_idf.idf_example_test(env_tag='Example_WIFI')
  83. def test_examples_protocol_simple_ota_example(env, extra_data):
  84. """
  85. steps: |
  86. 1. join AP
  87. 2. Fetch OTA image over HTTPS
  88. 3. Reboot with the new OTA image
  89. """
  90. dut1 = env.get_dut('simple_ota_example', 'examples/system/ota/simple_ota_example', dut_class=ttfw_idf.ESP32DUT)
  91. # check and log bin size
  92. binary_file = os.path.join(dut1.app.binary_path, 'simple_ota.bin')
  93. bin_size = os.path.getsize(binary_file)
  94. ttfw_idf.log_performance('simple_ota_bin_size', '{}KB'.format(bin_size // 1024))
  95. # start test
  96. host_ip = get_my_ip()
  97. thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, 8000))
  98. thread1.daemon = True
  99. thread1.start()
  100. dut1.start_app()
  101. dut1.expect('Loaded app from partition at offset 0x10000', timeout=30)
  102. try:
  103. ip_address = dut1.expect(re.compile(r' sta ip: ([^,]+),'), timeout=30)
  104. print('Connected to AP with IP: {}'.format(ip_address))
  105. except DUT.ExpectTimeout:
  106. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  107. dut1.expect('Starting OTA example', timeout=30)
  108. print('writing to device: {}'.format('https://' + host_ip + ':8000/simple_ota.bin'))
  109. dut1.write('https://' + host_ip + ':8000/simple_ota.bin')
  110. dut1.expect('Loaded app from partition at offset 0x110000', timeout=60)
  111. dut1.expect('Starting OTA example', timeout=30)
  112. @ttfw_idf.idf_example_test(env_tag='Example_EthKitV1')
  113. def test_examples_protocol_simple_ota_example_ethernet_with_spiram_config(env, extra_data):
  114. """
  115. steps: |
  116. 1. join AP
  117. 2. Fetch OTA image over HTTPS
  118. 3. Reboot with the new OTA image
  119. """
  120. dut1 = env.get_dut('simple_ota_example', 'examples/system/ota/simple_ota_example', dut_class=ttfw_idf.ESP32DUT, app_config_name='spiram')
  121. # check and log bin size
  122. binary_file = os.path.join(dut1.app.binary_path, 'simple_ota.bin')
  123. bin_size = os.path.getsize(binary_file)
  124. ttfw_idf.log_performance('simple_ota_bin_size', '{}KB'.format(bin_size // 1024))
  125. # start test
  126. host_ip = get_my_ip()
  127. thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, 8000))
  128. thread1.daemon = True
  129. thread1.start()
  130. dut1.start_app()
  131. dut1.expect('Loaded app from partition at offset 0x10000', timeout=30)
  132. try:
  133. ip_address = dut1.expect(re.compile(r' eth ip: ([^,]+),'), timeout=30)
  134. print('Connected to AP with IP: {}'.format(ip_address))
  135. except DUT.ExpectTimeout:
  136. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  137. dut1.expect('Starting OTA example', timeout=30)
  138. print('writing to device: {}'.format('https://' + host_ip + ':8000/simple_ota.bin'))
  139. dut1.write('https://' + host_ip + ':8000/simple_ota.bin')
  140. dut1.expect('Loaded app from partition at offset 0x110000', timeout=60)
  141. dut1.expect('Starting OTA example', timeout=30)
  142. @ttfw_idf.idf_example_test(env_tag='Example_Flash_Encryption_OTA')
  143. def test_examples_protocol_simple_ota_example_with_flash_encryption(env, extra_data):
  144. """
  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('simple_ota_example', 'examples/system/ota/simple_ota_example', dut_class=ttfw_idf.ESP32DUT, app_config_name='flash_enc')
  151. # check and log bin size
  152. binary_file = os.path.join(dut1.app.binary_path, 'simple_ota.bin')
  153. bin_size = os.path.getsize(binary_file)
  154. ttfw_idf.log_performance('simple_ota_bin_size', '{}KB'.format(bin_size // 1024))
  155. # erase flash on the device
  156. print('Erasing the flash in order to have an empty NVS key partiton')
  157. dut1.erase_flash()
  158. # start test
  159. host_ip = get_my_ip()
  160. thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, 8000))
  161. thread1.daemon = True
  162. thread1.start()
  163. dut1.start_app()
  164. dut1.expect('Loaded app from partition at offset 0x20000', timeout=30)
  165. dut1.expect('Flash encryption mode is DEVELOPMENT (not secure)', timeout=10)
  166. try:
  167. ip_address = dut1.expect(re.compile(r' eth ip: ([^,]+),'), timeout=30)
  168. print('Connected to AP with IP: {}'.format(ip_address))
  169. except DUT.ExpectTimeout:
  170. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  171. dut1.expect('Starting OTA example', timeout=30)
  172. print('writing to device: {}'.format('https://' + host_ip + ':8000/simple_ota.bin'))
  173. dut1.write('https://' + host_ip + ':8000/simple_ota.bin')
  174. dut1.expect('Loaded app from partition at offset 0x120000', timeout=60)
  175. dut1.expect('Flash encryption mode is DEVELOPMENT (not secure)', timeout=10)
  176. dut1.expect('Starting OTA example', timeout=30)
  177. @ttfw_idf.idf_example_test(env_tag='Example_Flash_Encryption_OTA_WiFi', target=['esp32c3'])
  178. def test_examples_protocol_simple_ota_example_with_flash_encryption_wifi(env, extra_data):
  179. """
  180. steps: |
  181. 1. join AP
  182. 2. Fetch OTA image over HTTPS
  183. 3. Reboot with the new OTA image
  184. """
  185. dut1 = env.get_dut('simple_ota_example', 'examples/system/ota/simple_ota_example', app_config_name='flash_enc_wifi')
  186. # check and log bin size
  187. binary_file = os.path.join(dut1.app.binary_path, 'simple_ota.bin')
  188. bin_size = os.path.getsize(binary_file)
  189. ttfw_idf.log_performance('simple_ota_bin_size', '{}KB'.format(bin_size // 1024))
  190. # erase flash on the device
  191. print('Erasing the flash in order to have an empty NVS key partiton')
  192. dut1.erase_flash()
  193. # start test
  194. host_ip = get_my_ip()
  195. thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, 8000))
  196. thread1.daemon = True
  197. thread1.start()
  198. dut1.start_app()
  199. dut1.expect('Loaded app from partition at offset 0x20000', timeout=30)
  200. dut1.expect('Flash encryption mode is DEVELOPMENT (not secure)', timeout=10)
  201. try:
  202. ip_address = dut1.expect(re.compile(r' sta ip: ([^,]+),'), timeout=30)
  203. print('Connected to AP with IP: {}'.format(ip_address))
  204. except DUT.ExpectTimeout:
  205. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  206. dut1.expect('Starting OTA example', timeout=30)
  207. print('writing to device: {}'.format('https://' + host_ip + ':8000/simple_ota.bin'))
  208. dut1.write('https://' + host_ip + ':8000/simple_ota.bin')
  209. dut1.expect('Loaded app from partition at offset 0x120000', timeout=60)
  210. dut1.expect('Flash encryption mode is DEVELOPMENT (not secure)', timeout=10)
  211. dut1.expect('Starting OTA example', timeout=30)
  212. if __name__ == '__main__':
  213. if sys.argv[2:]: # if two or more arguments provided:
  214. # Usage: example_test.py <image_dir> <server_port> [cert_di>]
  215. this_dir = os.path.dirname(os.path.realpath(__file__))
  216. bin_dir = os.path.join(this_dir, sys.argv[1])
  217. port = int(sys.argv[2])
  218. cert_dir = bin_dir if not sys.argv[3:] else os.path.join(this_dir, sys.argv[3]) # optional argument
  219. print('Starting HTTPS server at "https://:{}"'.format(port))
  220. start_https_server(bin_dir, '', port,
  221. server_file=os.path.join(cert_dir, 'ca_cert.pem'),
  222. key_file=os.path.join(cert_dir, 'ca_key.pem'))
  223. else:
  224. test_examples_protocol_simple_ota_example()
  225. test_examples_protocol_simple_ota_example_ethernet_with_spiram_config()
  226. test_examples_protocol_simple_ota_example_with_flash_encryption()
  227. test_examples_protocol_simple_ota_example_with_flash_encryption_wifi()