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

https://gitlab.com/brian0218/rk3188_r-box_android4.2.2_sdk · Java · 346 lines · 218 code · 55 blank · 73 comment · 1 complexity · d6f48543561aa2834fef26084b05f0f8 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.Inet4Address;
  20. import java.net.InetAddress;
  21. import org.apache.harmony.testframework.serialization.SerializationTest;
  22. import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
  23. public class Inet4AddressTest extends junit.framework.TestCase {
  24. /**
  25. * @tests java.net.Inet4Address#isMulticastAddress()
  26. */
  27. public void test_isMulticastAddress() throws Exception {
  28. // Create 2 IP v4 addresses and call "isMulticastAddress()"
  29. // result should return true if the first 4 bits of the
  30. // address are: 1110, false otherwise
  31. // Make 1 address with 1110, and 1 without
  32. String addrName = "";
  33. addrName = "224.0.0.0"; // a multicast addr 1110 = 224-239
  34. InetAddress addr = InetAddress.getByName(addrName);
  35. assertTrue("Multicast address " + addrName + " not detected.", addr
  36. .isMulticastAddress());
  37. addrName = "239.255.255.255"; // a multicast addr 1110 = 224-239
  38. addr = InetAddress.getByName(addrName);
  39. assertTrue("Multicast address " + addrName + " not detected.", addr
  40. .isMulticastAddress());
  41. addrName = "42.42.42.42"; // a non-multicast address
  42. addr = InetAddress.getByName(addrName);
  43. assertTrue("Non multicast address " + addrName
  44. + " reporting as a multicast address.", !addr
  45. .isMulticastAddress());
  46. }
  47. public void test_isAnyLocalAddress() throws Exception {
  48. assertTrue(InetAddress.getByName("0.0.0.0").isAnyLocalAddress());
  49. assertFalse(InetAddress.getByName("127.0.0.1").isAnyLocalAddress());
  50. }
  51. public void test_isLoopbackAddress() throws Exception {
  52. // Create some IP V4 addresses and test if they are local...
  53. String addrName = "";
  54. addrName = "127.0.0.0"; // a loopback address should be 127.d.d.d
  55. InetAddress addr = InetAddress.getByName(addrName);
  56. assertTrue("Loopback address " + addrName + " not detected.", addr
  57. .isLoopbackAddress());
  58. addrName = "127.42.42.42"; // a loopback address should be
  59. // 127.d.d.d
  60. addr = InetAddress.getByName(addrName);
  61. assertTrue("Loopback address " + addrName + " not detected.", addr
  62. .isLoopbackAddress());
  63. addrName = "42.42.42.42"; // a loopback address should be
  64. // 127.d.d.d
  65. addr = InetAddress.getByName(addrName);
  66. assertTrue("Address incorrectly " + addrName
  67. + " detected as a loopback address.", !addr
  68. .isLoopbackAddress());
  69. }
  70. /**
  71. * @tests java.net.Inet4Address#isLinkLocalAddress()
  72. */
  73. public void test_isLinkLocalAddress() throws Exception {
  74. String addrName = "";
  75. // There are no link local addresses for IPv4
  76. // We'll test one to ensure we get "false"
  77. addrName = "42.42.42.42";
  78. InetAddress addr = InetAddress.getByName(addrName);
  79. assertTrue("IPv4 address " + addrName
  80. + " incorrectly reporting as a link local address.", !addr
  81. .isLinkLocalAddress());
  82. }
  83. /**
  84. * @tests java.net.Inet4Address#isSiteLocalAddress()
  85. */
  86. public void test_isSiteLocalAddress() throws Exception {
  87. String addrName = "";
  88. // There are no site local addresses for IPv4
  89. // We'll test one to ensure we get "false"
  90. addrName = "42.42.42.42";
  91. InetAddress addr = InetAddress.getByName(addrName);
  92. assertTrue("IPv4 address " + addrName
  93. + " incorrectly reporting as a site local address.", !addr
  94. .isSiteLocalAddress());
  95. }
  96. /**
  97. * @tests java.net.Inet4Address#isMCGlobal()
  98. */
  99. public void test_isMCGlobal() throws Exception {
  100. // Create an IPv4 mulitcast address. It should return
  101. // false for globabl mutlicast. There are no valid IPv4
  102. // global multicast addresses
  103. String addrName = "";
  104. addrName = "224.0.0.0"; // a multicast addr 1110
  105. InetAddress addr = InetAddress.getByName(addrName);
  106. assertTrue("IPv4 link-local multicast address " + addrName
  107. + " incorrectly identified as a global multicast address.",
  108. !addr.isMCGlobal());
  109. addrName = "224.0.0.255"; // a multicast addr 1110
  110. addr = InetAddress.getByName(addrName);
  111. assertTrue("IPv4 link-local multicast address " + addrName
  112. + " incorrectly identified as a global multicast address.",
  113. !addr.isMCGlobal());
  114. addrName = "224.0.1.0"; // a multicast addr 1110
  115. addr = InetAddress.getByName(addrName);
  116. assertTrue("IPv4 global multicast address " + addrName
  117. + " not identified as a global multicast address.", addr
  118. .isMCGlobal());
  119. addrName = "238.255.255.255"; // a multicast addr 1110
  120. addr = InetAddress.getByName(addrName);
  121. assertTrue("IPv4 global multicast address " + addrName
  122. + " not identified as a global multicast address.", addr
  123. .isMCGlobal());
  124. addrName = "239.0.0.0"; // a multicast addr 1110
  125. addr = InetAddress.getByName(addrName);
  126. assertTrue("IPv4 reserved multicast address " + addrName
  127. + " incorrectly identified as a global multicast address.",
  128. !addr.isMCGlobal());
  129. addrName = "239.191.255.255"; // a multicast addr 1110
  130. addr = InetAddress.getByName(addrName);
  131. assertTrue("IPv4 reserved multicast address " + addrName
  132. + " incorrectly identified as a global multicast address.",
  133. !addr.isMCGlobal());
  134. }
  135. /**
  136. * @tests java.net.Inet4Address#isMCNodeLocal()
  137. */
  138. public void test_isMCNodeLocal() throws Exception {
  139. // Create an IPv4 mulitcast address. It should return
  140. // false for node-local mutlicast. There are no valid IPv4
  141. // node-local multicast addresses
  142. String addrName = "";
  143. addrName = "224.42.42.42"; // a multicast addr 1110 = 224
  144. InetAddress addr = InetAddress.getByName(addrName);
  145. assertTrue(
  146. "IPv4 multicast address "
  147. + addrName
  148. + " incorrectly identified as a node-local multicast address.",
  149. !addr.isMCNodeLocal());
  150. addrName = "239.0.0.0"; // a multicast addr 1110
  151. addr = InetAddress.getByName(addrName);
  152. assertTrue(
  153. "IPv4 reserved multicast address "
  154. + addrName
  155. + " incorrectly identified as a node-local multicast address.",
  156. !addr.isMCNodeLocal());
  157. }
  158. /**
  159. * @tests java.net.Inet4Address#isMCLinkLocal()
  160. */
  161. public void test_isMCLinkLocal() throws Exception {
  162. // Create an IPv4 mulitcast address. It should return
  163. // false for link-local mutlicast. There are no valid IPv4
  164. // link-local multicast addresses
  165. String addrName = "";
  166. addrName = "224.0.0.0"; // a multicast addr 1110
  167. InetAddress addr = InetAddress.getByName(addrName);
  168. assertTrue("IPv4 link-local multicast address " + addrName
  169. + " not identified as a link-local multicast address.",
  170. addr.isMCLinkLocal());
  171. addrName = "224.0.0.255"; // a multicast addr 1110
  172. addr = InetAddress.getByName(addrName);
  173. assertTrue("IPv4 link-local multicast address " + addrName
  174. + " not identified as a link-local multicast address.",
  175. addr.isMCLinkLocal());
  176. addrName = "224.0.1.0"; // a multicast addr 1110
  177. addr = InetAddress.getByName(addrName);
  178. assertTrue(
  179. "IPv4 global multicast address "
  180. + addrName
  181. + " incorrectly identified as a link-local multicast address.",
  182. !addr.isMCLinkLocal());
  183. addrName = "239.0.0.0"; // a multicast addr 1110
  184. addr = InetAddress.getByName(addrName);
  185. assertTrue(
  186. "IPv4 reserved multicast address "
  187. + addrName
  188. + " incorrectly identified as a link-local multicast address.",
  189. !addr.isMCLinkLocal());
  190. }
  191. /**
  192. * @tests java.net.Inet4Address#isMCSiteLocal()
  193. */
  194. public void test_isMCSiteLocal() throws Exception {
  195. // Create an IPv4 mulitcast address. It should return
  196. // false for site-local mutlicast. There are no valid IPv4
  197. // site-local multicast addresses
  198. String addrName = "";
  199. addrName = "240.0.0.0"; // a multicast addr 1110 = 224
  200. InetAddress addr = InetAddress.getByName(addrName);
  201. assertTrue(
  202. "IPv4 multicast address "
  203. + addrName
  204. + " incorrectly identified as a site-local multicast address.",
  205. !addr.isMCSiteLocal());
  206. addrName = "239.0.0.0"; // a multicast addr 1110
  207. addr = InetAddress.getByName(addrName);
  208. assertTrue(
  209. "IPv4 reserved multicast address "
  210. + addrName
  211. + " incorrectly identified as a site-local multicast address.",
  212. !addr.isMCSiteLocal());
  213. addrName = "239.255.0.0"; // a multicast addr 1110
  214. addr = InetAddress.getByName(addrName);
  215. assertTrue("IPv4 site-local multicast address " + addrName
  216. + " not identified as a site-local multicast address.",
  217. addr.isMCSiteLocal());
  218. addrName = "239.255.255.255"; // a multicast addr 1110
  219. addr = InetAddress.getByName(addrName);
  220. assertTrue("IPv4 site-local multicast address " + addrName
  221. + " not identified as a site-local multicast address.",
  222. addr.isMCSiteLocal());
  223. addrName = "239.255.2.2"; // a multicast addr 1110
  224. addr = InetAddress.getByName(addrName);
  225. assertTrue("IPv4 site-local multicast address " + addrName
  226. + " not identified as a site-local multicast address.",
  227. addr.isMCSiteLocal());
  228. }
  229. /**
  230. * @tests java.net.Inet4Address#isMCOrgLocal()
  231. */
  232. public void test_isMCOrgLocal() throws Exception {
  233. // Create an IPv4 mulitcast address. It should return
  234. // false for organization-local mutlicast. There are no valid IPv4
  235. // organization-local multicast addresses
  236. String addrName = "";
  237. addrName = "239.191.255.255"; // a multicast addr 1110
  238. InetAddress addr = InetAddress.getByName(addrName);
  239. assertTrue(
  240. "IPv4 reserved multicast address "
  241. + addrName
  242. + " incorrectly identified as a org-local multicast address.",
  243. !addr.isMCOrgLocal());
  244. addrName = "239.252.0.0"; // a multicast addr 1110
  245. addr = InetAddress.getByName(addrName);
  246. assertTrue(
  247. "IPv4 site-local multicast address "
  248. + addrName
  249. + " incorrectly identified as a org-local multicast address.",
  250. !addr.isMCOrgLocal());
  251. addrName = "239.192.0.0"; // a multicast addr 1110
  252. addr = InetAddress.getByName(addrName);
  253. assertTrue("IPv4 org-local multicast address " + addrName
  254. + " not identified as a org-local multicast address.", addr
  255. .isMCOrgLocal());
  256. addrName = "239.195.255.255"; // a multicast addr 1110
  257. addr = InetAddress.getByName(addrName);
  258. assertTrue("IPv4 org-local multicast address " + addrName
  259. + " not identified as a org-local multicast address.", addr
  260. .isMCOrgLocal());
  261. }
  262. // comparator for Inet4Address objects
  263. private static final SerializableAssert COMPARATOR = new SerializableAssert() {
  264. public void assertDeserialized(Serializable initial,
  265. Serializable deserialized) {
  266. Inet4Address initAddr = (Inet4Address) initial;
  267. Inet4Address desrAddr = (Inet4Address) deserialized;
  268. byte[] iaAddresss = initAddr.getAddress();
  269. byte[] deIAAddresss = desrAddr.getAddress();
  270. for (int i = 0; i < iaAddresss.length; i++) {
  271. assertEquals(iaAddresss[i], deIAAddresss[i]);
  272. }
  273. assertEquals(4, deIAAddresss.length);
  274. assertEquals(initAddr.getHostName(), desrAddr.getHostName());
  275. }
  276. };
  277. /**
  278. * @tests serialization/deserialization compatibility.
  279. */
  280. public void testSerializationSelf() throws Exception {
  281. SerializationTest.verifySelf(Inet4Address.getByName("localhost"),
  282. COMPARATOR);
  283. }
  284. /**
  285. * @tests serialization/deserialization compatibility with RI.
  286. */
  287. public void testSerializationCompatibility() throws Exception {
  288. SerializationTest.verifyGolden(this, Inet4Address
  289. .getByName("localhost"), COMPARATOR);
  290. }
  291. }