|
| 1 | +package com.baeldung.java.networking.interfaces; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertFalse; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | + |
| 7 | +import java.net.InetAddress; |
| 8 | +import java.net.InterfaceAddress; |
| 9 | +import java.net.NetworkInterface; |
| 10 | +import java.net.SocketException; |
| 11 | +import java.net.UnknownHostException; |
| 12 | +import java.util.Enumeration; |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +import org.junit.Test; |
| 16 | + |
| 17 | +public class NetworkInterfaceTest { |
| 18 | + @Test |
| 19 | + public void givenName_whenReturnsNetworkInterface_thenCorrect() throws SocketException { |
| 20 | + NetworkInterface nif = NetworkInterface.getByName("lo"); |
| 21 | + assertFalse(nif == null); |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + public void givenInExistentName_whenReturnsNull_thenCorrect() throws SocketException { |
| 26 | + NetworkInterface nif = NetworkInterface.getByName("inexistent_name"); |
| 27 | + assertTrue(nif == null); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void givenIP_whenReturnsNetworkInterface_thenCorrect() throws SocketException, UnknownHostException { |
| 32 | + byte[] ip = new byte[] { 127, 0, 0, 1 }; |
| 33 | + NetworkInterface nif = NetworkInterface.getByInetAddress(InetAddress.getByAddress(ip)); |
| 34 | + assertFalse(nif == null); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void givenHostName_whenReturnsNetworkInterface_thenCorrect() throws SocketException, UnknownHostException { |
| 39 | + NetworkInterface nif = NetworkInterface.getByInetAddress(InetAddress.getByName("localhost")); |
| 40 | + assertFalse(nif == null); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void givenLocalHost_whenReturnsNetworkInterface_thenCorrect() throws SocketException, UnknownHostException { |
| 45 | + NetworkInterface nif = NetworkInterface.getByInetAddress(InetAddress.getLocalHost()); |
| 46 | + assertFalse(nif == null); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void givenLoopBack_whenReturnsNetworkInterface_thenCorrect() throws SocketException, UnknownHostException { |
| 51 | + NetworkInterface nif = NetworkInterface.getByInetAddress(InetAddress.getLoopbackAddress()); |
| 52 | + assertFalse(nif == null); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void givenIndex_whenReturnsNetworkInterface_thenCorrect() throws SocketException, UnknownHostException { |
| 57 | + NetworkInterface nif = NetworkInterface.getByIndex(0); |
| 58 | + assertFalse(nif == null); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void givenInterface_whenReturnsInetAddresses_thenCorrect() throws SocketException, UnknownHostException { |
| 63 | + NetworkInterface nif = NetworkInterface.getByName("lo"); |
| 64 | + Enumeration<InetAddress> addressEnum = nif.getInetAddresses(); |
| 65 | + InetAddress address = addressEnum.nextElement(); |
| 66 | + assertEquals("127.0.0.1", address.getHostAddress()); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void givenInterface_whenReturnsInterfaceAddresses_thenCorrect() throws SocketException, UnknownHostException { |
| 71 | + NetworkInterface nif = NetworkInterface.getByName("lo"); |
| 72 | + |
| 73 | + List<InterfaceAddress> addressEnum = nif.getInterfaceAddresses(); |
| 74 | + InterfaceAddress address = addressEnum.get(0); |
| 75 | + InetAddress localAddress = address.getAddress(); |
| 76 | + InetAddress broadCastAddress = address.getBroadcast(); |
| 77 | + assertEquals("127.0.0.1", localAddress.getHostAddress()); |
| 78 | + assertEquals("127.255.255.255", broadCastAddress.getHostAddress()); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void givenInterface_whenChecksIfLoopback_thenCorrect() throws SocketException, UnknownHostException { |
| 83 | + NetworkInterface nif = NetworkInterface.getByName("lo"); |
| 84 | + assertTrue(nif.isLoopback()); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void givenInterface_whenChecksIfUp_thenCorrect() throws SocketException, UnknownHostException { |
| 89 | + NetworkInterface nif = NetworkInterface.getByName("lo"); |
| 90 | + assertTrue(nif.isUp()); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void givenInterface_whenChecksIfPointToPoint_thenCorrect() throws SocketException, UnknownHostException { |
| 95 | + NetworkInterface nif = NetworkInterface.getByName("lo"); |
| 96 | + assertFalse(nif.isPointToPoint()); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void givenInterface_whenChecksIfVirtual_thenCorrect() throws SocketException, UnknownHostException { |
| 101 | + NetworkInterface nif = NetworkInterface.getByName("lo"); |
| 102 | + assertFalse(nif.isVirtual()); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void givenInterface_whenChecksMulticastSupport_thenCorrect() throws SocketException, UnknownHostException { |
| 107 | + NetworkInterface nif = NetworkInterface.getByName("lo"); |
| 108 | + assertTrue(nif.supportsMulticast()); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void givenInterface_whenGetsMacAddress_thenCorrect() throws SocketException, UnknownHostException { |
| 113 | + NetworkInterface nif = NetworkInterface.getByName("lo"); |
| 114 | + byte[] bytes = nif.getHardwareAddress(); |
| 115 | + assertFalse(bytes == null); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void givenInterface_whenGetsMTU_thenCorrect() throws SocketException, UnknownHostException { |
| 120 | + NetworkInterface nif = NetworkInterface.getByName("lo"); |
| 121 | + int mtu = nif.getMTU(); |
| 122 | + } |
| 123 | +} |
0 commit comments