jdk/test/javax/smartcardio/Serialize.java
author alanb
Tue, 03 May 2016 09:09:57 +0100
changeset 37779 7c84df693837
parent 5506 202f599c92aa
child 40261 86a49ba76f52
permissions -rw-r--r--
8154956: Module system implementation refresh (4/2016) Reviewed-by: alanb, mchung, chegar, redestad Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com, erik.joelsson@oracle.com, chris.hegarty@oracle.com, peter.levart@gmail.com, sundararajan.athijegannathan@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6445367
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary make sure serialization works
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Andreas Sterbenz
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 5506
diff changeset
    29
 * @compile -addmods java.smartcardio Serialize.java
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 5506
diff changeset
    30
 * @run main/othervm -addmods java.smartcardio Serialize
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.smartcardio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class Serialize {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        ObjectOutputStream oout = new ObjectOutputStream(bout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        CommandAPDU c1 = new CommandAPDU(parse("00 A4 04 00 07 A0 00 00 00 62 81 01 00"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        oout.writeObject(c1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        oout.writeObject(r1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        oout.writeObject(a1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        oout.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        ObjectInputStream oin = new ObjectInputStream(bin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        CommandAPDU c2 = (CommandAPDU)oin.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        ResponseAPDU r2 = (ResponseAPDU)oin.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        ATR a2 = (ATR)oin.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (!c2.equals(c1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            throw new Exception("CommandAPDU not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        if (c2.getNc() != 7) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            throw new Exception("Nc mismatch: " + c2.getNc());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        if (c2.getNe() != 256) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            throw new Exception("Ne mismatch: " + c2.getNe());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (c2.getINS() != 0xA4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            throw new Exception("INS mismatch: " + c2.getINS());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        if (!r2.equals(r1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            throw new Exception("ResponseAPDU not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (r2.getSW1() != 0x90) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            throw new Exception("SW1 mismatch: " + r2.getSW1());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (!a2.equals(a1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            throw new Exception("ATR not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (!java.util.Arrays.equals(a2.getHistoricalBytes(), a1.getHistoricalBytes())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            throw new Exception("Historical bytes mismatch");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        System.out.println("OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private final static char[] hexDigits = "0123456789abcdef".toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public static String toString(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        StringBuffer sb = new StringBuffer(b.length * 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        for (int i = 0; i < b.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            int k = b[i] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            if (i != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                sb.append(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            sb.append(hexDigits[k >>> 4]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            sb.append(hexDigits[k & 0xf]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public static byte[] parse(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            int n = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            ByteArrayOutputStream out = new ByteArrayOutputStream(n >> 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            StringReader r = new StringReader(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                int b1 = nextNibble(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                if (b1 < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                int b2 = nextNibble(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                if (b2 < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    throw new RuntimeException("Invalid string " + s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                int b = (b1 << 4) | b2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                out.write(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            return out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private static int nextNibble(StringReader r) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            int ch = r.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            if (ch == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            } else if ((ch >= '0') && (ch <= '9')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                return ch - '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            } else if ((ch >= 'a') && (ch <= 'f')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                return ch - 'a' + 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            } else if ((ch >= 'A') && (ch <= 'F')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                return ch - 'A' + 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
}