author | mli |
Fri, 16 Mar 2018 18:15:31 +0800 | |
changeset 49255 | acdb8531cc8b |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
49255
acdb8531cc8b
8199215: Re-examine getFreePort method in test infrastructure library
mli
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2003, 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 |
/* @test |
|
25 |
* @bug 4865031 |
|
26 |
* @summary Test ScatteringByteChannel/GatheringByteChannel read/write |
|
49255
acdb8531cc8b
8199215: Re-examine getFreePort method in test infrastructure library
mli
parents:
47216
diff
changeset
|
27 |
* @library .. /test/lib |
acdb8531cc8b
8199215: Re-examine getFreePort method in test infrastructure library
mli
parents:
47216
diff
changeset
|
28 |
* @build jdk.test.lib.Utils TestServers |
acdb8531cc8b
8199215: Re-examine getFreePort method in test infrastructure library
mli
parents:
47216
diff
changeset
|
29 |
* @run main VectorParams |
2 | 30 |
*/ |
31 |
||
14415
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
5506
diff
changeset
|
32 |
import java.io.*; |
2 | 33 |
import java.net.*; |
34 |
import java.nio.*; |
|
35 |
import java.nio.channels.*; |
|
36 |
||
37 |
public class VectorParams { |
|
38 |
||
39 |
static java.io.PrintStream out = System.out; |
|
40 |
||
41 |
static final int testSize = 10; |
|
42 |
static ByteBuffer[] bufs = null; |
|
43 |
static InetSocketAddress isa = null; |
|
44 |
||
45 |
public static void main(String[] args) throws Exception { |
|
14415
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
5506
diff
changeset
|
46 |
try (TestServers.DayTimeServer daytimeServer |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
5506
diff
changeset
|
47 |
= TestServers.DayTimeServer.startNewServer(100)) { |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
5506
diff
changeset
|
48 |
initBufs(daytimeServer); |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
5506
diff
changeset
|
49 |
testSocketChannelVectorParams(); |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
5506
diff
changeset
|
50 |
testDatagramChannelVectorParams(); |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
5506
diff
changeset
|
51 |
testPipeVectorParams(); |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
5506
diff
changeset
|
52 |
testFileVectorParams(); |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
5506
diff
changeset
|
53 |
} |
2 | 54 |
} |
55 |
||
14415
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
5506
diff
changeset
|
56 |
static void initBufs(TestServers.DayTimeServer daytimeServer) throws Exception { |
2 | 57 |
bufs = new ByteBuffer[testSize]; |
58 |
for(int i=0; i<testSize; i++) { |
|
59 |
String source = "buffer" + i; |
|
60 |
bufs[i] = ByteBuffer.allocate(source.length()); |
|
61 |
bufs[i].put(source.getBytes("8859_1")); |
|
62 |
bufs[i].flip(); |
|
63 |
} |
|
14415
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
5506
diff
changeset
|
64 |
isa = new InetSocketAddress(daytimeServer.getAddress(), |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
5506
diff
changeset
|
65 |
daytimeServer.getPort()); |
2 | 66 |
} |
67 |
||
68 |
static void testSocketChannelVectorParams() throws Exception { |
|
69 |
SocketChannel sc = SocketChannel.open(isa); |
|
70 |
tryBadWrite(sc, bufs, 0, -1); |
|
71 |
tryBadWrite(sc, bufs, -1, 0); |
|
72 |
tryBadWrite(sc, bufs, 0, 1000); |
|
73 |
tryBadWrite(sc, bufs, 1000, 1); |
|
74 |
tryBadRead(sc, bufs, 0, -1); |
|
75 |
tryBadRead(sc, bufs, -1, 0); |
|
76 |
tryBadRead(sc, bufs, 0, 1000); |
|
77 |
tryBadRead(sc, bufs, 1000, 1); |
|
78 |
sc.close(); |
|
79 |
} |
|
80 |
||
81 |
static void testDatagramChannelVectorParams() throws Exception { |
|
82 |
DatagramChannel dc = DatagramChannel.open(); |
|
83 |
dc.connect(isa); |
|
84 |
tryBadRead(dc, bufs, 0, -1); |
|
85 |
tryBadRead(dc, bufs, -1, 0); |
|
86 |
tryBadRead(dc, bufs, 0, 1000); |
|
87 |
tryBadRead(dc, bufs, 1000, 1); |
|
88 |
tryBadWrite(dc, bufs, 0, -1); |
|
89 |
tryBadWrite(dc, bufs, -1, 0); |
|
90 |
tryBadWrite(dc, bufs, 0, 1000); |
|
91 |
tryBadWrite(dc, bufs, 1000, 1); |
|
92 |
dc.close(); |
|
93 |
} |
|
94 |
||
95 |
static void testPipeVectorParams() throws Exception { |
|
96 |
Pipe p = Pipe.open(); |
|
97 |
Pipe.SinkChannel sink = p.sink(); |
|
98 |
Pipe.SourceChannel source = p.source(); |
|
99 |
tryBadWrite(sink, bufs, 0, -1); |
|
100 |
tryBadWrite(sink, bufs, -1, 0); |
|
101 |
tryBadWrite(sink, bufs, 0, 1000); |
|
102 |
tryBadWrite(sink, bufs, 1000, 1); |
|
103 |
tryBadRead(source, bufs, 0, -1); |
|
104 |
tryBadRead(source, bufs, -1, 0); |
|
105 |
tryBadRead(source, bufs, 0, 1000); |
|
106 |
tryBadRead(source, bufs, 1000, 1); |
|
107 |
sink.close(); |
|
108 |
source.close(); |
|
109 |
} |
|
110 |
||
111 |
static void testFileVectorParams() throws Exception { |
|
112 |
File testFile = File.createTempFile("filevec", null); |
|
113 |
testFile.deleteOnExit(); |
|
114 |
RandomAccessFile raf = new RandomAccessFile(testFile, "rw"); |
|
115 |
FileChannel fc = raf.getChannel(); |
|
116 |
tryBadWrite(fc, bufs, 0, -1); |
|
117 |
tryBadWrite(fc, bufs, -1, 0); |
|
118 |
tryBadWrite(fc, bufs, 0, 1000); |
|
119 |
tryBadWrite(fc, bufs, 1000, 1); |
|
120 |
tryBadRead(fc, bufs, 0, -1); |
|
121 |
tryBadRead(fc, bufs, -1, 0); |
|
122 |
tryBadRead(fc, bufs, 0, 1000); |
|
123 |
tryBadRead(fc, bufs, 1000, 1); |
|
124 |
fc.close(); |
|
125 |
} |
|
126 |
||
127 |
private static void tryBadWrite(GatheringByteChannel gbc, |
|
128 |
ByteBuffer[] bufs, int offset, int len) |
|
129 |
throws Exception |
|
130 |
{ |
|
131 |
try { |
|
132 |
gbc.write(bufs, offset, len); |
|
133 |
throw new RuntimeException("Expected exception not thrown"); |
|
134 |
} catch (IndexOutOfBoundsException ioobe) { |
|
135 |
// Correct result |
|
136 |
} |
|
137 |
} |
|
138 |
||
139 |
private static void tryBadRead(ScatteringByteChannel sbc, |
|
140 |
ByteBuffer[] bufs, int offset, int len) |
|
141 |
throws Exception |
|
142 |
{ |
|
143 |
try { |
|
144 |
sbc.read(bufs, offset, len); |
|
145 |
throw new RuntimeException("Expected exception not thrown"); |
|
146 |
} catch (IndexOutOfBoundsException ioobe) { |
|
147 |
// Correct result |
|
148 |
} |
|
149 |
} |
|
150 |
||
151 |
} |