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