From 0337ca552e34ec7d2f79d32ebe4b0c8171231b0d Mon Sep 17 00:00:00 2001 From: Joeri Date: Thu, 30 Nov 2017 21:30:54 +0100 Subject: [PATCH] Added some first unit tests --- .gitmodules | 2 +- build.xml | 54 ++++++++++++++++++-- {ext => lib}/oracle_javacard_sdks | 0 test/openpgpcard/OpenPGPAppletTest.java | 66 +++++++++++++++++++++++++ 4 files changed, 116 insertions(+), 6 deletions(-) rename {ext => lib}/oracle_javacard_sdks (100%) create mode 100644 test/openpgpcard/OpenPGPAppletTest.java diff --git a/.gitmodules b/.gitmodules index 00dfd0b..da6dff5 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "ext/oracle_javacard_sdks"] - path = ext/oracle_javacard_sdks + path = lib/oracle_javacard_sdks url = https://github.com/martinpaljak/oracle_javacard_sdks.git diff --git a/build.xml b/build.xml index 9cb0891..0ac8d10 100644 --- a/build.xml +++ b/build.xml @@ -1,14 +1,29 @@ + + + + + - - - + + + + + + + + + + + + + - + - + @@ -16,4 +31,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ext/oracle_javacard_sdks b/lib/oracle_javacard_sdks similarity index 100% rename from ext/oracle_javacard_sdks rename to lib/oracle_javacard_sdks diff --git a/test/openpgpcard/OpenPGPAppletTest.java b/test/openpgpcard/OpenPGPAppletTest.java new file mode 100644 index 0000000..6dee3e4 --- /dev/null +++ b/test/openpgpcard/OpenPGPAppletTest.java @@ -0,0 +1,66 @@ +package openpgpcard; + +import static org.junit.Assert.assertArrayEquals; + +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import com.licel.jcardsim.base.Simulator; + +import javacard.framework.AID; + +import openpgpcard.OpenPGPApplet; + +public class OpenPGPAppletTest { + static private byte[] appletAID = new byte[] {(byte)0xD2, 0x76, 0x00, 0x01, 0x24, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00}; + static private AID aid = new AID(appletAID, (short)0, (byte)appletAID.length); + static private byte[] bArray = new byte[appletAID.length + 1]; + + Simulator simulator; + + @BeforeClass + public static void setUpClass() { + bArray[0] = (byte)appletAID.length; + System.arraycopy(appletAID, 0, bArray, 1, appletAID.length); + } + + @Before + public void setUp() { + // Set up the Java Card simulator and install the applet + simulator = new Simulator(); + simulator.resetRuntime(); + simulator.installApplet(aid, OpenPGPApplet.class, bArray, (short)0, (byte)bArray.length); + simulator.selectApplet(aid); + } + + @Test + public void test_userPINValid() { + byte[] response = simulator.transmitCommand(new byte[] {0x00, 0x20, 0x00, (byte)0x81, 0x06, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36}); + assertArrayEquals(response, new byte[] {(byte)0x90, 0x00}); + } + + @Test + public void test_userPINInvalid() { + byte[] response = simulator.transmitCommand(new byte[] {0x00, 0x20, 0x00, (byte)0x81, 0x06, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31}); + assertArrayEquals(response, new byte[] {(byte)0x63, (byte)0xc2}); + response = simulator.transmitCommand(new byte[] {0x00, 0x20, 0x00, (byte)0x81, 0x06, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31}); + assertArrayEquals(response, new byte[] {(byte)0x63, (byte)0xc1}); + response = simulator.transmitCommand(new byte[] {0x00, 0x20, 0x00, (byte)0x81, 0x06, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31}); + assertArrayEquals(response, new byte[] {(byte)0x63, (byte)0xc0}); + response = simulator.transmitCommand(new byte[] {0x00, 0x20, 0x00, (byte)0x81, 0x06, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36}); + assertArrayEquals(response, new byte[] {(byte)0x63, (byte)0xc0}); + } + + /** + * Test reset of PIN try counter by first verifying a wrong PIN, followed by a correct PIN and again a wrong PIN. For the second wrong PIN the counter should be reset to 2 again. + */ + @Test + public void test_userPINReset() { + byte[] response = simulator.transmitCommand(new byte[] {0x00, 0x20, 0x00, (byte)0x81, 0x06, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31}); + assertArrayEquals(response, new byte[] {(byte)0x63, (byte)0xc2}); + response = simulator.transmitCommand(new byte[] {0x00, 0x20, 0x00, (byte)0x81, 0x06, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36}); + assertArrayEquals(response, new byte[] {(byte)0x90, (byte)0x00}); + response = simulator.transmitCommand(new byte[] {0x00, 0x20, 0x00, (byte)0x81, 0x06, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31}); + assertArrayEquals(response, new byte[] {(byte)0x63, (byte)0xc2}); + } +}