NetworkInterfaceTest.java revision 671a834be6d345a7df01ba19407dd1c96509c785
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package libcore.java.net;
18
19import junit.framework.TestCase;
20import java.net.Inet4Address;
21import java.net.Inet6Address;
22import java.net.InetAddress;
23import java.net.InterfaceAddress;
24import java.net.NetworkInterface;
25import java.util.ArrayList;
26import java.util.Collections;
27import java.util.HashSet;
28import java.util.List;
29import java.util.Set;
30
31public class NetworkInterfaceTest extends TestCase {
32    // http://code.google.com/p/android/issues/detail?id=13784
33    public void testIPv6() throws Exception {
34        NetworkInterface lo = NetworkInterface.getByName("lo");
35        Set<InetAddress> actual = new HashSet<InetAddress>(Collections.list(lo.getInetAddresses()));
36
37        Set<InetAddress> expected = new HashSet<InetAddress>();
38        expected.add(Inet4Address.LOOPBACK);
39        expected.add(Inet6Address.getByAddress("localhost", new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, null));
40
41        assertEquals(expected, actual);
42    }
43
44    // http://code.google.com/p/android/issues/detail?id=34022
45    public void test_collectIpv6Addresses_3digitInterfaceIndex() throws Exception {
46        String lines[] = new String[] {
47                "fe800000000000000000000000000000 407 40 20 80    wlan0" };
48        List<InetAddress> addresses = new ArrayList<InetAddress>(1);
49        List<InterfaceAddress> ifAddresses = new ArrayList<InterfaceAddress>(1);
50
51        NetworkInterface.collectIpv6Addresses("wlan0", 1, addresses,
52                ifAddresses, lines);
53        assertEquals(1, addresses.size());
54        assertEquals(1, ifAddresses.size());
55        // Make sure the prefix length (field #3) is parsed correctly
56        assertEquals(4*16 + 0, ifAddresses.get(0).getNetworkPrefixLength());
57    }
58
59    public void test_collectIpv6Addresses_skipsUnmatchedLines() throws Exception {
60        String[] lines = new String[] {
61                "fe800000000000000000000000000000 40 40 20 80    wlan0",
62                "fe100000000000000000000000000000 41 40 20 80    wlan1",
63                "feb00000000000000000000000000000 42 40 20 80    wlan2" };
64        List<InetAddress> addresses = new ArrayList<InetAddress>(1);
65        List<InterfaceAddress> ifAddresses = new ArrayList<InterfaceAddress>(1);
66
67        NetworkInterface.collectIpv6Addresses("wlan0", 1, addresses,
68                ifAddresses, lines);
69        assertEquals(1, addresses.size());
70        assertEquals(1, ifAddresses.size());
71    }
72
73    public void testLoopback() throws Exception {
74        // We know lo shouldn't have a hardware address or an IPv4 broadcast address.
75        NetworkInterface lo = NetworkInterface.getByName("lo");
76        assertNull(lo.getHardwareAddress());
77        for (InterfaceAddress ia : lo.getInterfaceAddresses()) {
78            assertNull(ia.getBroadcast());
79        }
80
81        // But eth0, if it exists, should...
82        NetworkInterface eth0 = NetworkInterface.getByName("eth0");
83        if (eth0 != null) {
84            assertEquals(6, eth0.getHardwareAddress().length);
85            for (InterfaceAddress ia : eth0.getInterfaceAddresses()) {
86                if (ia.getAddress() instanceof Inet4Address) {
87                    assertNotNull(ia.getBroadcast());
88                }
89            }
90        }
91    }
92
93    public void testDumpAll() throws Exception {
94        Set<String> allNames = new HashSet<String>();
95        Set<Integer> allIndexes = new HashSet<Integer>();
96        for (NetworkInterface nif : Collections.list(NetworkInterface.getNetworkInterfaces())) {
97            System.err.println(nif);
98            System.err.println(nif.getInterfaceAddresses());
99            String flags = nif.isUp() ? "UP" : "DOWN";
100            if (nif.isLoopback()) {
101                flags += " LOOPBACK";
102            }
103            if (nif.isPointToPoint()) {
104                flags += " PTP";
105            }
106            if (nif.isVirtual()) {
107                flags += " VIRTUAL";
108            }
109            if (nif.supportsMulticast()) {
110                flags += " MULTICAST";
111            }
112            flags += " MTU=" + nif.getMTU();
113            byte[] mac = nif.getHardwareAddress();
114            if (mac != null) {
115                flags += " HWADDR=";
116                for (int i = 0; i < mac.length; ++i) {
117                    if (i > 0) {
118                        flags += ":";
119                    }
120                    flags += String.format("%02x", mac[i]);
121                }
122            }
123            System.err.println(flags);
124            System.err.println("-");
125
126            assertFalse(allNames.contains(nif.getName()));
127            allNames.add(nif.getName());
128
129            assertFalse(allIndexes.contains(nif.getIndex()));
130            allIndexes.add(nif.getIndex());
131        }
132    }
133}
134