1/* 2 * Copyright (c) 2005, 2016, 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 * @modules java.smartcardio/javax.smartcardio 30 * @run main/manual TestTransmit 31 */ 32 33// This test requires special hardware. 34 35import java.io.BufferedReader; 36import java.io.ByteArrayOutputStream; 37import java.io.FileReader; 38import java.io.IOException; 39import java.io.StringReader; 40import javax.smartcardio.Card; 41import javax.smartcardio.CardChannel; 42import javax.smartcardio.CardTerminal; 43import javax.smartcardio.CommandAPDU; 44import javax.smartcardio.ResponseAPDU; 45 46public 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 // disconnect 94 card.disconnect(true); 95 96 System.out.println("OK."); 97 } 98 99 private static class Bytes { 100 final byte[] bytes; 101 final byte[] mask; 102 Bytes(byte[] bytes, byte[] mask) { 103 this.bytes = bytes; 104 this.mask = mask; 105 } 106 } 107 108 private static Bytes parseWildcard(String s) { 109 try { 110 int n = s.length(); 111 ByteArrayOutputStream out = new ByteArrayOutputStream(n >> 1); 112 ByteArrayOutputStream mask = new ByteArrayOutputStream(n >> 1); 113 StringReader r = new StringReader(s); 114 while (true) { 115 int b1 = nextNibble(r); 116 if (b1 < 0) { 117 if (b1 == -1) { 118 break; 119 } 120 int b2 = nextNibble(r); 121 if (b2 != -2) { 122 throw new RuntimeException("Invalid wildcard: " + s); 123 } 124 out.write(0); 125 mask.write(0); 126 continue; 127 } 128 int b2 = nextNibble(r); 129 if (b2 < 0) { 130 throw new RuntimeException("Invalid string " + s); 131 } 132 int b = (b1 << 4) | b2; 133 out.write(b); 134 mask.write(0xff); 135 } 136 byte[] b = out.toByteArray(); 137 byte[] m = mask.toByteArray(); 138 return new Bytes(b, m); 139 } catch (IOException e) { 140 throw new RuntimeException(e); 141 } 142 } 143 144 private static int nextNibble(StringReader r) throws IOException { 145 while (true) { 146 int ch = r.read(); 147 if (ch == -1) { 148 return -1; 149 } else if ((ch >= '0') && (ch <= '9')) { 150 return ch - '0'; 151 } else if ((ch >= 'a') && (ch <= 'f')) { 152 return ch - 'a' + 10; 153 } else if ((ch >= 'A') && (ch <= 'F')) { 154 return ch - 'A' + 10; 155 } else if (ch == 'X') { 156 return -2; 157 } 158 } 159 } 160 161} 162