1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 import java.util.Locale;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.hadoop.hbase.testclassification.SmallTests;
32 import org.junit.Assert;
33 import org.junit.Rule;
34 import org.junit.Test;
35 import org.junit.experimental.categories.Category;
36 import org.junit.rules.TestRule;
37
38
39
40
41
42
43
44
45
46
47
48
49 @Category(SmallTests.class)
50 public class TestIPv6NIOServerSocketChannel {
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
58
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
70 LOG.info("Failed on " + addr + ", inedAddr=" + inetAddr, ex);
71 } finally {
72 if (serverSocket != null) {
73 serverSocket.close();
74 }
75 }
76 }
77 }
78
79
80
81
82
83
84
85 private void bindNIOServerSocket(InetAddress inetAddr) throws IOException {
86 while (true) {
87 int port = HBaseTestingUtility.randomFreePort();
88 InetSocketAddress addr = new InetSocketAddress(inetAddr, port);
89 ServerSocketChannel channel = null;
90 ServerSocket serverSocket = null;
91 try {
92 channel = ServerSocketChannel.open();
93 serverSocket = channel.socket();
94 serverSocket.bind(addr);
95 break;
96 } catch (BindException ex) {
97
98 } finally {
99 if (serverSocket != null) {
100 serverSocket.close();
101 }
102 if (channel != null) {
103 channel.close();
104 }
105 }
106 }
107 }
108
109
110
111
112
113 @Test
114 public void testServerSocket() throws IOException {
115 byte[] addr = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
116 InetAddress inetAddr = InetAddress.getByAddress(addr);
117
118 try {
119 bindServerSocket(inetAddr);
120 bindNIOServerSocket(inetAddr);
121
122 } catch(java.net.SocketException ex) {
123
124
125
126 Assert.assertFalse(ex instanceof BindException);
127 Assert.assertTrue(ex.getMessage().toLowerCase(Locale.ROOT).contains("protocol family"));
128 LOG.info("Received expected exception:");
129 LOG.info(ex);
130
131
132 ensurePreferIPv4();
133 }
134 }
135
136
137
138
139 public void ensurePreferIPv4() throws IOException {
140 InetAddress[] addrs = InetAddress.getAllByName("localhost");
141 for (InetAddress addr : addrs) {
142 LOG.info("resolved localhost as:" + addr);
143 Assert.assertEquals(4, addr.getAddress().length);
144 }
145 }
146
147
148
149
150
151 @Test
152 public void testServerSocketFromLocalhostResolution() throws IOException {
153 InetAddress[] addrs = {InetAddress.getLocalHost()};
154 for (InetAddress addr : addrs) {
155 LOG.info("Resolved localhost as: " + addr);
156 bindServerSocket(addr);
157 bindNIOServerSocket(addr);
158 }
159 }
160
161 public static void main(String[] args) throws Exception {
162 TestIPv6NIOServerSocketChannel test = new TestIPv6NIOServerSocketChannel();
163 test.testServerSocket();
164 test.testServerSocketFromLocalhostResolution();
165 }
166 }