jdk/test/java/net/InterfaceAddress/NetworkPrefixLength.java
author cl
Wed, 22 Dec 2010 15:57:14 -0800
changeset 7655 3461980d82e1
parent 5506 202f599c92aa
child 17921 8ae76f95800a
permissions -rw-r--r--
Added tag jdk7-b123 for changeset 5d1dc8153960
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4811
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4811
diff changeset
     2
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
4811
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
     4
 *
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
     8
 *
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    13
 * accompanied this code).
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    14
 *
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4811
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4811
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4811
diff changeset
    21
 * questions.
4811
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    22
 */
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    23
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    24
/* @test
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    25
 * @bug 6707289
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    26
 * @summary InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    27
 */
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    28
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    29
import java.net.InetAddress;
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    30
import java.net.Inet4Address;
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    31
import java.net.InterfaceAddress;
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    32
import java.net.NetworkInterface;
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    33
import java.util.Enumeration;
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    34
import static java.lang.System.out;
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    35
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    36
public class NetworkPrefixLength {
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    37
    static boolean passed = true;
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    38
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    39
    public static void main(String[] args) throws Exception {
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    40
        Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    41
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    42
        while (nics.hasMoreElements()) {
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    43
            NetworkInterface nic = nics.nextElement();
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    44
            for (InterfaceAddress iaddr : nic.getInterfaceAddresses()) {
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    45
                boolean valid = checkPrefix(iaddr);
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    46
                if (!valid) {
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    47
                    passed = false;
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    48
                    debug(nic.getName(), iaddr);
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    49
                }
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    50
            }
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    51
        }
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    52
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    53
        if (!passed)
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    54
            throw new RuntimeException("Failed: some interfaces have invalid prefix lengths");
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    55
    }
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    56
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    57
    static boolean checkPrefix(InterfaceAddress iaddr) {
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    58
        InetAddress addr = iaddr.getAddress();
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    59
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    60
        if (addr instanceof Inet4Address)
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    61
            return checkIPv4PrefixLength(iaddr.getNetworkPrefixLength());
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    62
        else
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    63
            return checkIPv6PrefixLength(iaddr.getNetworkPrefixLength());
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    64
    }
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    65
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    66
    static boolean checkIPv4PrefixLength(int prefix) {
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    67
        if (prefix >=0 && prefix <= 32)
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    68
            return true;
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    69
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    70
        return false;
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    71
    }
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    72
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    73
    static boolean checkIPv6PrefixLength(int prefix) {
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    74
        if (prefix >=0 && prefix <= 128)
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    75
            return true;
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    76
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    77
        return false;
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    78
    }
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    79
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    80
    static void debug(String nicName, InterfaceAddress iaddr) {
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    81
        out.println("NIC " + nicName + " has an address with an invalid prefix length:\n" + iaddr);
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    82
    }
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    83
}
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents:
diff changeset
    84