Added some first unit tests

This commit is contained in:
Joeri 2017-11-30 21:30:54 +01:00
parent ca41603090
commit 0337ca552e
4 changed files with 116 additions and 6 deletions

2
.gitmodules vendored
View File

@ -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

View File

@ -1,14 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="openpgpcard" name="JavaCard OpenPGP Applet">
<property name="test.build.dir" value="bin"/>
<property name="main.src.dir" value="src"/>
<property name="test.src.dir" value="test"/>
<property name="lib.dir" value="lib"/>
<!-- Applet building dependencies -->
<property name="JC" value="ext/oracle_javacard_sdks/jc303_kit/"/>
<get src="https://github.com/martinpaljak/ant-javacard/releases/download/v1.8/ant-javacard.jar" dest="ext/" skipexisting="true"/>
<property name="JC" value="${lib.dir}/oracle_javacard_sdks/jc303_kit/"/>
<get src="https://github.com/martinpaljak/ant-javacard/releases/download/v1.8/ant-javacard.jar" dest="${lib.dir}" skipexisting="true"/>
<get src="https://github.com/licel/jcardsim/raw/master/jcardsim-3.0.4-SNAPSHOT.jar" dest="${lib.dir}" skipexisting="true"/>
<get src="https://github.com/junit-team/junit/releases/download/r4.12/junit-4.12.jar" dest="${lib.dir}" skipexisting="true"/>
<get src="http://search.maven.org/remotecontent?filepath=org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar" dest="${lib.dir}/hamcrest-core-1.3.jar" skipexisting="true"/>
<path id="classpath.test">
<pathelement location="${lib.dir}/junit-4.12.jar"/>
<pathelement location="${lib.dir}/hamcrest-core-1.3.jar"/>
<pathelement location="${lib.dir}/jcardsim-3.0.4-SNAPSHOT.jar"/>
<pathelement location="${test.build.dir}"/>
</path>
<!-- ant-javacard task from javacard.pro -->
<taskdef name="javacard" classname="pro.javacard.ant.JavaCard" classpath="ext/ant-javacard.jar"/>
<taskdef name="javacard" classname="pro.javacard.ant.JavaCard" classpath="${lib.dir}/ant-javacard.jar"/>
<!-- All included applets -->
<!-- Build OpenPGP applet -->
<target name="openpgpcard">
<javacard jckit="${JC}">
<cap output="openpgpcard.cap" sources="src" aid="D27600012401">
@ -16,4 +31,33 @@
</cap>
</javacard>
</target>
<!-- Build applet used in the unit tests -->
<target name="compileTestApplet" depends="openpgpcard">
<mkdir dir="${test.build.dir}"/>
<javac srcdir="${main.src.dir}" destdir="${test.build.dir}" includeantruntime="false" debug="yes">
<classpath refid="classpath.test"/>
</javac>
</target>
<!-- Build unit tests -->
<target name="compileTest" depends="openpgpcard,compileTestApplet">
<javac srcdir="${test.src.dir}" destdir="${test.build.dir}" includeantruntime="false">
<classpath refid="classpath.test"/>
</javac>
</target>
<!-- Run tests -->
<target name="test" depends="compileTest">
<junit printsummary="yes" haltonfailure="no" fork="true">
<classpath>
<path refid="classpath.test"/>
<pathelement location="${test.build.dir}"/>
</classpath>
<test name="openpgpcard.OpenPGPAppletTest" haltonfailure="no" todir=".">
<formatter type="plain" />
<formatter type="xml" />
</test>
</junit>
</target>
</project>

View File

@ -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});
}
}