1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-13 19:18:56 +00:00

Unit tests of recovery device, apply settings

This commit is contained in:
slush0 2014-02-22 02:15:21 +01:00
parent 89d17297ce
commit 8e97281f29
7 changed files with 430 additions and 41 deletions

View File

@ -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

View File

@ -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()

View File

@ -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()

View File

@ -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()

View File

@ -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()

File diff suppressed because one or more lines are too long

View File

@ -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()