jdk/test/java/net/NetworkInterface/UniqueMacAddressesTest.java
author coleenp
Wed, 17 Jun 2015 21:44:48 +0000
changeset 31362 8957ccbb5821
parent 19854 dda528ceb1de
permissions -rw-r--r--
8098821: Crash in system dictionary initialization with shared strings Summary: map string regions after the compressed class base is known Reviewed-by: iklam, dcubed Contributed-by: coleen.phillimore@oracle.com, mikhailo.seledtsov@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19827
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
     1
/*
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
     4
 *
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
     8
 *
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    13
 * accompanied this code).
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    14
 *
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    18
 *
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    21
 * questions.
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    22
 */
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    23
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    24
import java.net.InetAddress;
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    25
import java.net.NetworkInterface;
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    26
import java.net.SocketException;
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    27
import java.util.ArrayList;
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    28
import java.util.Arrays;
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    29
import java.util.Enumeration;
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    30
import java.util.List;
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    31
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    32
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    33
/*
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    34
 * @test
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    35
 * @bug 8021372
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    36
 * @summary Tests that the MAC addresses returned by NetworkInterface.getNetworkInterfaces are unique for each adapter.
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    37
 *
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    38
 */
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    39
public class UniqueMacAddressesTest {
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    40
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    41
    public static void main(String[] args) throws Exception {
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    42
        new UniqueMacAddressesTest().execute();
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    43
        System.out.println("UniqueMacAddressesTest: OK");
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    44
    }
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    45
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    46
    public UniqueMacAddressesTest() {
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    47
        System.out.println("UniqueMacAddressesTest: start ");
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    48
    }
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    49
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    50
    public void execute() throws Exception {
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    51
        Enumeration<NetworkInterface> networkInterfaces;
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    52
        boolean areMacAddressesUnique = false;
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    53
        List<NetworkInterface> networkInterfaceList = new ArrayList<NetworkInterface>();
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    54
            networkInterfaces = NetworkInterface.getNetworkInterfaces();
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    55
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    56
        // build a list of NetworkInterface objects to test MAC address
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    57
        // uniqueness
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    58
        createNetworkInterfaceList(networkInterfaces, networkInterfaceList);
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    59
        areMacAddressesUnique = checkMacAddressesAreUnique(networkInterfaceList);
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    60
        if (!areMacAddressesUnique) {
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    61
            throw new RuntimeException("mac address uniqueness test failed");
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    62
        }
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    63
    }
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    64
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    65
    private boolean checkMacAddressesAreUnique (
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    66
            List<NetworkInterface> networkInterfaces) throws Exception {
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    67
        boolean uniqueMacAddresses = true;
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    68
        for (NetworkInterface networkInterface : networkInterfaces) {
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    69
            for (NetworkInterface comparisonNetIf : networkInterfaces) {
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    70
                System.out.println("Comparing netif "
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    71
                        + networkInterface.getName() + " and netif "
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    72
                        + comparisonNetIf.getName());
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    73
                if (testMacAddressesEqual(networkInterface, comparisonNetIf)) {
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    74
                    uniqueMacAddresses = false;
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    75
                    break;
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    76
                }
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    77
            }
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    78
            if (uniqueMacAddresses != true)
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    79
                break;
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    80
        }
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    81
        return uniqueMacAddresses;
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    82
    }
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    83
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    84
    private boolean testMacAddressesEqual(NetworkInterface netIf1,
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    85
            NetworkInterface netIf2) throws Exception {
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    86
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    87
        byte[] rawMacAddress1 = null;
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    88
        byte[] rawMacAddress2 = null;
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    89
        boolean macAddressesEqual = false;
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    90
        if (!netIf1.getName().equals(netIf2.getName())) {
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    91
            System.out.println("compare hardware addresses "
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    92
                +  createMacAddressString(netIf1) + " and " + createMacAddressString(netIf2));
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    93
            rawMacAddress1 = netIf1.getHardwareAddress();
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    94
            rawMacAddress2 = netIf2.getHardwareAddress();
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    95
            macAddressesEqual = Arrays.equals(rawMacAddress1, rawMacAddress2);
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    96
        } else {
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    97
            // same interface
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    98
            macAddressesEqual = false;
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
    99
        }
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
   100
        return macAddressesEqual;
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
   101
    }
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
   102
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
   103
    private String createMacAddressString (NetworkInterface netIf) throws Exception {
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
   104
        byte[] macAddr = netIf.getHardwareAddress();
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
   105
        StringBuilder sb =  new StringBuilder();
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
   106
        if (macAddr != null) {
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
   107
            for (int i = 0; i < macAddr.length; i++) {
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
   108
                sb.append(String.format("%02X%s", macAddr[i],
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
   109
                        (i < macAddr.length - 1) ? "-" : ""));
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
   110
            }
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
   111
        }
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
   112
        return sb.toString();
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
   113
    }
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
   114
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
   115
    private void createNetworkInterfaceList(Enumeration<NetworkInterface> nis,
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
   116
            List<NetworkInterface> networkInterfaceList) throws Exception {
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
   117
        byte[] macAddr = null;
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
   118
        NetworkInterface netIf = null;
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
   119
        while (nis.hasMoreElements()) {
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
   120
            netIf = (NetworkInterface) nis.nextElement();
19854
dda528ceb1de 8024675: java/net/NetworkInterface/UniqueMacAddressesTest.java fails on Windows
msheppar
parents: 19827
diff changeset
   121
            if (netIf.isUp()) {
dda528ceb1de 8024675: java/net/NetworkInterface/UniqueMacAddressesTest.java fails on Windows
msheppar
parents: 19827
diff changeset
   122
                macAddr = netIf.getHardwareAddress();
dda528ceb1de 8024675: java/net/NetworkInterface/UniqueMacAddressesTest.java fails on Windows
msheppar
parents: 19827
diff changeset
   123
                if (macAddr != null) {
dda528ceb1de 8024675: java/net/NetworkInterface/UniqueMacAddressesTest.java fails on Windows
msheppar
parents: 19827
diff changeset
   124
                    System.out.println("Adding NetworkInterface "
dda528ceb1de 8024675: java/net/NetworkInterface/UniqueMacAddressesTest.java fails on Windows
msheppar
parents: 19827
diff changeset
   125
                            + netIf.getName() + " with mac address "
dda528ceb1de 8024675: java/net/NetworkInterface/UniqueMacAddressesTest.java fails on Windows
msheppar
parents: 19827
diff changeset
   126
                            + createMacAddressString(netIf));
dda528ceb1de 8024675: java/net/NetworkInterface/UniqueMacAddressesTest.java fails on Windows
msheppar
parents: 19827
diff changeset
   127
                    networkInterfaceList.add(netIf);
dda528ceb1de 8024675: java/net/NetworkInterface/UniqueMacAddressesTest.java fails on Windows
msheppar
parents: 19827
diff changeset
   128
                }
19827
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
   129
            }
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
   130
        }
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
   131
    }
082c76535c1f 8021372: NetworkInterface.getNetworkInterfaces() returns duplicate hardware address
msheppar
parents:
diff changeset
   132
}