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 import java.io.IOException; 32 import java.net.DatagramSocket; 33 import java.net.MulticastSocket; 34 import java.net.ServerSocket; 35 import java.net.Socket; 36 import jdk.net.ExtendedSocketOptions; 37 38 public class TcpKeepAliveTest { 39 40 private static final String LOCAL_HOST = "127.0.0.1"; 41 private static final int DEFAULT_KEEP_ALIVE_PROBES = 7; 42 private static final int DEFAULT_KEEP_ALIVE_TIME = 1973; 43 private static final int DEFAULT_KEEP_ALIVE_INTVL = 53; 44 main(String args[])45 public static void main(String args[]) throws IOException { 46 47 try (ServerSocket ss = new ServerSocket(0); 48 Socket s = new Socket(LOCAL_HOST, ss.getLocalPort()); 49 DatagramSocket ds = new DatagramSocket(0); 50 MulticastSocket mc = new MulticastSocket(0)) { 51 if (ss.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPIDLE)) { 52 ss.setOption(ExtendedSocketOptions.TCP_KEEPIDLE, DEFAULT_KEEP_ALIVE_TIME); 53 if (ss.getOption(ExtendedSocketOptions.TCP_KEEPIDLE) != DEFAULT_KEEP_ALIVE_TIME) { 54 throw new RuntimeException("Test failed, TCP_KEEPIDLE should have been " + DEFAULT_KEEP_ALIVE_TIME); 55 } 56 } 57 if (ss.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPCOUNT)) { 58 ss.setOption(ExtendedSocketOptions.TCP_KEEPCOUNT, DEFAULT_KEEP_ALIVE_PROBES); 59 if (ss.getOption(ExtendedSocketOptions.TCP_KEEPCOUNT) != DEFAULT_KEEP_ALIVE_PROBES) { 60 throw new RuntimeException("Test failed, TCP_KEEPCOUNT should have been " + DEFAULT_KEEP_ALIVE_PROBES); 61 } 62 } 63 if (ss.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPINTERVAL)) { 64 ss.setOption(ExtendedSocketOptions.TCP_KEEPINTERVAL, DEFAULT_KEEP_ALIVE_INTVL); 65 if (ss.getOption(ExtendedSocketOptions.TCP_KEEPINTERVAL) != DEFAULT_KEEP_ALIVE_INTVL) { 66 throw new RuntimeException("Test failed, TCP_KEEPINTERVAL should have been " + DEFAULT_KEEP_ALIVE_INTVL); 67 } 68 } 69 if (s.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPIDLE)) { 70 s.setOption(ExtendedSocketOptions.TCP_KEEPIDLE, DEFAULT_KEEP_ALIVE_TIME); 71 if (s.getOption(ExtendedSocketOptions.TCP_KEEPIDLE) != DEFAULT_KEEP_ALIVE_TIME) { 72 throw new RuntimeException("Test failed, TCP_KEEPIDLE should have been " + DEFAULT_KEEP_ALIVE_TIME); 73 } 74 } 75 if (s.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPCOUNT)) { 76 s.setOption(ExtendedSocketOptions.TCP_KEEPCOUNT, DEFAULT_KEEP_ALIVE_PROBES); 77 if (s.getOption(ExtendedSocketOptions.TCP_KEEPCOUNT) != DEFAULT_KEEP_ALIVE_PROBES) { 78 throw new RuntimeException("Test failed, TCP_KEEPCOUNT should have been " + DEFAULT_KEEP_ALIVE_PROBES); 79 } 80 } 81 if (s.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPINTERVAL)) { 82 s.setOption(ExtendedSocketOptions.TCP_KEEPINTERVAL, DEFAULT_KEEP_ALIVE_INTVL); 83 if (s.getOption(ExtendedSocketOptions.TCP_KEEPINTERVAL) != DEFAULT_KEEP_ALIVE_INTVL) { 84 throw new RuntimeException("Test failed, TCP_KEEPINTERVAL should have been " + DEFAULT_KEEP_ALIVE_INTVL); 85 } 86 } 87 if (ds.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPCOUNT)) { 88 throw new RuntimeException("Test failed, TCP_KEEPCOUNT is applicable" 89 + " for TCP Sockets only."); 90 } 91 if (ds.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPIDLE)) { 92 throw new RuntimeException("Test failed, TCP_KEEPIDLE is applicable" 93 + " for TCP Sockets only."); 94 } 95 if (ds.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPINTERVAL)) { 96 throw new RuntimeException("Test failed, TCP_KEEPINTERVAL is applicable" 97 + " for TCP Sockets only."); 98 } 99 if (mc.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPCOUNT)) { 100 throw new RuntimeException("Test failed, TCP_KEEPCOUNT is applicable" 101 + " for TCP Sockets only"); 102 } 103 if (mc.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPIDLE)) { 104 throw new RuntimeException("Test failed, TCP_KEEPIDLE is applicable" 105 + " for TCP Sockets only"); 106 } 107 if (mc.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPINTERVAL)) { 108 throw new RuntimeException("Test failed, TCP_KEEPINTERVAL is applicable" 109 + " for TCP Sockets only"); 110 } 111 } 112 } 113 } 114