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.TestTargets; 22import dalvik.annotation.TestLevel; 23import dalvik.annotation.TestTargetNew; 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 @TestTargetNew( 39 level = TestLevel.COMPLETE, 40 notes = "", 41 method = "isMulticastAddress", 42 args = {} 43 ) 44 public void test_isMulticastAddress() { 45 46 // Create 2 IP v4 addresses and call "isMulticastAddress()" 47 // result should return true if the first 4 bits of the 48 // address are: 1110, false otherwise 49 // Make 1 address with 1110, and 1 without 50 String addrName = ""; 51 try { 52 addrName = "224.0.0.0"; // a multicast addr 1110 = 224-239 53 InetAddress addr = Inet4Address.getByName(addrName); 54 assertTrue("Multicast address " + addrName + " not detected.", addr 55 .isMulticastAddress()); 56 57 addrName = "239.255.255.255"; // a multicast addr 1110 = 224-239 58 addr = Inet4Address.getByName(addrName); 59 assertTrue("Multicast address " + addrName + " not detected.", addr 60 .isMulticastAddress()); 61 62 addrName = "42.42.42.42"; // a non-multicast address 63 addr = Inet4Address.getByName(addrName); 64 assertTrue("Non multicast address " + addrName 65 + " reporting as a multicast address.", !addr 66 .isMulticastAddress()); 67 } catch (Exception e) { 68 fail("Unknown address : " + addrName); 69 } 70 71 } 72 73 /** 74 * @tests java.net.Inet4Address#isAnyLocalAddress() 75 */ 76 @TestTargetNew( 77 level = TestLevel.COMPLETE, 78 notes = "", 79 method = "isAnyLocalAddress", 80 args = {} 81 ) 82 public void test_isAnyLocalAddress() { 83 String addrName = ""; 84 try { 85 addrName = "0.0.0.0"; 86 InetAddress addr = Inet4Address.getByName(addrName); 87 assertTrue("ANY address " + addrName + " not detected.", addr 88 .isAnyLocalAddress()); 89 } catch (Exception e) { 90 fail("Unknown address : " + addrName); 91 } 92 } 93 94 /** 95 * @tests java.net.Inet4Address#isLoopbackAddress() 96 */ 97 @TestTargetNew( 98 level = TestLevel.COMPLETE, 99 notes = "", 100 method = "isLoopbackAddress", 101 args = {} 102 ) 103 public void test_isLoopbackAddress() { 104 // Create some IP V4 addresses and test if they are local... 105 106 String addrName = ""; 107 try { 108 addrName = "127.0.0.0"; // a loopback address should be 127.d.d.d 109 InetAddress addr = Inet4Address.getByName(addrName); 110 assertTrue("Loopback address " + addrName + " not detected.", addr 111 .isLoopbackAddress()); 112 113 addrName = "127.42.42.42"; // a loopback address should be 114 // 127.d.d.d 115 addr = Inet4Address.getByName(addrName); 116 assertTrue("Loopback address " + addrName + " not detected.", addr 117 .isLoopbackAddress()); 118 119 addrName = "42.42.42.42"; // a loopback address should be 120 // 127.d.d.d 121 addr = Inet4Address.getByName(addrName); 122 assertTrue("Address incorrectly " + addrName 123 + " detected as a loopback address.", !addr 124 .isLoopbackAddress()); 125 126 } catch (Exception e) { 127 fail("Unknown address : " + addrName); 128 } 129 130 } 131 132 /** 133 * @tests java.net.Inet4Address#isLinkLocalAddress() 134 */ 135 @TestTargetNew( 136 level = TestLevel.COMPLETE, 137 notes = "", 138 method = "isLinkLocalAddress", 139 args = {} 140 ) 141 public void test_isLinkLocalAddress() { 142 143 String addrName = ""; 144 try { 145 // There are no link local addresses for IPv4 146 // We'll test one to ensure we get "false" 147 148 addrName = "42.42.42.42"; 149 InetAddress addr = Inet4Address.getByName(addrName); 150 assertTrue("IPv4 address " + addrName 151 + " incorrectly reporting as a link local address.", !addr 152 .isLinkLocalAddress()); 153 } catch (Exception e) { 154 fail("Unknown address : " + e.getMessage()); 155 } 156 } 157 158 /** 159 * @tests java.net.Inet4Address#isSiteLocalAddress() 160 */ 161 @TestTargetNew( 162 level = TestLevel.COMPLETE, 163 notes = "", 164 method = "isSiteLocalAddress", 165 args = {} 166 ) 167 public void test_isSiteLocalAddress() { 168 String addrName = ""; 169 try { 170 // There are no site local addresses for IPv4 171 // We'll test one to ensure we get "false" 172 173 addrName = "42.42.42.42"; 174 InetAddress addr = Inet4Address.getByName(addrName); 175 assertTrue("IPv4 address " + addrName 176 + " incorrectly reporting as a site local address.", !addr 177 .isSiteLocalAddress()); 178 } catch (Exception e) { 179 fail("Unknown address : " + e.getMessage()); 180 } 181 } 182 183 /** 184 * @tests java.net.Inet4Address#isMCGlobal() 185 */ 186 @TestTargetNew( 187 level = TestLevel.COMPLETE, 188 notes = "", 189 method = "isMCGlobal", 190 args = {} 191 ) 192 public void test_isMCGlobal() { 193 194 // Create an IPv4 mulitcast address. It should return 195 // false for globabl mutlicast. There are no valid IPv4 196 // global multicast addresses 197 198 String addrName = ""; 199 try { 200 addrName = "224.0.0.0"; // a multicast addr 1110 201 InetAddress addr = Inet4Address.getByName(addrName); 202 assertTrue("IPv4 link-local multicast address " + addrName 203 + " incorrectly identified as a global multicast address.", 204 !addr.isMCGlobal()); 205 206 addrName = "224.0.0.255"; // a multicast addr 1110 207 addr = Inet4Address.getByName(addrName); 208 assertTrue("IPv4 link-local multicast address " + addrName 209 + " incorrectly identified as a global multicast address.", 210 !addr.isMCGlobal()); 211 212 addrName = "224.0.1.0"; // a multicast addr 1110 213 addr = Inet4Address.getByName(addrName); 214 assertTrue("IPv4 global multicast address " + addrName 215 + " not identified as a global multicast address.", addr 216 .isMCGlobal()); 217 218 addrName = "238.255.255.255"; // a multicast addr 1110 219 addr = Inet4Address.getByName(addrName); 220 assertTrue("IPv4 global multicast address " + addrName 221 + " not identified as a global multicast address.", addr 222 .isMCGlobal()); 223 224 addrName = "239.0.0.0"; // a multicast addr 1110 225 addr = Inet4Address.getByName(addrName); 226 assertTrue("IPv4 reserved multicast address " + addrName 227 + " incorrectly identified as a global multicast address.", 228 !addr.isMCGlobal()); 229 230 addrName = "239.191.255.255"; // a multicast addr 1110 231 addr = Inet4Address.getByName(addrName); 232 assertTrue("IPv4 reserved multicast address " + addrName 233 + " incorrectly identified as a global multicast address.", 234 !addr.isMCGlobal()); 235 236 } catch (Exception e) { 237 fail("Unknown address : " + e.getMessage()); 238 } 239 } 240 241 /** 242 * @tests java.net.Inet4Address#isMCNodeLocal() 243 */ 244 @TestTargetNew( 245 level = TestLevel.COMPLETE, 246 notes = "", 247 method = "isMCNodeLocal", 248 args = {} 249 ) 250 public void test_isMCNodeLocal() { 251 // Create an IPv4 mulitcast address. It should return 252 // false for node-local mutlicast. There are no valid IPv4 253 // node-local multicast addresses 254 255 String addrName = ""; 256 try { 257 addrName = "224.42.42.42"; // a multicast addr 1110 = 224 258 InetAddress addr = Inet4Address.getByName(addrName); 259 assertTrue( 260 "IPv4 multicast address " 261 + addrName 262 + " incorrectly identified as a node-local multicast address.", 263 !addr.isMCNodeLocal()); 264 265 addrName = "239.0.0.0"; // a multicast addr 1110 266 addr = Inet4Address.getByName(addrName); 267 assertTrue( 268 "IPv4 reserved multicast address " 269 + addrName 270 + " incorrectly identified as a node-local multicast address.", 271 !addr.isMCNodeLocal()); 272 273 } catch (Exception e) { 274 fail("Unknown address : " + e.getMessage()); 275 } 276 } 277 278 /** 279 * @tests java.net.Inet4Address#isMCLinkLocal() 280 */ 281 @TestTargetNew( 282 level = TestLevel.COMPLETE, 283 notes = "", 284 method = "isMCLinkLocal", 285 args = {} 286 ) 287 public void test_isMCLinkLocal() { 288 // Create an IPv4 mulitcast address. It should return 289 // false for link-local mutlicast. There are no valid IPv4 290 // link-local multicast addresses 291 292 String addrName = ""; 293 try { 294 addrName = "224.0.0.0"; // a multicast addr 1110 295 InetAddress addr = Inet4Address.getByName(addrName); 296 assertTrue("IPv4 link-local multicast address " + addrName 297 + " not identified as a link-local multicast address.", 298 addr.isMCLinkLocal()); 299 300 addrName = "224.0.0.255"; // a multicast addr 1110 301 addr = Inet4Address.getByName(addrName); 302 assertTrue("IPv4 link-local multicast address " + addrName 303 + " not identified as a link-local multicast address.", 304 addr.isMCLinkLocal()); 305 306 addrName = "224.0.1.0"; // a multicast addr 1110 307 addr = Inet4Address.getByName(addrName); 308 assertTrue( 309 "IPv4 global multicast address " 310 + addrName 311 + " incorrectly identified as a link-local multicast address.", 312 !addr.isMCLinkLocal()); 313 314 addrName = "239.0.0.0"; // a multicast addr 1110 315 addr = Inet4Address.getByName(addrName); 316 assertTrue( 317 "IPv4 reserved multicast address " 318 + addrName 319 + " incorrectly identified as a link-local multicast address.", 320 !addr.isMCLinkLocal()); 321 322 } catch (Exception e) { 323 fail("Unknown address : " + addrName); 324 } 325 } 326 327 /** 328 * @tests java.net.Inet4Address#isMCSiteLocal() 329 */ 330 @TestTargetNew( 331 level = TestLevel.COMPLETE, 332 notes = "", 333 method = "isMCSiteLocal", 334 args = {} 335 ) 336 public void test_isMCSiteLocal() { 337 // Create an IPv4 mulitcast address. It should return 338 // false for site-local mutlicast. There are no valid IPv4 339 // site-local multicast addresses 340 341 String addrName = ""; 342 try { 343 addrName = "240.0.0.0"; // a multicast addr 1110 = 224 344 InetAddress addr = Inet4Address.getByName(addrName); 345 assertTrue( 346 "IPv4 multicast address " 347 + addrName 348 + " incorrectly identified as a site-local multicast address.", 349 !addr.isMCSiteLocal()); 350 351 addrName = "239.0.0.0"; // a multicast addr 1110 352 addr = Inet4Address.getByName(addrName); 353 assertTrue( 354 "IPv4 reserved multicast address " 355 + addrName 356 + " incorrectly identified as a site-local multicast address.", 357 !addr.isMCSiteLocal()); 358 359 addrName = "239.255.0.0"; // a multicast addr 1110 360 addr = Inet4Address.getByName(addrName); 361 assertTrue("IPv4 site-local multicast address " + addrName 362 + " not identified as a site-local multicast address.", 363 addr.isMCSiteLocal()); 364 365 addrName = "239.255.255.255"; // a multicast addr 1110 366 addr = Inet4Address.getByName(addrName); 367 assertTrue("IPv4 site-local multicast address " + addrName 368 + " not identified as a site-local multicast address.", 369 addr.isMCSiteLocal()); 370 371 addrName = "239.255.2.2"; // a multicast addr 1110 372 addr = Inet4Address.getByName(addrName); 373 assertTrue("IPv4 site-local multicast address " + addrName 374 + " not identified as a site-local multicast address.", 375 addr.isMCSiteLocal()); 376 377 } catch (Exception e) { 378 fail("Unknown address : " + addrName); 379 } 380 } 381 382 /** 383 * @tests java.net.Inet4Address#isMCOrgLocal() 384 */ 385 @TestTargetNew( 386 level = TestLevel.COMPLETE, 387 notes = "", 388 method = "isMCOrgLocal", 389 args = {} 390 ) 391 public void test_isMCOrgLocal() { 392 // Create an IPv4 mulitcast address. It should return 393 // false for organization-local mutlicast. There are no valid IPv4 394 // organization-local multicast addresses 395 396 String addrName = ""; 397 try { 398 399 addrName = "239.191.255.255"; // a multicast addr 1110 400 InetAddress addr = Inet4Address.getByName(addrName); 401 assertTrue( 402 "IPv4 reserved multicast address " 403 + addrName 404 + " incorrectly identified as a org-local multicast address.", 405 !addr.isMCOrgLocal()); 406 407 addrName = "239.252.0.0"; // a multicast addr 1110 408 addr = Inet4Address.getByName(addrName); 409 assertTrue( 410 "IPv4 site-local multicast address " 411 + addrName 412 + " incorrectly identified as a org-local multicast address.", 413 !addr.isMCOrgLocal()); 414 415 addrName = "239.192.0.0"; // a multicast addr 1110 416 addr = Inet4Address.getByName(addrName); 417 assertTrue("IPv4 org-local multicast address " + addrName 418 + " not identified as a org-local multicast address.", addr 419 .isMCOrgLocal()); 420 421 addrName = "239.195.255.255"; // a multicast addr 1110 422 addr = Inet4Address.getByName(addrName); 423 assertTrue("IPv4 org-local multicast address " + addrName 424 + " not identified as a org-local multicast address.", addr 425 .isMCOrgLocal()); 426 427 } catch (Exception e) { 428 fail("Unknown address : " + addrName); 429 } 430 } 431 432 // comparator for Inet4Address objects 433 private static final SerializableAssert COMPARATOR = new SerializableAssert() { 434 public void assertDeserialized(Serializable initial, 435 Serializable deserialized) { 436 437 Inet4Address initAddr = (Inet4Address) initial; 438 Inet4Address desrAddr = (Inet4Address) deserialized; 439 440 byte[] iaAddresss = initAddr.getAddress(); 441 byte[] deIAAddresss = desrAddr.getAddress(); 442 for (int i = 0; i < iaAddresss.length; i++) { 443 assertEquals(iaAddresss[i], deIAAddresss[i]); 444 } 445 assertEquals(4, deIAAddresss.length); 446 assertEquals(initAddr.getHostName(), desrAddr.getHostName()); 447 } 448 }; 449 450 /** 451 * @tests serialization/deserialization compatibility. 452 */ 453 @TestTargetNew( 454 level = TestLevel.COMPLETE, 455 notes = "Checks serialization", 456 method = "!SerializationSelf", 457 args = {} 458 ) 459 public void testSerializationSelf() throws Exception { 460 461 SerializationTest.verifySelf(Inet4Address.getByName("localhost"), 462 COMPARATOR); 463 } 464 465 /** 466 * @tests serialization/deserialization compatibility with RI. 467 */ 468 @TestTargetNew( 469 level = TestLevel.COMPLETE, 470 notes = "Checks serialization", 471 method = "!SerializationGolden", 472 args = {} 473 ) 474 public void testSerializationCompatibility() throws Exception { 475 476 SerializationTest.verifyGolden(this, Inet4Address 477 .getByName("localhost"), COMPARATOR); 478 } 479 480 @TestTargetNew( 481 level = TestLevel.COMPLETE, 482 notes = "", 483 method = "equals", 484 args = {java.lang.Object.class} 485 ) 486 public void test_equals() throws Exception { 487 InetAddress addr = Inet4Address.getByName("239.191.255.255"); 488 assertTrue(addr.equals(addr)); 489 InetAddress addr1 = Inet4Address.getByName("127.0.0.1"); 490 InetAddress addr2 = Inet4Address.getByName("localhost"); 491 assertTrue(addr1.equals(addr2)); 492 assertFalse(addr.equals(addr1)); 493 494 InetAddress addr3 = Inet4Address.getByName("127.0.0"); 495 assertFalse(addr1.equals(addr3)); 496 } 497 498 @TestTargetNew( 499 level = TestLevel.COMPLETE, 500 notes = "", 501 method = "getHostAddress", 502 args = {} 503 ) 504 public void test_getHostAddress() throws Exception { 505 InetAddress addr = Inet4Address.getByName("localhost"); 506 assertEquals("127.0.0.1", addr.getHostAddress()); 507 508 addr = Inet4Address.getByName("127.0.0.1"); 509 assertEquals("127.0.0.1", addr.getHostAddress()); 510 511 addr = Inet4Address.getByName("224.0.0.0"); 512 assertEquals("224.0.0.0", addr.getHostAddress()); 513 514 addr = Inet4Address.getByName("1"); 515 assertEquals("0.0.0.1", addr.getHostAddress()); 516 517 addr = Inet4Address.getByName("1.1"); 518 assertEquals("1.0.0.1", addr.getHostAddress()); 519 520 addr = Inet4Address.getByName("1.1.1"); 521 assertEquals("1.1.0.1", addr.getHostAddress()); 522 } 523 524 @TestTargetNew( 525 level = TestLevel.COMPLETE, 526 notes = "", 527 method = "hashCode", 528 args = {} 529 ) 530 public void test_hashCode() throws Exception { 531 InetAddress addr1 = Inet4Address.getByName("1.1"); 532 InetAddress addr2 = Inet4Address.getByName("1.1.1"); 533 assertFalse(addr1.hashCode() == addr2.hashCode()); 534 535 addr2 = InetAddress.getByName("1.0.0.1"); 536 assertTrue(addr1.hashCode() == addr2.hashCode()); 537 538 addr1 = Inet4Address.getByName("127.0.0.1"); 539 addr2 = Inet4Address.getByName("localhost"); 540 assertTrue(addr1.hashCode() == addr2.hashCode()); 541 } 542} 543