Inet4AddressTest.java revision 89c1feb0a69a7707b271086e749975b3f7acacf7
1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.apache.harmony.luni.tests.java.net;
19
20import dalvik.annotation.TestTargetClass;
21import dalvik.annotation.TestInfo;
22import dalvik.annotation.TestLevel;
23import dalvik.annotation.TestTarget;
24
25import java.io.Serializable;
26import java.net.Inet4Address;
27import java.net.InetAddress;
28
29import org.apache.harmony.testframework.serialization.SerializationTest;
30import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
31
32@TestTargetClass(Inet4Address.class)
33public class Inet4AddressTest extends junit.framework.TestCase {
34
35    /**
36     * @tests java.net.Inet4Address#isMulticastAddress()
37     */
38@TestInfo(
39      level = TestLevel.COMPLETE,
40      purpose = "",
41      targets = {
42        @TestTarget(
43          methodName = "isMulticastAddress",
44          methodArgs = {}
45        )
46    })
47    public void test_isMulticastAddress() {
48
49        // Create 2 IP v4 addresses and call "isMulticastAddress()"
50        // result should return true if the first 4 bits of the
51        // address are: 1110, false otherwise
52        // Make 1 address with 1110, and 1 without
53        String addrName = "";
54        try {
55            addrName = "224.0.0.0"; // a multicast addr 1110 = 224-239
56            InetAddress addr = InetAddress.getByName(addrName);
57            assertTrue("Multicast address " + addrName + " not detected.", addr
58                    .isMulticastAddress());
59
60            addrName = "239.255.255.255"; // a multicast addr 1110 = 224-239
61            addr = InetAddress.getByName(addrName);
62            assertTrue("Multicast address " + addrName + " not detected.", addr
63                    .isMulticastAddress());
64
65            addrName = "42.42.42.42"; // a non-multicast address
66            addr = InetAddress.getByName(addrName);
67            assertTrue("Non multicast address " + addrName
68                    + " reporting as a multicast address.", !addr
69                    .isMulticastAddress());
70        } catch (Exception e) {
71            fail("Unknown address : " + addrName);
72        }
73
74    }
75
76    /**
77     * @tests java.net.Inet4Address#isAnyLocalAddress()
78     */
79@TestInfo(
80      level = TestLevel.COMPLETE,
81      purpose = "",
82      targets = {
83        @TestTarget(
84          methodName = "isAnyLocalAddress",
85          methodArgs = {}
86        )
87    })
88    public void test_isAnyLocalAddress() {
89        String addrName = "";
90        try {
91            addrName = "0.0.0.0";
92            InetAddress addr = InetAddress.getByName(addrName);
93            assertTrue("ANY address " + addrName + " not detected.", addr
94                    .isAnyLocalAddress());
95        } catch (Exception e) {
96            fail("Unknown address : " + addrName);
97        }
98    }
99
100    /**
101     * @tests java.net.Inet4Address#isLoopbackAddress()
102     */
103@TestInfo(
104      level = TestLevel.COMPLETE,
105      purpose = "",
106      targets = {
107        @TestTarget(
108          methodName = "isLoopbackAddress",
109          methodArgs = {}
110        )
111    })
112    public void test_isLoopbackAddress() {
113        // Create some IP V4 addresses and test if they are local...
114
115        String addrName = "";
116        try {
117            addrName = "127.0.0.0"; // a loopback address should be 127.d.d.d
118            InetAddress addr = InetAddress.getByName(addrName);
119            assertTrue("Loopback address " + addrName + " not detected.", addr
120                    .isLoopbackAddress());
121
122            addrName = "127.42.42.42"; // a loopback address should be
123            // 127.d.d.d
124            addr = InetAddress.getByName(addrName);
125            assertTrue("Loopback address " + addrName + " not detected.", addr
126                    .isLoopbackAddress());
127
128            addrName = "42.42.42.42"; // a loopback address should be
129            // 127.d.d.d
130            addr = InetAddress.getByName(addrName);
131            assertTrue("Address incorrectly " + addrName
132                    + " detected as a loopback address.", !addr
133                    .isLoopbackAddress());
134
135        } catch (Exception e) {
136            fail("Unknown address : " + addrName);
137        }
138
139    }
140
141    /**
142     * @tests java.net.Inet4Address#isLinkLocalAddress()
143     */
144@TestInfo(
145      level = TestLevel.COMPLETE,
146      purpose = "",
147      targets = {
148        @TestTarget(
149          methodName = "isLinkLocalAddress",
150          methodArgs = {}
151        )
152    })
153    public void test_isLinkLocalAddress() {
154
155        String addrName = "";
156        try {
157            // There are no link local addresses for IPv4
158            // We'll test one to ensure we get "false"
159
160            addrName = "42.42.42.42";
161            InetAddress addr = InetAddress.getByName(addrName);
162            assertTrue("IPv4 address " + addrName
163                    + " incorrectly reporting as a link local address.", !addr
164                    .isLinkLocalAddress());
165        } catch (Exception e) {
166            fail("Unknown address : " + e.getMessage());
167        }
168    }
169
170    /**
171     * @tests java.net.Inet4Address#isSiteLocalAddress()
172     */
173@TestInfo(
174      level = TestLevel.COMPLETE,
175      purpose = "",
176      targets = {
177        @TestTarget(
178          methodName = "isSiteLocalAddress",
179          methodArgs = {}
180        )
181    })
182    public void test_isSiteLocalAddress() {
183        String addrName = "";
184        try {
185            // There are no site local addresses for IPv4
186            // We'll test one to ensure we get "false"
187
188            addrName = "42.42.42.42";
189            InetAddress addr = InetAddress.getByName(addrName);
190            assertTrue("IPv4 address " + addrName
191                    + " incorrectly reporting as a site local address.", !addr
192                    .isSiteLocalAddress());
193        } catch (Exception e) {
194            fail("Unknown address : " + e.getMessage());
195        }
196    }
197
198    /**
199     * @tests java.net.Inet4Address#isMCGlobal()
200     */
201@TestInfo(
202      level = TestLevel.COMPLETE,
203      purpose = "",
204      targets = {
205        @TestTarget(
206          methodName = "isMCGlobal",
207          methodArgs = {}
208        )
209    })
210    public void test_isMCGlobal() {
211
212        // Create an IPv4 mulitcast address. It should return
213        // false for globabl mutlicast. There are no valid IPv4
214        // global multicast addresses
215
216        String addrName = "";
217        try {
218            addrName = "224.0.0.0"; // a multicast addr 1110
219            InetAddress addr = InetAddress.getByName(addrName);
220            assertTrue("IPv4 link-local multicast address " + addrName
221                    + " incorrectly identified as a global multicast address.",
222                    !addr.isMCGlobal());
223
224            addrName = "224.0.0.255"; // a multicast addr 1110
225            addr = InetAddress.getByName(addrName);
226            assertTrue("IPv4 link-local multicast address " + addrName
227                    + " incorrectly identified as a global multicast address.",
228                    !addr.isMCGlobal());
229
230            addrName = "224.0.1.0"; // a multicast addr 1110
231            addr = InetAddress.getByName(addrName);
232            assertTrue("IPv4 global multicast address " + addrName
233                    + " not identified as a global multicast address.", addr
234                    .isMCGlobal());
235
236            addrName = "238.255.255.255"; // a multicast addr 1110
237            addr = InetAddress.getByName(addrName);
238            assertTrue("IPv4 global multicast address " + addrName
239                    + " not identified as a global multicast address.", addr
240                    .isMCGlobal());
241
242            addrName = "239.0.0.0"; // a multicast addr 1110
243            addr = InetAddress.getByName(addrName);
244            assertTrue("IPv4 reserved multicast address " + addrName
245                    + " incorrectly identified as a global multicast address.",
246                    !addr.isMCGlobal());
247
248            addrName = "239.191.255.255"; // a multicast addr 1110
249            addr = InetAddress.getByName(addrName);
250            assertTrue("IPv4 reserved multicast address " + addrName
251                    + " incorrectly identified as a global multicast address.",
252                    !addr.isMCGlobal());
253
254        } catch (Exception e) {
255            fail("Unknown address : " + e.getMessage());
256        }
257    }
258
259    /**
260     * @tests java.net.Inet4Address#isMCNodeLocal()
261     */
262@TestInfo(
263      level = TestLevel.COMPLETE,
264      purpose = "",
265      targets = {
266        @TestTarget(
267          methodName = "isMCNodeLocal",
268          methodArgs = {}
269        )
270    })
271    public void test_isMCNodeLocal() {
272        // Create an IPv4 mulitcast address. It should return
273        // false for node-local mutlicast. There are no valid IPv4
274        // node-local multicast addresses
275
276        String addrName = "";
277        try {
278            addrName = "224.42.42.42"; // a multicast addr 1110 = 224
279            InetAddress addr = InetAddress.getByName(addrName);
280            assertTrue(
281                    "IPv4 multicast address "
282                            + addrName
283                            + " incorrectly identified as a node-local multicast address.",
284                    !addr.isMCNodeLocal());
285
286            addrName = "239.0.0.0"; // a multicast addr 1110
287            addr = InetAddress.getByName(addrName);
288            assertTrue(
289                    "IPv4 reserved multicast address "
290                            + addrName
291                            + " incorrectly identified as a node-local multicast address.",
292                    !addr.isMCNodeLocal());
293
294        } catch (Exception e) {
295            fail("Unknown address : " + e.getMessage());
296        }
297    }
298
299    /**
300     * @tests java.net.Inet4Address#isMCLinkLocal()
301     */
302@TestInfo(
303      level = TestLevel.COMPLETE,
304      purpose = "",
305      targets = {
306        @TestTarget(
307          methodName = "isMCLinkLocal",
308          methodArgs = {}
309        )
310    })
311    public void test_isMCLinkLocal() {
312        // Create an IPv4 mulitcast address. It should return
313        // false for link-local mutlicast. There are no valid IPv4
314        // link-local multicast addresses
315
316        String addrName = "";
317        try {
318            addrName = "224.0.0.0"; // a multicast addr 1110
319            InetAddress addr = InetAddress.getByName(addrName);
320            assertTrue("IPv4 link-local multicast address " + addrName
321                    + " not identified as a link-local multicast address.",
322                    addr.isMCLinkLocal());
323
324            addrName = "224.0.0.255"; // a multicast addr 1110
325            addr = InetAddress.getByName(addrName);
326            assertTrue("IPv4 link-local multicast address " + addrName
327                    + " not identified as a link-local multicast address.",
328                    addr.isMCLinkLocal());
329
330            addrName = "224.0.1.0"; // a multicast addr 1110
331            addr = InetAddress.getByName(addrName);
332            assertTrue(
333                    "IPv4 global multicast address "
334                            + addrName
335                            + " incorrectly identified as a link-local multicast address.",
336                    !addr.isMCLinkLocal());
337
338            addrName = "239.0.0.0"; // a multicast addr 1110
339            addr = InetAddress.getByName(addrName);
340            assertTrue(
341                    "IPv4 reserved multicast address "
342                            + addrName
343                            + " incorrectly identified as a link-local multicast address.",
344                    !addr.isMCLinkLocal());
345
346        } catch (Exception e) {
347            fail("Unknown address : " + addrName);
348        }
349    }
350
351    /**
352     * @tests java.net.Inet4Address#isMCSiteLocal()
353     */
354@TestInfo(
355      level = TestLevel.COMPLETE,
356      purpose = "",
357      targets = {
358        @TestTarget(
359          methodName = "isMCSiteLocal",
360          methodArgs = {}
361        )
362    })
363    public void test_isMCSiteLocal() {
364        // Create an IPv4 mulitcast address. It should return
365        // false for site-local mutlicast. There are no valid IPv4
366        // site-local multicast addresses
367
368        String addrName = "";
369        try {
370            addrName = "240.0.0.0"; // a multicast addr 1110 = 224
371            InetAddress addr = InetAddress.getByName(addrName);
372            assertTrue(
373                    "IPv4 multicast address "
374                            + addrName
375                            + " incorrectly identified as a site-local multicast address.",
376                    !addr.isMCSiteLocal());
377
378            addrName = "239.0.0.0"; // a multicast addr 1110
379            addr = InetAddress.getByName(addrName);
380            assertTrue(
381                    "IPv4 reserved multicast address "
382                            + addrName
383                            + " incorrectly identified as a site-local multicast address.",
384                    !addr.isMCSiteLocal());
385
386            addrName = "239.255.0.0"; // a multicast addr 1110
387            addr = InetAddress.getByName(addrName);
388            assertTrue("IPv4 site-local multicast address " + addrName
389                    + " not identified as a site-local multicast address.",
390                    addr.isMCSiteLocal());
391
392            addrName = "239.255.255.255"; // a multicast addr 1110
393            addr = InetAddress.getByName(addrName);
394            assertTrue("IPv4 site-local multicast address " + addrName
395                    + " not identified as a site-local multicast address.",
396                    addr.isMCSiteLocal());
397
398            addrName = "239.255.2.2"; // a multicast addr 1110
399            addr = InetAddress.getByName(addrName);
400            assertTrue("IPv4 site-local multicast address " + addrName
401                    + " not identified as a site-local multicast address.",
402                    addr.isMCSiteLocal());
403
404        } catch (Exception e) {
405            fail("Unknown address : " + addrName);
406        }
407    }
408
409    /**
410     * @tests java.net.Inet4Address#isMCOrgLocal()
411     */
412@TestInfo(
413      level = TestLevel.COMPLETE,
414      purpose = "",
415      targets = {
416        @TestTarget(
417          methodName = "isMCOrgLocal",
418          methodArgs = {}
419        )
420    })
421    public void test_isMCOrgLocal() {
422        // Create an IPv4 mulitcast address. It should return
423        // false for organization-local mutlicast. There are no valid IPv4
424        // organization-local multicast addresses
425
426        String addrName = "";
427        try {
428
429            addrName = "239.191.255.255"; // a multicast addr 1110
430            InetAddress addr = InetAddress.getByName(addrName);
431            assertTrue(
432                    "IPv4 reserved multicast address "
433                            + addrName
434                            + " incorrectly identified as a org-local multicast address.",
435                    !addr.isMCOrgLocal());
436
437            addrName = "239.252.0.0"; // a multicast addr 1110
438            addr = InetAddress.getByName(addrName);
439            assertTrue(
440                    "IPv4 site-local multicast address "
441                            + addrName
442                            + " incorrectly identified as a org-local multicast address.",
443                    !addr.isMCOrgLocal());
444
445            addrName = "239.192.0.0"; // a multicast addr 1110
446            addr = InetAddress.getByName(addrName);
447            assertTrue("IPv4 org-local multicast address " + addrName
448                    + " not identified as a org-local multicast address.", addr
449                    .isMCOrgLocal());
450
451            addrName = "239.195.255.255"; // a multicast addr 1110
452            addr = InetAddress.getByName(addrName);
453            assertTrue("IPv4 org-local multicast address " + addrName
454                    + " not identified as a org-local multicast address.", addr
455                    .isMCOrgLocal());
456
457        } catch (Exception e) {
458            fail("Unknown address : " + addrName);
459        }
460    }
461
462    // comparator for Inet4Address objects
463    private static final SerializableAssert COMPARATOR = new SerializableAssert() {
464        public void assertDeserialized(Serializable initial,
465                Serializable deserialized) {
466
467            Inet4Address initAddr = (Inet4Address) initial;
468            Inet4Address desrAddr = (Inet4Address) deserialized;
469
470            byte[] iaAddresss = initAddr.getAddress();
471            byte[] deIAAddresss = desrAddr.getAddress();
472            for (int i = 0; i < iaAddresss.length; i++) {
473                assertEquals(iaAddresss[i], deIAAddresss[i]);
474            }
475            assertEquals(4, deIAAddresss.length);
476            assertEquals(initAddr.getHostName(), desrAddr.getHostName());
477        }
478    };
479
480    /**
481     * @tests serialization/deserialization compatibility.
482     */
483    @TestInfo(
484              level = TestLevel.COMPLETE,
485              purpose = "Checks serialization",
486              targets = {
487                @TestTarget(
488                  methodName = "!SerializationSelf",
489                  methodArgs = {}
490                )
491            })
492    public void testSerializationSelf() throws Exception {
493
494        SerializationTest.verifySelf(Inet4Address.getByName("localhost"),
495                COMPARATOR);
496    }
497
498    /**
499     * @tests serialization/deserialization compatibility with RI.
500     */
501    @TestInfo(
502              level = TestLevel.COMPLETE,
503              purpose = "Checks serialization",
504              targets = {
505                @TestTarget(
506                  methodName = "!SerializationGolden",
507                  methodArgs = {}
508                )
509            })
510    public void testSerializationCompatibility() throws Exception {
511
512        SerializationTest.verifyGolden(this, Inet4Address
513                .getByName("localhost"), COMPARATOR);
514    }
515}
516