|
|
@@ -71,19 +71,32 @@ def get_my_ip():
|
|
|
return my_ip
|
|
|
|
|
|
|
|
|
-def start_https_server(ota_image_dir, server_ip, server_port):
|
|
|
- os.chdir(ota_image_dir)
|
|
|
+def get_server_status(host_ip, port):
|
|
|
+ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
+ server_status = sock.connect_ex((host_ip, port))
|
|
|
+ sock.close()
|
|
|
+ if server_status == 0:
|
|
|
+ return True
|
|
|
+ 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")
|
|
|
- cert_file_handle = open(server_file, "w+")
|
|
|
- cert_file_handle.write(server_cert)
|
|
|
- cert_file_handle.close()
|
|
|
+ create_file(server_file, server_cert)
|
|
|
|
|
|
key_file = os.path.join(ota_image_dir, "server_key.pem")
|
|
|
- key_file_handle = open("server_key.pem", "w+")
|
|
|
- key_file_handle.write(server_key)
|
|
|
- key_file_handle.close()
|
|
|
+ create_file(key_file, server_key)
|
|
|
+ return server_file, key_file
|
|
|
|
|
|
+
|
|
|
+def start_https_server(ota_image_dir, server_ip, server_port):
|
|
|
+ server_file, key_file = get_ca_cert(ota_image_dir)
|
|
|
httpd = BaseHTTPServer.HTTPServer((server_ip, server_port),
|
|
|
SimpleHTTPServer.SimpleHTTPRequestHandler)
|
|
|
|
|
|
@@ -94,17 +107,37 @@ 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)
|
|
|
+ chunked_server = subprocess.Popen(["openssl", "s_server", "-WWW", "-key", key_file, "-cert", server_file, "-port", str(server_port)])
|
|
|
+ return chunked_server
|
|
|
+
|
|
|
+
|
|
|
+def redirect_handler_factory(url):
|
|
|
+ """
|
|
|
+ Returns a request handler class that redirects to supplied `url`
|
|
|
+ """
|
|
|
+ class RedirectHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
|
|
|
+ def do_GET(self):
|
|
|
+ print("Sending resp, URL: " + url)
|
|
|
+ self.send_response(301)
|
|
|
+ self.send_header('Location', url)
|
|
|
+ self.end_headers()
|
|
|
+
|
|
|
+ return RedirectHandler
|
|
|
+
|
|
|
+
|
|
|
+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")
|
|
|
|
|
|
- server_file = os.path.join(ota_image_dir, "server_cert.pem")
|
|
|
- cert_file_handle = open(server_file, "w+")
|
|
|
- cert_file_handle.write(server_cert)
|
|
|
+ httpd = BaseHTTPServer.HTTPServer((server_ip, server_port),
|
|
|
+ redirectHandler)
|
|
|
|
|
|
- key_file = os.path.join(ota_image_dir, "server_key.pem")
|
|
|
- key_file_handle = open("server_key.pem", "w+")
|
|
|
- key_file_handle.write(server_key)
|
|
|
- chunked_server = subprocess.Popen(["openssl", "s_server", "-WWW", "-key", key_file, "-cert", server_file, "-port", str(server_port)])
|
|
|
- return chunked_server
|
|
|
+ httpd.socket = ssl.wrap_socket(httpd.socket,
|
|
|
+ keyfile=key_file,
|
|
|
+ certfile=server_file, server_side=True)
|
|
|
+ httpd.serve_forever()
|
|
|
|
|
|
|
|
|
@ttfw_idf.idf_example_test(env_tag="Example_WIFI")
|
|
|
@@ -120,6 +153,7 @@ def test_examples_protocol_advanced_https_ota_example(env, extra_data):
|
|
|
dut1 = env.get_dut("advanced_https_ota_example", "examples/system/ota/advanced_https_ota", dut_class=ttfw_idf.ESP32DUT)
|
|
|
# Number of iterations to validate OTA
|
|
|
iterations = 3
|
|
|
+ server_port = 8001
|
|
|
# File to be downloaded. This file is generated after compilation
|
|
|
bin_name = "advanced_https_ota.bin"
|
|
|
# check and log bin size
|
|
|
@@ -129,9 +163,10 @@ def test_examples_protocol_advanced_https_ota_example(env, extra_data):
|
|
|
ttfw_idf.check_performance("advanced_https_ota_bin_size", bin_size // 1024)
|
|
|
# start test
|
|
|
host_ip = get_my_ip()
|
|
|
- thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, 8001))
|
|
|
- thread1.daemon = True
|
|
|
- thread1.start()
|
|
|
+ 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.daemon = True
|
|
|
+ thread1.start()
|
|
|
dut1.start_app()
|
|
|
for i in range(iterations):
|
|
|
dut1.expect("Loaded app from partition at offset", timeout=30)
|
|
|
@@ -143,8 +178,8 @@ def test_examples_protocol_advanced_https_ota_example(env, extra_data):
|
|
|
thread1.close()
|
|
|
dut1.expect("Starting Advanced OTA example", timeout=30)
|
|
|
|
|
|
- print("writing to device: {}".format("https://" + host_ip + ":8001/" + bin_name))
|
|
|
- dut1.write("https://" + host_ip + ":8001/" + bin_name)
|
|
|
+ print("writing to device: {}".format("https://" + host_ip + ":" + str(server_port) + "/" + bin_name))
|
|
|
+ dut1.write("https://" + host_ip + ":" + str(server_port) + "/" + bin_name)
|
|
|
dut1.expect("Loaded app from partition at offset", timeout=60)
|
|
|
dut1.expect("Starting Advanced OTA example", timeout=30)
|
|
|
dut1.reset()
|
|
|
@@ -162,6 +197,7 @@ def test_examples_protocol_advanced_https_ota_example_truncated_bin(env, extra_d
|
|
|
4. Check working of code if bin is truncated
|
|
|
"""
|
|
|
dut1 = env.get_dut("advanced_https_ota_example", "examples/system/ota/advanced_https_ota", dut_class=ttfw_idf.ESP32DUT)
|
|
|
+ server_port = 8001
|
|
|
# Original binary file generated after compilation
|
|
|
bin_name = "advanced_https_ota.bin"
|
|
|
# Truncated binary file to be generated from original binary file
|
|
|
@@ -182,6 +218,10 @@ def test_examples_protocol_advanced_https_ota_example_truncated_bin(env, extra_d
|
|
|
ttfw_idf.check_performance("advanced_https_ota_bin_size", bin_size // 1024)
|
|
|
# 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.daemon = True
|
|
|
+ thread1.start()
|
|
|
dut1.start_app()
|
|
|
dut1.expect("Loaded app from partition at offset", timeout=30)
|
|
|
try:
|
|
|
@@ -191,8 +231,8 @@ def test_examples_protocol_advanced_https_ota_example_truncated_bin(env, extra_d
|
|
|
raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
|
|
|
dut1.expect("Starting Advanced OTA example", timeout=30)
|
|
|
|
|
|
- print("writing to device: {}".format("https://" + host_ip + ":8001/" + truncated_bin_name))
|
|
|
- dut1.write("https://" + host_ip + ":8001/" + truncated_bin_name)
|
|
|
+ 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)
|
|
|
|
|
|
@@ -209,6 +249,7 @@ def test_examples_protocol_advanced_https_ota_example_truncated_header(env, extr
|
|
|
4. Check working of code if headers are not sent completely
|
|
|
"""
|
|
|
dut1 = env.get_dut("advanced_https_ota_example", "examples/system/ota/advanced_https_ota", dut_class=ttfw_idf.ESP32DUT)
|
|
|
+ server_port = 8001
|
|
|
# Original binary file generated after compilation
|
|
|
bin_name = "advanced_https_ota.bin"
|
|
|
# Truncated binary file to be generated from original binary file
|
|
|
@@ -228,6 +269,10 @@ def test_examples_protocol_advanced_https_ota_example_truncated_header(env, extr
|
|
|
ttfw_idf.check_performance("advanced_https_ota_bin_size", bin_size // 1024)
|
|
|
# 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.daemon = True
|
|
|
+ thread1.start()
|
|
|
dut1.start_app()
|
|
|
dut1.expect("Loaded app from partition at offset", timeout=30)
|
|
|
try:
|
|
|
@@ -237,8 +282,8 @@ def test_examples_protocol_advanced_https_ota_example_truncated_header(env, extr
|
|
|
raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
|
|
|
dut1.expect("Starting Advanced OTA example", timeout=30)
|
|
|
|
|
|
- print("writing to device: {}".format("https://" + host_ip + ":8001/" + truncated_bin_name))
|
|
|
- dut1.write("https://" + host_ip + ":8001/" + truncated_bin_name)
|
|
|
+ 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)
|
|
|
|
|
|
@@ -255,6 +300,7 @@ def test_examples_protocol_advanced_https_ota_example_random(env, extra_data):
|
|
|
4. Check working of code for random binary file
|
|
|
"""
|
|
|
dut1 = env.get_dut("advanced_https_ota_example", "examples/system/ota/advanced_https_ota", dut_class=ttfw_idf.ESP32DUT)
|
|
|
+ server_port = 8001
|
|
|
# Random binary file to be generated
|
|
|
random_bin_name = "random.bin"
|
|
|
# Size of random binary file. 32000 is choosen, to reduce the time required to run the test-case
|
|
|
@@ -273,6 +319,10 @@ def test_examples_protocol_advanced_https_ota_example_random(env, extra_data):
|
|
|
ttfw_idf.check_performance("advanced_https_ota_bin_size", bin_size // 1024)
|
|
|
# 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.daemon = True
|
|
|
+ thread1.start()
|
|
|
dut1.start_app()
|
|
|
dut1.expect("Loaded app from partition at offset", timeout=30)
|
|
|
try:
|
|
|
@@ -282,8 +332,8 @@ def test_examples_protocol_advanced_https_ota_example_random(env, extra_data):
|
|
|
raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
|
|
|
dut1.expect("Starting Advanced OTA example", timeout=30)
|
|
|
|
|
|
- print("writing to device: {}".format("https://" + host_ip + ":8001/" + random_bin_name))
|
|
|
- dut1.write("https://" + host_ip + ":8001/" + random_bin_name)
|
|
|
+ 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)
|
|
|
|
|
|
@@ -327,9 +377,59 @@ def test_examples_protocol_advanced_https_ota_example_chunked(env, extra_data):
|
|
|
os.remove(os.path.join(dut1.app.binary_path, "server_key.pem"))
|
|
|
|
|
|
|
|
|
+@ttfw_idf.idf_example_test(env_tag="Example_WIFI")
|
|
|
+def test_examples_protocol_advanced_https_ota_example_redirect_url(env, extra_data):
|
|
|
+ """
|
|
|
+ This is a positive test case, which starts a server and a redirection server.
|
|
|
+ Redirection server redirects http_request to different port
|
|
|
+ Number of iterations can be specified in variable iterations.
|
|
|
+ steps: |
|
|
|
+ 1. join AP
|
|
|
+ 2. Fetch OTA image over HTTPS
|
|
|
+ 3. Reboot with the new OTA image
|
|
|
+ """
|
|
|
+ dut1 = env.get_dut("advanced_https_ota_example", "examples/system/ota/advanced_https_ota", dut_class=ttfw_idf.ESP32DUT)
|
|
|
+ server_port = 8001
|
|
|
+ # Port to which the request should be redirecetd
|
|
|
+ redirection_server_port = 8081
|
|
|
+ # File to be downloaded. This file is generated after compilation
|
|
|
+ bin_name = "advanced_https_ota.bin"
|
|
|
+ # check and log bin size
|
|
|
+ binary_file = os.path.join(dut1.app.binary_path, bin_name)
|
|
|
+ bin_size = os.path.getsize(binary_file)
|
|
|
+ ttfw_idf.log_performance("advanced_https_ota_bin_size", "{}KB".format(bin_size // 1024))
|
|
|
+ ttfw_idf.check_performance("advanced_https_ota_bin_size", bin_size // 1024)
|
|
|
+ # 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.daemon = True
|
|
|
+ thread1.start()
|
|
|
+ thread2 = Thread(target=start_redirect_server, args=(dut1.app.binary_path, host_ip, redirection_server_port, server_port))
|
|
|
+ thread2.daemon = True
|
|
|
+ thread2.start()
|
|
|
+ dut1.start_app()
|
|
|
+ dut1.expect("Loaded app from partition at offset", timeout=30)
|
|
|
+ try:
|
|
|
+ ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30)
|
|
|
+ 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()
|
|
|
+ dut1.expect("Starting Advanced OTA example", timeout=30)
|
|
|
+
|
|
|
+ print("writing to device: {}".format("https://" + host_ip + ":" + str(redirection_server_port) + "/" + bin_name))
|
|
|
+ dut1.write("https://" + host_ip + ":" + str(redirection_server_port) + "/" + bin_name)
|
|
|
+ dut1.expect("Loaded app from partition at offset", timeout=60)
|
|
|
+ dut1.expect("Starting Advanced OTA example", timeout=30)
|
|
|
+ dut1.reset()
|
|
|
+
|
|
|
+
|
|
|
if __name__ == '__main__':
|
|
|
test_examples_protocol_advanced_https_ota_example()
|
|
|
test_examples_protocol_advanced_https_ota_example_chunked()
|
|
|
+ test_examples_protocol_advanced_https_ota_example_redirect_url()
|
|
|
test_examples_protocol_advanced_https_ota_example_truncated_bin()
|
|
|
test_examples_protocol_advanced_https_ota_example_truncated_header()
|
|
|
test_examples_protocol_advanced_https_ota_example_random()
|