blob: 3fee2eee565c26411ec302ff58bb84c4f155bf99 [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 4417734
27 * @summary Test that we get a BindException in all expected combinations
28 */
29import java.net.*;
30import java.util.Enumeration;
31
32public class Test {
33
34 static Object[][] getTestCombinations() {
35 return new Object[][] {
36 { "ServerSocket", "Socket" },
37 { "Socket", "Socket" },
38 { "DatagramSocket", "DatagramSocket" },
39 };
40 }
41
42 static InetAddress ia4_this;
43 static InetAddress ia6_this;
44
45 static int count;
46 static int failures;
47
48 static void doTest(Object test[], InetAddress ia1, InetAddress ia2,
49 boolean silent) {
50 String s1_type = (String)test[0];
51 String s2_type = (String)test[1];
52 int port = 0;
53
54 /*
55 * Increment test count
56 */
57 count++;
58
59 /*
60 * Do the test
61 */
62
63 boolean gotBindException = false;
64 boolean failed = false;
65 Exception failed_exc = null;
66
67 try {
68 Socket sock1, sock2;
69 ServerSocket ss;
70 DatagramSocket dsock1, dsock2;
71
72 /* bind the first socket */
73
74 if (s1_type.equals("Socket")) {
75 sock1 = new Socket();
76 sock1.bind( new InetSocketAddress(ia1, 0));
77 port = sock1.getLocalPort();
78 }
79
80 if (s1_type.equals("ServerSocket")) {
81 ss = new ServerSocket(0, 0, ia1);
82 port = ss.getLocalPort();
83 }
84
85 if (s1_type.equals("DatagramSocket")) {
86 dsock1 = new DatagramSocket( new InetSocketAddress(ia1, 0) );
87 port = dsock1.getLocalPort();
88 }
89
90 /* bind the second socket */
91
92 if (s2_type.equals("Socket")) {
93 sock2 = new Socket();
94 sock2.bind( new InetSocketAddress(ia2, port));
95 }
96
97 if (s2_type.equals("ServerSocket")) {
98 ss = new ServerSocket(port, 0, ia2);
99 }
100
101 if (s2_type.equals("DatagramSocket")) {
102 dsock2 = new DatagramSocket( new InetSocketAddress(ia2, port) );
103 }
104
105 } catch (BindException be) {
106 gotBindException = true;
107 } catch (Exception e) {
108 failed = true;
109 failed_exc = e;
110 }
111
112 /*
113 * Did we expect a BindException?
114 */
115 boolean expectedBindException = true;
116 if (ia1 == ia4_this && ia2 == ia6_this) {
117 expectedBindException = false;
118 }
119 if (ia1 == ia6_this && ia2 == ia4_this) {
120 expectedBindException = false;
121 }
122
123 /*
124 * Did it fail?
125 */
126
127 if (!failed && gotBindException != expectedBindException) {
128 failed = true;
129 }
130
131 /*
132 * If test passed and running in silent mode then exit
133 */
134 if (!failed && silent) {
135 return;
136 }
137
138 if (failed || !silent) {
139 System.out.println("");
140 System.out.println("**************************");
141 System.out.println("Test " + count);
142
143 System.out.println(s1_type + " binds: " + ia1 + " port: " + port);
144 System.out.println(s2_type + " binds: " + ia2);
145
146 if (!failed) {
147 if (gotBindException) {
148 System.out.println("Got expected BindException - test passed!");
149 } else {
150 System.out.println("No BindException as expected - test passed!");
151 }
152 return;
153 }
154 }
155 if (gotBindException) {
156 System.out.println("BindException unexpected - test failed!!!");
157 } else {
158 System.out.println("No bind failure as expected - test failed!!!");
159 }
160 failures++;
161 }
162
163 public static void main(String args[]) throws Exception {
164
165 boolean silent = true;
166 if (args.length > 0) {
167 if (args[0].equals("-d")) {
168 silent = false;
169 }
170 }
171
172 /*
173 * Test needs an IPv4 and IPv6 address to run.
174 */
175 Enumeration nifs = NetworkInterface.getNetworkInterfaces();
176 while (nifs.hasMoreElements()) {
177 NetworkInterface ni = (NetworkInterface)nifs.nextElement();
178
179 Enumeration addrs = ni.getInetAddresses();
180 while (addrs.hasMoreElements()) {
181 InetAddress ia = (InetAddress)addrs.nextElement();
182
183 if (ia.isLoopbackAddress()) {
184 continue;
185 }
186
187 if ((ia instanceof Inet4Address) && (ia4_this == null)) {
188 ia4_this = ia;
189 }
190
191 if ((ia instanceof Inet6Address) && (ia6_this == null)) {
192 ia6_this = ia;
193 }
194 }
195 }
196
197 /*
198 * Perform tests on all combinations of IPv4 and IPv6
199 * addresses.
200 */
201 InetAddress addrs[] = { ia4_this, ia6_this };
202
203 Object tests[][] = getTestCombinations();
204
205 for (int i=0; i<tests.length; i++) {
206 Object test[] = tests[i];
207
208 for (int j=0; j<addrs.length; j++) {
209 for (int k=0; k<addrs.length; k++) {
210
211 if (addrs[j] == null || addrs[k] == null) {
212 continue;
213 }
214
215 doTest( test, addrs[j], addrs[k], silent);
216 }
217 }
218 }
219
220 System.out.println("");
221 System.out.println(count + " test(s) executed. " + failures + " failure(s).");
222
223 if (failures > 0) {
224 throw new Exception(failures + " tests(s) failed - see log");
225 }
226 }
227}