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