78N/A/*
3261N/A * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
78N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
78N/A *
78N/A * This code is free software; you can redistribute it and/or modify it
78N/A * under the terms of the GNU General Public License version 2 only, as
78N/A * published by the Free Software Foundation.
78N/A *
78N/A * This code is distributed in the hope that it will be useful, but WITHOUT
78N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
78N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
78N/A * version 2 for more details (a copy is included in the LICENSE file that
78N/A * accompanied this code).
78N/A *
78N/A * You should have received a copy of the GNU General Public License version
78N/A * 2 along with this work; if not, write to the Free Software Foundation,
78N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
78N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
78N/A */
78N/A
78N/A/*
78N/A * @test
78N/A * @bug 6578538
78N/A * @summary com.sun.crypto.provider.SunJCE instance leak using KRB5 and
78N/A * LoginContext
78N/A * @author Brad Wetmore
78N/A *
2119N/A * @run main/othervm -Xmx2m -XX:OldSize=1m -XX:NewSize=512k TestProviderLeak
2119N/A *
2119N/A * The original test invocation is below, but had to use the above
2119N/A * workaround for bug 6923123.
2119N/A *
2119N/A * run main/othervm -Xmx2m TestProviderLeak
78N/A */
78N/A
78N/A/*
78N/A * We force the leak to become a problem by specifying the minimum
78N/A * size heap we can (above). In current runs on a server and client
78N/A * machine, it took roughly 220-240 iterations to have the memory leak
78N/A * shut down other operations. It complained about "Unable to verify
78N/A * the SunJCE provider."
78N/A */
78N/A
78N/Aimport javax.crypto.*;
78N/Aimport javax.crypto.spec.*;
78N/A
78N/Apublic class TestProviderLeak {
78N/A private static void dumpMemoryStats(String s) throws Exception {
78N/A Runtime rt = Runtime.getRuntime();
78N/A System.out.println(s + ":\t" +
78N/A rt.freeMemory() + " bytes free");
78N/A }
78N/A
78N/A public static void main(String [] args) throws Exception {
78N/A SecretKeyFactory skf =
78N/A SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1", "SunJCE");
78N/A PBEKeySpec pbeKS = new PBEKeySpec(
78N/A "passPhrase".toCharArray(), new byte [] { 0 }, 5, 512);
78N/A for (int i = 0; i <= 1000; i++) {
78N/A try {
78N/A skf.generateSecret(pbeKS);
78N/A if ((i % 20) == 0) {
78N/A // Calling gc() isn't dependable, but doesn't hurt.
78N/A // Gives better output in leak cases.
78N/A System.gc();
78N/A dumpMemoryStats("Iteration " + i);
78N/A }
78N/A } catch (Exception e) {
78N/A dumpMemoryStats("\nException seen at iteration " + i);
78N/A throw e;
78N/A }
78N/A }
78N/A }
78N/A}