1 /*
   2  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * 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 -XX:OldSize=1m -XX:NewSize=512k TestProviderLeak
  32  *
  33  * The original test invocation is below, but had to use the above
  34  * workaround for bug 6923123.
  35  *
  36  * run main/othervm -Xmx2m TestProviderLeak
  37  */
  38 
  39 /*
  40  * We force the leak to become a problem by specifying the minimum
  41  * size heap we can (above).  In current runs on a server and client
  42  * machine, it took roughly 220-240 iterations to have the memory leak
  43  * shut down other operations.  It complained about "Unable to verify
  44  * the SunJCE provider."
  45  */
  46 
  47 import javax.crypto.*;
  48 import javax.crypto.spec.*;
  49 
  50 public class TestProviderLeak {
  51     private static void dumpMemoryStats(String s) throws Exception {
  52         Runtime rt = Runtime.getRuntime();
  53         System.out.println(s + ":\t" +
  54             rt.freeMemory() + " bytes free");
  55     }
  56 
  57     public static void main(String [] args) throws Exception {
  58         SecretKeyFactory skf =
  59             SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1", "SunJCE");
  60         PBEKeySpec pbeKS = new PBEKeySpec(
  61             "passPhrase".toCharArray(), new byte [] { 0 }, 5, 512);
  62         for (int i = 0; i <= 1000; i++) {
  63             try {
  64                 skf.generateSecret(pbeKS);
  65                 if ((i % 20) == 0) {
  66                      // Calling gc() isn't dependable, but doesn't hurt.
  67                      // Gives better output in leak cases.
  68                     System.gc();
  69                     dumpMemoryStats("Iteration " + i);
  70                 }
  71             } catch (Exception e) {
  72                 dumpMemoryStats("\nException seen at iteration " + i);
  73                 throw e;
  74             }
  75         }
  76     }
  77 }