Home | History | Annotate | Download | only in net
      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 
     18 package org.apache.harmony.luni.tests.java.net;
     19 
     20 import java.net.Inet4Address;
     21 import java.net.Inet6Address;
     22 import java.net.InetAddress;
     23 import java.net.InterfaceAddress;
     24 import java.net.NetworkInterface;
     25 import java.util.Enumeration;
     26 import java.util.List;
     27 
     28 import junit.framework.TestCase;
     29 
     30 public class InterfaceAddressTest extends TestCase {
     31     private InterfaceAddress interfaceAddr;
     32 
     33     private InterfaceAddress anotherInterfaceAddr;
     34 
     35     /**
     36      * @tests java.net.InterfaceAddress.hashCode()
     37      *
     38      * @since 1.6
     39      */
     40     public void test_hashCode() {
     41         // RI may fail on this when both broadcast addresses are null
     42         if (interfaceAddr != null) {
     43             assertEquals(anotherInterfaceAddr, interfaceAddr);
     44             assertEquals(anotherInterfaceAddr.hashCode(), interfaceAddr
     45                     .hashCode());
     46         }
     47     }
     48 
     49     /**
     50      * @tests java.net.InterfaceAddress.equals(Object)
     51      *
     52      * @since 1.6
     53      */
     54     public void test_equals_LObject() {
     55         // RI may fail on this when both broadcast addresses are null
     56         if (interfaceAddr != null) {
     57             assertFalse(interfaceAddr.equals(null));
     58             assertFalse(interfaceAddr.equals(new Object()));
     59 
     60             assertTrue(interfaceAddr.equals(anotherInterfaceAddr));
     61             assertNotSame(anotherInterfaceAddr, interfaceAddr);
     62         }
     63     }
     64 
     65     /**
     66      * @tests java.net.InterfaceAddress.toString()
     67      *
     68      * @since 1.6
     69      */
     70     public void test_toString() {
     71         if (interfaceAddr != null) {
     72             assertNotNull(interfaceAddr.toString());
     73             assertEquals(anotherInterfaceAddr.toString(), interfaceAddr
     74                     .toString());
     75             assertTrue(interfaceAddr.toString().contains("/"));
     76             assertTrue(interfaceAddr.toString().contains("["));
     77             assertTrue(interfaceAddr.toString().contains("]"));
     78         }
     79     }
     80 
     81     /**
     82      * @tests java.net.InterfaceAddress.getAddress()
     83      *
     84      * @since 1.6
     85      */
     86     public void test_getAddress() {
     87         if (interfaceAddr != null) {
     88             InetAddress addr1 = interfaceAddr.getAddress();
     89             assertNotNull(addr1);
     90             InetAddress addr2 = anotherInterfaceAddr.getAddress();
     91             assertNotNull(addr2);
     92             assertEquals(addr2, addr1);
     93         }
     94     }
     95 
     96     /**
     97      * @tests java.net.InterfaceAddress.getBroadcast()
     98      *
     99      * @since 1.6
    100      */
    101     public void test_getBroadcast() {
    102         if (interfaceAddr != null) {
    103             InetAddress addr = interfaceAddr.getAddress();
    104             InetAddress addr1 = interfaceAddr.getBroadcast();
    105             InetAddress addr2 = anotherInterfaceAddr.getBroadcast();
    106             if (addr instanceof Inet4Address) {
    107                 assertEquals(addr2, addr1);
    108             } else if (addr instanceof Inet6Address) {
    109                 assertNull(addr1);
    110                 assertNull(addr2);
    111             }
    112         }
    113     }
    114 
    115     /**
    116      * @tests java.net.InterfaceAddress.getNetworkPrefixLength()
    117      *
    118      * @since 1.6
    119      */
    120     public void test_getNetworkPrefixLength() {
    121         if (interfaceAddr != null) {
    122             short prefix1 = interfaceAddr.getNetworkPrefixLength();
    123             short prefix2 = anotherInterfaceAddr.getNetworkPrefixLength();
    124             assertEquals(prefix2, prefix1);
    125         }
    126     }
    127 
    128     @Override
    129     protected void setUp() throws Exception {
    130         super.setUp();
    131 
    132         Enumeration<NetworkInterface> netifs = NetworkInterface
    133                 .getNetworkInterfaces();
    134         NetworkInterface theInterface = null;
    135         if (netifs != null) {
    136             while (netifs.hasMoreElements()) {
    137                 theInterface = netifs.nextElement();
    138                 if (theInterface != null) {
    139                     List<InterfaceAddress> addrs = theInterface
    140                             .getInterfaceAddresses();
    141                     if (!(addrs == null || addrs.isEmpty())) {
    142                         interfaceAddr = addrs.get(0);
    143                         break;
    144                     }
    145                 }
    146             }
    147         }
    148 
    149         // get another InterfaceAddress object if the interfaceAddr exists. It
    150         // equals to interfaceAddr, but is not the same one.
    151         if (theInterface != null && interfaceAddr != null) {
    152             Enumeration<InetAddress> addresses = theInterface
    153                     .getInetAddresses();
    154             if (addresses != null && addresses.hasMoreElements()) {
    155                 NetworkInterface anotherNetworkInter = NetworkInterface
    156                         .getByInetAddress(addresses.nextElement());
    157                 anotherInterfaceAddr = anotherNetworkInter
    158                         .getInterfaceAddresses().get(0);
    159             }
    160         }
    161     }
    162 
    163     @Override
    164     protected void tearDown() throws Exception {
    165         interfaceAddr = null;
    166         anotherInterfaceAddr = null;
    167         super.tearDown();
    168     }
    169 
    170 }
    171