blob: 73ade737ed1344364b340c1e4a916094b6ad53d6 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005-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 6293769 6294527
27 * @summary test transmit() works
28 * @author Andreas Sterbenz
29 * @ignore requires special hardware
30 * @run main/manual TestTransmit
31 */
32
33import java.io.*;
34import java.util.*;
35
36import javax.smartcardio.*;
37
38public class TestTransmit extends Utils {
39
40 private final static String CMD_MARKER = "C-APDU: ";
41 private final static String RES_MARKER = "R-APDU: ";
42
43 public static void main(String[] args) throws Exception {
44 CardTerminal terminal = getTerminal(args);
45
46 Card card = terminal.connect("T=0");
47 CardChannel channel = card.getBasicChannel();
48
49 BufferedReader reader = new BufferedReader(new FileReader("apdu.log"));
50
51 byte[] command = null;
52 while (true) {
53 String line = reader.readLine();
54 if (line == null) {
55 break;
56 }
57 if (line.startsWith(CMD_MARKER)) {
58 System.out.println(line);
59 line = line.substring(CMD_MARKER.length());
60 command = parse(line);
61 } else if (line.startsWith(RES_MARKER)) {
62 System.out.println(line);
63 line = line.substring(RES_MARKER.length());
64 Bytes response = parseWildcard(line);
65 CommandAPDU capdu = new CommandAPDU(command);
66 ResponseAPDU rapdu = channel.transmit(capdu);
67 byte[] received = rapdu.getBytes();
68 if (received.length != response.bytes.length) {
69 throw new Exception("Length mismatch: " + toString(received));
70 }
71 for (int i = 0; i < received.length; i++) {
72 byte mask = response.mask[i];
73 if ((received[i] & response.mask[i]) != response.bytes[i]) {
74 throw new Exception("Mismatch: " + toString(received));
75 }
76 }
77 } // else ignore
78 }
79
80 // disconnect
81 card.disconnect(false);
82
83 System.out.println("OK.");
84 }
85
86 private static class Bytes {
87 final byte[] bytes;
88 final byte[] mask;
89 Bytes(byte[] bytes, byte[] mask) {
90 this.bytes = bytes;
91 this.mask = mask;
92 }
93 }
94
95 private static Bytes parseWildcard(String s) {
96 try {
97 int n = s.length();
98 ByteArrayOutputStream out = new ByteArrayOutputStream(n >> 1);
99 ByteArrayOutputStream mask = new ByteArrayOutputStream(n >> 1);
100 StringReader r = new StringReader(s);
101 while (true) {
102 int b1 = nextNibble(r);
103 if (b1 < 0) {
104 if (b1 == -1) {
105 break;
106 }
107 int b2 = nextNibble(r);
108 if (b2 != -2) {
109 throw new RuntimeException("Invalid wildcard: " + s);
110 }
111 out.write(0);
112 mask.write(0);
113 continue;
114 }
115 int b2 = nextNibble(r);
116 if (b2 < 0) {
117 throw new RuntimeException("Invalid string " + s);
118 }
119 int b = (b1 << 4) | b2;
120 out.write(b);
121 mask.write(0xff);
122 }
123 byte[] b = out.toByteArray();
124 byte[] m = mask.toByteArray();
125 return new Bytes(b, m);
126 } catch (IOException e) {
127 throw new RuntimeException(e);
128 }
129 }
130
131 private static int nextNibble(StringReader r) throws IOException {
132 while (true) {
133 int ch = r.read();
134 if (ch == -1) {
135 return -1;
136 } else if ((ch >= '0') && (ch <= '9')) {
137 return ch - '0';
138 } else if ((ch >= 'a') && (ch <= 'f')) {
139 return ch - 'a' + 10;
140 } else if ((ch >= 'A') && (ch <= 'F')) {
141 return ch - 'A' + 10;
142 } else if (ch == 'X') {
143 return -2;
144 }
145 }
146 }
147
148}