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