1 /*
   2  * Copyright (c) 2006, 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 6445367
  27  * @summary make sure serialization works
  28  * @author Andreas Sterbenz
  29  * @compile --add-modules=java.smartcardio Serialize.java
  30  * @run main/othervm --add-modules=java.smartcardio Serialize
  31  */
  32 
  33 import java.io.*;
  34 
  35 import javax.smartcardio.*;
  36 
  37 public class Serialize {
  38 
  39     public static void main(String[] args) throws Exception {
  40         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  41         ObjectOutputStream oout = new ObjectOutputStream(bout);
  42 
  43         CommandAPDU c1 = new CommandAPDU(parse("00 A4 04 00 07 A0 00 00 00 62 81 01 00"));
  44         ResponseAPDU r1 = new ResponseAPDU(parse("07 A0 00 00 00 62 81 01 04 01 00 00 24 05 00 0B 04 B0 25 90 00"));
  45         ATR a1 = new ATR(parse("3B 7F 18 00 00 00 31 C0 73 9E 01 0B 64 52 D9 04 00 82 90 00"));
  46 
  47         oout.writeObject(c1);
  48         oout.writeObject(r1);
  49         oout.writeObject(a1);
  50         oout.close();
  51 
  52         ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
  53         ObjectInputStream oin = new ObjectInputStream(bin);
  54 
  55         CommandAPDU c2 = (CommandAPDU)oin.readObject();
  56         ResponseAPDU r2 = (ResponseAPDU)oin.readObject();
  57         ATR a2 = (ATR)oin.readObject();
  58 
  59         if (!c2.equals(c1)) {
  60             throw new Exception("CommandAPDU not equal");
  61         }
  62         if (c2.getNc() != 7) {
  63             throw new Exception("Nc mismatch: " + c2.getNc());
  64         }
  65         if (c2.getNe() != 256) {
  66             throw new Exception("Ne mismatch: " + c2.getNe());
  67         }
  68         if (c2.getINS() != 0xA4) {
  69             throw new Exception("INS mismatch: " + c2.getINS());
  70         }
  71         if (!r2.equals(r1)) {
  72             throw new Exception("ResponseAPDU not equal");
  73         }
  74         if (r2.getSW1() != 0x90) {
  75             throw new Exception("SW1 mismatch: " + r2.getSW1());
  76         }
  77         if (!a2.equals(a1)) {
  78             throw new Exception("ATR not equal");
  79         }
  80         if (!java.util.Arrays.equals(a2.getHistoricalBytes(), a1.getHistoricalBytes())) {
  81             throw new Exception("Historical bytes mismatch");
  82         }
  83         System.out.println("OK");
  84     }
  85 
  86     private final static char[] hexDigits = "0123456789abcdef".toCharArray();
  87 
  88     public static String toString(byte[] b) {
  89         StringBuffer sb = new StringBuffer(b.length * 3);
  90         for (int i = 0; i < b.length; i++) {
  91             int k = b[i] & 0xff;
  92             if (i != 0) {
  93                 sb.append(':');
  94             }
  95             sb.append(hexDigits[k >>> 4]);
  96             sb.append(hexDigits[k & 0xf]);
  97         }
  98         return sb.toString();
  99     }
 100 
 101     public static byte[] parse(String s) {
 102         try {
 103             int n = s.length();
 104             ByteArrayOutputStream out = new ByteArrayOutputStream(n >> 1);
 105             StringReader r = new StringReader(s);
 106             while (true) {
 107                 int b1 = nextNibble(r);
 108                 if (b1 < 0) {
 109                     break;
 110                 }
 111                 int b2 = nextNibble(r);
 112                 if (b2 < 0) {
 113                     throw new RuntimeException("Invalid string " + s);
 114                 }
 115                 int b = (b1 << 4) | b2;
 116                 out.write(b);
 117             }
 118             return out.toByteArray();
 119         } catch (IOException e) {
 120             throw new RuntimeException(e);
 121         }
 122     }
 123 
 124     private static int nextNibble(StringReader r) throws IOException {
 125         while (true) {
 126             int ch = r.read();
 127             if (ch == -1) {
 128                 return -1;
 129             } else if ((ch >= '0') && (ch <= '9')) {
 130                 return ch - '0';
 131             } else if ((ch >= 'a') && (ch <= 'f')) {
 132                 return ch - 'a' + 10;
 133             } else if ((ch >= 'A') && (ch <= 'F')) {
 134                 return ch - 'A' + 10;
 135             }
 136         }
 137     }
 138 
 139 }