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 8257997
  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 public class SSLSocketLeak {
  49 
  50     // number of sockets to open/close
  51     private static final int NUM_TEST_SOCK = 500;
  52     private static final boolean IS_WINDOWS = System.getProperty("os.name").toLowerCase().startsWith("windows");
  53     private static volatile boolean nativeLibLoaded;
  54 
  55     // percentage of accepted growth of open handles
  56     private static final int OPEN_HANDLE_GROWTH_THRESHOLD_PERCENTAGE = IS_WINDOWS ? 25 : 10;
  57 
  58     public static void main(String[] args) throws IOException {
  59         long fds_start = getProcessHandleCount();
  60         System.out.println("FDs at the beginning: " + fds_start);
  61 
  62         SocketFactory f = SSLSocketFactory.getDefault();
  63         for (int i = 0; i < NUM_TEST_SOCK; i++) {
  64             f.createSocket().close();
  65         }
  66 
  67         long fds_end = getProcessHandleCount();
  68         System.out.println("FDs in the end: " + fds_end);
  69 
  70         if ((fds_end - fds_start) > ((NUM_TEST_SOCK * OPEN_HANDLE_GROWTH_THRESHOLD_PERCENTAGE)) / 100) {
  71             throw new RuntimeException("Too many open file descriptors. Looks leaky.");
  72         }
  73     }
  74 
  75     // Return the current process handle count
  76     private static long getProcessHandleCount() {
  77         if (IS_WINDOWS) {
  78             if (!nativeLibLoaded) {
  79                 System.loadLibrary("FileUtils");
  80                 nativeLibLoaded = true;
  81             }
  82             return getWinProcessHandleCount();
  83         } else {
  84             return ((UnixOperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean()).getOpenFileDescriptorCount();
  85         }
  86     }
  87 
  88     private static native long getWinProcessHandleCount();
  89 }