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

https://gitlab.com/brian0218/rk3188_rk3066_r-box_android4.4.2_sdk · Java · 502 lines · 353 code · 48 blank · 101 comment · 83 complexity · 071a066fe649a58eede830bcc079bb13 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.net.InetAddress;
  19. import java.net.InterfaceAddress;
  20. import java.net.NetworkInterface;
  21. import java.net.SocketException;
  22. import java.util.ArrayList;
  23. import java.util.Enumeration;
  24. import java.util.List;
  25. public class NetworkInterfaceTest extends junit.framework.TestCase {
  26. // private member variables used for tests
  27. Enumeration<NetworkInterface> theInterfaces = null;
  28. boolean atLeastOneInterface = false;
  29. boolean atLeastTwoInterfaces = false;
  30. private NetworkInterface networkInterface1 = null;
  31. private NetworkInterface sameAsNetworkInterface1 = null;
  32. private NetworkInterface networkInterface2 = null;
  33. /**
  34. * @tests java.net.NetworkInterface#getName()
  35. */
  36. public void test_getName() {
  37. if (atLeastOneInterface) {
  38. assertNotNull("validate that non null name is returned",
  39. networkInterface1.getName());
  40. assertFalse("validate that non-zero length name is generated",
  41. networkInterface1.getName().equals(""));
  42. }
  43. if (atLeastTwoInterfaces) {
  44. assertFalse(
  45. "Validate strings are different for different interfaces",
  46. networkInterface1.getName().equals(
  47. networkInterface2.getName()));
  48. }
  49. }
  50. /**
  51. * @tests java.net.NetworkInterface#getInetAddresses()
  52. */
  53. public void test_getInetAddresses() throws Exception {
  54. if (atLeastOneInterface) {
  55. Enumeration theAddresses = networkInterface1.getInetAddresses();
  56. while (theAddresses.hasMoreElements()) {
  57. InetAddress theAddress = (InetAddress) theAddresses
  58. .nextElement();
  59. assertNotNull("validate that address is not null", theAddress);
  60. }
  61. }
  62. if (atLeastTwoInterfaces) {
  63. Enumeration theAddresses = networkInterface2.getInetAddresses();
  64. while (theAddresses.hasMoreElements()) {
  65. InetAddress theAddress = (InetAddress) theAddresses
  66. .nextElement();
  67. assertNotNull("validate that address is not null", theAddress);
  68. }
  69. }
  70. // create the list of ok and not ok addresses to return
  71. if (atLeastOneInterface) {
  72. ArrayList okAddresses = new ArrayList();
  73. Enumeration addresses = networkInterface1.getInetAddresses();
  74. int index = 0;
  75. ArrayList notOkAddresses = new ArrayList();
  76. while (addresses.hasMoreElements()) {
  77. InetAddress theAddress = (InetAddress) addresses.nextElement();
  78. okAddresses.add(theAddress);
  79. index++;
  80. }
  81. // do the same for network interface 2 if it exists
  82. if (atLeastTwoInterfaces) {
  83. addresses = networkInterface2.getInetAddresses();
  84. index = 0;
  85. while (addresses.hasMoreElements()) {
  86. InetAddress theAddress = (InetAddress) addresses
  87. .nextElement();
  88. okAddresses.add(theAddress);
  89. index++;
  90. }
  91. }
  92. // validate not ok addresses are not returned
  93. for (int i = 0; i < notOkAddresses.size(); i++) {
  94. Enumeration reducedAddresses = networkInterface1
  95. .getInetAddresses();
  96. while (reducedAddresses.hasMoreElements()) {
  97. InetAddress nextAddress = (InetAddress) reducedAddresses
  98. .nextElement();
  99. assertTrue(
  100. "validate that address without permission is not returned",
  101. !nextAddress.equals(notOkAddresses.get(i)));
  102. }
  103. if (atLeastTwoInterfaces) {
  104. reducedAddresses = networkInterface2.getInetAddresses();
  105. while (reducedAddresses.hasMoreElements()) {
  106. InetAddress nextAddress = (InetAddress) reducedAddresses
  107. .nextElement();
  108. assertTrue(
  109. "validate that address without permission is not returned",
  110. !nextAddress.equals(notOkAddresses.get(i)));
  111. }
  112. }
  113. }
  114. // validate that ok addresses are returned
  115. for (int i = 0; i < okAddresses.size(); i++) {
  116. boolean addressReturned = false;
  117. Enumeration reducedAddresses = networkInterface1
  118. .getInetAddresses();
  119. while (reducedAddresses.hasMoreElements()) {
  120. InetAddress nextAddress = (InetAddress) reducedAddresses
  121. .nextElement();
  122. if (nextAddress.equals(okAddresses.get(i))) {
  123. addressReturned = true;
  124. }
  125. }
  126. if (atLeastTwoInterfaces) {
  127. reducedAddresses = networkInterface2.getInetAddresses();
  128. while (reducedAddresses.hasMoreElements()) {
  129. InetAddress nextAddress = (InetAddress) reducedAddresses
  130. .nextElement();
  131. if (nextAddress.equals(okAddresses.get(i))) {
  132. addressReturned = true;
  133. }
  134. }
  135. }
  136. assertTrue("validate that address with permission is returned",
  137. addressReturned);
  138. }
  139. // validate that we can get the interface by specifying the address.
  140. // This is to be compatible
  141. for (int i = 0; i < notOkAddresses.size(); i++) {
  142. assertNotNull(
  143. "validate we cannot get the NetworkInterface with an address for which we have no privs",
  144. NetworkInterface
  145. .getByInetAddress((InetAddress) notOkAddresses
  146. .get(i)));
  147. }
  148. // validate that we can get the network interface for the good
  149. // addresses
  150. for (int i = 0; i < okAddresses.size(); i++) {
  151. assertNotNull(
  152. "validate we cannot get the NetworkInterface with an address fro which we have no privs",
  153. NetworkInterface
  154. .getByInetAddress((InetAddress) okAddresses
  155. .get(i)));
  156. }
  157. }
  158. }
  159. /**
  160. * @tests java.net.NetworkInterface#getDisplayName()
  161. */
  162. public void test_getDisplayName() {
  163. if (atLeastOneInterface) {
  164. assertNotNull("validate that non null display name is returned",
  165. networkInterface1.getDisplayName());
  166. assertFalse(
  167. "validate that non-zero length display name is generated",
  168. networkInterface1.getDisplayName().equals(""));
  169. }
  170. if (atLeastTwoInterfaces) {
  171. assertFalse(
  172. "Validate strings are different for different interfaces",
  173. networkInterface1.getDisplayName().equals(
  174. networkInterface2.getDisplayName()));
  175. }
  176. }
  177. /**
  178. * @tests java.net.NetworkInterface#getByName(java.lang.String)
  179. */
  180. public void test_getByNameLjava_lang_String() throws Exception {
  181. try {
  182. assertNull("validate null handled ok",
  183. NetworkInterface.getByName(null));
  184. fail("getByName did not throw NullPointerException for null argument");
  185. } catch (NullPointerException e) {
  186. }
  187. assertNull("validate handled ok if we ask for name not associated with any interface",
  188. NetworkInterface.getByName("8not a name4"));
  189. // for each address in an interface validate that we get the right
  190. // interface for that name
  191. if (atLeastOneInterface) {
  192. String theName = networkInterface1.getName();
  193. if (theName != null) {
  194. assertEquals(
  195. "validate that Interface can be obtained with its name",
  196. networkInterface1, NetworkInterface.getByName(theName));
  197. }
  198. }
  199. // validate that we get the right interface with the second interface as
  200. // well (ie we just don't always get the first interface
  201. if (atLeastTwoInterfaces) {
  202. String theName = networkInterface2.getName();
  203. if (theName != null) {
  204. assertEquals(
  205. "validate that Interface can be obtained with its name",
  206. networkInterface2, NetworkInterface.getByName(theName));
  207. }
  208. }
  209. }
  210. /**
  211. * @tests java.net.NetworkInterface#getByInetAddress(java.net.InetAddress)
  212. */
  213. public void test_getByInetAddressLjava_net_InetAddress() throws Exception {
  214. byte addressBytes[] = new byte[4];
  215. addressBytes[0] = 0;
  216. addressBytes[1] = 0;
  217. addressBytes[2] = 0;
  218. addressBytes[3] = 0;
  219. try {
  220. assertNull("validate null handled ok",
  221. NetworkInterface.getByInetAddress(null));
  222. fail("should not get here if getByInetAddress throws "
  223. + "NullPointerException if null passed in");
  224. } catch (NullPointerException e) {
  225. }
  226. assertNull("validate handled ok if we ask for address not associated with any interface",
  227. NetworkInterface.getByInetAddress(InetAddress
  228. .getByAddress(addressBytes)));
  229. // for each address in an interface validate that we get the right
  230. // interface for that address
  231. if (atLeastOneInterface) {
  232. Enumeration addresses = networkInterface1.getInetAddresses();
  233. while (addresses.hasMoreElements()) {
  234. InetAddress theAddress = (InetAddress) addresses.nextElement();
  235. assertEquals(
  236. "validate that Interface can be obtained with any one of its addresses",
  237. networkInterface1, NetworkInterface
  238. .getByInetAddress(theAddress));
  239. }
  240. }
  241. // validate that we get the right interface with the second interface as
  242. // well (ie we just don't always get the first interface
  243. if (atLeastTwoInterfaces) {
  244. Enumeration addresses = networkInterface2.getInetAddresses();
  245. while (addresses.hasMoreElements()) {
  246. InetAddress theAddress = (InetAddress) addresses.nextElement();
  247. assertEquals(
  248. "validate that Interface can be obtained with any one of its addresses",
  249. networkInterface2, NetworkInterface
  250. .getByInetAddress(theAddress));
  251. }
  252. }
  253. }
  254. /**
  255. * @tests java.net.NetworkInterface#getNetworkInterfaces()
  256. */
  257. public void test_getNetworkInterfaces() throws Exception {
  258. // really this is tested by all of the other calls but just make sure we
  259. // can call it and get a list of interfaces if they exist
  260. Enumeration theInterfaces = NetworkInterface.getNetworkInterfaces();
  261. }
  262. /**
  263. * @tests java.net.NetworkInterface#equals(java.lang.Object)
  264. */
  265. public void test_equalsLjava_lang_Object() {
  266. // Test for method boolean
  267. // java.net.SocketPermission.equals(java.lang.Object)
  268. if (atLeastOneInterface) {
  269. assertEquals("If objects are the same true is returned",
  270. sameAsNetworkInterface1, networkInterface1);
  271. assertNotNull("Validate Null handled ok", networkInterface1);
  272. }
  273. if (atLeastTwoInterfaces) {
  274. assertFalse("If objects are different false is returned",
  275. networkInterface1.equals(networkInterface2));
  276. }
  277. }
  278. /**
  279. * @tests java.net.NetworkInterface#hashCode()
  280. */
  281. public void test_hashCode() {
  282. if (atLeastOneInterface) {
  283. assertTrue(
  284. "validate that hash codes are the same for two calls on the same object",
  285. networkInterface1.hashCode() == networkInterface1
  286. .hashCode());
  287. assertTrue(
  288. "validate that hash codes are the same for two objects for which equals is true",
  289. networkInterface1.hashCode() == sameAsNetworkInterface1
  290. .hashCode());
  291. }
  292. }
  293. /**
  294. * @tests java.net.NetworkInterface#toString()
  295. */
  296. public void test_toString() {
  297. if (atLeastOneInterface) {
  298. assertNotNull("validate that non null string is generated",
  299. networkInterface1.toString());
  300. assertFalse("validate that non-zero length string is generated",
  301. networkInterface1.toString().equals(""));
  302. }
  303. if (atLeastTwoInterfaces) {
  304. assertFalse(
  305. "Validate strings are different for different interfaces",
  306. networkInterface1.toString().equals(
  307. networkInterface2.toString()));
  308. }
  309. }
  310. /**
  311. *
  312. * @tests java.net.NetworkInterface#getInterfaceAddresses()
  313. *
  314. * @since 1.6
  315. */
  316. public void test_getInterfaceAddresses() throws SocketException {
  317. if (theInterfaces != null) {
  318. while (theInterfaces.hasMoreElements()) {
  319. NetworkInterface netif = theInterfaces.nextElement();
  320. assertEquals(netif.getName()
  321. + " getInterfaceAddresses should contain no element", 0,
  322. netif.getInterfaceAddresses().size());
  323. }
  324. theInterfaces = NetworkInterface.getNetworkInterfaces();
  325. while (theInterfaces.hasMoreElements()) {
  326. NetworkInterface netif = theInterfaces.nextElement();
  327. List<InterfaceAddress> interfaceAddrs = netif.getInterfaceAddresses();
  328. assertTrue(interfaceAddrs instanceof ArrayList);
  329. for (InterfaceAddress addr : interfaceAddrs) {
  330. assertNotNull(addr);
  331. }
  332. List<InterfaceAddress> interfaceAddrs2 = netif.getInterfaceAddresses();
  333. // RI fails on this since it cannot tolerate null broadcast address.
  334. assertEquals(interfaceAddrs, interfaceAddrs2);
  335. }
  336. }
  337. }
  338. /**
  339. * @tests java.net.NetworkInterface#isLoopback()
  340. *
  341. * @since 1.6
  342. */
  343. public void test_isLoopback() throws SocketException {
  344. if (theInterfaces != null) {
  345. while (theInterfaces.hasMoreElements()) {
  346. NetworkInterface netif = theInterfaces.nextElement();
  347. boolean loopback = false;
  348. Enumeration<InetAddress> addrs = netif.getInetAddresses();
  349. while(addrs != null && addrs.hasMoreElements()){
  350. if(addrs.nextElement().isLoopbackAddress()){
  351. loopback = true;
  352. break;
  353. }
  354. }
  355. assertEquals(loopback, netif.isLoopback());
  356. }
  357. }
  358. }
  359. /**
  360. * @tests java.net.NetworkInterface#getHardwareAddress()
  361. *
  362. * @since 1.6
  363. */
  364. public void test_getHardwareAddress() throws SocketException {
  365. if (theInterfaces != null) {
  366. while (theInterfaces.hasMoreElements()) {
  367. NetworkInterface netif = theInterfaces.nextElement();
  368. byte[] hwAddr = netif.getHardwareAddress();
  369. if (netif.isLoopback()) {
  370. assertTrue(hwAddr == null || hwAddr.length == 0);
  371. } else {
  372. assertTrue(hwAddr.length >= 0);
  373. }
  374. }
  375. }
  376. }
  377. /**
  378. *
  379. * @tests java.net.NetworkInterface#getHardwareAddress()
  380. *
  381. * @since 1.6
  382. */
  383. public void test_getMTU() throws SocketException {
  384. if (theInterfaces != null) {
  385. while (theInterfaces.hasMoreElements()) {
  386. NetworkInterface netif = theInterfaces.nextElement();
  387. assertTrue(netif.getName() + "has non-positive MTU", netif.getMTU() >= 0);
  388. }
  389. }
  390. }
  391. protected void setUp() throws SocketException {
  392. Enumeration theInterfaces = null;
  393. try {
  394. theInterfaces = NetworkInterface.getNetworkInterfaces();
  395. } catch (Exception e) {
  396. fail("Exception occurred getting network interfaces : " + e);
  397. }
  398. // Set up NetworkInterface instance members. Note that because the call
  399. // to NetworkInterface.getNetworkInterfaces() returns *all* of the
  400. // interfaces on the test machine it is possible that one or more of
  401. // them will not currently be bound to an InetAddress. e.g. a laptop
  402. // running connected by a wire to the local network may also have a
  403. // wireless interface that is not active and so has no InetAddress
  404. // bound to it. For these tests only work with NetworkInterface objects
  405. // that are bound to an InetAddress.
  406. if ((theInterfaces != null) && (theInterfaces.hasMoreElements())) {
  407. while ((theInterfaces.hasMoreElements())
  408. && (atLeastOneInterface == false)) {
  409. NetworkInterface theInterface = (NetworkInterface) theInterfaces
  410. .nextElement();
  411. if (theInterface.getInetAddresses().hasMoreElements()) {
  412. // Ensure that the current NetworkInterface has at least
  413. // one InetAddress bound to it.
  414. Enumeration addrs = theInterface.getInetAddresses();
  415. if ((addrs != null) && (addrs.hasMoreElements())) {
  416. atLeastOneInterface = true;
  417. networkInterface1 = theInterface;
  418. }// end if
  419. }
  420. }
  421. while ((theInterfaces.hasMoreElements())
  422. && (atLeastTwoInterfaces == false)) {
  423. NetworkInterface theInterface = (NetworkInterface) theInterfaces
  424. .nextElement();
  425. if (theInterface.getInetAddresses().hasMoreElements()) {
  426. // Ensure that the current NetworkInterface has at least
  427. // one InetAddress bound to it.
  428. Enumeration addrs = theInterface.getInetAddresses();
  429. if ((addrs != null) && (addrs.hasMoreElements())) {
  430. atLeastTwoInterfaces = true;
  431. networkInterface2 = theInterface;
  432. }// end if
  433. }
  434. }
  435. // Only set sameAsNetworkInterface1 if we succeeded in finding
  436. // at least one good NetworkInterface
  437. if (atLeastOneInterface) {
  438. Enumeration addresses = networkInterface1.getInetAddresses();
  439. if (addresses.hasMoreElements()) {
  440. try {
  441. if (addresses.hasMoreElements()) {
  442. sameAsNetworkInterface1 = NetworkInterface
  443. .getByInetAddress((InetAddress) addresses
  444. .nextElement());
  445. }
  446. } catch (SocketException e) {
  447. fail("SocketException occurred : " + e);
  448. }
  449. }
  450. }// end if atLeastOneInterface
  451. }
  452. theInterfaces = NetworkInterface.getNetworkInterfaces();
  453. }
  454. }