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