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