1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 15:38:11 +00:00

style(core/tests): fix warnings

This commit is contained in:
Ondřej Vejpustek 2024-11-15 14:17:38 +01:00
parent d44a51ef49
commit d081f185b1
27 changed files with 86 additions and 92 deletions

View File

@ -1,7 +1,7 @@
# flake8: noqa: F403,F405
from common import * # isort:skip
if utils.INTERNAL_MODEL in ("T2T1", ):
if utils.INTERNAL_MODEL in ("T2T1", ): # pylint: disable=internal-model-tuple-comparison
from trezor.crypto import bip39
from trezor.enums import AmountUnit, OutputScriptType
from trezor.enums.RequestType import TXFINISHED, TXINPUT, TXMETA, TXOUTPUT
@ -406,5 +406,8 @@ class TestSignTxDecred(unittest.TestCase):
if __name__ == "__main__":
if utils.INTERNAL_MODEL in ("T2T1",):
if utils.INTERNAL_MODEL in ( # pylint: disable=internal-model-tuple-comparison
"T2T1",
):
unittest.main()

View File

@ -628,7 +628,7 @@ class TestCardanoAddress(unittest.TestCase):
for (
network_id,
address_type,
_,
address_parameters,
expected_address,
) in test_vectors:

View File

@ -23,7 +23,9 @@ class TestCoins(unittest.TestCase):
("ZEC", "Zcash", 7352),
("TAZ", "Zcash Testnet", 7461),
]
if utils.INTERNAL_MODEL in ("T2T1",):
if utils.INTERNAL_MODEL in ( # pylint: disable=internal-model-tuple-comparison
"T2T1",
):
ref.extend(
[
("NMC", "Namecoin", 52),

View File

@ -58,7 +58,7 @@ class TestDecodeDefinition(unittest.TestCase):
def test_missing_signature(self):
payload = make_payload()
proof, signature = sign_payload(payload, [])
proof, _ = sign_payload(payload, [])
self.assertFailed(payload + proof)
def test_mangled_payload(self):
@ -69,7 +69,7 @@ class TestDecodeDefinition(unittest.TestCase):
def test_proof_length_mismatch(self):
payload = make_payload()
proof, signature = sign_payload(payload, [])
_, signature = sign_payload(payload, [])
bad_proof = b"\x01"
self.assertFailed(payload + bad_proof + signature)
@ -133,13 +133,13 @@ class TestEthereumDefinitions(unittest.TestCase):
return
if what is tokens.UNKNOWN_TOKEN:
return
self.fail("Expected UNKNOWN_*, got %r" % what)
self.fail(f"Expected UNKNOWN_*, got {what}")
def assertKnown(self, what: t.Any) -> None:
if not EthereumNetworkInfo.is_type_of(
what
) and not EthereumTokenInfo.is_type_of(what):
self.fail("Expected network / token info, got %r" % what)
self.fail(f"Expected network / token info, got {what}")
if what is networks.UNKNOWN_NETWORK:
self.fail("Expected known network, got UNKNOWN_NETWORK")
if what is tokens.UNKNOWN_TOKEN:

View File

@ -135,6 +135,7 @@ def parse_type_n(type_name: str) -> int:
buf += char
else:
return int("".join(reversed(buf)))
raise ValueError(f"Invalid type name: {type_name}")
def parse_array_n(type_name: str) -> Union[int, str]:

View File

@ -175,19 +175,19 @@ class TestSlip39(unittest.TestCase):
storage.recovery.set_in_progress(True)
words = MNEMONIC_SLIP39_BASIC_20_3of6[0]
secret, share = process_slip39(words)
secret, _ = process_slip39(words)
self.assertIsNone(secret)
# same mnemonic
words = MNEMONIC_SLIP39_BASIC_20_3of6[0]
with self.assertRaises(RuntimeError):
secret, share = process_slip39(words)
secret, _ = process_slip39(words)
self.assertIsNone(secret)
# identifier mismatch
words = MNEMONIC_SLIP39_ADVANCED_20[0]
with self.assertRaises(RuntimeError):
secret, share = process_slip39(words)
secret, _ = process_slip39(words)
self.assertIsNone(secret)
# same identifier but different group settings
@ -200,7 +200,7 @@ class TestSlip39(unittest.TestCase):
w[19] = "merchant"
words = " ".join(w)
with self.assertRaises(RuntimeError):
secret, share = process_slip39(words)
secret, _ = process_slip39(words)
self.assertIsNone(secret)
@mock_storage
@ -218,11 +218,11 @@ class TestSlip39(unittest.TestCase):
check(BackupType.Bip39, ["ocean"])
# let's store two shares in the storage
secret, share = process_slip39(
secret, _ = process_slip39(
"trash smug adjust ambition criminal prisoner security math cover pecan response pharmacy center criminal salary elbow bracelet lunar briefing dragon"
)
self.assertIsNone(secret)
secret, share = process_slip39(
secret, _ = process_slip39(
"trash smug adjust aide benefit temple round clogs devote prevent type cards clogs plastic aspect paper behavior lunar custody intimate"
)
self.assertIsNone(secret)
@ -244,11 +244,11 @@ class TestSlip39(unittest.TestCase):
check(BackupType.Slip39_Advanced, ["trash", "smug", "adjust", "ambition"])
# Let's store two more. The group is 4/6 so this group is now complete.
secret, share = process_slip39(
secret, _ = process_slip39(
"trash smug adjust arena beard quick language program true hush amount round geology should training practice language diet order ruin"
)
self.assertIsNone(secret)
secret, share = process_slip39(
secret, _ = process_slip39(
"trash smug adjust beam brave sack magazine radar toxic emission domestic cradle vocal petition mule toxic acid hobo welcome downtown"
)
self.assertIsNone(secret)

View File

@ -14,7 +14,7 @@ class TestMoneroBulletproof(unittest.TestCase):
ss = crypto.random_scalar()
s1 = crypto.sc_copy(None, ss)
s2 = crypto.sc_copy(None, ss)
for i in range(1, x):
for _ in range(1, x):
crypto.sc_mul_into(s1, s1, ss)
bp._sc_square_mult(s2, ss, x)

View File

@ -108,7 +108,7 @@ class TestMoneroClsag(unittest.TestCase):
Cp = crypto.add_keys2_into(None, alpha, amnt, crypto.xmr_H())
ring = []
for i in range(ring_size - 1):
for _ in range(ring_size - 1):
tk = TmpKey(
crypto_helpers.encodepoint(
crypto.scalarmult_base_into(None, crypto.random_scalar())

View File

@ -1,5 +1,5 @@
# flake8: noqa: F403,F405
from common import *
from common import * # isort: skip
from trezor import config, utils
from trezor import log

View File

@ -139,7 +139,7 @@ class TestZcashSigHasher(unittest.TestCase):
self.assertEqual(computed_txid, expected_txid)
# test ZcashSigHasher.signature_digest
for txi, expected_sighash, pk in zip(inputs, expected_sighashes, pubkeys):
for txi, expected_sighash, _ in zip(inputs, expected_sighashes, pubkeys):
computed_sighash = hasher.signature_digest(txi, txi.script_pubkey)
self.assertEqual(computed_sighash, expected_sighash)

View File

@ -64,7 +64,7 @@ class TestStorageCache(unittest.TestCase):
session_id = cache.start_session()
self.assertEqual(cache.start_session(session_id), session_id)
cache.set(KEY, b"A")
for i in range(cache._MAX_SESSIONS_COUNT):
for _ in range(cache._MAX_SESSIONS_COUNT):
cache.start_session()
self.assertNotEqual(cache.start_session(session_id), session_id)
self.assertIsNone(cache.get(KEY))

View File

@ -26,15 +26,13 @@ class TestCryptoBase32(unittest.TestCase):
b"zlutoucky kun upel dabelske ody",
"PJWHK5DPOVRWW6JANN2W4IDVOBSWYIDEMFRGK3DTNNSSA33EPE======",
),
# fmt: off
(b"中文", "4S4K3ZUWQ4======"), # noqa: E999
(b"中文1", "4S4K3ZUWQ4YQ===="), # noqa: E999
(b"中文12", "4S4K3ZUWQ4YTE==="), # noqa: E999
(b"aécio", "MHB2SY3JN4======"), # noqa: E999
(b"𠜎", "6CQJZDQ="), # noqa: E999
(b"Base64是一種基於64個可列印字元來表示二進制資料的表示方法", # noqa: E999
("中文".encode(), "4S4K3ZUWQ4======"),
("中文1".encode(), "4S4K3ZUWQ4YQ===="),
("中文12".encode(), "4S4K3ZUWQ4YTE==="),
("aécio".encode(), "MHB2SY3JN4======"),
("𠜎".encode(), "6CQJZDQ="),
("Base64是一種基於64個可列印字元來表示二進制資料的表示方法".encode(),
"IJQXGZJWGTTJRL7EXCAOPKFO4WP3VZUWXQ3DJZMARPSY7L7FRCL6LDNQ4WWZPZMFQPSL5BXIUGUOPJF24S5IZ2MAWLSYRNXIWOD6NFUZ46NIJ2FBVDT2JOXGS246NM4V"),
# fmt: on
]
def test_encode(self):

View File

@ -1,6 +1,3 @@
# flake8: noqa: F403,F405
from common import * # isort:skip
# Copyright (c) 2017, 2020 Pieter Wuille
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
@ -21,9 +18,11 @@ from common import * # isort:skip
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""Reference tests for segwit adresses"""
# flake8: noqa: F403,F405
from common import * # isort:skip
from trezor.crypto import bech32

View File

@ -1,6 +1,3 @@
# flake8: noqa: F403,F405
from common import * # isort:skip
# Copyright (c) 2017 Pieter Wuille
# Copyright (c) 2018 Pavol Rusnak
#
@ -22,9 +19,11 @@ from common import * # isort:skip
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""Reference tests for cashaddr adresses"""
# flake8: noqa: F403,F405
from common import * # isort:skip
from trezor.crypto import base58, cashaddr
VALID_CHECKSUM = [

View File

@ -63,7 +63,7 @@ class TestCryptoEd25519(unittest.TestCase):
self.assertEqual(sig2, unhexlify(sig))
def test_verify(self):
for sk, pk, sig in self.vectors:
for _, pk, sig in self.vectors:
# msg = pk
self.assertTrue(
ed25519.verify(unhexlify(pk), unhexlify(sig), unhexlify(pk))

View File

@ -63,12 +63,10 @@ class TestCryptoGroestl512(unittest.TestCase):
self.assertEqual(x.digest(), unhexlify(d))
# Test from ExtremelyLongMsgKAT_512.txt, disabled by default because it resource-expensive
"""
x = hashlib.groestl512()
for i in range(16777216):
x.update(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno')
self.assertEqual(x.digest(), unhexlify('787C88460E5D09ABD7A98C050F3422BBFDBD36A74B05DE04B57A13FA3F36A570B8561580AB9DA4096CCD5111B5DE948F769D9D61833A6CE2B2F223061E688994'))
"""
# x = hashlib.groestl512()
# for i in range(16777216):
# x.update(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno')
# self.assertEqual(x.digest(), unhexlify('787C88460E5D09ABD7A98C050F3422BBFDBD36A74B05DE04B57A13FA3F36A570B8561580AB9DA4096CCD5111B5DE948F769D9D61833A6CE2B2F223061E688994'))
def test_digest_multi(self):
x = hashlib.groestl512()

View File

@ -38,7 +38,7 @@ class TestCryptoRipemd160(unittest.TestCase):
self.assertEqual(x.digest(), unhexlify(d))
x = hashlib.ripemd160()
for i in range(8):
for _ in range(8):
x.update(b"1234567890")
self.assertEqual(
x.digest(), unhexlify("9b752e45573d4b39f4dbd3323cab82bf63326bfb")

View File

@ -31,18 +31,16 @@ class TestCryptoSha1(unittest.TestCase):
self.assertEqual(x.digest(), unhexlify(d))
x = hashlib.sha1()
for i in range(1000000):
for _ in range(1000000):
x.update(b"a")
self.assertEqual(
x.digest(), unhexlify("34aa973cd4c4daa4f61eeb2bdbad27316534016f")
)
"""
x = hashlib.sha1()
for i in range(16777216):
x.update(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno')
self.assertEqual(x.digest(), unhexlify('7789f0c9ef7bfc40d93311143dfbe69e2017f592'))
"""
# x = hashlib.sha1()
# for i in range(16777216):
# x.update(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno')
# self.assertEqual(x.digest(), unhexlify('7789f0c9ef7bfc40d93311143dfbe69e2017f592'))
def test_digest_multi(self):
x = hashlib.sha1()

View File

@ -31,7 +31,7 @@ class TestCryptoSha256(unittest.TestCase):
self.assertEqual(x.digest(), unhexlify(d))
x = hashlib.sha256()
for i in range(1000000):
for _ in range(1000000):
x.update(b"a")
self.assertEqual(
x.digest(),
@ -40,12 +40,10 @@ class TestCryptoSha256(unittest.TestCase):
),
)
"""
x = hashlib.sha256()
for i in range(16777216):
x.update(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno')
self.assertEqual(x.digest(), unhexlify('50e72a0e26442fe2552dc3938ac58658228c0cbfb1d2ca872ae435266fcd055e'))
"""
# x = hashlib.sha256()
# for i in range(16777216):
# x.update(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno')
# self.assertEqual(x.digest(), unhexlify('50e72a0e26442fe2552dc3938ac58658228c0cbfb1d2ca872ae435266fcd055e'))
def test_digest_multi(self):
x = hashlib.sha256()

View File

@ -48,7 +48,7 @@ class TestCryptoSha3_256(unittest.TestCase):
self.assertEqual(x.digest(), unhexlify(d))
x = hashlib.sha3_256()
for i in range(1000000):
for _ in range(1000000):
x.update(b"a")
self.assertEqual(
x.digest(),
@ -57,12 +57,10 @@ class TestCryptoSha3_256(unittest.TestCase):
),
)
"""
x = hashlib.sha3_256()
for i in range(16777216):
x.update(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno')
self.assertEqual(x.digest(), unhexlify('ecbbc42cbf296603acb2c6bc0410ef4378bafb24b710357f12df607758b33e2b'))
"""
# x = hashlib.sha3_256()
# for i in range(16777216):
# x.update(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno')
# self.assertEqual(x.digest(), unhexlify('ecbbc42cbf296603acb2c6bc0410ef4378bafb24b710357f12df607758b33e2b'))
def test_update_keccak(self):
for b, d in self.vectors_keccak:

View File

@ -60,7 +60,7 @@ class TestCryptoSha3_512(unittest.TestCase):
self.assertEqual(x.digest(), unhexlify(d))
x = hashlib.sha3_512()
for i in range(1000000):
for _ in range(1000000):
x.update(b"a")
self.assertEqual(
x.digest(),
@ -69,12 +69,10 @@ class TestCryptoSha3_512(unittest.TestCase):
),
)
"""
x = hashlib.sha3_512()
for i in range(16777216):
x.update(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno')
self.assertEqual(x.digest(), unhexlify('235ffd53504ef836a1342b488f483b396eabbfe642cf78ee0d31feec788b23d0d18d5c339550dd5958a500d4b95363da1b5fa18affc1bab2292dc63b7d85097c'))
"""
# x = hashlib.sha3_512()
# for i in range(16777216):
# x.update(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno')
# self.assertEqual(x.digest(), unhexlify('235ffd53504ef836a1342b488f483b396eabbfe642cf78ee0d31feec788b23d0d18d5c339550dd5958a500d4b95363da1b5fa18affc1bab2292dc63b7d85097c'))
def test_update_keccak(self):
for b, d in self.vectors_keccak:

View File

@ -37,7 +37,7 @@ class TestCryptoSha512(unittest.TestCase):
self.assertEqual(x.digest(), unhexlify(d))
x = hashlib.sha512()
for i in range(1000000):
for _ in range(1000000):
x.update(b"a")
self.assertEqual(
x.digest(),
@ -46,12 +46,10 @@ class TestCryptoSha512(unittest.TestCase):
),
)
"""
x = hashlib.sha512()
for i in range(16777216):
x.update(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno')
self.assertEqual(x.digest(), unhexlify('b47c933421ea2db149ad6e10fce6c7f93d0752380180ffd7f4629a712134831d77be6091b819ed352c2967a2e2d4fa5050723c9630691f1a05a7281dbe6c1086'))
"""
# x = hashlib.sha512()
# for i in range(16777216):
# x.update(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno')
# self.assertEqual(x.digest(), unhexlify('b47c933421ea2db149ad6e10fce6c7f93d0752380180ffd7f4629a712134831d77be6091b819ed352c2967a2e2d4fa5050723c9630691f1a05a7281dbe6c1086'))
def test_digest_multi(self):
x = hashlib.sha512()

View File

@ -111,7 +111,7 @@ class TestCryptoHmac(unittest.TestCase):
# case 3
key = b"\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
x = hmac(hmac.SHA256, key)
for i in range(50):
for _ in range(50):
x.update(b"\xdd")
self.assertEqual(
x.digest(),
@ -120,7 +120,7 @@ class TestCryptoHmac(unittest.TestCase):
),
)
x = hmac(hmac.SHA512, key)
for i in range(50):
for _ in range(50):
x.update(b"\xdd")
self.assertEqual(
x.digest(),
@ -132,7 +132,7 @@ class TestCryptoHmac(unittest.TestCase):
# case 4
key = b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19"
x = hmac(hmac.SHA256, key)
for i in range(50):
for _ in range(50):
x.update(b"\xcd")
self.assertEqual(
x.digest(),
@ -141,7 +141,7 @@ class TestCryptoHmac(unittest.TestCase):
),
)
x = hmac(hmac.SHA512, key)
for i in range(50):
for _ in range(50):
x.update(b"\xcd")
self.assertEqual(
x.digest(),

View File

@ -65,7 +65,7 @@ class TestCryptoPbkdf2(unittest.TestCase):
),
)
p = pbkdf2(pbkdf2.HMAC_SHA256, P, S)
for i in range(32):
for _ in range(32):
p.update(128)
dk = p.key()
self.assertEqual(
@ -77,7 +77,7 @@ class TestCryptoPbkdf2(unittest.TestCase):
P = b"passwordPASSWORDpassword"
S = b"saltSALTsaltSALTsaltSALTsaltSALTsalt"
p = pbkdf2(pbkdf2.HMAC_SHA256, P, S)
for i in range(64):
for _ in range(64):
p.update(64)
dk = p.key()
self.assertEqual(
@ -146,7 +146,7 @@ class TestCryptoPbkdf2(unittest.TestCase):
),
)
p = pbkdf2(pbkdf2.HMAC_SHA512, P, S)
for i in range(32):
for _ in range(32):
p.update(128)
dk = p.key()
self.assertEqual(
@ -158,7 +158,7 @@ class TestCryptoPbkdf2(unittest.TestCase):
P = b"passwordPASSWORDpassword"
S = b"saltSALTsaltSALTsaltSALTsaltSALTsalt"
p = pbkdf2(pbkdf2.HMAC_SHA512, P, S)
for i in range(64):
for _ in range(64):
p.update(64)
dk = p.key()
self.assertEqual(

View File

@ -65,13 +65,13 @@ class TestCryptoSlip39(unittest.TestCase):
identifier = slip39.generate_random_identifier()
mnemonics = slip39.split_ems(1, [(3, 5)], identifier, extendable, 1, self.EMS)
mnemonics = mnemonics[0]
identifier, extendable, exponent, ems = slip39.recover_ems(mnemonics[1:4])
identifier, extendable, _, ems = slip39.recover_ems(mnemonics[1:4])
self.assertEqual(ems, self.EMS)
identifier = slip39.generate_random_identifier()
mnemonics = slip39.split_ems(1, [(3, 5)], identifier, extendable, 2, self.EMS)
mnemonics = mnemonics[0]
identifier, extendable, exponent, ems = slip39.recover_ems(mnemonics[1:4])
identifier, extendable, _, ems = slip39.recover_ems(mnemonics[1:4])
self.assertEqual(ems, self.EMS)
def test_group_sharing(self):

View File

@ -54,7 +54,10 @@ class TestUtils(unittest.TestCase):
) # b'\xe1\x88\xb4\xe5\x99\xb8
def test_firmware_hash(self):
if utils.INTERNAL_MODEL in ('DISC2', 'T3W1'):
if utils.INTERNAL_MODEL in ( # pylint: disable=internal-model-tuple-comparison
"DISC2",
"T3W1",
):
self.assertEqual(
utils.firmware_hash(),
b"\xde\xce\xc5\xf6\xa4vgl5\x13l2\xa5\xf8F\xd8\xba\n$\x0b!x\x1fVM\x1e\xf3}@\xd9\xa8\xe9",

View File

@ -140,10 +140,11 @@ class TestCase:
func(*args, **kwargs)
except Exception as e:
if isinstance(e, exc):
return
return None
raise
else:
ensure(False, f"{repr(exc)} not raised")
return None
def assertListEqual(self, x, y, msg=""):
if len(x) != len(y):