blob: 8ad948d5bb7af86f838309417f6f90b0d18948d2 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2001 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 4422122
27 * @summary Test that MulticastSocket.getInterface returns the
28 * same InetAddress set by MulticastSocket.setInterface
29 */
30import java.net.*;
31import java.util.Enumeration;
32import java.io.IOException;
33
34public 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}