jdk/test/com/sun/crypto/provider/KeyFactory/TestProviderLeak.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 101 3c3ec5dac510
child 4821 c4b695155c91
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
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
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 101
diff changeset
     2
 * Copyright 2005-2008 Sun Microsystems, Inc.  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
 *
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    21
 * have any questions.
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
 *
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    31
 * @run main/othervm -Xmx2m TestProviderLeak
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    32
 */
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    33
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    34
/*
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    35
 * 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
    36
 * 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
    37
 * 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
    38
 * 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
    39
 * the SunJCE provider."
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    40
 */
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    41
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    42
import javax.crypto.*;
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    43
import javax.crypto.spec.*;
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    44
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    45
public class TestProviderLeak {
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    46
    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
    47
        Runtime rt = Runtime.getRuntime();
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    48
        System.out.println(s + ":\t" +
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    49
            rt.freeMemory() + " bytes free");
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    50
    }
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    51
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    52
    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
    53
        SecretKeyFactory skf =
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    54
            SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1", "SunJCE");
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    55
        PBEKeySpec pbeKS = new PBEKeySpec(
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    56
            "passPhrase".toCharArray(), new byte [] { 0 }, 5, 512);
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    57
        for (int i = 0; i <= 1000; i++) {
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    58
            try {
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    59
                skf.generateSecret(pbeKS);
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    60
                if ((i % 20) == 0) {
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    61
                     // 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
    62
                     // Gives better output in leak cases.
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    63
                    System.gc();
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    64
                    dumpMemoryStats("Iteration " + i);
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    65
                }
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    66
            } catch (Exception e) {
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    67
                dumpMemoryStats("\nException seen at iteration " + i);
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    68
                throw e;
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    69
            }
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
    }
3c3ec5dac510 6578538: com.sun.crypto.provider.SunJCE instance leak using KRB5 and LoginContext
wetmore
parents:
diff changeset
    72
}