1
- # -*- coding: utf-8 -*-
1
+ import pytest
2
2
3
- import io
4
- import sys
5
- import unittest
6
3
from smartcard .ATR import ATR
7
4
from smartcard .Exceptions import SmartcardException
8
5
from smartcard .util import toBytes
9
6
10
7
11
- class TestUtil (unittest .TestCase ):
12
-
13
- def setUp (self ):
14
- self .held , sys .stdout = sys .stdout , io .StringIO ()
15
-
16
- def test_ATR1 (self ):
17
- atr = [0x3F , 0x65 , 0x25 , 0x00 , 0x2C , 0x09 , 0x69 , 0x90 , 0x00 ]
18
- data_out = """TB1: 25
8
+ def test_atr1 (capsys ):
9
+ atr = [0x3F , 0x65 , 0x25 , 0x00 , 0x2C , 0x09 , 0x69 , 0x90 , 0x00 ]
10
+ data_out = """TB1: 25
19
11
TC1: 0
20
12
supported protocols T=0
21
13
T=0 supported: True
22
14
T=1 supported: False
23
- clock rate conversion factor: 372
24
- bit rate adjustment factor: 1
25
- maximum programming current: 50
26
- programming voltage: 30
27
- guard time: 0
15
+ \t clock rate conversion factor: 372
16
+ \t bit rate adjustment factor: 1
17
+ \t maximum programming current: 50
18
+ \t programming voltage: 30
19
+ \t guard time: 0
28
20
nb of interface bytes: 2
29
21
nb of historical bytes: 5
30
22
"""
31
- a = ATR (atr )
32
- a .dump ()
33
- output = sys .stdout .getvalue ()
34
- self .assertEqual (output , data_out )
35
-
36
- def test_ATR2 (self ):
37
- atr = [0x3F , 0x65 , 0x25 , 0x08 , 0x93 , 0x04 , 0x6C , 0x90 , 0x00 ]
38
- data_out = """TB1: 25
23
+ a = ATR (atr )
24
+ a .dump ()
25
+ stdout , _ = capsys .readouterr ()
26
+ assert stdout == data_out
27
+
28
+
29
+ def test_atr2 (capsys ):
30
+ atr = [0x3F , 0x65 , 0x25 , 0x08 , 0x93 , 0x04 , 0x6C , 0x90 , 0x00 ]
31
+ data_out = """TB1: 25
39
32
TC1: 8
40
33
supported protocols T=0
41
34
T=0 supported: True
42
35
T=1 supported: False
43
- clock rate conversion factor: 372
44
- bit rate adjustment factor: 1
45
- maximum programming current: 50
46
- programming voltage: 30
47
- guard time: 8
36
+ \t clock rate conversion factor: 372
37
+ \t bit rate adjustment factor: 1
38
+ \t maximum programming current: 50
39
+ \t programming voltage: 30
40
+ \t guard time: 8
48
41
nb of interface bytes: 2
49
42
nb of historical bytes: 5
50
43
"""
51
- a = ATR (atr )
52
- a .dump ()
53
- output = sys .stdout .getvalue ()
54
- self .assertEqual (output , data_out )
55
-
56
- def test_ATR3 (self ):
57
- atr = [0x3B , 0x16 , 0x94 , 0x7C , 0x03 , 0x01 , 0x00 , 0x00 , 0x0D ]
58
- data_out = """TA1: 94
44
+ a = ATR (atr )
45
+ a .dump ()
46
+
47
+ stdout , _ = capsys .readouterr ()
48
+ assert stdout == data_out
49
+
50
+
51
+ def test_atr3 (capsys ):
52
+ atr = [0x3B , 0x16 , 0x94 , 0x7C , 0x03 , 0x01 , 0x00 , 0x00 , 0x0D ]
53
+ data_out = """TA1: 94
59
54
supported protocols T=0
60
55
T=0 supported: True
61
56
T=1 supported: False
62
- clock rate conversion factor: 512
63
- bit rate adjustment factor: 8
64
- maximum programming current: 50
65
- programming voltage: 5
66
- guard time: None
57
+ \t clock rate conversion factor: 512
58
+ \t bit rate adjustment factor: 8
59
+ \t maximum programming current: 50
60
+ \t programming voltage: 5
61
+ \t guard time: None
67
62
nb of interface bytes: 1
68
63
nb of historical bytes: 6
69
64
"""
70
- a = ATR (atr )
71
- a .dump ()
72
- output = sys .stdout .getvalue ()
73
- self .assertEqual (output , data_out )
74
-
75
- def test_ATR4 (self ):
76
- atr = [0x3B , 0x65 , 0x00 , 0x00 , 0x9C , 0x11 , 0x01 , 0x01 , 0x03 ]
77
- data_out = """TB1: 0
65
+ a = ATR (atr )
66
+ a .dump ()
67
+ stdout , _ = capsys .readouterr ()
68
+ assert stdout == data_out
69
+
70
+
71
+ def test_atr4 (capsys ):
72
+ atr = [0x3B , 0x65 , 0x00 , 0x00 , 0x9C , 0x11 , 0x01 , 0x01 , 0x03 ]
73
+ data_out = """TB1: 0
78
74
TC1: 0
79
75
supported protocols T=0
80
76
T=0 supported: True
81
77
T=1 supported: False
82
- clock rate conversion factor: 372
83
- bit rate adjustment factor: 1
84
- maximum programming current: 25
85
- programming voltage: 5
86
- guard time: 0
78
+ \t clock rate conversion factor: 372
79
+ \t bit rate adjustment factor: 1
80
+ \t maximum programming current: 25
81
+ \t programming voltage: 5
82
+ \t guard time: 0
87
83
nb of interface bytes: 2
88
84
nb of historical bytes: 5
89
85
"""
90
- a = ATR (atr )
91
- a .dump ()
92
- output = sys . stdout . getvalue ()
93
- self . assertEqual ( output , data_out )
94
-
95
- def test_ATR5 ( self ):
96
- atr = [ 0x3B , 0xE3 , 0x00 , 0xFF , 0x81 , 0x31 , 0x52 , 0x45 , 0xA1 ,
97
- 0xA2 , 0xA3 , 0x1B ]
98
- data_out = """TB1: 0
86
+ a = ATR (atr )
87
+ a .dump ()
88
+ stdout , _ = capsys . readouterr ()
89
+ assert stdout == data_out
90
+
91
+
92
+ def test_atr5 ( capsys ):
93
+ atr = [ 0x3B , 0xE3 , 0x00 , 0xFF , 0x81 , 0x31 , 0x52 , 0x45 , 0xA1 , 0xA2 , 0xA3 , 0x1B ]
94
+ data_out = """TB1: 0
99
95
TC1: ff
100
96
TD1: 81
101
97
TD2: 31
@@ -105,23 +101,23 @@ def test_ATR5(self):
105
101
T=0 supported: False
106
102
T=1 supported: True
107
103
checksum: 27
108
- clock rate conversion factor: 372
109
- bit rate adjustment factor: 1
110
- maximum programming current: 25
111
- programming voltage: 5
112
- guard time: 255
104
+ \t clock rate conversion factor: 372
105
+ \t bit rate adjustment factor: 1
106
+ \t maximum programming current: 25
107
+ \t programming voltage: 5
108
+ \t guard time: 255
113
109
nb of interface bytes: 6
114
110
nb of historical bytes: 3
115
111
"""
116
- a = ATR (atr )
117
- a .dump ()
118
- output = sys . stdout . getvalue ()
119
- self . assertEqual ( output , data_out )
120
-
121
- def test_ATR6 ( self ):
122
- atr = [ 0x3B , 0xE5 , 0x00 , 0x00 , 0x81 , 0x21 , 0x45 , 0x9C , 0x10 ,
123
- 0x01 , 0x00 , 0x80 , 0x0D ]
124
- data_out = """TB1: 0
112
+ a = ATR (atr )
113
+ a .dump ()
114
+ stdout , _ = capsys . readouterr ()
115
+ assert stdout == data_out
116
+
117
+
118
+ def test_atr6 ( capsys ):
119
+ atr = [ 0x3B , 0xE5 , 0x00 , 0x00 , 0x81 , 0x21 , 0x45 , 0x9C , 0x10 , 0x01 , 0x00 , 0x80 , 0x0D ]
120
+ data_out = """TB1: 0
125
121
TC1: 0
126
122
TD1: 81
127
123
TD2: 21
@@ -130,34 +126,33 @@ def test_ATR6(self):
130
126
T=0 supported: False
131
127
T=1 supported: True
132
128
checksum: 13
133
- clock rate conversion factor: 372
134
- bit rate adjustment factor: 1
135
- maximum programming current: 25
136
- programming voltage: 5
137
- guard time: 0
129
+ \t clock rate conversion factor: 372
130
+ \t bit rate adjustment factor: 1
131
+ \t maximum programming current: 25
132
+ \t programming voltage: 5
133
+ \t guard time: 0
138
134
nb of interface bytes: 5
139
135
nb of historical bytes: 5
140
136
"""
141
- a = ATR (atr )
142
- a .dump ()
143
- output = sys .stdout .getvalue ()
144
- self .assertEqual (output , data_out )
145
-
146
- def test_ATR_TS (self ):
147
- atr = [0x42 ]
148
- with self .assertRaises (SmartcardException ):
149
- ATR (atr )
150
-
151
- def test_ATR_get (self ):
152
- atr = "3B F2 95 12 34 01 36 06"
153
- a = ATR (toBytes (atr ))
154
- self .assertEqual (a .getTA1 (), 0x95 )
155
- self .assertEqual (a .getTB1 (), 0x12 )
156
- self .assertEqual (a .getTC1 (), 0x34 )
157
- self .assertEqual (a .getTD1 (), 0x01 )
158
- self .assertEqual (a .getHistoricalBytes (), [0x36 , 0x06 ])
159
- self .assertFalse (a .isT15Supported ())
160
- self .assertEqual (str (a ), atr )
161
-
162
- if __name__ == '__main__' :
163
- unittest .main (buffer = True )
137
+ a = ATR (atr )
138
+ a .dump ()
139
+ stdout , _ = capsys .readouterr ()
140
+ assert stdout == data_out
141
+
142
+
143
+ def test_atr_ts ():
144
+ atr = [0x42 ]
145
+ with pytest .raises (SmartcardException ):
146
+ ATR (atr )
147
+
148
+
149
+ def test_atr_get ():
150
+ atr = "3B F2 95 12 34 01 36 06"
151
+ a = ATR (toBytes (atr ))
152
+ assert a .getTA1 () == 0x95
153
+ assert a .getTB1 () == 0x12
154
+ assert a .getTC1 () == 0x34
155
+ assert a .getTD1 () == 0x01
156
+ assert a .getHistoricalBytes (), [0x36 == 0x06 ]
157
+ assert a .isT15Supported () is False
158
+ assert str (a ) == atr
0 commit comments