1 /*
   2  * Copyright 2005-2007 Sun Microsystems, Inc.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6578538
  27  * @summary com.sun.crypto.provider.SunJCE instance leak using KRB5 and
  28  *     LoginContext
  29  * @author Brad Wetmore
  30  *
  31  * @run main/othervm -Xmx2m TestProviderLeak
  32  */
  33 
  34 /*
  35  * We force the leak to become a problem by specifying the minimum
  36  * size heap we can (above).  In current runs on a server and client
  37  * machine, it took roughly 220-240 iterations to have the memory leak
  38  * shut down other operations.  It complained about "Unable to verify
  39  * the SunJCE provider."
  40  */
  41 
  42 import javax.crypto.*;
  43 import javax.crypto.spec.*;
  44 
  45 public class TestProviderLeak {
  46     private static void dumpMemoryStats(String s) throws Exception {
  47         Runtime rt = Runtime.getRuntime();
  48         System.out.println(s + ":\t" +
  49             rt.freeMemory() + " bytes free");
  50     }
  51 
  52     public static void main(String [] args) throws Exception {
  53         SecretKeyFactory skf =
  54             SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1", "SunJCE");
  55         PBEKeySpec pbeKS = new PBEKeySpec(
  56             "passPhrase".toCharArray(), new byte [] { 0 }, 5, 512);
  57         for (int i = 0; i <= 600; i++) {
  58             try {
  59                 skf.generateSecret(pbeKS);
  60                 if ((i % 20) == 0) {
  61                      // Calling gc() isn't dependable, but doesn't hurt.
  62                      // Gives better output in leak cases.
  63                     System.gc();
  64                     dumpMemoryStats("Iteration " + i);
  65                 }
  66             } catch (Exception e) {
  67                 dumpMemoryStats("\nException seen at iteration " + i);
  68                 throw e;
  69             }
  70         }
  71     }
  72 }