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 Test different constructors for CommandAPDU and check CLA,INS,NC,NE, 28 * P1,and P2 29 * @run testng CommandAPDUTest 30 */ 31 import java.nio.ByteBuffer; 32 import javax.smartcardio.CommandAPDU; 33 import static org.testng.Assert.*; 34 import org.testng.annotations.BeforeClass; 35 import org.testng.annotations.DataProvider; 36 import org.testng.annotations.Test; 37 38 public class CommandAPDUTest { 39 40 static final byte[] C1 = {(byte) 0x00, (byte) 0xA4, (byte) 0x04, 41 (byte) 0x00, (byte) 0x07, (byte) 0xA0, (byte) 0x00, (byte) 0x00, 42 (byte) 0x00, (byte) 0x62, (byte) 0x81, (byte) 0x01, (byte) 0x00}; 43 static int cla, ins, nc, ne, p1, p2; 44 static byte[] apdu, data; 45 static CommandAPDU cm1, cm2, cm3, cm4, cm5, cm6, cm7, cm8, cm9; 46 47 @BeforeClass 48 public static void setUpClass() throws Exception { 49 //expected values of apdu, data, headers, nc, ne 50 CommandAPDU capdu = new CommandAPDU(C1); 51 apdu = capdu.getBytes(); 52 data = capdu.getData(); 53 54 cla = capdu.getCLA(); 55 if (cla != (C1[0] & 0xff)) { 56 throw new RuntimeException("Failure: cla is not right"); 57 } 58 59 ins = capdu.getINS(); 60 if (ins != (C1[1] & 0xff)) { 61 throw new RuntimeException("Failure: ins is not right"); 62 } 63 64 p1 = capdu.getP1(); 65 if (p1 != (C1[2] & 0xff)) { 66 throw new RuntimeException("Failure: p1 is not right"); 67 } 68 69 p2 = capdu.getP2(); 70 if (p2 != (C1[3] & 0xff)) { 71 throw new RuntimeException("Failure: p2 is not right"); 72 } 73 74 nc = capdu.getNc(); 75 ne = capdu.getNe(); 76 77 //Test on following constructors 78 cm1 = new CommandAPDU(apdu); 79 cm2 = new CommandAPDU(cla, ins, p1, p2); 80 cm3 = new CommandAPDU(cla, ins, p1, p2, data); 81 cm4 = new CommandAPDU(cla, ins, p1, p2, data, ne); 82 cm5 = new CommandAPDU(cla, ins, p1, p2, ne); 83 cm6 = new CommandAPDU(ByteBuffer.wrap(apdu)); 84 cm7 = new CommandAPDU(apdu, 0, apdu.length); 85 cm8 = new CommandAPDU(cla, ins, p1, p2, data, 0, nc); 86 cm9 = new CommandAPDU(cla, ins, p1, p2, data, 0, nc, ne); 87 } 88 89 @Test(dataProvider = "provider1") 90 public static void testHeaders(CommandAPDU cm) { 91 assertEquals(cla, cm.getCLA()); 92 assertEquals(ins, cm.getINS()); 93 assertEquals(p1, cm.getP1()); 94 assertEquals(p2, cm.getP2()); 95 } 96 97 @Test(dataProvider = "provider2") 98 public static void testAPDU(CommandAPDU cm) { 99 assertEquals(apdu, cm.getBytes()); 100 } 101 102 @Test(dataProvider = "provider3") 103 public static void testData(CommandAPDU cm) { 104 assertEquals(data, cm.getData()); 105 } 106 107 @Test(dataProvider = "provider3") 108 public static void testNC(CommandAPDU cm) { 109 assertEquals(nc, cm.getNc()); 110 } 111 112 @Test(dataProvider = "provider4") 113 public static void testNE(CommandAPDU cm) { 114 assertEquals(ne, cm.getNe()); 115 } 116 117 @DataProvider 118 public Object[][] provider1() { 119 return new Object[][]{{cm1}, {cm2}, {cm3}, {cm4}, {cm5}, {cm6}, {cm7}, 120 {cm8}, {cm9}}; 121 } 122 123 @DataProvider 124 public Object[][] provider2() { 125 return new Object[][]{{cm1}, {cm6}, {cm7}}; 126 } 127 128 @DataProvider 129 public Object[][] provider3() { 130 return new Object[][]{{cm1}, {cm3}, {cm4}, {cm6}, {cm7}, {cm8}, {cm9}}; 131 } 132 133 @DataProvider 134 public Object[][] provider4() { 135 return new Object[][]{{cm1}, {cm4}, {cm5}, {cm6}, {cm7}, {cm9}}; 136 } 137 138 }