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  * @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         if (terminal == null) {
  53             System.out.println("Skipping the test: " +
  54                     "no card terminals available");
  55             return;
  56         }
  57 
  58         Card card = terminal.connect("T=0");
  59         CardChannel channel = card.getBasicChannel();
  60 
  61         BufferedReader reader = new BufferedReader(new FileReader("apdu.log"));
  62 
  63         byte[] command = null;
  64         while (true) {
  65             String line = reader.readLine();
  66             if (line == null) {
  67                 break;
  68             }
  69             if (line.startsWith(CMD_MARKER)) {
  70                 System.out.println(line);
  71                 line = line.substring(CMD_MARKER.length());
  72                 command = parse(line);
  73             } else if (line.startsWith(RES_MARKER)) {
  74                 System.out.println(line);
  75                 line = line.substring(RES_MARKER.length());
  76                 Bytes response = parseWildcard(line);
  77                 CommandAPDU capdu = new CommandAPDU(command);
  78                 ResponseAPDU rapdu = channel.transmit(capdu);
  79                 byte[] received = rapdu.getBytes();
  80                 if (received.length != response.bytes.length) {
  81                     throw new Exception("Length mismatch: " + toString(received));
  82                 }
  83                 for (int i = 0; i < received.length; i++) {
  84                     byte mask = response.mask[i];
  85                     if ((received[i] & response.mask[i]) != response.bytes[i]) {
  86                         throw new Exception("Mismatch: " + toString(received));
  87                     }
  88                 }
  89             } // else ignore
  90         }
  91 
  92         // disconnect
  93         card.disconnect(true);
  94 
  95         System.out.println("OK.");
  96     }
  97 
  98     private static class Bytes {
  99         final byte[] bytes;
 100         final byte[] mask;
 101         Bytes(byte[] bytes, byte[] mask) {
 102             this.bytes = bytes;
 103             this.mask = mask;
 104         }
 105     }
 106 
 107     private static Bytes parseWildcard(String s) {
 108         try {
 109             int n = s.length();
 110             ByteArrayOutputStream out = new ByteArrayOutputStream(n >> 1);
 111             ByteArrayOutputStream mask = new ByteArrayOutputStream(n >> 1);
 112             StringReader r = new StringReader(s);
 113             while (true) {
 114                 int b1 = nextNibble(r);
 115                 if (b1 < 0) {
 116                     if (b1 == -1) {
 117                         break;
 118                     }
 119                     int b2 = nextNibble(r);
 120                     if (b2 != -2) {
 121                         throw new RuntimeException("Invalid wildcard: " + s);
 122                     }
 123                     out.write(0);
 124                     mask.write(0);
 125                     continue;
 126                 }
 127                 int b2 = nextNibble(r);
 128                 if (b2 < 0) {
 129                     throw new RuntimeException("Invalid string " + s);
 130                 }
 131                 int b = (b1 << 4) | b2;
 132                 out.write(b);
 133                 mask.write(0xff);
 134             }
 135             byte[] b = out.toByteArray();
 136             byte[] m = mask.toByteArray();
 137             return new Bytes(b, m);
 138         } catch (IOException e) {
 139             throw new RuntimeException(e);
 140         }
 141     }
 142 
 143     private static int nextNibble(StringReader r) throws IOException {
 144         while (true) {
 145             int ch = r.read();
 146             if (ch == -1) {
 147                 return -1;
 148             } else if ((ch >= '0') && (ch <= '9')) {
 149                 return ch - '0';
 150             } else if ((ch >= 'a') && (ch <= 'f')) {
 151                 return ch - 'a' + 10;
 152             } else if ((ch >= 'A') && (ch <= 'F')) {
 153                 return ch - 'A' + 10;
 154             } else if (ch == 'X') {
 155                 return -2;
 156             }
 157         }
 158     }
 159 
 160 }