jdk/test/com/sun/crypto/provider/KeyFactory/TestProviderLeak.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5506 202f599c92aa
child 13251 58f0a0823496
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
101
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5506
diff changeset
     2
 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
101
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
     4
 *
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
     8
 *
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    13
 * accompanied this code).
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    14
 *
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4821
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4821
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4821
diff changeset
    21
 * questions.
101
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    22
 */
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    23
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    24
/*
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    25
 * @test
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    26
 * @bug 6578538
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    27
 * @summary com.sun.crypto.provider.SunJCE instance leak using KRB5 and
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    28
 *     LoginContext
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    29
 * @author Brad Wetmore
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    30
 *
4821
c4b695155c91 6923976: TestProviderLeak.java is using too small of an initial heap under newer Hotspot (b79+)
wetmore
parents: 715
diff changeset
    31
 * @run main/othervm -Xmx2m -XX:OldSize=1m -XX:NewSize=512k TestProviderLeak
c4b695155c91 6923976: TestProviderLeak.java is using too small of an initial heap under newer Hotspot (b79+)
wetmore
parents: 715
diff changeset
    32
 *
c4b695155c91 6923976: TestProviderLeak.java is using too small of an initial heap under newer Hotspot (b79+)
wetmore
parents: 715
diff changeset
    33
 * The original test invocation is below, but had to use the above
c4b695155c91 6923976: TestProviderLeak.java is using too small of an initial heap under newer Hotspot (b79+)
wetmore
parents: 715
diff changeset
    34
 * workaround for bug 6923123.
c4b695155c91 6923976: TestProviderLeak.java is using too small of an initial heap under newer Hotspot (b79+)
wetmore
parents: 715
diff changeset
    35
 *
c4b695155c91 6923976: TestProviderLeak.java is using too small of an initial heap under newer Hotspot (b79+)
wetmore
parents: 715
diff changeset
    36
 * run main/othervm -Xmx2m TestProviderLeak
101
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    37
 */
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    38
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    39
/*
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    40
 * We force the leak to become a problem by specifying the minimum
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    41
 * size heap we can (above).  In current runs on a server and client
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    42
 * machine, it took roughly 220-240 iterations to have the memory leak
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    43
 * shut down other operations.  It complained about "Unable to verify
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    44
 * the SunJCE provider."
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    45
 */
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    46
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    47
import javax.crypto.*;
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    48
import javax.crypto.spec.*;
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    49
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    50
public class TestProviderLeak {
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    51
    private static void dumpMemoryStats(String s) throws Exception {
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    52
        Runtime rt = Runtime.getRuntime();
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    53
        System.out.println(s + ":\t" +
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    54
            rt.freeMemory() + " bytes free");
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    55
    }
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    56
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    57
    public static void main(String [] args) throws Exception {
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    58
        SecretKeyFactory skf =
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    59
            SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1", "SunJCE");
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    60
        PBEKeySpec pbeKS = new PBEKeySpec(
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    61
            "passPhrase".toCharArray(), new byte [] { 0 }, 5, 512);
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    62
        for (int i = 0; i <= 1000; i++) {
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    63
            try {
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    64
                skf.generateSecret(pbeKS);
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    65
                if ((i % 20) == 0) {
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    66
                     // Calling gc() isn't dependable, but doesn't hurt.
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    67
                     // Gives better output in leak cases.
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    68
                    System.gc();
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    69
                    dumpMemoryStats("Iteration " + i);
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    70
                }
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    71
            } catch (Exception e) {
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    72
                dumpMemoryStats("\nException seen at iteration " + i);
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    73
                throw e;
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    74
            }
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    75
        }
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    76
    }
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    77
}