diff --git a/tests/test.py b/tests/test.py index 37753fe01..d69208fa5 100755 --- a/tests/test.py +++ b/tests/test.py @@ -3,26 +3,26 @@ TODO: -ApplySettings +x ApplySettings x ChangePin x Ping x GetEntropy -GetPublicKey +x GetPublicKey x GetAddress x WipeDevice x LoadDevice x ResetDevice -RecoveryDevice +x RecoveryDevice x SignMessage x VerifyMessage -EstimateTxSize +x EstimateTxSize - SignTx x SimpleSignTx -FirmwareErase -FirmwareUpload +- FirmwareErase +- FirmwareUpload -protection levels -neuspesna zmena pinu +x protection levels +x neuspesna zmena pinu - zrejme v sucinnosti s inymi testami x ButtonRequest/ButtonAck workflow @@ -42,9 +42,6 @@ neuspesna zmena pinu otestovat session handling (tento test bude zrejme failovat na RPi) Failure_NotInitialized Features reflects all variations of LoadDevice -Maxfee settings -Client requires OTP -Client requires PIN x Zero signature test diff --git a/tests/test_msg_applysettings.py b/tests/test_msg_applysettings.py new file mode 100644 index 000000000..6c0855b18 --- /dev/null +++ b/tests/test_msg_applysettings.py @@ -0,0 +1,35 @@ +import time +import unittest +import common + +from trezorlib import messages_pb2 as proto + +class TestMsgApplysettings(common.TrezorTest): + def test_apply_settings(self): + self.setup_mnemonic_pin_passphrase() + self.assertEqual(self.client.features.label, 'test') + + with self.client: + self.client.set_expected_responses([proto.ButtonRequest(), + proto.PinMatrixRequest(), + proto.Success(), + proto.Features()]) + self.client.apply_settings('new label', 'english') + + self.assertEqual(self.client.features.label, 'new label') + + def test_invalid_language(self): + self.setup_mnemonic_pin_passphrase() + self.assertEqual(self.client.features.language, 'english') + + with self.client: + self.client.set_expected_responses([proto.ButtonRequest(), + proto.PinMatrixRequest(), + proto.Success(), + proto.Features()]) + self.client.apply_settings('new label', 'nonexistent') + + self.assertEqual(self.client.features.language, 'english') + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_msg_estimatetxsize.py b/tests/test_msg_estimatetxsize.py new file mode 100644 index 000000000..85283e84a --- /dev/null +++ b/tests/test_msg_estimatetxsize.py @@ -0,0 +1,34 @@ +import unittest +import common +import binascii + +import trezorlib.messages_pb2 as proto +import trezorlib.types_pb2 as proto_types +from trezorlib.client import CallException + +class TestMsgEstimatetxsize(common.TrezorTest): + def test_estimate_size(self): + self.setup_mnemonic_nopin_nopassphrase() + + inp1 = proto_types.TxInputType(address_n=[0], # 14LmW5k4ssUrtbAB4255zdqv3b4w1TuX9e + # amount=390000, + prev_hash=binascii.unhexlify('d5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882'), + prev_index=0, + ) + + out1 = proto_types.TxOutputType(address='1MJ2tj2ThBE62zXbBYA5ZaN3fdve5CPAz1', + amount=390000 - 10000, + script_type=proto_types.PAYTOADDRESS, + ) + + + est_size = self.client.estimate_tx_size('Bitcoin', [inp1, ], [out1, ]) + self.assertEqual(est_size, 194) + + tx = self.client.simple_sign_tx('Bitcoin', [inp1, ], [out1, ]) + real_size = len(tx) + + self.assertGreaterEqual(est_size, real_size) + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_msg_recoverydevice.py b/tests/test_msg_recoverydevice.py index 1f634c7a5..b2b2ac050 100644 --- a/tests/test_msg_recoverydevice.py +++ b/tests/test_msg_recoverydevice.py @@ -1,12 +1,150 @@ import unittest import common -from trezorlib import messages_pb2 as messages +from trezorlib import messages_pb2 as proto -class TestDeviceRecover(common.TrezorTest): +class TestDeviceRecovery(common.TrezorTest): + def test_pin_passphrase(self): + mnemonic = self.mnemonic12.split(' ') + ret = self.client.call_raw(proto.RecoveryDevice(word_count=12, + passphrase_protection=True, + pin_protection=True, + label='label', + language='english', + enforce_wordlist=True)) - def test_recover_device(self): - pass + + self.assertIsInstance(ret, proto.ButtonRequest) + self.client.debug.press_yes() + ret = self.client.call_raw(proto.ButtonAck()) + + self.assertIsInstance(ret, proto.PinMatrixRequest) + + # Enter PIN for first time + pin_encoded = self.client.debug.encode_pin(self.pin6) + ret = self.client.call_raw(proto.PinMatrixAck(pin=pin_encoded)) + self.assertIsInstance(ret, proto.PinMatrixRequest) + + # Enter PIN for second time + pin_encoded = self.client.debug.encode_pin(self.pin6) + ret = self.client.call_raw(proto.PinMatrixAck(pin=pin_encoded)) + + fakes = 0 + for _ in range(int(12 * 1.5)): + self.assertIsInstance(ret, proto.WordRequest) + word = self.client.debug.read_word() + + if word[1] != 0: + ret = self.client.call_raw(proto.WordAck(word=mnemonic[word[1] - 1])) + mnemonic[word[1] - 1] = None + else: + ret = self.client.call_raw(proto.WordAck(word=word[0])) + fakes += 1 + + print mnemonic + + # Workflow succesfully ended + self.assertIsInstance(ret, proto.Success) + + # 6 expected fake words and all words of mnemonic are used + self.assertEqual(fakes, 6) + self.assertEqual(mnemonic, [None] * 12) + + # Mnemonic is the same + self.client.init_device() + self.assertEqual(self.client.debug.read_mnemonic(), self.mnemonic12) + + self.assertTrue(self.client.features.pin_protection) + self.assertTrue(self.client.features.passphrase_protection) + + # Do passphrase-protected action, PassphraseRequest should be raised + resp = self.client.call_raw(proto.Ping(passphrase_protection=True)) + self.assertIsInstance(resp, proto.PassphraseRequest) + + # Do PIN-protected action, PinRequest should be raised + resp = self.client.call_raw(proto.Ping(pin_protection=True)) + self.assertIsInstance(resp, proto.PinMatrixRequest) + + def test_nopin_nopassphrase(self): + mnemonic = self.mnemonic12.split(' ') + ret = self.client.call_raw(proto.RecoveryDevice(word_count=12, + passphrase_protection=False, + pin_protection=False, + label='label', + language='english', + enforce_wordlist=True)) + + + self.assertIsInstance(ret, proto.ButtonRequest) + self.client.debug.press_yes() + ret = self.client.call_raw(proto.ButtonAck()) + + fakes = 0 + for _ in range(int(12 * 1.5)): + self.assertIsInstance(ret, proto.WordRequest) + word = self.client.debug.read_word() + + if word[1] != 0: + ret = self.client.call_raw(proto.WordAck(word=mnemonic[word[1] - 1])) + mnemonic[word[1] - 1] = None + else: + ret = self.client.call_raw(proto.WordAck(word=word[0])) + fakes += 1 + + print mnemonic + + # Workflow succesfully ended + self.assertIsInstance(ret, proto.Success) + + # 6 expected fake words and all words of mnemonic are used + self.assertEqual(fakes, 6) + self.assertEqual(mnemonic, [None] * 12) + + # Mnemonic is the same + self.client.init_device() + self.assertEqual(self.client.debug.read_mnemonic(), self.mnemonic12) + + self.assertFalse(self.client.features.pin_protection) + self.assertFalse(self.client.features.passphrase_protection) + + # Do passphrase-protected action, PassphraseRequest should NOT be raised + resp = self.client.call_raw(proto.Ping(passphrase_protection=True)) + self.assertIsInstance(resp, proto.Success) + + # Do PIN-protected action, PinRequest should NOT be raised + resp = self.client.call_raw(proto.Ping(pin_protection=True)) + self.assertIsInstance(resp, proto.Success) + + def test_pin_fail(self): + ret = self.client.call_raw(proto.RecoveryDevice(word_count=12, + passphrase_protection=True, + pin_protection=True, + label='label', + language='english', + enforce_wordlist=True)) + + + self.assertIsInstance(ret, proto.ButtonRequest) + self.client.debug.press_yes() + ret = self.client.call_raw(proto.ButtonAck()) + + self.assertIsInstance(ret, proto.PinMatrixRequest) + + # Enter PIN for first time + pin_encoded = self.client.debug.encode_pin(self.pin4) + ret = self.client.call_raw(proto.PinMatrixAck(pin=pin_encoded)) + self.assertIsInstance(ret, proto.PinMatrixRequest) + + # Enter PIN for second time, but different one + pin_encoded = self.client.debug.encode_pin(self.pin6) + ret = self.client.call_raw(proto.PinMatrixAck(pin=pin_encoded)) + + # Failure should be raised + self.assertIsInstance(ret, proto.Failure) + + def test_already_initialized(self): + self.setup_mnemonic_nopin_nopassphrase() + self.assertRaises(Exception, self.client.recovery_device, 12, False, False, 'label', 'english') if __name__ == '__main__': unittest.main() diff --git a/tests/test_msg_resetdevice.py b/tests/test_msg_resetdevice.py index 9ddc8665d..1aa1aedab 100644 --- a/tests/test_msg_resetdevice.py +++ b/tests/test_msg_resetdevice.py @@ -34,7 +34,6 @@ def generate_entropy(strength, internal_entropy, external_entropy): return entropy_stripped class TestDeviceReset(common.TrezorTest): - def test_reset_device(self): # No PIN, no passphrase external_entropy = 'zlutoucky kun upel divoke ody' * 2 @@ -170,6 +169,41 @@ class TestDeviceReset(common.TrezorTest): # Do PIN-protected action, PinRequest should be raised resp = self.client.call_raw(proto.Ping(pin_protection=True)) self.assertIsInstance(resp, proto.PinMatrixRequest) + + def test_failed_pin(self): + external_entropy = 'zlutoucky kun upel divoke ody' * 2 + strength = 128 + + ret = self.client.call_raw(proto.ResetDevice(display_random=True, + strength=strength, + passphrase_protection=True, + pin_protection=True, + language='english', + label='test')) + + self.assertIsInstance(ret, proto.ButtonRequest) + self.client.debug.press_yes() + ret = self.client.call_raw(proto.ButtonAck()) + + self.assertIsInstance(ret, proto.EntropyRequest) + ret = self.client.call_raw(proto.EntropyAck(entropy=external_entropy)) + + self.assertIsInstance(ret, proto.PinMatrixRequest) + + # Enter PIN for first time + pin_encoded = self.client.debug.encode_pin(self.pin4) + ret = self.client.call_raw(proto.PinMatrixAck(pin=pin_encoded)) + self.assertIsInstance(ret, proto.PinMatrixRequest) + + # Enter PIN for second time + pin_encoded = self.client.debug.encode_pin(self.pin6) + ret = self.client.call_raw(proto.PinMatrixAck(pin=pin_encoded)) + + self.assertIsInstance(ret, proto.Failure) + def test_already_initialized(self): + self.setup_mnemonic_nopin_nopassphrase() + self.assertRaises(Exception, self.client.reset_device, False, 128, True, True, 'label', 'english') + if __name__ == '__main__': unittest.main() diff --git a/tests/test_msg_simplesigntx.py b/tests/test_msg_simplesigntx.py index f2e2f37fd..19c217fc7 100644 --- a/tests/test_msg_simplesigntx.py +++ b/tests/test_msg_simplesigntx.py @@ -36,7 +36,7 @@ class FakeTestnetBlockchain(object): return t -class TestSignTx(common.TrezorTest): +class TestMsgSimplesigntx(common.TrezorTest): def test_one_one_fee(self): self.setup_mnemonic_nopin_nopassphrase() @@ -58,6 +58,7 @@ class TestSignTx(common.TrezorTest): with self.client: msg = self.client._prepare_simple_sign_tx('Bitcoin', [inp1, ], [out1, ]) self.client.set_expected_responses([proto.ButtonRequest(code=proto_types.ButtonRequest_ConfirmOutput), + proto.ButtonRequest(code=proto_types.ButtonRequest_SignTx), proto.TxRequest(request_index=-1)]) tx = self.client.call(msg) @@ -90,6 +91,7 @@ class TestSignTx(common.TrezorTest): msg = self.client._prepare_simple_sign_tx('Testnet', [inp1, ], [out1, out2]) self.client.set_expected_responses([proto.ButtonRequest(code=proto_types.ButtonRequest_ConfirmOutput), proto.ButtonRequest(code=proto_types.ButtonRequest_ConfirmOutput), + proto.ButtonRequest(code=proto_types.ButtonRequest_SignTx), proto.TxRequest()]) tx = self.client.call(msg) @@ -122,6 +124,7 @@ class TestSignTx(common.TrezorTest): self.client.set_expected_responses([proto.ButtonRequest(code=proto_types.ButtonRequest_ConfirmOutput), proto.ButtonRequest(code=proto_types.ButtonRequest_ConfirmOutput), proto.ButtonRequest(code=proto_types.ButtonRequest_FeeOverThreshold), + proto.ButtonRequest(code=proto_types.ButtonRequest_SignTx), proto.TxRequest()]) tx = self.client.call(msg) @@ -153,6 +156,7 @@ class TestSignTx(common.TrezorTest): msg = self.client._prepare_simple_sign_tx('Bitcoin', [inp1, ], [out1, out2]) self.client.set_expected_responses([proto.ButtonRequest(code=proto_types.ButtonRequest_ConfirmOutput), proto.ButtonRequest(code=proto_types.ButtonRequest_ConfirmOutput), + proto.ButtonRequest(code=proto_types.ButtonRequest_SignTx), proto.TxRequest()]) tx = self.client.call(msg) @@ -190,6 +194,7 @@ class TestSignTx(common.TrezorTest): self.client.set_expected_responses([proto.ButtonRequest(code=proto_types.ButtonRequest_ConfirmOutput), proto.ButtonRequest(code=proto_types.ButtonRequest_ConfirmOutput), proto.ButtonRequest(code=proto_types.ButtonRequest_ConfirmOutput), + proto.ButtonRequest(code=proto_types.ButtonRequest_SignTx), proto.TxRequest()]) tx = self.client.call(msg) @@ -229,6 +234,7 @@ class TestSignTx(common.TrezorTest): msg = self.client._prepare_simple_sign_tx('Bitcoin', [inp1, inp2], [out1, out2]) self.client.set_expected_responses([proto.ButtonRequest(code=proto_types.ButtonRequest_ConfirmOutput), proto.ButtonRequest(code=proto_types.ButtonRequest_ConfirmOutput), + proto.ButtonRequest(code=proto_types.ButtonRequest_SignTx), proto.TxRequest()]) tx = self.client.call(msg) @@ -269,7 +275,8 @@ class TestSignTx(common.TrezorTest): with self.client: msg = self.client._prepare_simple_sign_tx('Bitcoin', [inp1, inp2], outputs) self.client.set_expected_responses([proto.ButtonRequest(code=proto_types.ButtonRequest_ConfirmOutput), ] * 255 + \ - [proto.TxRequest(), ]) + [proto.ButtonRequest(code=proto_types.ButtonRequest_SignTx), + proto.TxRequest(), ]) tx = self.client.call(msg) self.assertEqual(binascii.hexlify(tx.serialized_tx), '0100000002fb792f470a58993e14964c9bd46cdf37cb4bbc3f61540cb651580c82ed243ec6010000006b483045022100969da46f94a81f34f3717b014e0c3e1826eda1b0022ec2f9ce39f3d750ab9235022026da269770993211a1503413566a339bbb4389a482fffcf8e1f76713fc3b94f5012103477b9f0f34ae85434ce795f0c5e1e90c9420e5b5fad084d7cce9a487b94a7902ffffffffe56582d2119100cb1d3da8232291e053f71e25fb669c87b32a667749959ea239010000006a473044022052e1419bb237b9db400ab5e3df16db6355619d545fde9030924a360763ae9ad40220704beab04d72ecaeb42eca7d98faca7a0941e65f2e1341f183be2b83e6b09e1c012103477b9f0f34ae85434ce795f0c5e1e90c9420e5b5fad084d7cce9a487b94a7902fffffffffdff00d8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088acd8270000000000001976a914f0a2b64e56ee2ff57126232f84af6e3a41d4055088ac00000000') @@ -296,6 +303,7 @@ class TestSignTx(common.TrezorTest): msg = self.client._prepare_simple_sign_tx('Bitcoin', [inp1, ], [out1, ]) self.client.set_expected_responses([proto.ButtonRequest(code=proto_types.ButtonRequest_ConfirmOutput), proto.ButtonRequest(code=proto_types.ButtonRequest_FeeOverThreshold), + proto.ButtonRequest(code=proto_types.ButtonRequest_SignTx), proto.TxRequest()]) tx = self.client.call(msg) @@ -330,29 +338,6 @@ class TestSignTx(common.TrezorTest): else: self.assert_(False, "types.Failure_NotEnoughFunds expected") - def test_estimate_size(self): - self.setup_mnemonic_nopin_nopassphrase() - - inp1 = proto_types.TxInputType(address_n=[0], # 14LmW5k4ssUrtbAB4255zdqv3b4w1TuX9e - # amount=390000, - prev_hash=binascii.unhexlify('d5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882'), - prev_index=0, - ) - - out1 = proto_types.TxOutputType(address='1MJ2tj2ThBE62zXbBYA5ZaN3fdve5CPAz1', - amount=390000 - 10000, - script_type=proto_types.PAYTOADDRESS, - ) - - - est_size = self.client.estimate_tx_size('Bitcoin', [inp1, ], [out1, ]) - self.assertEqual(est_size, 194) - - tx = self.client.simple_sign_tx('Bitcoin', [inp1, ], [out1, ]) - real_size = len(tx) - - self.assertGreaterEqual(est_size, real_size) - ''' def test_simplesigntx_testnet(self): self.client.load_device_by_xprv('xprv9s21ZrQH143K3zttRjiQmYwyugvd13pnd2VzefWrfSouRfnj5oSkJgBQXxtn18E9mqrDop7fQ8Xnb9JCLPE4vghzhpU4dT33ZJ7frjzTEW8', diff --git a/tests/test_protection_levels.py b/tests/test_protection_levels.py new file mode 100644 index 000000000..bcf9b3266 --- /dev/null +++ b/tests/test_protection_levels.py @@ -0,0 +1,166 @@ +import unittest +import common +import binascii + +from trezorlib import messages_pb2 as proto +from trezorlib import types_pb2 as proto_types + +class TestProtectionLevels(common.TrezorTest): + # Tests protection levels of all messages as defined here: + # https://github.com/trezor/documentation/wiki/Protection-Levels + + def test_initialize(self): + with self.client: + self.setup_mnemonic_pin_passphrase() + self.client.set_expected_responses([proto.Features()]) + self.client.init_device() + + def test_apply_settings(self): + with self.client: + self.setup_mnemonic_pin_passphrase() + self.client.set_expected_responses([proto.ButtonRequest(), + proto.PinMatrixRequest(), + proto.Success(), + proto.Features()]) # TrezorClient reinitializes device + self.client.apply_settings('nazdar', 'english') + + def test_change_pin(self): + with self.client: + self.setup_mnemonic_pin_passphrase() + self.client.set_expected_responses([proto.ButtonRequest(), + proto.PinMatrixRequest(), + proto.PinMatrixRequest(), + proto.PinMatrixRequest(), + proto.Success(), + proto.Features()]) + self.client.change_pin() + + def test_ping(self): + with self.client: + self.setup_mnemonic_pin_passphrase() + self.client.set_expected_responses([proto.ButtonRequest(), + proto.PinMatrixRequest(), + proto.PassphraseRequest(), + proto.Success()]) + self.client.ping('msg', True, True, True) + + def test_get_entropy(self): + with self.client: + self.setup_mnemonic_pin_passphrase() + self.client.set_expected_responses([proto.ButtonRequest(), + proto.Entropy()]) + self.client.get_entropy(10) + + def test_get_public_key(self): + with self.client: + self.setup_mnemonic_pin_passphrase() + self.client.set_expected_responses([proto.PassphraseRequest(), + proto.PublicKey()]) + self.client.get_public_node([]) + + def test_get_address(self): + with self.client: + self.setup_mnemonic_pin_passphrase() + self.client.set_expected_responses([proto.PassphraseRequest(), + proto.Address()]) + self.client.get_address('Bitcoin', []) + + def test_wipe_device(self): + with self.client: + self.setup_mnemonic_pin_passphrase() + self.client.set_expected_responses([proto.ButtonRequest(), + proto.Success(), + proto.Features()]) + self.client.wipe_device() + + def test_load_device(self): + with self.client: + self.client.set_expected_responses([proto.ButtonRequest(), + proto.Success(), + proto.Features()]) + self.client.load_device_by_mnemonic('this is mnemonic', '1234', True, 'label', 'english', skip_checksum=True) + + # This must fail, because device is already initialized + self.assertRaises(Exception, self.client.load_device_by_mnemonic, + 'this is mnemonic', '1234', True, 'label', 'english', skip_checksum=True) + + def test_reset_device(self): + with self.client: + self.client.set_expected_responses([proto.ButtonRequest(), + proto.EntropyRequest()] +\ + [proto.ButtonRequest()] * 24 + \ + [proto.Success(), + proto.Features()]) + self.client.reset_device(False, 128, True, False, 'label', 'english') + + # This must fail, because device is already initialized + self.assertRaises(Exception, self.client.reset_device, False, 128, True, False, 'label', 'english') + + def test_recovery_device(self): + with self.client: + self.client.set_mnemonic(self.mnemonic12) + self.client.set_expected_responses([proto.ButtonRequest(), ] + \ + [proto.WordRequest()] * int(12 * 1.5) + \ + [proto.Success(), + proto.Features()]) + self.client.recovery_device(12, False, False, 'label', 'english') + + # This must fail, because device is already initialized + self.assertRaises(Exception, self.client.recovery_device, 12, False, False, 'label', 'english') + + def test_sign_message(self): + with self.client: + self.setup_mnemonic_pin_passphrase() + self.client.set_expected_responses([proto.ButtonRequest(), + proto.PinMatrixRequest(), + proto.PassphraseRequest(), + proto.MessageSignature()]) + self.client.sign_message('Bitcoin', [], 'testing message') + + def test_verify_message(self): + with self.client: + self.setup_mnemonic_pin_passphrase() + self.client.set_expected_responses([proto.Success()]) + self.client.verify_message( + '14LmW5k4ssUrtbAB4255zdqv3b4w1TuX9e', + binascii.unhexlify('209e23edf0e4e47ff1dec27f32cd78c50e74ef018ee8a6adf35ae17c7a9b0dd96f48b493fd7dbab03efb6f439c6383c9523b3bbc5f1a7d158a6af90ab154e9be80'), + 'This is an example of a signed message.') + + def test_estimate_txsize(self): + with self.client: + self.setup_mnemonic_pin_passphrase() + self.client.set_expected_responses([proto.TxSize()]) + self.client.estimate_tx_size('Bitcoin', [], []) + + #def test_signtx(self): + # pass + + def test_simplesigntx(self): + self.setup_mnemonic_pin_passphrase() + + inp1 = proto_types.TxInputType(address_n=[0], # 14LmW5k4ssUrtbAB4255zdqv3b4w1TuX9e + prev_hash=binascii.unhexlify('d5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882'), + prev_index=0, + ) + + out1 = proto_types.TxOutputType(address='1MJ2tj2ThBE62zXbBYA5ZaN3fdve5CPAz1', + amount=390000 - 10000, + script_type=proto_types.PAYTOADDRESS, + ) + + with self.client: + self.client.set_expected_responses([proto.PinMatrixRequest(), + proto.PassphraseRequest(), + proto.ButtonRequest(code=proto_types.ButtonRequest_ConfirmOutput), + proto.ButtonRequest(code=proto_types.ButtonRequest_SignTx), + proto.TxRequest(request_index=-1)]) + self.client.simple_sign_tx('Bitcoin', [inp1, ], [out1, ]) + + # def test_firmware_erase(self): + # pass + + # def test_firmware_upload(self): + # pass + +if __name__ == '__main__': + unittest.main()