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
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
39
40
41
42
43
44
45
46
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
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 } finally {
71 if (serverSocket != null) {
72 serverSocket.close();
73 }
74 }
75 }
76 }
77
78
79
80
81
82
83
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);
94 break;
95 } catch (BindException ex) {
96
97 } finally {
98 if (serverSocket != null) {
99 serverSocket.close();
100 }
101 if (channel != null) {
102 channel.close();
103 }
104 }
105 }
106 }
107
108
109
110
111
112 @Test
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
121 } catch(java.net.SocketException ex) {
122
123
124
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
131 ensurePreferIPv4();
132 }
133 }
134
135
136
137
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);
143 }
144 }
145
146
147
148
149
150 @Test
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
160 public static void main(String[] args) throws Exception {
161 TestIPv6NIOServerSocketChannel test = new TestIPv6NIOServerSocketChannel();
162 test.testServerSocket();
163 test.testServerSocketFromLocalhostResolution();
164 }
165 }