1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 package org.apache.hadoop.hbase; 20 21 import java.io.IOException; 22 import java.net.BindException; 23 import java.net.InetAddress; 24 import java.net.InetSocketAddress; 25 import java.net.ServerSocket; 26 import java.nio.channels.ServerSocketChannel; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.apache.hadoop.hbase.testclassification.SmallTests; 31 import org.junit.Assert; 32 import org.junit.Rule; 33 import org.junit.Test; 34 import org.junit.experimental.categories.Category; 35 import org.junit.rules.TestRule; 36 37 /** 38 * This tests whether ServerSocketChannel works over ipv6, which Zookeeper 39 * depends on. On Windows Oracle JDK 6, creating a ServerSocketChannel throws 40 * java.net.SocketException: Address family not supported by protocol family 41 * exception. It is a known JVM bug, seems to be only resolved for JDK7: 42 * http://bugs.sun.com/view_bug.do?bug_id=6230761 43 * 44 * For this test, we check that whether we are effected by this bug, and if so 45 * the test ensures that we are running with java.net.preferIPv4Stack=true, so 46 * that ZK will not fail to bind to ipv6 address using ClientCnxnSocketNIO. 47 */ 48 @Category(SmallTests.class) 49 public class TestIPv6NIOServerSocketChannel { 50 51 private static final Log LOG = LogFactory.getLog(TestIPv6NIOServerSocketChannel.class); 52 53 @Rule 54 public final TestRule timeout = CategoryBasedTimeout.builder(). 55 withTimeout(this.getClass()).withLookingForStuckThread(true).build(); 56 /** 57 * Creates and binds a regular ServerSocket. 58 */ bindServerSocket(InetAddress inetAddr)59 private void bindServerSocket(InetAddress inetAddr) throws IOException { 60 while(true) { 61 int port = HBaseTestingUtility.randomFreePort(); 62 InetSocketAddress addr = new InetSocketAddress(inetAddr, port); 63 ServerSocket serverSocket = null; 64 try { 65 serverSocket = new ServerSocket(); 66 serverSocket.bind(addr); 67 break; 68 } catch (BindException ex) { 69 //continue 70 } finally { 71 if (serverSocket != null) { 72 serverSocket.close(); 73 } 74 } 75 } 76 } 77 78 /** 79 * Creates a NIO ServerSocketChannel, and gets the ServerSocket from 80 * there. Then binds the obtained socket. 81 * This fails on Windows with Oracle JDK1.6.0u33, if the passed InetAddress is a 82 * IPv6 address. Works on Oracle JDK 1.7. 83 */ bindNIOServerSocket(InetAddress inetAddr)84 private void bindNIOServerSocket(InetAddress inetAddr) throws IOException { 85 while (true) { 86 int port = HBaseTestingUtility.randomFreePort(); 87 InetSocketAddress addr = new InetSocketAddress(inetAddr, port); 88 ServerSocketChannel channel = null; 89 ServerSocket serverSocket = null; 90 try { 91 channel = ServerSocketChannel.open(); 92 serverSocket = channel.socket(); 93 serverSocket.bind(addr); // This does not work 94 break; 95 } catch (BindException ex) { 96 //continue 97 } finally { 98 if (serverSocket != null) { 99 serverSocket.close(); 100 } 101 if (channel != null) { 102 channel.close(); 103 } 104 } 105 } 106 } 107 108 /** 109 * Checks whether we are effected by the JDK issue on windows, and if so 110 * ensures that we are running with preferIPv4Stack=true. 111 */ 112 @Test testServerSocket()113 public void testServerSocket() throws IOException { 114 byte[] addr = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }; 115 InetAddress inetAddr = InetAddress.getByAddress(addr); 116 117 try { 118 bindServerSocket(inetAddr); 119 bindNIOServerSocket(inetAddr); 120 //if on *nix or windows JDK7, both will pass 121 } catch(java.net.SocketException ex) { 122 //On Windows JDK6, we will get expected exception: 123 //java.net.SocketException: Address family not supported by protocol family 124 //or java.net.SocketException: Protocol family not supported 125 Assert.assertFalse(ex.getClass().isInstance(BindException.class)); 126 Assert.assertTrue(ex.getMessage().toLowerCase().contains("protocol family")); 127 LOG.info("Received expected exception:"); 128 LOG.info(ex); 129 130 //if this is the case, ensure that we are running on preferIPv4=true 131 ensurePreferIPv4(); 132 } 133 } 134 135 /** 136 * Checks whether we are running with java.net.preferIPv4Stack=true 137 */ ensurePreferIPv4()138 public void ensurePreferIPv4() throws IOException { 139 InetAddress[] addrs = InetAddress.getAllByName("localhost"); 140 for (InetAddress addr : addrs) { 141 LOG.info("resolved localhost as:" + addr); 142 Assert.assertEquals(4, addr.getAddress().length); //ensure 4 byte ipv4 address 143 } 144 } 145 146 /** 147 * Tests whether every InetAddress we obtain by resolving can open a 148 * ServerSocketChannel. 149 */ 150 @Test testServerSocketFromLocalhostResolution()151 public void testServerSocketFromLocalhostResolution() throws IOException { 152 InetAddress[] addrs = InetAddress.getAllByName("localhost"); 153 for (InetAddress addr : addrs) { 154 LOG.info("resolved localhost as:" + addr); 155 bindServerSocket(addr); 156 bindNIOServerSocket(addr); 157 } 158 } 159 main(String[] args)160 public static void main(String[] args) throws Exception { 161 TestIPv6NIOServerSocketChannel test = new TestIPv6NIOServerSocketChannel(); 162 test.testServerSocket(); 163 test.testServerSocketFromLocalhostResolution(); 164 } 165 } 166