Path: blob/master/test/jdk/java/net/ipv6tests/ScopeTests.java
41149 views
/*1* Copyright (c) 2003, 2018, 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* @library /test/lib28* @build jdk.test.lib.NetworkConfiguration29* jdk.test.lib.Platform30* @run main ScopeTests31*/3233import java.net.*;34import java.util.*;3536public class ScopeTests extends Tests {3738public static void main (String[] args) throws Exception {39checkDebug (args);40simpleTests();41complexTests();42System.out.println ("Tests passed: OK");43}4445static void sassert (boolean condition, String msg) throws Exception {46if (!condition) {47throw new Exception (msg);48}49}5051static void checkAddress (String a, int numeric) throws Exception {52Inet6Address addr = (Inet6Address) InetAddress.getByName (a);53if (addr.getScopeId () != numeric) {54throw new Exception ("wroing numeric scopeid");55}56}5758static void checkAddress (String a, String str) throws Exception {59Inet6Address addr = (Inet6Address) InetAddress.getByName (a);60if (!addr.getScopedInterface().getName().equals (str)) {61throw new Exception ("wroing scoped interface name");62}63}6465/* These tests check generic API functionality that is not66* dependent on scoping being implemented in the platform67*/68static void simpleTests () throws Exception {69checkAddress ("fe80::%1", 1);70checkAddress ("fe80::1%1", 1);71checkAddress ("fec0::1:a00:20ff:feed:b08d%0", 0);72checkAddress ("fec0::1:a00:20ff:feed:b08d%1", 1);73checkAddress ("fec0::1:a00:20ff:feed:b08d%99", 99);74checkAddress ("fe80::", 0);75checkAddress ("fec0::1:a00:20ff:feed:b08d", 0);76checkAddress ("fec0::1:a00:20ff:feed:b08d", 0);77checkAddress ("fec0::1:a00:20ff:feed:b08d", 0);78}7980/* These tests check the NetworkInterfaces for the actual scopeids81* configured on the system and do tests using that information82*/83static void complexTests () throws Exception {84dprintln ("ComplexTests");85Enumeration e = NetworkInterface.getNetworkInterfaces();86while (e.hasMoreElements()) {87NetworkInterface nif = (NetworkInterface)e.nextElement();88String name = nif.getName();89Enumeration addrs = nif.getInetAddresses();90while (addrs.hasMoreElements()) {91InetAddress addr = (InetAddress) addrs.nextElement();92dprintln ("ComplexTests: "+addr);93if (addr instanceof Inet6Address) {94Inet6Address ia6 = (Inet6Address) addr;95if (ia6.getScopeId() != 0) {96System.out.println ("Testing interface: " + name +97" and address: " + ia6);98ctest1 (name, ia6);99ctest2 (name, ia6);100} else {101System.out.println ("Interface: " + name +102" Address: "+ ia6 +103" does not support scope");104}105}106}107}108}109110111/* check the scoped name on the Inet6Address is the same as112* the interface it is attached to113*/114static void ctest1 (String ifname, Inet6Address ia6) throws Exception {115System.out.println ("ctest1:" + ia6);116String s = ia6.getScopedInterface().getName();117int scope = ia6.getScopeId();118sassert (ifname.equals (s), "ctest1:"+ifname+":"+s);119120}121122/* create an Inet6Adddress by all three methods using the ifname123* compare the numeric scope id with that of the Inet6Address passed in124*/125static void ctest2 (String ifname, Inet6Address ia6) throws Exception {126System.out.println ("ctest2:" + ia6);127String s = ia6.getScopedInterface().getName();128int scope = ia6.getScopeId();129String s1 = ia6.getHostAddress();130if (s1.indexOf('%') != -1) {131s1 = s1.substring (0, s1.indexOf ('%'));132}133Inet6Address add = (Inet6Address) InetAddress.getByName (s1+"%"+ifname);134sassert (add.getScopeId() == scope, "ctest2:1:" +scope);135}136}137138139