1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.apache.harmony.tests.java.net;
19
20import java.net.Inet4Address;
21import java.net.Inet6Address;
22import java.net.InetAddress;
23import java.net.InterfaceAddress;
24import java.net.NetworkInterface;
25import java.util.Enumeration;
26import java.util.List;
27
28import junit.framework.TestCase;
29
30public class InterfaceAddressTest extends TestCase {
31    private InterfaceAddress interfaceAddr;
32
33    private InterfaceAddress anotherInterfaceAddr;
34
35    /**
36     * java.net.InterfaceAddress.hashCode()
37     * @since 1.6
38     */
39    public void test_hashCode() {
40        // RI may fail on this when both broadcast addresses are null
41        if (interfaceAddr != null) {
42            assertEquals(anotherInterfaceAddr, interfaceAddr);
43            assertEquals(anotherInterfaceAddr.hashCode(), interfaceAddr
44                    .hashCode());
45        }
46    }
47
48    /**
49     * java.net.InterfaceAddress.equals(Object)
50     * @since 1.6
51     */
52    public void test_equals_LObject() {
53        // RI may fail on this when both broadcast addresses are null
54        if (interfaceAddr != null) {
55            assertFalse(interfaceAddr.equals(null));
56            assertFalse(interfaceAddr.equals(new Object()));
57
58            assertTrue(interfaceAddr.equals(anotherInterfaceAddr));
59            assertNotSame(anotherInterfaceAddr, interfaceAddr);
60        }
61    }
62
63    /**
64     * java.net.InterfaceAddress.toString()
65     * @since 1.6
66     */
67    public void test_toString() {
68        if (interfaceAddr != null) {
69            assertNotNull(interfaceAddr.toString());
70            assertEquals(anotherInterfaceAddr.toString(), interfaceAddr
71                    .toString());
72            assertTrue(interfaceAddr.toString().contains("/"));
73            assertTrue(interfaceAddr.toString().contains("["));
74            assertTrue(interfaceAddr.toString().contains("]"));
75        }
76    }
77
78    /**
79     * java.net.InterfaceAddress.getAddress()
80     * @since 1.6
81     */
82    public void test_getAddress() {
83        if (interfaceAddr != null) {
84            InetAddress addr1 = interfaceAddr.getAddress();
85            assertNotNull(addr1);
86            InetAddress addr2 = anotherInterfaceAddr.getAddress();
87            assertNotNull(addr2);
88            assertEquals(addr2, addr1);
89        }
90    }
91
92    /**
93     * java.net.InterfaceAddress.getBroadcast()
94     * @since 1.6
95     */
96    public void test_getBroadcast() {
97        if (interfaceAddr != null) {
98            InetAddress addr = interfaceAddr.getAddress();
99            InetAddress addr1 = interfaceAddr.getBroadcast();
100            InetAddress addr2 = anotherInterfaceAddr.getBroadcast();
101            if (addr instanceof Inet4Address) {
102                assertEquals(addr2, addr1);
103            } else if (addr instanceof Inet6Address) {
104                assertNull(addr1);
105                assertNull(addr2);
106            }
107        }
108    }
109
110    /**
111     * java.net.InterfaceAddress.getNetworkPrefixLength()
112     * @since 1.6
113     */
114    public void test_getNetworkPrefixLength() {
115        if (interfaceAddr != null) {
116            short prefix1 = interfaceAddr.getNetworkPrefixLength();
117            short prefix2 = anotherInterfaceAddr.getNetworkPrefixLength();
118            assertEquals(prefix2, prefix1);
119        }
120    }
121
122    @Override
123    protected void setUp() throws Exception {
124        super.setUp();
125
126        Enumeration<NetworkInterface> netifs = NetworkInterface
127                .getNetworkInterfaces();
128        NetworkInterface theInterface = null;
129        if (netifs != null) {
130            while (netifs.hasMoreElements()) {
131                theInterface = netifs.nextElement();
132                if (theInterface != null) {
133                    List<InterfaceAddress> addrs = theInterface
134                            .getInterfaceAddresses();
135                    if (!(addrs == null || addrs.isEmpty())) {
136                        interfaceAddr = addrs.get(0);
137                        break;
138                    }
139                }
140            }
141        }
142
143        // get another InterfaceAddress object if the interfaceAddr exists. It
144        // equals to interfaceAddr, but is not the same one.
145        if (theInterface != null && interfaceAddr != null) {
146            Enumeration<InetAddress> addresses = theInterface
147                    .getInetAddresses();
148            if (addresses != null && addresses.hasMoreElements()) {
149                NetworkInterface anotherNetworkInter = NetworkInterface
150                        .getByInetAddress(addresses.nextElement());
151                anotherInterfaceAddr = anotherNetworkInter
152                        .getInterfaceAddresses().get(0);
153            }
154        }
155    }
156
157    @Override
158    protected void tearDown() throws Exception {
159        interfaceAddr = null;
160        anotherInterfaceAddr = null;
161        super.tearDown();
162    }
163
164}
165