1 /* 2 * Copyright (c) 2019, 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 import jtreg.SkippedException; 25 26 import javax.naming.directory.InitialDirContext; 27 28 import java.io.IOException; 29 import java.net.BindException; 30 import java.net.InetAddress; 31 import java.net.ServerSocket; 32 33 /* 34 * @test 35 * @bug 8228580 36 * @summary Tests that we get a DNS response when the UDP DNS server returns a 37 * truncated response and the TCP DNS server does not respond at all 38 * after connect. 39 * @library ../lib/ 40 * @library /test/lib 41 * @modules java.base/sun.security.util 42 * @run main TcpTimeout 43 */ 44 45 public class TcpTimeout extends DNSTestBase { 46 private TcpDnsServer tcpDnsServer; 47 48 public static void main(String[] args) throws Exception { 49 new TcpTimeout().run(args); 50 } 51 52 @Override 53 public void runTest() throws Exception { 54 /* Using the default context because we rely on the default DNS client 55 timeout setting, com.sun.jndi.dns.timeout.initial (which is currently 1000) */ 56 setContext(new InitialDirContext(env())); 57 58 /* perform query */ 59 var attrs = context().getAttributes("host1"); 60 DNSTestUtils.debug(attrs); 61 62 /* Note that the returned attributes are truncated and the response 63 is not valid. */ 64 var txtAttr = attrs.get("TXT"); 65 if (txtAttr == null) 66 throw new RuntimeException("TXT attribute missing."); 67 } 68 69 @Override 70 public void initTest(String[] args) { 71 /* We need to bind the TCP server on the same port the UDP server is 72 listening to. This may not be possible if that port is in use. Retry 73 MAX_RETRIES times relying on UDP port randomness. */ 74 final int MAX_RETRIES = 5; 75 for (int i = 0; i < MAX_RETRIES; i++) { 76 super.initTest(args); 77 var udpServer = (Server) env().get(DNSTestUtils.TEST_DNS_SERVER_THREAD); 78 int port = udpServer.getPort(); 79 try { 80 tcpDnsServer = new TcpDnsServer(port); 81 break; // success 82 } catch (BindException be) { 83 DNSTestUtils.debug("Failed to bind server socket on port " + port 84 + ", retry no. " + (i + 1) + ", " + be.getMessage()); 85 } catch (Exception ex) { 86 throw new RuntimeException("Unexpected exception during initTest", ex); 87 } finally { 88 if (tcpDnsServer == null) { // cleanup behind exceptions 89 super.cleanupTest(); 90 } 91 } 92 } 93 94 if (tcpDnsServer == null) { 95 throw new SkippedException("Cannot start TCP server after " 96 + MAX_RETRIES 97 + " tries, skip the test"); 98 } 99 } 100 101 @Override 102 public void cleanupTest() { 103 super.cleanupTest(); 104 if (tcpDnsServer != null) 105 tcpDnsServer.stopServer(); 106 } 107 108 /** 109 * A TCP server that accepts a connection and does nothing else: causes read 110 * timeout on client side. 111 */ 112 private static class TcpDnsServer { 113 final ServerSocket serverSocket; 114 115 TcpDnsServer(int port) throws IOException { 116 serverSocket = new ServerSocket(port, 0, InetAddress.getLoopbackAddress()); 117 System.out.println("TcpDnsServer: listening on port " + port); 118 } 119 120 void stopServer() { 121 try { 122 if (serverSocket != null) 123 serverSocket.close(); 124 } 125 catch (Exception e) {} 126 } 127 } 128 }