|
| 1 | +package malva.java.net; |
| 2 | + |
| 3 | +import java.net.InetAddress; |
| 4 | +import java.net.InterfaceAddress; |
| 5 | +import java.net.NetworkInterface; |
| 6 | +import java.util.Collections; |
| 7 | +import java.util.Enumeration; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.HashSet; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.Set; |
| 13 | +import malva.TestCase; |
| 14 | + |
| 15 | +public class NetworkInterfaceTest extends TestCase { |
| 16 | + public static void testGetName() throws Exception { |
| 17 | + for (NetworkInterface iface : getNetworkInterfaces()) { |
| 18 | + assertNotNull(iface.getName()); |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + public static void testGetInetAddresses() throws Exception { |
| 23 | + for (NetworkInterface iface : getNetworkInterfaces()) { |
| 24 | + for (InetAddress addr : getInetAddresses(iface)) { |
| 25 | + assertNotNull(addr); |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + public static void testGetInterfaceAddresses() throws Exception { |
| 31 | + for (NetworkInterface iface : getNetworkInterfaces()) { |
| 32 | + for (InterfaceAddress addr : iface.getInterfaceAddresses()) { |
| 33 | + assertNotNull(addr); |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + public static void testGetSubInterfacesAndGetParent() throws Exception { |
| 39 | + for (NetworkInterface iface : getNetworkInterfaces()) { |
| 40 | + for (NetworkInterface subIface : Collections.list(iface.getSubInterfaces())) { |
| 41 | + assertNotNull(subIface); |
| 42 | + assertEquals(subIface.getParent(), iface); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + public static void testGetByName() throws Exception { |
| 48 | + for (NetworkInterface iface : getNetworkInterfaces()) { |
| 49 | + assertEquals(iface, NetworkInterface.getByName(iface.getName())); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public static void testGetByInetAddress() throws Exception { |
| 54 | + Map<InetAddress, Set<NetworkInterface>> map = mapInetAddressesToNetworkInterfaces(getNetworkInterfaces()); |
| 55 | + |
| 56 | + // Multiple network interfaces can be bound to the same address. Which one in the set is returned by |
| 57 | + // getByInetAddress() is not defined. |
| 58 | + for (InetAddress addr : map.keySet()) { |
| 59 | + assertTrue(map.get(addr).contains(NetworkInterface.getByInetAddress(addr))); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + public static void testGetMTU() throws Exception { |
| 65 | + for (NetworkInterface iface : getNetworkInterfaces()) { |
| 66 | + assertTrue(iface.getMTU() > 0); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public static void testIsLoopback() throws Exception { |
| 71 | + for (NetworkInterface iface : getNetworkInterfaces()) { |
| 72 | + if (iface.isLoopback()) { |
| 73 | + for (InetAddress addr : getInetAddresses(iface)) { |
| 74 | + assertTrue(addr.isLoopbackAddress()); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public static void testToString() throws Exception { |
| 81 | + for (NetworkInterface iface : getNetworkInterfaces()) { |
| 82 | + assertNotNull(iface.toString()); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private static List<NetworkInterface> getNetworkInterfaces() throws Exception { |
| 87 | + return Collections.list(NetworkInterface.getNetworkInterfaces()); |
| 88 | + } |
| 89 | + |
| 90 | + private static List<InetAddress> getInetAddresses(NetworkInterface iface) { |
| 91 | + return Collections.list(iface.getInetAddresses()); |
| 92 | + } |
| 93 | + |
| 94 | + private static Map<InetAddress, Set<NetworkInterface>> mapInetAddressesToNetworkInterfaces(List<NetworkInterface> ifaces) { |
| 95 | + Map<InetAddress, Set<NetworkInterface>> map = new HashMap<InetAddress, Set<NetworkInterface>>(); |
| 96 | + |
| 97 | + for (NetworkInterface iface : ifaces) { |
| 98 | + for (InetAddress addr : getInetAddresses(iface)) { |
| 99 | + Set<NetworkInterface> set = map.get(addr); |
| 100 | + if (set == null) { |
| 101 | + set = new HashSet<NetworkInterface>(); |
| 102 | + map.put(addr, set); |
| 103 | + } |
| 104 | + set.add(iface); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return map; |
| 109 | + } |
| 110 | + |
| 111 | + public static void main(String[] args) throws Exception { |
| 112 | + testGetName(); |
| 113 | + testGetInetAddresses(); |
| 114 | + testGetInterfaceAddresses(); |
| 115 | + testGetSubInterfacesAndGetParent(); |
| 116 | + testGetByName(); |
| 117 | + testGetByInetAddress(); |
| 118 | + testGetMTU(); |
| 119 | + // FIXME: BROKEN |
| 120 | + // testIsLoopback(); |
| 121 | + testToString(); |
| 122 | + } |
| 123 | +} |
0 commit comments