1 /*
   2  * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8049021
  27  * @summary Construct ResponseAPDU from byte array and check NR< SW, SW1 and SW2
  28  * @compile -addmods java.smartcardio ResponseAPDUTest.java
  29  * @run testng/othervm -addmods java.smartcardio ResponseAPDUTest
  30  */
  31 import javax.smartcardio.ResponseAPDU;
  32 import static org.testng.Assert.*;
  33 import org.testng.annotations.BeforeClass;
  34 import org.testng.annotations.Test;
  35 
  36 public class ResponseAPDUTest {
  37 
  38     static final byte[] R1 = {(byte) 0x07, (byte) 0xA0, (byte) 0x00,
  39         (byte) 0x00, (byte) 0x00, (byte) 0x62, (byte) 0x81, (byte) 0x01,
  40         (byte) 0x04, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x24,
  41         (byte) 0x05, (byte) 0x00, (byte) 0x0B, (byte) 0x04, (byte) 0xB0,
  42         (byte) 0x25, (byte) 0x90, (byte) 0x00};
  43     static final ResponseAPDU RAPDU = new ResponseAPDU(R1);
  44     static byte[] expectedData;
  45     static int expectedNr, expectedSw1, expectedSw2, expectedSw;
  46 
  47     @BeforeClass
  48     public static void setUpClass() throws Exception {
  49         //expected values for data,nr,sw1,sw2 and sw
  50 
  51         int apduLen = R1.length;
  52         expectedData = new byte[apduLen - 2];
  53         for (int i = 0; i < (apduLen - 2); i++) {
  54             expectedData[i] = R1[i];
  55         }
  56 
  57         expectedNr = expectedData.length;
  58         expectedSw1 = R1[apduLen - 2] & 0xff;
  59         expectedSw2 = R1[apduLen - 1] & 0xff;
  60         expectedSw = (expectedSw1 << 8) | expectedSw2;
  61     }
  62 
  63     @Test
  64     public static void test() {
  65         assertEquals(RAPDU.getBytes(), R1);
  66         assertEquals(RAPDU.getData(), expectedData);
  67         assertEquals(RAPDU.getNr(), expectedNr);
  68         assertEquals(RAPDU.getSW(), expectedSw);
  69         assertEquals(RAPDU.getSW1(), expectedSw1);
  70         assertEquals(RAPDU.getSW2(), expectedSw2);
  71     }
  72 }