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 }
|
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 }
|