author | aeubanks |
Tue, 16 Apr 2019 13:06:23 -0700 | |
changeset 54770 | 62b6e7587b1f |
parent 52209 | 3b2e68c9e7a6 |
child 54811 | 9db7c0f561a6 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
49431
5812849b5027
8198358: Align organization of TwoStacksPlainSocketImp with DualStackPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 1998, 2018, 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 |
|
52104
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
26 |
* @bug 4091859 8189366 |
54770
62b6e7587b1f
8220673: Add test library support for determining platform IP support
aeubanks
parents:
52209
diff
changeset
|
27 |
* @library /test/lib |
2 | 28 |
* @summary Test Socket.available() |
49431
5812849b5027
8198358: Align organization of TwoStacksPlainSocketImp with DualStackPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
29 |
* @run main CloseAvailable |
5812849b5027
8198358: Align organization of TwoStacksPlainSocketImp with DualStackPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
30 |
* @run main/othervm -Djava.net.preferIPv4Stack=true CloseAvailable |
2 | 31 |
*/ |
32 |
||
33 |
import java.net.*; |
|
34 |
import java.io.*; |
|
54770
62b6e7587b1f
8220673: Add test library support for determining platform IP support
aeubanks
parents:
52209
diff
changeset
|
35 |
import jdk.test.lib.net.IPSupport; |
2 | 36 |
|
37 |
||
52104
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
38 |
public class CloseAvailable { |
2 | 39 |
|
40 |
public static void main(String[] args) throws Exception { |
|
54770
62b6e7587b1f
8220673: Add test library support for determining platform IP support
aeubanks
parents:
52209
diff
changeset
|
41 |
IPSupport.skipIfCurrentConfigurationIsInvalid(); |
62b6e7587b1f
8220673: Add test library support for determining platform IP support
aeubanks
parents:
52209
diff
changeset
|
42 |
|
52104
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
43 |
testClose(); |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
44 |
|
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
45 |
testEOF(true); |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
46 |
testEOF(false); |
52209
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
47 |
testIOEOnClosed(true); |
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
48 |
testIOEOnClosed(false); |
52104
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
49 |
} |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
50 |
|
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
51 |
static void testClose() throws IOException { |
2 | 52 |
boolean error = true; |
52104
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
53 |
InetAddress addr = InetAddress.getLocalHost(); |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
54 |
ServerSocket ss = new ServerSocket(0); |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
55 |
int port = ss.getLocalPort(); |
2 | 56 |
|
52104
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
57 |
Thread t = new Thread(new Thread("Close-Available-1") { |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
58 |
public void run() { |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
59 |
try { |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
60 |
Socket s = new Socket(addr, port); |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
61 |
s.close(); |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
62 |
} catch (Exception e) { |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
63 |
e.printStackTrace(); |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
64 |
} |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
65 |
} |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
66 |
}); |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
67 |
|
2 | 68 |
t.start(); |
69 |
||
70 |
Socket soc = ss.accept(); |
|
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
71 |
ss.close(); |
2 | 72 |
|
73 |
DataInputStream is = new DataInputStream(soc.getInputStream()); |
|
74 |
is.close(); |
|
75 |
||
76 |
try { |
|
77 |
is.available(); |
|
78 |
} |
|
79 |
catch (IOException ex) { |
|
80 |
error = false; |
|
81 |
} |
|
82 |
if (error) |
|
83 |
throw new RuntimeException("Available() can be called after stream closed."); |
|
84 |
} |
|
85 |
||
52104
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
86 |
// Verifies consistency of `available` behaviour when EOF reached, both |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
87 |
// explicitly and implicitly. |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
88 |
static void testEOF(boolean readUntilEOF) throws IOException { |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
89 |
System.out.println("testEOF, readUntilEOF: " + readUntilEOF); |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
90 |
InetAddress addr = InetAddress.getLoopbackAddress(); |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
91 |
ServerSocket ss = new ServerSocket(); |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
92 |
ss.bind(new InetSocketAddress(addr, 0), 0); |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
93 |
int port = ss.getLocalPort(); |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
94 |
|
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
95 |
try (Socket s = new Socket(addr, port)) { |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
96 |
s.getOutputStream().write(0x42); |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
97 |
s.shutdownOutput(); |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
98 |
|
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
99 |
try (Socket soc = ss.accept()) { |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
100 |
ss.close(); |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
101 |
|
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
102 |
InputStream is = soc.getInputStream(); |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
103 |
int b = is.read(); |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
104 |
assert b == 0x42; |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
105 |
assert !s.isClosed(); |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
106 |
if (readUntilEOF) { |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
107 |
b = is.read(); |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
108 |
assert b == -1; |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
109 |
} |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
110 |
|
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
111 |
int a; |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
112 |
for (int i = 0; i < 100; i++) { |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
113 |
a = is.available(); |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
114 |
System.out.print(a + ", "); |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
115 |
if (a != 0) |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
116 |
throw new RuntimeException("Unexpected non-zero available: " + a); |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
117 |
} |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
118 |
assert !s.isClosed(); |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
119 |
assert is.read() == -1; |
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
120 |
} |
2 | 121 |
} |
52104
331fbd2db6b5
8189366: SocketInputStream.available() should check for eof
vtewari
parents:
49431
diff
changeset
|
122 |
System.out.println("\ncomplete"); |
2 | 123 |
} |
52209
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
124 |
|
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
125 |
// Verifies IOException thrown by `available`, on a closed input stream |
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
126 |
// that may, or may not, have reached EOF prior to closure. |
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
127 |
static void testIOEOnClosed(boolean readUntilEOF) throws IOException { |
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
128 |
System.out.println("testIOEOnClosed, readUntilEOF: " + readUntilEOF); |
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
129 |
InetAddress addr = InetAddress.getLoopbackAddress(); |
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
130 |
ServerSocket ss = new ServerSocket(); |
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
131 |
ss.bind(new InetSocketAddress(addr, 0), 0); |
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
132 |
int port = ss.getLocalPort(); |
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
133 |
|
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
134 |
try (Socket s = new Socket(addr, port)) { |
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
135 |
s.getOutputStream().write(0x43); |
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
136 |
s.shutdownOutput(); |
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
137 |
|
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
138 |
try (Socket soc = ss.accept()) { |
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
139 |
ss.close(); |
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
140 |
|
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
141 |
InputStream is = soc.getInputStream(); |
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
142 |
int b = is.read(); |
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
143 |
assert b == 0x43; |
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
144 |
assert !s.isClosed(); |
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
145 |
if (readUntilEOF) { |
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
146 |
b = is.read(); |
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
147 |
assert b == -1; |
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
148 |
} |
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
149 |
is.close(); |
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
150 |
try { |
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
151 |
b = is.available(); |
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
152 |
throw new RuntimeException("UNEXPECTED successful read: " + b); |
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
153 |
} catch (IOException expected) { |
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
154 |
System.out.println("caught expected IOException:" + expected); |
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
155 |
} |
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
156 |
} |
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
157 |
} |
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
158 |
System.out.println("\ncomplete"); |
3b2e68c9e7a6
8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents:
52104
diff
changeset
|
159 |
} |
2 | 160 |
} |