2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
|
2
|
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 |
*
|
5506
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @bug 4422122
|
|
27 |
* @summary Test that MulticastSocket.getInterface returns the
|
|
28 |
* same InetAddress set by MulticastSocket.setInterface
|
|
29 |
*/
|
|
30 |
import java.net.*;
|
|
31 |
import java.util.Enumeration;
|
|
32 |
import java.io.IOException;
|
|
33 |
|
|
34 |
public class TestInterfaces {
|
|
35 |
|
|
36 |
public static void main(String args[]) throws Exception {
|
|
37 |
int failures = 0;
|
|
38 |
|
|
39 |
MulticastSocket soc = new MulticastSocket();
|
|
40 |
|
|
41 |
Enumeration nifs = NetworkInterface.getNetworkInterfaces();
|
|
42 |
while (nifs.hasMoreElements()) {
|
|
43 |
NetworkInterface ni = (NetworkInterface)nifs.nextElement();
|
|
44 |
|
|
45 |
/*
|
|
46 |
* Test MulticastSocket.getInterface
|
|
47 |
*/
|
|
48 |
Enumeration addrs = ni.getInetAddresses();
|
|
49 |
while (addrs.hasMoreElements()) {
|
|
50 |
InetAddress ia = (InetAddress)addrs.nextElement();
|
|
51 |
|
|
52 |
System.out.println("********************************");
|
|
53 |
System.out.println("MulticastSocket.setInterface(" + ia + ")");
|
|
54 |
|
|
55 |
try {
|
|
56 |
soc.setInterface(ia);
|
|
57 |
} catch (IOException ioe) {
|
|
58 |
System.err.println("Can't set interface to: " + ia
|
|
59 |
+ " " + ioe.getMessage());
|
|
60 |
continue;
|
|
61 |
}
|
|
62 |
|
|
63 |
InetAddress curr = soc.getInterface();
|
|
64 |
if (!curr.equals(ia)) {
|
|
65 |
System.err.println("MulticastSocket.getInterface returned: " + curr);
|
|
66 |
System.err.println("Failed! Expected: " + ia);
|
|
67 |
failures++;
|
|
68 |
} else {
|
|
69 |
System.out.println("Passed.");
|
|
70 |
}
|
|
71 |
}
|
|
72 |
|
|
73 |
/*
|
|
74 |
* Test MulticastSocket.getNetworkInterface
|
|
75 |
*/
|
|
76 |
System.out.println("********************************");
|
|
77 |
System.out.println("MulticastSocket.setNetworkInterface(" +
|
|
78 |
ni.getName() + ")");
|
|
79 |
|
|
80 |
try {
|
|
81 |
soc.setNetworkInterface(ni);
|
|
82 |
} catch (IOException ioe) {
|
|
83 |
System.err.println("Can't set interface to: " + ni.getName()
|
|
84 |
+ " " + ioe.getMessage());
|
|
85 |
continue;
|
|
86 |
}
|
|
87 |
|
|
88 |
NetworkInterface curr = soc.getNetworkInterface();
|
|
89 |
if (!curr.equals(ni)) {
|
|
90 |
System.err.println("MulticastSocket.getNetworkInterface returned: " + curr);
|
|
91 |
System.err.println("Failed! Expected: " + ni);
|
|
92 |
failures++;
|
|
93 |
} else {
|
|
94 |
System.out.println("Passed.");
|
|
95 |
}
|
|
96 |
|
|
97 |
}
|
|
98 |
|
|
99 |
if (failures > 0) {
|
|
100 |
System.out.println("********************************");
|
|
101 |
throw new Exception(failures + " test(s) failed!!!");
|
|
102 |
}
|
|
103 |
|
|
104 |
}
|
|
105 |
|
|
106 |
}
|