package org.dogtagpki.javacard; import com.licel.jcardsim.base.Simulator; import javacard.framework.AID; import org.bouncycastle.util.encoders.Hex; import org.junit.Assert; import org.junit.Test; import javax.smartcardio.*; import java.io.FileReader; import java.util.Properties; /** * @author Endi Sukma Dewata */ public class MainTest { @Test public void testMainApplet() throws Exception { Properties props = new Properties(); props.load(new FileReader("conf/jcardsim.cfg")); String s = (String)props.get("com.licel.jcardsim.smartcardio.applet.0.AID"); byte[] aidBytes = Hex.decode(s); AID appletAID = new AID(aidBytes, (short) 0, (byte) aidBytes.length); Simulator simulator = new Simulator(); simulator.installApplet(appletAID, TestApplet.class); simulator.selectApplet(appletAID); // test NOP ResponseAPDU response = simulator.transmitCommand(new CommandAPDU(0x01, 0x02, 0x00, 0x00)); Assert.assertEquals(0x9000, response.getSW()); // test hello world from card response = simulator.transmitCommand(new CommandAPDU(0x01, 0x01, 0x00, 0x00)); Assert.assertEquals(0x9000, response.getSW()); Assert.assertEquals("TestApplet", new String(response.getData())); // test echo response = simulator.transmitCommand(new CommandAPDU(0x01, 0x01, 0x01, 0x00, ("Hello javacard world !").getBytes())); Assert.assertEquals(0x9000, response.getSW()); Assert.assertEquals("Hello javacard world !", new String(response.getData())); } @Test public void testProxyApplet() throws Exception { Properties props = new Properties(); props.load(new FileReader("conf/jcardsim.cfg")); String s = (String)props.get("com.licel.jcardsim.smartcardio.applet.1.AID"); byte[] aidBytes = Hex.decode(s); AID appletAID = new AID(aidBytes, (short) 0, (byte) aidBytes.length); Simulator simulator = new Simulator(); simulator.installApplet(appletAID, ProxyApplet.class); simulator.selectApplet(appletAID); // test NOP ResponseAPDU response = simulator.transmitCommand(new CommandAPDU(0x01, 0x02, 0x00, 0x00)); Assert.assertEquals(0x9000, response.getSW()); // test hello world from card response = simulator.transmitCommand(new CommandAPDU(0x01, 0x01, 0x00, 0x00)); Assert.assertEquals(0x9000, response.getSW()); Assert.assertEquals("ProxyApplet", new String(response.getData())); // test echo response = simulator.transmitCommand(new CommandAPDU(0x01, 0x01, 0x01, 0x00, ("Hello javacard world !").getBytes())); Assert.assertEquals(0x9000, response.getSW()); Assert.assertEquals("Hello javacard world !", new String(response.getData())); } }