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 java.io.Serializable;
21import java.net.Inet4Address;
22import java.net.InetAddress;
23
24import org.apache.harmony.testframework.serialization.SerializationTest;
25import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
26
27public class Inet4AddressTest extends junit.framework.TestCase {
28
29	/**
30	 * @tests java.net.Inet4Address#isMulticastAddress()
31	 */
32	public void test_isMulticastAddress() throws Exception {
33
34		// Create 2 IP v4 addresses and call "isMulticastAddress()"
35		// result should return true if the first 4 bits of the
36		// address are: 1110, false otherwise
37		// Make 1 address with 1110, and 1 without
38		String addrName = "";
39                addrName = "224.0.0.0"; // a multicast addr 1110 = 224-239
40                InetAddress addr = InetAddress.getByName(addrName);
41                assertTrue("Multicast address " + addrName + " not detected.", addr
42                                .isMulticastAddress());
43
44                addrName = "239.255.255.255"; // a multicast addr 1110 = 224-239
45                addr = InetAddress.getByName(addrName);
46                assertTrue("Multicast address " + addrName + " not detected.", addr
47                                .isMulticastAddress());
48
49                addrName = "42.42.42.42"; // a non-multicast address
50                addr = InetAddress.getByName(addrName);
51                assertTrue("Non multicast address " + addrName
52                                + " reporting as a multicast address.", !addr
53                                .isMulticastAddress());
54
55	}
56
57    public void test_isAnyLocalAddress() throws Exception {
58        assertTrue(InetAddress.getByName("0.0.0.0").isAnyLocalAddress());
59        assertFalse(InetAddress.getByName("127.0.0.1").isAnyLocalAddress());
60    }
61
62	public void test_isLoopbackAddress() throws Exception {
63		// Create some IP V4 addresses and test if they are local...
64
65		String addrName = "";
66
67                addrName = "127.0.0.0"; // a loopback address should be 127.d.d.d
68                InetAddress addr = InetAddress.getByName(addrName);
69                assertTrue("Loopback address " + addrName + " not detected.", addr
70                                .isLoopbackAddress());
71
72                addrName = "127.42.42.42"; // a loopback address should be
73                // 127.d.d.d
74                addr = InetAddress.getByName(addrName);
75                assertTrue("Loopback address " + addrName + " not detected.", addr
76                                .isLoopbackAddress());
77
78                addrName = "42.42.42.42"; // a loopback address should be
79                // 127.d.d.d
80                addr = InetAddress.getByName(addrName);
81                assertTrue("Address incorrectly " + addrName
82                                + " detected as a loopback address.", !addr
83                                .isLoopbackAddress());
84	}
85
86	/**
87	 * @tests java.net.Inet4Address#isLinkLocalAddress()
88	 */
89	public void test_isLinkLocalAddress() throws Exception {
90
91		String addrName = "";
92                // There are no link local addresses for IPv4
93                // We'll test one to ensure we get "false"
94
95                addrName = "42.42.42.42";
96                InetAddress addr = InetAddress.getByName(addrName);
97                assertTrue("IPv4 address " + addrName
98                                + " incorrectly reporting as a link local address.", !addr
99                                .isLinkLocalAddress());
100	}
101
102	/**
103	 * @tests java.net.Inet4Address#isSiteLocalAddress()
104	 */
105	public void test_isSiteLocalAddress() throws Exception {
106		String addrName = "";
107                // There are no site local addresses for IPv4
108                // We'll test one to ensure we get "false"
109
110                addrName = "42.42.42.42";
111                InetAddress addr = InetAddress.getByName(addrName);
112                assertTrue("IPv4 address " + addrName
113                                + " incorrectly reporting as a site local address.", !addr
114                                .isSiteLocalAddress());
115	}
116
117	/**
118	 * @tests java.net.Inet4Address#isMCGlobal()
119	 */
120	public void test_isMCGlobal() throws Exception {
121
122		// Create an IPv4 mulitcast address. It should return
123		// false for globabl mutlicast. There are no valid IPv4
124		// global multicast addresses
125
126		String addrName = "";
127                addrName = "224.0.0.0"; // a multicast addr 1110
128                InetAddress addr = InetAddress.getByName(addrName);
129                assertTrue("IPv4 link-local multicast address " + addrName
130                                + " incorrectly identified as a global multicast address.",
131                                !addr.isMCGlobal());
132
133                addrName = "224.0.0.255"; // a multicast addr 1110
134                addr = InetAddress.getByName(addrName);
135                assertTrue("IPv4 link-local multicast address " + addrName
136                                + " incorrectly identified as a global multicast address.",
137                                !addr.isMCGlobal());
138
139                addrName = "224.0.1.0"; // a multicast addr 1110
140                addr = InetAddress.getByName(addrName);
141                assertTrue("IPv4 global multicast address " + addrName
142                                + " not identified as a global multicast address.", addr
143                                .isMCGlobal());
144
145                addrName = "238.255.255.255"; // a multicast addr 1110
146                addr = InetAddress.getByName(addrName);
147                assertTrue("IPv4 global multicast address " + addrName
148                                + " not identified as a global multicast address.", addr
149                                .isMCGlobal());
150
151                addrName = "239.0.0.0"; // a multicast addr 1110
152                addr = InetAddress.getByName(addrName);
153                assertTrue("IPv4 reserved multicast address " + addrName
154                                + " incorrectly identified as a global multicast address.",
155                                !addr.isMCGlobal());
156
157                addrName = "239.191.255.255"; // a multicast addr 1110
158                addr = InetAddress.getByName(addrName);
159                assertTrue("IPv4 reserved multicast address " + addrName
160                                + " incorrectly identified as a global multicast address.",
161                                !addr.isMCGlobal());
162	}
163
164	/**
165	 * @tests java.net.Inet4Address#isMCNodeLocal()
166	 */
167	public void test_isMCNodeLocal() throws Exception {
168		// Create an IPv4 mulitcast address. It should return
169		// false for node-local mutlicast. There are no valid IPv4
170		// node-local multicast addresses
171
172		String addrName = "";
173                addrName = "224.42.42.42"; // a multicast addr 1110 = 224
174                InetAddress addr = InetAddress.getByName(addrName);
175                assertTrue(
176                                "IPv4 multicast address "
177                                                + addrName
178                                                + " incorrectly identified as a node-local multicast address.",
179                                !addr.isMCNodeLocal());
180
181                addrName = "239.0.0.0"; // a multicast addr 1110
182                addr = InetAddress.getByName(addrName);
183                assertTrue(
184                                "IPv4 reserved multicast address "
185                                                + addrName
186                                                + " incorrectly identified as a node-local multicast address.",
187                                !addr.isMCNodeLocal());
188	}
189
190	/**
191	 * @tests java.net.Inet4Address#isMCLinkLocal()
192	 */
193	public void test_isMCLinkLocal() throws Exception {
194		// Create an IPv4 mulitcast address. It should return
195		// false for link-local mutlicast. There are no valid IPv4
196		// link-local multicast addresses
197
198		String addrName = "";
199                addrName = "224.0.0.0"; // a multicast addr 1110
200                InetAddress addr = InetAddress.getByName(addrName);
201                assertTrue("IPv4 link-local multicast address " + addrName
202                                + " not identified as a link-local multicast address.",
203                                addr.isMCLinkLocal());
204
205                addrName = "224.0.0.255"; // a multicast addr 1110
206                addr = InetAddress.getByName(addrName);
207                assertTrue("IPv4 link-local multicast address " + addrName
208                                + " not identified as a link-local multicast address.",
209                                addr.isMCLinkLocal());
210
211                addrName = "224.0.1.0"; // a multicast addr 1110
212                addr = InetAddress.getByName(addrName);
213                assertTrue(
214                                "IPv4 global multicast address "
215                                                + addrName
216                                                + " incorrectly identified as a link-local multicast address.",
217                                !addr.isMCLinkLocal());
218
219                addrName = "239.0.0.0"; // a multicast addr 1110
220                addr = InetAddress.getByName(addrName);
221                assertTrue(
222                                "IPv4 reserved multicast address "
223                                                + addrName
224                                                + " incorrectly identified as a link-local multicast address.",
225                                !addr.isMCLinkLocal());
226	}
227
228	/**
229	 * @tests java.net.Inet4Address#isMCSiteLocal()
230	 */
231	public void test_isMCSiteLocal() throws Exception {
232		// Create an IPv4 mulitcast address. It should return
233		// false for site-local mutlicast. There are no valid IPv4
234		// site-local multicast addresses
235
236		String addrName = "";
237                addrName = "240.0.0.0"; // a multicast addr 1110 = 224
238                InetAddress addr = InetAddress.getByName(addrName);
239                assertTrue(
240                                "IPv4 multicast address "
241                                                + addrName
242                                                + " incorrectly identified as a site-local multicast address.",
243                                !addr.isMCSiteLocal());
244
245                addrName = "239.0.0.0"; // a multicast addr 1110
246                addr = InetAddress.getByName(addrName);
247                assertTrue(
248                                "IPv4 reserved multicast address "
249                                                + addrName
250                                                + " incorrectly identified as a site-local multicast address.",
251                                !addr.isMCSiteLocal());
252
253                addrName = "239.255.0.0"; // a multicast addr 1110
254                addr = InetAddress.getByName(addrName);
255                assertTrue("IPv4 site-local multicast address " + addrName
256                                + " not identified as a site-local multicast address.",
257                                addr.isMCSiteLocal());
258
259                addrName = "239.255.255.255"; // a multicast addr 1110
260                addr = InetAddress.getByName(addrName);
261                assertTrue("IPv4 site-local multicast address " + addrName
262                                + " not identified as a site-local multicast address.",
263                                addr.isMCSiteLocal());
264
265                addrName = "239.255.2.2"; // a multicast addr 1110
266                addr = InetAddress.getByName(addrName);
267                assertTrue("IPv4 site-local multicast address " + addrName
268                                + " not identified as a site-local multicast address.",
269                                addr.isMCSiteLocal());
270	}
271
272	/**
273	 * @tests java.net.Inet4Address#isMCOrgLocal()
274	 */
275	public void test_isMCOrgLocal() throws Exception {
276		// Create an IPv4 mulitcast address. It should return
277		// false for organization-local mutlicast. There are no valid IPv4
278		// organization-local multicast addresses
279
280		String addrName = "";
281
282                addrName = "239.191.255.255"; // a multicast addr 1110
283                InetAddress addr = InetAddress.getByName(addrName);
284                assertTrue(
285                                "IPv4 reserved multicast address "
286                                                + addrName
287                                                + " incorrectly identified as a org-local multicast address.",
288                                !addr.isMCOrgLocal());
289
290                addrName = "239.252.0.0"; // a multicast addr 1110
291                addr = InetAddress.getByName(addrName);
292                assertTrue(
293                                "IPv4 site-local multicast address "
294                                                + addrName
295                                                + " incorrectly identified as a org-local multicast address.",
296                                !addr.isMCOrgLocal());
297
298                addrName = "239.192.0.0"; // a multicast addr 1110
299                addr = InetAddress.getByName(addrName);
300                assertTrue("IPv4 org-local multicast address " + addrName
301                                + " not identified as a org-local multicast address.", addr
302                                .isMCOrgLocal());
303
304                addrName = "239.195.255.255"; // a multicast addr 1110
305                addr = InetAddress.getByName(addrName);
306                assertTrue("IPv4 org-local multicast address " + addrName
307                                + " not identified as a org-local multicast address.", addr
308                                .isMCOrgLocal());
309	}
310
311    // comparator for Inet4Address objects
312    private static final SerializableAssert COMPARATOR = new SerializableAssert() {
313        public void assertDeserialized(Serializable initial,
314                Serializable deserialized) {
315
316            Inet4Address initAddr = (Inet4Address) initial;
317            Inet4Address desrAddr = (Inet4Address) deserialized;
318
319            byte[] iaAddresss = initAddr.getAddress();
320            byte[] deIAAddresss = desrAddr.getAddress();
321            for (int i = 0; i < iaAddresss.length; i++) {
322                assertEquals(iaAddresss[i], deIAAddresss[i]);
323            }
324            assertEquals(4, deIAAddresss.length);
325            assertEquals(initAddr.getHostName(), desrAddr.getHostName());
326        }
327    };
328
329    /**
330     * @tests serialization/deserialization compatibility.
331     */
332    public void testSerializationSelf() throws Exception {
333
334        SerializationTest.verifySelf(Inet4Address.getByName("localhost"),
335                COMPARATOR);
336    }
337
338    /**
339     * @tests serialization/deserialization compatibility with RI.
340     */
341    public void testSerializationCompatibility() throws Exception {
342
343        SerializationTest.verifyGolden(this, Inet4Address
344                .getByName("localhost"), COMPARATOR);
345    }
346}
347