Path: blob/master/test/jdk/javax/smartcardio/TestCommandAPDU.java
41144 views
/*1* Copyright (c) 2005, 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 629376726* @summary Test for the CommandAPDU class27* @author Andreas Sterbenz28* @key randomness29*/3031import java.util.*;3233import javax.smartcardio.*;3435public class TestCommandAPDU {3637private static final Random random = new Random();3839private static void test(int nc, int ne) throws Exception {40System.out.println("nc = " + nc + ", ne = " + ne);41byte[] data = new byte[nc];42random.nextBytes(data);43CommandAPDU apdu = new CommandAPDU((byte)0, (byte)0, (byte)0, (byte)0, data, ne);44//System.out.println(apdu);45if (apdu.getNc() != nc) {46throw new Exception("dataLength does not match");47}48if (Arrays.equals(data, apdu.getData()) == false) {49throw new Exception("data does not match");50}51if (apdu.getNe() != ne) {52throw new Exception("ne does not match");53}54byte[] apduBytes = apdu.getBytes();55CommandAPDU apdu2 = new CommandAPDU(apduBytes);56//System.out.println(apdu2);57if (Arrays.equals(apduBytes, apdu2.getBytes()) == false) {58throw new Exception("apduBytes do not match");59}60if (apdu2.getNc() != nc) {61throw new Exception("dataLength does not match");62}63if (Arrays.equals(data, apdu2.getData()) == false) {64throw new Exception("data does not match");65}66if (apdu2.getNe() != ne) {67throw new Exception("ne does not match");68}69}7071public static void main(String[] args) throws Exception {72int[] t = {0, 42, 0x81, 255, 256, 4242, 0x8181, 65535, 65536};73for (int nc : t) {74if (nc == 65536) {75continue;76}77for (int ne : t) {78test(nc, ne);79}80}81System.out.println("OK");82}83}848586