Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/ipv6tests/ScopeTests.java
38812 views
/*1* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 486882026* @summary IPv6 support for Windows XP and 2003 server27*/2829import java.net.*;30import java.util.*;3132public class ScopeTests extends Tests {3334public static void main (String[] args) throws Exception {35checkDebug (args);36simpleTests();37complexTests();38System.out.println ("Tests passed: OK");39}4041static void sassert (boolean condition, String msg) throws Exception {42if (!condition) {43throw new Exception (msg);44}45}4647static void checkAddress (String a, int numeric) throws Exception {48Inet6Address addr = (Inet6Address) InetAddress.getByName (a);49if (addr.getScopeId () != numeric) {50throw new Exception ("wroing numeric scopeid");51}52}5354static void checkAddress (String a, String str) throws Exception {55Inet6Address addr = (Inet6Address) InetAddress.getByName (a);56if (!addr.getScopedInterface().getName().equals (str)) {57throw new Exception ("wroing scoped interface name");58}59}6061/* These tests check generic API functionality that is not62* dependent on scoping being implemented in the platform63*/64static void simpleTests () throws Exception {65checkAddress ("fe80::%1", 1);66checkAddress ("fe80::1%1", 1);67checkAddress ("fec0::1:a00:20ff:feed:b08d%0", 0);68checkAddress ("fec0::1:a00:20ff:feed:b08d%1", 1);69checkAddress ("fec0::1:a00:20ff:feed:b08d%99", 99);70checkAddress ("fe80::", 0);71checkAddress ("fec0::1:a00:20ff:feed:b08d", 0);72checkAddress ("fec0::1:a00:20ff:feed:b08d", 0);73checkAddress ("fec0::1:a00:20ff:feed:b08d", 0);74}7576/* These tests check the NetworkInterfaces for the actual scopeids77* configured on the system and do tests using that information78*/79static void complexTests () throws Exception {80dprintln ("ComplexTests");81Enumeration e = NetworkInterface.getNetworkInterfaces();82while (e.hasMoreElements()) {83NetworkInterface nif = (NetworkInterface)e.nextElement();84String name = nif.getName();85Enumeration addrs = nif.getInetAddresses();86while (addrs.hasMoreElements()) {87InetAddress addr = (InetAddress) addrs.nextElement();88dprintln ("ComplexTests: "+addr);89if (addr instanceof Inet6Address) {90Inet6Address ia6 = (Inet6Address) addr;91if (ia6.getScopeId() != 0) {92System.out.println ("Testing interface: " + name +93" and address: " + ia6);94ctest1 (name, ia6);95ctest2 (name, ia6);96} else {97System.out.println ("Interface: " + name +98" Address: "+ ia6 +99" does not support scope");100}101}102}103}104}105106107/* check the scoped name on the Inet6Address is the same as108* the interface it is attached to109*/110static void ctest1 (String ifname, Inet6Address ia6) throws Exception {111System.out.println ("ctest1:" + ia6);112String s = ia6.getScopedInterface().getName();113int scope = ia6.getScopeId();114sassert (ifname.equals (s), "ctest1:"+ifname+":"+s);115116}117118/* create an Inet6Adddress by all three methods using the ifname119* compare the numeric scope id with that of the Inet6Address passed in120*/121static void ctest2 (String ifname, Inet6Address ia6) throws Exception {122System.out.println ("ctest2:" + ia6);123String s = ia6.getScopedInterface().getName();124int scope = ia6.getScopeId();125String s1 = ia6.getHostAddress();126if (s1.indexOf('%') != -1) {127s1 = s1.substring (0, s1.indexOf ('%'));128}129Inet6Address add = (Inet6Address) InetAddress.getByName (s1+"%"+ifname);130sassert (add.getScopeId() == scope, "ctest2:1:" +scope);131}132}133134135