Path: blob/master/test/jdk/sun/security/smartcardio/TestTransmit.java
41149 views
/*1* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6293769 6294527 647485826* @summary test transmit() works27* @author Andreas Sterbenz28* @modules java.smartcardio/javax.smartcardio29* @run main/manual TestTransmit30*/3132// This test requires special hardware.3334import java.io.BufferedReader;35import java.io.ByteArrayOutputStream;36import java.io.FileReader;37import java.io.IOException;38import java.io.StringReader;39import javax.smartcardio.Card;40import javax.smartcardio.CardChannel;41import javax.smartcardio.CardTerminal;42import javax.smartcardio.CommandAPDU;43import javax.smartcardio.ResponseAPDU;4445public class TestTransmit extends Utils {4647private final static String CMD_MARKER = "C-APDU: ";48private final static String RES_MARKER = "R-APDU: ";4950public static void main(String[] args) throws Exception {51CardTerminal terminal = getTerminal(args);52if (terminal == null) {53System.out.println("Skipping the test: " +54"no card terminals available");55return;56}5758Card card = terminal.connect("T=0");59CardChannel channel = card.getBasicChannel();6061BufferedReader reader = new BufferedReader(new FileReader("apdu.log"));6263byte[] command = null;64while (true) {65String line = reader.readLine();66if (line == null) {67break;68}69if (line.startsWith(CMD_MARKER)) {70System.out.println(line);71line = line.substring(CMD_MARKER.length());72command = parse(line);73} else if (line.startsWith(RES_MARKER)) {74System.out.println(line);75line = line.substring(RES_MARKER.length());76Bytes response = parseWildcard(line);77CommandAPDU capdu = new CommandAPDU(command);78ResponseAPDU rapdu = channel.transmit(capdu);79byte[] received = rapdu.getBytes();80if (received.length != response.bytes.length) {81throw new Exception("Length mismatch: " + toString(received));82}83for (int i = 0; i < received.length; i++) {84byte mask = response.mask[i];85if ((received[i] & response.mask[i]) != response.bytes[i]) {86throw new Exception("Mismatch: " + toString(received));87}88}89} // else ignore90}9192// JDK-6474858 : CardChannel.transmit(CommandAPDU) throws93// unexpected ArrayIndexOutOfBoundsException94{95CommandAPDU capdu2 = new CommandAPDU(0x00, 0xA4, 0x00, 0x00);96channel.transmit(capdu2);97}9899// disconnect100card.disconnect(true);101102System.out.println("OK.");103}104105private static class Bytes {106final byte[] bytes;107final byte[] mask;108Bytes(byte[] bytes, byte[] mask) {109this.bytes = bytes;110this.mask = mask;111}112}113114private static Bytes parseWildcard(String s) {115try {116int n = s.length();117ByteArrayOutputStream out = new ByteArrayOutputStream(n >> 1);118ByteArrayOutputStream mask = new ByteArrayOutputStream(n >> 1);119StringReader r = new StringReader(s);120while (true) {121int b1 = nextNibble(r);122if (b1 < 0) {123if (b1 == -1) {124break;125}126int b2 = nextNibble(r);127if (b2 != -2) {128throw new RuntimeException("Invalid wildcard: " + s);129}130out.write(0);131mask.write(0);132continue;133}134int b2 = nextNibble(r);135if (b2 < 0) {136throw new RuntimeException("Invalid string " + s);137}138int b = (b1 << 4) | b2;139out.write(b);140mask.write(0xff);141}142byte[] b = out.toByteArray();143byte[] m = mask.toByteArray();144return new Bytes(b, m);145} catch (IOException e) {146throw new RuntimeException(e);147}148}149150private static int nextNibble(StringReader r) throws IOException {151while (true) {152int ch = r.read();153if (ch == -1) {154return -1;155} else if ((ch >= '0') && (ch <= '9')) {156return ch - '0';157} else if ((ch >= 'a') && (ch <= 'f')) {158return ch - 'a' + 10;159} else if ((ch >= 'A') && (ch <= 'F')) {160return ch - 'A' + 10;161} else if (ch == 'X') {162return -2;163}164}165}166167}168169170