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