2
|
1 |
/*
|
|
2 |
* Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @bug 4868820
|
|
27 |
* @summary IPv6 support for Windows XP and 2003 server
|
|
28 |
*/
|
|
29 |
|
|
30 |
import java.net.*;
|
|
31 |
import java.util.*;
|
|
32 |
|
|
33 |
public class ScopeTests extends Tests {
|
|
34 |
|
|
35 |
public static void main (String[] args) throws Exception {
|
|
36 |
checkDebug (args);
|
|
37 |
simpleTests();
|
|
38 |
complexTests();
|
|
39 |
System.out.println ("Tests passed: OK");
|
|
40 |
}
|
|
41 |
|
|
42 |
static void sassert (boolean condition, String msg) throws Exception {
|
|
43 |
if (!condition) {
|
|
44 |
throw new Exception (msg);
|
|
45 |
}
|
|
46 |
}
|
|
47 |
|
|
48 |
static void checkAddress (String a, int numeric) throws Exception {
|
|
49 |
Inet6Address addr = (Inet6Address) InetAddress.getByName (a);
|
|
50 |
if (addr.getScopeId () != numeric) {
|
|
51 |
throw new Exception ("wroing numeric scopeid");
|
|
52 |
}
|
|
53 |
}
|
|
54 |
|
|
55 |
static void checkAddress (String a, String str) throws Exception {
|
|
56 |
Inet6Address addr = (Inet6Address) InetAddress.getByName (a);
|
|
57 |
if (!addr.getScopedInterface().getName().equals (str)) {
|
|
58 |
throw new Exception ("wroing scoped interface name");
|
|
59 |
}
|
|
60 |
}
|
|
61 |
|
|
62 |
/* These tests check generic API functionality that is not
|
|
63 |
* dependent on scoping being implemented in the platform
|
|
64 |
*/
|
|
65 |
static void simpleTests () throws Exception {
|
|
66 |
checkAddress ("fe80::%1", 1);
|
|
67 |
checkAddress ("fe80::1%1", 1);
|
|
68 |
checkAddress ("fec0::1:a00:20ff:feed:b08d%0", 0);
|
|
69 |
checkAddress ("fec0::1:a00:20ff:feed:b08d%1", 1);
|
|
70 |
checkAddress ("fec0::1:a00:20ff:feed:b08d%99", 99);
|
|
71 |
checkAddress ("fe80::", 0);
|
|
72 |
checkAddress ("fec0::1:a00:20ff:feed:b08d", 0);
|
|
73 |
checkAddress ("fec0::1:a00:20ff:feed:b08d", 0);
|
|
74 |
checkAddress ("fec0::1:a00:20ff:feed:b08d", 0);
|
|
75 |
}
|
|
76 |
|
|
77 |
/* These tests check the NetworkInterfaces for the actual scopeids
|
|
78 |
* configured on the system and do tests using that information
|
|
79 |
*/
|
|
80 |
static void complexTests () throws Exception {
|
|
81 |
dprintln ("ComplexTests");
|
|
82 |
Enumeration e = NetworkInterface.getNetworkInterfaces();
|
|
83 |
while (e.hasMoreElements()) {
|
|
84 |
NetworkInterface nif = (NetworkInterface)e.nextElement();
|
|
85 |
String name = nif.getName();
|
|
86 |
Enumeration addrs = nif.getInetAddresses();
|
|
87 |
while (addrs.hasMoreElements()) {
|
|
88 |
InetAddress addr = (InetAddress) addrs.nextElement();
|
|
89 |
dprintln ("ComplexTests: "+addr);
|
|
90 |
if (addr instanceof Inet6Address) {
|
|
91 |
Inet6Address ia6 = (Inet6Address) addr;
|
|
92 |
if (ia6.getScopeId() != 0) {
|
|
93 |
System.out.println ("Testing interface: " + name +
|
|
94 |
" and address: " + ia6);
|
|
95 |
ctest1 (name, ia6);
|
|
96 |
ctest2 (name, ia6);
|
|
97 |
} else {
|
|
98 |
System.out.println ("Interface: " + name +
|
|
99 |
" Address: "+ ia6 +
|
|
100 |
" does not support scope");
|
|
101 |
}
|
|
102 |
}
|
|
103 |
}
|
|
104 |
}
|
|
105 |
}
|
|
106 |
|
|
107 |
|
|
108 |
/* check the scoped name on the Inet6Address is the same as
|
|
109 |
* the interface it is attached to
|
|
110 |
*/
|
|
111 |
static void ctest1 (String ifname, Inet6Address ia6) throws Exception {
|
|
112 |
System.out.println ("ctest1:" + ia6);
|
|
113 |
String s = ia6.getScopedInterface().getName();
|
|
114 |
int scope = ia6.getScopeId();
|
|
115 |
sassert (ifname.equals (s), "ctest1:"+ifname+":"+s);
|
|
116 |
|
|
117 |
}
|
|
118 |
|
|
119 |
/* create an Inet6Adddress by all three methods using the ifname
|
|
120 |
* compare the numeric scope id with that of the Inet6Address passed in
|
|
121 |
*/
|
|
122 |
static void ctest2 (String ifname, Inet6Address ia6) throws Exception {
|
|
123 |
System.out.println ("ctest2:" + ia6);
|
|
124 |
String s = ia6.getScopedInterface().getName();
|
|
125 |
int scope = ia6.getScopeId();
|
|
126 |
String s1 = ia6.getHostAddress();
|
|
127 |
if (s1.indexOf('%') != -1) {
|
|
128 |
s1 = s1.substring (0, s1.indexOf ('%'));
|
|
129 |
}
|
|
130 |
Inet6Address add = (Inet6Address) InetAddress.getByName (s1+"%"+ifname);
|
|
131 |
sassert (add.getScopeId() == scope, "ctest2:1:" +scope);
|
|
132 |
}
|
|
133 |
}
|