-
Notifications
You must be signed in to change notification settings - Fork 2
/
testAPDU.py
108 lines (85 loc) · 3.81 KB
/
testAPDU.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import unittest, random
from pythoncard.framework import APDU, ISO7816
class testAPDU(unittest.TestCase):
def testState(self):
apdu = APDU([0x00, 0x20, 0x01, 0x00, 0x04, 0x31, 0x32, 0x33, 0x34, 0x00])
self.assertEquals(APDU.STATE_INITIAL, apdu.getCurrentState())
apdu.setIncomingAndReceive()
self.assertTrue(APDU.STATE_PARTIAL_INCOMING <= apdu.getCurrentState())
apdu.receiveBytes(ISO7816.OFFSET_CDATA)
self.assertEquals(APDU.STATE_FULL_INCOMING, apdu.getCurrentState())
apdu.setOutgoing()
self.assertEquals(APDU.STATE_OUTGOING, apdu.getCurrentState())
apdu.setOutgoingLength(10)
self.assertEquals(APDU.STATE_OUTGOING_LENGTH_KNOWN, apdu.getCurrentState())
apdu.sendBytes(0, 2)
self.assertEquals(APDU.STATE_PARTIAL_OUTGOING, apdu.getCurrentState())
apdu.sendBytes(0, 8)
self.assertEquals(APDU.STATE_FULL_OUTGOING, apdu.getCurrentState())
def testBuffer(self):
apdu = APDU([0x00, 0x20, 0x01, 0x00, 0x04, 0x31, 0x32, 0x33, 0x34])
buffer = apdu.getBuffer()
self.assertEquals(0x00, buffer[ISO7816.OFFSET_CLA])
self.assertEquals(0x20, buffer[ISO7816.OFFSET_INS])
self.assertEquals(0x01, buffer[ISO7816.OFFSET_P1])
self.assertEquals(0x00, buffer[ISO7816.OFFSET_CLA])
buffer[0] = 1; buffer[1] = 2; buffer[2] = 3
buff = apdu.getBuffer()
self.assertEquals(1, buff[0])
self.assertEquals(2, buff[1])
self.assertEquals(3, buff[2])
def testAPDUDoc(self):
""" This is an adaptation of the piece of code on the APDU page """
apdu = APDU([0x00, 0x20, 0x01, 0x00, 0x04, 0x31, 0x32, 0x33, 0x34, 0x00])
buffer = apdu.getBuffer()
cla = buffer[ISO7816.OFFSET_CLA]
self.assertEquals(0, cla)
ins = buffer[ISO7816.OFFSET_INS]
self.assertEquals(0x20, ins)
# assume this command has incoming data
# Lc tells us the incoming apdu command length
bytesLeft = buffer[ISO7816.OFFSET_LC]
self.assertEquals(4, bytesLeft)
readCount = apdu.setIncomingAndReceive()
self.assertEquals(4, readCount)
while bytesLeft > 0:
# process bytes in buffer[5] to buffer[readCount+4];
bytesLeft -= readCount
readCount = apdu.receiveBytes ( ISO7816.OFFSET_CDATA )
# Note that for a short response as in the case illustrated here
# the three APDU method calls shown : setOutgoing(),setOutgoingLength() & sendBytes()
# could be replaced by one APDU method call : setOutgoingAndSend().
# construct the reply APDU
le = apdu.setOutgoing()
apdu.setOutgoingLength( 3 )
# build response data in apdu.buffer[ 0.. outCount-1 ];
buffer[0] = 1; buffer[1] = 2; buffer[3] = 3
apdu.sendBytes ( 0 , 3 )
self.assertEquals(APDU.STATE_FULL_OUTGOING,
apdu.getCurrentState())
def testExtAPDULength(self):
pass
def testReceiveData(self):
def receiveData(apdu):
buffer = apdu.getBuffer()
LC = apdu.getIncomingLength()
recvLen = apdu.setIncomingAndReceive()
dataOffset = apdu.getOffsetCdata()
while recvLen > 0:
# process data in buffer[dataOffset]
recvLen = apdu.receiveBytes(dataOffset)
# done
pass
def testSendData(self):
def sendData(apdu):
buffer = apdu.getBuffer()
LE = apdu.setOutgoing()
toSend = random.randint(0,65535)
if LE != toSend:
apdu.setOutgoingLength(toSend)
while toSend > 0:
# prepare data to send in APDU buffer
apdu.sendBytes(dataOffset, sentLen)
toSend -= sentLen
# done
pass