1
2
3
4
5
6
7
8
9
10
11 package org.mule.test.firewall;
12
13 import org.mule.config.factories.HostNameFactory;
14 import org.mule.tck.junit4.AbstractMuleTestCase;
15
16 import java.io.IOException;
17 import java.net.DatagramPacket;
18 import java.net.DatagramSocket;
19 import java.net.InetAddress;
20 import java.net.ServerSocket;
21 import java.net.Socket;
22 import java.net.UnknownHostException;
23 import java.security.SecureRandom;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.junit.Test;
28
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.assertNotSame;
31
32 public class FirewallTestCase extends AbstractMuleTestCase
33 {
34 public static final String LOCALHOST = "localhost";
35 public static final String LOCALADDR = "127.0.0.1";
36 public static final int TEST_COUNT = 1;
37
38 protected final Log logger = LogFactory.getLog(this.getClass());
39
40 private SecureRandom random = new SecureRandom();
41
42 @Test
43 public void testLoopback() throws Exception
44 {
45
46
47 consistentAddress(LOCALHOST, false);
48
49 }
50
51 @Test
52 public void testLocalHost() throws Exception
53 {
54 InetAddress aLocalAddress = InetAddress.getLocalHost();
55 logger.info("Java returns " + addressToString(aLocalAddress) + " as the 'local' address");
56 assertNotSame("No external address", LOCALADDR, aLocalAddress.getHostAddress());
57 consistentAddress(aLocalAddress.getHostName(), false);
58 assertEquals("Inconsistent hostname", aLocalAddress.getHostName(), new HostNameFactory().create(null));
59 }
60
61 @Test
62 public void testCanonicalHost() throws Exception
63 {
64 InetAddress aLocalAddress = InetAddress.getLocalHost();
65 assertNotSame("No extrernal name", LOCALHOST, aLocalAddress.getCanonicalHostName());
66 consistentAddress(aLocalAddress.getCanonicalHostName(), true);
67 }
68
69 protected void consistentAddress(String name, boolean canonical) throws UnknownHostException
70 {
71 String address = InetAddress.getByName(name).getHostAddress();
72 logger.debug("Testing relationship between " + name + " and " + address);
73 assertEquals("Name " + name + " is inconsistent", name,
74 name(InetAddress.getByName(name), canonical));
75 assertEquals("Address " + address + " is inconsistent", address,
76 InetAddress.getByName(address).getHostAddress());
77
78
79
80 assertEquals(name + " -> " + address + " is inconsistent", address,
81 InetAddress.getByName(name).getHostAddress());
82 assertEquals(name + " -> " + address + " -> " + name + " -> " + address + " is inconsistent", address,
83 InetAddress.getByName(
84 name(
85 InetAddress.getByName(
86 InetAddress.getByName(name).getHostAddress()), canonical)).getHostAddress());
87 }
88
89 protected String name(InetAddress address, boolean canonical)
90 {
91 if (canonical)
92 {
93 return address.getCanonicalHostName();
94 }
95 else
96 {
97 return address.getHostName();
98 }
99 }
100
101 @Test
102 public void testLocalhostTcp() throws Exception
103 {
104 for (int i = 0; i < TEST_COUNT; ++i)
105 {
106 doTestTcp(InetAddress.getByName(LOCALHOST), randomPrivatePort());
107 }
108 }
109
110 @Test
111 public void testHostnameTcp() throws Exception
112 {
113 for (int i = 0; i < TEST_COUNT; ++i)
114 {
115 doTestTcp(InetAddress.getLocalHost(), randomPrivatePort());
116 }
117 }
118
119 @Test
120 public void testLocalhostUdp() throws Exception
121 {
122 for (int i = 0; i < TEST_COUNT; ++i)
123 {
124 doTestUdp(InetAddress.getByName(LOCALHOST), randomPrivatePort());
125 }
126 }
127
128 @Test
129 public void testHostnameUdp() throws Exception
130 {
131 for (int i = 0; i < TEST_COUNT; ++i)
132 {
133 doTestUdp(InetAddress.getLocalHost(), randomPrivatePort());
134 }
135 }
136
137 protected void doTestTcp(InetAddress address, int port) throws Exception
138 {
139 try
140 {
141 logger.debug("Testing TCP on " + addressToString(address, port));
142 ServerSocket server = openTcpServer(address, port);
143 Socket client = openTcpClient(address, port);
144 Socket receiver = server.accept();
145 client.getOutputStream().write(1);
146 assertEquals("Failed to send byte via " + addressToString(address, port),
147 1, receiver.getInputStream().read());
148 client.close();
149 server.close();
150 }
151 catch (Exception e)
152 {
153 logger.error("Error while attempting TCP message on " + addressToString(address, port));
154 throw e;
155 }
156 }
157
158 protected void doTestUdp(InetAddress address, int port) throws Exception
159 {
160 try
161 {
162 logger.debug("Testing UDP on " + addressToString(address, port));
163 DatagramSocket server = openUdpServer(address, port);
164 DatagramSocket client = openUdpClient();
165 client.send(new DatagramPacket(new byte[]{1}, 1, address, port));
166 DatagramPacket packet = new DatagramPacket(new byte[1], 1);
167 server.receive(packet);
168 assertEquals("Failed to send packet via " + addressToString(address, port),
169 1, packet.getData()[0]);
170 client.close();
171 server.close();
172 }
173 catch (Exception e)
174 {
175 logger.error("Error while attempting UDP message on " + addressToString(address, port));
176 throw e;
177 }
178 }
179
180 protected Socket openTcpClient(InetAddress address, int port) throws IOException
181 {
182 try
183 {
184 return new Socket(address, port);
185 }
186 catch (IOException e)
187 {
188 logger.error("Could not open TCP client to " + addressToString(address, port));
189 throw e;
190 }
191 }
192
193 protected ServerSocket openTcpServer(InetAddress address, int port) throws IOException
194 {
195 try
196 {
197 return new ServerSocket(port, 1, address);
198 }
199 catch (IOException e)
200 {
201 logger.error("Could not open TCP server on " + addressToString(address, port));
202 throw e;
203 }
204 }
205
206 protected DatagramSocket openUdpServer(InetAddress address, int port) throws IOException
207 {
208 try
209 {
210 return new DatagramSocket(port, address);
211 }
212 catch (IOException e)
213 {
214 logger.error("Could not open UDP server on " + addressToString(address, port));
215 throw e;
216 }
217 }
218
219 protected DatagramSocket openUdpClient() throws IOException
220 {
221 try
222 {
223 return new DatagramSocket();
224 }
225 catch (IOException e)
226 {
227 logger.error("Could not open UDP client");
228 throw e;
229 }
230 }
231
232 protected String addressToString(InetAddress address, int port)
233 {
234 return addressToString(address) + ":" + port;
235 }
236
237 protected String addressToString(InetAddress address)
238 {
239 return address.getHostName() + "/" + address.getCanonicalHostName() + "/" + address.getHostAddress();
240 }
241
242 protected int randomPrivatePort()
243 {
244 return randomPort(49152, 65535);
245 }
246
247
248
249
250
251
252 protected int randomPort(int lo, int hi)
253 {
254 return lo + random.nextInt(hi - lo + 1);
255 }
256
257 }