1 /* 2 * Copyright (c) 2018, 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 8194298 27 * @summary Add support for per Socket configuration of TCP keepalive 28 * @modules jdk.net 29 * @run main TcpKeepAliveTest 30 */ 31 package test.java.net.SocketOptions; 32 33 import java.io.IOException; 34 import java.net.DatagramSocket; 35 import java.net.MulticastSocket; 36 import java.net.ServerSocket; 37 import java.net.Socket; 38 import jdk.net.ExtendedSocketOptions; 39 import org.testng.annotations.Test; 40 import org.testng.Assert; 41 42 public class TcpKeepAliveTest { 43 44 private static final String LOCAL_HOST = "127.0.0.1"; 45 private static final int DEFAULT_KEEP_ALIVE_PROBES = 7; 46 private static final int DEFAULT_KEEP_ALIVE_TIME = 1973; 47 private static final int DEFAULT_KEEP_ALIVE_INTVL = 53; 48 49 @Test testTcpKeepAlive()50 public void testTcpKeepAlive() throws IOException { 51 52 try (ServerSocket ss = new ServerSocket(0); 53 Socket s = new Socket(LOCAL_HOST, ss.getLocalPort()); 54 DatagramSocket ds = new DatagramSocket(0); 55 MulticastSocket mc = new MulticastSocket(0)) { 56 if (ss.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPIDLE)) { 57 ss.setOption(ExtendedSocketOptions.TCP_KEEPIDLE, DEFAULT_KEEP_ALIVE_TIME); 58 if (ss.getOption(ExtendedSocketOptions.TCP_KEEPIDLE) != DEFAULT_KEEP_ALIVE_TIME) { 59 Assert.fail("Test failed, TCP_KEEPIDLE should have been " + DEFAULT_KEEP_ALIVE_TIME); 60 } 61 } 62 if (ss.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPCOUNT)) { 63 ss.setOption(ExtendedSocketOptions.TCP_KEEPCOUNT, DEFAULT_KEEP_ALIVE_PROBES); 64 if (ss.getOption(ExtendedSocketOptions.TCP_KEEPCOUNT) != DEFAULT_KEEP_ALIVE_PROBES) { 65 Assert.fail("Test failed, TCP_KEEPCOUNT should have been " + DEFAULT_KEEP_ALIVE_PROBES); 66 } 67 } 68 if (ss.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPINTERVAL)) { 69 ss.setOption(ExtendedSocketOptions.TCP_KEEPINTERVAL, DEFAULT_KEEP_ALIVE_INTVL); 70 if (ss.getOption(ExtendedSocketOptions.TCP_KEEPINTERVAL) != DEFAULT_KEEP_ALIVE_INTVL) { 71 Assert.fail("Test failed, TCP_KEEPINTERVAL should have been " + DEFAULT_KEEP_ALIVE_INTVL); 72 } 73 } 74 if (s.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPIDLE)) { 75 s.setOption(ExtendedSocketOptions.TCP_KEEPIDLE, DEFAULT_KEEP_ALIVE_TIME); 76 if (s.getOption(ExtendedSocketOptions.TCP_KEEPIDLE) != DEFAULT_KEEP_ALIVE_TIME) { 77 Assert.fail("Test failed, TCP_KEEPIDLE should have been " + DEFAULT_KEEP_ALIVE_TIME); 78 } 79 } 80 if (s.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPCOUNT)) { 81 s.setOption(ExtendedSocketOptions.TCP_KEEPCOUNT, DEFAULT_KEEP_ALIVE_PROBES); 82 if (s.getOption(ExtendedSocketOptions.TCP_KEEPCOUNT) != DEFAULT_KEEP_ALIVE_PROBES) { 83 Assert.fail("Test failed, TCP_KEEPCOUNT should have been " + DEFAULT_KEEP_ALIVE_PROBES); 84 } 85 } 86 if (s.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPINTERVAL)) { 87 s.setOption(ExtendedSocketOptions.TCP_KEEPINTERVAL, DEFAULT_KEEP_ALIVE_INTVL); 88 if (s.getOption(ExtendedSocketOptions.TCP_KEEPINTERVAL) != DEFAULT_KEEP_ALIVE_INTVL) { 89 Assert.fail("Test failed, TCP_KEEPINTERVAL should have been " + DEFAULT_KEEP_ALIVE_INTVL); 90 } 91 } 92 if (ds.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPCOUNT)) { 93 Assert.fail("Test failed, TCP_KEEPCOUNT is applicable" 94 + " for TCP Sockets only."); 95 } 96 if (ds.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPIDLE)) { 97 Assert.fail("Test failed, TCP_KEEPIDLE is applicable" 98 + " for TCP Sockets only."); 99 } 100 if (ds.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPINTERVAL)) { 101 Assert.fail("Test failed, TCP_KEEPINTERVAL is applicable" 102 + " for TCP Sockets only."); 103 } 104 if (mc.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPCOUNT)) { 105 Assert.fail("Test failed, TCP_KEEPCOUNT is applicable" 106 + " for TCP Sockets only"); 107 } 108 if (mc.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPIDLE)) { 109 Assert.fail("Test failed, TCP_KEEPIDLE is applicable" 110 + " for TCP Sockets only"); 111 } 112 if (mc.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPINTERVAL)) { 113 Assert.fail("Test failed, TCP_KEEPINTERVAL is applicable" 114 + " for TCP Sockets only"); 115 } 116 } 117 } 118 }