Path: blob/master/test/jdk/javax/smartcardio/Serialize.java
41144 views
/*1* Copyright (c) 2006, 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 644536726* @summary make sure serialization works27* @author Andreas Sterbenz28*/2930import java.io.*;3132import javax.smartcardio.*;3334public class Serialize {3536public static void main(String[] args) throws Exception {37ByteArrayOutputStream bout = new ByteArrayOutputStream();38ObjectOutputStream oout = new ObjectOutputStream(bout);3940CommandAPDU c1 = new CommandAPDU(parse("00 A4 04 00 07 A0 00 00 00 62 81 01 00"));41ResponseAPDU r1 = new ResponseAPDU(parse("07 A0 00 00 00 62 81 01 04 01 00 00 24 05 00 0B 04 B0 25 90 00"));42ATR a1 = new ATR(parse("3B 7F 18 00 00 00 31 C0 73 9E 01 0B 64 52 D9 04 00 82 90 00"));4344oout.writeObject(c1);45oout.writeObject(r1);46oout.writeObject(a1);47oout.close();4849ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());50ObjectInputStream oin = new ObjectInputStream(bin);5152CommandAPDU c2 = (CommandAPDU)oin.readObject();53ResponseAPDU r2 = (ResponseAPDU)oin.readObject();54ATR a2 = (ATR)oin.readObject();5556if (!c2.equals(c1)) {57throw new Exception("CommandAPDU not equal");58}59if (c2.getNc() != 7) {60throw new Exception("Nc mismatch: " + c2.getNc());61}62if (c2.getNe() != 256) {63throw new Exception("Ne mismatch: " + c2.getNe());64}65if (c2.getINS() != 0xA4) {66throw new Exception("INS mismatch: " + c2.getINS());67}68if (!r2.equals(r1)) {69throw new Exception("ResponseAPDU not equal");70}71if (r2.getSW1() != 0x90) {72throw new Exception("SW1 mismatch: " + r2.getSW1());73}74if (!a2.equals(a1)) {75throw new Exception("ATR not equal");76}77if (!java.util.Arrays.equals(a2.getHistoricalBytes(), a1.getHistoricalBytes())) {78throw new Exception("Historical bytes mismatch");79}80System.out.println("OK");81}8283private final static char[] hexDigits = "0123456789abcdef".toCharArray();8485public static String toString(byte[] b) {86StringBuffer sb = new StringBuffer(b.length * 3);87for (int i = 0; i < b.length; i++) {88int k = b[i] & 0xff;89if (i != 0) {90sb.append(':');91}92sb.append(hexDigits[k >>> 4]);93sb.append(hexDigits[k & 0xf]);94}95return sb.toString();96}9798public static byte[] parse(String s) {99try {100int n = s.length();101ByteArrayOutputStream out = new ByteArrayOutputStream(n >> 1);102StringReader r = new StringReader(s);103while (true) {104int b1 = nextNibble(r);105if (b1 < 0) {106break;107}108int b2 = nextNibble(r);109if (b2 < 0) {110throw new RuntimeException("Invalid string " + s);111}112int b = (b1 << 4) | b2;113out.write(b);114}115return out.toByteArray();116} catch (IOException e) {117throw new RuntimeException(e);118}119}120121private static int nextNibble(StringReader r) throws IOException {122while (true) {123int ch = r.read();124if (ch == -1) {125return -1;126} else if ((ch >= '0') && (ch <= '9')) {127return ch - '0';128} else if ((ch >= 'a') && (ch <= 'f')) {129return ch - 'a' + 10;130} else if ((ch >= 'A') && (ch <= 'F')) {131return ch - 'A' + 10;132}133}134}135136}137138139