001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.net;
019:
020: import java.io.Serializable;
021: import java.net.Inet4Address;
022: import java.net.InetAddress;
023:
024: import org.apache.harmony.testframework.serialization.SerializationTest;
025: import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
026:
027: public class Inet4AddressTest extends junit.framework.TestCase {
028:
029: /**
030: * @tests java.net.Inet4Address#isMulticastAddress()
031: */
032: public void test_isMulticastAddress() throws Exception {
033:
034: // Create 2 IP v4 addresses and call "isMulticastAddress()"
035: // result should return true if the first 4 bits of the
036: // address are: 1110, false otherwise
037: // Make 1 address with 1110, and 1 without
038: String addrName = "";
039: addrName = "224.0.0.0"; // a multicast addr 1110 = 224-239
040: InetAddress addr = InetAddress.getByName(addrName);
041: assertTrue("Multicast address " + addrName + " not detected.",
042: addr.isMulticastAddress());
043:
044: addrName = "239.255.255.255"; // a multicast addr 1110 = 224-239
045: addr = InetAddress.getByName(addrName);
046: assertTrue("Multicast address " + addrName + " not detected.",
047: addr.isMulticastAddress());
048:
049: addrName = "42.42.42.42"; // a non-multicast address
050: addr = InetAddress.getByName(addrName);
051: assertTrue("Non multicast address " + addrName
052: + " reporting as a multicast address.", !addr
053: .isMulticastAddress());
054:
055: }
056:
057: /**
058: * @tests java.net.Inet4Address#isAnyLocalAddress()
059: */
060: public void test_isAnyLocalAddress() throws Exception {
061: String addrName = "";
062: addrName = "0.0.0.0";
063: InetAddress addr = InetAddress.getByName(addrName);
064: assertTrue("ANY address " + addrName + " not detected.", addr
065: .isAnyLocalAddress());
066: }
067:
068: /**
069: * @tests java.net.Inet4Address#isLoopbackAddress()
070: */
071: public void test_isLoopbackAddress() throws Exception {
072: // Create some IP V4 addresses and test if they are local...
073:
074: String addrName = "";
075:
076: addrName = "127.0.0.0"; // a loopback address should be 127.d.d.d
077: InetAddress addr = InetAddress.getByName(addrName);
078: assertTrue("Loopback address " + addrName + " not detected.",
079: addr.isLoopbackAddress());
080:
081: addrName = "127.42.42.42"; // a loopback address should be
082: // 127.d.d.d
083: addr = InetAddress.getByName(addrName);
084: assertTrue("Loopback address " + addrName + " not detected.",
085: addr.isLoopbackAddress());
086:
087: addrName = "42.42.42.42"; // a loopback address should be
088: // 127.d.d.d
089: addr = InetAddress.getByName(addrName);
090: assertTrue("Address incorrectly " + addrName
091: + " detected as a loopback address.", !addr
092: .isLoopbackAddress());
093: }
094:
095: /**
096: * @tests java.net.Inet4Address#isLinkLocalAddress()
097: */
098: public void test_isLinkLocalAddress() throws Exception {
099:
100: String addrName = "";
101: // There are no link local addresses for IPv4
102: // We'll test one to ensure we get "false"
103:
104: addrName = "42.42.42.42";
105: InetAddress addr = InetAddress.getByName(addrName);
106: assertTrue("IPv4 address " + addrName
107: + " incorrectly reporting as a link local address.",
108: !addr.isLinkLocalAddress());
109: }
110:
111: /**
112: * @tests java.net.Inet4Address#isSiteLocalAddress()
113: */
114: public void test_isSiteLocalAddress() throws Exception {
115: String addrName = "";
116: // There are no site local addresses for IPv4
117: // We'll test one to ensure we get "false"
118:
119: addrName = "42.42.42.42";
120: InetAddress addr = InetAddress.getByName(addrName);
121: assertTrue("IPv4 address " + addrName
122: + " incorrectly reporting as a site local address.",
123: !addr.isSiteLocalAddress());
124: }
125:
126: /**
127: * @tests java.net.Inet4Address#isMCGlobal()
128: */
129: public void test_isMCGlobal() throws Exception {
130:
131: // Create an IPv4 mulitcast address. It should return
132: // false for globabl mutlicast. There are no valid IPv4
133: // global multicast addresses
134:
135: String addrName = "";
136: addrName = "224.0.0.0"; // a multicast addr 1110
137: InetAddress addr = InetAddress.getByName(addrName);
138: assertTrue(
139: "IPv4 link-local multicast address "
140: + addrName
141: + " incorrectly identified as a global multicast address.",
142: !addr.isMCGlobal());
143:
144: addrName = "224.0.0.255"; // a multicast addr 1110
145: addr = InetAddress.getByName(addrName);
146: assertTrue(
147: "IPv4 link-local multicast address "
148: + addrName
149: + " incorrectly identified as a global multicast address.",
150: !addr.isMCGlobal());
151:
152: addrName = "224.0.1.0"; // a multicast addr 1110
153: addr = InetAddress.getByName(addrName);
154: assertTrue("IPv4 global multicast address " + addrName
155: + " not identified as a global multicast address.",
156: addr.isMCGlobal());
157:
158: addrName = "238.255.255.255"; // a multicast addr 1110
159: addr = InetAddress.getByName(addrName);
160: assertTrue("IPv4 global multicast address " + addrName
161: + " not identified as a global multicast address.",
162: addr.isMCGlobal());
163:
164: addrName = "239.0.0.0"; // a multicast addr 1110
165: addr = InetAddress.getByName(addrName);
166: assertTrue(
167: "IPv4 reserved multicast address "
168: + addrName
169: + " incorrectly identified as a global multicast address.",
170: !addr.isMCGlobal());
171:
172: addrName = "239.191.255.255"; // a multicast addr 1110
173: addr = InetAddress.getByName(addrName);
174: assertTrue(
175: "IPv4 reserved multicast address "
176: + addrName
177: + " incorrectly identified as a global multicast address.",
178: !addr.isMCGlobal());
179: }
180:
181: /**
182: * @tests java.net.Inet4Address#isMCNodeLocal()
183: */
184: public void test_isMCNodeLocal() throws Exception {
185: // Create an IPv4 mulitcast address. It should return
186: // false for node-local mutlicast. There are no valid IPv4
187: // node-local multicast addresses
188:
189: String addrName = "";
190: addrName = "224.42.42.42"; // a multicast addr 1110 = 224
191: InetAddress addr = InetAddress.getByName(addrName);
192: assertTrue(
193: "IPv4 multicast address "
194: + addrName
195: + " incorrectly identified as a node-local multicast address.",
196: !addr.isMCNodeLocal());
197:
198: addrName = "239.0.0.0"; // a multicast addr 1110
199: addr = InetAddress.getByName(addrName);
200: assertTrue(
201: "IPv4 reserved multicast address "
202: + addrName
203: + " incorrectly identified as a node-local multicast address.",
204: !addr.isMCNodeLocal());
205: }
206:
207: /**
208: * @tests java.net.Inet4Address#isMCLinkLocal()
209: */
210: public void test_isMCLinkLocal() throws Exception {
211: // Create an IPv4 mulitcast address. It should return
212: // false for link-local mutlicast. There are no valid IPv4
213: // link-local multicast addresses
214:
215: String addrName = "";
216: addrName = "224.0.0.0"; // a multicast addr 1110
217: InetAddress addr = InetAddress.getByName(addrName);
218: assertTrue("IPv4 link-local multicast address " + addrName
219: + " not identified as a link-local multicast address.",
220: addr.isMCLinkLocal());
221:
222: addrName = "224.0.0.255"; // a multicast addr 1110
223: addr = InetAddress.getByName(addrName);
224: assertTrue("IPv4 link-local multicast address " + addrName
225: + " not identified as a link-local multicast address.",
226: addr.isMCLinkLocal());
227:
228: addrName = "224.0.1.0"; // a multicast addr 1110
229: addr = InetAddress.getByName(addrName);
230: assertTrue(
231: "IPv4 global multicast address "
232: + addrName
233: + " incorrectly identified as a link-local multicast address.",
234: !addr.isMCLinkLocal());
235:
236: addrName = "239.0.0.0"; // a multicast addr 1110
237: addr = InetAddress.getByName(addrName);
238: assertTrue(
239: "IPv4 reserved multicast address "
240: + addrName
241: + " incorrectly identified as a link-local multicast address.",
242: !addr.isMCLinkLocal());
243: }
244:
245: /**
246: * @tests java.net.Inet4Address#isMCSiteLocal()
247: */
248: public void test_isMCSiteLocal() throws Exception {
249: // Create an IPv4 mulitcast address. It should return
250: // false for site-local mutlicast. There are no valid IPv4
251: // site-local multicast addresses
252:
253: String addrName = "";
254: addrName = "240.0.0.0"; // a multicast addr 1110 = 224
255: InetAddress addr = InetAddress.getByName(addrName);
256: assertTrue(
257: "IPv4 multicast address "
258: + addrName
259: + " incorrectly identified as a site-local multicast address.",
260: !addr.isMCSiteLocal());
261:
262: addrName = "239.0.0.0"; // a multicast addr 1110
263: addr = InetAddress.getByName(addrName);
264: assertTrue(
265: "IPv4 reserved multicast address "
266: + addrName
267: + " incorrectly identified as a site-local multicast address.",
268: !addr.isMCSiteLocal());
269:
270: addrName = "239.255.0.0"; // a multicast addr 1110
271: addr = InetAddress.getByName(addrName);
272: assertTrue("IPv4 site-local multicast address " + addrName
273: + " not identified as a site-local multicast address.",
274: addr.isMCSiteLocal());
275:
276: addrName = "239.255.255.255"; // a multicast addr 1110
277: addr = InetAddress.getByName(addrName);
278: assertTrue("IPv4 site-local multicast address " + addrName
279: + " not identified as a site-local multicast address.",
280: addr.isMCSiteLocal());
281:
282: addrName = "239.255.2.2"; // a multicast addr 1110
283: addr = InetAddress.getByName(addrName);
284: assertTrue("IPv4 site-local multicast address " + addrName
285: + " not identified as a site-local multicast address.",
286: addr.isMCSiteLocal());
287: }
288:
289: /**
290: * @tests java.net.Inet4Address#isMCOrgLocal()
291: */
292: public void test_isMCOrgLocal() throws Exception {
293: // Create an IPv4 mulitcast address. It should return
294: // false for organization-local mutlicast. There are no valid IPv4
295: // organization-local multicast addresses
296:
297: String addrName = "";
298:
299: addrName = "239.191.255.255"; // a multicast addr 1110
300: InetAddress addr = InetAddress.getByName(addrName);
301: assertTrue(
302: "IPv4 reserved multicast address "
303: + addrName
304: + " incorrectly identified as a org-local multicast address.",
305: !addr.isMCOrgLocal());
306:
307: addrName = "239.252.0.0"; // a multicast addr 1110
308: addr = InetAddress.getByName(addrName);
309: assertTrue(
310: "IPv4 site-local multicast address "
311: + addrName
312: + " incorrectly identified as a org-local multicast address.",
313: !addr.isMCOrgLocal());
314:
315: addrName = "239.192.0.0"; // a multicast addr 1110
316: addr = InetAddress.getByName(addrName);
317: assertTrue("IPv4 org-local multicast address " + addrName
318: + " not identified as a org-local multicast address.",
319: addr.isMCOrgLocal());
320:
321: addrName = "239.195.255.255"; // a multicast addr 1110
322: addr = InetAddress.getByName(addrName);
323: assertTrue("IPv4 org-local multicast address " + addrName
324: + " not identified as a org-local multicast address.",
325: addr.isMCOrgLocal());
326: }
327:
328: // comparator for Inet4Address objects
329: private static final SerializableAssert COMPARATOR = new SerializableAssert() {
330: public void assertDeserialized(Serializable initial,
331: Serializable deserialized) {
332:
333: Inet4Address initAddr = (Inet4Address) initial;
334: Inet4Address desrAddr = (Inet4Address) deserialized;
335:
336: byte[] iaAddresss = initAddr.getAddress();
337: byte[] deIAAddresss = desrAddr.getAddress();
338: for (int i = 0; i < iaAddresss.length; i++) {
339: assertEquals(iaAddresss[i], deIAAddresss[i]);
340: }
341: assertEquals(4, deIAAddresss.length);
342: assertEquals(initAddr.getHostName(), desrAddr.getHostName());
343: }
344: };
345:
346: /**
347: * @tests serialization/deserialization compatibility.
348: */
349: public void testSerializationSelf() throws Exception {
350:
351: SerializationTest.verifySelf(Inet4Address
352: .getByName("localhost"), COMPARATOR);
353: }
354:
355: /**
356: * @tests serialization/deserialization compatibility with RI.
357: */
358: public void testSerializationCompatibility() throws Exception {
359:
360: SerializationTest.verifyGolden(this , Inet4Address
361: .getByName("localhost"), COMPARATOR);
362: }
363: }
|