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

https://github.com/lems111/Intercept-CM6-Kernel · Java · 1267 lines · 875 code · 183 blank · 209 comment · 14 complexity · 787c880755a140d9c7ba1f8214380be1 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. class MockSecurityManager extends SecurityManager {
  769. public void checkPermission(Permission permission) {
  770. if (permission.getName().equals("setSecurityManager")){
  771. return;
  772. }
  773. if (permission.getName().equals("3d.com")){
  774. throw new SecurityException();
  775. }
  776. super.checkPermission(permission);
  777. }
  778. }
  779. SecurityManager oldman = System.getSecurityManager();
  780. System.setSecurityManager(new MockSecurityManager());
  781. try {
  782. boolean exception = false;
  783. try {
  784. Inet6Address.getByName("3d.com");
  785. fail("SecurityException was not thrown.");
  786. } catch (SecurityException ex) {
  787. //expected
  788. } catch (Exception ex) {
  789. fail("getByName threw wrong exception : " + ex.getMessage());
  790. }
  791. } finally {
  792. System.setSecurityManager(oldman);
  793. }
  794. }
  795. /**
  796. * @tests java.net.Inet6Address#getByAddress(String, byte[], int)
  797. */
  798. @TestTargetNew(
  799. level = TestLevel.COMPLETE,
  800. notes = "",
  801. method = "getByAddress",
  802. args = {java.lang.String.class, byte[].class, int.class}
  803. )
  804. public void test_getByAddressLString$BI() throws UnknownHostException{
  805. try {
  806. Inet6Address.getByAddress("123", null, 0);
  807. fail("should throw UnknownHostException");
  808. } catch (UnknownHostException uhe) {
  809. // expected
  810. }
  811. byte[] addr1 = { (byte) 127, 0, 0, 1 };
  812. try {
  813. Inet6Address.getByAddress("123", addr1, 0);
  814. fail("should throw UnknownHostException");
  815. } catch (UnknownHostException uhe) {
  816. // expected
  817. }
  818. byte[] addr2 = { (byte) 0xFE, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0x02,
  819. 0x11, 0x25, (byte) 0xFF, (byte) 0xFE, (byte) 0xF8, (byte) 0x7C,
  820. (byte) 0xB2 };
  821. // should not throw any exception
  822. Inet6Address.getByAddress("123", addr2, 3);
  823. Inet6Address.getByAddress("123", addr2, 0);
  824. Inet6Address.getByAddress("123", addr2, -1);
  825. }
  826. /**
  827. * @tests java.net.Inet6Address#getByAddress(String, byte[],
  828. * NetworkInterface)
  829. */
  830. @TestTargetNew(
  831. level = TestLevel.COMPLETE,
  832. notes = "",
  833. method = "getByAddress",
  834. args = {java.lang.String.class, byte[].class, java.net.NetworkInterface.class}
  835. )
  836. public void test_getByAddressLString$BLNetworkInterface()
  837. throws UnknownHostException {
  838. NetworkInterface nif = null;
  839. try {
  840. Inet6Address.getByAddress("123", null, nif);
  841. fail("should throw UnknownHostException");
  842. } catch (UnknownHostException uhe) {
  843. // expected
  844. }
  845. byte[] addr1 = { (byte) 127, 0, 0, 1 };
  846. try {
  847. Inet6Address.getByAddress("123", addr1, nif);
  848. fail("should throw UnknownHostException");
  849. } catch (UnknownHostException uhe) {
  850. // expected
  851. }
  852. byte[] addr2 = { (byte) 0xFE, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0x02,
  853. 0x11, 0x25, (byte) 0xFF, (byte) 0xFE, (byte) 0xF8, (byte)
  854. 0x7C, (byte) 0xB2 };
  855. // should not throw any exception
  856. Inet6Address.getByAddress("123", addr2, nif);
  857. }
  858. /**
  859. * @throws UnknownHostException
  860. * @tests java.net.Inet6Address#getScopeID()
  861. */
  862. @TestTargetNew(
  863. level = TestLevel.COMPLETE,
  864. notes = "",
  865. method = "getScopeId",
  866. args = {}
  867. )
  868. public void test_getScopeID() throws UnknownHostException {
  869. Inet6Address v6ia;
  870. byte[] addr = { (byte) 0xFE, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x11,
  871. 0x25, (byte) 0xFF, (byte) 0xFE, (byte) 0xF8, (byte) 0x7C,
  872. (byte) 0xB2 };
  873. v6ia = Inet6Address.getByAddress("123", addr, 3);
  874. assertEquals(3, v6ia.getScopeId());
  875. v6ia = Inet6Address.getByAddress("123", addr, 0);
  876. assertEquals(0, v6ia.getScopeId());
  877. v6ia = Inet6Address.getByAddress("123", addr, -1);
  878. assertEquals(0, v6ia.getScopeId());
  879. }
  880. /**
  881. * @tests java.net.Inet6Address#getScopedInterface()
  882. */
  883. @TestTargetNew(
  884. level = TestLevel.COMPLETE,
  885. notes = "",
  886. method = "getScopedInterface",
  887. args = {}
  888. )
  889. public void test_getScopedInterface() throws UnknownHostException {
  890. byte[] addr = { (byte) 0xFE, (byte) 0x80, (byte) 0x09, (byte) 0xb5,
  891. (byte) 0x6b, (byte) 0xa4, 0, 0, 0, 0, 0, 0, (byte) 0x09,
  892. (byte) 0xb5, (byte) 0x6b, (byte) 0xa4 };
  893. Inet6Address v6Addr;
  894. v6Addr = Inet6Address.getByAddress("123", addr, null);
  895. assertNull(v6Addr.getScopedInterface());
  896. }
  897. int bytesToInt(byte bytes[], int start) {
  898. int byteMask = 255;
  899. int value = ((bytes[start + 3] & byteMask))
  900. | ((bytes[start + 2] & byteMask) << 8)
  901. | ((bytes[start + 1] & byteMask) << 16)
  902. | ((bytes[start] & byteMask) << 24);
  903. return value;
  904. }
  905. String byteArrayToHexString(byte bytes[], boolean leadingZeros) {
  906. String fullString = "";
  907. int times = bytes.length / 4;
  908. int intArray[] = new int[times];
  909. for (int i = 0; i < times; i++) {
  910. intArray[i] = bytesToInt(bytes, i * 4);
  911. }
  912. return intArrayToHexString(intArray, leadingZeros);
  913. }
  914. void intToBytes(int value, byte bytes[], int start) {
  915. int byteMask = 255;
  916. bytes[start + 3] = (byte) (value & byteMask);
  917. bytes[start + 2] = (byte) ((value >> 8) & byteMask);
  918. bytes[start + 1] = (byte) ((value >> 16) & byteMask);
  919. bytes[start] = (byte) ((value >> 24) & byteMask);
  920. }
  921. String intArrayToHexString(int ints[], boolean leadingZeros) {
  922. String fullString = "";
  923. String tempString;
  924. int intsLength = ints.length;
  925. for (int i = 0; i < intsLength; i++) {
  926. tempString = Integer.toHexString(ints[i]);
  927. while (tempString.length() < 4 && leadingZeros) {
  928. tempString = "0" + tempString;
  929. }
  930. if (i + 1 < intsLength) {
  931. tempString += ":";
  932. }
  933. fullString += tempString;
  934. }
  935. return fullString.toUpperCase();
  936. }
  937. // comparator for Inet6Address objects
  938. private static final SerializableAssert COMPARATOR = new SerializableAssert() {
  939. public void assertDeserialized(Serializable initial,
  940. Serializable deserialized) {
  941. Inet6Address initAddr = (Inet6Address) initial;
  942. Inet6Address desrAddr = (Inet6Address) deserialized;
  943. byte[] iaAddresss = initAddr.getAddress();
  944. byte[] deIAAddresss = desrAddr.getAddress();
  945. for (int i = 0; i < iaAddresss.length; i++) {
  946. assertEquals(iaAddresss[i], deIAAddresss[i]);
  947. }
  948. assertEquals(initAddr.getScopeId(), desrAddr.getScopeId());
  949. assertEquals(initAddr.getScopedInterface(), desrAddr
  950. .getScopedInterface());
  951. }
  952. };
  953. /**
  954. * @tests serialization/deserialization compatibility.
  955. */
  956. @TestTargetNew(
  957. level = TestLevel.COMPLETE,
  958. notes = "Checks serialization",
  959. method = "!SerializationSelf",
  960. args = {}
  961. )
  962. public void testSerializationSelf() throws Exception {
  963. byte[] localv6 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
  964. SerializationTest.verifySelf(InetAddress.getByAddress(localv6),
  965. COMPARATOR);
  966. }
  967. /**
  968. * @tests serialization/deserialization compatibility with RI.
  969. */
  970. @TestTargetNew(
  971. level = TestLevel.COMPLETE,
  972. notes = "Checks serialization",
  973. method = "!SerializationGolden",
  974. args = {}
  975. )
  976. public void testSerializationCompatibility() throws Exception {
  977. byte[] localv6 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
  978. Object[] addresses = { InetAddress.getByAddress(localv6),
  979. // Regression for Harmony-1039: ser-form has
  980. // null interface name
  981. InetAddress.getByAddress(localv6) };
  982. SerializationTest.verifyGolden(this, addresses, COMPARATOR);
  983. }
  984. @TestTargetNew(
  985. level = TestLevel.COMPLETE,
  986. notes = "",
  987. method = "equals",
  988. args = {java.lang.Object.class}
  989. )
  990. public void test_equals() throws Exception {
  991. InetAddress addr = Inet6Address.getByName("239.191.255.255");
  992. assertTrue(addr.equals(addr));
  993. InetAddress addr1 = Inet6Address.getByName("127.0.0.1");
  994. InetAddress addr2 = Inet6Address.getByName("localhost");
  995. assertTrue(addr1.equals(addr2));
  996. assertFalse(addr.equals(addr1));
  997. InetAddress addr3 = Inet6Address.getByName("127.0.0");
  998. assertFalse(addr1.equals(addr3));
  999. }
  1000. @TestTargetNew(
  1001. level = TestLevel.COMPLETE,
  1002. notes = "",
  1003. method = "getHostAddress",
  1004. args = {}
  1005. )
  1006. public void test_getHostAddress() throws Exception {
  1007. InetAddress aAddr = Inet6Address.getByName("localhost");
  1008. assertEquals("127.0.0.1", aAddr.getHostAddress());
  1009. aAddr = Inet6Address.getByName("127.0.0.1");
  1010. assertEquals("127.0.0.1", aAddr.getHostAddress());
  1011. aAddr = Inet6Address.getByName("224.0.0.0");
  1012. assertEquals("224.0.0.0", aAddr.getHostAddress());
  1013. aAddr = Inet6Address.getByName("1");
  1014. assertEquals("0.0.0.1", aAddr.getHostAddress());
  1015. aAddr = Inet6Address.getByName("1.1");
  1016. assertEquals("1.0.0.1", aAddr.getHostAddress());
  1017. aAddr = Inet6Address.getByName("1.1.1");
  1018. assertEquals("1.1.0.1", aAddr.getHostAddress());
  1019. byte[] bAddr = { (byte) 0xFE, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x11,
  1020. 0x25, (byte) 0xFF, (byte) 0xFE, (byte) 0xF8, (byte) 0x7C,
  1021. (byte) 0xB2 };
  1022. aAddr = Inet6Address.getByAddress(bAddr);
  1023. String aString = aAddr.getHostAddress();
  1024. assertTrue(aString.equals("fe80:0:0:0:211:25ff:fef8:7cb2") ||
  1025. aString.equals("fe80::211:25ff:fef8:7cb2"));
  1026. byte[] cAddr = { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
  1027. (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
  1028. (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
  1029. (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
  1030. aAddr = Inet6Address.getByAddress(cAddr);
  1031. assertEquals("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", aAddr.getHostAddress());
  1032. byte[] dAddr = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
  1033. (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
  1034. (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
  1035. (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00};
  1036. aAddr = Inet6Address.getByAddress(dAddr);
  1037. aString = aAddr.getHostAddress();
  1038. assertTrue(aString.equals("0:0:0:0:0:0:0:0") ||
  1039. aString.equals("::"));
  1040. byte[] eAddr = { (byte) 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03,
  1041. (byte) 0x04, (byte) 0x05, (byte) 0x06, (byte) 0x07,
  1042. (byte) 0x08, (byte) 0x09, (byte) 0x0a, (byte) 0x0b,
  1043. (byte) 0x0c, (byte) 0x0d, (byte) 0x0e, (byte) 0x0f};
  1044. aAddr = Inet6Address.getByAddress(eAddr);
  1045. assertEquals("1:203:405:607:809:a0b:c0d:e0f", aAddr.getHostAddress());
  1046. byte[] fAddr = { (byte) 0x00, (byte) 0x10, (byte) 0x20, (byte) 0x30,
  1047. (byte) 0x40, (byte) 0x50, (byte) 0x60, (byte) 0x70,
  1048. (byte) 0x80, (byte) 0x90, (byte) 0xa0, (byte) 0xb0,
  1049. (byte) 0xc0, (byte) 0xd0, (byte) 0xe0, (byte) 0xf0};
  1050. aAddr = Inet6Address.getByAddress(fAddr);
  1051. assertEquals("10:2030:4050:6070:8090:a0b0:c0d0:e0f0", aAddr.getHostAddress());
  1052. }
  1053. @TestTargetNew(
  1054. level = TestLevel.COMPLETE,
  1055. notes = "",
  1056. method = "hashCode",
  1057. args = {}
  1058. )
  1059. public void test_hashCode() throws Exception {
  1060. InetAddress addr1 = Inet6Address.getByName("1.1");
  1061. InetAddress addr2 = Inet6Address.getByName("1.1.1");
  1062. assertFalse(addr1.hashCode() == addr2.hashCode());
  1063. addr2 = Inet6Address.getByName("1.0.0.1");
  1064. assertTrue(addr1.hashCode() == addr2.hashCode());
  1065. addr1 = Inet6Address.getByName("127.0.0.1");
  1066. addr2 = Inet6Address.getByName("localhost");
  1067. assertTrue(addr1.hashCode() == addr2.hashCode());
  1068. }
  1069. @TestTargetNew(
  1070. level = TestLevel.COMPLETE,
  1071. notes = "",
  1072. method = "toString",
  1073. args = {}
  1074. )
  1075. public void test_toString() throws Exception {
  1076. String validIPAddresses[] = { "::1.2.3.4", "::", "::", "1::0", "1::",
  1077. "::1", "0", /* jdk1.5 accepts 0 as valid */
  1078. "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF",
  1079. "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:255.255.255.255",
  1080. "0:0:0:0:0:0:0:0", "0:0:0:0:0:0:0.0.0.0" };
  1081. String [] resultStrings = {"/0:0:0:0:0:0:102:304", "/0:0:0:0:0:0:0:0",
  1082. "/0:0:0:0:0:0:0:0", "/1:0:0:0:0:0:0:0", "/1:0:0:0:0:0:0:0",
  1083. "/0:0:0:0:0:0:0:1",
  1084. "/0.0.0.0", "/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",