Skip to content

Commit 6a92c14

Browse files
committed
Merge branch 'fix/pytest_server_start_command_failed_v5.3' into 'release/v5.3'
fix: Refactored script for initiating Python-based HTTPS server (v5.3) See merge request espressif/esp-idf!30667
2 parents c91bdda + 855d1eb commit 6a92c14

File tree

5 files changed

+55
-48
lines changed

5 files changed

+55
-48
lines changed

examples/protocols/https_request/pytest_https_request.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
1+
# SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
22
# SPDX-License-Identifier: Unlicense OR CC0-1.0
33
import http.server
44
import logging
@@ -50,8 +50,10 @@ def start_https_server(server_file: str, key_file: str, server_ip: str, server_p
5050
requestHandler = https_request_handler()
5151
httpd = http.server.HTTPServer((server_ip, server_port), requestHandler)
5252

53-
httpd.socket = ssl.wrap_socket(httpd.socket, keyfile=key_file,
54-
certfile=server_file, server_side=True)
53+
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
54+
ssl_context.load_cert_chain(certfile=server_file, keyfile=key_file)
55+
56+
httpd.socket = ssl_context.wrap_socket(httpd.socket, server_side=True)
5557
httpd.serve_forever()
5658

5759

examples/system/ota/advanced_https_ota/pytest_advanced_ota.py

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ def start_https_server(ota_image_dir: str, server_ip: str, server_port: int) ->
5050
requestHandler = https_request_handler()
5151
httpd = http.server.HTTPServer((server_ip, server_port), requestHandler)
5252

53-
httpd.socket = ssl.wrap_socket(httpd.socket,
54-
keyfile=key_file,
55-
certfile=server_file, server_side=True)
53+
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
54+
ssl_context.load_cert_chain(certfile=server_file, keyfile=key_file)
55+
56+
httpd.socket = ssl_context.wrap_socket(httpd.socket, server_side=True)
5657
httpd.serve_forever()
5758

5859

@@ -88,9 +89,10 @@ def start_redirect_server(ota_image_dir: str, server_ip: str, server_port: int,
8889

8990
httpd = http.server.HTTPServer((server_ip, server_port), redirectHandler)
9091

91-
httpd.socket = ssl.wrap_socket(httpd.socket,
92-
keyfile=key_file,
93-
certfile=server_file, server_side=True)
92+
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
93+
ssl_context.load_cert_chain(certfile=server_file, keyfile=key_file)
94+
95+
httpd.socket = ssl_context.wrap_socket(httpd.socket, server_side=True)
9496
httpd.serve_forever()
9597

9698

@@ -154,8 +156,8 @@ def test_examples_protocol_advanced_https_ota_example_truncated_bin(dut: Dut) ->
154156
truncated_bin_size = 64000
155157
binary_file = os.path.join(dut.app.binary_path, bin_name)
156158
with open(binary_file, 'rb+') as f:
157-
with open(os.path.join(dut.app.binary_path, truncated_bin_name), 'wb+') as fo:
158-
fo.write(f.read(truncated_bin_size))
159+
with open(os.path.join(dut.app.binary_path, truncated_bin_name), 'wb+') as output_file:
160+
output_file.write(f.read(truncated_bin_size))
159161
binary_file = os.path.join(dut.app.binary_path, truncated_bin_name)
160162
# Start server
161163
thread1 = multiprocessing.Process(target=start_https_server, args=(dut.app.binary_path, '0.0.0.0', server_port))
@@ -187,7 +189,7 @@ def test_examples_protocol_advanced_https_ota_example_truncated_bin(dut: Dut) ->
187189
@pytest.mark.ethernet_ota
188190
def test_examples_protocol_advanced_https_ota_example_truncated_header(dut: Dut) -> None:
189191
"""
190-
Working of OTA if headers of binary file are truncated is vaildated in this test case.
192+
Working of OTA if headers of binary file are truncated is validated in this test case.
191193
Application should return with error message in this case.
192194
steps: |
193195
1. join AP/Ethernet
@@ -205,8 +207,8 @@ def test_examples_protocol_advanced_https_ota_example_truncated_header(dut: Dut)
205207
# check and log bin size
206208
binary_file = os.path.join(dut.app.binary_path, bin_name)
207209
with open(binary_file, 'rb+') as f:
208-
with open(os.path.join(dut.app.binary_path, truncated_bin_name), 'wb+') as fo:
209-
fo.write(f.read(truncated_bin_size))
210+
with open(os.path.join(dut.app.binary_path, truncated_bin_name), 'wb+') as output_file:
211+
output_file.write(f.read(truncated_bin_size))
210212
binary_file = os.path.join(dut.app.binary_path, truncated_bin_name)
211213
# Start server
212214
thread1 = multiprocessing.Process(target=start_https_server, args=(dut.app.binary_path, '0.0.0.0', server_port))
@@ -249,16 +251,16 @@ def test_examples_protocol_advanced_https_ota_example_random(dut: Dut) -> None:
249251
server_port = 8001
250252
# Random binary file to be generated
251253
random_bin_name = 'random.bin'
252-
# Size of random binary file. 32000 is choosen, to reduce the time required to run the test-case
254+
# Size of random binary file. 32000 is chosen, to reduce the time required to run the test-case
253255
random_bin_size = 32000
254256
# check and log bin size
255257
binary_file = os.path.join(dut.app.binary_path, random_bin_name)
256-
with open(binary_file, 'wb+') as fo:
258+
with open(binary_file, 'wb+') as output_file:
257259
# First byte of binary file is always set to zero. If first byte is generated randomly,
258260
# in some cases it may generate 0xE9 which will result in failure of testcase.
259-
fo.write(struct.pack('B', 0))
261+
output_file.write(struct.pack('B', 0))
260262
for i in range(random_bin_size - 1):
261-
fo.write(struct.pack('B', random.randrange(0,255,1)))
263+
output_file.write(struct.pack('B', random.randrange(0,255,1)))
262264
# Start server
263265
thread1 = multiprocessing.Process(target=start_https_server, args=(dut.app.binary_path, '0.0.0.0', server_port))
264266
thread1.daemon = True
@@ -302,16 +304,16 @@ def test_examples_protocol_advanced_https_ota_example_invalid_chip_id(dut: Dut)
302304
# Random binary file to be generated
303305
random_bin_name = 'random.bin'
304306
random_binary_file = os.path.join(dut.app.binary_path, random_bin_name)
305-
# Size of random binary file. 2000 is choosen, to reduce the time required to run the test-case
307+
# Size of random binary file. 2000 is chosen, to reduce the time required to run the test-case
306308
random_bin_size = 2000
307309

308310
binary_file = os.path.join(dut.app.binary_path, bin_name)
309311
with open(binary_file, 'rb+') as f:
310312
data = list(f.read(random_bin_size))
311313
# Changing Chip id
312314
data[13] = 0xfe
313-
with open(random_binary_file, 'wb+') as fo:
314-
fo.write(bytearray(data))
315+
with open(random_binary_file, 'wb+') as output_file:
316+
output_file.write(bytearray(data))
315317
# Start server
316318
thread1 = multiprocessing.Process(target=start_https_server, args=(dut.app.binary_path, '0.0.0.0', server_port))
317319
thread1.daemon = True
@@ -452,11 +454,11 @@ def test_examples_protocol_advanced_https_ota_example_anti_rollback(dut: Dut) ->
452454
binary_file = os.path.join(dut.app.binary_path, bin_name)
453455
file_size = os.path.getsize(binary_file)
454456
with open(binary_file, 'rb+') as f:
455-
with open(os.path.join(dut.app.binary_path, anti_rollback_bin_name), 'wb+') as fo:
456-
fo.write(f.read(file_size))
457+
with open(os.path.join(dut.app.binary_path, anti_rollback_bin_name), 'wb+') as output_file:
458+
output_file.write(f.read(file_size))
457459
# Change security_version to 0 for negative test case
458-
fo.seek(36)
459-
fo.write(b'\x00')
460+
output_file.seek(36)
461+
output_file.write(b'\x00')
460462
binary_file = os.path.join(dut.app.binary_path, anti_rollback_bin_name)
461463
# Start server
462464
thread1 = multiprocessing.Process(target=start_https_server, args=(dut.app.binary_path, '0.0.0.0', server_port))
@@ -666,10 +668,10 @@ def test_examples_protocol_advanced_https_ota_example_openssl_aligned_bin(dut: D
666668
# Dummy data required to align binary size to 289 bytes boundary
667669
dummy_data_size = 289 - (bin_size % 289)
668670
with open(binary_file, 'rb+') as f:
669-
with open(os.path.join(dut.app.binary_path, aligned_bin_name), 'wb+') as fo:
670-
fo.write(f.read(bin_size))
671+
with open(os.path.join(dut.app.binary_path, aligned_bin_name), 'wb+') as output_file:
672+
output_file.write(f.read(bin_size))
671673
for _ in range(dummy_data_size):
672-
fo.write(struct.pack('B', random.randrange(0,255,1)))
674+
output_file.write(struct.pack('B', random.randrange(0,255,1)))
673675
# Start server
674676
chunked_server = start_chunked_server(dut.app.binary_path, 8070)
675677
try:

examples/system/ota/native_ota_example/pytest_native_ota.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,10 @@ def start_https_server(ota_image_dir: str, server_ip: str, server_port: int) ->
110110
requestHandler = https_request_handler()
111111
httpd = http.server.HTTPServer((server_ip, server_port), requestHandler)
112112

113-
httpd.socket = ssl.wrap_socket(httpd.socket,
114-
keyfile=key_file,
115-
certfile=server_file, server_side=True)
113+
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
114+
ssl_context.load_cert_chain(certfile=server_file, keyfile=key_file)
115+
116+
httpd.socket = ssl_context.wrap_socket(httpd.socket, server_side=True)
116117
httpd.serve_forever()
117118

118119

@@ -186,8 +187,8 @@ def test_examples_protocol_native_ota_example_truncated_bin(dut: Dut) -> None:
186187
with open(binary_file, 'rb+') as fr:
187188
bin_data = fr.read(truncated_bin_size)
188189
binary_file = os.path.join(dut.app.binary_path, truncated_bin_name)
189-
with open(binary_file, 'wb+') as fo:
190-
fo.write(bin_data)
190+
with open(binary_file, 'wb+') as output_file:
191+
output_file.write(bin_data)
191192
# Start server
192193
thread1 = multiprocessing.Process(target=start_https_server, args=(dut.app.binary_path, '0.0.0.0', server_port))
193194
thread1.daemon = True
@@ -215,7 +216,7 @@ def test_examples_protocol_native_ota_example_truncated_bin(dut: Dut) -> None:
215216
@pytest.mark.ethernet_ota
216217
def test_examples_protocol_native_ota_example_truncated_header(dut: Dut) -> None:
217218
"""
218-
Working of OTA if headers of binary file are truncated is vaildated in this test case.
219+
Working of OTA if headers of binary file are truncated is validated in this test case.
219220
Application should return with error message in this case.
220221
steps: |
221222
1. join AP/Ethernet
@@ -235,8 +236,8 @@ def test_examples_protocol_native_ota_example_truncated_header(dut: Dut) -> None
235236
with open(binary_file, 'rb+') as fr:
236237
bin_data = fr.read(truncated_bin_size)
237238
binary_file = os.path.join(dut.app.binary_path, truncated_bin_name)
238-
with open(binary_file, 'wb+') as fo:
239-
fo.write(bin_data)
239+
with open(binary_file, 'wb+') as output_file:
240+
output_file.write(bin_data)
240241
# Start server
241242
thread1 = multiprocessing.Process(target=start_https_server, args=(dut.app.binary_path, '0.0.0.0', server_port))
242243
thread1.daemon = True
@@ -275,17 +276,17 @@ def test_examples_protocol_native_ota_example_random(dut: Dut) -> None:
275276
server_port = 8002
276277
# Random binary file to be generated
277278
random_bin_name = 'random.bin'
278-
# Size of random binary file. 32000 is choosen, to reduce the time required to run the test-case
279+
# Size of random binary file. 32000 is chosen, to reduce the time required to run the test-case
279280
random_bin_size = 32000
280281
# check and log bin size
281282
binary_file = os.path.join(dut.app.binary_path, random_bin_name)
282-
fo = open(binary_file, 'wb+')
283+
output_file = open(binary_file, 'wb+')
283284
# First byte of binary file is always set to zero. If first byte is generated randomly,
284285
# in some cases it may generate 0xE9 which will result in failure of testcase.
285-
with open(binary_file, 'wb+') as fo:
286-
fo.write(struct.pack('B', 0))
286+
with open(binary_file, 'wb+') as output_file:
287+
output_file.write(struct.pack('B', 0))
287288
for _ in range(random_bin_size - 1):
288-
fo.write(struct.pack('B', random.randrange(0,255,1)))
289+
output_file.write(struct.pack('B', random.randrange(0,255,1)))
289290
# Start server
290291
thread1 = multiprocessing.Process(target=start_https_server, args=(dut.app.binary_path, '0.0.0.0', server_port))
291292
thread1.daemon = True

examples/system/ota/pre_encrypted_ota/pytest_pre_encrypted_ota.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
1+
# SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
22
# SPDX-License-Identifier: Unlicense OR CC0-1.0
33
import http.server
44
import multiprocessing
@@ -46,9 +46,10 @@ def start_https_server(ota_image_dir: str, server_ip: str, server_port: int) ->
4646
requestHandler = https_request_handler()
4747
httpd = http.server.HTTPServer((server_ip, server_port), requestHandler)
4848

49-
httpd.socket = ssl.wrap_socket(httpd.socket,
50-
keyfile=key_file,
51-
certfile=server_file, server_side=True)
49+
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
50+
ssl_context.load_cert_chain(certfile=server_file, keyfile=key_file)
51+
52+
httpd.socket = ssl_context.wrap_socket(httpd.socket, server_side=True)
5253
httpd.serve_forever()
5354

5455

examples/system/ota/simple_ota_example/pytest_simple_ota.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,10 @@ def start_https_server(ota_image_dir: str, server_ip: str, server_port: int, ser
8787

8888
httpd = http.server.HTTPServer((server_ip, server_port), http.server.SimpleHTTPRequestHandler)
8989

90-
httpd.socket = ssl.wrap_socket(httpd.socket,
91-
keyfile=key_file,
92-
certfile=server_file, server_side=True)
90+
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
91+
ssl_context.load_cert_chain(certfile=server_file, keyfile=key_file)
92+
93+
httpd.socket = ssl_context.wrap_socket(httpd.socket, server_side=True)
9394
httpd.serve_forever()
9495

9596

0 commit comments

Comments
 (0)