package com.dangerousthings.aliro; import com.licel.jcardsim.smartcardio.CardSimulator; import javacard.framework.AID; import javax.smartcardio.CommandAPDU; import javax.smartcardio.ResponseAPDU; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; /** * Tests for {@link StepUpApplet} — scaffold for the Aliro step-up phase * (spec §8.4). This first pass only covers AID selection and * INS-not-supported handling; the mdoc DeviceRequest/DeviceResponse + * StepUpSK key derivation pipelines come in follow-up iterations. */ class StepUpAppletTest { private CardSimulator sim; @BeforeEach void setUp() { CredentialStore.get().resetForTesting(); sim = new CardSimulator(); AID aid = new AID(AliroAids.STEP_UP, (short) 0, (byte) AliroAids.STEP_UP.length); sim.installApplet(aid, StepUpApplet.class); } @Test void selectStepUpAidSucceeds() { CommandAPDU select = new CommandAPDU(0x00, 0xA4, 0x04, 0x00, AliroAids.STEP_UP, 256); ResponseAPDU r = sim.transmitCommand(select); assertEquals(0x9000, r.getSW(), "SELECT of step-up AID must succeed"); } @Test void selectStepUpAidReturnsFciWithAid() { CommandAPDU select = new CommandAPDU(0x00, 0xA4, 0x04, 0x00, AliroAids.STEP_UP, 256); ResponseAPDU r = sim.transmitCommand(select); byte[] aidFromFci = TlvUtil.findTopLevel(unwrap6F(r.getData()), 0x84); org.junit.jupiter.api.Assertions.assertArrayEquals(AliroAids.STEP_UP, aidFromFci, "FCI 84 tag must echo the step-up AID"); } @Test void unsupportedInsReturns6D00() { CommandAPDU select = new CommandAPDU(0x00, 0xA4, 0x04, 0x00, AliroAids.STEP_UP, 256); sim.transmitCommand(select); // Some arbitrary unsupported INS under the proprietary class. CommandAPDU bogus = new CommandAPDU(0x80, 0xFE, 0x00, 0x00, new byte[0], 256); assertEquals(0x6D00, sim.transmitCommand(bogus).getSW(), "unrecognized INS on step-up applet must return SW_INS_NOT_SUPPORTED"); } @Test void wrongClaReturns6E00() { CommandAPDU select = new CommandAPDU(0x00, 0xA4, 0x04, 0x00, AliroAids.STEP_UP, 256); sim.transmitCommand(select); CommandAPDU wrongCla = new CommandAPDU(0x81, 0xC3, 0x00, 0x00, new byte[0], 256); assertEquals(0x6E00, sim.transmitCommand(wrongCla).getSW(), "wrong CLA must return SW_CLA_NOT_SUPPORTED"); } /** Unwraps the outer 6F FCI template to expose its nested TLVs. */ private static byte[] unwrap6F(byte[] fci) { if (fci.length < 2 || fci[0] != 0x6F) { throw new IllegalArgumentException("expected a 6F-wrapped FCI template"); } int len = fci[1] & 0xFF; byte[] inner = new byte[len]; System.arraycopy(fci, 2, inner, 0, len); return inner; } }