4 * Copyright 2015 Eddie Berrisford-Lynch <n0521366@ntu.ac.uk>.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 package uk.ac.ntu.n0521366.wsyd.libs.net;
26 import java.net.InetAddress;
27 import org.junit.After;
28 import org.junit.AfterClass;
29 import org.junit.Before;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import static org.junit.Assert.*;
34 import org.junit.Ignore;
35 import java.net.InetAddress;
36 import java.nio.ByteBuffer;
37 import java.nio.IntBuffer;
38 import java.text.MessageFormat;
39 import java.util.ArrayList;
40 import java.util.Arrays;
41 import java.util.ListIterator;
42 import uk.ac.ntu.n0521366.wsyd.libs.net.WSYD_SocketAddress.Protocol;
46 * @author TJ <hacker@iam.tj>
48 public class WSYD_SocketAddressTest {
50 public WSYD_SocketAddressTest() {
54 public static void setUpClass() {
58 public static void tearDownClass() {
66 public void tearDown() {
70 public void testStringIPv4ToBytes4() {
71 System.out.println(MessageFormat.format("{0}.stringIPv4ToBytes4()",WSYD_SocketAddress.class.getCanonicalName()));
72 String[] ipv4_strings = {"1.2.3.4", "255.255.255.255", "0.0.0.0", "a12.2.3.4", "256.2.3.4", "-1.2.3.4", "1.2.3.4.5" };
73 long[] ipv4_values = {16909060, 4294967295L, 0, -1, -1, -1, -1};
74 String[] exceptionExpected = {null, null, null, "Not a decimal number", "Value must not be greater", "Value must not be negative", "Not a correct dotted" };
78 for (int index = 0; index < ipv4_strings.length; index++) {
79 System.out.print(MessageFormat.format("Testing {0} expecting \"{1}\"", (Object[])new String[] {ipv4_strings[index], (ipv4_values[index] != -1 ? Long.toString(ipv4_values[index]) : exceptionExpected[index])} ));
80 try { // convoluted way to convert a byte[] to an *unsigned* 32-bit integer (have to store value in a long since all Java numeric types are signed)
81 ByteBuffer bb = ByteBuffer.allocate(Integer.SIZE/8); // SIZE is number of bits
82 bytes = WSYD_SocketAddress.stringIPv4ToBytes4(ipv4_strings[index]);
83 bb.put(bytes).rewind();
84 IntBuffer ib = bb.asIntBuffer(); // convert the 4 bytes to a 32-bit signed integer
85 value = ((long)ib.get()) & (-1L >>> 32); // extract the unsigned 32-bit integer value
86 System.out.println(MessageFormat.format(" got correct value {0} ( {1} {2} {3} {4})", value, bytes[0], bytes[1], bytes[2], bytes[3]));
87 assertEquals(ipv4_values[index], value);
88 } catch (IllegalArgumentException e) {
89 if (ipv4_values[index] == -1 && e.getMessage().startsWith(exceptionExpected[index])) {
90 System.out.println(MessageFormat.format(" got correct exception {0} {1}", e.getClass().getName(), e.getMessage()));
91 assertEquals(exceptionExpected[index], e.getMessage().substring(0, exceptionExpected[index].length()));
93 fail(MessageFormat.format("Unexpected exception {2} {1} converting: {0}", ipv4_strings[index], e.getMessage(), e.getClass().getName()));
100 public void testConstructorDefault() {
101 WSYD_SocketAddress instance = new WSYD_SocketAddress();
102 System.out.println(java.text.MessageFormat.format("{0}.WSYD_SocketAddress(default)", instance.getClass().getName()));
103 assertEquals(instance._isReachable, false);
104 assertEquals(instance._multicast, false);
105 assertEquals(instance._protocol, Protocol.TCP);
106 assertEquals(instance._socketAddress, null);
110 * Tests the Constructor(s) that accept parameters
113 public void testConstructorsByValue() {
114 System.out.println(MessageFormat.format("{0}.WSYD_SocketAddress(values ...)",WSYD_SocketAddress.class.getCanonicalName()));
116 ArrayList<String> aS = new ArrayList(Arrays.asList(new String[] {"1.2.3.4", "0.0.0.0", "255.255.255.255", "239.192.3.4"}));
117 ArrayList<Boolean> aMulti = new ArrayList<>(Arrays.asList(new Boolean[] {false, false, false, true}));
119 ArrayList<Integer> aP = new ArrayList<>(Arrays.asList(new Integer[] {65535, 0, -1, 65536}));
120 ArrayList<Protocol> aPr = new ArrayList<>(Arrays.asList(new Protocol[] {Protocol.TCP, Protocol.UDP}));
124 InetAddress iaEx = null;
126 WSYD_SocketAddress wia;
127 ListIterator<String> iter = aS.listIterator();
128 while(iter.hasNext()) {
129 int index = iter.nextIndex();
131 bEx = WSYD_SocketAddress.stringIPv4ToBytes4(sEx); // byte[]
133 iaEx = InetAddress.getByAddress(bEx); // InetAddress
134 } catch (java.net.UnknownHostException e) {
135 fail(MessageFormat.format("Got {0} whilst creating InetAddress({1})", e.getClass().getName(), sEx));
137 for (Integer p: aP) {
138 for (Protocol pr: aPr) {
139 mEx = aMulti.get(index);
140 if (mEx == true && pr == Protocol.TCP)
142 String expecting = MessageFormat.format("Address: /{0}, Port: {1}, Protocol: {2}, MultiCast: {3}", sEx, p.toString(), pr, mEx);
143 System.out.println(MessageFormat.format("Expecting: {0}", expecting));
144 // Test WSYD_SocketAddress(InetAddress address, int port, Protocol protocol)
146 wia = new WSYD_SocketAddress(iaEx, p, pr);
147 System.out.println(MessageFormat.format("Got : {0}", wia.toString()));
148 assertEquals(expecting, wia.toString());
149 } catch (IllegalArgumentException e) {
150 if (!e.getMessage().startsWith("port out of range")) {
151 fail(MessageFormat.format("Got unexcepted Exception {0}: {1}", e.getClass().getName(), e.getMessage()));
154 System.out.println(MessageFormat.format("Got expected exception {0}", e.getClass().getName()));
156 // TODO: testConstructorsByValue() add test of WSYD_SocketAddress(InetAddress address, int port)
157 // TODO: testConstructorsByValue() add test of WSYD_SocketAddress(InetAddress address)
158 // TODO: testConstructorsByValue() add test of WSYD_SocketAddress(int port, Protocol protocol)
159 // TODO: testConstructorsByValue() add test of WSYD_SocketAddress(int port)
166 * Test of setAddress method, of class WSYD_SocketAddress.
170 public void testSetAddress() {
171 System.out.println("setAddress");
172 InetAddress address = null;
173 WSYD_SocketAddress instance = new WSYD_SocketAddress();
174 instance.setAddress(address);
175 // TODO review the generated test code and remove the default call to fail.
176 fail("The test case is a prototype.");
180 * Test of getAddress method, of class WSYD_SocketAddress.
184 public void testGetAddress() {
185 System.out.println("getAddress");
186 WSYD_SocketAddress instance = new WSYD_SocketAddress();
187 InetAddress expResult = null;
188 InetAddress result = instance.getAddress();
189 assertEquals(expResult, result);
190 // TODO review the generated test code and remove the default call to fail.
191 fail("The test case is a prototype.");
195 * Test of setPort method, of class WSYD_SocketAddress.
199 public void testSetPort() {
200 System.out.println("setPort");
202 WSYD_SocketAddress instance = new WSYD_SocketAddress();
203 instance.setPort(port);
204 // TODO review the generated test code and remove the default call to fail.
205 fail("The test case is a prototype.");
209 * Test of getPort method, of class WSYD_SocketAddress.
213 public void testGetPort() {
214 System.out.println("getPort");
215 WSYD_SocketAddress instance = new WSYD_SocketAddress();
217 int result = instance.getPort();
218 assertEquals(expResult, result);
219 // TODO review the generated test code and remove the default call to fail.
220 fail("The test case is a prototype.");
224 * Test of setSocketAddress method, of class WSYD_SocketAddress.
228 public void testSetSocketAddress() {
229 System.out.println("setSocketAddress");
230 InetAddress address = null;
232 WSYD_SocketAddress instance = new WSYD_SocketAddress();
233 instance.setSocketAddress(address, port);
234 // TODO review the generated test code and remove the default call to fail.
235 fail("The test case is a prototype.");
239 * Test of setProtocol method, of class WSYD_SocketAddress.
243 public void testSetProtocol() {
244 System.out.println("setProtocol");
245 WSYD_SocketAddress.Protocol protocol = null;
246 WSYD_SocketAddress instance = new WSYD_SocketAddress();
247 instance.setProtocol(protocol);
248 // TODO review the generated test code and remove the default call to fail.
249 fail("The test case is a prototype.");
253 * Test of getProtocol method, of class WSYD_SocketAddress.
257 public void testGetProtocol() {
258 System.out.println("getProtocol");
259 WSYD_SocketAddress instance = new WSYD_SocketAddress();
260 WSYD_SocketAddress.Protocol expResult = null;
261 WSYD_SocketAddress.Protocol result = instance.getProtocol();
262 assertEquals(expResult, result);
263 // TODO review the generated test code and remove the default call to fail.
264 fail("The test case is a prototype.");
268 * Test of isMulticast method, of class WSYD_SocketAddress.
272 public void testIsMulticast() {
273 System.out.println("isMulticast");
274 WSYD_SocketAddress instance = new WSYD_SocketAddress();
275 boolean expResult = false;
276 boolean result = instance.isMulticast();
277 assertEquals(expResult, result);
278 // TODO review the generated test code and remove the default call to fail.
279 fail("The test case is a prototype.");
283 * Test of setReachable method, of class WSYD_SocketAddress.
287 public void testSetReachable() {
288 System.out.println("setReachable");
289 WSYD_SocketAddress instance = new WSYD_SocketAddress();
290 boolean expResult = false;
291 boolean result = instance.setReachable();
292 assertEquals(expResult, result);
293 // TODO review the generated test code and remove the default call to fail.
294 fail("The test case is a prototype.");
298 * Test of getReachable method, of class WSYD_SocketAddress.
302 public void testGetReachable() {
303 System.out.println("getReachable");
304 WSYD_SocketAddress instance = new WSYD_SocketAddress();
305 boolean expResult = false;
306 boolean result = instance.getReachable();
307 assertEquals(expResult, result);
308 // TODO review the generated test code and remove the default call to fail.
309 fail("The test case is a prototype.");
313 * Test of toString method, of class WSYD_SocketAddress.
317 public void testToString() {
318 System.out.println("toString");
319 WSYD_SocketAddress instance = new WSYD_SocketAddress();
320 String expResult = "";
321 String result = instance.toString();
322 assertEquals(expResult, result);
323 // TODO review the generated test code and remove the default call to fail.
324 fail("The test case is a prototype.");