example_test.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import http.server
  2. import os
  3. import re
  4. import socket
  5. import ssl
  6. from threading import Thread
  7. import ttfw_idf
  8. from tiny_test_fw import DUT
  9. server_cert = '-----BEGIN CERTIFICATE-----\n' \
  10. 'MIIDXTCCAkWgAwIBAgIJAP4LF7E72HakMA0GCSqGSIb3DQEBCwUAMEUxCzAJBgNV\n'\
  11. 'BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX\n'\
  12. 'aWRnaXRzIFB0eSBMdGQwHhcNMTkwNjA3MDk1OTE2WhcNMjAwNjA2MDk1OTE2WjBF\n'\
  13. 'MQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50\n'\
  14. 'ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB\n'\
  15. 'CgKCAQEAlzfCyv3mIv7TlLkObxunKfCdrJ/zgdANrsx0RBtpEPhV560hWJ0fEin0\n'\
  16. 'nIOMpJSiF9E6QsPdr6Q+eogH4XnOMU9JE+iG743N1dPfGEzJvRlyct/Ck8SswKPC\n'\
  17. '9+VXsnOdZmUw9y/xtANbURA/TspvPzz3Avv382ffffrJGh7ooOmaZSCZFlSYHLZA\n'\
  18. 'w/XlRr0sSRbLpFGY0gXjaAV8iHHiPDYLy4kZOepjV9U51xi+IGsL4w75zuMgsHyF\n'\
  19. '3nJeGYHgtGVBrkL0ZKG5udY0wcBjysjubDJC4iSlNiq2HD3fhs7j6CZddV2v845M\n'\
  20. 'lVKNxP0kO4Uj4D8r+5USWC8JKfAwxQIDAQABo1AwTjAdBgNVHQ4EFgQU6OE7ssfY\n'\
  21. 'IIPTDThiUoofUpsD5NwwHwYDVR0jBBgwFoAU6OE7ssfYIIPTDThiUoofUpsD5Nww\n'\
  22. 'DAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAXIlHS/FJWfmcinUAxyBd\n'\
  23. '/xd5Lu8ykeru6oaUCci+Vk9lyoMMES7lQ+b/00d5x7AcTawkTil9EWpBTPTOTraA\n'\
  24. 'lzJMQhNKmSLk0iIoTtAJtSZgUSpIIozqK6lenxQQDsHbXKU6h+u9H6KZE8YcjsFl\n'\
  25. '6vL7sw9BVotw/VxfgjQ5OSGLgoLrdVT0z5C2qOuwOgz1c7jNiJhtMdwN+cOtnJp2\n'\
  26. 'fuBgEYyE3eeuWogvkWoDcIA8r17Ixzkpq2oJsdvZcHZPIZShPKW2SHUsl98KDemu\n'\
  27. 'y0pQyExmQUbwKE4vbFb9XuWCcL9XaOHQytyszt2DeD67AipvoBwVU7/LBOvqnsmy\n'\
  28. 'hA==\n'\
  29. '-----END CERTIFICATE-----\n'
  30. server_key = '-----BEGIN PRIVATE KEY-----\n'\
  31. 'MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCXN8LK/eYi/tOU\n'\
  32. 'uQ5vG6cp8J2sn/OB0A2uzHREG2kQ+FXnrSFYnR8SKfScg4yklKIX0TpCw92vpD56\n'\
  33. 'iAfhec4xT0kT6Ibvjc3V098YTMm9GXJy38KTxKzAo8L35Veyc51mZTD3L/G0A1tR\n'\
  34. 'ED9Oym8/PPcC+/fzZ999+skaHuig6ZplIJkWVJgctkDD9eVGvSxJFsukUZjSBeNo\n'\
  35. 'BXyIceI8NgvLiRk56mNX1TnXGL4gawvjDvnO4yCwfIXecl4ZgeC0ZUGuQvRkobm5\n'\
  36. '1jTBwGPKyO5sMkLiJKU2KrYcPd+GzuPoJl11Xa/zjkyVUo3E/SQ7hSPgPyv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vjLymBH5TiBKDXKqEpgsW0huoi8Gq9Uu\n'\
  52. 'nvvXXAgkIyRYF36f0vUe0nkjLuYAQAWgC2pZYgNLJR13iVbol0xHJoXQUHtgiaJ8\n'\
  53. 'GLYFzjHQPqFMpSalQe3oELko39uOC1CoJCHFySECgYBeycUnRBikCO2n8DNhY4Eg\n'\
  54. '9Y3oxcssRt6ea5BZwgW2eAYi7/XqKkmxoSoOykUt3MJx9+EkkrL17bxFSpkj1tvL\n'\
  55. 'qvxn7egtsKjjgGNAxwXC4MwCvhveyUQQxtQb8AqGrGqo4jEEN0L15cnP38i2x1Uo\n'\
  56. 'muhfskWf4MABV0yTUaKcGg==\n'\
  57. '-----END PRIVATE KEY-----\n'
  58. def get_my_ip():
  59. s1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  60. s1.connect(('8.8.8.8', 80))
  61. my_ip = s1.getsockname()[0]
  62. s1.close()
  63. return my_ip
  64. def start_https_server(ota_image_dir, server_ip, server_port):
  65. # parser = argparse.ArgumentParser()
  66. # parser.add_argument('-p', '--port', dest='port', type= int,
  67. # help= "Server Port", default= 8000)
  68. # args = parser.parse_args()
  69. os.chdir(ota_image_dir)
  70. server_file = os.path.join(ota_image_dir, 'server_cert.pem')
  71. cert_file_handle = open(server_file, 'w+')
  72. cert_file_handle.write(server_cert)
  73. cert_file_handle.close()
  74. key_file = os.path.join(ota_image_dir, 'server_key.pem')
  75. key_file_handle = open('server_key.pem', 'w+')
  76. key_file_handle.write(server_key)
  77. key_file_handle.close()
  78. httpd = http.server.HTTPServer((server_ip, server_port), http.server.SimpleHTTPRequestHandler)
  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_WIFI')
  84. def test_examples_protocol_simple_ota_example(env, extra_data):
  85. """
  86. steps: |
  87. 1. join AP
  88. 2. Fetch OTA image over HTTPS
  89. 3. Reboot with the new OTA image
  90. """
  91. dut1 = env.get_dut('simple_ota_example', 'examples/system/ota/simple_ota_example', dut_class=ttfw_idf.ESP32DUT)
  92. # check and log bin size
  93. binary_file = os.path.join(dut1.app.binary_path, 'simple_ota.bin')
  94. bin_size = os.path.getsize(binary_file)
  95. ttfw_idf.log_performance('simple_ota_bin_size', '{}KB'.format(bin_size // 1024))
  96. # start test
  97. host_ip = get_my_ip()
  98. thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, 8000))
  99. thread1.daemon = True
  100. thread1.start()
  101. dut1.start_app()
  102. dut1.expect('Loaded app from partition at offset 0x10000', timeout=30)
  103. try:
  104. ip_address = dut1.expect(re.compile(r' sta ip: ([^,]+),'), timeout=30)
  105. print('Connected to AP with IP: {}'.format(ip_address))
  106. except DUT.ExpectTimeout:
  107. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  108. thread1.close()
  109. dut1.expect('Starting OTA example', timeout=30)
  110. print('writing to device: {}'.format('https://' + host_ip + ':8000/simple_ota.bin'))
  111. dut1.write('https://' + host_ip + ':8000/simple_ota.bin')
  112. dut1.expect('Loaded app from partition at offset 0x110000', timeout=60)
  113. dut1.expect('Starting OTA example', timeout=30)
  114. @ttfw_idf.idf_example_test(env_tag='Example_EthKitV1')
  115. def test_examples_protocol_simple_ota_example_ethernet_with_spiram_config(env, extra_data):
  116. """
  117. steps: |
  118. 1. join AP
  119. 2. Fetch OTA image over HTTPS
  120. 3. Reboot with the new OTA image
  121. """
  122. dut1 = env.get_dut('simple_ota_example', 'examples/system/ota/simple_ota_example', dut_class=ttfw_idf.ESP32DUT, app_config_name='spiram')
  123. # check and log bin size
  124. binary_file = os.path.join(dut1.app.binary_path, 'simple_ota.bin')
  125. bin_size = os.path.getsize(binary_file)
  126. ttfw_idf.log_performance('simple_ota_bin_size', '{}KB'.format(bin_size // 1024))
  127. # start test
  128. host_ip = get_my_ip()
  129. thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, 8000))
  130. thread1.daemon = True
  131. thread1.start()
  132. dut1.start_app()
  133. dut1.expect('Loaded app from partition at offset 0x10000', timeout=30)
  134. try:
  135. ip_address = dut1.expect(re.compile(r' eth ip: ([^,]+),'), timeout=30)
  136. print('Connected to AP with IP: {}'.format(ip_address))
  137. except DUT.ExpectTimeout:
  138. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  139. thread1.close()
  140. dut1.expect('Starting OTA example', timeout=30)
  141. print('writing to device: {}'.format('https://' + host_ip + ':8000/simple_ota.bin'))
  142. dut1.write('https://' + host_ip + ':8000/simple_ota.bin')
  143. dut1.expect('Loaded app from partition at offset 0x110000', timeout=60)
  144. dut1.expect('Starting OTA example', timeout=30)
  145. @ttfw_idf.idf_example_test(env_tag='Example_Flash_Encryption_OTA')
  146. def test_examples_protocol_simple_ota_example_with_flash_encryption(env, extra_data):
  147. """
  148. steps: |
  149. 1. join AP
  150. 2. Fetch OTA image over HTTPS
  151. 3. Reboot with the new OTA image
  152. """
  153. dut1 = env.get_dut('simple_ota_example', 'examples/system/ota/simple_ota_example', dut_class=ttfw_idf.ESP32DUT, app_config_name='flash_enc')
  154. # check and log bin size
  155. binary_file = os.path.join(dut1.app.binary_path, 'simple_ota.bin')
  156. bin_size = os.path.getsize(binary_file)
  157. ttfw_idf.log_performance('simple_ota_bin_size', '{}KB'.format(bin_size // 1024))
  158. # erase flash on the device
  159. print('Erasing the flash in order to have an empty NVS key partiton')
  160. dut1.erase_flash()
  161. # start test
  162. host_ip = get_my_ip()
  163. thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, 8000))
  164. thread1.daemon = True
  165. thread1.start()
  166. dut1.start_app()
  167. dut1.expect('Loaded app from partition at offset 0x20000', timeout=30)
  168. dut1.expect('Flash encryption mode is DEVELOPMENT (not secure)', timeout=10)
  169. try:
  170. ip_address = dut1.expect(re.compile(r' eth 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 OTA example', timeout=30)
  176. print('writing to device: {}'.format('https://' + host_ip + ':8000/simple_ota.bin'))
  177. dut1.write('https://' + host_ip + ':8000/simple_ota.bin')
  178. dut1.expect('Loaded app from partition at offset 0x120000', timeout=60)
  179. dut1.expect('Flash encryption mode is DEVELOPMENT (not secure)', timeout=10)
  180. dut1.expect('Starting OTA example', timeout=30)
  181. if __name__ == '__main__':
  182. test_examples_protocol_simple_ota_example()
  183. test_examples_protocol_simple_ota_example_ethernet_with_spiram_config()
  184. test_examples_protocol_simple_ota_example_with_flash_encryption()