package tests;

import org.junit.Assert;
import org.testng.annotations.*;

import java.math.BigInteger;
import java.nio.ByteBuffer;

import javax.smartcardio.CommandAPDU;
import javax.smartcardio.ResponseAPDU;

import cardTools.CardManager;

/**
 * Example tests for the eSIM-Applet
 * @author Jinghao Zhao
 */
public class AppletTest extends BaseTest {
    
    public AppletTest() {
    }

    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @BeforeMethod
    public void setUpMethod() throws Exception {
    }

    @AfterMethod
    public void tearDownMethod() throws Exception {
    }
    @Test
    public void hello() throws Exception {
        CardManager connection = connect();

        CommandAPDU cmd = new CommandAPDU(new BigInteger("00a40000023F00", 16).toByteArray());
        ResponseAPDU responseAPDU = connection.transmit(cmd);
        cmd = new CommandAPDU(new BigInteger("00a8000000", 16).toByteArray());
        responseAPDU = connection.transmit(cmd);
        cmd = new CommandAPDU(new BigInteger("00c0000002", 16).toByteArray());
        responseAPDU = connection.transmit(cmd);

        System.out.println("============Create GSM================");
        cmd = new CommandAPDU(new BigInteger("00e000003262308202782183027f20a51683027fffcb0d00000000000000000000000000ca01828a01058b032f0601c606900100830101", 16).toByteArray());
        responseAPDU = connection.transmit(cmd);
        cmd = new CommandAPDU(new BigInteger("00a40004027F20", 16).toByteArray());
        responseAPDU = connection.transmit(cmd);
        // Create IMSI
        cmd = new CommandAPDU(new BigInteger("00e000002462228202412183026f07a50ac00100cd02ff01ca01848a01058b036f0603800200098800", 16).toByteArray());
        responseAPDU = connection.transmit(cmd);
        cmd = new CommandAPDU(new BigInteger("00a40004026F07", 16).toByteArray());
        responseAPDU = connection.transmit(cmd);
        cmd = new CommandAPDU(new BigInteger("00b000000a", 16).toByteArray());
        responseAPDU = connection.transmit(cmd);

        System.out.println("============Create ADF================");
        cmd = new CommandAPDU(new BigInteger("00a40004023F00", 16).toByteArray());
        responseAPDU = connection.transmit(cmd);
        cmd = new CommandAPDU(new BigInteger("00e000005962578202782183027fff8410a0000000871002ffffffff8907090000a51683027fffcb0d00000000000000000000000000ca01808a0105ab15800101a40683010a95010880014097008001069000c609900140830101830181", 16).toByteArray());
        responseAPDU = connection.transmit(cmd);
        cmd = new CommandAPDU(new BigInteger("00a4040410a0000000871002ffffffff8907090000", 16).toByteArray());
        responseAPDU = connection.transmit(cmd);
        // Create IMSI
        cmd = new CommandAPDU(new BigInteger("00e000002462228202412183026f07a50ac00100cd02ff01ca01848a01058b036f0603800200098800", 16).toByteArray());
        responseAPDU = connection.transmit(cmd);
        cmd = new CommandAPDU(new BigInteger("00a40004026F07", 16).toByteArray());
        responseAPDU = connection.transmit(cmd);
        cmd = new CommandAPDU(new BigInteger("00b000000a", 16).toByteArray());
        responseAPDU = connection.transmit(cmd);

        cmd = new CommandAPDU(new BigInteger("00e0000024622282054221000f0583026f4fa506c00100ca01808a01058b036f06048002004b8801b0", 16).toByteArray());
        responseAPDU = connection.transmit(cmd);
        cmd = new CommandAPDU(new BigInteger("00a40004026F4F", 16).toByteArray());
        responseAPDU = connection.transmit(cmd);
        cmd = new CommandAPDU(new BigInteger("00b201040f", 16).toByteArray());
        responseAPDU = connection.transmit(cmd);
        cmd = new CommandAPDU(new BigInteger("00dc01040fffffffffffffffffffffffffffffff", 16).toByteArray());
        responseAPDU = connection.transmit(cmd);
        cmd = new CommandAPDU(new BigInteger("00b201040f", 16).toByteArray());
        responseAPDU = connection.transmit(cmd);
        // read next record
        cmd = new CommandAPDU(new BigInteger("00b200020f", 16).toByteArray());
        responseAPDU = connection.transmit(cmd);
        cmd = new CommandAPDU(new BigInteger("00b200020f", 16).toByteArray());
        responseAPDU = connection.transmit(cmd);
        Assert.assertEquals(0x9000, responseAPDU.getSW());
        Assert.assertNotNull(responseAPDU.getBytes());
        cmd = new CommandAPDU(new BigInteger("00b200030f", 16).toByteArray());
        responseAPDU = connection.transmit(cmd);
        cmd = new CommandAPDU(new BigInteger("00b200030f", 16).toByteArray());
        responseAPDU = connection.transmit(cmd);
        cmd = new CommandAPDU(new BigInteger("00b200030f", 16).toByteArray());
        responseAPDU = connection.transmit(cmd);
        Assert.assertEquals(0x9000, responseAPDU.getSW());
        Assert.assertNotNull(responseAPDU.getBytes());
    }

}
