1 /*
   2  * Copyright (c) 2020 SAP SE. 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 import java.io.IOException;
  25 import java.lang.management.ManagementFactory;
  26 
  27 import javax.net.SocketFactory;
  28 import javax.net.ssl.SSLSocketFactory;
  29 
  30 import com.sun.management.UnixOperatingSystemMXBean;
  31 
  32 /*
  33  * @test
  34  * @bug 8256818 8257670 8257884
  35  * @summary Test that creating and closing SSL Sockets without bind/connect
  36  *          will not leave leaking socket file descriptors
  37  * @run main/native/manual/othervm SSLSocketLeak
  38  * @comment native library is required only on Windows, use the following commands:
  39  *          "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat"
  40  *          cl ^
  41  *              /LD ^
  42  *              <path/to/jdksrc>\jdk\test\sun\security\ssl\SSLSocketImpl\libFileUtils.c ^
  43  *              /I<path/to/jdkbin>\include ^
  44  *              /I<path/to/jdkbin>\include\win32 ^
  45  *              /FeFileUtils.dll
  46  *          jtreg <...> -nativepath:. <path/to/jdk8u>\jdk\test\sun\security\ssl\SSLSocketImpl\SSLSocketLeak.java
  47  */
  48 // Note: this test is not reliable, run it manually.
  49 public class SSLSocketLeak {
  50 
  51     // number of sockets to open/close
  52     private static final int NUM_TEST_SOCK = 500;
  53     private static final boolean IS_WINDOWS = System.getProperty("os.name").toLowerCase().startsWith("windows");
  54     private static volatile boolean nativeLibLoaded;
  55 
  56     // percentage of accepted growth of open handles
  57     private static final int OPEN_HANDLE_GROWTH_THRESHOLD = IS_WINDOWS ? 25 : 10;
  58 
  59     public static void main(String[] args) throws IOException {
  60         long fds_start = getProcessHandleCount();
  61         System.out.println("FDs at the beginning: " + fds_start);
  62 
  63         SocketFactory f = SSLSocketFactory.getDefault();
  64         for (int i = 0; i < NUM_TEST_SOCK; i++) {
  65             f.createSocket().close();
  66         }
  67 
  68         long fds_end = getProcessHandleCount();
  69         System.out.println("FDs in the end: " + fds_end);
  70 
  71         if ((fds_end - fds_start) > (NUM_TEST_SOCK / OPEN_HANDLE_GROWTH_THRESHOLD)) {
  72             throw new RuntimeException("Too many open file descriptors. Looks leaky.");
  73         }
  74     }
  75 
  76     // Return the current process handle count
  77     private static long getProcessHandleCount() {
  78         if (IS_WINDOWS) {
  79             if (!nativeLibLoaded) {
  80                 System.loadLibrary("FileUtils");
  81                 nativeLibLoaded = true;
  82             }
  83             return getWinProcessHandleCount();
  84         } else {
  85             return ((UnixOperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean()).getOpenFileDescriptorCount();
  86         }
  87     }
  88 
  89     private static native long getWinProcessHandleCount();
  90 }