jdk/test/sun/security/pkcs11/SecureRandom/TestDeserialization.java
author skovalev
Thu, 15 Sep 2016 13:03:03 +0300
changeset 40975 680639c9b307
parent 5506 202f599c92aa
child 42693 6645de32a866
permissions -rw-r--r--
8165689: Fix module dependencies for sun/security/pkcs11/* tests Reviewed-by: mullan
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
/*
40975
680639c9b307 8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents: 5506
diff changeset
     2
 * Copyright (c) 2010, 2016, 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
40975
680639c9b307 8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents: 5506
diff changeset
    24
/*
5157
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 ..
40975
680639c9b307 8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents: 5506
diff changeset
    29
 * @modules jdk.crypto.pkcs11
5157
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
40975
680639c9b307 8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents: 5506
diff changeset
    32
import java.io.ByteArrayInputStream;
680639c9b307 8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents: 5506
diff changeset
    33
import java.io.ByteArrayOutputStream;
680639c9b307 8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents: 5506
diff changeset
    34
import java.io.ObjectInputStream;
680639c9b307 8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents: 5506
diff changeset
    35
import java.io.ObjectOutputStream;
680639c9b307 8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents: 5506
diff changeset
    36
import java.security.NoSuchAlgorithmException;
680639c9b307 8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents: 5506
diff changeset
    37
import java.security.Provider;
680639c9b307 8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents: 5506
diff changeset
    38
import java.security.SecureRandom;
680639c9b307 8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents: 5506
diff changeset
    39
import java.security.Security;
5157
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    40
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    41
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
    42
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    43
    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
    44
        // 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
    45
        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
    46
            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
    47
            return;
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    48
        }
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    49
        SecureRandom r;
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    50
        try {
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    51
            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
    52
            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
    53
        } catch (NoSuchAlgorithmException e) {
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    54
            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
    55
                               " 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
    56
            e.printStackTrace();
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    57
            return;
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    58
        }
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    59
        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
    60
        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
    61
        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
    62
        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
    63
        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
    64
        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
    65
        r2.nextBytes(buf);
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    66
        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
    67
    }
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    68
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    69
    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
    70
        main(new TestDeserialization());
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    71
    }
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    72
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    73
    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
    74
        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
    75
        ObjectOutputStream outStream = null;
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    76
        try {
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    77
            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
    78
            outStream.writeObject(r);
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    79
            return out.toByteArray();
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    80
        } finally {
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    81
            if (outStream != null) {
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    82
                outStream.close();
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    83
            }
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    84
        }
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    85
    }
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    86
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    87
    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
    88
        SecureRandom r = null;
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    89
        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
    90
        ObjectInputStream ois = null;
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    91
        try {
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    92
            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
    93
            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
    94
        } finally {
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    95
            if (ois != null) {
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    96
                ois.close();
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    97
            }
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    98
        }
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
    99
        return r;
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
   100
    }
eccef47ec254 6837847: PKCS#11 A SecureRandom and a serialization error following installation of 1.5.0_18
valeriep
parents:
diff changeset
   101
}