OTA: Add fix in case of URL redirection

Test case for URL Redirection and Chunked server is added
This commit is contained in:
Shubham Kulkarni 2020-02-13 13:43:58 +05:30 committed by maojianxin
parent 239bd70ea1
commit bc881cc32d
4 changed files with 295 additions and 85 deletions

View File

@ -957,7 +957,7 @@ nvs_compatible_test:
example_test_001: example_test_001:
<<: *example_test_template <<: *example_test_template
parallel: 2 parallel: 3
tags: tags:
- ESP32 - ESP32
- Example_WIFI - Example_WIFI

View File

@ -70,27 +70,20 @@ static esp_err_t _http_handle_response_code(esp_http_client_handle_t http_client
} }
char upgrade_data_buf[DEFAULT_OTA_BUF_SIZE]; char upgrade_data_buf[DEFAULT_OTA_BUF_SIZE];
/* // process_again() returns true only in case of redirection.
* `data_read_size` holds number of bytes to be read.
* `bytes_read` holds number of bytes read.
*/
int bytes_read = 0, data_read_size = DEFAULT_OTA_BUF_SIZE;
if (process_again(status_code)) { if (process_again(status_code)) {
while (data_read_size > 0) { while (1) {
int data_read = esp_http_client_read(http_client, (upgrade_data_buf + bytes_read), data_read_size);
/* /*
* As esp_http_client_read never returns negative error code, we rely on * In case of redirection, esp_http_client_read() is called
* `errno` to check for underlying transport connectivity closure if any * to clear the response buffer of http_client.
*/ */
if (errno == ENOTCONN || errno == ECONNRESET) { int data_read = esp_http_client_read(http_client, upgrade_data_buf, DEFAULT_OTA_BUF_SIZE);
ESP_LOGE(TAG, "Connection closed, errno = %d", errno); if (data_read < 0) {
break; ESP_LOGE(TAG, "Error: SSL data read error");
return ESP_FAIL;
} else if (data_read == 0) {
return ESP_OK;
} }
bytes_read += data_read;
data_read_size -= data_read;
}
if (data_read_size > 0) {
return ESP_FAIL;
} }
} }
return ESP_OK; return ESP_OK;

View File

@ -21,6 +21,7 @@ except ImportError:
import DUT import DUT
import random import random
import subprocess
server_cert = "-----BEGIN CERTIFICATE-----\n" \ server_cert = "-----BEGIN CERTIFICATE-----\n" \
"MIIDXTCCAkWgAwIBAgIJAP4LF7E72HakMA0GCSqGSIb3DQEBCwUAMEUxCzAJBgNV\n"\ "MIIDXTCCAkWgAwIBAgIJAP4LF7E72HakMA0GCSqGSIb3DQEBCwUAMEUxCzAJBgNV\n"\
@ -82,23 +83,32 @@ def get_my_ip():
return my_ip return my_ip
def start_https_server(ota_image_dir, server_ip, server_port): def get_server_status(host_ip, port):
# parser = argparse.ArgumentParser() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# parser.add_argument('-p', '--port', dest='port', type= int, server_status = sock.connect_ex((host_ip, port))
# help= "Server Port", default= 8000) sock.close()
# args = parser.parse_args() if server_status == 0:
os.chdir(ota_image_dir) 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") server_file = os.path.join(ota_image_dir, "server_cert.pem")
cert_file_handle = open(server_file, "w+") create_file(server_file, server_cert)
cert_file_handle.write(server_cert)
cert_file_handle.close()
key_file = os.path.join(ota_image_dir, "server_key.pem") key_file = os.path.join(ota_image_dir, "server_key.pem")
key_file_handle = open("server_key.pem", "w+") create_file(key_file, server_key)
key_file_handle.write(server_key) return server_file, key_file
key_file_handle.close()
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), httpd = BaseHTTPServer.HTTPServer((server_ip, server_port),
SimpleHTTPServer.SimpleHTTPRequestHandler) SimpleHTTPServer.SimpleHTTPRequestHandler)
@ -108,6 +118,40 @@ def start_https_server(ota_image_dir, server_ip, server_port):
httpd.serve_forever() httpd.serve_forever()
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")
httpd = BaseHTTPServer.HTTPServer((server_ip, server_port),
redirectHandler)
httpd.socket = ssl.wrap_socket(httpd.socket,
keyfile=key_file,
certfile=server_file, server_side=True)
httpd.serve_forever()
@IDF.idf_example_test(env_tag="Example_WIFI") @IDF.idf_example_test(env_tag="Example_WIFI")
def test_examples_protocol_advanced_https_ota_example(env, extra_data): def test_examples_protocol_advanced_https_ota_example(env, extra_data):
""" """
@ -121,6 +165,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") dut1 = env.get_dut("advanced_https_ota_example", "examples/system/ota/advanced_https_ota")
# Number of iterations to validate OTA # Number of iterations to validate OTA
iterations = 3 iterations = 3
server_port = 8001
# File to be downloaded. This file is generated after compilation # File to be downloaded. This file is generated after compilation
bin_name = "advanced_https_ota.bin" bin_name = "advanced_https_ota.bin"
# check and log bin size # check and log bin size
@ -130,9 +175,10 @@ def test_examples_protocol_advanced_https_ota_example(env, extra_data):
IDF.check_performance("advanced_https_ota_bin_size", bin_size // 1024) IDF.check_performance("advanced_https_ota_bin_size", bin_size // 1024)
# start test # start test
host_ip = get_my_ip() host_ip = get_my_ip()
thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, 8001)) if (get_server_status(host_ip, server_port) is False):
thread1.daemon = True thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
thread1.start() thread1.daemon = True
thread1.start()
dut1.start_app() dut1.start_app()
for i in range(iterations): for i in range(iterations):
dut1.expect("Loaded app from partition at offset", timeout=30) dut1.expect("Loaded app from partition at offset", timeout=30)
@ -144,8 +190,8 @@ def test_examples_protocol_advanced_https_ota_example(env, extra_data):
thread1.close() thread1.close()
dut1.expect("Connected to WiFi network! Attempting to connect to server...", timeout=30) dut1.expect("Connected to WiFi network! Attempting to connect to server...", timeout=30)
print("writing to device: {}".format("https://" + host_ip + ":8001/" + bin_name)) print("writing to device: {}".format("https://" + host_ip + ":" + str(server_port) + "/" + bin_name))
dut1.write("https://" + host_ip + ":8001/" + bin_name) dut1.write("https://" + host_ip + ":" + str(server_port) + "/" + bin_name)
dut1.expect("Loaded app from partition at offset", timeout=60) dut1.expect("Loaded app from partition at offset", timeout=60)
dut1.expect("Connected to WiFi network! Attempting to connect to server...", timeout=30) dut1.expect("Connected to WiFi network! Attempting to connect to server...", timeout=30)
dut1.reset() dut1.reset()
@ -163,6 +209,7 @@ def test_examples_protocol_advanced_https_ota_example_truncated_bin(env, extra_d
4. Check working of code if bin is truncated 4. Check working of code if bin is truncated
""" """
dut1 = env.get_dut("advanced_https_ota_example", "examples/system/ota/advanced_https_ota") dut1 = env.get_dut("advanced_https_ota_example", "examples/system/ota/advanced_https_ota")
server_port = 8001
# Original binary file generated after compilation # Original binary file generated after compilation
bin_name = "advanced_https_ota.bin" bin_name = "advanced_https_ota.bin"
# Truncated binary file to be generated from original binary file # Truncated binary file to be generated from original binary file
@ -183,9 +230,10 @@ def test_examples_protocol_advanced_https_ota_example_truncated_bin(env, extra_d
IDF.check_performance("advanced_https_ota_bin_size", bin_size // 1024) IDF.check_performance("advanced_https_ota_bin_size", bin_size // 1024)
# start test # start test
host_ip = get_my_ip() host_ip = get_my_ip()
thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, 8002)) if (get_server_status(host_ip, server_port) is False):
thread1.daemon = True thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
thread1.start() thread1.daemon = True
thread1.start()
dut1.start_app() dut1.start_app()
dut1.expect("Loaded app from partition at offset", timeout=30) dut1.expect("Loaded app from partition at offset", timeout=30)
try: try:
@ -195,9 +243,10 @@ def test_examples_protocol_advanced_https_ota_example_truncated_bin(env, extra_d
raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP') raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
dut1.expect("Connected to WiFi network! Attempting to connect to server...", timeout=30) dut1.expect("Connected to WiFi network! Attempting to connect to server...", timeout=30)
print("writing to device: {}".format("https://" + host_ip + ":8002/" + truncated_bin_name)) print("writing to device: {}".format("https://" + host_ip + ":" + str(server_port) + "/" + truncated_bin_name))
dut1.write("https://" + host_ip + ":8002/" + truncated_bin_name) dut1.write("https://" + host_ip + ":" + str(server_port) + "/" + truncated_bin_name)
dut1.expect("Image validation failed, image is corrupted", timeout=30) dut1.expect("Image validation failed, image is corrupted", timeout=30)
os.remove(binary_file)
@IDF.idf_example_test(env_tag="Example_WIFI") @IDF.idf_example_test(env_tag="Example_WIFI")
@ -212,6 +261,7 @@ def test_examples_protocol_advanced_https_ota_example_truncated_header(env, extr
4. Check working of code if headers are not sent completely 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") dut1 = env.get_dut("advanced_https_ota_example", "examples/system/ota/advanced_https_ota")
server_port = 8001
# Original binary file generated after compilation # Original binary file generated after compilation
bin_name = "advanced_https_ota.bin" bin_name = "advanced_https_ota.bin"
# Truncated binary file to be generated from original binary file # Truncated binary file to be generated from original binary file
@ -231,9 +281,10 @@ def test_examples_protocol_advanced_https_ota_example_truncated_header(env, extr
IDF.check_performance("advanced_https_ota_bin_size", bin_size // 1024) IDF.check_performance("advanced_https_ota_bin_size", bin_size // 1024)
# start test # start test
host_ip = get_my_ip() host_ip = get_my_ip()
thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, 8003)) if (get_server_status(host_ip, server_port) is False):
thread1.daemon = True thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
thread1.start() thread1.daemon = True
thread1.start()
dut1.start_app() dut1.start_app()
dut1.expect("Loaded app from partition at offset", timeout=30) dut1.expect("Loaded app from partition at offset", timeout=30)
try: try:
@ -243,9 +294,10 @@ def test_examples_protocol_advanced_https_ota_example_truncated_header(env, extr
raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP') raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
dut1.expect("Connected to WiFi network! Attempting to connect to server...", timeout=30) dut1.expect("Connected to WiFi network! Attempting to connect to server...", timeout=30)
print("writing to device: {}".format("https://" + host_ip + ":8003/" + truncated_bin_name)) print("writing to device: {}".format("https://" + host_ip + ":" + str(server_port) + "/" + truncated_bin_name))
dut1.write("https://" + host_ip + ":8003/" + 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) dut1.expect("advanced_https_ota_example: esp_https_ota_read_img_desc failed", timeout=30)
os.remove(binary_file)
@IDF.idf_example_test(env_tag="Example_WIFI") @IDF.idf_example_test(env_tag="Example_WIFI")
@ -260,6 +312,7 @@ def test_examples_protocol_advanced_https_ota_example_random(env, extra_data):
4. Check working of code for random binary file 4. Check working of code for random binary file
""" """
dut1 = env.get_dut("advanced_https_ota_example", "examples/system/ota/advanced_https_ota") dut1 = env.get_dut("advanced_https_ota_example", "examples/system/ota/advanced_https_ota")
server_port = 8001
# Random binary file to be generated # Random binary file to be generated
random_bin_name = "random.bin" random_bin_name = "random.bin"
# Size of random binary file. 32000 is choosen, to reduce the time required to run the test-case # Size of random binary file. 32000 is choosen, to reduce the time required to run the test-case
@ -267,7 +320,10 @@ def test_examples_protocol_advanced_https_ota_example_random(env, extra_data):
# check and log bin size # check and log bin size
binary_file = os.path.join(dut1.app.binary_path, random_bin_name) binary_file = os.path.join(dut1.app.binary_path, random_bin_name)
fo = open(binary_file, "w+") fo = open(binary_file, "w+")
for i in range(random_bin_size): # First byte of binary file is always set to zero. If first byte is generated randomly,
# in some cases it may generate 0xE9 which will result in failure of testcase.
fo.write(str(0))
for i in range(random_bin_size - 1):
fo.write(str(random.randrange(0,255,1))) fo.write(str(random.randrange(0,255,1)))
fo.close() fo.close()
bin_size = os.path.getsize(binary_file) bin_size = os.path.getsize(binary_file)
@ -275,9 +331,10 @@ def test_examples_protocol_advanced_https_ota_example_random(env, extra_data):
IDF.check_performance("advanced_https_ota_bin_size", bin_size // 1024) IDF.check_performance("advanced_https_ota_bin_size", bin_size // 1024)
# start test # start test
host_ip = get_my_ip() host_ip = get_my_ip()
thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, 8004)) if (get_server_status(host_ip, server_port) is False):
thread1.daemon = True thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
thread1.start() thread1.daemon = True
thread1.start()
dut1.start_app() dut1.start_app()
dut1.expect("Loaded app from partition at offset", timeout=30) dut1.expect("Loaded app from partition at offset", timeout=30)
try: try:
@ -287,13 +344,103 @@ def test_examples_protocol_advanced_https_ota_example_random(env, extra_data):
raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP') raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
dut1.expect("Connected to WiFi network! Attempting to connect to server...", timeout=30) dut1.expect("Connected to WiFi network! Attempting to connect to server...", timeout=30)
print("writing to device: {}".format("https://" + host_ip + ":8004/" + random_bin_name)) print("writing to device: {}".format("https://" + host_ip + ":" + str(server_port) + "/" + random_bin_name))
dut1.write("https://" + host_ip + ":8004/" + 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) dut1.expect("esp_ota_ops: OTA image has invalid magic byte", timeout=10)
os.remove(binary_file)
@IDF.idf_example_test(env_tag="Example_WIFI")
def test_examples_protocol_advanced_https_ota_example_chunked(env, extra_data):
"""
This is a positive test case, which downloads complete binary file multiple number of times.
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")
# 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)
IDF.log_performance("advanced_https_ota_bin_size", "{}KB".format(bin_size // 1024))
IDF.check_performance("advanced_https_ota_bin_size", bin_size // 1024)
# start test
host_ip = get_my_ip()
chunked_server = start_chunked_server(dut1.app.binary_path, 8070)
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')
dut1.expect("Connected to WiFi network! Attempting to connect to server...", timeout=30)
print("writing to device: {}".format("https://" + host_ip + ":8070/" + bin_name))
dut1.write("https://" + host_ip + ":8070/" + bin_name)
dut1.expect("Loaded app from partition at offset", timeout=60)
dut1.expect("Connected to WiFi network! Attempting to connect to server...", 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"))
@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
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")
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)
IDF.log_performance("advanced_https_ota_bin_size", "{}KB".format(bin_size // 1024))
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("Connected to WiFi network! Attempting to connect to server...", 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("Connected to WiFi network! Attempting to connect to server...", timeout=30)
dut1.reset()
if __name__ == '__main__': if __name__ == '__main__':
test_examples_protocol_advanced_https_ota_example() 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_bin()
test_examples_protocol_advanced_https_ota_example_truncated_header() test_examples_protocol_advanced_https_ota_example_truncated_header()
test_examples_protocol_advanced_https_ota_example_random() test_examples_protocol_advanced_https_ota_example_random()

View File

@ -21,6 +21,7 @@ except ImportError:
import DUT import DUT
import random import random
import subprocess
server_cert = "-----BEGIN CERTIFICATE-----\n" \ server_cert = "-----BEGIN CERTIFICATE-----\n" \
"MIIDXTCCAkWgAwIBAgIJAP4LF7E72HakMA0GCSqGSIb3DQEBCwUAMEUxCzAJBgNV\n"\ "MIIDXTCCAkWgAwIBAgIJAP4LF7E72HakMA0GCSqGSIb3DQEBCwUAMEUxCzAJBgNV\n"\
@ -82,23 +83,32 @@ def get_my_ip():
return my_ip return my_ip
def start_https_server(ota_image_dir, server_ip, server_port): def get_server_status(host_ip, port):
# parser = argparse.ArgumentParser() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# parser.add_argument('-p', '--port', dest='port', type= int, server_status = sock.connect_ex((host_ip, port))
# help= "Server Port", default= 8000) sock.close()
# args = parser.parse_args() if server_status == 0:
os.chdir(ota_image_dir) 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") server_file = os.path.join(ota_image_dir, "server_cert.pem")
cert_file_handle = open(server_file, "w+") create_file(server_file, server_cert)
cert_file_handle.write(server_cert)
cert_file_handle.close()
key_file = os.path.join(ota_image_dir, "server_key.pem") key_file = os.path.join(ota_image_dir, "server_key.pem")
key_file_handle = open("server_key.pem", "w+") create_file(key_file, server_key)
key_file_handle.write(server_key) return server_file, key_file
key_file_handle.close()
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), httpd = BaseHTTPServer.HTTPServer((server_ip, server_port),
SimpleHTTPServer.SimpleHTTPRequestHandler) SimpleHTTPServer.SimpleHTTPRequestHandler)
@ -108,6 +118,12 @@ def start_https_server(ota_image_dir, server_ip, server_port):
httpd.serve_forever() httpd.serve_forever()
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
@IDF.idf_example_test(env_tag="Example_WIFI") @IDF.idf_example_test(env_tag="Example_WIFI")
def test_examples_protocol_native_ota_example(env, extra_data): def test_examples_protocol_native_ota_example(env, extra_data):
""" """
@ -119,6 +135,7 @@ def test_examples_protocol_native_ota_example(env, extra_data):
3. Reboot with the new OTA image 3. Reboot with the new OTA image
""" """
dut1 = env.get_dut("native_ota_example", "examples/system/ota/native_ota_example") dut1 = env.get_dut("native_ota_example", "examples/system/ota/native_ota_example")
server_port = 8002
# No. of times working of application to be validated # No. of times working of application to be validated
iterations = 3 iterations = 3
# File to be downloaded. This file is generated after compilation # File to be downloaded. This file is generated after compilation
@ -130,9 +147,10 @@ def test_examples_protocol_native_ota_example(env, extra_data):
IDF.check_performance("native_ota_bin_size", bin_size // 1024) IDF.check_performance("native_ota_bin_size", bin_size // 1024)
# start test # start test
host_ip = get_my_ip() host_ip = get_my_ip()
thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, 8005)) if (get_server_status(host_ip, server_port) is False):
thread1.daemon = True thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
thread1.start() thread1.daemon = True
thread1.start()
dut1.start_app() dut1.start_app()
for i in range(iterations): for i in range(iterations):
dut1.expect("Loaded app from partition at offset", timeout=30) dut1.expect("Loaded app from partition at offset", timeout=30)
@ -144,10 +162,10 @@ def test_examples_protocol_native_ota_example(env, extra_data):
thread1.close() thread1.close()
dut1.expect("Connect to Wifi ! Start to Connect to Server....", timeout=30) dut1.expect("Connect to Wifi ! Start to Connect to Server....", timeout=30)
print("writing to device: {}".format("https://" + host_ip + ":8005/" + bin_name)) print("writing to device: {}".format("https://" + host_ip + ":" + str(server_port) + "/" + bin_name))
dut1.write("https://" + host_ip + ":8005/" + bin_name) dut1.write("https://" + host_ip + ":" + str(server_port) + "/" + bin_name)
dut1.expect("Loaded app from partition at offset", timeout=60) dut1.expect("Loaded app from partition at offset", timeout=60)
dut1.expect("Starting OTA example", timeout=30) dut1.expect("Connect to Wifi ! Start to Connect to Server....", timeout=30)
dut1.reset() dut1.reset()
@ -163,6 +181,7 @@ def test_examples_protocol_native_ota_example_truncated_bin(env, extra_data):
4. Check working of code if bin is truncated 4. Check working of code if bin is truncated
""" """
dut1 = env.get_dut("native_ota_example", "examples/system/ota/native_ota_example") dut1 = env.get_dut("native_ota_example", "examples/system/ota/native_ota_example")
server_port = 8002
# Original binary file generated after compilation # Original binary file generated after compilation
bin_name = "native_ota.bin" bin_name = "native_ota.bin"
# Truncated binary file to be generated from original binary file # Truncated binary file to be generated from original binary file
@ -183,9 +202,10 @@ def test_examples_protocol_native_ota_example_truncated_bin(env, extra_data):
IDF.check_performance("native_ota_bin_size", bin_size // 1024) IDF.check_performance("native_ota_bin_size", bin_size // 1024)
# start test # start test
host_ip = get_my_ip() host_ip = get_my_ip()
thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, 8006)) if (get_server_status(host_ip, server_port) is False):
thread1.daemon = True thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
thread1.start() thread1.daemon = True
thread1.start()
dut1.start_app() dut1.start_app()
dut1.expect("Loaded app from partition at offset", timeout=30) dut1.expect("Loaded app from partition at offset", timeout=30)
try: try:
@ -195,9 +215,10 @@ def test_examples_protocol_native_ota_example_truncated_bin(env, extra_data):
raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP') raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
dut1.expect("Connect to Wifi ! Start to Connect to Server....", timeout=30) dut1.expect("Connect to Wifi ! Start to Connect to Server....", timeout=30)
print("writing to device: {}".format("https://" + host_ip + ":8006/" + truncated_bin_name)) print("writing to device: {}".format("https://" + host_ip + ":" + str(server_port) + "/" + truncated_bin_name))
dut1.write("https://" + host_ip + ":8006/" + 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) dut1.expect("native_ota_example: Image validation failed, image is corrupted", timeout=20)
os.remove(binary_file)
@IDF.idf_example_test(env_tag="Example_WIFI") @IDF.idf_example_test(env_tag="Example_WIFI")
@ -212,6 +233,7 @@ def test_examples_protocol_native_ota_example_truncated_header(env, extra_data):
4. Check working of code if headers are not sent completely 4. Check working of code if headers are not sent completely
""" """
dut1 = env.get_dut("native_ota_example", "examples/system/ota/native_ota_example") dut1 = env.get_dut("native_ota_example", "examples/system/ota/native_ota_example")
server_port = 8002
# Original binary file generated after compilation # Original binary file generated after compilation
bin_name = "native_ota.bin" bin_name = "native_ota.bin"
# Truncated binary file to be generated from original binary file # Truncated binary file to be generated from original binary file
@ -231,9 +253,10 @@ def test_examples_protocol_native_ota_example_truncated_header(env, extra_data):
IDF.check_performance("native_ota_bin_size", bin_size // 1024) IDF.check_performance("native_ota_bin_size", bin_size // 1024)
# start test # start test
host_ip = get_my_ip() host_ip = get_my_ip()
thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, 8007)) if (get_server_status(host_ip, server_port) is False):
thread1.daemon = True thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
thread1.start() thread1.daemon = True
thread1.start()
dut1.start_app() dut1.start_app()
dut1.expect("Loaded app from partition at offset", timeout=30) dut1.expect("Loaded app from partition at offset", timeout=30)
try: try:
@ -243,9 +266,10 @@ def test_examples_protocol_native_ota_example_truncated_header(env, extra_data):
raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP') raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
dut1.expect("Connect to Wifi ! Start to Connect to Server....", timeout=30) dut1.expect("Connect to Wifi ! Start to Connect to Server....", timeout=30)
print("writing to device: {}".format("https://" + host_ip + ":8007/" + truncated_bin_name)) print("writing to device: {}".format("https://" + host_ip + ":" + str(server_port) + "/" + truncated_bin_name))
dut1.write("https://" + host_ip + ":8007/" + 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) dut1.expect("native_ota_example: received package is not fit len", timeout=20)
os.remove(binary_file)
@IDF.idf_example_test(env_tag="Example_WIFI") @IDF.idf_example_test(env_tag="Example_WIFI")
@ -260,6 +284,7 @@ def test_examples_protocol_native_ota_example_random(env, extra_data):
4. Check working of code for random binary file 4. Check working of code for random binary file
""" """
dut1 = env.get_dut("native_ota_example", "examples/system/ota/native_ota_example") dut1 = env.get_dut("native_ota_example", "examples/system/ota/native_ota_example")
server_port = 8002
# Random binary file to be generated # Random binary file to be generated
random_bin_name = "random.bin" random_bin_name = "random.bin"
# Size of random binary file. 32000 is choosen, to reduce the time required to run the test-case # Size of random binary file. 32000 is choosen, to reduce the time required to run the test-case
@ -267,7 +292,10 @@ def test_examples_protocol_native_ota_example_random(env, extra_data):
# check and log bin size # check and log bin size
binary_file = os.path.join(dut1.app.binary_path, random_bin_name) binary_file = os.path.join(dut1.app.binary_path, random_bin_name)
fo = open(binary_file, "w+") fo = open(binary_file, "w+")
for i in range(random_bin_size): # First byte of binary file is always set to zero. If first byte is generated randomly,
# in some cases it may generate 0xE9 which will result in failure of testcase.
fo.write(str(0))
for i in range(random_bin_size - 1):
fo.write(str(random.randrange(0,255,1))) fo.write(str(random.randrange(0,255,1)))
fo.close() fo.close()
bin_size = os.path.getsize(binary_file) bin_size = os.path.getsize(binary_file)
@ -275,9 +303,10 @@ def test_examples_protocol_native_ota_example_random(env, extra_data):
IDF.check_performance("native_ota_bin_size", bin_size // 1024) IDF.check_performance("native_ota_bin_size", bin_size // 1024)
# start test # start test
host_ip = get_my_ip() host_ip = get_my_ip()
thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, 8008)) if (get_server_status(host_ip, server_port) is False):
thread1.daemon = True thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port))
thread1.start() thread1.daemon = True
thread1.start()
dut1.start_app() dut1.start_app()
dut1.expect("Loaded app from partition at offset", timeout=30) dut1.expect("Loaded app from partition at offset", timeout=30)
try: try:
@ -287,13 +316,54 @@ def test_examples_protocol_native_ota_example_random(env, extra_data):
raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP') raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
dut1.expect("Connect to Wifi ! Start to Connect to Server....", timeout=30) dut1.expect("Connect to Wifi ! Start to Connect to Server....", timeout=30)
print("writing to device: {}".format("https://" + host_ip + ":8008/" + random_bin_name)) print("writing to device: {}".format("https://" + host_ip + ":" + str(server_port) + "/" + random_bin_name))
dut1.write("https://" + host_ip + ":8008/" + 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) dut1.expect("esp_ota_ops: OTA image has invalid magic byte", timeout=20)
os.remove(binary_file)
@IDF.idf_example_test(env_tag="Example_WIFI")
def test_examples_protocol_native_ota_example_chunked(env, extra_data):
"""
This is a positive test case, which downloads complete binary file multiple number of times.
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("native_ota_example", "examples/system/ota/native_ota_example")
# File to be downloaded. This file is generated after compilation
bin_name = "native_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)
IDF.log_performance("native_ota_bin_size", "{}KB".format(bin_size // 1024))
IDF.check_performance("native_ota_bin_size", bin_size // 1024)
# start test
host_ip = get_my_ip()
chunked_server = start_chunked_server(dut1.app.binary_path, 8070)
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')
dut1.expect("Connect to Wifi ! Start to Connect to Server....", timeout=30)
print("writing to device: {}".format("https://" + host_ip + ":8070/" + bin_name))
dut1.write("https://" + host_ip + ":8070/" + bin_name)
dut1.expect("Loaded app from partition at offset", timeout=60)
dut1.expect("Connect to Wifi ! Start to Connect to Server....", 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"))
if __name__ == '__main__': if __name__ == '__main__':
test_examples_protocol_native_ota_example() test_examples_protocol_native_ota_example()
test_examples_protocol_native_ota_example_chunked()
test_examples_protocol_native_ota_example_truncated_bin() test_examples_protocol_native_ota_example_truncated_bin()
test_examples_protocol_native_ota_example_truncated_header() test_examples_protocol_native_ota_example_truncated_header()
test_examples_protocol_native_ota_example_random() test_examples_protocol_native_ota_example_random()