Add Registration functionality and tidy up
[WeStealzYourDataz.git] / test / uk / ac / ntu / n0521366 / wsyd / libs / net / WSYD_SocketAddressTest.java
1 /*
2  * The MIT License
3  *
4  * Copyright 2015 Eddie Berrisford-Lynch <n0521366@ntu.ac.uk>.
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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
22  * THE SOFTWARE.
23  */
24 package uk.ac.ntu.n0521366.wsyd.libs.net;
25
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.*;
33
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;
43
44 /**
45  *
46  * @author Eddie Berrisford-Lynch <dev@fun2be.me>
47  */
48 public class WSYD_SocketAddressTest {
49
50     public WSYD_SocketAddressTest() {
51     }
52
53     @BeforeClass
54     public static void setUpClass() {
55     }
56
57     @AfterClass
58     public static void tearDownClass() {
59     }
60
61     @Before
62     public void setUp() {
63     }
64
65     @After
66     public void tearDown() {
67     }
68
69     @Test
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" };
75         
76         long value;
77         byte[] bytes;
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()));
92                 } else {
93                     fail(MessageFormat.format("Unexpected exception {2} {1} converting: {0}", ipv4_strings[index], e.getMessage(), e.getClass().getName()));
94                 }
95             }
96         }
97     }
98
99     @Test
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);
107     }
108
109     /**
110      * Tests the Constructor(s) that accept parameters
111      */
112     @Test
113     public void testConstructorsByValue() {
114         System.out.println(MessageFormat.format("{0}.WSYD_SocketAddress(values ...)",WSYD_SocketAddress.class.getCanonicalName()));
115
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}));
118
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}));
121         
122         String sEx;
123         byte[] bEx;
124         InetAddress iaEx = null;
125         boolean mEx;
126         WSYD_SocketAddress wia;
127         ListIterator<String> iter = aS.listIterator();
128         while(iter.hasNext()) {
129             int index = iter.nextIndex();
130             sEx = iter.next();
131             bEx = WSYD_SocketAddress.stringIPv4ToBytes4(sEx); // byte[]
132             try {
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));
136             }
137             for (Integer p: aP) {
138                 for (Protocol pr: aPr) {
139                     mEx = aMulti.get(index);
140                     if (mEx == true && pr == Protocol.TCP)
141                         mEx = false;
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)
145                     try {
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()));
152                         }
153                         else
154                             System.out.println(MessageFormat.format("Got expected exception {0}", e.getClass().getName()));
155                     }
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)
160                 }
161             }
162         }
163     }
164
165     /**
166      * Test of setAddress method, of class WSYD_SocketAddress.
167      */
168     @Ignore
169     @Test
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.");
177     }
178
179     /**
180      * Test of getAddress method, of class WSYD_SocketAddress.
181      */
182     @Ignore
183     @Test
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.");
192     }
193
194     /**
195      * Test of setPort method, of class WSYD_SocketAddress.
196      */
197     @Ignore
198     @Test
199     public void testSetPort() {
200         System.out.println("setPort");
201         int port = 0;
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.");
206     }
207
208     /**
209      * Test of getPort method, of class WSYD_SocketAddress.
210      */
211     @Ignore
212     @Test
213     public void testGetPort() {
214         System.out.println("getPort");
215         WSYD_SocketAddress instance = new WSYD_SocketAddress();
216         int expResult = 0;
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.");
221     }
222
223     /**
224      * Test of setSocketAddress method, of class WSYD_SocketAddress.
225      */
226     @Ignore
227     @Test
228     public void testSetSocketAddress() {
229         System.out.println("setSocketAddress");
230         InetAddress address = null;
231         int port = 0;
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.");
236     }
237
238     /**
239      * Test of setProtocol method, of class WSYD_SocketAddress.
240      */
241     @Ignore
242     @Test
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.");
250     }
251
252     /**
253      * Test of getProtocol method, of class WSYD_SocketAddress.
254      */
255     @Ignore
256     @Test
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.");
265     }
266
267     /**
268      * Test of isMulticast method, of class WSYD_SocketAddress.
269      */
270     @Ignore
271     @Test
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.");
280     }
281
282     /**
283      * Test of setReachable method, of class WSYD_SocketAddress.
284      */
285     @Ignore
286     @Test
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.");
295     }
296
297     /**
298      * Test of getReachable method, of class WSYD_SocketAddress.
299      */
300     @Ignore
301     @Test
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.");
310     }
311
312     /**
313      * Test of toString method, of class WSYD_SocketAddress.
314      */
315     @Ignore
316     @Test
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.");
325     }
326
327 }