2021-01-25 21:49:01 -05:00
|
|
|
import http.server
|
2019-12-09 09:23:05 -05:00
|
|
|
import os
|
2021-01-25 21:49:01 -05:00
|
|
|
import random
|
|
|
|
import re
|
2019-12-09 09:23:05 -05:00
|
|
|
import socket
|
|
|
|
import ssl
|
2021-01-25 21:49:01 -05:00
|
|
|
import struct
|
|
|
|
import subprocess
|
|
|
|
from threading import Thread
|
2019-12-09 09:23:05 -05:00
|
|
|
|
|
|
|
import ttfw_idf
|
2021-02-02 02:56:27 -05:00
|
|
|
from RangeHTTPServer import RangeRequestHandler
|
|
|
|
from tiny_test_fw import DUT, Utility
|
2019-12-09 09:23:05 -05:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
server_cert = '-----BEGIN CERTIFICATE-----\n' \
|
2021-07-12 08:42:06 -04:00
|
|
|
'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'\
|
2021-01-25 21:49:01 -05:00
|
|
|
'-----END CERTIFICATE-----\n'
|
|
|
|
|
|
|
|
server_key = '-----BEGIN PRIVATE KEY-----\n'\
|
2021-07-12 08:42:06 -04:00
|
|
|
'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'\
|
2021-01-25 21:49:01 -05:00
|
|
|
'-----END PRIVATE KEY-----\n'
|
2019-12-09 09:23:05 -05:00
|
|
|
|
|
|
|
|
|
|
|
def get_my_ip():
|
|
|
|
s1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
2021-01-25 21:49:01 -05:00
|
|
|
s1.connect(('8.8.8.8', 80))
|
2019-12-09 09:23:05 -05:00
|
|
|
my_ip = s1.getsockname()[0]
|
|
|
|
s1.close()
|
|
|
|
return my_ip
|
|
|
|
|
|
|
|
|
2020-02-13 03:13:58 -05:00
|
|
|
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):
|
2021-01-25 21:49:01 -05:00
|
|
|
with open(server_file, 'w+') as file:
|
2020-02-13 03:13:58 -05:00
|
|
|
file.write(file_data)
|
2019-12-09 09:23:05 -05:00
|
|
|
|
2020-02-13 03:13:58 -05:00
|
|
|
|
|
|
|
def get_ca_cert(ota_image_dir):
|
|
|
|
os.chdir(ota_image_dir)
|
2021-01-25 21:49:01 -05:00
|
|
|
server_file = os.path.join(ota_image_dir, 'server_cert.pem')
|
2020-02-13 03:13:58 -05:00
|
|
|
create_file(server_file, server_cert)
|
2019-12-09 09:23:05 -05:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
key_file = os.path.join(ota_image_dir, 'server_key.pem')
|
2020-02-13 03:13:58 -05:00
|
|
|
create_file(key_file, server_key)
|
|
|
|
return server_file, key_file
|
2019-12-09 09:23:05 -05:00
|
|
|
|
2020-02-13 03:13:58 -05:00
|
|
|
|
2020-04-28 11:23:19 -04:00
|
|
|
def https_request_handler():
|
|
|
|
"""
|
|
|
|
Returns a request handler class that handles broken pipe exception
|
|
|
|
"""
|
2021-02-02 02:56:27 -05:00
|
|
|
class RequestHandler(RangeRequestHandler):
|
2020-04-28 11:23:19 -04:00
|
|
|
def finish(self):
|
|
|
|
try:
|
|
|
|
if not self.wfile.closed:
|
|
|
|
self.wfile.flush()
|
|
|
|
self.wfile.close()
|
|
|
|
except socket.error:
|
|
|
|
pass
|
|
|
|
self.rfile.close()
|
|
|
|
|
|
|
|
def handle(self):
|
|
|
|
try:
|
2021-02-02 02:56:27 -05:00
|
|
|
RangeRequestHandler.handle(self)
|
2020-04-28 11:23:19 -04:00
|
|
|
except socket.error:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return RequestHandler
|
|
|
|
|
|
|
|
|
2020-02-13 03:13:58 -05:00
|
|
|
def start_https_server(ota_image_dir, server_ip, server_port):
|
|
|
|
server_file, key_file = get_ca_cert(ota_image_dir)
|
2020-04-28 11:23:19 -04:00
|
|
|
requestHandler = https_request_handler()
|
2020-05-14 02:39:29 -04:00
|
|
|
httpd = http.server.HTTPServer((server_ip, server_port), requestHandler)
|
2019-12-09 09:23:05 -05:00
|
|
|
|
|
|
|
httpd.socket = ssl.wrap_socket(httpd.socket,
|
|
|
|
keyfile=key_file,
|
|
|
|
certfile=server_file, server_side=True)
|
|
|
|
httpd.serve_forever()
|
|
|
|
|
|
|
|
|
2020-01-27 07:03:36 -05:00
|
|
|
def start_chunked_server(ota_image_dir, server_port):
|
2020-02-13 03:13:58 -05:00
|
|
|
server_file, key_file = get_ca_cert(ota_image_dir)
|
2021-01-25 21:49:01 -05:00
|
|
|
chunked_server = subprocess.Popen(['openssl', 's_server', '-WWW', '-key', key_file, '-cert', server_file, '-port', str(server_port)])
|
2020-02-13 03:13:58 -05:00
|
|
|
return chunked_server
|
|
|
|
|
|
|
|
|
|
|
|
def redirect_handler_factory(url):
|
|
|
|
"""
|
|
|
|
Returns a request handler class that redirects to supplied `url`
|
|
|
|
"""
|
2020-05-14 02:39:29 -04:00
|
|
|
class RedirectHandler(http.server.SimpleHTTPRequestHandler):
|
2020-02-13 03:13:58 -05:00
|
|
|
def do_GET(self):
|
2021-01-25 21:49:01 -05:00
|
|
|
print('Sending resp, URL: ' + url)
|
2020-02-13 03:13:58 -05:00
|
|
|
self.send_response(301)
|
|
|
|
self.send_header('Location', url)
|
|
|
|
self.end_headers()
|
|
|
|
|
2020-04-28 11:23:19 -04:00
|
|
|
def handle(self):
|
|
|
|
try:
|
2020-05-14 02:39:29 -04:00
|
|
|
http.server.BaseHTTPRequestHandler.handle(self)
|
2020-04-28 11:23:19 -04:00
|
|
|
except socket.error:
|
|
|
|
pass
|
|
|
|
|
2020-02-13 03:13:58 -05:00
|
|
|
return RedirectHandler
|
|
|
|
|
|
|
|
|
|
|
|
def start_redirect_server(ota_image_dir, server_ip, server_port, redirection_port):
|
2020-01-27 07:03:36 -05:00
|
|
|
os.chdir(ota_image_dir)
|
2020-02-13 03:13:58 -05:00
|
|
|
server_file, key_file = get_ca_cert(ota_image_dir)
|
2021-01-25 21:49:01 -05:00
|
|
|
redirectHandler = redirect_handler_factory('https://' + server_ip + ':' + str(redirection_port) + '/advanced_https_ota.bin')
|
2020-01-27 07:03:36 -05:00
|
|
|
|
2020-05-14 02:39:29 -04:00
|
|
|
httpd = http.server.HTTPServer((server_ip, server_port), redirectHandler)
|
2020-01-27 07:03:36 -05:00
|
|
|
|
2020-02-13 03:13:58 -05:00
|
|
|
httpd.socket = ssl.wrap_socket(httpd.socket,
|
|
|
|
keyfile=key_file,
|
|
|
|
certfile=server_file, server_side=True)
|
|
|
|
httpd.serve_forever()
|
2020-01-27 07:03:36 -05:00
|
|
|
|
|
|
|
|
2021-03-25 05:50:30 -04:00
|
|
|
@ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
|
2019-12-09 09:23:05 -05:00
|
|
|
def test_examples_protocol_advanced_https_ota_example(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
|
|
|
|
"""
|
2021-01-25 21:49:01 -05:00
|
|
|
dut1 = env.get_dut('advanced_https_ota_example', 'examples/system/ota/advanced_https_ota', dut_class=ttfw_idf.ESP32DUT)
|
2019-12-09 09:23:05 -05:00
|
|
|
# Number of iterations to validate OTA
|
|
|
|
iterations = 3
|
2020-02-13 03:13:58 -05:00
|
|
|
server_port = 8001
|
2019-12-09 09:23:05 -05:00
|
|
|
# File to be downloaded. This file is generated after compilation
|
2021-01-25 21:49:01 -05:00
|
|
|
bin_name = 'advanced_https_ota.bin'
|
2019-12-09 09:23:05 -05:00
|
|
|
# check and log bin size
|
|
|
|
binary_file = os.path.join(dut1.app.binary_path, bin_name)
|
|
|
|
bin_size = os.path.getsize(binary_file)
|
2021-01-25 21:49:01 -05:00
|
|
|
ttfw_idf.log_performance('advanced_https_ota_bin_size', '{}KB'.format(bin_size // 1024))
|
2019-12-09 09:23:05 -05:00
|
|
|
# start test
|
|
|
|
host_ip = get_my_ip()
|
2020-02-13 03:13:58 -05:00
|
|
|
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()
|
2019-12-09 09:23:05 -05:00
|
|
|
dut1.start_app()
|
|
|
|
for i in range(iterations):
|
2021-01-25 21:49:01 -05:00
|
|
|
dut1.expect('Loaded app from partition at offset', timeout=30)
|
2019-12-09 09:23:05 -05:00
|
|
|
try:
|
2021-01-25 21:49:01 -05:00
|
|
|
ip_address = dut1.expect(re.compile(r' sta ip: ([^,]+),'), timeout=30)
|
|
|
|
print('Connected to AP with IP: {}'.format(ip_address))
|
2019-12-09 09:23:05 -05:00
|
|
|
except DUT.ExpectTimeout:
|
|
|
|
raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
|
|
|
|
thread1.close()
|
2021-01-25 21:49:01 -05:00
|
|
|
dut1.expect('Starting Advanced OTA example', timeout=30)
|
2019-12-09 09:23:05 -05:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
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)
|
2019-12-09 09:23:05 -05:00
|
|
|
dut1.reset()
|
|
|
|
|
|
|
|
|
2021-03-25 05:50:30 -04:00
|
|
|
@ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
|
2019-12-09 09:23:05 -05:00
|
|
|
def test_examples_protocol_advanced_https_ota_example_truncated_bin(env, extra_data):
|
|
|
|
"""
|
|
|
|
Working of OTA if binary file is truncated is validated in this test case.
|
|
|
|
Application should return with error message in this case.
|
|
|
|
steps: |
|
|
|
|
1. join AP
|
|
|
|
2. Generate truncated binary file
|
|
|
|
3. Fetch OTA image over HTTPS
|
|
|
|
4. Check working of code if bin is truncated
|
|
|
|
"""
|
2021-01-25 21:49:01 -05:00
|
|
|
dut1 = env.get_dut('advanced_https_ota_example', 'examples/system/ota/advanced_https_ota', dut_class=ttfw_idf.ESP32DUT)
|
2020-02-13 03:13:58 -05:00
|
|
|
server_port = 8001
|
2019-12-09 09:23:05 -05:00
|
|
|
# Original binary file generated after compilation
|
2021-01-25 21:49:01 -05:00
|
|
|
bin_name = 'advanced_https_ota.bin'
|
2019-12-09 09:23:05 -05:00
|
|
|
# Truncated binary file to be generated from original binary file
|
2021-01-25 21:49:01 -05:00
|
|
|
truncated_bin_name = 'truncated.bin'
|
2019-12-09 09:23:05 -05:00
|
|
|
# Size of truncated file to be grnerated. This value can range from 288 bytes (Image header size) to size of original binary file
|
|
|
|
# truncated_bin_size is set to 64000 to reduce consumed by the test case
|
|
|
|
truncated_bin_size = 64000
|
|
|
|
# check and log bin size
|
|
|
|
binary_file = os.path.join(dut1.app.binary_path, bin_name)
|
2021-01-25 21:49:01 -05:00
|
|
|
f = open(binary_file, 'rb+')
|
|
|
|
fo = open(os.path.join(dut1.app.binary_path, truncated_bin_name), 'wb+')
|
2019-12-09 09:23:05 -05:00
|
|
|
fo.write(f.read(truncated_bin_size))
|
|
|
|
fo.close()
|
|
|
|
f.close()
|
|
|
|
binary_file = os.path.join(dut1.app.binary_path, truncated_bin_name)
|
|
|
|
bin_size = os.path.getsize(binary_file)
|
2021-01-25 21:49:01 -05:00
|
|
|
ttfw_idf.log_performance('advanced_https_ota_bin_size', '{}KB'.format(bin_size // 1024))
|
2019-12-09 09:23:05 -05:00
|
|
|
# start test
|
|
|
|
host_ip = get_my_ip()
|
2020-02-13 03:13:58 -05:00
|
|
|
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()
|
2019-12-09 09:23:05 -05:00
|
|
|
dut1.start_app()
|
2021-01-25 21:49:01 -05:00
|
|
|
dut1.expect('Loaded app from partition at offset', timeout=30)
|
2019-12-09 09:23:05 -05:00
|
|
|
try:
|
2021-01-25 21:49:01 -05:00
|
|
|
ip_address = dut1.expect(re.compile(r' sta ip: ([^,]+),'), timeout=30)
|
|
|
|
print('Connected to AP with IP: {}'.format(ip_address))
|
2019-12-09 09:23:05 -05:00
|
|
|
except DUT.ExpectTimeout:
|
|
|
|
raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
|
2021-01-25 21:49:01 -05:00
|
|
|
dut1.expect('Starting Advanced OTA example', timeout=30)
|
2019-12-09 09:23:05 -05:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
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)
|
2020-01-27 07:03:36 -05:00
|
|
|
os.remove(binary_file)
|
2019-12-09 09:23:05 -05:00
|
|
|
|
|
|
|
|
2021-03-25 05:50:30 -04:00
|
|
|
@ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
|
2019-12-09 09:23:05 -05:00
|
|
|
def test_examples_protocol_advanced_https_ota_example_truncated_header(env, extra_data):
|
|
|
|
"""
|
|
|
|
Working of OTA if headers of binary file are truncated is vaildated in this test case.
|
|
|
|
Application should return with error message in this case.
|
|
|
|
steps: |
|
|
|
|
1. join AP
|
|
|
|
2. Generate binary file with truncated headers
|
|
|
|
3. Fetch OTA image over HTTPS
|
|
|
|
4. Check working of code if headers are not sent completely
|
|
|
|
"""
|
2021-01-25 21:49:01 -05:00
|
|
|
dut1 = env.get_dut('advanced_https_ota_example', 'examples/system/ota/advanced_https_ota', dut_class=ttfw_idf.ESP32DUT)
|
2020-02-13 03:13:58 -05:00
|
|
|
server_port = 8001
|
2019-12-09 09:23:05 -05:00
|
|
|
# Original binary file generated after compilation
|
2021-01-25 21:49:01 -05:00
|
|
|
bin_name = 'advanced_https_ota.bin'
|
2019-12-09 09:23:05 -05:00
|
|
|
# Truncated binary file to be generated from original binary file
|
2021-01-25 21:49:01 -05:00
|
|
|
truncated_bin_name = 'truncated_header.bin'
|
2019-12-09 09:23:05 -05:00
|
|
|
# Size of truncated file to be grnerated. This value should be less than 288 bytes (Image header size)
|
|
|
|
truncated_bin_size = 180
|
|
|
|
# check and log bin size
|
|
|
|
binary_file = os.path.join(dut1.app.binary_path, bin_name)
|
2021-01-25 21:49:01 -05:00
|
|
|
f = open(binary_file, 'rb+')
|
|
|
|
fo = open(os.path.join(dut1.app.binary_path, truncated_bin_name), 'wb+')
|
2019-12-09 09:23:05 -05:00
|
|
|
fo.write(f.read(truncated_bin_size))
|
|
|
|
fo.close()
|
|
|
|
f.close()
|
|
|
|
binary_file = os.path.join(dut1.app.binary_path, truncated_bin_name)
|
|
|
|
bin_size = os.path.getsize(binary_file)
|
2021-01-25 21:49:01 -05:00
|
|
|
ttfw_idf.log_performance('advanced_https_ota_bin_size', '{}KB'.format(bin_size // 1024))
|
2019-12-09 09:23:05 -05:00
|
|
|
# start test
|
|
|
|
host_ip = get_my_ip()
|
2020-02-13 03:13:58 -05:00
|
|
|
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()
|
2019-12-09 09:23:05 -05:00
|
|
|
dut1.start_app()
|
2021-01-25 21:49:01 -05:00
|
|
|
dut1.expect('Loaded app from partition at offset', timeout=30)
|
2019-12-09 09:23:05 -05:00
|
|
|
try:
|
2021-01-25 21:49:01 -05:00
|
|
|
ip_address = dut1.expect(re.compile(r' sta ip: ([^,]+),'), timeout=30)
|
|
|
|
print('Connected to AP with IP: {}'.format(ip_address))
|
2019-12-09 09:23:05 -05:00
|
|
|
except DUT.ExpectTimeout:
|
|
|
|
raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
|
2021-01-25 21:49:01 -05:00
|
|
|
dut1.expect('Starting Advanced OTA example', timeout=30)
|
2019-12-09 09:23:05 -05:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
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)
|
2020-01-27 07:03:36 -05:00
|
|
|
os.remove(binary_file)
|
2019-12-09 09:23:05 -05:00
|
|
|
|
|
|
|
|
2021-03-25 05:50:30 -04:00
|
|
|
@ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
|
2019-12-09 09:23:05 -05:00
|
|
|
def test_examples_protocol_advanced_https_ota_example_random(env, extra_data):
|
|
|
|
"""
|
|
|
|
Working of OTA if random data is added in binary file are validated in this test case.
|
|
|
|
Magic byte verification should fail in this case.
|
|
|
|
steps: |
|
|
|
|
1. join AP
|
|
|
|
2. Generate random binary image
|
|
|
|
3. Fetch OTA image over HTTPS
|
|
|
|
4. Check working of code for random binary file
|
|
|
|
"""
|
2021-01-25 21:49:01 -05:00
|
|
|
dut1 = env.get_dut('advanced_https_ota_example', 'examples/system/ota/advanced_https_ota', dut_class=ttfw_idf.ESP32DUT)
|
2020-02-13 03:13:58 -05:00
|
|
|
server_port = 8001
|
2019-12-09 09:23:05 -05:00
|
|
|
# Random binary file to be generated
|
2021-01-25 21:49:01 -05:00
|
|
|
random_bin_name = 'random.bin'
|
2019-12-09 09:23:05 -05:00
|
|
|
# Size of random binary file. 32000 is choosen, to reduce the time required to run the test-case
|
|
|
|
random_bin_size = 32000
|
|
|
|
# check and log bin size
|
|
|
|
binary_file = os.path.join(dut1.app.binary_path, random_bin_name)
|
2021-01-25 21:49:01 -05:00
|
|
|
fo = open(binary_file, 'wb+')
|
2019-12-09 09:23:05 -05:00
|
|
|
# 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.
|
2021-01-25 21:49:01 -05:00
|
|
|
fo.write(struct.pack('B', 0))
|
2019-12-09 09:23:05 -05:00
|
|
|
for i in range(random_bin_size - 1):
|
2021-01-25 21:49:01 -05:00
|
|
|
fo.write(struct.pack('B', random.randrange(0,255,1)))
|
2019-12-09 09:23:05 -05:00
|
|
|
fo.close()
|
|
|
|
bin_size = os.path.getsize(binary_file)
|
2021-01-25 21:49:01 -05:00
|
|
|
ttfw_idf.log_performance('advanced_https_ota_bin_size', '{}KB'.format(bin_size // 1024))
|
2019-12-09 09:23:05 -05:00
|
|
|
# start test
|
|
|
|
host_ip = get_my_ip()
|
2020-02-13 03:13:58 -05:00
|
|
|
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()
|
2019-12-09 09:23:05 -05:00
|
|
|
dut1.start_app()
|
2021-01-25 21:49:01 -05:00
|
|
|
dut1.expect('Loaded app from partition at offset', timeout=30)
|
2019-12-09 09:23:05 -05:00
|
|
|
try:
|
2021-01-25 21:49:01 -05:00
|
|
|
ip_address = dut1.expect(re.compile(r' sta ip: ([^,]+),'), timeout=30)
|
|
|
|
print('Connected to AP with IP: {}'.format(ip_address))
|
2019-12-09 09:23:05 -05:00
|
|
|
except DUT.ExpectTimeout:
|
|
|
|
raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
|
2021-01-25 21:49:01 -05:00
|
|
|
dut1.expect('Starting Advanced OTA example', timeout=30)
|
2019-12-09 09:23:05 -05:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
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)
|
2020-01-27 07:03:36 -05:00
|
|
|
os.remove(binary_file)
|
|
|
|
|
|
|
|
|
2021-03-25 05:50:30 -04:00
|
|
|
@ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
|
2020-01-27 07:03:36 -05:00
|
|
|
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
|
|
|
|
"""
|
2021-01-25 21:49:01 -05:00
|
|
|
dut1 = env.get_dut('advanced_https_ota_example', 'examples/system/ota/advanced_https_ota', dut_class=ttfw_idf.ESP32DUT)
|
2020-01-27 07:03:36 -05:00
|
|
|
# File to be downloaded. This file is generated after compilation
|
2021-01-25 21:49:01 -05:00
|
|
|
bin_name = 'advanced_https_ota.bin'
|
2020-01-27 07:03:36 -05:00
|
|
|
# check and log bin size
|
|
|
|
binary_file = os.path.join(dut1.app.binary_path, bin_name)
|
|
|
|
bin_size = os.path.getsize(binary_file)
|
2021-01-25 21:49:01 -05:00
|
|
|
ttfw_idf.log_performance('advanced_https_ota_bin_size', '{}KB'.format(bin_size // 1024))
|
2020-01-27 07:03:36 -05:00
|
|
|
# start test
|
|
|
|
host_ip = get_my_ip()
|
|
|
|
chunked_server = start_chunked_server(dut1.app.binary_path, 8070)
|
|
|
|
dut1.start_app()
|
2021-01-25 21:49:01 -05:00
|
|
|
dut1.expect('Loaded app from partition at offset', timeout=30)
|
2020-01-27 07:03:36 -05:00
|
|
|
try:
|
2021-01-25 21:49:01 -05:00
|
|
|
ip_address = dut1.expect(re.compile(r' sta ip: ([^,]+),'), timeout=30)
|
|
|
|
print('Connected to AP with IP: {}'.format(ip_address))
|
2020-01-27 07:03:36 -05:00
|
|
|
except DUT.ExpectTimeout:
|
|
|
|
raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
|
2021-01-25 21:49:01 -05:00
|
|
|
dut1.expect('Starting Advanced OTA example', timeout=30)
|
2020-01-27 07:03:36 -05:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
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('Starting Advanced OTA example', timeout=30)
|
2020-01-27 07:03:36 -05:00
|
|
|
chunked_server.kill()
|
2021-01-25 21:49:01 -05:00
|
|
|
os.remove(os.path.join(dut1.app.binary_path, 'server_cert.pem'))
|
|
|
|
os.remove(os.path.join(dut1.app.binary_path, 'server_key.pem'))
|
2019-12-09 09:23:05 -05:00
|
|
|
|
|
|
|
|
2021-03-25 05:50:30 -04:00
|
|
|
@ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
|
2020-02-13 03:13:58 -05:00
|
|
|
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
|
|
|
|
"""
|
2021-01-25 21:49:01 -05:00
|
|
|
dut1 = env.get_dut('advanced_https_ota_example', 'examples/system/ota/advanced_https_ota', dut_class=ttfw_idf.ESP32DUT)
|
2020-02-13 03:13:58 -05:00
|
|
|
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
|
2021-01-25 21:49:01 -05:00
|
|
|
bin_name = 'advanced_https_ota.bin'
|
2020-02-13 03:13:58 -05:00
|
|
|
# check and log bin size
|
|
|
|
binary_file = os.path.join(dut1.app.binary_path, bin_name)
|
|
|
|
bin_size = os.path.getsize(binary_file)
|
2021-01-25 21:49:01 -05:00
|
|
|
ttfw_idf.log_performance('advanced_https_ota_bin_size', '{}KB'.format(bin_size // 1024))
|
2020-02-13 03:13:58 -05:00
|
|
|
# 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()
|
2021-01-25 21:49:01 -05:00
|
|
|
dut1.expect('Loaded app from partition at offset', timeout=30)
|
2020-02-13 03:13:58 -05:00
|
|
|
try:
|
2021-01-25 21:49:01 -05:00
|
|
|
ip_address = dut1.expect(re.compile(r' sta ip: ([^,]+),'), timeout=30)
|
|
|
|
print('Connected to AP with IP: {}'.format(ip_address))
|
2020-02-13 03:13:58 -05:00
|
|
|
except DUT.ExpectTimeout:
|
|
|
|
raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
|
|
|
|
thread1.close()
|
|
|
|
thread2.close()
|
2021-01-25 21:49:01 -05:00
|
|
|
dut1.expect('Starting Advanced OTA example', timeout=30)
|
2020-02-13 03:13:58 -05:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
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)
|
2020-02-13 03:13:58 -05:00
|
|
|
dut1.reset()
|
|
|
|
|
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
@ttfw_idf.idf_example_test(env_tag='Example_8Mflash_Ethernet')
|
2020-10-23 12:22:51 -04:00
|
|
|
def test_examples_protocol_advanced_https_ota_example_anti_rollback(env, extra_data):
|
|
|
|
"""
|
|
|
|
Working of OTA when anti_rollback is enabled and security version of new image is less than current one.
|
|
|
|
Application should return with error message in this case.
|
|
|
|
steps: |
|
|
|
|
1. join AP
|
|
|
|
2. Generate binary file with lower security version
|
|
|
|
3. Fetch OTA image over HTTPS
|
|
|
|
4. Check working of anti_rollback feature
|
|
|
|
"""
|
2021-01-25 21:49:01 -05:00
|
|
|
dut1 = env.get_dut('advanced_https_ota_example', 'examples/system/ota/advanced_https_ota', dut_class=ttfw_idf.ESP32DUT, app_config_name='anti_rollback')
|
2021-06-16 19:21:36 -04:00
|
|
|
Utility.console_log('Erasing the flash on the chip')
|
|
|
|
# erase the flash
|
|
|
|
dut1.erase_flash()
|
2020-10-23 12:22:51 -04:00
|
|
|
server_port = 8001
|
|
|
|
# Original binary file generated after compilation
|
2021-01-25 21:49:01 -05:00
|
|
|
bin_name = 'advanced_https_ota.bin'
|
2020-10-23 12:22:51 -04:00
|
|
|
# Modified firmware image to lower security version in its header. This is to enable negative test case
|
2021-01-25 21:49:01 -05:00
|
|
|
anti_rollback_bin_name = 'advanced_https_ota_lower_sec_version.bin'
|
2020-10-23 12:22:51 -04:00
|
|
|
# check and log bin size
|
|
|
|
binary_file = os.path.join(dut1.app.binary_path, bin_name)
|
|
|
|
file_size = os.path.getsize(binary_file)
|
2021-01-25 21:49:01 -05:00
|
|
|
f = open(binary_file, 'rb+')
|
|
|
|
fo = open(os.path.join(dut1.app.binary_path, anti_rollback_bin_name), 'wb+')
|
2020-10-23 12:22:51 -04:00
|
|
|
fo.write(f.read(file_size))
|
|
|
|
# Change security_version to 0 for negative test case
|
|
|
|
fo.seek(36)
|
|
|
|
fo.write(b'\x00')
|
|
|
|
fo.close()
|
|
|
|
f.close()
|
|
|
|
binary_file = os.path.join(dut1.app.binary_path, anti_rollback_bin_name)
|
|
|
|
bin_size = os.path.getsize(binary_file)
|
2021-01-25 21:49:01 -05:00
|
|
|
ttfw_idf.log_performance('advanced_https_ota_bin_size', '{}KB'.format(bin_size // 1024))
|
2020-10-23 12:22:51 -04:00
|
|
|
# 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()
|
|
|
|
# Positive Case
|
2021-01-25 21:49:01 -05:00
|
|
|
dut1.expect('Loaded app from partition at offset', timeout=30)
|
2020-10-23 12:22:51 -04:00
|
|
|
try:
|
2021-01-25 21:49:01 -05:00
|
|
|
ip_address = dut1.expect(re.compile(r' eth ip: ([^,]+),'), timeout=30)
|
|
|
|
print('Connected to AP with IP: {}'.format(ip_address))
|
2020-10-23 12:22:51 -04:00
|
|
|
except DUT.ExpectTimeout:
|
|
|
|
raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
|
2021-01-25 21:49:01 -05:00
|
|
|
dut1.expect('Starting Advanced OTA example', timeout=30)
|
2020-10-23 12:22:51 -04:00
|
|
|
|
|
|
|
# Use originally generated image with secure_version=1
|
2021-01-25 21:49:01 -05:00
|
|
|
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(re.compile(r' eth ip: ([^,]+),'), timeout=30)
|
|
|
|
dut1.expect('App is valid, rollback cancelled successfully', 30)
|
2020-10-23 12:22:51 -04:00
|
|
|
|
|
|
|
# Negative Case
|
2021-01-25 21:49:01 -05:00
|
|
|
dut1.expect('Starting Advanced OTA example', timeout=30)
|
2020-10-23 12:22:51 -04:00
|
|
|
# Use modified image with secure_version=0
|
2021-01-25 21:49:01 -05:00
|
|
|
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)
|
2020-10-23 12:22:51 -04:00
|
|
|
os.remove(anti_rollback_bin_name)
|
|
|
|
|
|
|
|
|
2021-03-25 05:50:30 -04:00
|
|
|
@ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
|
2021-02-02 02:56:27 -05:00
|
|
|
def test_examples_protocol_advanced_https_ota_example_partial_request(env, extra_data):
|
|
|
|
"""
|
|
|
|
This is a positive test case, to test OTA workflow with Range HTTP header.
|
|
|
|
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, app_config_name='partial_download')
|
|
|
|
server_port = 8001
|
|
|
|
# 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))
|
|
|
|
http_requests = int((bin_size / 50000) + 1)
|
|
|
|
# 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:
|
|
|
|
ip_address = dut1.expect(re.compile(r' sta ip: ([^,]+),'), timeout=30)
|
|
|
|
print('Connected to AP with IP: {}'.format(ip_address))
|
|
|
|
except DUT.ExpectTimeout:
|
|
|
|
Utility.console_log('ENV_TEST_FAILURE: Cannot connect to AP')
|
|
|
|
raise
|
|
|
|
dut1.expect('Starting Advanced OTA example', timeout=30)
|
|
|
|
|
|
|
|
print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + bin_name))
|
|
|
|
dut1.write('https://' + host_ip + ':' + str(server_port) + '/' + bin_name)
|
|
|
|
for _ in range(http_requests):
|
|
|
|
dut1.expect('Connection closed', timeout=60)
|
|
|
|
dut1.expect('Loaded app from partition at offset', timeout=60)
|
|
|
|
dut1.expect('Starting Advanced OTA example', timeout=30)
|
|
|
|
dut1.reset()
|
|
|
|
|
|
|
|
|
2021-03-02 05:30:49 -05:00
|
|
|
@ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
|
|
|
|
def test_examples_protocol_advanced_https_ota_example_nimble_gatts(env, extra_data):
|
|
|
|
"""
|
|
|
|
Run an OTA image update while a BLE GATT Server is running in background. This GATT server will be using NimBLE Host stack.
|
|
|
|
steps: |
|
|
|
|
1. join AP
|
|
|
|
2. Run BLE advertise and then GATT server.
|
|
|
|
3. Fetch OTA image over HTTPS
|
|
|
|
4. 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, app_config_name='nimble')
|
|
|
|
server_port = 8001
|
|
|
|
# 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))
|
|
|
|
# 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:
|
|
|
|
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('Starting Advanced OTA example', timeout=30)
|
|
|
|
print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + bin_name))
|
|
|
|
dut1.expect('GAP procedure initiated: advertise', timeout=30)
|
|
|
|
print('Started GAP advertising.')
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
@ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
|
|
|
|
def test_examples_protocol_advanced_https_ota_example_bluedroid_gatts(env, extra_data):
|
|
|
|
"""
|
|
|
|
Run an OTA image update while a BLE GATT Server is running in background. This GATT server will be using Bluedroid Host stack.
|
|
|
|
steps: |
|
|
|
|
1. join AP
|
|
|
|
2. Run BLE advertise and then GATT server.
|
|
|
|
3. Fetch OTA image over HTTPS
|
|
|
|
4. 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, app_config_name='bluedroid')
|
|
|
|
server_port = 8001
|
|
|
|
# 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))
|
|
|
|
# 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:
|
|
|
|
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('Starting Advanced OTA example', timeout=30)
|
|
|
|
print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + bin_name))
|
|
|
|
dut1.expect('Started advertising.', timeout=30)
|
|
|
|
print('Started GAP advertising.')
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
2021-04-19 03:17:06 -04:00
|
|
|
@ttfw_idf.idf_example_test(env_tag='Example_WIFI_OTA')
|
|
|
|
def test_examples_protocol_advanced_https_ota_example_openssl_aligned_bin(env, extra_data):
|
|
|
|
"""
|
|
|
|
This is a test case for esp_http_client_read with binary size multiple of 289 bytes
|
|
|
|
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)
|
|
|
|
# Original binary file generated after compilation
|
|
|
|
bin_name = 'advanced_https_ota.bin'
|
|
|
|
# Binary file aligned to DEFAULT_OTA_BUF_SIZE(289 bytes) boundary
|
|
|
|
aligned_bin_name = 'aligned.bin'
|
|
|
|
# check and log bin size
|
|
|
|
binary_file = os.path.join(dut1.app.binary_path, bin_name)
|
|
|
|
# Original binary size
|
|
|
|
bin_size = os.path.getsize(binary_file)
|
|
|
|
# Dummy data required to align binary size to 289 bytes boundary
|
|
|
|
dummy_data_size = 289 - (bin_size % 289)
|
|
|
|
f = open(binary_file, 'rb+')
|
|
|
|
fo = open(os.path.join(dut1.app.binary_path, aligned_bin_name), 'wb+')
|
|
|
|
fo.write(f.read(bin_size))
|
|
|
|
for _ in range(dummy_data_size):
|
|
|
|
fo.write(struct.pack('B', random.randrange(0,255,1)))
|
|
|
|
fo.close()
|
|
|
|
f.close()
|
|
|
|
# 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('Starting Advanced OTA example', timeout=30)
|
|
|
|
|
|
|
|
print('writing to device: {}'.format('https://' + host_ip + ':8070/' + aligned_bin_name))
|
|
|
|
dut1.write('https://' + host_ip + ':8070/' + aligned_bin_name)
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2019-12-09 09:23:05 -05:00
|
|
|
if __name__ == '__main__':
|
|
|
|
test_examples_protocol_advanced_https_ota_example()
|
2020-01-27 07:03:36 -05:00
|
|
|
test_examples_protocol_advanced_https_ota_example_chunked()
|
2020-02-13 03:13:58 -05:00
|
|
|
test_examples_protocol_advanced_https_ota_example_redirect_url()
|
2019-12-09 09:23:05 -05:00
|
|
|
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()
|
2020-10-23 12:22:51 -04:00
|
|
|
test_examples_protocol_advanced_https_ota_example_anti_rollback()
|
2021-02-02 02:56:27 -05:00
|
|
|
test_examples_protocol_advanced_https_ota_example_partial_request()
|
2021-03-02 05:30:49 -05:00
|
|
|
test_examples_protocol_advanced_https_ota_example_nimble_gatts()
|
|
|
|
test_examples_protocol_advanced_https_ota_example_bluedroid_gatts()
|
2021-04-19 03:17:06 -04:00
|
|
|
test_examples_protocol_advanced_https_ota_example_openssl_aligned_bin()
|