/luni/src/test/java/org/apache/harmony/luni/tests/java/net/Inet6AddressTest.java

https://github.com/MIPS/libcore · Java · 1250 lines · 859 code · 181 blank · 210 comment · 13 complexity · 05a8ab10cbed8f4ab45062394ca3dd50 MD5 · raw file

  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. package org.apache.harmony.luni.tests.java.net;
  18. import dalvik.annotation.TestTargetClass;
  19. import dalvik.annotation.TestLevel;
  20. import dalvik.annotation.TestTargetNew;
  21. import java.io.Serializable;
  22. import java.net.Inet6Address;
  23. import java.net.InetAddress;
  24. import java.net.NetworkInterface;
  25. import java.net.UnknownHostException;
  26. import java.security.Permission;
  27. import org.apache.harmony.luni.tests.java.net.InetAddressTest.MockSecurityManager;
  28. import org.apache.harmony.testframework.serialization.SerializationTest;
  29. import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
  30. @TestTargetClass(Inet6Address.class)
  31. public class Inet6AddressTest extends junit.framework.TestCase {
  32. /**
  33. * @tests java.net.Inet6Address#isMulticastAddress()
  34. */
  35. @TestTargetNew(
  36. level = TestLevel.COMPLETE,
  37. notes = "",
  38. method = "isMulticastAddress",
  39. args = {}
  40. )
  41. public void test_isMulticastAddress() {
  42. String addrName = "";
  43. InetAddress addr = null;
  44. try {
  45. // IP V6 regular multicast and non-multicast tests
  46. //
  47. // Create 2 IP v6 addresses and call "isMulticastAddress()"
  48. // A prefix of "11111111" means that the address is multicast
  49. // The first one will be one with the prefix the second without
  50. addrName = "FFFF::42:42"; // 11111111 = FFFF
  51. addr = Inet6Address.getByName(addrName);
  52. assertTrue("Multicast address " + addrName + " not detected.", addr
  53. .isMulticastAddress());
  54. addrName = "42::42:42"; // an non-multicast address
  55. addr = Inet6Address.getByName(addrName);
  56. assertTrue("Non multicast address " + addrName
  57. + " reporting as a multicast address.", !addr
  58. .isMulticastAddress());
  59. // IPv4-compatible IPv6 address tests
  60. //
  61. // Now create 2 IP v6 addresses that are IP v4 compatable
  62. // to IP v6 addresses. The address prefix for a multicast ip v4
  63. // address is 1110 for the last 16 bits ::d.d.d.d
  64. // We expect these to be false
  65. addrName = "::224.42.42.42"; // an ipv4 multicast addr 1110 = 224
  66. addr = Inet6Address.getByName(addrName);
  67. assertTrue("IPv4 compatable address " + addrName
  68. + " reported incorrectly as multicast.", !addr
  69. .isMulticastAddress());
  70. addrName = "::42.42.42.42"; // an ipv4 non-multicast address
  71. addr = Inet6Address.getByName(addrName);
  72. assertTrue("IPv4 compatable address " + addrName
  73. + " reported incorrectly as multicast.", !addr
  74. .isMulticastAddress());
  75. // IPv4-mapped IPv6 address tests
  76. //
  77. // Now create 2 IP v6 addresses that are IP v4 compatable
  78. // to IP v6 addresses. The address prefix for a multicast ip v4
  79. // address is 1110 for the last 16 bits ::FFFF:d.d.d.d
  80. addrName = "::FFFF:224.42.42.42"; // an ipv4 multicast addr 1110 =
  81. // 224
  82. addr = Inet6Address.getByName(addrName);
  83. assertTrue("IPv4-mapped IPv6 multicast address " + addrName
  84. + " not detected.", addr.isMulticastAddress());
  85. addrName = "::FFFF:42.42.42.42"; // an ipv4 non-multicast address
  86. addr = Inet6Address.getByName(addrName);
  87. assertTrue("IPv4-mapped IPv6 non-multicast address " + addrName
  88. + " reporting as a multicast address.", !addr
  89. .isMulticastAddress());
  90. } catch (Exception e) {
  91. fail("Unknown address : " + addrName);
  92. }
  93. }
  94. /**
  95. * @tests java.net.Inet6Address#isAnyLocalAddress()
  96. */
  97. @TestTargetNew(
  98. level = TestLevel.COMPLETE,
  99. notes = "",
  100. method = "isAnyLocalAddress",
  101. args = {}
  102. )
  103. public void test_isAnyLocalAddress() {
  104. String addrName = "";
  105. InetAddress addr = null;
  106. try {
  107. // test to ensure that the unspecified address returns tru
  108. addrName = "::0"; // The unspecified address
  109. addr = InetAddress.getByName(addrName);
  110. assertTrue(
  111. "The unspecified (also known as wildcard and any local address) "
  112. + addrName + " not detected.", addr
  113. .isAnyLocalAddress());
  114. addrName = "::"; // another form of the unspecified address
  115. addr = InetAddress.getByName(addrName);
  116. assertTrue(
  117. "The unspecified (also known as wildcard and any local address) "
  118. + addrName + " not detected.", addr
  119. .isAnyLocalAddress());
  120. addrName = "::1"; // The loopback address
  121. addr = InetAddress.getByName(addrName);
  122. assertTrue("The addresses " + addrName
  123. + " incorrectly reporting an the unspecified address.",
  124. !addr.isAnyLocalAddress());
  125. } catch (Exception e) {
  126. fail("Unknown address : " + addrName);
  127. }
  128. }
  129. /**
  130. * @tests java.net.Inet6Address#isLoopbackAddress()
  131. */
  132. @TestTargetNew(
  133. level = TestLevel.COMPLETE,
  134. notes = "",
  135. method = "isLoopbackAddress",
  136. args = {}
  137. )
  138. public void test_isLoopbackAddress() {
  139. String addrName = "";
  140. try {
  141. // IP V6 regular address tests for loopback
  142. // The loopback address for IPv6 is ::1
  143. addrName = "::1";
  144. InetAddress addr = Inet6Address.getByName(addrName);
  145. assertTrue("IPv6 loopback address " + addrName + " not detected.",
  146. addr.isLoopbackAddress());
  147. addrName = "::2";
  148. addr = Inet6Address.getByName(addrName);
  149. assertTrue("IPv6 address incorrectly " + addrName
  150. + " detected as a loopback address.", !addr
  151. .isLoopbackAddress());
  152. // a loopback address should be 127.d.d.d
  153. addrName = "42:42::42:42";
  154. addr = Inet6Address.getByName(addrName);
  155. assertTrue("IPv6 address incorrectly " + addrName
  156. + " detected as a loopback address.", !addr
  157. .isLoopbackAddress());
  158. // IPv4-compatible IPv6 address tests
  159. //
  160. // Now create 2 IP v6 addresses that are IP v4 compatable
  161. // to IP v6 addresses. The address prefix for a multicast ip v4
  162. // address is 1110 for the last 16 bits ::d.d.d.d
  163. // We expect these to be false, as they are not IPv4 addresses
  164. // a loopback address should be 127.d.d.d
  165. addrName = "::127.0.0.0";
  166. addr = Inet6Address.getByName(addrName);
  167. assertTrue("IPv4-compatible IPv6 address " + addrName
  168. + " detected incorrectly as a loopback.", !addr
  169. .isLoopbackAddress());
  170. addrName = "::127.42.42.42"; // a loopback address should be
  171. // 127.d.d.d
  172. addr = Inet6Address.getByName(addrName);
  173. assertTrue("IPv4-compatible IPv6 address " + addrName
  174. + " detected incorrectly as a loopback.", !addr
  175. .isLoopbackAddress());
  176. // a loopback address should be 127.d.d.d
  177. addrName = "::42.42.42.42";
  178. addr = Inet6Address.getByName(addrName);
  179. assertTrue("IPv4-compatible IPv6 address " + addrName
  180. + " detected incorrectly as a loopback.", !addr
  181. .isLoopbackAddress());
  182. // IPv4-mapped IPv6 address tests
  183. //
  184. // Now create 2 IP v6 addresses that are IP v4 compatable
  185. // to IP v6 addresses. The address prefix for a multicast ip v4
  186. // address is 1110 for the last 16 bits ::FFFF:d.d.d.d
  187. // a loopback address should be 127.d.d.d
  188. addrName = "::FFFF:127.0.0.0";
  189. addr = Inet6Address.getByName(addrName);
  190. assertTrue("IPv4-compatible IPv6 loopback address " + addrName
  191. + " not detected.", addr.isLoopbackAddress());
  192. // a loopback address should be 127.d.d.d
  193. addrName = "::FFFF:127.42.42.42";
  194. addr = Inet6Address.getByName(addrName);
  195. assertTrue("IPv4-compatible IPv6 loopback address " + addrName
  196. + " not detected.", addr.isLoopbackAddress());
  197. // a loopback address should be 127.d.d.d
  198. addrName = "::FFFF:42.42.42.42";
  199. addr = Inet6Address.getByName(addrName);
  200. assertTrue("IPv4-compatible IPv6 address incorrectly " + addrName
  201. + " detected as a loopback address.", !addr
  202. .isLoopbackAddress());
  203. } catch (UnknownHostException e) {
  204. fail("Unknown address : " + addrName);
  205. }
  206. }
  207. /**
  208. * @tests java.net.Inet6Address#isLinkLocalAddress()
  209. */
  210. @TestTargetNew(
  211. level = TestLevel.COMPLETE,
  212. notes = "",
  213. method = "isLinkLocalAddress",
  214. args = {}
  215. )
  216. public void test_isLinkLocalAddress() {
  217. String addrName = "";
  218. try {
  219. // IP V6 regular address tests for link local addresses
  220. //
  221. // Link local addresses are FE80:: -
  222. // FEBF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF
  223. addrName = "FE80::0";
  224. InetAddress addr = Inet6Address.getByName(addrName);
  225. assertTrue(
  226. "IPv6 link local address " + addrName + " not detected.",
  227. addr.isLinkLocalAddress());
  228. addrName = "FEBF::FFFF:FFFF:FFFF:FFFF";
  229. addr = Inet6Address.getByName(addrName);
  230. assertTrue(
  231. "IPv6 link local address " + addrName + " not detected.",
  232. addr.isLinkLocalAddress());
  233. addrName = "FEC0::1";
  234. addr = Inet6Address.getByName(addrName);
  235. assertTrue("IPv6 address " + addrName
  236. + " detected incorrectly as a link local address.", !addr
  237. .isLinkLocalAddress());
  238. addrName = "FD80::1:FFFF:FFFF:FFFF:FFFF";
  239. addr = Inet6Address.getByName(addrName);
  240. assertTrue("IPv6 address " + addrName
  241. + " detected incorrectly as a link local address.", !addr
  242. .isLinkLocalAddress());
  243. addrName = "FE7F::FFFF:FFFF:FFFF:FFFF";
  244. addr = Inet6Address.getByName(addrName);
  245. assertTrue("IPv6 address " + addrName
  246. + " detected incorrectly as a link local address.", !addr
  247. .isLinkLocalAddress());
  248. } catch (Exception e) {
  249. fail("Unknown address : " + addrName);
  250. }
  251. }
  252. /**
  253. * @tests java.net.Inet6Address#isSiteLocalAddress()
  254. */
  255. @TestTargetNew(
  256. level = TestLevel.COMPLETE,
  257. notes = "",
  258. method = "isSiteLocalAddress",
  259. args = {}
  260. )
  261. public void test_isSiteLocalAddress() {
  262. String addrName = "";
  263. try {
  264. // IP V6 regular address tests for link local addresses
  265. //
  266. // Link local addresses are FEC0::0 through to
  267. // FEFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF
  268. addrName = "FEC0::0";
  269. InetAddress addr = Inet6Address.getByName(addrName);
  270. assertTrue(
  271. "IPv6 site local address " + addrName + " not detected.",
  272. addr.isSiteLocalAddress());
  273. addrName = "FEFF::FFFF:FFFF:FFFF:FFFF:FFFF";
  274. addr = Inet6Address.getByName(addrName);
  275. assertTrue(
  276. "IPv6 site local address " + addrName + " not detected.",
  277. addr.isSiteLocalAddress());
  278. addrName = "FEBF::FFFF:FFFF:FFFF:FFFF:FFFF";
  279. addr = Inet6Address.getByName(addrName);
  280. assertTrue("IPv6 address " + addrName
  281. + " detected incorrectly as a site local address.", !addr
  282. .isSiteLocalAddress());
  283. addrName = "FFC0::0";
  284. addr = Inet6Address.getByName(addrName);
  285. assertTrue("IPv6 address " + addrName
  286. + " detected incorrectly as a site local address.", !addr
  287. .isSiteLocalAddress());
  288. } catch (Exception e) {
  289. fail("Unknown address : " + addrName);
  290. }
  291. }
  292. /**
  293. * @tests java.net.Inet6Address#isMCGlobal()
  294. */
  295. @TestTargetNew(
  296. level = TestLevel.COMPLETE,
  297. notes = "",
  298. method = "isMCGlobal",
  299. args = {}
  300. )
  301. public void test_isMCGlobal() {
  302. String addrName = "";
  303. try {
  304. // IP V6 regular address tests for Mulitcase Global addresses
  305. //
  306. // Multicast global addresses are FFxE:/112 where x is
  307. // a set of flags, and the addition 112 bits make up
  308. // the global address space
  309. addrName = "FF0E::0";
  310. InetAddress addr = Inet6Address.getByName(addrName);
  311. assertTrue("IPv6 global mutlicast address " + addrName
  312. + " not detected.", addr.isMCGlobal());
  313. addrName = "FF0E:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
  314. addr = Inet6Address.getByName(addrName);
  315. assertTrue("IPv6 global multicast address " + addrName
  316. + " not detected.", addr.isMCGlobal());
  317. // a currently invalid address as the prefix FFxE
  318. // is only valid for x = {1,0} as the rest are reserved
  319. addrName = "FFFE::0";
  320. addr = Inet6Address.getByName(addrName);
  321. assertTrue("IPv6 global mutlicast address " + addrName
  322. + " not detected.", addr.isMCGlobal());
  323. // a currently invalid address as the prefix FFxE
  324. // is only valid for x = {1,0} as the rest are reserved
  325. addrName = "FFFE:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
  326. addr = Inet6Address.getByName(addrName);
  327. assertTrue("IPv6 global multicast address " + addrName
  328. + " not detected.", addr.isMCGlobal());
  329. // a sample MC organizational address
  330. addrName = "FF08:42:42:42:42:42:42:42";
  331. addr = Inet6Address.getByName(addrName);
  332. assertTrue("IPv6 mulitcast organizational " + addrName
  333. + " incorrectly indicated as a global address.", !addr
  334. .isMCGlobal());
  335. // a sample MC site address
  336. addrName = "FF05:42:42:42:42:42:42:42";
  337. addr = Inet6Address.getByName(addrName);
  338. assertTrue("IPv6 mulitcast site address " + addrName
  339. + " incorrectly indicated as a global address.", !addr
  340. .isMCGlobal());
  341. // a sample MC link address
  342. addrName = "FF02:42:42:42:42:42:42:42";
  343. addr = Inet6Address.getByName(addrName);
  344. assertTrue("IPv6 mulitcast link address " + addrName
  345. + " incorrectly indicated as a global address.", !addr
  346. .isMCGlobal());
  347. // a sample MC Node
  348. addrName = "FF01:42:42:42:42:42:42:42";
  349. addr = Inet6Address.getByName(addrName);
  350. assertTrue("IPv6 mulitcast node address " + addrName
  351. + " incorrectly indicated as a global address.", !addr
  352. .isMCGlobal());
  353. // IPv4-mapped IPv6 address tests
  354. addrName = "::FFFF:224.0.1.0";
  355. addr = Inet6Address.getByName(addrName);
  356. assertTrue("IPv4 global multicast address " + addrName
  357. + " not identified as a global multicast address.", addr
  358. .isMCGlobal());
  359. addrName = "::FFFF:238.255.255.255";
  360. addr = Inet6Address.getByName(addrName);
  361. assertTrue("IPv4 global multicast address " + addrName
  362. + " not identified as a global multicast address.", addr
  363. .isMCGlobal());
  364. } catch (Exception e) {
  365. fail("Unknown address : " + addrName);
  366. }
  367. }
  368. /**
  369. * @tests java.net.Inet6Address#isMCNodeLocal()
  370. */
  371. @TestTargetNew(
  372. level = TestLevel.COMPLETE,
  373. notes = "",
  374. method = "isMCNodeLocal",
  375. args = {}
  376. )
  377. public void test_isMCNodeLocal() {
  378. String addrName = "";
  379. try {
  380. // IP V6 regular address tests for Mulitcase node local addresses
  381. //
  382. // Multicast node local addresses are FFx1:/112 where x is
  383. // a set of flags, and the addition 112 bits make up
  384. // the global address space
  385. addrName = "FF01::0";
  386. InetAddress addr = Inet6Address.getByName(addrName);
  387. assertTrue("IPv6 node-local mutlicast address " + addrName
  388. + " not detected.", addr.isMCNodeLocal());
  389. addrName = "FF01:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
  390. addr = Inet6Address.getByName(addrName);
  391. assertTrue("IPv6 node-local multicast address " + addrName
  392. + " not detected.", addr.isMCNodeLocal());
  393. // a currently invalid address as the prefix FFxE
  394. // is only valid for x = {1,0} as the rest are reserved
  395. addrName = "FFF1::0";
  396. addr = Inet6Address.getByName(addrName);
  397. assertTrue("IPv6 node-local mutlicast address " + addrName
  398. + " not detected.", addr.isMCNodeLocal());
  399. // a currently invalid address as the prefix FFxE
  400. // is only valid for x = {1,0} as the rest are reserved
  401. addrName = "FFF1:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
  402. addr = Inet6Address.getByName(addrName);
  403. assertTrue("IPv6 node-local multicast address " + addrName
  404. + " not detected.", addr.isMCNodeLocal());
  405. // a sample MC organizational address
  406. addrName = "FF08:42:42:42:42:42:42:42";
  407. addr = Inet6Address.getByName(addrName);
  408. assertTrue("IPv6 mulitcast organizational address " + addrName
  409. + " incorrectly indicated as a node-local address.", !addr
  410. .isMCNodeLocal());
  411. // a sample MC site address
  412. addrName = "FF05:42:42:42:42:42:42:42";
  413. addr = Inet6Address.getByName(addrName);
  414. assertTrue("IPv6 mulitcast site address " + addrName
  415. + " incorrectly indicated as a node-local address.", !addr
  416. .isMCNodeLocal());
  417. // a sample MC link address
  418. addrName = "FF02:42:42:42:42:42:42:42";
  419. addr = Inet6Address.getByName(addrName);
  420. assertTrue("IPv6 mulitcast link address " + addrName
  421. + " incorrectly indicated as a node-local address.", !addr
  422. .isMCNodeLocal());
  423. // a sample MC global address
  424. addrName = "FF0E:42:42:42:42:42:42:42";
  425. addr = Inet6Address.getByName(addrName);
  426. assertTrue("IPv6 mulitcast node address " + addrName
  427. + " incorrectly indicated as a node-local address.", !addr
  428. .isMCNodeLocal());
  429. } catch (Exception e) {
  430. fail("Unknown address : " + addrName);
  431. }
  432. }
  433. /**
  434. * @tests java.net.Inet6Address#isMCLinkLocal()
  435. */
  436. @TestTargetNew(
  437. level = TestLevel.COMPLETE,
  438. notes = "",
  439. method = "isMCLinkLocal",
  440. args = {}
  441. )
  442. public void test_isMCLinkLocal() {
  443. String addrName = "";
  444. try {
  445. // IP V6 regular address tests for Mulitcase link local addresses
  446. //
  447. // Multicast link local addresses are FFx2:/112 where x is
  448. // a set of flags, and the addition 112 bits make up
  449. // the global address space
  450. addrName = "FF02::0";
  451. InetAddress addr = Inet6Address.getByName(addrName);
  452. assertTrue("IPv6 link local multicast address " + addrName
  453. + " not detected.", addr.isMCLinkLocal());
  454. addrName = "FF02:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
  455. addr = Inet6Address.getByName(addrName);
  456. assertTrue("IPv6 link local multicast address " + addrName
  457. + " not detected.", addr.isMCLinkLocal());
  458. // a currently invalid address as the prefix FFxE
  459. // is only valid for x = {1,0} as the rest are reserved
  460. addrName = "FFF2::0";
  461. addr = Inet6Address.getByName(addrName);
  462. assertTrue("IPv6 link local multicast address " + addrName
  463. + " not detected.", addr.isMCLinkLocal());
  464. // a currently invalid address as the prefix FFxE
  465. // is only valid for x = {1,0} as the rest are reserved
  466. addrName = "FFF2:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
  467. addr = Inet6Address.getByName(addrName);
  468. assertTrue("IPv6 link local multicast address " + addrName
  469. + " not detected.", addr.isMCLinkLocal());
  470. // a sample MC organizational address
  471. addrName = "FF08:42:42:42:42:42:42:42";
  472. addr = Inet6Address.getByName(addrName);
  473. assertTrue(
  474. "IPv6 organization multicast address "
  475. + addrName
  476. + " incorrectly indicated as a link-local mulitcast address.",
  477. !addr.isMCLinkLocal());
  478. // a sample MC site address
  479. addrName = "FF05:42:42:42:42:42:42:42";
  480. addr = Inet6Address.getByName(addrName);
  481. assertTrue(
  482. "IPv6 site-local mulitcast address "
  483. + addrName
  484. + " incorrectly indicated as a link-local mulitcast address.",
  485. !addr.isMCLinkLocal());
  486. // a sample MC global address
  487. addrName = "FF0E:42:42:42:42:42:42:42";
  488. addr = Inet6Address.getByName(addrName);
  489. assertTrue(
  490. "IPv6 global multicast address "
  491. + addrName
  492. + " incorrectly indicated as a link-local mulitcast address.",
  493. !addr.isMCLinkLocal());
  494. // a sample MC Node
  495. addrName = "FF01:42:42:42:42:42:42:42";
  496. addr = Inet6Address.getByName(addrName);
  497. assertTrue(
  498. "IPv6 mulitcast node address "
  499. + addrName
  500. + " incorrectly indicated as a link-local mulitcast address.",
  501. !addr.isMCLinkLocal());
  502. // Ipv4-mapped IPv6 addresses
  503. addrName = "::FFFF:224.0.0.0"; // a multicast addr 1110
  504. addr = Inet6Address.getByName(addrName);
  505. assertTrue("IPv4 link-local multicast address " + addrName
  506. + " not identified as a link-local multicast address.",
  507. addr.isMCLinkLocal());
  508. addrName = "::FFFF:224.0.0.255"; // a multicast addr 1110
  509. addr = Inet6Address.getByName(addrName);
  510. assertTrue("IPv4 link-local multicast address " + addrName
  511. + " not identified as a link-local multicast address.",
  512. addr.isMCLinkLocal());
  513. } catch (Exception e) {
  514. fail("Unknown address : " + addrName);
  515. }
  516. }
  517. /**
  518. * @tests java.net.Inet6Address#isMCSiteLocal()
  519. */
  520. @TestTargetNew(
  521. level = TestLevel.COMPLETE,
  522. notes = "",
  523. method = "isMCSiteLocal",
  524. args = {}
  525. )
  526. public void test_isMCSiteLocal() {
  527. String addrName = "";
  528. try {
  529. // IP V6 regular address tests for Multicast site-local addresses
  530. //
  531. // Multicast global addresses are FFx5:/112 where x is
  532. // a set of flags, and the addition 112 bits make up
  533. // the global address space
  534. addrName = "FF05::0";
  535. InetAddress addr = Inet6Address.getByName(addrName);
  536. assertTrue("IPv6 site-local mutlicast address " + addrName
  537. + " not detected.", addr.isMCSiteLocal());
  538. addrName = "FF05:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
  539. addr = Inet6Address.getByName(addrName);
  540. assertTrue("IPv6 site-local multicast address " + addrName
  541. + " not detected.", addr.isMCSiteLocal());
  542. // a currently invalid address as the prefix FFxE
  543. // is only valid for x = {1,0} as the rest are reserved
  544. addrName = "FFF5::0";
  545. addr = Inet6Address.getByName(addrName);
  546. assertTrue("IPv6 site-local mutlicast address " + addrName
  547. + " not detected.", addr.isMCSiteLocal());
  548. // a currently invalid address as the prefix FFxE
  549. // is only valid for x = {1,0} as the rest are reserved
  550. addrName = "FFF5:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
  551. addr = Inet6Address.getByName(addrName);
  552. assertTrue("IPv6 site-local multicast address " + addrName
  553. + " not detected.", addr.isMCSiteLocal());
  554. // a sample MC organizational address
  555. addrName = "FF08:42:42:42:42:42:42:42";
  556. addr = Inet6Address.getByName(addrName);
  557. assertTrue(
  558. "IPv6 organization multicast address "
  559. + addrName
  560. + " incorrectly indicated as a site-local mulitcast address.",
  561. !addr.isMCSiteLocal());
  562. // a sample MC global address
  563. addrName = "FF0E:42:42:42:42:42:42:42";
  564. addr = Inet6Address.getByName(addrName);
  565. assertTrue(
  566. "IPv6 global mulitcast address "
  567. + addrName
  568. + " incorrectly indicated as a site-local mulitcast address.",
  569. !addr.isMCSiteLocal());
  570. // a sample MC link address
  571. addrName = "FF02:42:42:42:42:42:42:42";
  572. addr = Inet6Address.getByName(addrName);
  573. assertTrue(
  574. "IPv6 link-local multicast address "
  575. + addrName
  576. + " incorrectly indicated as a site-local mulitcast address.",
  577. !addr.isMCSiteLocal());
  578. // a sample MC Node
  579. addrName = "FF01:42:42:42:42:42:42:42";
  580. addr = Inet6Address.getByName(addrName);
  581. assertTrue(
  582. "IPv6 mulitcast node address "
  583. + addrName
  584. + " incorrectly indicated as a site-local mulitcast address.",
  585. !addr.isMCSiteLocal());
  586. // IPv4-mapped IPv6 addresses
  587. addrName = "::FFFF:239.255.0.0";
  588. addr = Inet6Address.getByName(addrName);
  589. assertTrue("IPv4 site-local multicast address " + addrName
  590. + " not identified as a site-local multicast address.",
  591. addr.isMCSiteLocal());
  592. addrName = "::FFFF:239.255.255.255";
  593. addr = Inet6Address.getByName(addrName);
  594. assertTrue("IPv4 site-local multicast address " + addrName
  595. + " not identified as a site-local multicast address.",
  596. addr.isMCSiteLocal());
  597. } catch (Exception e) {
  598. fail("Unknown address : " + addrName);
  599. }
  600. }
  601. /**
  602. * @tests java.net.Inet6Address#isMCOrgLocal()
  603. */
  604. @TestTargetNew(
  605. level = TestLevel.COMPLETE,
  606. notes = "",
  607. method = "isMCOrgLocal",
  608. args = {}
  609. )
  610. public void test_isMCOrgLocal() {
  611. String addrName = "";
  612. try {
  613. // IP V6 regular address tests for Mulitcase organization-local
  614. // addresses
  615. //
  616. // Multicast global addresses are FFxE:/112 where x is
  617. // a set of flags, and the addition 112 bits make up
  618. // the global address space
  619. addrName = "FF08::0";
  620. InetAddress addr = Inet6Address.getByName(addrName);
  621. assertTrue("IPv6 organization-local mutlicast address " + addrName
  622. + " not detected.", addr.isMCOrgLocal());
  623. addrName = "FF08:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
  624. addr = Inet6Address.getByName(addrName);
  625. assertTrue("IPv6 organization-local multicast address " + addrName
  626. + " not detected.", addr.isMCOrgLocal());
  627. // a currently invalid address as the prefix FFxE
  628. // is only valid for x = {1,0} as the rest are reserved
  629. addrName = "FFF8::0";
  630. addr = Inet6Address.getByName(addrName);
  631. assertTrue("IPv6 organization-local mutlicast address " + addrName
  632. + " not detected.", addr.isMCOrgLocal());
  633. // a currently invalid address as the prefix FFxE
  634. // is only valid for x = {1,0} as the rest are reserved
  635. addrName = "FFF8:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
  636. addr = Inet6Address.getByName(addrName);
  637. assertTrue("IPv6 organization-local multicast address " + addrName
  638. + " not detected.", addr.isMCOrgLocal());
  639. // a sample MC global address
  640. addrName = "FF0E:42:42:42:42:42:42:42";
  641. addr = Inet6Address.getByName(addrName);
  642. assertTrue(
  643. "IPv6 global multicast address "
  644. + addrName
  645. + " incorrectly indicated as an organization-local mulitcast address.",
  646. !addr.isMCOrgLocal());
  647. // a sample MC site address
  648. addrName = "FF05:42:42:42:42:42:42:42";
  649. addr = Inet6Address.getByName(addrName);
  650. assertTrue(
  651. "IPv6 site-local mulitcast address "
  652. + addrName
  653. + " incorrectly indicated as an organization-local mulitcast address.",
  654. !addr.isMCOrgLocal());
  655. // a sample MC link address
  656. addrName = "FF02:42:42:42:42:42:42:42";
  657. addr = Inet6Address.getByName(addrName);
  658. assertTrue(
  659. "IPv6 link-local multicast address "
  660. + addrName
  661. + " incorrectly indicated as an organization-local mulitcast address.",
  662. !addr.isMCOrgLocal());
  663. // a sample MC Node
  664. addrName = "FF01:42:42:42:42:42:42:42";
  665. addr = Inet6Address.getByName(addrName);
  666. assertTrue(
  667. "IPv6 mulitcast node address "
  668. + addrName
  669. + " incorrectly indicated as an organization-local mulitcast address.",
  670. !addr.isMCOrgLocal());
  671. // IPv4-mapped IPv6 addresses
  672. addrName = "::FFFF:239.192.0.0";
  673. addr = Inet6Address.getByName(addrName);
  674. assertTrue("IPv4 org-local multicast address " + addrName
  675. + " not identified as a org-local multicast address.", addr
  676. .isMCOrgLocal());
  677. addrName = "::FFFF:239.195.255.255";
  678. addr = Inet6Address.getByName(addrName);
  679. assertTrue("IPv4 org-local multicast address " + addrName
  680. + " not identified as a org-local multicast address.", addr
  681. .isMCOrgLocal());
  682. } catch (Exception e) {
  683. fail("Unknown address : " + addrName);
  684. }
  685. }
  686. /**
  687. * @tests java.net.Inet6Address#isIPv4CompatibleAddress()
  688. */
  689. @TestTargetNew(
  690. level = TestLevel.COMPLETE,
  691. notes = "",
  692. method = "isIPv4CompatibleAddress",
  693. args = {}
  694. )
  695. public void test_isIPv4CompatibleAddress() {
  696. String addrName = "";
  697. Inet6Address addr = null;
  698. try {
  699. // Tests a number of addresses to see if they are compatable with
  700. // IPv6 addresses
  701. addrName = "FFFF::42:42"; // 11111111 = FFFF
  702. addr = (Inet6Address) InetAddress.getByName(addrName);
  703. assertTrue("A non-compatable IPv6 address " + addrName
  704. + " incorrectly identified as a IPv4 compatable address.",
  705. !addr.isIPv4CompatibleAddress());
  706. // IPv4-compatible IPv6 address tests
  707. //
  708. // Now create 2 IP v6 addresses that are IP v4 compatable
  709. // to IP v6 addresses.
  710. addrName = "::0.0.0.0";
  711. addr = (Inet6Address) InetAddress.getByName(addrName);
  712. assertTrue("IPv4 compatable address " + addrName
  713. + " not detected correctly.", addr
  714. .isIPv4CompatibleAddress());
  715. addrName = "::255.255.255.255"; // an ipv4 non-multicast address
  716. addr = (Inet6Address) InetAddress.getByName(addrName);
  717. assertTrue("IPv4 compatable address " + addrName
  718. + " not detected correctly.", addr
  719. .isIPv4CompatibleAddress());
  720. } catch (Exception e) {
  721. e.printStackTrace();
  722. fail("Unknown address : " + addrName);
  723. }
  724. }
  725. /**
  726. * @tests java.net.Inet6Address#getByName(java.lang.String)
  727. */
  728. @TestTargetNew(
  729. level = TestLevel.PARTIAL_COMPLETE,
  730. notes = "",
  731. method = "getByName",
  732. args = {java.lang.String.class}
  733. )
  734. public void test_getByNameLjava_lang_String() throws Exception {
  735. // ones to add "::255.255.255.255", "::FFFF:0.0.0.0",
  736. // "0.0.0.0.0.0::255.255.255.255", "F:F:F:F:F:F:F:F",
  737. // "[F:F:F:F:F:F:F:F]"
  738. String validIPAddresses[] = { "::1.2.3.4", "::", "::", "1::0", "1::",
  739. "::1", "0", /* jdk1.5 accepts 0 as valid */
  740. "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF",
  741. "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:255.255.255.255",
  742. "0:0:0:0:0:0:0:0", "0:0:0:0:0:0:0.0.0.0" };
  743. String invalidIPAddresses[] = { "FFFF:FFFF" };
  744. for (int i = 0; i < validIPAddresses.length; i++) {
  745. InetAddress.getByName(validIPAddresses[i]);
  746. //exercise positive cache
  747. InetAddress.getByName(validIPAddresses[i]);
  748. if (!validIPAddresses[i].equals("0")) {
  749. String tempIPAddress = "[" + validIPAddresses[i] + "]";
  750. InetAddress.getByName(tempIPAddress);
  751. }
  752. }
  753. for (int i = 0; i < invalidIPAddresses.length; i++) {
  754. try {
  755. InetAddress.getByName(invalidIPAddresses[i]);
  756. fail("Invalid IP address incorrectly recognized as valid: "
  757. + invalidIPAddresses[i]);
  758. } catch (Exception e) {
  759. }
  760. //exercise negative cache
  761. try {
  762. InetAddress.getByName(invalidIPAddresses[i]);
  763. fail("Invalid IP address incorrectly recognized as valid: "
  764. + invalidIPAddresses[i]);
  765. } catch (Exception e) {
  766. }
  767. }
  768. }
  769. /**
  770. * @tests java.net.Inet6Address#getByAddress(String, byte[], int)
  771. */
  772. @TestTargetNew(
  773. level = TestLevel.COMPLETE,
  774. notes = "",
  775. method = "getByAddress",
  776. args = {java.lang.String.class, byte[].class, int.class}
  777. )
  778. public void test_getByAddressLString$BI() throws UnknownHostException{
  779. try {
  780. Inet6Address.getByAddress("123", null, 0);
  781. fail("should throw UnknownHostException");
  782. } catch (UnknownHostException uhe) {
  783. // expected
  784. }
  785. byte[] addr1 = { (byte) 127, 0, 0, 1 };
  786. try {
  787. Inet6Address.getByAddress("123", addr1, 0);
  788. fail("should throw UnknownHostException");
  789. } catch (UnknownHostException uhe) {
  790. // expected
  791. }
  792. byte[] addr2 = { (byte) 0xFE, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0x02,
  793. 0x11, 0x25, (byte) 0xFF, (byte) 0xFE, (byte) 0xF8, (byte) 0x7C,
  794. (byte) 0xB2 };
  795. // should not throw any exception
  796. Inet6Address.getByAddress("123", addr2, 3);
  797. Inet6Address.getByAddress("123", addr2, 0);
  798. Inet6Address.getByAddress("123", addr2, -1);
  799. }
  800. /**
  801. * @tests java.net.Inet6Address#getByAddress(String, byte[],
  802. * NetworkInterface)
  803. */
  804. @TestTargetNew(
  805. level = TestLevel.COMPLETE,
  806. notes = "",
  807. method = "getByAddress",
  808. args = {java.lang.String.class, byte[].class, java.net.NetworkInterface.class}
  809. )
  810. public void test_getByAddressLString$BLNetworkInterface()
  811. throws UnknownHostException {
  812. NetworkInterface nif = null;
  813. try {
  814. Inet6Address.getByAddress("123", null, nif);
  815. fail("should throw UnknownHostException");
  816. } catch (UnknownHostException uhe) {
  817. // expected
  818. }
  819. byte[] addr1 = { (byte) 127, 0, 0, 1 };
  820. try {
  821. Inet6Address.getByAddress("123", addr1, nif);
  822. fail("should throw UnknownHostException");
  823. } catch (UnknownHostException uhe) {
  824. // expected
  825. }
  826. byte[] addr2 = { (byte) 0xFE, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0x02,
  827. 0x11, 0x25, (byte) 0xFF, (byte) 0xFE, (byte) 0xF8, (byte)
  828. 0x7C, (byte) 0xB2 };
  829. // should not throw any exception
  830. Inet6Address.getByAddress("123", addr2, nif);
  831. }
  832. /**
  833. * @throws UnknownHostException
  834. * @tests java.net.Inet6Address#getScopeID()
  835. */
  836. @TestTargetNew(
  837. level = TestLevel.COMPLETE,
  838. notes = "",
  839. method = "getScopeId",
  840. args = {}
  841. )
  842. public void test_getScopeID() throws UnknownHostException {
  843. Inet6Address v6ia;
  844. byte[] addr = { (byte) 0xFE, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x11,
  845. 0x25, (byte) 0xFF, (byte) 0xFE, (byte) 0xF8, (byte) 0x7C,
  846. (byte) 0xB2 };
  847. v6ia = Inet6Address.getByAddress("123", addr, 3);
  848. assertEquals(3, v6ia.getScopeId());
  849. v6ia = Inet6Address.getByAddress("123", addr, 0);
  850. assertEquals(0, v6ia.getScopeId());
  851. v6ia = Inet6Address.getByAddress("123", addr, -1);
  852. assertEquals(0, v6ia.getScopeId());
  853. }
  854. /**
  855. * @tests java.net.Inet6Address#getScopedInterface()
  856. */
  857. @TestTargetNew(
  858. level = TestLevel.COMPLETE,
  859. notes = "",
  860. method = "getScopedInterface",
  861. args = {}
  862. )
  863. public void test_getScopedInterface() throws UnknownHostException {
  864. byte[] addr = { (byte) 0xFE, (byte) 0x80, (byte) 0x09, (byte) 0xb5,
  865. (byte) 0x6b, (byte) 0xa4, 0, 0, 0, 0, 0, 0, (byte) 0x09,
  866. (byte) 0xb5, (byte) 0x6b, (byte) 0xa4 };
  867. Inet6Address v6Addr;
  868. v6Addr = Inet6Address.getByAddress("123", addr, null);
  869. assertNull(v6Addr.getScopedInterface());
  870. }
  871. int bytesToInt(byte bytes[], int start) {
  872. int byteMask = 255;
  873. int value = ((bytes[start + 3] & byteMask))
  874. | ((bytes[start + 2] & byteMask) << 8)
  875. | ((bytes[start + 1] & byteMask) << 16)
  876. | ((bytes[start] & byteMask) << 24);
  877. return value;
  878. }
  879. String byteArrayToHexString(byte bytes[], boolean leadingZeros) {
  880. String fullString = "";
  881. int times = bytes.length / 4;
  882. int intArray[] = new int[times];
  883. for (int i = 0; i < times; i++) {
  884. intArray[i] = bytesToInt(bytes, i * 4);
  885. }
  886. return intArrayToHexString(intArray, leadingZeros);
  887. }
  888. void intToBytes(int value, byte bytes[], int start) {
  889. int byteMask = 255;
  890. bytes[start + 3] = (byte) (value & byteMask);
  891. bytes[start + 2] = (byte) ((value >> 8) & byteMask);
  892. bytes[start + 1] = (byte) ((value >> 16) & byteMask);
  893. bytes[start] = (byte) ((value >> 24) & byteMask);
  894. }
  895. String intArrayToHexString(int ints[], boolean leadingZeros) {
  896. String fullString = "";
  897. String tempString;
  898. int intsLength = ints.length;
  899. for (int i = 0; i < intsLength; i++) {
  900. tempString = Integer.toHexString(ints[i]);
  901. while (tempString.length() < 4 && leadingZeros) {
  902. tempString = "0" + tempString;
  903. }
  904. if (i + 1 < intsLength) {
  905. tempString += ":";
  906. }
  907. fullString += tempString;
  908. }
  909. return fullString.toUpperCase();
  910. }
  911. // comparator for Inet6Address objects
  912. private static final SerializableAssert COMPARATOR = new SerializableAssert() {
  913. public void assertDeserialized(Serializable initial,
  914. Serializable deserialized) {
  915. Inet6Address initAddr = (Inet6Address) initial;
  916. Inet6Address desrAddr = (Inet6Address) deserialized;
  917. byte[] iaAddresss = initAddr.getAddress();
  918. byte[] deIAAddresss = desrAddr.getAddress();
  919. for (int i = 0; i < iaAddresss.length; i++) {
  920. assertEquals(iaAddresss[i], deIAAddresss[i]);
  921. }
  922. assertEquals(initAddr.getScopeId(), desrAddr.getScopeId());
  923. assertEquals(initAddr.getScopedInterface(), desrAddr
  924. .getScopedInterface());
  925. }
  926. };
  927. /**
  928. * @tests serialization/deserialization compatibility.
  929. */
  930. @TestTargetNew(
  931. level = TestLevel.COMPLETE,
  932. notes = "Checks serialization",
  933. method = "!SerializationSelf",
  934. args = {}
  935. )
  936. public void testSerializationSelf() throws Exception {
  937. byte[] localv6 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
  938. SerializationTest.verifySelf(InetAddress.getByAddress(localv6),
  939. COMPARATOR);
  940. }
  941. /**
  942. * @tests serialization/deserialization compatibility with RI.
  943. */
  944. @TestTargetNew(
  945. level = TestLevel.COMPLETE,
  946. notes = "Checks serialization",
  947. method = "!SerializationGolden",
  948. args = {}
  949. )
  950. public void testSerializationCompatibility() throws Exception {
  951. byte[] localv6 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
  952. Object[] addresses = { InetAddress.getByAddress(localv6),
  953. // Regression for Harmony-1039: ser-form has
  954. // null interface name
  955. InetAddress.getByAddress(localv6) };
  956. SerializationTest.verifyGolden(this, addresses, COMPARATOR);
  957. }
  958. @TestTargetNew(
  959. level = TestLevel.COMPLETE,
  960. notes = "",
  961. method = "equals",
  962. args = {java.lang.Object.class}
  963. )
  964. public void test_equals() throws Exception {
  965. InetAddress addr = Inet6Address.getByName("239.191.255.255");
  966. assertTrue(addr.equals(addr));
  967. InetAddress addr1 = Inet6Address.getByName("127.0.0.1");
  968. InetAddress addr2 = Inet6Address.getByName("localhost");
  969. assertTrue(addr1.equals(addr2));
  970. assertFalse(addr.equals(addr1));
  971. InetAddress addr3 = Inet6Address.getByName("127.0.0");
  972. assertFalse(addr1.equals(addr3));
  973. }
  974. @TestTargetNew(
  975. level = TestLevel.COMPLETE,
  976. notes = "",
  977. method = "getHostAddress",
  978. args = {}
  979. )
  980. public void test_getHostAddress() throws Exception {
  981. InetAddress aAddr = Inet6Address.getByName("localhost");
  982. assertEquals("127.0.0.1", aAddr.getHostAddress());
  983. aAddr = Inet6Address.getByName("127.0.0.1");
  984. assertEquals("127.0.0.1", aAddr.getHostAddress());
  985. aAddr = Inet6Address.getByName("224.0.0.0");
  986. assertEquals("224.0.0.0", aAddr.getHostAddress());
  987. aAddr = Inet6Address.getByName("1");
  988. assertEquals("0.0.0.1", aAddr.getHostAddress());
  989. aAddr = Inet6Address.getByName("1.1");
  990. assertEquals("1.0.0.1", aAddr.getHostAddress());
  991. aAddr = Inet6Address.getByName("1.1.1");
  992. assertEquals("1.1.0.1", aAddr.getHostAddress());
  993. byte[] bAddr = { (byte) 0xFE, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x11,
  994. 0x25, (byte) 0xFF, (byte) 0xFE, (byte) 0xF8, (byte) 0x7C,
  995. (byte) 0xB2 };
  996. aAddr = Inet6Address.getByAddress(bAddr);
  997. String aString = aAddr.getHostAddress();
  998. assertTrue(aString.equals("fe80:0:0:0:211:25ff:fef8:7cb2") ||
  999. aString.equals("fe80::211:25ff:fef8:7cb2"));
  1000. byte[] cAddr = { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
  1001. (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
  1002. (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
  1003. (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
  1004. aAddr = Inet6Address.getByAddress(cAddr);
  1005. assertEquals("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", aAddr.getHostAddress());
  1006. byte[] dAddr = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
  1007. (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
  1008. (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
  1009. (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00};
  1010. aAddr = Inet6Address.getByAddress(dAddr);
  1011. aString = aAddr.getHostAddress();
  1012. assertTrue(aString.equals("0:0:0:0:0:0:0:0") ||
  1013. aString.equals("::"));
  1014. byte[] eAddr = { (byte) 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03,
  1015. (byte) 0x04, (byte) 0x05, (byte) 0x06, (byte) 0x07,
  1016. (byte) 0x08, (byte) 0x09, (byte) 0x0a, (byte) 0x0b,
  1017. (byte) 0x0c, (byte) 0x0d, (byte) 0x0e, (byte) 0x0f};
  1018. aAddr = Inet6Address.getByAddress(eAddr);
  1019. assertEquals("1:203:405:607:809:a0b:c0d:e0f", aAddr.getHostAddress());
  1020. byte[] fAddr = { (byte) 0x00, (byte) 0x10, (byte) 0x20, (byte) 0x30,
  1021. (byte) 0x40, (byte) 0x50, (byte) 0x60, (byte) 0x70,
  1022. (byte) 0x80, (byte) 0x90, (byte) 0xa0, (byte) 0xb0,
  1023. (byte) 0xc0, (byte) 0xd0, (byte) 0xe0, (byte) 0xf0};
  1024. aAddr = Inet6Address.getByAddress(fAddr);
  1025. assertEquals("10:2030:4050:6070:8090:a0b0:c0d0:e0f0", aAddr.getHostAddress());
  1026. }
  1027. @TestTargetNew(
  1028. level = TestLevel.COMPLETE,
  1029. notes = "",
  1030. method = "hashCode",
  1031. args = {}
  1032. )
  1033. public void test_hashCode() throws Exception {
  1034. InetAddress addr1 = Inet6Address.getByName("1.1");
  1035. InetAddress addr2 = Inet6Address.getByName("1.1.1");
  1036. assertFalse(addr1.hashCode() == addr2.hashCode());
  1037. addr2 = Inet6Address.getByName("1.0.0.1");
  1038. assertTrue(addr1.hashCode() == addr2.hashCode());
  1039. addr1 = Inet6Address.getByName("127.0.0.1");
  1040. addr2 = Inet6Address.getByName("localhost");
  1041. assertTrue(addr1.hashCode() == addr2.hashCode());
  1042. }
  1043. @TestTargetNew(
  1044. level = TestLevel.COMPLETE,
  1045. notes = "",
  1046. method = "toString",
  1047. args = {}
  1048. )
  1049. public void test_toString() throws Exception {
  1050. String validIPAddresses[] = { "::1.2.3.4", "::", "::", "1::0", "1::",
  1051. "::1", "0", /* jdk1.5 accepts 0 as valid */
  1052. "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF",
  1053. "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:255.255.255.255",
  1054. "0:0:0:0:0:0:0:0", "0:0:0:0:0:0:0.0.0.0" };
  1055. String [] resultStrings = {"/0:0:0:0:0:0:102:304", "/0:0:0:0:0:0:0:0",
  1056. "/0:0:0:0:0:0:0:0", "/1:0:0:0:0:0:0:0", "/1:0:0:0:0:0:0:0",
  1057. "/0:0:0:0:0:0:0:1",
  1058. "/0.0.0.0", "/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
  1059. "/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", "/0:0:0:0:0:0:0:0",
  1060. "/0:0:0:0:0:0:0:0"};
  1061. for(int i = 0; i < validIPAddresses.length; i++) {
  1062. InetAddress ia = Inet6Address.getByName(validIPAddresses[i]);
  1063. String result = ia.toString();
  1064. assertNotNull(result);
  1065. //assertEquals("toString method returns incorrect value: " +
  1066. // result, resultStrings[i], result);
  1067. }
  1068. }
  1069. }