Path: blob/master/test/jdk/javax/smartcardio/CommandAPDUTest.java
41144 views
/*1* Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 804902126* @summary Test different constructors for CommandAPDU and check CLA,INS,NC,NE,27* P1,and P228* @run testng CommandAPDUTest29*/30import java.nio.ByteBuffer;31import javax.smartcardio.CommandAPDU;32import static org.testng.Assert.*;33import org.testng.annotations.BeforeClass;34import org.testng.annotations.DataProvider;35import org.testng.annotations.Test;3637public class CommandAPDUTest {3839static final byte[] C1 = {(byte) 0x00, (byte) 0xA4, (byte) 0x04,40(byte) 0x00, (byte) 0x07, (byte) 0xA0, (byte) 0x00, (byte) 0x00,41(byte) 0x00, (byte) 0x62, (byte) 0x81, (byte) 0x01, (byte) 0x00};42static int cla, ins, nc, ne, p1, p2;43static byte[] apdu, data;44static CommandAPDU cm1, cm2, cm3, cm4, cm5, cm6, cm7, cm8, cm9;4546@BeforeClass47public static void setUpClass() throws Exception {48//expected values of apdu, data, headers, nc, ne49CommandAPDU capdu = new CommandAPDU(C1);50apdu = capdu.getBytes();51data = capdu.getData();5253cla = capdu.getCLA();54if (cla != (C1[0] & 0xff)) {55throw new RuntimeException("Failure: cla is not right");56}5758ins = capdu.getINS();59if (ins != (C1[1] & 0xff)) {60throw new RuntimeException("Failure: ins is not right");61}6263p1 = capdu.getP1();64if (p1 != (C1[2] & 0xff)) {65throw new RuntimeException("Failure: p1 is not right");66}6768p2 = capdu.getP2();69if (p2 != (C1[3] & 0xff)) {70throw new RuntimeException("Failure: p2 is not right");71}7273nc = capdu.getNc();74ne = capdu.getNe();7576//Test on following constructors77cm1 = new CommandAPDU(apdu);78cm2 = new CommandAPDU(cla, ins, p1, p2);79cm3 = new CommandAPDU(cla, ins, p1, p2, data);80cm4 = new CommandAPDU(cla, ins, p1, p2, data, ne);81cm5 = new CommandAPDU(cla, ins, p1, p2, ne);82cm6 = new CommandAPDU(ByteBuffer.wrap(apdu));83cm7 = new CommandAPDU(apdu, 0, apdu.length);84cm8 = new CommandAPDU(cla, ins, p1, p2, data, 0, nc);85cm9 = new CommandAPDU(cla, ins, p1, p2, data, 0, nc, ne);86}8788@Test(dataProvider = "provider1")89public static void testHeaders(CommandAPDU cm) {90assertEquals(cla, cm.getCLA());91assertEquals(ins, cm.getINS());92assertEquals(p1, cm.getP1());93assertEquals(p2, cm.getP2());94}9596@Test(dataProvider = "provider2")97public static void testAPDU(CommandAPDU cm) {98assertEquals(apdu, cm.getBytes());99}100101@Test(dataProvider = "provider3")102public static void testData(CommandAPDU cm) {103assertEquals(data, cm.getData());104}105106@Test(dataProvider = "provider3")107public static void testNC(CommandAPDU cm) {108assertEquals(nc, cm.getNc());109}110111@Test(dataProvider = "provider4")112public static void testNE(CommandAPDU cm) {113assertEquals(ne, cm.getNe());114}115116@DataProvider117public Object[][] provider1() {118return new Object[][]{{cm1}, {cm2}, {cm3}, {cm4}, {cm5}, {cm6}, {cm7},119{cm8}, {cm9}};120}121122@DataProvider123public Object[][] provider2() {124return new Object[][]{{cm1}, {cm6}, {cm7}};125}126127@DataProvider128public Object[][] provider3() {129return new Object[][]{{cm1}, {cm3}, {cm4}, {cm6}, {cm7}, {cm8}, {cm9}};130}131132@DataProvider133public Object[][] provider4() {134return new Object[][]{{cm1}, {cm4}, {cm5}, {cm6}, {cm7}, {cm9}};135}136137}138139140