Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/smartcardio/TestTransmit.java
38840 views
/*1* Copyright (c) 2005, 2016, 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 629452726* @summary test transmit() works27* @author Andreas Sterbenz28* @ignore requires special hardware29* @run main/manual TestTransmit30*/3132import java.io.BufferedReader;33import java.io.ByteArrayOutputStream;34import java.io.FileReader;35import java.io.IOException;36import java.io.StringReader;37import javax.smartcardio.Card;38import javax.smartcardio.CardChannel;39import javax.smartcardio.CardTerminal;40import javax.smartcardio.CommandAPDU;41import javax.smartcardio.ResponseAPDU;4243public class TestTransmit extends Utils {4445private final static String CMD_MARKER = "C-APDU: ";46private final static String RES_MARKER = "R-APDU: ";4748public static void main(String[] args) throws Exception {49CardTerminal terminal = getTerminal(args);50if (terminal == null) {51System.out.println("Skipping the test: " +52"no card terminals available");53return;54}5556Card card = terminal.connect("T=0");57CardChannel channel = card.getBasicChannel();5859BufferedReader reader = new BufferedReader(new FileReader("apdu.log"));6061byte[] command = null;62while (true) {63String line = reader.readLine();64if (line == null) {65break;66}67if (line.startsWith(CMD_MARKER)) {68System.out.println(line);69line = line.substring(CMD_MARKER.length());70command = parse(line);71} else if (line.startsWith(RES_MARKER)) {72System.out.println(line);73line = line.substring(RES_MARKER.length());74Bytes response = parseWildcard(line);75CommandAPDU capdu = new CommandAPDU(command);76ResponseAPDU rapdu = channel.transmit(capdu);77byte[] received = rapdu.getBytes();78if (received.length != response.bytes.length) {79throw new Exception("Length mismatch: " + toString(received));80}81for (int i = 0; i < received.length; i++) {82byte mask = response.mask[i];83if ((received[i] & response.mask[i]) != response.bytes[i]) {84throw new Exception("Mismatch: " + toString(received));85}86}87} // else ignore88}8990// disconnect91card.disconnect(true);9293System.out.println("OK.");94}9596private static class Bytes {97final byte[] bytes;98final byte[] mask;99Bytes(byte[] bytes, byte[] mask) {100this.bytes = bytes;101this.mask = mask;102}103}104105private static Bytes parseWildcard(String s) {106try {107int n = s.length();108ByteArrayOutputStream out = new ByteArrayOutputStream(n >> 1);109ByteArrayOutputStream mask = new ByteArrayOutputStream(n >> 1);110StringReader r = new StringReader(s);111while (true) {112int b1 = nextNibble(r);113if (b1 < 0) {114if (b1 == -1) {115break;116}117int b2 = nextNibble(r);118if (b2 != -2) {119throw new RuntimeException("Invalid wildcard: " + s);120}121out.write(0);122mask.write(0);123continue;124}125int b2 = nextNibble(r);126if (b2 < 0) {127throw new RuntimeException("Invalid string " + s);128}129int b = (b1 << 4) | b2;130out.write(b);131mask.write(0xff);132}133byte[] b = out.toByteArray();134byte[] m = mask.toByteArray();135return new Bytes(b, m);136} catch (IOException e) {137throw new RuntimeException(e);138}139}140141private static int nextNibble(StringReader r) throws IOException {142while (true) {143int ch = r.read();144if (ch == -1) {145return -1;146} else if ((ch >= '0') && (ch <= '9')) {147return ch - '0';148} else if ((ch >= 'a') && (ch <= 'f')) {149return ch - 'a' + 10;150} else if ((ch >= 'A') && (ch <= 'F')) {151return ch - 'A' + 10;152} else if (ch == 'X') {153return -2;154}155}156}157158}159160161