Explorar o código

Merge branch 'bugfix/address_already_used_exception_in_ota_tests_v4.4' into 'release/v4.4'

Fix 'address already in use' exception in OTA tests (v4.4)

See merge request espressif/esp-idf!16416
Mahavir Jain %!s(int64=4) %!d(string=hai) anos
pai
achega
036e3f2e46

+ 36 - 86
examples/system/ota/advanced_https_ota/example_test.py

@@ -1,4 +1,5 @@
 import http.server
+import multiprocessing
 import os
 import random
 import re
@@ -6,61 +7,13 @@ import socket
 import ssl
 import struct
 import subprocess
-from threading import Thread
 
 import ttfw_idf
 from RangeHTTPServer import RangeRequestHandler
 from tiny_test_fw import DUT, Utility
 
-server_cert = '-----BEGIN CERTIFICATE-----\n' \
-              'MIIDWDCCAkACCQCbF4+gVh/MLjANBgkqhkiG9w0BAQsFADBuMQswCQYDVQQGEwJJ\n'\
-              'TjELMAkGA1UECAwCTUgxDDAKBgNVBAcMA1BVTjEMMAoGA1UECgwDRVNQMQwwCgYD\n'\
-              'VQQLDANFU1AxDDAKBgNVBAMMA0VTUDEaMBgGCSqGSIb3DQEJARYLZXNwQGVzcC5j\n'\
-              'b20wHhcNMjEwNzEyMTIzNjI3WhcNNDEwNzA3MTIzNjI3WjBuMQswCQYDVQQGEwJJ\n'\
-              'TjELMAkGA1UECAwCTUgxDDAKBgNVBAcMA1BVTjEMMAoGA1UECgwDRVNQMQwwCgYD\n'\
-              'VQQLDANFU1AxDDAKBgNVBAMMA0VTUDEaMBgGCSqGSIb3DQEJARYLZXNwQGVzcC5j\n'\
-              'b20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDhxF/y7bygndxPwiWL\n'\
-              'SwS9LY3uBMaJgup0ufNKVhx+FhGQOu44SghuJAaH3KkPUnt6SOM8jC97/yQuc32W\n'\
-              'ukI7eBZoA12kargSnzdv5m5rZZpd+NznSSpoDArOAONKVlzr25A1+aZbix2mKRbQ\n'\
-              'S5w9o1N2BriQuSzd8gL0Y0zEk3VkOWXEL+0yFUT144HnErnD+xnJtHe11yPO2fEz\n'\
-              'YaGiilh0ddL26PXTugXMZN/8fRVHP50P2OG0SvFpC7vghlLp4VFM1/r3UJnvL6Oz\n'\
-              '3ALc6dhxZEKQucqlpj8l1UegszQToopemtIj0qXTHw2+uUnkUyWIPjPC+wdOAoap\n'\
-              'rFTRAgMBAAEwDQYJKoZIhvcNAQELBQADggEBAItw24y565k3C/zENZlxyzto44ud\n'\
-              'IYPQXN8Fa2pBlLe1zlSIyuaA/rWQ+i1daS8nPotkCbWZyf5N8DYaTE4B0OfvoUPk\n'\
-              'B5uGDmbuk6akvlB5BGiYLfQjWHRsK9/4xjtIqN1H58yf3QNROuKsPAeywWS3Fn32\n'\
-              '3//OpbWaClQePx6udRYMqAitKR+QxL7/BKZQsX+UyShuq8hjphvXvk0BW8ONzuw9\n'\
-              'RcoORxM0FzySYjeQvm4LhzC/P3ZBhEq0xs55aL2a76SJhq5hJy7T/Xz6NFByvlrN\n'\
-              'lFJJey33KFrAf5vnV9qcyWFIo7PYy2VsaaEjFeefr7q3sTFSMlJeadexW2Y=\n'\
-              '-----END CERTIFICATE-----\n'
-
-server_key = '-----BEGIN PRIVATE KEY-----\n'\
-             'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDhxF/y7bygndxP\n'\
-             'wiWLSwS9LY3uBMaJgup0ufNKVhx+FhGQOu44SghuJAaH3KkPUnt6SOM8jC97/yQu\n'\
-             'c32WukI7eBZoA12kargSnzdv5m5rZZpd+NznSSpoDArOAONKVlzr25A1+aZbix2m\n'\
-             'KRbQS5w9o1N2BriQuSzd8gL0Y0zEk3VkOWXEL+0yFUT144HnErnD+xnJtHe11yPO\n'\
-             '2fEzYaGiilh0ddL26PXTugXMZN/8fRVHP50P2OG0SvFpC7vghlLp4VFM1/r3UJnv\n'\
-             'L6Oz3ALc6dhxZEKQucqlpj8l1UegszQToopemtIj0qXTHw2+uUnkUyWIPjPC+wdO\n'\
-             'AoaprFTRAgMBAAECggEAE0HCxV/N1Q1h+1OeDDGL5+74yjKSFKyb/vTVcaPCrmaH\n'\
-             'fPvp0ddOvMZJ4FDMAsiQS6/n4gQ7EKKEnYmwTqj4eUYW8yxGUn3f0YbPHbZT+Mkj\n'\
-             'z5woi3nMKi/MxCGDQZX4Ow3xUQlITUqibsfWcFHis8c4mTqdh4qj7xJzehD2PVYF\n'\
-             'gNHZsvVj6MltjBDAVwV1IlGoHjuElm6vuzkfX7phxcA1B4ZqdYY17yCXUnvui46z\n'\
-             'Xn2kUTOOUCEgfgvGa9E+l4OtdXi5IxjaSraU+dlg2KsE4TpCuN2MEVkeR5Ms3Y7Q\n'\
-             'jgJl8vlNFJDQpbFukLcYwG7rO5N5dQ6WWfVia/5XgQKBgQD74at/bXAPrh9NxPmz\n'\
-             'i1oqCHMDoM9sz8xIMZLF9YVu3Jf8ux4xVpRSnNy5RU1gl7ZXbpdgeIQ4v04zy5aw\n'\
-             '8T4tu9K3XnR3UXOy25AK0q+cnnxZg3kFQm+PhtOCKEFjPHrgo2MUfnj+EDddod7N\n'\
-             'JQr9q5rEFbqHupFPpWlqCa3QmQKBgQDldWUGokNaEpmgHDMnHxiibXV5LQhzf8Rq\n'\
-             'gJIQXb7R9EsTSXEvsDyqTBb7PHp2Ko7rZ5YQfyf8OogGGjGElnPoU/a+Jij1gVFv\n'\
-             'kZ064uXAAISBkwHdcuobqc5EbG3ceyH46F+FBFhqM8KcbxJxx08objmh58+83InN\n'\
-             'P9Qr25Xw+QKBgEGXMHuMWgQbSZeM1aFFhoMvlBO7yogBTKb4Ecpu9wI5e3Kan3Al\n'\
-             'pZYltuyf+VhP6XG3IMBEYdoNJyYhu+nzyEdMg8CwXg+8LC7FMis/Ve+o7aS5scgG\n'\
-             '1to/N9DK/swCsdTRdzmc/ZDbVC+TuVsebFBGYZTyO5KgqLpezqaIQrTxAoGALFCU\n'\
-             '10glO9MVyl9H3clap5v+MQ3qcOv/EhaMnw6L2N6WVT481tnxjW4ujgzrFcE4YuxZ\n'\
-             'hgwYu9TOCmeqopGwBvGYWLbj+C4mfSahOAs0FfXDoYazuIIGBpuv03UhbpB1Si4O\n'\
-             'rJDfRnuCnVWyOTkl54gKJ2OusinhjztBjcrV1XkCgYEA3qNi4uBsPdyz9BZGb/3G\n'\
-             'rOMSw0CaT4pEMTLZqURmDP/0hxvTk1polP7O/FYwxVuJnBb6mzDa0xpLFPTpIAnJ\n'\
-             'YXB8xpXU69QVh+EBbemdJWOd+zp5UCfXvb2shAeG3Tn/Dz4cBBMEUutbzP+or0nG\n'\
-             'vSXnRLaxQhooWm+IuX9SuBQ=\n'\
-             '-----END PRIVATE KEY-----\n'
+server_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'test_certs/server_cert.pem')
+key_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'test_certs/server_key.pem')
 
 
 def get_my_ip():
@@ -80,21 +33,6 @@ def get_server_status(host_ip, port):
     return False
 
 
-def create_file(server_file, file_data):
-    with open(server_file, 'w+') as file:
-        file.write(file_data)
-
-
-def get_ca_cert(ota_image_dir):
-    os.chdir(ota_image_dir)
-    server_file = os.path.join(ota_image_dir, 'server_cert.pem')
-    create_file(server_file, server_cert)
-
-    key_file = os.path.join(ota_image_dir, 'server_key.pem')
-    create_file(key_file, server_key)
-    return server_file, key_file
-
-
 def https_request_handler():
     """
     Returns a request handler class that handles broken pipe exception
@@ -119,7 +57,7 @@ def https_request_handler():
 
 
 def start_https_server(ota_image_dir, server_ip, server_port):
-    server_file, key_file = get_ca_cert(ota_image_dir)
+    os.chdir(ota_image_dir)
     requestHandler = https_request_handler()
     httpd = http.server.HTTPServer((server_ip, server_port), requestHandler)
 
@@ -130,7 +68,7 @@ def start_https_server(ota_image_dir, server_ip, server_port):
 
 
 def start_chunked_server(ota_image_dir, server_port):
-    server_file, key_file = get_ca_cert(ota_image_dir)
+    os.chdir(ota_image_dir)
     chunked_server = subprocess.Popen(['openssl', 's_server', '-WWW', '-key', key_file, '-cert', server_file, '-port', str(server_port)])
     return chunked_server
 
@@ -157,7 +95,6 @@ def redirect_handler_factory(url):
 
 def start_redirect_server(ota_image_dir, server_ip, server_port, redirection_port):
     os.chdir(ota_image_dir)
-    server_file, key_file = get_ca_cert(ota_image_dir)
     redirectHandler = redirect_handler_factory('https://' + server_ip + ':' + str(redirection_port) + '/advanced_https_ota.bin')
 
     httpd = http.server.HTTPServer((server_ip, server_port), redirectHandler)
@@ -191,7 +128,7 @@ def test_examples_protocol_advanced_https_ota_example(env, extra_data):
     # start test
     host_ip = get_my_ip()
     if (get_server_status(host_ip, server_port) is False):
-        thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
+        thread1 = multiprocessing.Process(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
         thread1.daemon = True
         thread1.start()
     dut1.start_app()
@@ -202,7 +139,7 @@ def test_examples_protocol_advanced_https_ota_example(env, extra_data):
             print('Connected to AP with IP: {}'.format(ip_address))
         except DUT.ExpectTimeout:
             raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
-            thread1.close()
+            thread1.terminate()
         dut1.expect('Starting Advanced OTA example', timeout=30)
 
         print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + bin_name))
@@ -210,6 +147,7 @@ def test_examples_protocol_advanced_https_ota_example(env, extra_data):
         dut1.expect('Loaded app from partition at offset', timeout=60)
         dut1.expect('Starting Advanced OTA example', timeout=30)
         dut1.reset()
+    thread1.terminate()
 
 
 @ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
@@ -245,7 +183,7 @@ def test_examples_protocol_advanced_https_ota_example_truncated_bin(env, extra_d
     # start test
     host_ip = get_my_ip()
     if (get_server_status(host_ip, server_port) is False):
-        thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
+        thread1 = multiprocessing.Process(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
         thread1.daemon = True
         thread1.start()
     dut1.start_app()
@@ -255,12 +193,14 @@ def test_examples_protocol_advanced_https_ota_example_truncated_bin(env, extra_d
         print('Connected to AP with IP: {}'.format(ip_address))
     except DUT.ExpectTimeout:
         raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
+        thread1.terminate()
     dut1.expect('Starting Advanced OTA example', timeout=30)
 
     print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + truncated_bin_name))
     dut1.write('https://' + host_ip + ':' + str(server_port) + '/' + truncated_bin_name)
     dut1.expect('Image validation failed, image is corrupted', timeout=30)
     os.remove(binary_file)
+    thread1.terminate()
 
 
 @ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
@@ -295,7 +235,7 @@ def test_examples_protocol_advanced_https_ota_example_truncated_header(env, extr
     # start test
     host_ip = get_my_ip()
     if (get_server_status(host_ip, server_port) is False):
-        thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
+        thread1 = multiprocessing.Process(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
         thread1.daemon = True
         thread1.start()
     dut1.start_app()
@@ -305,12 +245,14 @@ def test_examples_protocol_advanced_https_ota_example_truncated_header(env, extr
         print('Connected to AP with IP: {}'.format(ip_address))
     except DUT.ExpectTimeout:
         raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
+        thread1.terminate()
     dut1.expect('Starting Advanced OTA example', timeout=30)
 
     print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + truncated_bin_name))
     dut1.write('https://' + host_ip + ':' + str(server_port) + '/' + truncated_bin_name)
     dut1.expect('advanced_https_ota_example: esp_https_ota_read_img_desc failed', timeout=30)
     os.remove(binary_file)
+    thread1.terminate()
 
 
 @ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
@@ -344,7 +286,7 @@ def test_examples_protocol_advanced_https_ota_example_random(env, extra_data):
     # start test
     host_ip = get_my_ip()
     if (get_server_status(host_ip, server_port) is False):
-        thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
+        thread1 = multiprocessing.Process(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
         thread1.daemon = True
         thread1.start()
     dut1.start_app()
@@ -354,12 +296,14 @@ def test_examples_protocol_advanced_https_ota_example_random(env, extra_data):
         print('Connected to AP with IP: {}'.format(ip_address))
     except DUT.ExpectTimeout:
         raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
+        thread1.terminate()
     dut1.expect('Starting Advanced OTA example', timeout=30)
 
     print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + random_bin_name))
     dut1.write('https://' + host_ip + ':' + str(server_port) + '/' + random_bin_name)
     dut1.expect('esp_ota_ops: OTA image has invalid magic byte', timeout=10)
     os.remove(binary_file)
+    thread1.terminate()
 
 
 @ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
@@ -396,8 +340,6 @@ def test_examples_protocol_advanced_https_ota_example_chunked(env, extra_data):
     dut1.expect('Loaded app from partition at offset', timeout=60)
     dut1.expect('Starting Advanced OTA example', timeout=30)
     chunked_server.kill()
-    os.remove(os.path.join(dut1.app.binary_path, 'server_cert.pem'))
-    os.remove(os.path.join(dut1.app.binary_path, 'server_key.pem'))
 
 
 @ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
@@ -424,10 +366,10 @@ def test_examples_protocol_advanced_https_ota_example_redirect_url(env, extra_da
     # start test
     host_ip = get_my_ip()
     if (get_server_status(host_ip, server_port) is False):
-        thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
+        thread1 = multiprocessing.Process(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
         thread1.daemon = True
         thread1.start()
-    thread2 = Thread(target=start_redirect_server, args=(dut1.app.binary_path, host_ip, redirection_server_port, server_port))
+    thread2 = multiprocessing.Process(target=start_redirect_server, args=(dut1.app.binary_path, host_ip, redirection_server_port, server_port))
     thread2.daemon = True
     thread2.start()
     dut1.start_app()
@@ -437,8 +379,8 @@ def test_examples_protocol_advanced_https_ota_example_redirect_url(env, extra_da
         print('Connected to AP with IP: {}'.format(ip_address))
     except DUT.ExpectTimeout:
         raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
-        thread1.close()
-        thread2.close()
+        thread1.terminate()
+        thread2.terminate()
     dut1.expect('Starting Advanced OTA example', timeout=30)
 
     print('writing to device: {}'.format('https://' + host_ip + ':' + str(redirection_server_port) + '/' + bin_name))
@@ -446,6 +388,8 @@ def test_examples_protocol_advanced_https_ota_example_redirect_url(env, extra_da
     dut1.expect('Loaded app from partition at offset', timeout=60)
     dut1.expect('Starting Advanced OTA example', timeout=30)
     dut1.reset()
+    thread1.terminate()
+    thread2.terminate()
 
 
 @ttfw_idf.idf_example_test(env_tag='Example_8Mflash_Ethernet')
@@ -485,7 +429,7 @@ def test_examples_protocol_advanced_https_ota_example_anti_rollback(env, extra_d
     # start test
     host_ip = get_my_ip()
     if (get_server_status(host_ip, server_port) is False):
-        thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
+        thread1 = multiprocessing.Process(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
         thread1.daemon = True
         thread1.start()
     dut1.start_app()
@@ -496,6 +440,7 @@ def test_examples_protocol_advanced_https_ota_example_anti_rollback(env, extra_d
         print('Connected to AP with IP: {}'.format(ip_address))
     except DUT.ExpectTimeout:
         raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
+        thread1.terminate()
     dut1.expect('Starting Advanced OTA example', timeout=30)
 
     # Use originally generated image with secure_version=1
@@ -511,7 +456,8 @@ def test_examples_protocol_advanced_https_ota_example_anti_rollback(env, extra_d
     print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + anti_rollback_bin_name))
     dut1.write('https://' + host_ip + ':' + str(server_port) + '/' + anti_rollback_bin_name)
     dut1.expect('New firmware security version is less than eFuse programmed, 0 < 1', timeout=30)
-    os.remove(anti_rollback_bin_name)
+    os.remove(binary_file)
+    thread1.terminate()
 
 
 @ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
@@ -537,7 +483,7 @@ def test_examples_protocol_advanced_https_ota_example_partial_request(env, extra
     # start test
     host_ip = get_my_ip()
     if (get_server_status(host_ip, server_port) is False):
-        thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
+        thread1 = multiprocessing.Process(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
         thread1.daemon = True
         thread1.start()
     dut1.start_app()
@@ -548,6 +494,7 @@ def test_examples_protocol_advanced_https_ota_example_partial_request(env, extra
     except DUT.ExpectTimeout:
         Utility.console_log('ENV_TEST_FAILURE: Cannot connect to AP')
         raise
+        thread1.terminate()
     dut1.expect('Starting Advanced OTA example', timeout=30)
 
     print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + bin_name))
@@ -557,6 +504,7 @@ def test_examples_protocol_advanced_https_ota_example_partial_request(env, extra
     dut1.expect('Loaded app from partition at offset', timeout=60)
     dut1.expect('Starting Advanced OTA example', timeout=30)
     dut1.reset()
+    thread1.terminate()
 
 
 @ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
@@ -580,7 +528,7 @@ def test_examples_protocol_advanced_https_ota_example_nimble_gatts(env, extra_da
     # start test
     host_ip = get_my_ip()
     if (get_server_status(host_ip, server_port) is False):
-        thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
+        thread1 = multiprocessing.Process(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
         thread1.daemon = True
         thread1.start()
     dut1.start_app()
@@ -590,6 +538,7 @@ def test_examples_protocol_advanced_https_ota_example_nimble_gatts(env, extra_da
         print('Connected to AP with IP: {}'.format(ip_address))
     except DUT.ExpectTimeout:
         raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
+        thread1.terminate()
 
     dut1.expect('Starting Advanced OTA example', timeout=30)
     print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + bin_name))
@@ -600,6 +549,7 @@ def test_examples_protocol_advanced_https_ota_example_nimble_gatts(env, extra_da
     dut1.expect('Loaded app from partition at offset', timeout=60)
     dut1.expect('Starting Advanced OTA example', timeout=30)
     dut1.reset()
+    thread1.terminate()
 
 
 @ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
@@ -623,7 +573,7 @@ def test_examples_protocol_advanced_https_ota_example_bluedroid_gatts(env, extra
     # start test
     host_ip = get_my_ip()
     if (get_server_status(host_ip, server_port) is False):
-        thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
+        thread1 = multiprocessing.Process(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
         thread1.daemon = True
         thread1.start()
     dut1.start_app()
@@ -633,6 +583,7 @@ def test_examples_protocol_advanced_https_ota_example_bluedroid_gatts(env, extra
         print('Connected to AP with IP: {}'.format(ip_address))
     except DUT.ExpectTimeout:
         raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
+        thread1.terminate()
 
     dut1.expect('Starting Advanced OTA example', timeout=30)
     print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + bin_name))
@@ -643,6 +594,7 @@ def test_examples_protocol_advanced_https_ota_example_bluedroid_gatts(env, extra
     dut1.expect('Loaded app from partition at offset', timeout=60)
     dut1.expect('Starting Advanced OTA example', timeout=30)
     dut1.reset()
+    thread1.terminate()
 
 
 @ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
@@ -689,8 +641,6 @@ def test_examples_protocol_advanced_https_ota_example_openssl_aligned_bin(env, e
     dut1.expect('Loaded app from partition at offset', timeout=60)
     dut1.expect('Starting Advanced OTA example', timeout=30)
     chunked_server.kill()
-    os.remove(os.path.join(dut1.app.binary_path, 'server_cert.pem'))
-    os.remove(os.path.join(dut1.app.binary_path, 'server_key.pem'))
     os.remove(aligned_bin_name)
 
 

+ 20 - 0
examples/system/ota/advanced_https_ota/test_certs/server_cert.pem

@@ -0,0 +1,20 @@
+-----BEGIN CERTIFICATE-----
+MIIDWDCCAkACCQCbF4+gVh/MLjANBgkqhkiG9w0BAQsFADBuMQswCQYDVQQGEwJJ
+TjELMAkGA1UECAwCTUgxDDAKBgNVBAcMA1BVTjEMMAoGA1UECgwDRVNQMQwwCgYD
+VQQLDANFU1AxDDAKBgNVBAMMA0VTUDEaMBgGCSqGSIb3DQEJARYLZXNwQGVzcC5j
+b20wHhcNMjEwNzEyMTIzNjI3WhcNNDEwNzA3MTIzNjI3WjBuMQswCQYDVQQGEwJJ
+TjELMAkGA1UECAwCTUgxDDAKBgNVBAcMA1BVTjEMMAoGA1UECgwDRVNQMQwwCgYD
+VQQLDANFU1AxDDAKBgNVBAMMA0VTUDEaMBgGCSqGSIb3DQEJARYLZXNwQGVzcC5j
+b20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDhxF/y7bygndxPwiWL
+SwS9LY3uBMaJgup0ufNKVhx+FhGQOu44SghuJAaH3KkPUnt6SOM8jC97/yQuc32W
+ukI7eBZoA12kargSnzdv5m5rZZpd+NznSSpoDArOAONKVlzr25A1+aZbix2mKRbQ
+S5w9o1N2BriQuSzd8gL0Y0zEk3VkOWXEL+0yFUT144HnErnD+xnJtHe11yPO2fEz
+YaGiilh0ddL26PXTugXMZN/8fRVHP50P2OG0SvFpC7vghlLp4VFM1/r3UJnvL6Oz
+3ALc6dhxZEKQucqlpj8l1UegszQToopemtIj0qXTHw2+uUnkUyWIPjPC+wdOAoap
+rFTRAgMBAAEwDQYJKoZIhvcNAQELBQADggEBAItw24y565k3C/zENZlxyzto44ud
+IYPQXN8Fa2pBlLe1zlSIyuaA/rWQ+i1daS8nPotkCbWZyf5N8DYaTE4B0OfvoUPk
+B5uGDmbuk6akvlB5BGiYLfQjWHRsK9/4xjtIqN1H58yf3QNROuKsPAeywWS3Fn32
+3//OpbWaClQePx6udRYMqAitKR+QxL7/BKZQsX+UyShuq8hjphvXvk0BW8ONzuw9
+RcoORxM0FzySYjeQvm4LhzC/P3ZBhEq0xs55aL2a76SJhq5hJy7T/Xz6NFByvlrN
+lFJJey33KFrAf5vnV9qcyWFIo7PYy2VsaaEjFeefr7q3sTFSMlJeadexW2Y=
+-----END CERTIFICATE-----

+ 28 - 0
examples/system/ota/advanced_https_ota/test_certs/server_key.pem

@@ -0,0 +1,28 @@
+-----BEGIN PRIVATE KEY-----
+MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDhxF/y7bygndxP
+wiWLSwS9LY3uBMaJgup0ufNKVhx+FhGQOu44SghuJAaH3KkPUnt6SOM8jC97/yQu
+c32WukI7eBZoA12kargSnzdv5m5rZZpd+NznSSpoDArOAONKVlzr25A1+aZbix2m
+KRbQS5w9o1N2BriQuSzd8gL0Y0zEk3VkOWXEL+0yFUT144HnErnD+xnJtHe11yPO
+2fEzYaGiilh0ddL26PXTugXMZN/8fRVHP50P2OG0SvFpC7vghlLp4VFM1/r3UJnv
+L6Oz3ALc6dhxZEKQucqlpj8l1UegszQToopemtIj0qXTHw2+uUnkUyWIPjPC+wdO
+AoaprFTRAgMBAAECggEAE0HCxV/N1Q1h+1OeDDGL5+74yjKSFKyb/vTVcaPCrmaH
+fPvp0ddOvMZJ4FDMAsiQS6/n4gQ7EKKEnYmwTqj4eUYW8yxGUn3f0YbPHbZT+Mkj
+z5woi3nMKi/MxCGDQZX4Ow3xUQlITUqibsfWcFHis8c4mTqdh4qj7xJzehD2PVYF
+gNHZsvVj6MltjBDAVwV1IlGoHjuElm6vuzkfX7phxcA1B4ZqdYY17yCXUnvui46z
+Xn2kUTOOUCEgfgvGa9E+l4OtdXi5IxjaSraU+dlg2KsE4TpCuN2MEVkeR5Ms3Y7Q
+jgJl8vlNFJDQpbFukLcYwG7rO5N5dQ6WWfVia/5XgQKBgQD74at/bXAPrh9NxPmz
+i1oqCHMDoM9sz8xIMZLF9YVu3Jf8ux4xVpRSnNy5RU1gl7ZXbpdgeIQ4v04zy5aw
+8T4tu9K3XnR3UXOy25AK0q+cnnxZg3kFQm+PhtOCKEFjPHrgo2MUfnj+EDddod7N
+JQr9q5rEFbqHupFPpWlqCa3QmQKBgQDldWUGokNaEpmgHDMnHxiibXV5LQhzf8Rq
+gJIQXb7R9EsTSXEvsDyqTBb7PHp2Ko7rZ5YQfyf8OogGGjGElnPoU/a+Jij1gVFv
+kZ064uXAAISBkwHdcuobqc5EbG3ceyH46F+FBFhqM8KcbxJxx08objmh58+83InN
+P9Qr25Xw+QKBgEGXMHuMWgQbSZeM1aFFhoMvlBO7yogBTKb4Ecpu9wI5e3Kan3Al
+pZYltuyf+VhP6XG3IMBEYdoNJyYhu+nzyEdMg8CwXg+8LC7FMis/Ve+o7aS5scgG
+1to/N9DK/swCsdTRdzmc/ZDbVC+TuVsebFBGYZTyO5KgqLpezqaIQrTxAoGALFCU
+10glO9MVyl9H3clap5v+MQ3qcOv/EhaMnw6L2N6WVT481tnxjW4ujgzrFcE4YuxZ
+hgwYu9TOCmeqopGwBvGYWLbj+C4mfSahOAs0FfXDoYazuIIGBpuv03UhbpB1Si4O
+rJDfRnuCnVWyOTkl54gKJ2OusinhjztBjcrV1XkCgYEA3qNi4uBsPdyz9BZGb/3G
+rOMSw0CaT4pEMTLZqURmDP/0hxvTk1polP7O/FYwxVuJnBb6mzDa0xpLFPTpIAnJ
+YXB8xpXU69QVh+EBbemdJWOd+zp5UCfXvb2shAeG3Tn/Dz4cBBMEUutbzP+or0nG
+vSXnRLaxQhooWm+IuX9SuBQ=
+-----END PRIVATE KEY-----

+ 13 - 6
examples/system/ota/native_ota_example/example_test.py

@@ -1,4 +1,5 @@
 import http.server
+import multiprocessing
 import os
 import random
 import re
@@ -6,7 +7,6 @@ import socket
 import ssl
 import struct
 import subprocess
-from threading import Thread
 
 import ttfw_idf
 from tiny_test_fw import DUT
@@ -157,7 +157,7 @@ def test_examples_protocol_native_ota_example(env, extra_data):
     # start test
     host_ip = get_my_ip()
     if (get_server_status(host_ip, server_port) is False):
-        thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
+        thread1 = multiprocessing.Process(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
         thread1.daemon = True
         thread1.start()
     dut1.start_app()
@@ -168,7 +168,7 @@ def test_examples_protocol_native_ota_example(env, extra_data):
             print('Connected to AP with IP: {}'.format(ip_address))
         except DUT.ExpectTimeout:
             raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
-            thread1.close()
+            thread1.terminate()
         dut1.expect('Starting OTA example', timeout=30)
 
         print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + bin_name))
@@ -176,6 +176,7 @@ def test_examples_protocol_native_ota_example(env, extra_data):
         dut1.expect('Loaded app from partition at offset', timeout=60)
         dut1.expect('Starting OTA example', timeout=30)
         dut1.reset()
+    thread1.terminate()
 
 
 @ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
@@ -211,7 +212,7 @@ def test_examples_protocol_native_ota_example_truncated_bin(env, extra_data):
     # start test
     host_ip = get_my_ip()
     if (get_server_status(host_ip, server_port) is False):
-        thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
+        thread1 = multiprocessing.Process(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
         thread1.daemon = True
         thread1.start()
     dut1.start_app()
@@ -221,12 +222,14 @@ def test_examples_protocol_native_ota_example_truncated_bin(env, extra_data):
         print('Connected to AP with IP: {}'.format(ip_address))
     except DUT.ExpectTimeout:
         raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
+        thread1.terminate()
     dut1.expect('Starting OTA example', timeout=30)
 
     print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + truncated_bin_name))
     dut1.write('https://' + host_ip + ':' + str(server_port) + '/' + truncated_bin_name)
     dut1.expect('native_ota_example: Image validation failed, image is corrupted', timeout=20)
     os.remove(binary_file)
+    thread1.terminate()
 
 
 @ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
@@ -261,7 +264,7 @@ def test_examples_protocol_native_ota_example_truncated_header(env, extra_data):
     # start test
     host_ip = get_my_ip()
     if (get_server_status(host_ip, server_port) is False):
-        thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
+        thread1 = multiprocessing.Process(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
         thread1.daemon = True
         thread1.start()
     dut1.start_app()
@@ -271,12 +274,14 @@ def test_examples_protocol_native_ota_example_truncated_header(env, extra_data):
         print('Connected to AP with IP: {}'.format(ip_address))
     except DUT.ExpectTimeout:
         raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
+        thread1.terminate()
     dut1.expect('Starting OTA example', timeout=30)
 
     print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + truncated_bin_name))
     dut1.write('https://' + host_ip + ':' + str(server_port) + '/' + truncated_bin_name)
     dut1.expect('native_ota_example: received package is not fit len', timeout=20)
     os.remove(binary_file)
+    thread1.terminate()
 
 
 @ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
@@ -310,7 +315,7 @@ def test_examples_protocol_native_ota_example_random(env, extra_data):
     # start test
     host_ip = get_my_ip()
     if (get_server_status(host_ip, server_port) is False):
-        thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
+        thread1 = multiprocessing.Process(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
         thread1.daemon = True
         thread1.start()
     dut1.start_app()
@@ -320,12 +325,14 @@ def test_examples_protocol_native_ota_example_random(env, extra_data):
         print('Connected to AP with IP: {}'.format(ip_address))
     except DUT.ExpectTimeout:
         raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
+        thread1.terminate()
     dut1.expect('Starting OTA example', timeout=30)
 
     print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + random_bin_name))
     dut1.write('https://' + host_ip + ':' + str(server_port) + '/' + random_bin_name)
     dut1.expect('esp_ota_ops: OTA image has invalid magic byte', timeout=20)
     os.remove(binary_file)
+    thread1.terminate()
 
 
 @ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')

+ 46 - 19
examples/system/ota/simple_ota_example/example_test.py

@@ -1,10 +1,10 @@
 import http.server
+import multiprocessing
 import os
 import re
 import socket
 import ssl
 import sys
-from threading import Thread
 
 import ttfw_idf
 from tiny_test_fw import DUT, Utility
@@ -68,6 +68,15 @@ def get_my_ip():
     return my_ip
 
 
+def get_server_status(host_ip, server_port):
+    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+    server_status = sock.connect_ex((host_ip, server_port))
+    sock.close()
+    if server_status == 0:
+        return True
+    return False
+
+
 def start_https_server(ota_image_dir, server_ip, server_port, server_file=None, key_file=None):
     os.chdir(ota_image_dir)
 
@@ -130,9 +139,10 @@ def test_examples_protocol_simple_ota_example(env, extra_data):
     sha256_bootloader, sha256_app = calc_all_sha256(dut1)
     # start test
     host_ip = get_my_ip()
-    thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, 8000))
-    thread1.daemon = True
-    thread1.start()
+    if (get_server_status(host_ip, 8000) is False):
+        thread1 = multiprocessing.Process(target=start_https_server, args=(dut1.app.binary_path, host_ip, 8000))
+        thread1.daemon = True
+        thread1.start()
     dut1.start_app()
     dut1.expect('Loaded app from partition at offset 0x10000', timeout=30)
     check_sha256(sha256_bootloader, dut1.expect(re.compile(r'SHA-256 for bootloader:\s+([a-f0-9]+)'))[0])
@@ -142,12 +152,14 @@ def test_examples_protocol_simple_ota_example(env, extra_data):
         print('Connected to AP with IP: {}'.format(ip_address))
     except DUT.ExpectTimeout:
         raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
+        thread1.terminate()
     dut1.expect('Starting OTA example', timeout=30)
 
     print('writing to device: {}'.format('https://' + host_ip + ':8000/simple_ota.bin'))
     dut1.write('https://' + host_ip + ':8000/simple_ota.bin')
     dut1.expect('Loaded app from partition at offset 0x110000', timeout=60)
     dut1.expect('Starting OTA example', timeout=30)
+    thread1.terminate()
 
 
 @ttfw_idf.idf_example_test(env_tag='Example_EthKitV1')
@@ -165,9 +177,10 @@ def test_examples_protocol_simple_ota_example_ethernet_with_spiram_config(env, e
     ttfw_idf.log_performance('simple_ota_bin_size', '{}KB'.format(bin_size // 1024))
     # start test
     host_ip = get_my_ip()
-    thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, 8000))
-    thread1.daemon = True
-    thread1.start()
+    if (get_server_status(host_ip, 8000) is False):
+        thread1 = multiprocessing.Process(target=start_https_server, args=(dut1.app.binary_path, host_ip, 8000))
+        thread1.daemon = True
+        thread1.start()
     dut1.start_app()
     dut1.expect('Loaded app from partition at offset 0x10000', timeout=30)
     try:
@@ -175,12 +188,14 @@ def test_examples_protocol_simple_ota_example_ethernet_with_spiram_config(env, e
         print('Connected to AP with IP: {}'.format(ip_address))
     except DUT.ExpectTimeout:
         raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
+        thread1.terminate()
     dut1.expect('Starting OTA example', timeout=30)
 
     print('writing to device: {}'.format('https://' + host_ip + ':8000/simple_ota.bin'))
     dut1.write('https://' + host_ip + ':8000/simple_ota.bin')
     dut1.expect('Loaded app from partition at offset 0x110000', timeout=60)
     dut1.expect('Starting OTA example', timeout=30)
+    thread1.terminate()
 
 
 @ttfw_idf.idf_example_test(env_tag='Example_Flash_Encryption_OTA')
@@ -201,9 +216,10 @@ def test_examples_protocol_simple_ota_example_with_flash_encryption(env, extra_d
     dut1.erase_flash()
     # start test
     host_ip = get_my_ip()
-    thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, 8000))
-    thread1.daemon = True
-    thread1.start()
+    if (get_server_status(host_ip, 8000) is False):
+        thread1 = multiprocessing.Process(target=start_https_server, args=(dut1.app.binary_path, host_ip, 8000))
+        thread1.daemon = True
+        thread1.start()
     dut1.start_app()
     dut1.expect('Loaded app from partition at offset 0x20000', timeout=30)
     dut1.expect('Flash encryption mode is DEVELOPMENT (not secure)', timeout=10)
@@ -212,6 +228,7 @@ def test_examples_protocol_simple_ota_example_with_flash_encryption(env, extra_d
         print('Connected to AP with IP: {}'.format(ip_address))
     except DUT.ExpectTimeout:
         raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
+        thread1.terminate()
     dut1.expect('Starting OTA example', timeout=30)
 
     print('writing to device: {}'.format('https://' + host_ip + ':8000/simple_ota.bin'))
@@ -219,6 +236,7 @@ def test_examples_protocol_simple_ota_example_with_flash_encryption(env, extra_d
     dut1.expect('Loaded app from partition at offset 0x120000', timeout=60)
     dut1.expect('Flash encryption mode is DEVELOPMENT (not secure)', timeout=10)
     dut1.expect('Starting OTA example', timeout=30)
+    thread1.terminate()
 
 
 @ttfw_idf.idf_example_test(env_tag='Example_Flash_Encryption_OTA_WiFi', target=['esp32c3'])
@@ -239,9 +257,10 @@ def test_examples_protocol_simple_ota_example_with_flash_encryption_wifi(env, ex
     dut1.erase_flash()
     # start test
     host_ip = get_my_ip()
-    thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, 8000))
-    thread1.daemon = True
-    thread1.start()
+    if (get_server_status(host_ip, 8000) is False):
+        thread1 = multiprocessing.Process(target=start_https_server, args=(dut1.app.binary_path, host_ip, 8000))
+        thread1.daemon = True
+        thread1.start()
     dut1.start_app()
     dut1.expect('Loaded app from partition at offset 0x20000', timeout=30)
     dut1.expect('Flash encryption mode is DEVELOPMENT (not secure)', timeout=10)
@@ -250,6 +269,7 @@ def test_examples_protocol_simple_ota_example_with_flash_encryption_wifi(env, ex
         print('Connected to AP with IP: {}'.format(ip_address))
     except DUT.ExpectTimeout:
         raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
+        thread1.terminate()
     dut1.expect('Starting OTA example', timeout=30)
 
     print('writing to device: {}'.format('https://' + host_ip + ':8000/simple_ota.bin'))
@@ -257,6 +277,7 @@ def test_examples_protocol_simple_ota_example_with_flash_encryption_wifi(env, ex
     dut1.expect('Loaded app from partition at offset 0x120000', timeout=60)
     dut1.expect('Flash encryption mode is DEVELOPMENT (not secure)', timeout=10)
     dut1.expect('Starting OTA example', timeout=30)
+    thread1.terminate()
 
 
 @ttfw_idf.idf_example_test(env_tag='Example_EthKitV1')
@@ -276,9 +297,10 @@ def test_examples_protocol_simple_ota_example_with_verify_app_signature_on_updat
     sha256_bootloader, sha256_app = calc_all_sha256(dut1)
     # start test
     host_ip = get_my_ip()
-    thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, 8000))
-    thread1.daemon = True
-    thread1.start()
+    if (get_server_status(host_ip, 8000) is False):
+        thread1 = multiprocessing.Process(target=start_https_server, args=(dut1.app.binary_path, host_ip, 8000))
+        thread1.daemon = True
+        thread1.start()
     dut1.start_app()
     dut1.expect('Loaded app from partition at offset 0x20000', timeout=30)
     check_sha256(sha256_bootloader, dut1.expect(re.compile(r'SHA-256 for bootloader:\s+([a-f0-9]+)'))[0])
@@ -288,6 +310,7 @@ def test_examples_protocol_simple_ota_example_with_verify_app_signature_on_updat
         print('Connected to AP with IP: {}'.format(ip_address))
     except DUT.ExpectTimeout:
         raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
+        thread1.terminate()
     dut1.expect('Starting OTA example', timeout=30)
 
     print('writing to device: {}'.format('https://' + host_ip + ':8000/simple_ota.bin'))
@@ -298,6 +321,7 @@ def test_examples_protocol_simple_ota_example_with_verify_app_signature_on_updat
 
     dut1.expect('Loaded app from partition at offset 0x120000', timeout=20)
     dut1.expect('Starting OTA example', timeout=30)
+    thread1.terminate()
 
 
 @ttfw_idf.idf_example_test(env_tag='Example_EthKitV12')
@@ -317,9 +341,10 @@ def test_examples_protocol_simple_ota_example_with_verify_app_signature_on_updat
     sha256_bootloader, sha256_app = calc_all_sha256(dut1)
     # start test
     host_ip = get_my_ip()
-    thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, 8000))
-    thread1.daemon = True
-    thread1.start()
+    if (get_server_status(host_ip, 8000) is False):
+        thread1 = multiprocessing.Process(target=start_https_server, args=(dut1.app.binary_path, host_ip, 8000))
+        thread1.daemon = True
+        thread1.start()
     dut1.start_app()
     dut1.expect('Loaded app from partition at offset 0x20000', timeout=30)
     check_sha256(sha256_bootloader, dut1.expect(re.compile(r'SHA-256 for bootloader:\s+([a-f0-9]+)'))[0])
@@ -329,6 +354,7 @@ def test_examples_protocol_simple_ota_example_with_verify_app_signature_on_updat
         print('Connected to AP with IP: {}'.format(ip_address))
     except DUT.ExpectTimeout:
         raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
+        thread1.terminate()
     dut1.expect('Starting OTA example', timeout=30)
 
     print('writing to device: {}'.format('https://' + host_ip + ':8000/simple_ota.bin'))
@@ -342,6 +368,7 @@ def test_examples_protocol_simple_ota_example_with_verify_app_signature_on_updat
 
     dut1.expect('Loaded app from partition at offset 0x120000', timeout=20)
     dut1.expect('Starting OTA example', timeout=30)
+    thread1.terminate()
 
 
 if __name__ == '__main__':