/harmony-tests/src/test/java/org/apache/harmony/tests/java/net/NetworkInterfaceTest.java

https://gitlab.com/Atomic-ROM/libcore · Java · 496 lines · 353 code · 48 blank · 95 comment · 84 complexity · cfab534d68c063d555d96978ccef5f45 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.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. * 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. * 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. * 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. * 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. * 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. * 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. * 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. * 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. * 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. * java.net.NetworkInterface#getInterfaceAddresses()
  312. * @since 1.6
  313. */
  314. public void test_getInterfaceAddresses() throws SocketException {
  315. if (theInterfaces != null) {
  316. while (theInterfaces.hasMoreElements()) {
  317. NetworkInterface netif = theInterfaces.nextElement();
  318. assertEquals(netif.getName()
  319. + " getInterfaceAddresses should contain no element", 0,
  320. netif.getInterfaceAddresses().size());
  321. }
  322. theInterfaces = NetworkInterface.getNetworkInterfaces();
  323. while (theInterfaces.hasMoreElements()) {
  324. NetworkInterface netif = theInterfaces.nextElement();
  325. List<InterfaceAddress> interfaceAddrs = netif.getInterfaceAddresses();
  326. assertTrue(interfaceAddrs instanceof ArrayList);
  327. for (InterfaceAddress addr : interfaceAddrs) {
  328. assertNotNull(addr);
  329. }
  330. List<InterfaceAddress> interfaceAddrs2 = netif.getInterfaceAddresses();
  331. // RI fails on this since it cannot tolerate null broadcast address.
  332. assertEquals(interfaceAddrs, interfaceAddrs2);
  333. }
  334. }
  335. }
  336. /**
  337. * java.net.NetworkInterface#isLoopback()
  338. * @since 1.6
  339. */
  340. public void test_isLoopback() throws SocketException {
  341. if (theInterfaces != null) {
  342. while (theInterfaces.hasMoreElements()) {
  343. NetworkInterface netif = theInterfaces.nextElement();
  344. boolean loopback = false;
  345. Enumeration<InetAddress> addrs = netif.getInetAddresses();
  346. while (addrs != null && addrs.hasMoreElements()) {
  347. if (addrs.nextElement().isLoopbackAddress()) {
  348. loopback = true;
  349. break;
  350. }
  351. }
  352. assertEquals(loopback, netif.isLoopback());
  353. }
  354. }
  355. }
  356. /**
  357. * java.net.NetworkInterface#getHardwareAddress()
  358. * @since 1.6
  359. */
  360. public void test_getHardwareAddress() throws SocketException {
  361. if (theInterfaces != null) {
  362. while (theInterfaces.hasMoreElements()) {
  363. NetworkInterface netif = theInterfaces.nextElement();
  364. byte[] hwAddr = netif.getHardwareAddress();
  365. if (netif.isLoopback()) {
  366. assertTrue(hwAddr == null || hwAddr.length == 0);
  367. } else {
  368. assertTrue(hwAddr.length >= 0);
  369. }
  370. }
  371. }
  372. }
  373. /**
  374. * java.net.NetworkInterface#getHardwareAddress()
  375. * @since 1.6
  376. */
  377. public void test_getMTU() throws SocketException {
  378. if (theInterfaces != null) {
  379. while (theInterfaces.hasMoreElements()) {
  380. NetworkInterface netif = theInterfaces.nextElement();
  381. assertTrue(netif.getName() + "has non-positive MTU", netif.getMTU() >= 0);
  382. }
  383. }
  384. }
  385. protected void setUp() throws SocketException {
  386. Enumeration theInterfaces = null;
  387. try {
  388. theInterfaces = NetworkInterface.getNetworkInterfaces();
  389. } catch (Exception e) {
  390. fail("Exception occurred getting network interfaces : " + e);
  391. }
  392. // Set up NetworkInterface instance members. Note that because the call
  393. // to NetworkInterface.getNetworkInterfaces() returns *all* of the
  394. // interfaces on the test machine it is possible that one or more of
  395. // them will not currently be bound to an InetAddress. e.g. a laptop
  396. // running connected by a wire to the local network may also have a
  397. // wireless interface that is not active and so has no InetAddress
  398. // bound to it. For these tests only work with NetworkInterface objects
  399. // that are bound to an InetAddress.
  400. if ((theInterfaces != null) && (theInterfaces.hasMoreElements())) {
  401. while ((theInterfaces.hasMoreElements())
  402. && (atLeastOneInterface == false)) {
  403. NetworkInterface theInterface = (NetworkInterface) theInterfaces
  404. .nextElement();
  405. if (theInterface.getInetAddresses().hasMoreElements()) {
  406. // Ensure that the current NetworkInterface has at least
  407. // one InetAddress bound to it.
  408. Enumeration addrs = theInterface.getInetAddresses();
  409. if ((addrs != null) && (addrs.hasMoreElements())) {
  410. atLeastOneInterface = true;
  411. networkInterface1 = theInterface;
  412. }// end if
  413. }
  414. }
  415. while ((theInterfaces.hasMoreElements())
  416. && (atLeastTwoInterfaces == false)) {
  417. NetworkInterface theInterface = (NetworkInterface) theInterfaces
  418. .nextElement();
  419. if (theInterface.getInetAddresses().hasMoreElements()) {
  420. // Ensure that the current NetworkInterface has at least
  421. // one InetAddress bound to it.
  422. Enumeration addrs = theInterface.getInetAddresses();
  423. if ((addrs != null) && (addrs.hasMoreElements())) {
  424. atLeastTwoInterfaces = true;
  425. networkInterface2 = theInterface;
  426. }// end if
  427. }
  428. }
  429. // Only set sameAsNetworkInterface1 if we succeeded in finding
  430. // at least one good NetworkInterface
  431. if (atLeastOneInterface) {
  432. Enumeration addresses = networkInterface1.getInetAddresses();
  433. if (addresses.hasMoreElements()) {
  434. try {
  435. if (addresses.hasMoreElements()) {
  436. sameAsNetworkInterface1 = NetworkInterface
  437. .getByInetAddress((InetAddress) addresses
  438. .nextElement());
  439. }
  440. } catch (SocketException e) {
  441. fail("SocketException occurred : " + e);
  442. }
  443. }
  444. }// end if atLeastOneInterface
  445. }
  446. theInterfaces = NetworkInterface.getNetworkInterfaces();
  447. }
  448. }