blob: 6b9dc64a62b3e098d70079b66e1d0b555356d59c [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/**
25 * @test
26 * @bug 6445367
27 * @summary make sure serialization works
28 * @author Andreas Sterbenz
29 */
30
31import java.io.*;
32
33import javax.smartcardio.*;
34
35public class Serialize {
36
37 public static void main(String[] args) throws Exception {
38 ByteArrayOutputStream bout = new ByteArrayOutputStream();
39 ObjectOutputStream oout = new ObjectOutputStream(bout);
40
41 CommandAPDU c1 = new CommandAPDU(parse("00 A4 04 00 07 A0 00 00 00 62 81 01 00"));
42 ResponseAPDU 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"));
43 ATR a1 = new ATR(parse("3B 7F 18 00 00 00 31 C0 73 9E 01 0B 64 52 D9 04 00 82 90 00"));
44
45 oout.writeObject(c1);
46 oout.writeObject(r1);
47 oout.writeObject(a1);
48 oout.close();
49
50 ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
51 ObjectInputStream oin = new ObjectInputStream(bin);
52
53 CommandAPDU c2 = (CommandAPDU)oin.readObject();
54 ResponseAPDU r2 = (ResponseAPDU)oin.readObject();
55 ATR a2 = (ATR)oin.readObject();
56
57 if (!c2.equals(c1)) {
58 throw new Exception("CommandAPDU not equal");
59 }
60 if (c2.getNc() != 7) {
61 throw new Exception("Nc mismatch: " + c2.getNc());
62 }
63 if (c2.getNe() != 256) {
64 throw new Exception("Ne mismatch: " + c2.getNe());
65 }
66 if (c2.getINS() != 0xA4) {
67 throw new Exception("INS mismatch: " + c2.getINS());
68 }
69 if (!r2.equals(r1)) {
70 throw new Exception("ResponseAPDU not equal");
71 }
72 if (r2.getSW1() != 0x90) {
73 throw new Exception("SW1 mismatch: " + r2.getSW1());
74 }
75 if (!a2.equals(a1)) {
76 throw new Exception("ATR not equal");
77 }
78 if (!java.util.Arrays.equals(a2.getHistoricalBytes(), a1.getHistoricalBytes())) {
79 throw new Exception("Historical bytes mismatch");
80 }
81 System.out.println("OK");
82 }
83
84 private final static char[] hexDigits = "0123456789abcdef".toCharArray();
85
86 public static String toString(byte[] b) {
87 StringBuffer sb = new StringBuffer(b.length * 3);
88 for (int i = 0; i < b.length; i++) {
89 int k = b[i] & 0xff;
90 if (i != 0) {
91 sb.append(':');
92 }
93 sb.append(hexDigits[k >>> 4]);
94 sb.append(hexDigits[k & 0xf]);
95 }
96 return sb.toString();
97 }
98
99 public static byte[] parse(String s) {
100 try {
101 int n = s.length();
102 ByteArrayOutputStream out = new ByteArrayOutputStream(n >> 1);
103 StringReader r = new StringReader(s);
104 while (true) {
105 int b1 = nextNibble(r);
106 if (b1 < 0) {
107 break;
108 }
109 int b2 = nextNibble(r);
110 if (b2 < 0) {
111 throw new RuntimeException("Invalid string " + s);
112 }
113 int b = (b1 << 4) | b2;
114 out.write(b);
115 }
116 return out.toByteArray();
117 } catch (IOException e) {
118 throw new RuntimeException(e);
119 }
120 }
121
122 private static int nextNibble(StringReader r) throws IOException {
123 while (true) {
124 int ch = r.read();
125 if (ch == -1) {
126 return -1;
127 } else if ((ch >= '0') && (ch <= '9')) {
128 return ch - '0';
129 } else if ((ch >= 'a') && (ch <= 'f')) {
130 return ch - 'a' + 10;
131 } else if ((ch >= 'A') && (ch <= 'F')) {
132 return ch - 'A' + 10;
133 }
134 }
135 }
136
137}