1 
2 package org.jgroups.tests;
3 
4 import org.jgroups.Address;
5 import org.jgroups.Global;
6 import org.jgroups.stack.IpAddress;
7 import org.jgroups.util.StackType;
8 import org.jgroups.util.Util;
9 import org.testng.Assert;
10 import org.testng.annotations.BeforeClass;
11 import org.testng.annotations.Test;
12 
13 import java.io.*;
14 import java.net.InetAddress;
15 import java.net.UnknownHostException;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.HashSet;
19 
20 @Test(groups=Global.FUNCTIONAL,sequential=true)
21 public class IpAddressTest {
22     IpAddress a, b, c, d, e, f, g, h, i, j, k;
23 
24 
25     @BeforeClass
setUp()26     public void setUp() throws Exception {
27         StackType type=Util.getIpStackType();
28         if(type == StackType.IPv6) {
29             a=new IpAddress("::1", 5555);
30             b=new IpAddress("::1", 5555);
31             d=new IpAddress("::1", 5556);
32             e=new IpAddress("::1", 5555);
33             f=new IpAddress("2001:0db8:0000:0000:0000:002e:0370:2334", 80);
34             g=new IpAddress("2001:0db8:0000:0000:0000:002e:0370:2334", 8080);
35             h=new IpAddress("ff0e::3:4:5", 5555);
36         }
37         else {
38             a=new IpAddress("localhost", 5555);
39             b=new IpAddress("localhost", 5555);
40             d=new IpAddress("localhost", 5556);
41             e=new IpAddress("127.0.0.1", 5555);
42             f=new IpAddress("www.ibm.com", 80);
43             g=new IpAddress("www.ibm.com", 8080);
44             h=new IpAddress("224.0.0.1", 5555);
45         }
46 
47         c=b;
48     }
49 
50 
testUnknownAddress()51     public static void testUnknownAddress() {
52         try {
53             IpAddress tmp=new IpAddress("idontknow.com", 55);
54             assert false : "should throw an UnknownHostException for " + tmp;
55         }
56         catch(UnknownHostException e1) {
57         }
58     }
59 
testEquality()60     public void testEquality() throws Exception {
61         Assert.assertEquals(a, b);
62         Assert.assertEquals(c, b);
63         Assert.assertEquals(a, e);
64         Assert.assertEquals(c, e);
65     }
66 
67 
testEqualityWithDnsRoundRobin()68     public static void testEqualityWithDnsRoundRobin() throws UnknownHostException {
69         IpAddress x1, x2, x3;
70 
71         StackType type=Util.getIpStackType();
72         String tmp=type == StackType.IPv6? "::1" : "127.0.0.1";
73         InetAddress addr=InetAddress.getByName(tmp);
74         byte[] rawAddr=addr.getAddress();
75 
76         InetAddress inet1=InetAddress.getByAddress("MyHost1", rawAddr);
77         InetAddress inet2=InetAddress.getByAddress("MyHost2", rawAddr);
78         InetAddress inet3=InetAddress.getByAddress("MyHost3", rawAddr);
79         Assert.assertEquals(inet1, inet2);
80 
81         x1=new IpAddress(inet1, 5555);
82         x2=new IpAddress(inet2, 5555);
83         x3=new IpAddress(inet3, 5555);
84 
85         Assert.assertEquals(x1, x2);
86         Assert.assertEquals(x3, x1);
87 
88         HashSet<Address> s=new HashSet<Address>();
89         Collections.addAll(s, x1, x2, x3);
90         System.out.println("s=" + s);
91         Assert.assertEquals(1, s.size());
92 
93         HashMap<Address,String> m=new HashMap<Address,String>();
94         m.put(x1, "Bela");
95         m.put(x2, "Michelle");
96         m.put(x3, "Nicole");
97         Assert.assertEquals(1, m.size());
98         Assert.assertEquals("Nicole", m.get(x1));
99     }
100 
101 
102 
testInequality()103     public void testInequality() throws Exception {
104         IpAddress tmp=null;
105         assert !a.equals(d);
106         assert !c.equals(d);
107         assert !a.equals(f);
108         assert !e.equals(f);
109         assert !f.equals(g);
110         assert !(a.equals(tmp));
111     }
112 
113 
114 
testSameHost()115     public void testSameHost() throws Exception {
116         assert Util.sameHost(a, b);
117         assert Util.sameHost(a, c);
118         assert Util.sameHost(a, d);
119         assert Util.sameHost(a, e);
120         assert Util.sameHost(f, g);
121     }
122 
123 
124 
testNotSameHost()125     public void testNotSameHost() throws Exception {
126         assert !Util.sameHost(a, f);
127         assert !Util.sameHost(e, f);
128         assert !Util.sameHost(e, null);
129         assert !Util.sameHost(null, null);
130     }
131 
132 
testMcast()133     public void testMcast() {
134         assert h.isMulticastAddress();
135         assert !a.isMulticastAddress();
136         assert !e.isMulticastAddress();
137         assert !g.isMulticastAddress();
138 
139     }
140 
141 
142 
testCompareTo()143     public void testCompareTo() {
144         Assert.assertEquals(0, a.compareTo(b));
145         assert a.compareTo(d) < 0;
146         assert d.compareTo(a) > 0;
147     }
148 
149 
150 
testCompareTime()151     public void testCompareTime() {
152         final int NUM=1000000;
153         _testCompareTime(a, a, NUM);
154         _testCompareTime(a, b, NUM);
155         _testCompareTime(a, c, NUM);
156         _testCompareTime(a, d, NUM);
157     }
158 
159 
_testCompareTime(IpAddress one, IpAddress two, int num)160     private static void _testCompareTime(IpAddress one, IpAddress two, int num) {
161         int rc=-99;
162         long start=System.currentTimeMillis(), stop;
163         for(int x=0; x < num; x++) {
164             rc=one.compareTo(two);
165         }
166         stop=System.currentTimeMillis();
167         long diff=stop-start;
168         System.out.println("calling compareTo(" + one + ", " + two + ") " + num + " times took " +
169                            diff + "ms, result=" + rc);
170     }
171 
172 
173 
testHashcode()174     public void testHashcode() {
175         int hcode_a=a.hashCode();
176         int hcode_b=b.hashCode();
177         Assert.assertEquals(hcode_a, hcode_b);
178     }
179 
180 
181 
testHashcodeTime()182     public void testHashcodeTime() {
183         int hash=-1;
184         final int NUM=10000000;
185 
186         long start=System.currentTimeMillis(), stop;
187         for(int x=0; x < NUM; x++) {
188             hash=a.hashCode();
189         }
190         stop=System.currentTimeMillis();
191         long diff=stop-start;
192         System.out.println("taking the hash code of " + a + "(" + hash + ") took " + diff + "ms");
193     }
194 
195 
196 
testIPv6WithExternalization()197     public static void testIPv6WithExternalization() throws IOException, ClassNotFoundException {
198         InetAddress tmp=Util.getNonLoopbackAddress();
199         IpAddress ip=new IpAddress(tmp, 5555);
200 
201         ByteArrayOutputStream bos=new ByteArrayOutputStream();
202         ObjectOutputStream    oos=new ObjectOutputStream(bos);
203         byte[]                buf=null;
204         ByteArrayInputStream  bis=null;
205         ObjectInputStream     ois;
206 
207         System.out.println("-- address is " + tmp);
208 
209         oos.writeObject(ip);
210         buf=bos.toByteArray();
211         bis=new ByteArrayInputStream(buf);
212         ois=new ObjectInputStream(bis);
213         IpAddress ip2=(IpAddress)ois.readObject();
214         Assert.assertEquals(ip, ip2);
215     }
216 
217 
218 
219 
testIPv6WithStreamable()220     public static void testIPv6WithStreamable() throws IOException, ClassNotFoundException {
221         InetAddress tmp=Util.getNonLoopbackAddress();
222         IpAddress ip=new IpAddress(tmp, 5555);
223 
224         ByteArrayOutputStream bos=new ByteArrayOutputStream();
225         DataOutputStream      dos=new DataOutputStream(bos);
226         byte[]                buf=null;
227         ByteArrayInputStream  bis=null;
228         DataInputStream       dis;
229 
230         System.out.println("-- address is " + tmp);
231 
232         ip.writeTo(dos);
233         buf=bos.toByteArray();
234         bis=new ByteArrayInputStream(buf);
235         dis=new DataInputStream(bis);
236         IpAddress ip2=new IpAddress();
237         ip2.readFrom(dis);
238         Assert.assertEquals(ip, ip2);
239     }
240 
241 
testExternalization()242     public void testExternalization() throws Exception {
243         ByteArrayOutputStream bos=new ByteArrayOutputStream();
244         ObjectOutputStream    oos=new ObjectOutputStream(bos);
245         byte[]                buf=null;
246         ByteArrayInputStream  bis=null;
247         ObjectInputStream     ois;
248         IpAddress             a2, b2;
249 
250         a.setAdditionalData(null);
251         b.setAdditionalData("Bela Ban".getBytes());
252         oos.writeObject(a);
253         oos.writeObject(b);
254 
255 
256         buf=bos.toByteArray();
257         bis=new ByteArrayInputStream(buf);
258         ois=new ObjectInputStream(bis);
259         a2=(IpAddress)ois.readObject();
260         b2=(IpAddress)ois.readObject();
261 
262         Assert.assertEquals(a, a2);
263         Assert.assertEquals(b, b2);
264 
265         assert a2.getAdditionalData() == null;
266         Assert.assertEquals("Bela Ban", new String(b2.getAdditionalData()));
267     }
268 
269 
270 
271 
testExternalizationAdditionalData()272     public void testExternalizationAdditionalData() throws Exception {
273         ByteArrayOutputStream bos=new ByteArrayOutputStream();
274         ObjectOutputStream    oos=new ObjectOutputStream(bos);
275         byte[]                buf=null;
276         ByteArrayInputStream  bis=null;
277         ObjectInputStream     ois;
278         IpAddress             a2, b2, c2, d2, e2, f2, g2, h2;
279 
280         oos.writeObject(a);
281         oos.writeObject(b);
282         oos.writeObject(c);
283         oos.writeObject(d);
284         oos.writeObject(e);
285         oos.writeObject(f);
286         oos.writeObject(g);
287         oos.writeObject(h);
288 
289 
290         buf=bos.toByteArray();
291         bis=new ByteArrayInputStream(buf);
292         ois=new ObjectInputStream(bis);
293         a2=(IpAddress)ois.readObject();
294         b2=(IpAddress)ois.readObject();
295         c2=(IpAddress)ois.readObject();
296         d2=(IpAddress)ois.readObject();
297         e2=(IpAddress)ois.readObject();
298         f2=(IpAddress)ois.readObject();
299         g2=(IpAddress)ois.readObject();
300         h2=(IpAddress)ois.readObject();
301 
302         Assert.assertEquals(b2, c2);
303         Assert.assertEquals(a, a2);
304         Assert.assertEquals(b, b2);
305         Assert.assertEquals(c, c2);
306         Assert.assertEquals(d, d2);
307         Assert.assertEquals(e, e2);
308         Assert.assertEquals(f, f2);
309         Assert.assertEquals(g, g2);
310         Assert.assertEquals(h, h2);
311     }
312 
313 
314 
testStreamable()315     public void testStreamable() throws Exception {
316         ByteArrayOutputStream bos=new ByteArrayOutputStream();
317         DataOutputStream      oos=new DataOutputStream(bos);
318         byte[]                buf=null;
319         ByteArrayInputStream  bis=null;
320         DataInputStream       ois;
321         IpAddress             a2, b2, x, x2, y, y2;
322 
323         x=createStackConformantAddress(5555);
324         x.setAdditionalData(new byte[]{'b','e','l','a'});
325 
326         y=createStackConformantAddress(1111);
327         y.setAdditionalData(new byte[]{'b','e','l','a'});
328 
329         a.setAdditionalData(null);
330         b.setAdditionalData("Bela Ban".getBytes());
331         a.writeTo(oos);
332         b.writeTo(oos);
333         x.writeTo(oos);
334         y.writeTo(oos);
335 
336         buf=bos.toByteArray();
337         bis=new ByteArrayInputStream(buf);
338         ois=new DataInputStream(bis);
339         a2=new IpAddress();
340         a2.readFrom(ois);
341         b2=new IpAddress();
342         b2.readFrom(ois);
343         x2=new IpAddress();
344         x2.readFrom(ois);
345         y2=new IpAddress();
346         y2.readFrom(ois);
347 
348         Assert.assertEquals(a, a2);
349         Assert.assertEquals(b, b2);
350 
351         assert a2.getAdditionalData() == null;
352         Assert.assertEquals("Bela Ban", new String(b2.getAdditionalData()));
353 
354         assert x2.getAdditionalData() != null;
355         Assert.assertEquals(4, x2.getAdditionalData().length);
356 
357         assert y2.getIpAddress() != null;
358         Assert.assertEquals(1111, y2.getPort());
359         assert y2.getAdditionalData() != null;
360         Assert.assertEquals(4, y2.getAdditionalData().length);
361     }
362 
363 
364 
365 
testStreamableWithHighPort()366     public static void testStreamableWithHighPort() throws Exception {
367         ByteArrayOutputStream bos=new ByteArrayOutputStream();
368         DataOutputStream      oos=new DataOutputStream(bos);
369         byte[]                buf=null;
370         ByteArrayInputStream  bis=null;
371         DataInputStream       dis;
372         IpAddress             x, x2;
373 
374         x=createStackConformantAddress(65535);
375         x.writeTo(oos);
376 
377         buf=bos.toByteArray();
378         bis=new ByteArrayInputStream(buf);
379         dis=new DataInputStream(bis);
380 
381         x2=new IpAddress();
382         x2.readFrom(dis);
383         System.out.println("x: " + x + ", x2: " + x2);
384 
385         assert x2.getPort() > 0;
386         Assert.assertEquals(x.getPort(), x2.getPort());
387     }
388 
389 
390 
391 
testStreamableAdditionalData()392     public void testStreamableAdditionalData() throws Exception {
393         ByteArrayOutputStream bos=new ByteArrayOutputStream();
394         DataOutputStream      oos=new DataOutputStream(bos);
395         byte[]                buf=null;
396         ByteArrayInputStream  bis=null;
397         DataInputStream       ois;
398         IpAddress             a2, b2, c2, d2, e2, f2, g2, h2;
399 
400         a.writeTo(oos);
401         b.writeTo(oos);
402         c.writeTo(oos);
403         d.writeTo(oos);
404         e.writeTo(oos);
405         f.writeTo(oos);
406         g.writeTo(oos);
407         h.writeTo(oos);
408 
409 
410         buf=bos.toByteArray();
411         bis=new ByteArrayInputStream(buf);
412         ois=new DataInputStream(bis);
413         a2=new IpAddress();
414         a2.readFrom(ois);
415         b2=new IpAddress();
416         b2.readFrom(ois);
417         c2=new IpAddress();
418         c2.readFrom(ois);
419         d2=new IpAddress();
420         d2.readFrom(ois);
421         e2=new IpAddress();
422         e2.readFrom(ois);
423         f2=new IpAddress();
424         f2.readFrom(ois);
425         g2=new IpAddress();
426         g2.readFrom(ois);
427         h2=new IpAddress();
428         h2.readFrom(ois);
429 
430         Assert.assertEquals(b2, c2);
431         Assert.assertEquals(a, a2);
432         Assert.assertEquals(b, b2);
433         Assert.assertEquals(c, c2);
434         Assert.assertEquals(d, d2);
435         Assert.assertEquals(e, e2);
436         Assert.assertEquals(f, f2);
437         Assert.assertEquals(g, g2);
438         Assert.assertEquals(h, h2);
439     }
440 
441 
createStackConformantAddress(int port)442     private static IpAddress createStackConformantAddress(int port) throws UnknownHostException {
443         StackType type=Util.getIpStackType();
444         if(type == StackType.IPv6)
445             return new IpAddress("::1", port);
446         else
447             return new IpAddress("127.0.0.1", port);
448     }
449 
450 
451 }
452