1/*
2 * Copyright (c) 2007, 2017, 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 * @run testng ResponseAPDUTest
29 */
30import javax.smartcardio.ResponseAPDU;
31import static org.testng.Assert.*;
32import org.testng.annotations.BeforeClass;
33import org.testng.annotations.Test;
34
35public class ResponseAPDUTest {
36
37    static final byte[] R1 = {(byte) 0x07, (byte) 0xA0, (byte) 0x00,
38        (byte) 0x00, (byte) 0x00, (byte) 0x62, (byte) 0x81, (byte) 0x01,
39        (byte) 0x04, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x24,
40        (byte) 0x05, (byte) 0x00, (byte) 0x0B, (byte) 0x04, (byte) 0xB0,
41        (byte) 0x25, (byte) 0x90, (byte) 0x00};
42    static final ResponseAPDU RAPDU = new ResponseAPDU(R1);
43    static byte[] expectedData;
44    static int expectedNr, expectedSw1, expectedSw2, expectedSw;
45
46    @BeforeClass
47    public static void setUpClass() throws Exception {
48        //expected values for data,nr,sw1,sw2 and sw
49
50        int apduLen = R1.length;
51        expectedData = new byte[apduLen - 2];
52        for (int i = 0; i < (apduLen - 2); i++) {
53            expectedData[i] = R1[i];
54        }
55
56        expectedNr = expectedData.length;
57        expectedSw1 = R1[apduLen - 2] & 0xff;
58        expectedSw2 = R1[apduLen - 1] & 0xff;
59        expectedSw = (expectedSw1 << 8) | expectedSw2;
60    }
61
62    @Test
63    public static void test() {
64        assertEquals(RAPDU.getBytes(), R1);
65        assertEquals(RAPDU.getData(), expectedData);
66        assertEquals(RAPDU.getNr(), expectedNr);
67        assertEquals(RAPDU.getSW(), expectedSw);
68        assertEquals(RAPDU.getSW1(), expectedSw1);
69        assertEquals(RAPDU.getSW2(), expectedSw2);
70    }
71}
72