Inet4AddressTest.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
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 /** 58 * @tests java.net.Inet4Address#isAnyLocalAddress() 59 */ 60 public void test_isAnyLocalAddress() throws Exception { 61 String addrName = ""; 62 addrName = "0.0.0.0"; 63 InetAddress addr = InetAddress.getByName(addrName); 64 assertTrue("ANY address " + addrName + " not detected.", addr 65 .isAnyLocalAddress()); 66 } 67 68 /** 69 * @tests java.net.Inet4Address#isLoopbackAddress() 70 */ 71 public void test_isLoopbackAddress() throws Exception { 72 // Create some IP V4 addresses and test if they are local... 73 74 String addrName = ""; 75 76 addrName = "127.0.0.0"; // a loopback address should be 127.d.d.d 77 InetAddress addr = InetAddress.getByName(addrName); 78 assertTrue("Loopback address " + addrName + " not detected.", addr 79 .isLoopbackAddress()); 80 81 addrName = "127.42.42.42"; // a loopback address should be 82 // 127.d.d.d 83 addr = InetAddress.getByName(addrName); 84 assertTrue("Loopback address " + addrName + " not detected.", addr 85 .isLoopbackAddress()); 86 87 addrName = "42.42.42.42"; // a loopback address should be 88 // 127.d.d.d 89 addr = InetAddress.getByName(addrName); 90 assertTrue("Address incorrectly " + addrName 91 + " detected as a loopback address.", !addr 92 .isLoopbackAddress()); 93 } 94 95 /** 96 * @tests java.net.Inet4Address#isLinkLocalAddress() 97 */ 98 public void test_isLinkLocalAddress() throws Exception { 99 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.", !addr 108 .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.", !addr 123 .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("IPv4 link-local multicast address " + addrName 139 + " incorrectly identified as a global multicast address.", 140 !addr.isMCGlobal()); 141 142 addrName = "224.0.0.255"; // a multicast addr 1110 143 addr = InetAddress.getByName(addrName); 144 assertTrue("IPv4 link-local multicast address " + addrName 145 + " incorrectly identified as a global multicast address.", 146 !addr.isMCGlobal()); 147 148 addrName = "224.0.1.0"; // a multicast addr 1110 149 addr = InetAddress.getByName(addrName); 150 assertTrue("IPv4 global multicast address " + addrName 151 + " not identified as a global multicast address.", addr 152 .isMCGlobal()); 153 154 addrName = "238.255.255.255"; // a multicast addr 1110 155 addr = InetAddress.getByName(addrName); 156 assertTrue("IPv4 global multicast address " + addrName 157 + " not identified as a global multicast address.", addr 158 .isMCGlobal()); 159 160 addrName = "239.0.0.0"; // a multicast addr 1110 161 addr = InetAddress.getByName(addrName); 162 assertTrue("IPv4 reserved multicast address " + addrName 163 + " incorrectly identified as a global multicast address.", 164 !addr.isMCGlobal()); 165 166 addrName = "239.191.255.255"; // a multicast addr 1110 167 addr = InetAddress.getByName(addrName); 168 assertTrue("IPv4 reserved multicast address " + addrName 169 + " incorrectly identified as a global multicast address.", 170 !addr.isMCGlobal()); 171 } 172 173 /** 174 * @tests java.net.Inet4Address#isMCNodeLocal() 175 */ 176 public void test_isMCNodeLocal() throws Exception { 177 // Create an IPv4 mulitcast address. It should return 178 // false for node-local mutlicast. There are no valid IPv4 179 // node-local multicast addresses 180 181 String addrName = ""; 182 addrName = "224.42.42.42"; // a multicast addr 1110 = 224 183 InetAddress addr = InetAddress.getByName(addrName); 184 assertTrue( 185 "IPv4 multicast address " 186 + addrName 187 + " incorrectly identified as a node-local multicast address.", 188 !addr.isMCNodeLocal()); 189 190 addrName = "239.0.0.0"; // a multicast addr 1110 191 addr = InetAddress.getByName(addrName); 192 assertTrue( 193 "IPv4 reserved multicast address " 194 + addrName 195 + " incorrectly identified as a node-local multicast address.", 196 !addr.isMCNodeLocal()); 197 } 198 199 /** 200 * @tests java.net.Inet4Address#isMCLinkLocal() 201 */ 202 public void test_isMCLinkLocal() throws Exception { 203 // Create an IPv4 mulitcast address. It should return 204 // false for link-local mutlicast. There are no valid IPv4 205 // link-local multicast addresses 206 207 String addrName = ""; 208 addrName = "224.0.0.0"; // a multicast addr 1110 209 InetAddress addr = InetAddress.getByName(addrName); 210 assertTrue("IPv4 link-local multicast address " + addrName 211 + " not identified as a link-local multicast address.", 212 addr.isMCLinkLocal()); 213 214 addrName = "224.0.0.255"; // a multicast addr 1110 215 addr = InetAddress.getByName(addrName); 216 assertTrue("IPv4 link-local multicast address " + addrName 217 + " not identified as a link-local multicast address.", 218 addr.isMCLinkLocal()); 219 220 addrName = "224.0.1.0"; // a multicast addr 1110 221 addr = InetAddress.getByName(addrName); 222 assertTrue( 223 "IPv4 global multicast address " 224 + addrName 225 + " incorrectly identified as a link-local multicast address.", 226 !addr.isMCLinkLocal()); 227 228 addrName = "239.0.0.0"; // a multicast addr 1110 229 addr = InetAddress.getByName(addrName); 230 assertTrue( 231 "IPv4 reserved multicast address " 232 + addrName 233 + " incorrectly identified as a link-local multicast address.", 234 !addr.isMCLinkLocal()); 235 } 236 237 /** 238 * @tests java.net.Inet4Address#isMCSiteLocal() 239 */ 240 public void test_isMCSiteLocal() throws Exception { 241 // Create an IPv4 mulitcast address. It should return 242 // false for site-local mutlicast. There are no valid IPv4 243 // site-local multicast addresses 244 245 String addrName = ""; 246 addrName = "240.0.0.0"; // a multicast addr 1110 = 224 247 InetAddress addr = InetAddress.getByName(addrName); 248 assertTrue( 249 "IPv4 multicast address " 250 + addrName 251 + " incorrectly identified as a site-local multicast address.", 252 !addr.isMCSiteLocal()); 253 254 addrName = "239.0.0.0"; // a multicast addr 1110 255 addr = InetAddress.getByName(addrName); 256 assertTrue( 257 "IPv4 reserved multicast address " 258 + addrName 259 + " incorrectly identified as a site-local multicast address.", 260 !addr.isMCSiteLocal()); 261 262 addrName = "239.255.0.0"; // a multicast addr 1110 263 addr = InetAddress.getByName(addrName); 264 assertTrue("IPv4 site-local multicast address " + addrName 265 + " not identified as a site-local multicast address.", 266 addr.isMCSiteLocal()); 267 268 addrName = "239.255.255.255"; // a multicast addr 1110 269 addr = InetAddress.getByName(addrName); 270 assertTrue("IPv4 site-local multicast address " + addrName 271 + " not identified as a site-local multicast address.", 272 addr.isMCSiteLocal()); 273 274 addrName = "239.255.2.2"; // a multicast addr 1110 275 addr = InetAddress.getByName(addrName); 276 assertTrue("IPv4 site-local multicast address " + addrName 277 + " not identified as a site-local multicast address.", 278 addr.isMCSiteLocal()); 279 } 280 281 /** 282 * @tests java.net.Inet4Address#isMCOrgLocal() 283 */ 284 public void test_isMCOrgLocal() throws Exception { 285 // Create an IPv4 mulitcast address. It should return 286 // false for organization-local mutlicast. There are no valid IPv4 287 // organization-local multicast addresses 288 289 String addrName = ""; 290 291 addrName = "239.191.255.255"; // a multicast addr 1110 292 InetAddress addr = InetAddress.getByName(addrName); 293 assertTrue( 294 "IPv4 reserved multicast address " 295 + addrName 296 + " incorrectly identified as a org-local multicast address.", 297 !addr.isMCOrgLocal()); 298 299 addrName = "239.252.0.0"; // a multicast addr 1110 300 addr = InetAddress.getByName(addrName); 301 assertTrue( 302 "IPv4 site-local multicast address " 303 + addrName 304 + " incorrectly identified as a org-local multicast address.", 305 !addr.isMCOrgLocal()); 306 307 addrName = "239.192.0.0"; // a multicast addr 1110 308 addr = InetAddress.getByName(addrName); 309 assertTrue("IPv4 org-local multicast address " + addrName 310 + " not identified as a org-local multicast address.", addr 311 .isMCOrgLocal()); 312 313 addrName = "239.195.255.255"; // a multicast addr 1110 314 addr = InetAddress.getByName(addrName); 315 assertTrue("IPv4 org-local multicast address " + addrName 316 + " not identified as a org-local multicast address.", addr 317 .isMCOrgLocal()); 318 } 319 320 // comparator for Inet4Address objects 321 private static final SerializableAssert COMPARATOR = new SerializableAssert() { 322 public void assertDeserialized(Serializable initial, 323 Serializable deserialized) { 324 325 Inet4Address initAddr = (Inet4Address) initial; 326 Inet4Address desrAddr = (Inet4Address) deserialized; 327 328 byte[] iaAddresss = initAddr.getAddress(); 329 byte[] deIAAddresss = desrAddr.getAddress(); 330 for (int i = 0; i < iaAddresss.length; i++) { 331 assertEquals(iaAddresss[i], deIAAddresss[i]); 332 } 333 assertEquals(4, deIAAddresss.length); 334 assertEquals(initAddr.getHostName(), desrAddr.getHostName()); 335 } 336 }; 337 338 /** 339 * @tests serialization/deserialization compatibility. 340 */ 341 public void testSerializationSelf() throws Exception { 342 343 SerializationTest.verifySelf(Inet4Address.getByName("localhost"), 344 COMPARATOR); 345 } 346 347 /** 348 * @tests serialization/deserialization compatibility with RI. 349 */ 350 public void testSerializationCompatibility() throws Exception { 351 352 SerializationTest.verifyGolden(this, Inet4Address 353 .getByName("localhost"), COMPARATOR); 354 } 355} 356