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