/external/apache-harmony/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/Inet6AddressTest.java

https://gitlab.com/brian0218/rk3188_r-box_android4.2.2_sdk · Java · 916 lines · 609 code · 149 blank · 158 comment · 9 complexity · 31c22322bd990b73fb759c1331372dee 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 java.io.Serializable;
  19. import java.net.Inet6Address;
  20. import java.net.InetAddress;
  21. import java.net.NetworkInterface;
  22. import java.net.UnknownHostException;
  23. import java.util.Locale;
  24. import org.apache.harmony.testframework.serialization.SerializationTest;
  25. import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
  26. public class Inet6AddressTest extends junit.framework.TestCase {
  27. public void test_isMulticastAddress() throws Exception {
  28. String addrName = "";
  29. InetAddress addr = null;
  30. // IP V6 regular multicast and non-multicast tests
  31. //
  32. // Create 2 IP v6 addresses and call "isMulticastAddress()"
  33. // A prefix of "11111111" means that the address is multicast
  34. // The first one will be one with the prefix the second without
  35. addrName = "FFFF::42:42"; // 11111111 = FFFF
  36. addr = InetAddress.getByName(addrName);
  37. assertTrue("Multicast address " + addrName + " not detected.", addr
  38. .isMulticastAddress());
  39. addrName = "42::42:42"; // an non-multicast address
  40. addr = InetAddress.getByName(addrName);
  41. assertTrue("Non multicast address " + addrName
  42. + " reporting as a multicast address.", !addr
  43. .isMulticastAddress());
  44. // IPv4-compatible IPv6 address tests
  45. //
  46. // Now create 2 IP v6 addresses that are IP v4 compatable
  47. // to IP v6 addresses. The address prefix for a multicast ip v4
  48. // address is 1110 for the last 16 bits ::d.d.d.d
  49. // We expect these to be false
  50. addrName = "::224.42.42.42"; // an ipv4 multicast addr 1110 = 224
  51. addr = InetAddress.getByName(addrName);
  52. assertTrue("IPv4 compatable address " + addrName
  53. + " reported incorrectly as multicast.", !addr
  54. .isMulticastAddress());
  55. addrName = "::42.42.42.42"; // an ipv4 non-multicast address
  56. addr = InetAddress.getByName(addrName);
  57. assertTrue("IPv4 compatable address " + addrName
  58. + " reported incorrectly as multicast.", !addr
  59. .isMulticastAddress());
  60. // IPv4-mapped IPv6 address tests
  61. //
  62. // Now create 2 IP v6 addresses that are IP v4 compatable
  63. // to IP v6 addresses. The address prefix for a multicast ip v4
  64. // address is 1110 for the last 16 bits ::FFFF:d.d.d.d
  65. addrName = "::FFFF:224.42.42.42"; // an ipv4 multicast addr 1110 =
  66. // 224
  67. addr = InetAddress.getByName(addrName);
  68. assertTrue("IPv4-mapped IPv6 multicast address " + addrName
  69. + " not detected.", addr.isMulticastAddress());
  70. addrName = "::FFFF:42.42.42.42"; // an ipv4 non-multicast address
  71. addr = InetAddress.getByName(addrName);
  72. assertTrue("IPv4-mapped IPv6 non-multicast address " + addrName
  73. + " reporting as a multicast address.", !addr
  74. .isMulticastAddress());
  75. }
  76. public void test_isAnyLocalAddress() throws Exception {
  77. String addrName = "";
  78. InetAddress addr = null;
  79. // test to ensure that the unspecified address returns tru
  80. addrName = "::0"; // The unspecified address
  81. addr = InetAddress.getByName(addrName);
  82. assertTrue(
  83. "The unspecified (also known as wildcard and any local address) "
  84. + addrName + " not detected.", addr
  85. .isAnyLocalAddress());
  86. addrName = "::"; // another form of the unspecified address
  87. addr = InetAddress.getByName(addrName);
  88. assertTrue(
  89. "The unspecified (also known as wildcard and any local address) "
  90. + addrName + " not detected.", addr
  91. .isAnyLocalAddress());
  92. addrName = "::1"; // The loopback address
  93. addr = InetAddress.getByName(addrName);
  94. assertTrue("The addresses " + addrName
  95. + " incorrectly reporting an the unspecified address.",
  96. !addr.isAnyLocalAddress());
  97. }
  98. public void test_isLoopbackAddress() throws Exception {
  99. String addrName = "";
  100. // IP V6 regular address tests for loopback
  101. // The loopback address for IPv6 is ::1
  102. addrName = "::1";
  103. InetAddress addr = InetAddress.getByName(addrName);
  104. assertTrue("IPv6 loopback address " + addrName + " not detected.",
  105. addr.isLoopbackAddress());
  106. addrName = "::2";
  107. addr = InetAddress.getByName(addrName);
  108. assertTrue("IPv6 address incorrectly " + addrName
  109. + " detected as a loopback address.", !addr
  110. .isLoopbackAddress());
  111. // a loopback address should be 127.d.d.d
  112. addrName = "42:42::42:42";
  113. addr = InetAddress.getByName(addrName);
  114. assertTrue("IPv6 address incorrectly " + addrName
  115. + " detected as a loopback address.", !addr
  116. .isLoopbackAddress());
  117. // IPv4-compatible IPv6 address tests
  118. //
  119. // Now create 2 IP v6 addresses that are IP v4 compatable
  120. // to IP v6 addresses. The address prefix for a multicast ip v4
  121. // address is 1110 for the last 16 bits ::d.d.d.d
  122. // We expect these to be false, as they are not IPv4 addresses
  123. // a loopback address should be 127.d.d.d
  124. addrName = "::127.0.0.0";
  125. addr = InetAddress.getByName(addrName);
  126. assertTrue("IPv4-compatible IPv6 address " + addrName
  127. + " detected incorrectly as a loopback.", !addr
  128. .isLoopbackAddress());
  129. addrName = "::127.42.42.42"; // a loopback address should be
  130. // 127.d.d.d
  131. addr = InetAddress.getByName(addrName);
  132. assertTrue("IPv4-compatible IPv6 address " + addrName
  133. + " detected incorrectly as a loopback.", !addr
  134. .isLoopbackAddress());
  135. // a loopback address should be 127.d.d.d
  136. addrName = "::42.42.42.42";
  137. addr = InetAddress.getByName(addrName);
  138. assertTrue("IPv4-compatible IPv6 address " + addrName
  139. + " detected incorrectly as a loopback.", !addr
  140. .isLoopbackAddress());
  141. // IPv4-mapped IPv6 address tests
  142. //
  143. // Now create 2 IP v6 addresses that are IP v4 compatable
  144. // to IP v6 addresses. The address prefix for a multicast ip v4
  145. // address is 1110 for the last 16 bits ::FFFF:d.d.d.d
  146. // a loopback address should be 127.d.d.d
  147. addrName = "::FFFF:127.0.0.0";
  148. addr = InetAddress.getByName(addrName);
  149. assertTrue("IPv4-compatible IPv6 loopback address " + addrName
  150. + " not detected.", addr.isLoopbackAddress());
  151. // a loopback address should be 127.d.d.d
  152. addrName = "::FFFF:127.42.42.42";
  153. addr = InetAddress.getByName(addrName);
  154. assertTrue("IPv4-compatible IPv6 loopback address " + addrName
  155. + " not detected.", addr.isLoopbackAddress());
  156. // a loopback address should be 127.d.d.d
  157. addrName = "::FFFF:42.42.42.42";
  158. addr = InetAddress.getByName(addrName);
  159. assertTrue("IPv4-compatible IPv6 address incorrectly " + addrName
  160. + " detected as a loopback address.", !addr
  161. .isLoopbackAddress());
  162. }
  163. public void test_isLinkLocalAddress() throws Exception {
  164. String addrName = "";
  165. // IP V6 regular address tests for link local addresses
  166. //
  167. // Link local addresses are FE80:: -
  168. // FEBF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF
  169. addrName = "FE80::0";
  170. InetAddress addr = InetAddress.getByName(addrName);
  171. assertTrue(
  172. "IPv6 link local address " + addrName + " not detected.",
  173. addr.isLinkLocalAddress());
  174. addrName = "FEBF::FFFF:FFFF:FFFF:FFFF";
  175. addr = InetAddress.getByName(addrName);
  176. assertTrue(
  177. "IPv6 link local address " + addrName + " not detected.",
  178. addr.isLinkLocalAddress());
  179. addrName = "FEC0::1";
  180. addr = InetAddress.getByName(addrName);
  181. assertTrue("IPv6 address " + addrName
  182. + " detected incorrectly as a link local address.", !addr
  183. .isLinkLocalAddress());
  184. addrName = "FD80::1:FFFF:FFFF:FFFF:FFFF";
  185. addr = InetAddress.getByName(addrName);
  186. assertTrue("IPv6 address " + addrName
  187. + " detected incorrectly as a link local address.", !addr
  188. .isLinkLocalAddress());
  189. addrName = "FE7F::FFFF:FFFF:FFFF:FFFF";
  190. addr = InetAddress.getByName(addrName);
  191. assertTrue("IPv6 address " + addrName
  192. + " detected incorrectly as a link local address.", !addr
  193. .isLinkLocalAddress());
  194. }
  195. public void test_isSiteLocalAddress() throws Exception {
  196. String addrName = "";
  197. // IP V6 regular address tests for link local addresses
  198. //
  199. // Link local addresses are FEC0::0 through to
  200. // FEFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF
  201. addrName = "FEC0::0";
  202. InetAddress addr = InetAddress.getByName(addrName);
  203. assertTrue(
  204. "IPv6 site local address " + addrName + " not detected.",
  205. addr.isSiteLocalAddress());
  206. addrName = "FEFF::FFFF:FFFF:FFFF:FFFF:FFFF";
  207. addr = InetAddress.getByName(addrName);
  208. assertTrue(
  209. "IPv6 site local address " + addrName + " not detected.",
  210. addr.isSiteLocalAddress());
  211. addrName = "FEBF::FFFF:FFFF:FFFF:FFFF:FFFF";
  212. addr = InetAddress.getByName(addrName);
  213. assertTrue("IPv6 address " + addrName
  214. + " detected incorrectly as a site local address.", !addr
  215. .isSiteLocalAddress());
  216. addrName = "FFC0::0";
  217. addr = InetAddress.getByName(addrName);
  218. assertTrue("IPv6 address " + addrName
  219. + " detected incorrectly as a site local address.", !addr
  220. .isSiteLocalAddress());
  221. }
  222. public void test_isMCGlobal() throws Exception {
  223. String addrName = "";
  224. // IP V6 regular address tests for Mulitcase Global addresses
  225. //
  226. // Multicast global addresses are FFxE:/112 where x is
  227. // a set of flags, and the addition 112 bits make up
  228. // the global address space
  229. addrName = "FF0E::0";
  230. InetAddress addr = InetAddress.getByName(addrName);
  231. assertTrue("IPv6 global mutlicast address " + addrName
  232. + " not detected.", addr.isMCGlobal());
  233. addrName = "FF0E:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
  234. addr = InetAddress.getByName(addrName);
  235. assertTrue("IPv6 global multicast address " + addrName
  236. + " not detected.", addr.isMCGlobal());
  237. // a currently invalid address as the prefix FFxE
  238. // is only valid for x = {1,0} as the rest are reserved
  239. addrName = "FFFE::0";
  240. addr = InetAddress.getByName(addrName);
  241. assertTrue("IPv6 global mutlicast address " + addrName
  242. + " not detected.", addr.isMCGlobal());
  243. // a currently invalid address as the prefix FFxE
  244. // is only valid for x = {1,0} as the rest are reserved
  245. addrName = "FFFE:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
  246. addr = InetAddress.getByName(addrName);
  247. assertTrue("IPv6 global multicast address " + addrName
  248. + " not detected.", addr.isMCGlobal());
  249. // a sample MC organizational address
  250. addrName = "FF08:42:42:42:42:42:42:42";
  251. addr = InetAddress.getByName(addrName);
  252. assertTrue("IPv6 mulitcast organizational " + addrName
  253. + " incorrectly indicated as a global address.", !addr
  254. .isMCGlobal());
  255. // a sample MC site address
  256. addrName = "FF05:42:42:42:42:42:42:42";
  257. addr = InetAddress.getByName(addrName);
  258. assertTrue("IPv6 mulitcast site address " + addrName
  259. + " incorrectly indicated as a global address.", !addr
  260. .isMCGlobal());
  261. // a sample MC link address
  262. addrName = "FF02:42:42:42:42:42:42:42";
  263. addr = InetAddress.getByName(addrName);
  264. assertTrue("IPv6 mulitcast link address " + addrName
  265. + " incorrectly indicated as a global address.", !addr
  266. .isMCGlobal());
  267. // a sample MC Node
  268. addrName = "FF01:42:42:42:42:42:42:42";
  269. addr = InetAddress.getByName(addrName);
  270. assertTrue("IPv6 mulitcast node address " + addrName
  271. + " incorrectly indicated as a global address.", !addr
  272. .isMCGlobal());
  273. // IPv4-mapped IPv6 address tests
  274. addrName = "::FFFF:224.0.1.0";
  275. addr = InetAddress.getByName(addrName);
  276. assertTrue("IPv4 global multicast address " + addrName
  277. + " not identified as a global multicast address.", addr
  278. .isMCGlobal());
  279. addrName = "::FFFF:238.255.255.255";
  280. addr = InetAddress.getByName(addrName);
  281. assertTrue("IPv4 global multicast address " + addrName
  282. + " not identified as a global multicast address.", addr
  283. .isMCGlobal());
  284. }
  285. public void test_isMCNodeLocal() throws Exception {
  286. String addrName = "";
  287. // IP V6 regular address tests for Mulitcase node local addresses
  288. //
  289. // Multicast node local addresses are FFx1:/112 where x is
  290. // a set of flags, and the addition 112 bits make up
  291. // the global address space
  292. addrName = "FF01::0";
  293. InetAddress addr = InetAddress.getByName(addrName);
  294. assertTrue("IPv6 node-local mutlicast address " + addrName
  295. + " not detected.", addr.isMCNodeLocal());
  296. addrName = "FF01:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
  297. addr = InetAddress.getByName(addrName);
  298. assertTrue("IPv6 node-local multicast address " + addrName
  299. + " not detected.", addr.isMCNodeLocal());
  300. // a currently invalid address as the prefix FFxE
  301. // is only valid for x = {1,0} as the rest are reserved
  302. addrName = "FFF1::0";
  303. addr = InetAddress.getByName(addrName);
  304. assertTrue("IPv6 node-local mutlicast address " + addrName
  305. + " not detected.", addr.isMCNodeLocal());
  306. // a currently invalid address as the prefix FFxE
  307. // is only valid for x = {1,0} as the rest are reserved
  308. addrName = "FFF1:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
  309. addr = InetAddress.getByName(addrName);
  310. assertTrue("IPv6 node-local multicast address " + addrName
  311. + " not detected.", addr.isMCNodeLocal());
  312. // a sample MC organizational address
  313. addrName = "FF08:42:42:42:42:42:42:42";
  314. addr = InetAddress.getByName(addrName);
  315. assertTrue("IPv6 mulitcast organizational address " + addrName
  316. + " incorrectly indicated as a node-local address.", !addr
  317. .isMCNodeLocal());
  318. // a sample MC site address
  319. addrName = "FF05:42:42:42:42:42:42:42";
  320. addr = InetAddress.getByName(addrName);
  321. assertTrue("IPv6 mulitcast site address " + addrName
  322. + " incorrectly indicated as a node-local address.", !addr
  323. .isMCNodeLocal());
  324. // a sample MC link address
  325. addrName = "FF02:42:42:42:42:42:42:42";
  326. addr = InetAddress.getByName(addrName);
  327. assertTrue("IPv6 mulitcast link address " + addrName
  328. + " incorrectly indicated as a node-local address.", !addr
  329. .isMCNodeLocal());
  330. // a sample MC global address
  331. addrName = "FF0E:42:42:42:42:42:42:42";
  332. addr = InetAddress.getByName(addrName);
  333. assertTrue("IPv6 mulitcast node address " + addrName
  334. + " incorrectly indicated as a node-local address.", !addr
  335. .isMCNodeLocal());
  336. }
  337. public void test_isMCLinkLocal() throws Exception {
  338. String addrName = "";
  339. // IP V6 regular address tests for Mulitcase link local addresses
  340. //
  341. // Multicast link local addresses are FFx2:/112 where x is
  342. // a set of flags, and the addition 112 bits make up
  343. // the global address space
  344. addrName = "FF02::0";
  345. InetAddress addr = InetAddress.getByName(addrName);
  346. assertTrue("IPv6 link local multicast address " + addrName
  347. + " not detected.", addr.isMCLinkLocal());
  348. addrName = "FF02:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
  349. addr = InetAddress.getByName(addrName);
  350. assertTrue("IPv6 link local multicast address " + addrName
  351. + " not detected.", addr.isMCLinkLocal());
  352. // a currently invalid address as the prefix FFxE
  353. // is only valid for x = {1,0} as the rest are reserved
  354. addrName = "FFF2::0";
  355. addr = InetAddress.getByName(addrName);
  356. assertTrue("IPv6 link local multicast address " + addrName
  357. + " not detected.", addr.isMCLinkLocal());
  358. // a currently invalid address as the prefix FFxE
  359. // is only valid for x = {1,0} as the rest are reserved
  360. addrName = "FFF2:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
  361. addr = InetAddress.getByName(addrName);
  362. assertTrue("IPv6 link local multicast address " + addrName
  363. + " not detected.", addr.isMCLinkLocal());
  364. // a sample MC organizational address
  365. addrName = "FF08:42:42:42:42:42:42:42";
  366. addr = InetAddress.getByName(addrName);
  367. assertTrue(
  368. "IPv6 organization multicast address "
  369. + addrName
  370. + " incorrectly indicated as a link-local mulitcast address.",
  371. !addr.isMCLinkLocal());
  372. // a sample MC site address
  373. addrName = "FF05:42:42:42:42:42:42:42";
  374. addr = InetAddress.getByName(addrName);
  375. assertTrue(
  376. "IPv6 site-local mulitcast address "
  377. + addrName
  378. + " incorrectly indicated as a link-local mulitcast address.",
  379. !addr.isMCLinkLocal());
  380. // a sample MC global address
  381. addrName = "FF0E:42:42:42:42:42:42:42";
  382. addr = InetAddress.getByName(addrName);
  383. assertTrue(
  384. "IPv6 global multicast address "
  385. + addrName
  386. + " incorrectly indicated as a link-local mulitcast address.",
  387. !addr.isMCLinkLocal());
  388. // a sample MC Node
  389. addrName = "FF01:42:42:42:42:42:42:42";
  390. addr = InetAddress.getByName(addrName);
  391. assertTrue(
  392. "IPv6 mulitcast node address "
  393. + addrName
  394. + " incorrectly indicated as a link-local mulitcast address.",
  395. !addr.isMCLinkLocal());
  396. // Ipv4-mapped IPv6 addresses
  397. addrName = "::FFFF:224.0.0.0"; // a multicast addr 1110
  398. addr = InetAddress.getByName(addrName);
  399. assertTrue("IPv4 link-local multicast address " + addrName
  400. + " not identified as a link-local multicast address.",
  401. addr.isMCLinkLocal());
  402. addrName = "::FFFF:224.0.0.255"; // a multicast addr 1110
  403. addr = InetAddress.getByName(addrName);
  404. assertTrue("IPv4 link-local multicast address " + addrName
  405. + " not identified as a link-local multicast address.",
  406. addr.isMCLinkLocal());
  407. }
  408. public void test_isMCSiteLocal() throws Exception {
  409. String addrName = "";
  410. // IP V6 regular address tests for Multicast site-local addresses
  411. //
  412. // Multicast global addresses are FFx5:/112 where x is
  413. // a set of flags, and the addition 112 bits make up
  414. // the global address space
  415. addrName = "FF05::0";
  416. InetAddress addr = InetAddress.getByName(addrName);
  417. assertTrue("IPv6 site-local mutlicast address " + addrName
  418. + " not detected.", addr.isMCSiteLocal());
  419. addrName = "FF05:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
  420. addr = InetAddress.getByName(addrName);
  421. assertTrue("IPv6 site-local multicast address " + addrName
  422. + " not detected.", addr.isMCSiteLocal());
  423. // a currently invalid address as the prefix FFxE
  424. // is only valid for x = {1,0} as the rest are reserved
  425. addrName = "FFF5::0";
  426. addr = InetAddress.getByName(addrName);
  427. assertTrue("IPv6 site-local mutlicast address " + addrName
  428. + " not detected.", addr.isMCSiteLocal());
  429. // a currently invalid address as the prefix FFxE
  430. // is only valid for x = {1,0} as the rest are reserved
  431. addrName = "FFF5:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
  432. addr = InetAddress.getByName(addrName);
  433. assertTrue("IPv6 site-local multicast address " + addrName
  434. + " not detected.", addr.isMCSiteLocal());
  435. // a sample MC organizational address
  436. addrName = "FF08:42:42:42:42:42:42:42";
  437. addr = InetAddress.getByName(addrName);
  438. assertTrue(
  439. "IPv6 organization multicast address "
  440. + addrName
  441. + " incorrectly indicated as a site-local mulitcast address.",
  442. !addr.isMCSiteLocal());
  443. // a sample MC global address
  444. addrName = "FF0E:42:42:42:42:42:42:42";
  445. addr = InetAddress.getByName(addrName);
  446. assertTrue(
  447. "IPv6 global mulitcast address "
  448. + addrName
  449. + " incorrectly indicated as a site-local mulitcast address.",
  450. !addr.isMCSiteLocal());
  451. // a sample MC link address
  452. addrName = "FF02:42:42:42:42:42:42:42";
  453. addr = InetAddress.getByName(addrName);
  454. assertTrue(
  455. "IPv6 link-local multicast address "
  456. + addrName
  457. + " incorrectly indicated as a site-local mulitcast address.",
  458. !addr.isMCSiteLocal());
  459. // a sample MC Node
  460. addrName = "FF01:42:42:42:42:42:42:42";
  461. addr = InetAddress.getByName(addrName);
  462. assertTrue(
  463. "IPv6 mulitcast node address "
  464. + addrName
  465. + " incorrectly indicated as a site-local mulitcast address.",
  466. !addr.isMCSiteLocal());
  467. // IPv4-mapped IPv6 addresses
  468. addrName = "::FFFF:239.255.0.0";
  469. addr = InetAddress.getByName(addrName);
  470. assertTrue("IPv4 site-local multicast address " + addrName
  471. + " not identified as a site-local multicast address.",
  472. addr.isMCSiteLocal());
  473. addrName = "::FFFF:239.255.255.255";
  474. addr = InetAddress.getByName(addrName);
  475. assertTrue("IPv4 site-local multicast address " + addrName
  476. + " not identified as a site-local multicast address.",
  477. addr.isMCSiteLocal());
  478. }
  479. public void test_isMCOrgLocal() throws Exception {
  480. String addrName = "";
  481. // IP V6 regular address tests for Mulitcase organization-local
  482. // addresses
  483. //
  484. // Multicast global addresses are FFxE:/112 where x is
  485. // a set of flags, and the addition 112 bits make up
  486. // the global address space
  487. addrName = "FF08::0";
  488. InetAddress addr = InetAddress.getByName(addrName);
  489. assertTrue("IPv6 organization-local mutlicast address " + addrName
  490. + " not detected.", addr.isMCOrgLocal());
  491. addrName = "FF08:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
  492. addr = InetAddress.getByName(addrName);
  493. assertTrue("IPv6 organization-local multicast address " + addrName
  494. + " not detected.", addr.isMCOrgLocal());
  495. // a currently invalid address as the prefix FFxE
  496. // is only valid for x = {1,0} as the rest are reserved
  497. addrName = "FFF8::0";
  498. addr = InetAddress.getByName(addrName);
  499. assertTrue("IPv6 organization-local mutlicast address " + addrName
  500. + " not detected.", addr.isMCOrgLocal());
  501. // a currently invalid address as the prefix FFxE
  502. // is only valid for x = {1,0} as the rest are reserved
  503. addrName = "FFF8:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
  504. addr = InetAddress.getByName(addrName);
  505. assertTrue("IPv6 organization-local multicast address " + addrName
  506. + " not detected.", addr.isMCOrgLocal());
  507. // a sample MC global address
  508. addrName = "FF0E:42:42:42:42:42:42:42";
  509. addr = InetAddress.getByName(addrName);
  510. assertTrue(
  511. "IPv6 global multicast address "
  512. + addrName
  513. + " incorrectly indicated as an organization-local mulitcast address.",
  514. !addr.isMCOrgLocal());
  515. // a sample MC site address
  516. addrName = "FF05:42:42:42:42:42:42:42";
  517. addr = InetAddress.getByName(addrName);
  518. assertTrue(
  519. "IPv6 site-local mulitcast address "
  520. + addrName
  521. + " incorrectly indicated as an organization-local mulitcast address.",
  522. !addr.isMCOrgLocal());
  523. // a sample MC link address
  524. addrName = "FF02:42:42:42:42:42:42:42";
  525. addr = InetAddress.getByName(addrName);
  526. assertTrue(
  527. "IPv6 link-local multicast address "
  528. + addrName
  529. + " incorrectly indicated as an organization-local mulitcast address.",
  530. !addr.isMCOrgLocal());
  531. // a sample MC Node
  532. addrName = "FF01:42:42:42:42:42:42:42";
  533. addr = InetAddress.getByName(addrName);
  534. assertTrue(
  535. "IPv6 mulitcast node address "
  536. + addrName
  537. + " incorrectly indicated as an organization-local mulitcast address.",
  538. !addr.isMCOrgLocal());
  539. // IPv4-mapped IPv6 addresses
  540. addrName = "::FFFF:239.192.0.0";
  541. addr = InetAddress.getByName(addrName);
  542. assertTrue("IPv4 org-local multicast address " + addrName
  543. + " not identified as a org-local multicast address.", addr
  544. .isMCOrgLocal());
  545. addrName = "::FFFF:239.195.255.255";
  546. addr = InetAddress.getByName(addrName);
  547. assertTrue("IPv4 org-local multicast address " + addrName
  548. + " not identified as a org-local multicast address.", addr
  549. .isMCOrgLocal());
  550. }
  551. public void test_isIPv4CompatibleAddress() throws Exception {
  552. String addrName = "";
  553. Inet6Address addr = null;
  554. // Tests a number of addresses to see if they are compatable with
  555. // IPv6 addresses
  556. addrName = "FFFF::42:42"; // 11111111 = FFFF
  557. addr = (Inet6Address) InetAddress.getByName(addrName);
  558. assertTrue("A non-compatable IPv6 address " + addrName
  559. + " incorrectly identified as a IPv4 compatable address.",
  560. !addr.isIPv4CompatibleAddress());
  561. // IPv4-compatible IPv6 address tests
  562. //
  563. // Now create 2 IP v6 addresses that are IP v4 compatable
  564. // to IP v6 addresses.
  565. addrName = "::0.0.0.0";
  566. addr = (Inet6Address) InetAddress.getByName(addrName);
  567. assertTrue("IPv4 compatable address " + addrName
  568. + " not detected correctly.", addr
  569. .isIPv4CompatibleAddress());
  570. addrName = "::255.255.255.255"; // an ipv4 non-multicast address
  571. addr = (Inet6Address) InetAddress.getByName(addrName);
  572. assertTrue("IPv4 compatable address " + addrName
  573. + " not detected correctly.", addr
  574. .isIPv4CompatibleAddress());
  575. }
  576. public void test_getByNameLjava_lang_String() throws Exception {
  577. // ones to add "::255.255.255.255", "::FFFF:0.0.0.0",
  578. // "0.0.0.0.0.0::255.255.255.255", "F:F:F:F:F:F:F:F",
  579. // "[F:F:F:F:F:F:F:F]"
  580. String validIPAddresses[] = { "::1.2.3.4", "::", "::", "1::0", "1::",
  581. "::1", "0", /* jdk1.5 accepts 0 as valid */
  582. "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF",
  583. "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:255.255.255.255",
  584. "0:0:0:0:0:0:0:0", "0:0:0:0:0:0:0.0.0.0" };
  585. String invalidIPAddresses[] = { "FFFF:FFFF" };
  586. for (int i = 0; i < validIPAddresses.length; i++) {
  587. InetAddress.getByName(validIPAddresses[i]);
  588. //exercise positive cache
  589. InetAddress.getByName(validIPAddresses[i]);
  590. if (!validIPAddresses[i].equals("0")) {
  591. String tempIPAddress = "[" + validIPAddresses[i] + "]";
  592. InetAddress.getByName(tempIPAddress);
  593. }
  594. }
  595. for (int i = 0; i < invalidIPAddresses.length; i++) {
  596. try {
  597. InetAddress.getByName(invalidIPAddresses[i]);
  598. fail("Invalid IP address incorrectly recognized as valid: "
  599. + invalidIPAddresses[i]);
  600. } catch (Exception e) {
  601. }
  602. //exercise negative cache
  603. try {
  604. InetAddress.getByName(invalidIPAddresses[i]);
  605. fail("Invalid IP address incorrectly recognized as valid: "
  606. + invalidIPAddresses[i]);
  607. } catch (Exception e) {
  608. }
  609. }
  610. }
  611. public void test_getByAddressLString$BI() throws UnknownHostException{
  612. try {
  613. Inet6Address.getByAddress("123", null, 0);
  614. fail("should throw UnknownHostException");
  615. } catch (UnknownHostException uhe) {
  616. // expected
  617. }
  618. byte[] addr1 = { (byte) 127, 0, 0, 1 };
  619. try {
  620. Inet6Address.getByAddress("123", addr1, 0);
  621. fail("should throw UnknownHostException");
  622. } catch (UnknownHostException uhe) {
  623. // expected
  624. }
  625. byte[] addr2 = { (byte) 0xFE, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0x02,
  626. 0x11, 0x25, (byte) 0xFF, (byte) 0xFE, (byte) 0xF8, (byte) 0x7C,
  627. (byte) 0xB2 };
  628. // should not throw any exception
  629. Inet6Address.getByAddress("123", addr2, 3);
  630. Inet6Address.getByAddress("123", addr2, 0);
  631. Inet6Address.getByAddress("123", addr2, -1);
  632. }
  633. public void test_getByAddressLString$BLNetworkInterface()
  634. throws UnknownHostException {
  635. NetworkInterface nif = null;
  636. try {
  637. Inet6Address.getByAddress("123", null, nif);
  638. fail("should throw UnknownHostException");
  639. } catch (UnknownHostException uhe) {
  640. // expected
  641. }
  642. byte[] addr1 = { (byte) 127, 0, 0, 1 };
  643. try {
  644. Inet6Address.getByAddress("123", addr1, nif);
  645. fail("should throw UnknownHostException");
  646. } catch (UnknownHostException uhe) {
  647. // expected
  648. }
  649. byte[] addr2 = { (byte) 0xFE, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0x02,
  650. 0x11, 0x25, (byte) 0xFF, (byte) 0xFE, (byte) 0xF8, (byte)
  651. 0x7C, (byte) 0xB2 };
  652. // should not throw any exception
  653. Inet6Address.getByAddress("123", addr2, nif);
  654. }
  655. public void test_getHostAddress_() throws Exception {
  656. byte[] ipAddress = new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
  657. InetAddress ia = InetAddress.getByAddress(ipAddress);
  658. assertEquals("::1", ia.getHostAddress().toLowerCase(Locale.US));
  659. ipAddress = new byte[] {-2, -128, 0, 0, 0, 0, 0, 0, 2, 17, 37, -1, -2, -8, 124, -79};
  660. ia = InetAddress.getByAddress(ipAddress);
  661. assertEquals("fe80::211:25ff:fef8:7cb1", ia.getHostAddress().toLowerCase(Locale.US));
  662. }
  663. public void test_getScopeID() throws UnknownHostException {
  664. Inet6Address v6ia;
  665. byte[] addr = { (byte) 0xFE, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x11,
  666. 0x25, (byte) 0xFF, (byte) 0xFE, (byte) 0xF8, (byte) 0x7C,
  667. (byte) 0xB2 };
  668. v6ia = Inet6Address.getByAddress("123", addr, 3);
  669. assertEquals(3, v6ia.getScopeId());
  670. v6ia = Inet6Address.getByAddress("123", addr, 0);
  671. assertEquals(0, v6ia.getScopeId());
  672. v6ia = Inet6Address.getByAddress("123", addr, -1);
  673. assertEquals(0, v6ia.getScopeId());
  674. }
  675. public void test_getScopedInterface() throws UnknownHostException {
  676. byte[] addr = { (byte) 0xFE, (byte) 0x80, (byte) 0x09, (byte) 0xb5,
  677. (byte) 0x6b, (byte) 0xa4, 0, 0, 0, 0, 0, 0, (byte) 0x09,
  678. (byte) 0xb5, (byte) 0x6b, (byte) 0xa4 };
  679. Inet6Address v6Addr;
  680. v6Addr = Inet6Address.getByAddress("123", addr, null);
  681. assertNull(v6Addr.getScopedInterface());
  682. }
  683. public void test_hashCode() throws UnknownHostException{
  684. byte[] addr = { (byte) 0xFE, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x11,
  685. 0x25, (byte) 0xFF, (byte) 0xFE, (byte) 0xF8, (byte) 0x7C,
  686. (byte) 0xB2 };
  687. Inet6Address address1, address2;
  688. address1 = Inet6Address.getByAddress("123", addr, 0);
  689. address2 = Inet6Address.getByAddress("1234", addr, 0);
  690. assertEquals(address1.hashCode(), address2.hashCode());
  691. }
  692. int bytesToInt(byte bytes[], int start) {
  693. int byteMask = 255;
  694. int value = ((bytes[start + 3] & byteMask))
  695. | ((bytes[start + 2] & byteMask) << 8)
  696. | ((bytes[start + 1] & byteMask) << 16)
  697. | ((bytes[start] & byteMask) << 24);
  698. return value;
  699. }
  700. String byteArrayToHexString(byte bytes[], boolean leadingZeros) {
  701. String fullString = "";
  702. int times = bytes.length / 4;
  703. int intArray[] = new int[times];
  704. for (int i = 0; i < times; i++) {
  705. intArray[i] = bytesToInt(bytes, i * 4);
  706. }
  707. return intArrayToHexString(intArray, leadingZeros);
  708. }
  709. void intToBytes(int value, byte bytes[], int start) {
  710. int byteMask = 255;
  711. bytes[start + 3] = (byte) (value & byteMask);
  712. bytes[start + 2] = (byte) ((value >> 8) & byteMask);
  713. bytes[start + 1] = (byte) ((value >> 16) & byteMask);
  714. bytes[start] = (byte) ((value >> 24) & byteMask);
  715. }
  716. String intArrayToHexString(int ints[], boolean leadingZeros) {
  717. String fullString = "";
  718. String tempString;
  719. int intsLength = ints.length;
  720. for (int i = 0; i < intsLength; i++) {
  721. tempString = Integer.toHexString(ints[i]);
  722. while (tempString.length() < 4 && leadingZeros) {
  723. tempString = "0" + tempString;
  724. }
  725. if (i + 1 < intsLength) {
  726. tempString += ":";
  727. }
  728. fullString += tempString;
  729. }
  730. return fullString.toUpperCase();
  731. }
  732. // comparator for Inet6Address objects
  733. private static final SerializableAssert COMPARATOR = new SerializableAssert() {
  734. public void assertDeserialized(Serializable initial,
  735. Serializable deserialized) {
  736. Inet6Address initAddr = (Inet6Address) initial;
  737. Inet6Address desrAddr = (Inet6Address) deserialized;
  738. byte[] iaAddresss = initAddr.getAddress();
  739. byte[] deIAAddresss = desrAddr.getAddress();
  740. for (int i = 0; i < iaAddresss.length; i++) {
  741. assertEquals(iaAddresss[i], deIAAddresss[i]);
  742. }
  743. assertEquals(initAddr.getScopeId(), desrAddr.getScopeId());
  744. assertEquals(initAddr.getScopedInterface(), desrAddr
  745. .getScopedInterface());
  746. }
  747. };
  748. /**
  749. * Tests serialization/deserialization compatibility with ourselves.
  750. */
  751. public void testSerializationSelf() throws Exception {
  752. byte[] localv6 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
  753. SerializationTest.verifySelf(InetAddress.getByAddress(localv6),
  754. COMPARATOR);
  755. }
  756. /**
  757. * Tests serialization/deserialization compatibility with RI.
  758. */
  759. public void testSerializationCompatibility() throws Exception {
  760. byte[] localv6 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
  761. Object[] addresses = { InetAddress.getByAddress(localv6),
  762. // Regression for Harmony-1039: ser-form has
  763. // null interface name
  764. InetAddress.getByAddress(localv6) };
  765. SerializationTest.verifyGolden(this, addresses, COMPARATOR);
  766. }
  767. }