test/jdk/javax/smartcardio/CommandAPDUTest.java
author herrick
Wed, 27 Mar 2019 08:27:58 -0400
branchJDK-8200758-branch
changeset 57288 0be43184f52a
parent 47216 71c04702a3d5
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28260
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
     1
/*
44108
8de4ce18c36f 8167525: update jdk tests to remove @compile --add-modules workaround
amlu
parents: 40261
diff changeset
     2
 * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
28260
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
     4
 *
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
     7
 * published by the Free Software Foundation.
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
     8
 *
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    13
 * accompanied this code).
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    14
 *
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    18
 *
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    21
 * questions.
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    22
 */
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    23
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    24
/*
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    25
 * @test
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    26
 * @bug 8049021
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    27
 * @summary Test different constructors for CommandAPDU and check CLA,INS,NC,NE,
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    28
 * P1,and P2
44108
8de4ce18c36f 8167525: update jdk tests to remove @compile --add-modules workaround
amlu
parents: 40261
diff changeset
    29
 * @run testng CommandAPDUTest
28260
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    30
 */
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    31
import java.nio.ByteBuffer;
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    32
import javax.smartcardio.CommandAPDU;
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    33
import static org.testng.Assert.*;
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    34
import org.testng.annotations.BeforeClass;
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    35
import org.testng.annotations.DataProvider;
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    36
import org.testng.annotations.Test;
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    37
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    38
public class CommandAPDUTest {
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    39
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    40
    static final byte[] C1 = {(byte) 0x00, (byte) 0xA4, (byte) 0x04,
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    41
        (byte) 0x00, (byte) 0x07, (byte) 0xA0, (byte) 0x00, (byte) 0x00,
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    42
        (byte) 0x00, (byte) 0x62, (byte) 0x81, (byte) 0x01, (byte) 0x00};
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    43
    static int cla, ins, nc, ne, p1, p2;
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    44
    static byte[] apdu, data;
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    45
    static CommandAPDU cm1, cm2, cm3, cm4, cm5, cm6, cm7, cm8, cm9;
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    46
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    47
    @BeforeClass
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    48
    public static void setUpClass() throws Exception {
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    49
        //expected values of apdu, data, headers, nc, ne
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    50
        CommandAPDU capdu = new CommandAPDU(C1);
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    51
        apdu = capdu.getBytes();
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    52
        data = capdu.getData();
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    53
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    54
        cla = capdu.getCLA();
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    55
        if (cla != (C1[0] & 0xff)) {
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    56
            throw new RuntimeException("Failure: cla is not right");
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    57
        }
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    58
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    59
        ins = capdu.getINS();
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    60
        if (ins != (C1[1] & 0xff)) {
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    61
            throw new RuntimeException("Failure: ins is not right");
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    62
        }
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    63
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    64
        p1 = capdu.getP1();
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    65
        if (p1 != (C1[2] & 0xff)) {
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    66
            throw new RuntimeException("Failure: p1 is not right");
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    67
        }
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    68
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    69
        p2 = capdu.getP2();
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    70
        if (p2 != (C1[3] & 0xff)) {
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    71
            throw new RuntimeException("Failure: p2 is not right");
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    72
        }
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    73
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    74
        nc = capdu.getNc();
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    75
        ne = capdu.getNe();
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    76
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    77
        //Test on following constructors
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    78
        cm1 = new CommandAPDU(apdu);
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    79
        cm2 = new CommandAPDU(cla, ins, p1, p2);
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    80
        cm3 = new CommandAPDU(cla, ins, p1, p2, data);
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    81
        cm4 = new CommandAPDU(cla, ins, p1, p2, data, ne);
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    82
        cm5 = new CommandAPDU(cla, ins, p1, p2, ne);
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    83
        cm6 = new CommandAPDU(ByteBuffer.wrap(apdu));
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    84
        cm7 = new CommandAPDU(apdu, 0, apdu.length);
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    85
        cm8 = new CommandAPDU(cla, ins, p1, p2, data, 0, nc);
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    86
        cm9 = new CommandAPDU(cla, ins, p1, p2, data, 0, nc, ne);
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    87
    }
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    88
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    89
    @Test(dataProvider = "provider1")
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    90
    public static void testHeaders(CommandAPDU cm) {
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    91
        assertEquals(cla, cm.getCLA());
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    92
        assertEquals(ins, cm.getINS());
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    93
        assertEquals(p1, cm.getP1());
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    94
        assertEquals(p2, cm.getP2());
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    95
    }
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    96
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    97
    @Test(dataProvider = "provider2")
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    98
    public static void testAPDU(CommandAPDU cm) {
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
    99
        assertEquals(apdu, cm.getBytes());
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   100
    }
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   101
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   102
    @Test(dataProvider = "provider3")
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   103
    public static void testData(CommandAPDU cm) {
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   104
        assertEquals(data, cm.getData());
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   105
    }
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   106
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   107
    @Test(dataProvider = "provider3")
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   108
    public static void testNC(CommandAPDU cm) {
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   109
        assertEquals(nc, cm.getNc());
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   110
    }
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   111
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   112
    @Test(dataProvider = "provider4")
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   113
    public static void testNE(CommandAPDU cm) {
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   114
        assertEquals(ne, cm.getNe());
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   115
    }
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   116
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   117
    @DataProvider
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   118
    public Object[][] provider1() {
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   119
        return new Object[][]{{cm1}, {cm2}, {cm3}, {cm4}, {cm5}, {cm6}, {cm7},
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   120
        {cm8}, {cm9}};
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   121
    }
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   122
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   123
    @DataProvider
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   124
    public Object[][] provider2() {
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   125
        return new Object[][]{{cm1}, {cm6}, {cm7}};
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   126
    }
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   127
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   128
    @DataProvider
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   129
    public Object[][] provider3() {
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   130
        return new Object[][]{{cm1}, {cm3}, {cm4}, {cm6}, {cm7}, {cm8}, {cm9}};
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   131
    }
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   132
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   133
    @DataProvider
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   134
    public Object[][] provider4() {
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   135
        return new Object[][]{{cm1}, {cm4}, {cm5}, {cm6}, {cm7}, {cm9}};
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   136
    }
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   137
b826987f5179 8049021: Add smartcardio tests with APDU buffer
valeriep
parents:
diff changeset
   138
}