add APDUResponse class and test

This commit is contained in:
Andrea Franz 2018-08-24 13:22:47 +02:00
parent dfdc61bb5e
commit 9e2596f835
No known key found for this signature in database
GPG Key ID: 4F0D2F2D9DE7F29D
2 changed files with 76 additions and 0 deletions

View File

@ -0,0 +1,53 @@
package im.status.applet_installer_test.appletinstaller;
public class APDUResponse {
public static int SW_OK = 0x9000;
public static int SW_SECURITY_CONDITION_NOT_SATISFIED = 0x6982;
public static int SW_AUTHENTICATION_METHOD_BLOCKED = 0x6983;
private byte[] apdu;
private byte[] data;
private int sw;
private int sw1;
private int sw2;
public APDUResponse(byte[] apdu) {
if (apdu.length < 2) {
throw new IllegalArgumentException("APDU response must be at least 2 bytes");
}
this.apdu = apdu;
this.parse();
}
private void parse() {
int length = this.apdu.length;
this.sw1 = this.apdu[length - 2] & 0xff;
this.sw2 = this.apdu[length - 1] & 0xff;
this.sw = (this.sw1 << 8) | this.sw2;
this.data = new byte[length - 2];
System.arraycopy(this.apdu, 0, this.data, 0, length - 2);
}
public boolean isOK() {
return this.sw == SW_OK;
}
public byte[] getData() {
return this.data;
}
public int getSw() {
return this.sw;
}
public int getSw1() {
return this.sw1;
}
public int getSw2() {
return this.sw2;
}
}

View File

@ -0,0 +1,23 @@
package im.status.applet_installer_test.appletinstaller;
import org.junit.Test;
import java.io.IOException;
import static org.junit.Assert.*;
public class APDUResponseTest {
@Test
public void parsing() {
byte[] apdu = HexUtils.hexStringToByteArray("000002650183039536622002003b5e508f751c0af3016e3fbc23d3a69000");
APDUResponse resp = new APDUResponse(apdu);
assertEquals(0x9000, resp.getSw());
assertEquals(0x90, resp.getSw1());
assertEquals(0x00, resp.getSw2());
String expected = "000002650183039536622002003B5E508F751C0AF3016E3FBC23D3A6";
assertEquals(expected, HexUtils.byteArrayToHexString(resp.getData()));
assertTrue(resp.isOK());
}
}