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.junit.Assert;
31 import org.junit.Test;
32 import org.junit.experimental.categories.Category;
33
34
35
36
37
38
39
40
41
42
43
44
45 @Category(SmallTests.class)
46 public class TestIPv6NIOServerSocketChannel {
47
48 private static final Log LOG = LogFactory.getLog(TestIPv6NIOServerSocketChannel.class);
49
50
51
52
53 private void bindServerSocket(InetAddress inetAddr) throws IOException {
54 while(true) {
55 int port = HBaseTestingUtility.randomFreePort();
56 InetSocketAddress addr = new InetSocketAddress(inetAddr, port);
57 ServerSocket serverSocket = null;
58 try {
59 serverSocket = new ServerSocket();
60 serverSocket.bind(addr);
61 break;
62 } catch (BindException ex) {
63
64 } finally {
65 if (serverSocket != null) {
66 serverSocket.close();
67 }
68 }
69 }
70 }
71
72
73
74
75
76
77
78 private void bindNIOServerSocket(InetAddress inetAddr) throws IOException {
79 while (true) {
80 int port = HBaseTestingUtility.randomFreePort();
81 InetSocketAddress addr = new InetSocketAddress(inetAddr, port);
82 ServerSocketChannel channel = null;
83 ServerSocket serverSocket = null;
84 try {
85 channel = ServerSocketChannel.open();
86 serverSocket = channel.socket();
87 serverSocket.bind(addr);
88 break;
89 } catch (BindException ex) {
90
91 } finally {
92 if (serverSocket != null) {
93 serverSocket.close();
94 }
95 if (channel != null) {
96 channel.close();
97 }
98 }
99 }
100 }
101
102
103
104
105
106 @Test
107 public void testServerSocket() throws IOException {
108 byte[] addr = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
109 InetAddress inetAddr = InetAddress.getByAddress(addr);
110
111 try {
112 bindServerSocket(inetAddr);
113 bindNIOServerSocket(inetAddr);
114
115 } catch(java.net.SocketException ex) {
116
117
118
119 Assert.assertFalse(ex.getClass().isInstance(BindException.class));
120 Assert.assertTrue(ex.getMessage().toLowerCase().contains("protocol family"));
121 LOG.info("Received expected exception:");
122 LOG.info(ex);
123
124
125 ensurePreferIPv4();
126 }
127 }
128
129
130
131
132 public void ensurePreferIPv4() throws IOException {
133 InetAddress[] addrs = InetAddress.getAllByName("localhost");
134 for (InetAddress addr : addrs) {
135 LOG.info("resolved localhost as:" + addr);
136 Assert.assertEquals(4, addr.getAddress().length);
137 }
138 }
139
140
141
142
143
144 @Test
145 public void testServerSocketFromLocalhostResolution() throws IOException {
146 InetAddress[] addrs = InetAddress.getAllByName("localhost");
147 for (InetAddress addr : addrs) {
148 LOG.info("resolved localhost as:" + addr);
149 bindServerSocket(addr);
150 bindNIOServerSocket(addr);
151 }
152 }
153
154 public static void main(String[] args) throws Exception {
155 TestIPv6NIOServerSocketChannel test = new TestIPv6NIOServerSocketChannel();
156 test.testServerSocket();
157 test.testServerSocketFromLocalhostResolution();
158 }
159 }