jdk/test/sun/security/pkcs11/SecureRandom/TestDeserialization.java
author lana
Thu, 26 Dec 2013 12:04:16 -0800
changeset 23010 6dadb192ad81
parent 5506 202f599c92aa
child 40975 680639c9b307
permissions -rw-r--r--
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013 Summary: updated files with 2011, 2012 and 2013 years according to the file's last updated date Reviewed-by: tbell, lancea, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5157
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5157
diff changeset
     2
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
5157
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
     4
 *
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
     7
 * published by the Free Software Foundation.
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
     8
 *
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    13
 * accompanied this code).
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    14
 *
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5157
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5157
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5157
diff changeset
    21
 * questions.
5157
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    22
 */
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    23
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    24
/**
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    25
 * @test
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    26
 * @bug 6837847
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    27
 * @summary Ensure a deserialized PKCS#11 SecureRandom is functional.
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    28
 * @library ..
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    29
 */
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    30
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    31
import java.security.*;
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    32
import java.io.*;
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    33
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    34
public class TestDeserialization extends PKCS11Test {
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    35
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    36
    public void main(Provider p) throws Exception {
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    37
        // Skip this test for providers not found by java.security.Security
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    38
        if (Security.getProvider(p.getName()) != p) {
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    39
            System.out.println("Skip test for provider " + p.getName());
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    40
            return;
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    41
        }
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    42
        SecureRandom r;
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    43
        try {
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    44
            r = SecureRandom.getInstance("PKCS11", p);
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    45
            System.out.println("SecureRandom instance " + r);
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    46
        } catch (NoSuchAlgorithmException e) {
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    47
            System.out.println("Provider " + p +
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    48
                               " does not support SecureRandom, skipping");
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    49
            e.printStackTrace();
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    50
            return;
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    51
        }
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    52
        r.setSeed(System.currentTimeMillis());
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    53
        byte[] buf = new byte[16];
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    54
        byte[] ser = toByteArray(r);
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    55
        System.out.println("Serialized Len = " + ser.length);
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    56
        SecureRandom r2 = fromByteArray(ser);
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    57
        System.out.println("Deserialized into " + r2);
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    58
        r2.nextBytes(buf);
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    59
        System.out.println("Done");
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    60
    }
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    61
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    62
    public static void main(String[] args) throws Exception {
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    63
        main(new TestDeserialization());
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    64
    }
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    65
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    66
    private byte[] toByteArray(SecureRandom r) throws Exception {
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    67
        ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    68
        ObjectOutputStream outStream = null;
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    69
        try {
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    70
            outStream = new ObjectOutputStream(out);
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    71
            outStream.writeObject(r);
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    72
            return out.toByteArray();
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    73
        } finally {
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    74
            if (outStream != null) {
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    75
                outStream.close();
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    76
            }
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    77
        }
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    78
    }
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    79
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    80
    private SecureRandom fromByteArray(byte[] buf) throws Exception {
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    81
        SecureRandom r = null;
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    82
        ByteArrayInputStream is = new ByteArrayInputStream(buf);
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    83
        ObjectInputStream ois = null;
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    84
        try {
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    85
            ois = new ObjectInputStream(is);
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    86
            r = (SecureRandom) ois.readObject();
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    87
        } finally {
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    88
            if (ois != null) {
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    89
                ois.close();
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    90
            }
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    91
        }
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    92
        return r;
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    93
    }
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    94
}