electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit d03da4d1a22a5f77e5700a89ab3bd78f3eedd9c9
parent 770f438249078b598964d44b72242fc64e4ece75
Author: SomberNight <somber.night@protonmail.com>
Date:   Fri, 25 May 2018 22:06:45 +0200

tests:  test with all AES implementations

Diffstat:
Mlib/tests/test_bitcoin.py | 34++++++++++++++++++++++++++++++++++
1 file changed, 34 insertions(+), 0 deletions(-)

diff --git a/lib/tests/test_bitcoin.py b/lib/tests/test_bitcoin.py @@ -53,12 +53,40 @@ def needs_test_with_all_ecc_implementations(func): return run_test +def needs_test_with_all_aes_implementations(func): + """Function decorator to run a unit test twice: + once when pycryptodomex is not available, once when it is. + + NOTE: this is inherently sequential; + tests running in parallel would break things + """ + def run_test(*args, **kwargs): + _aes = crypto.AES + crypto.AES = None + try: + # first test without pycryptodomex + func(*args, **kwargs) + finally: + # if pycryptodomex is not available, we are done + if not _aes: + return + crypto.AES = _aes + # if pycryptodomex is available, test again now + func(*args, **kwargs) + return run_test + + class Test_bitcoin(SequentialTestCase): def test_libsecp256k1_is_available(self): # we want the unit testing framework to test with libsecp256k1 available. self.assertTrue(bool(ecc_fast._libsecp256k1)) + def test_pycryptodomex_is_available(self): + # we want the unit testing framework to test with pycryptodomex available. + self.assertTrue(bool(crypto.AES)) + + @needs_test_with_all_aes_implementations @needs_test_with_all_ecc_implementations def test_crypto(self): for message in [b"Chancellor on brink of second bailout for banks", b'\xff'*512]: @@ -120,6 +148,7 @@ class Test_bitcoin(SequentialTestCase): self.assertFalse(ecc.verify_message_with_address(addr1, b'wrong', msg1)) self.assertFalse(ecc.verify_message_with_address(addr1, sig2, msg1)) + @needs_test_with_all_aes_implementations @needs_test_with_all_ecc_implementations def test_decrypt_message(self): key = WalletStorage.get_eckey_from_password('pw123') @@ -127,6 +156,7 @@ class Test_bitcoin(SequentialTestCase): self.assertEqual(b'me<(s_s)>age', key.decrypt_message(b'QklFMQKXOXbylOQTSMGf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eXmwBxKHsRx/3+aJu32Ml9+za41zVk2viaxcGqwTc5KMexQFLAUwqhv+aIik7U+5qk/gEVSuRoVkihoweFzKolNF+BknH2oB4rZdPixag5Zje3DvgjsSFlOl69W/67t/Gs8htfSAaHlsB8vWRQr9+v/lxTbrAw+O0E+sYGoObQ4qQMyQshNZEHbpPg63eWiHtJJnrVBvOeIbIHzoLDnMDsWVWZSMzAQ1vhX1H5QLgSEbRlKSliVY03kDkh/Nk/KOn+B2q37Ialq4JcRoIYFGJ8AoYEAD0tRuTqFddIclE75HzwaNG7NyKW1plsa72ciOPwsPJsdd5F0qdSQ3OSKtooTn7uf6dXOc4lDkfrVYRlZ0PX')) + @needs_test_with_all_aes_implementations @needs_test_with_all_ecc_implementations def test_encrypt_message(self): key = WalletStorage.get_eckey_from_password('secret_password77') @@ -151,6 +181,7 @@ class Test_bitcoin(SequentialTestCase): sig2 = eckey2.sign_transaction(bfh('642a2e66332f507c92bda910158dfe46fc10afbf72218764899d3af99a043fac')) self.assertEqual(bfh('30440220618513f4cfc87dde798ce5febae7634c23e7b9254a1eabf486be820f6a7c2c4702204fef459393a2b931f949e63ced06888f35e286e446dc46feb24b5b5f81c6ed52'), sig2) + @needs_test_with_all_aes_implementations def test_aes_homomorphic(self): """Make sure AES is homomorphic.""" payload = u'\u66f4\u7a33\u5b9a\u7684\u4ea4\u6613\u5e73\u53f0' @@ -159,18 +190,21 @@ class Test_bitcoin(SequentialTestCase): dec = crypto.pw_decode(enc, password) self.assertEqual(dec, payload) + @needs_test_with_all_aes_implementations def test_aes_encode_without_password(self): """When not passed a password, pw_encode is noop on the payload.""" payload = u'\u66f4\u7a33\u5b9a\u7684\u4ea4\u6613\u5e73\u53f0' enc = crypto.pw_encode(payload, None) self.assertEqual(payload, enc) + @needs_test_with_all_aes_implementations def test_aes_deencode_without_password(self): """When not passed a password, pw_decode is noop on the payload.""" payload = u'\u66f4\u7a33\u5b9a\u7684\u4ea4\u6613\u5e73\u53f0' enc = crypto.pw_decode(payload, None) self.assertEqual(payload, enc) + @needs_test_with_all_aes_implementations def test_aes_decode_with_invalid_password(self): """pw_decode raises an Exception when supplied an invalid password.""" payload = u"blah"