1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  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 org.apache.harmony.luni.tests.java.net;
18
19import java.net.NetworkInterface;
20import java.util.Enumeration;
21
22import junit.framework.TestCase;
23
24/**
25 * Please note that this case can only be passed on Linux with the user is
26 * 'root'.
27 */
28public class UnixNetworkInterfaceTest extends TestCase {
29    private Enumeration<NetworkInterface> netifs = null;
30
31    private boolean valid = false;
32
33    /**
34     * @tests java.net.NetworkInterface#isUp()
35     * @since 1.6
36     */
37    public void test_isUp() throws Exception {
38        while (netifs.hasMoreElements()) {
39            NetworkInterface netif = netifs.nextElement();
40            String name = netif.getName();
41            boolean up = netif.isUp();
42            // Down network interface will bring side effect on the
43            // platform. So chooses the already-down interface to up it.
44            if (!up && valid) {
45                String cmd = "ifconfig " + name + " up";
46                Process proc = Runtime.getRuntime().exec(cmd);
47                proc.waitFor();
48                assertEquals(name + " up should be " + !up, !up, netif.isUp());
49
50                cmd = "ifconfig " + name + " down";
51                proc = Runtime.getRuntime().exec(cmd);
52                proc.waitFor();
53                assertEquals(name + " up should be " + up, up, netif.isUp());
54            }
55        }
56    }
57
58    /**
59     * @tests java.net.NetworkInterface#supportsMulticast()
60     * @since 1.6
61     */
62    public void test_supportsMulticast() throws Exception {
63        while (netifs.hasMoreElements()) {
64            NetworkInterface netif = netifs.nextElement();
65            String name = netif.getName();
66            boolean multicast = netif.supportsMulticast();
67            if (valid) {
68                String cmd = multicast ? "ifconfig " + name + " -multicast"
69                        : "ifconfig " + name + " multicast";
70                Process proc = Runtime.getRuntime().exec(cmd);
71                proc.waitFor();
72                assertEquals(name + " multicast should be " + !multicast,
73                        !multicast, netif.supportsMulticast());
74
75                cmd = multicast ? "ifconfig " + name + " multicast"
76                        : "ifconfig " + name + " -multicast";
77                proc = Runtime.getRuntime().exec(cmd);
78                proc.waitFor();
79                assertEquals(name + " multicast should be " + multicast,
80                        multicast, netif.supportsMulticast());
81            }
82        }
83    }
84
85    /**
86     * @tests java.net.NetworkInterface#getHardwareAddress()
87     * @since 1.6
88     */
89    public void test_getHardwareAddress() throws Exception {
90        while (netifs.hasMoreElements()) {
91            NetworkInterface netif = netifs.nextElement();
92            String name = netif.getName();
93            byte[] hardAddr = netif.getHardwareAddress();
94
95            if (hardAddr != null && valid) {
96                String hardwareAddress = toHardwareAddress(hardAddr);
97                String newHardwareAddr = "0:11:25:1B:BD:FF";
98                String cmd = "ifconfig " + name + " hw ether "
99                        + newHardwareAddr;
100                Process proc = Runtime.getRuntime().exec(cmd);
101                proc.waitFor();
102                assertEquals(name + "'s hardware address should be"
103                        + newHardwareAddr, newHardwareAddr,
104                        toHardwareAddress(netif.getHardwareAddress()));
105
106                cmd = "ifconfig " + name + " hw ether " + hardwareAddress;
107                proc = Runtime.getRuntime().exec(cmd);
108                proc.waitFor();
109                assertEquals(name + "'s hardware address should be"
110                        + hardwareAddress, hardwareAddress,
111                        toHardwareAddress(netif.getHardwareAddress()));
112            }
113        }
114    }
115
116    /**
117     * @tests java.net.NetworkInterface#getMTU()
118     * @since 1.6
119     */
120    public void test_getMTU() throws Exception {
121        while (netifs.hasMoreElements()) {
122            NetworkInterface netif = netifs.nextElement();
123            String name = netif.getName();
124            int mtu = netif.getMTU();
125            if (valid) {
126                String cmd = "ifconfig " + name + " mtu 1000";
127                Process proc = Runtime.getRuntime().exec(cmd);
128                proc.waitFor();
129                assertEquals(name + " MTU should be 1000", 1000, netif.getMTU());
130
131                cmd = "ifconfig " + name + " mtu " + mtu;
132                proc = Runtime.getRuntime().exec(cmd);
133                proc.waitFor();
134                assertEquals(name + " MTU should be " + mtu, mtu, netif
135                        .getMTU());
136            }
137        }
138    }
139
140    /**
141     * @tests java.net.NetworkInterface#getSubInterfaces()
142     * @since 1.6
143     */
144    public void test_getSubInterfaces() throws Exception {
145        while (netifs.hasMoreElements()) {
146            NetworkInterface netif = netifs.nextElement();
147            Enumeration<NetworkInterface> subInterfaces1 = netif
148                    .getSubInterfaces();
149            Enumeration<NetworkInterface> subInterfaces2 = netif
150                    .getSubInterfaces();
151            while (subInterfaces1.hasMoreElements()
152                    && subInterfaces2.hasMoreElements() && valid) {
153                NetworkInterface sub1 = subInterfaces1.nextElement();
154                NetworkInterface sub2 = subInterfaces2.nextElement();
155                assertSame(sub1, sub2);
156                assertSame(netif, sub1.getParent());
157            }
158        }
159    }
160
161    /**
162     * @tests java.net.NetworkInterface#getParent()
163     * @since 1.6
164     */
165    public void test_getParent() {
166        while (netifs.hasMoreElements()) {
167            NetworkInterface netif = netifs.nextElement();
168            Enumeration<NetworkInterface> subInterfaces = netif
169                    .getSubInterfaces();
170            boolean contains = false;
171            boolean hasSubInterfaces = false;
172            while (subInterfaces.hasMoreElements() && valid) {
173                hasSubInterfaces = true;
174                NetworkInterface sub = subInterfaces.nextElement();
175                NetworkInterface parent1 = sub.getParent();
176                NetworkInterface parent2 = sub.getParent();
177                assertSame(parent1, parent2);
178                if (netif.equals(parent1)) {
179                    contains = true;
180                    break;
181                }
182            }
183            assertEquals(hasSubInterfaces, contains);
184        }
185    }
186
187    /**
188     * @tests java.net.NetworkInterface#isVirtual()
189     * @since 1.6
190     */
191    public void test_isVirtual() {
192        while (netifs.hasMoreElements()) {
193            NetworkInterface netif = netifs.nextElement();
194            boolean virtual = netif.getParent() != null;
195            assertEquals(netif.getName() + " isVirtual() is" + !virtual,
196                    virtual, netif.isVirtual());
197            Enumeration<NetworkInterface> subInterfaces = netif
198                    .getSubInterfaces();
199            while (subInterfaces.hasMoreElements()) {
200                assertTrue(subInterfaces.nextElement().isVirtual());
201            }
202        }
203    }
204
205    @Override
206    protected void setUp() throws Exception {
207        super.setUp();
208        netifs = NetworkInterface.getNetworkInterfaces();
209        valid = (netifs != null)
210                && System.getProperty("user.name").equals("root");
211    }
212
213    @Override
214    protected void tearDown() throws Exception {
215        netifs = null;
216        valid = false;
217        super.tearDown();
218    }
219
220    private static String toHardwareAddress(byte[] hardAddr) {
221        StringBuilder builder = new StringBuilder();
222        for (byte b : hardAddr) {
223            builder.append(Integer.toHexString(b >= 0 ? b : b + 256)
224                    .toUpperCase());
225            if (hardAddr[hardAddr.length - 1] != b) {
226                builder.append(":");
227            }
228        }
229        return builder.toString();
230    }
231}
232