1 /* |
|
2 * Copyright (c) 2019, Oracle and/or its affiliates. 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 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. |
|
22 */ |
|
23 |
|
24 /* |
|
25 * @test |
|
26 * @summary Combinations of bad SocketImpls |
|
27 * @run testng/othervm BadSocketImpls |
|
28 * @run testng/othervm -Djdk.net.usePlainSocketImpl BadSocketImpls |
|
29 */ |
|
30 |
|
31 import java.io.IOException; |
|
32 import java.io.InputStream; |
|
33 import java.io.OutputStream; |
|
34 import java.net.InetAddress; |
|
35 import java.net.InetSocketAddress; |
|
36 import java.net.ServerSocket; |
|
37 import java.net.Socket; |
|
38 import java.net.SocketAddress; |
|
39 import java.net.SocketImpl; |
|
40 import org.testng.annotations.BeforeMethod; |
|
41 import org.testng.annotations.Test; |
|
42 import static java.lang.String.format; |
|
43 import static java.lang.System.out; |
|
44 import static org.testng.Assert.fail; |
|
45 |
|
46 public class BadSocketImpls { |
|
47 |
|
48 Class<IOException> IOE = IOException.class; |
|
49 |
|
50 /** |
|
51 * Tests that a server-side custom socket impl, whose accept is a no-op, |
|
52 * does not have any adverse negative effects. Default accept impl. |
|
53 */ |
|
54 @Test |
|
55 public void testNoOpAccept() throws IOException { |
|
56 ServerSocket ss = new ServerSocket(new NoOpSocketImpl()) { }; |
|
57 try (ss) { |
|
58 ss.bind(new InetSocketAddress(0)); |
|
59 assertThrows(IOE, ss::accept); |
|
60 } |
|
61 } |
|
62 |
|
63 /** |
|
64 * Tests that a server-side custom socket impl, whose accept is a no-op, |
|
65 * does not have any adverse negative effects. Custom accept, client has |
|
66 * an explicit null impl. |
|
67 */ |
|
68 @Test |
|
69 public void testNoOpAcceptWithNullClientImpl() throws IOException { |
|
70 ServerSocket ss = new ServerSocket(new NoOpSocketImpl()) { |
|
71 @Override |
|
72 public Socket accept() throws IOException { |
|
73 Socket s = new Socket((SocketImpl)null) { }; |
|
74 implAccept(s); |
|
75 return s; |
|
76 } |
|
77 }; |
|
78 try (ss) { |
|
79 ss.bind(new InetSocketAddress(0)); |
|
80 assertThrows(IOE, ss::accept); |
|
81 } |
|
82 } |
|
83 |
|
84 /** |
|
85 * Tests that a server-side custom socket impl, whose accept is a no-op, |
|
86 * does not have any adverse negative effects. Custom accept, client has |
|
87 * a platform impl. |
|
88 */ |
|
89 @Test |
|
90 public void testNoOpAcceptWithPlatformClientImpl() throws IOException { |
|
91 ServerSocket ss = new ServerSocket(new NoOpSocketImpl()) { |
|
92 @Override |
|
93 public Socket accept() throws IOException { |
|
94 Socket s = new Socket(); |
|
95 implAccept(s); |
|
96 return s; |
|
97 } |
|
98 }; |
|
99 try (ss) { |
|
100 ss.bind(new InetSocketAddress(0)); |
|
101 assertThrows(IOE, ss::accept); |
|
102 } |
|
103 } |
|
104 |
|
105 static class NoOpSocketImpl extends SocketImpl { |
|
106 @Override protected void create(boolean b) { } |
|
107 @Override protected void connect(String s, int i) { } |
|
108 @Override protected void connect(InetAddress inetAddress, int i) { } |
|
109 @Override protected void connect(SocketAddress socketAddress, int i) { } |
|
110 @Override protected void bind(InetAddress inetAddress, int i) { } |
|
111 @Override protected void listen(int i) { } |
|
112 @Override protected void accept(SocketImpl socket) { } |
|
113 @Override protected InputStream getInputStream() { return null; } |
|
114 @Override protected OutputStream getOutputStream() { return null; } |
|
115 @Override protected int available() { return 0; } |
|
116 @Override protected void close() { } |
|
117 @Override protected void sendUrgentData(int i) { } |
|
118 @Override public void setOption(int i, Object o) { } |
|
119 @Override public Object getOption(int i) { return null; } |
|
120 } |
|
121 |
|
122 static <T extends Throwable> void assertThrows(Class<T> throwableClass, |
|
123 ThrowingRunnable runnable) { |
|
124 try { |
|
125 runnable.run(); |
|
126 fail(format("Expected %s to be thrown, but nothing was thrown", |
|
127 throwableClass.getSimpleName())); |
|
128 } catch (Throwable t) { |
|
129 if (!throwableClass.isInstance(t)) { |
|
130 fail(format("Expected %s to be thrown, but %s was thrown", |
|
131 throwableClass.getSimpleName(), |
|
132 t.getClass().getSimpleName()), |
|
133 t); |
|
134 } |
|
135 out.println("caught expected exception: " + t); |
|
136 } |
|
137 } |
|
138 |
|
139 interface ThrowingRunnable { |
|
140 void run() throws Throwable; |
|
141 } |
|
142 |
|
143 @BeforeMethod |
|
144 public void breakBetweenTests() { |
|
145 System.out.println("\n-------\n"); |
|
146 } |
|
147 } |
|