Skip to content

Commit f7bda2e

Browse files
committed
tests: Fix python code using ruff
This auto-fixes the code using ruff Signed-off-by: Jamie McCrae <[email protected]>
1 parent 49e0f1e commit f7bda2e

File tree

14 files changed

+142
-151
lines changed

14 files changed

+142
-151
lines changed

tests/drivers/grtc/grtc_reset/pytest/tests.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import logging
88
import re
99
import subprocess
10-
import time
1110

1211
from twister_harness import DeviceAdapter
1312

tests/drivers/uart/uart_passtrough/pytest/serial_port.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def send(
7474
"""
7575
response: str = ""
7676
if self.serial_port.is_open is False:
77-
raise IOError("Serial port is closed")
77+
raise OSError("Serial port is closed")
7878

7979
try:
8080
time.sleep(0.25)

tests/drivers/uart/uart_passtrough/pytest/test_passtrough.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import logging
66
import time
7-
from typing import Callable
7+
from collections.abc import Callable
88

99
from serial_port import SerialPort
1010
from twister_harness import DeviceAdapter
@@ -29,7 +29,7 @@ def send_with_dead_time_between_characters(
2929
write_handler(character)
3030
time.sleep(dead_time_s)
3131
if apply_encoding:
32-
line_termination = "\n".encode()
32+
line_termination = b"\n"
3333
else:
3434
line_termination = "\n"
3535
write_handler(line_termination)

tests/lib/hw_unique_key_tfm/create_test_vector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#
55
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
66

7-
import cryptography.hazmat.primitives.cmac as cmac
87
import cryptography.hazmat.primitives.ciphers.algorithms as alg
8+
import cryptography.hazmat.primitives.cmac as cmac
99
from cryptography.hazmat.primitives import hashes
1010
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
1111

tests/lib/hw_unique_key_tfm/write_kmu.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
#
55
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
66

7-
from intelhex import IntelHex as ih
7+
from argparse import ArgumentParser
88
from struct import pack
99
from subprocess import run
10-
from argparse import ArgumentParser
10+
11+
from intelhex import IntelHex as ih
1112

1213
key_nrf53 = [
1314
0xc5, 0xa8, 0x08, 0xeb, 0xe3, 0x1e, 0xa5, 0xb4,

tests/subsys/bootloader/bl_crypto/test_generator.py

Lines changed: 117 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -4,131 +4,132 @@
44
#
55
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
66

7-
from ecdsa import SigningKey, NIST256p
7+
import operator
88
import random
9+
import sys
910
from hashlib import sha256
10-
import operator
1111
from os import linesep as ls
12-
import sys
12+
13+
from ecdsa import NIST256p, SigningKey
1314

1415

1516
def c_code_declare_array(name, array):
16-
return "uint8_t %s[] = {%s};%s" % (name, ", ".join([hex(c) for c in array]), ls+ls)
17+
return "uint8_t {}[] = {{{}}};{}".format(name, ", ".join([hex(c) for c in array]), ls+ls)
1718

1819
def arr_to_hexstr(arr):
19-
return b''.join([bytes([x]) for x in arr])
20+
return b''.join([bytes([x]) for x in arr])
2021

2122
def hexstr_to_array(hexstr):
22-
ret_str = ""
23-
for byte in map(operator.add, hexstr[::2], hexstr[1::2]):
24-
ret_str += "0x"+byte+","
25-
return ret_str[:-1]
23+
ret_str = ""
24+
for byte in map(operator.add, hexstr[::2], hexstr[1::2]):
25+
ret_str += "0x"+byte+","
26+
return ret_str[:-1]
2627

2728
if __name__ == "__main__":
28-
firmware = bytearray(random.randint(0, 255) for _ in range(random.randrange(4, 1000)))
29-
firmware_hash = sha256(firmware).digest()
30-
metadata = b"a%sb" % firmware_hash
31-
priv = SigningKey.generate(curve=NIST256p)
32-
pub = priv.get_verifying_key()
33-
sig = priv.sign(firmware_hash, hashfunc = sha256)
34-
pub_hash = sha256(pub.to_string()).digest()
35-
36-
with open('fw_data.bin', 'rb') as f:
37-
fw_hex = f.read()
38-
39-
fw_sk = SigningKey.generate(curve=NIST256p, hashfunc = sha256)
40-
fw_vk = fw_sk.get_verifying_key()
41-
generated_sig = fw_sk.sign(fw_hex, hashfunc = sha256)
42-
fw_hash = sha256(fw_hex).hexdigest()
43-
fw_hash = hexstr_to_array(fw_hash)
44-
gen_sig = hexstr_to_array(generated_sig.hex())
45-
46-
fw_x = fw_vk.pubkey.point.x()
47-
fw_pubkey_x = hexstr_to_array(fw_x.to_bytes(32, "big").hex())
48-
fw_y = fw_vk.pubkey.point.y()
49-
fw_pubkey_y = hexstr_to_array(fw_y.to_bytes(32, "big").hex())
50-
fw_pubkey = fw_pubkey_x +","+ fw_pubkey_y
51-
52-
assert fw_vk.verify(generated_sig, fw_hex, hashfunc = sha256)
53-
54-
sk = SigningKey.generate(curve=NIST256p, hashfunc = sha256)
55-
vk = sk.get_verifying_key()
56-
my_hash = b"breadcrumb"
57-
my_hash_array = hexstr_to_array(my_hash.hex())
58-
breadcrumb = sha256(b"breadcrumb")
59-
sha256_hash = hexstr_to_array(breadcrumb.hexdigest())
60-
61-
signature = sk.sign(my_hash)
62-
r = signature[:int(len(signature)/2)]
63-
s = signature[int(len(signature)/2):]
64-
sig_r = hexstr_to_array(r.hex())
65-
sig_s = hexstr_to_array(s.hex())
66-
sig_concat = hexstr_to_array(signature.hex())
67-
68-
x = vk.pubkey.point.x()
69-
pubkey_x = hexstr_to_array(x.to_bytes(32, "big").hex())
70-
y = vk.pubkey.point.y()
71-
pubkey_y = hexstr_to_array(y.to_bytes(32, "big").hex())
72-
73-
pubkey_concat = pubkey_x + "," + pubkey_y
74-
75-
mcuboot_key = [0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86,
76-
0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a,
77-
0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
78-
0x42, 0x00, 0x04, 0x2a, 0xcb, 0x40, 0x3c, 0xe8,
79-
0xfe, 0xed, 0x5b, 0xa4, 0x49, 0x95, 0xa1, 0xa9,
80-
0x1d, 0xae, 0xe8, 0xdb, 0xbe, 0x19, 0x37, 0xcd,
81-
0x14, 0xfb, 0x2f, 0x24, 0x57, 0x37, 0xe5, 0x95,
82-
0x39, 0x88, 0xd9, 0x94, 0xb9, 0xd6, 0x5a, 0xeb,
83-
0xd7, 0xcd, 0xd5, 0x30, 0x8a, 0xd6, 0xfe, 0x48, 0xb2, 0x4a, 0x6a, 0x81, 0x0e, 0xe5, 0xf0, 0x7d,
84-
0x8b, 0x68, 0x34, 0xcc, 0x3a, 0x6a, 0xfc, 0x53,
85-
0x8e, 0xfa, 0xc1]
86-
87-
mcuboot_key_hash = sha256(b''.join(bytes([x]) for x in mcuboot_key))
88-
mcuboot_key_hash = hexstr_to_array(mcuboot_key_hash.hexdigest())
89-
90-
mcuboot_key = b''.join(bytes([x]) for x in mcuboot_key)
91-
mcuboot_key = hexstr_to_array(mcuboot_key.hex())
92-
93-
long_input = b'a' * 100000
94-
long_input_hash = hexstr_to_array(sha256(long_input).hexdigest())
95-
long_input = hexstr_to_array(long_input.hex())
96-
97-
fw_sig = b'ac95651230dee1b857d29971fd5177cf4536ee4a819abaec950cccae27548a3823ff093cc2a64a8dab7f4df73dec98'
98-
99-
assert vk.verify(signature, my_hash)
100-
101-
with open(sys.argv[1], 'w') as f:
102-
f.write(c_code_declare_array("pk_hash", pub_hash))
103-
f.write(c_code_declare_array("pk", pub.to_string()))
104-
f.write(c_code_declare_array("sig", sig))
105-
f.write(c_code_declare_array("firmware_hash", firmware_hash))
106-
f.write(c_code_declare_array("firmware", firmware))
107-
f.write(c_code_declare_array("pub_x", pubkey_x))
108-
f.write(c_code_declare_array("pub_y", pubkey_y))
109-
f.write(c_code_declare_array("pub_concat", pubkey_concat))
110-
f.write(c_code_declare_array("const_pub_concat", pubkey_concat))
111-
f.write(c_code_declare_array("sig_r", sig_r))
112-
f.write(c_code_declare_array("sig_s", sig_s))
113-
f.write(c_code_declare_array("sig_concat", sig_concat))
114-
f.write(c_code_declare_array("const_sig_concat", sig_concat))
115-
f.write(c_code_declare_array("hash", my_hash_array))
116-
f.write(c_code_declare_array("const_hash", my_hash_array))
117-
f.write(c_code_declare_array("hash_sha256", sha256_hash))
118-
f.write(c_code_declare_array("const_hash_sha256", sha256_hash))
119-
f.write(c_code_declare_array("mcuboot_key", mcuboot_key))
120-
f.write(c_code_declare_array("const_mcuboot_key", mcuboot_key))
121-
f.write(c_code_declare_array("mcuboot_key_hash", mcuboot_key_hash))
122-
f.write(c_code_declare_array("long_input", long_input))
123-
f.write(c_code_declare_array("const_long_input", long_input))
124-
f.write(c_code_declare_array("long_input_hash", long_input_hash))
125-
f.write(c_code_declare_array("image_fw_data", hexstr_to_array(fw_hex.hex())))
126-
f.write(c_code_declare_array("image_fw_sig", hexstr_to_array(fw_sig_hex.hex())))
127-
f.write(c_code_declare_array("image_gen_sig", gen_sig))
128-
f.write(c_code_declare_array("image_public_key", fw_pubkey))
129-
f.write(c_code_declare_array("image_fw_hash", fw_hash))
130-
f.write(c_code_declare_array("const_fw_sig", hexstr_to_array(fw_sig_hex.hex())))
131-
f.write(c_code_declare_array("const_gen_sig", gen_sig))
132-
f.write(c_code_declare_array("const_public_key", fw_pubkey))
133-
f.write(c_code_declare_array("const_fw_hash", fw_hash))
134-
f.write(c_code_declare_array("const_fw_data", hexstr_to_array(fw_hex.hex())))
29+
firmware = bytearray(random.randint(0, 255) for _ in range(random.randrange(4, 1000)))
30+
firmware_hash = sha256(firmware).digest()
31+
metadata = b"a%sb" % firmware_hash
32+
priv = SigningKey.generate(curve=NIST256p)
33+
pub = priv.get_verifying_key()
34+
sig = priv.sign(firmware_hash, hashfunc = sha256)
35+
pub_hash = sha256(pub.to_string()).digest()
36+
37+
with open('fw_data.bin', 'rb') as f:
38+
fw_hex = f.read()
39+
40+
fw_sk = SigningKey.generate(curve=NIST256p, hashfunc = sha256)
41+
fw_vk = fw_sk.get_verifying_key()
42+
generated_sig = fw_sk.sign(fw_hex, hashfunc = sha256)
43+
fw_hash = sha256(fw_hex).hexdigest()
44+
fw_hash = hexstr_to_array(fw_hash)
45+
gen_sig = hexstr_to_array(generated_sig.hex())
46+
47+
fw_x = fw_vk.pubkey.point.x()
48+
fw_pubkey_x = hexstr_to_array(fw_x.to_bytes(32, "big").hex())
49+
fw_y = fw_vk.pubkey.point.y()
50+
fw_pubkey_y = hexstr_to_array(fw_y.to_bytes(32, "big").hex())
51+
fw_pubkey = fw_pubkey_x +","+ fw_pubkey_y
52+
53+
assert fw_vk.verify(generated_sig, fw_hex, hashfunc = sha256)
54+
55+
sk = SigningKey.generate(curve=NIST256p, hashfunc = sha256)
56+
vk = sk.get_verifying_key()
57+
my_hash = b"breadcrumb"
58+
my_hash_array = hexstr_to_array(my_hash.hex())
59+
breadcrumb = sha256(b"breadcrumb")
60+
sha256_hash = hexstr_to_array(breadcrumb.hexdigest())
61+
62+
signature = sk.sign(my_hash)
63+
r = signature[:int(len(signature)/2)]
64+
s = signature[int(len(signature)/2):]
65+
sig_r = hexstr_to_array(r.hex())
66+
sig_s = hexstr_to_array(s.hex())
67+
sig_concat = hexstr_to_array(signature.hex())
68+
69+
x = vk.pubkey.point.x()
70+
pubkey_x = hexstr_to_array(x.to_bytes(32, "big").hex())
71+
y = vk.pubkey.point.y()
72+
pubkey_y = hexstr_to_array(y.to_bytes(32, "big").hex())
73+
74+
pubkey_concat = pubkey_x + "," + pubkey_y
75+
76+
mcuboot_key = [0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86,
77+
0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a,
78+
0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
79+
0x42, 0x00, 0x04, 0x2a, 0xcb, 0x40, 0x3c, 0xe8,
80+
0xfe, 0xed, 0x5b, 0xa4, 0x49, 0x95, 0xa1, 0xa9,
81+
0x1d, 0xae, 0xe8, 0xdb, 0xbe, 0x19, 0x37, 0xcd,
82+
0x14, 0xfb, 0x2f, 0x24, 0x57, 0x37, 0xe5, 0x95,
83+
0x39, 0x88, 0xd9, 0x94, 0xb9, 0xd6, 0x5a, 0xeb,
84+
0xd7, 0xcd, 0xd5, 0x30, 0x8a, 0xd6, 0xfe, 0x48, 0xb2, 0x4a, 0x6a, 0x81, 0x0e, 0xe5, 0xf0, 0x7d,
85+
0x8b, 0x68, 0x34, 0xcc, 0x3a, 0x6a, 0xfc, 0x53,
86+
0x8e, 0xfa, 0xc1]
87+
88+
mcuboot_key_hash = sha256(b''.join(bytes([x]) for x in mcuboot_key))
89+
mcuboot_key_hash = hexstr_to_array(mcuboot_key_hash.hexdigest())
90+
91+
mcuboot_key = b''.join(bytes([x]) for x in mcuboot_key)
92+
mcuboot_key = hexstr_to_array(mcuboot_key.hex())
93+
94+
long_input = b'a' * 100000
95+
long_input_hash = hexstr_to_array(sha256(long_input).hexdigest())
96+
long_input = hexstr_to_array(long_input.hex())
97+
98+
fw_sig = b'ac95651230dee1b857d29971fd5177cf4536ee4a819abaec950cccae27548a3823ff093cc2a64a8dab7f4df73dec98'
99+
100+
assert vk.verify(signature, my_hash)
101+
102+
with open(sys.argv[1], 'w') as f:
103+
f.write(c_code_declare_array("pk_hash", pub_hash))
104+
f.write(c_code_declare_array("pk", pub.to_string()))
105+
f.write(c_code_declare_array("sig", sig))
106+
f.write(c_code_declare_array("firmware_hash", firmware_hash))
107+
f.write(c_code_declare_array("firmware", firmware))
108+
f.write(c_code_declare_array("pub_x", pubkey_x))
109+
f.write(c_code_declare_array("pub_y", pubkey_y))
110+
f.write(c_code_declare_array("pub_concat", pubkey_concat))
111+
f.write(c_code_declare_array("const_pub_concat", pubkey_concat))
112+
f.write(c_code_declare_array("sig_r", sig_r))
113+
f.write(c_code_declare_array("sig_s", sig_s))
114+
f.write(c_code_declare_array("sig_concat", sig_concat))
115+
f.write(c_code_declare_array("const_sig_concat", sig_concat))
116+
f.write(c_code_declare_array("hash", my_hash_array))
117+
f.write(c_code_declare_array("const_hash", my_hash_array))
118+
f.write(c_code_declare_array("hash_sha256", sha256_hash))
119+
f.write(c_code_declare_array("const_hash_sha256", sha256_hash))
120+
f.write(c_code_declare_array("mcuboot_key", mcuboot_key))
121+
f.write(c_code_declare_array("const_mcuboot_key", mcuboot_key))
122+
f.write(c_code_declare_array("mcuboot_key_hash", mcuboot_key_hash))
123+
f.write(c_code_declare_array("long_input", long_input))
124+
f.write(c_code_declare_array("const_long_input", long_input))
125+
f.write(c_code_declare_array("long_input_hash", long_input_hash))
126+
f.write(c_code_declare_array("image_fw_data", hexstr_to_array(fw_hex.hex())))
127+
f.write(c_code_declare_array("image_fw_sig", hexstr_to_array(fw_sig_hex.hex())))
128+
f.write(c_code_declare_array("image_gen_sig", gen_sig))
129+
f.write(c_code_declare_array("image_public_key", fw_pubkey))
130+
f.write(c_code_declare_array("image_fw_hash", fw_hash))
131+
f.write(c_code_declare_array("const_fw_sig", hexstr_to_array(fw_sig_hex.hex())))
132+
f.write(c_code_declare_array("const_gen_sig", gen_sig))
133+
f.write(c_code_declare_array("const_public_key", fw_pubkey))
134+
f.write(c_code_declare_array("const_fw_hash", fw_hash))
135+
f.write(c_code_declare_array("const_fw_data", hexstr_to_array(fw_hex.hex())))

tests/subsys/bootloader/bl_validation_ff_key/generate_ff_key.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@
44
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
55
#
66

7-
from ecdsa import SigningKey
8-
from ecdsa.curves import NIST256p
97
import hashlib
108
import sys
119

12-
for i in range(100000):
10+
from ecdsa import SigningKey
11+
from ecdsa.curves import NIST256p
12+
13+
for _i in range(100000):
1314
sk = SigningKey.generate(curve=NIST256p)
1415
vk = sk.get_verifying_key()
1516
vk_hash = hashlib.sha256(vk.to_string()).digest()[:16]
1617

1718
# Search for aligned 0xFFFF.
18-
for u16 in zip(vk_hash[::2], vk_hash[1::2]):
19+
for u16 in zip(vk_hash[::2], vk_hash[1::2], strict=False):
1920
if u16 == (0xff, 0xff):
2021
print(vk_hash)
2122
with open('ff.pem', 'wb') as f:

tests/subsys/kmu/pytest/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import os
88
import shlex
99
import subprocess
10-
1110
from pathlib import Path
11+
1212
from twister_harness.helpers.utils import find_in_config
1313

1414
logger = logging.getLogger(__name__)

tests/subsys/kmu/pytest/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Copyright (c) 2024 Nordic Semiconductor ASA
22
#
33
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
4-
import pytest
54
import logging
65

6+
import pytest
77
from twister_harness.device.device_adapter import DeviceAdapter
88
from twister_harness.fixtures import determine_scope
99

@@ -12,7 +12,7 @@
1212

1313
@pytest.fixture(scope='function', autouse=True)
1414
def test_log(request: pytest.FixtureRequest):
15-
logging.info("========= Test '{}' STARTED".format(request.node.nodeid))
15+
logging.info(f"========= Test '{request.node.nodeid}' STARTED")
1616

1717

1818
@pytest.fixture(scope=determine_scope)

tests/subsys/kmu/pytest/test_kmu_provision.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,12 @@
44
from __future__ import annotations
55

66
import logging
7-
87
from pathlib import Path
98

109
import pytest
10+
from common import APP_KEYS_FOR_KMU, get_keyname_for_mcuboot, provision_keys_for_kmu, reset_board
1111
from twister_harness import DeviceAdapter
12-
from twister_harness.helpers.utils import match_lines, find_in_config
13-
from common import (
14-
get_keyname_for_mcuboot,
15-
provision_keys_for_kmu,
16-
reset_board,
17-
APP_KEYS_FOR_KMU
18-
)
12+
from twister_harness.helpers.utils import find_in_config, match_lines
1913

2014
logger = logging.getLogger(__name__)
2115

0 commit comments

Comments
 (0)