jdk/test/javax/smartcardio/Serialize.java
author naoto
Mon, 18 Oct 2010 14:45:00 -0700
changeset 6843 6ab7e78c51eb
parent 5506 202f599c92aa
child 37779 7c84df693837
permissions -rw-r--r--
6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set Reviewed-by: srl Contributed-by: y.umaoka@gmail.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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.smartcardio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class Serialize {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        ObjectOutputStream oout = new ObjectOutputStream(bout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        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
    42
        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
    43
        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
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        oout.writeObject(c1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        oout.writeObject(r1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        oout.writeObject(a1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        oout.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        ObjectInputStream oin = new ObjectInputStream(bin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        CommandAPDU c2 = (CommandAPDU)oin.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        ResponseAPDU r2 = (ResponseAPDU)oin.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        ATR a2 = (ATR)oin.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        if (!c2.equals(c1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            throw new Exception("CommandAPDU not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        if (c2.getNc() != 7) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            throw new Exception("Nc mismatch: " + c2.getNc());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if (c2.getNe() != 256) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            throw new Exception("Ne mismatch: " + c2.getNe());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        if (c2.getINS() != 0xA4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            throw new Exception("INS mismatch: " + c2.getINS());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        if (!r2.equals(r1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            throw new Exception("ResponseAPDU not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (r2.getSW1() != 0x90) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            throw new Exception("SW1 mismatch: " + r2.getSW1());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (!a2.equals(a1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            throw new Exception("ATR not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (!java.util.Arrays.equals(a2.getHistoricalBytes(), a1.getHistoricalBytes())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            throw new Exception("Historical bytes mismatch");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        System.out.println("OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private final static char[] hexDigits = "0123456789abcdef".toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public static String toString(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        StringBuffer sb = new StringBuffer(b.length * 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        for (int i = 0; i < b.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            int k = b[i] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            if (i != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                sb.append(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            sb.append(hexDigits[k >>> 4]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            sb.append(hexDigits[k & 0xf]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public static byte[] parse(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            int n = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            ByteArrayOutputStream out = new ByteArrayOutputStream(n >> 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            StringReader r = new StringReader(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                int b1 = nextNibble(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                if (b1 < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                int b2 = nextNibble(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                if (b2 < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    throw new RuntimeException("Invalid string " + s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                int b = (b1 << 4) | b2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                out.write(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            return out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private static int nextNibble(StringReader r) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            int ch = r.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            if (ch == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            } else if ((ch >= '0') && (ch <= '9')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                return ch - '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            } else if ((ch >= 'a') && (ch <= 'f')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                return ch - 'a' + 10;
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
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
}