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

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