-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_error_cmd.py
57 lines (46 loc) · 2.42 KB
/
test_error_cmd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import pytest
from ragger.error import ExceptionRAPDU
from application_client.boilerplate_command_sender import CLA, InsType, P1, P2, Errors
# Ensure the app returns an error when a bad CLA is used
def test_bad_cla(backend):
with pytest.raises(ExceptionRAPDU) as e:
backend.exchange(cla=CLA + 1, ins=InsType.GET_VERSION)
assert e.value.status == Errors.SW_CLA_NOT_SUPPORTED
# Ensure the app returns an error when a bad INS is used
def test_bad_ins(backend):
with pytest.raises(ExceptionRAPDU) as e:
backend.exchange(cla=CLA, ins=0xff)
assert e.value.status == Errors.SW_INS_NOT_SUPPORTED
# Ensure the app returns an error when a bad P1 or P2 is used
def test_wrong_p1p2(backend):
with pytest.raises(ExceptionRAPDU) as e:
backend.exchange(cla=CLA, ins=InsType.GET_VERSION, p1=P1.P1_START + 1, p2=P2.P2_LAST)
assert e.value.status == Errors.SW_WRONG_P1P2
with pytest.raises(ExceptionRAPDU) as e:
backend.exchange(cla=CLA, ins=InsType.GET_VERSION, p1=P1.P1_START, p2=P2.P2_MORE)
assert e.value.status == Errors.SW_WRONG_P1P2
with pytest.raises(ExceptionRAPDU) as e:
backend.exchange(cla=CLA, ins=InsType.GET_APP_NAME, p1=P1.P1_START + 1, p2=P2.P2_LAST)
assert e.value.status == Errors.SW_WRONG_P1P2
with pytest.raises(ExceptionRAPDU) as e:
backend.exchange(cla=CLA, ins=InsType.GET_APP_NAME, p1=P1.P1_START, p2=P2.P2_MORE)
assert e.value.status == Errors.SW_WRONG_P1P2
# Ensure the app returns an error when a bad data length is used
def test_wrong_data_length(backend):
# APDUs must be at least 4 bytes: CLA, INS, P1, P2.
with pytest.raises(ExceptionRAPDU) as e:
backend.exchange_raw(bytes.fromhex("E00300"))
assert e.value.status == Errors.SW_WRONG_DATA_LENGTH
# APDUs advertises a too long length
with pytest.raises(ExceptionRAPDU) as e:
backend.exchange_raw(bytes.fromhex("E003000005"))
assert e.value.status == Errors.SW_WRONG_DATA_LENGTH
# Ensure there is no state confusion when trying wrong APDU sequences
def test_invalid_state(backend):
with pytest.raises(ExceptionRAPDU) as e:
backend.exchange(cla=CLA,
ins=InsType.SIGN_TX,
p1=P1.P1_START + 1, # Try to continue a flow instead of start a new one
p2=P2.P2_MORE,
data=b"abcde") # data is not parsed in this case
assert e.value.status == Errors.SW_BAD_STATE