author | bpb |
Thu, 22 Mar 2018 12:29:52 -0700 | |
changeset 49284 | a51ca91c2cde |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
49284
a51ca91c2cde
8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
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 |
|
16480
fcad3b6ec812
7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents:
5506
diff
changeset
|
25 |
* @bug 4849277 7183800 |
2 | 26 |
* @summary Test DatagramChannel send while connected |
49284
a51ca91c2cde
8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
47216
diff
changeset
|
27 |
* @library .. |
a51ca91c2cde
8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
47216
diff
changeset
|
28 |
* @run testng ConnectedSend |
2 | 29 |
* @author Mike McCloskey |
30 |
*/ |
|
31 |
||
32 |
import java.io.*; |
|
33 |
import java.net.*; |
|
34 |
import java.nio.*; |
|
35 |
import java.nio.channels.*; |
|
36 |
import java.nio.charset.*; |
|
49284
a51ca91c2cde
8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
47216
diff
changeset
|
37 |
import org.testng.annotations.Test; |
a51ca91c2cde
8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
47216
diff
changeset
|
38 |
import static org.testng.Assert.*; |
2 | 39 |
|
40 |
public class ConnectedSend { |
|
41 |
// Check if DatagramChannel.send while connected can include |
|
42 |
// address without throwing |
|
49284
a51ca91c2cde
8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
47216
diff
changeset
|
43 |
@Test |
a51ca91c2cde
8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
47216
diff
changeset
|
44 |
public static void sendToConnectedAddress() throws Exception { |
2 | 45 |
DatagramChannel sndChannel = DatagramChannel.open(); |
49284
a51ca91c2cde
8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
47216
diff
changeset
|
46 |
sndChannel.bind(null); |
16480
fcad3b6ec812
7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents:
5506
diff
changeset
|
47 |
InetAddress address = InetAddress.getLocalHost(); |
fcad3b6ec812
7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents:
5506
diff
changeset
|
48 |
if (address.isLoopbackAddress()) { |
fcad3b6ec812
7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents:
5506
diff
changeset
|
49 |
address = InetAddress.getLoopbackAddress(); |
fcad3b6ec812
7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents:
5506
diff
changeset
|
50 |
} |
2 | 51 |
InetSocketAddress sender = new InetSocketAddress( |
16480
fcad3b6ec812
7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents:
5506
diff
changeset
|
52 |
address, |
2 | 53 |
sndChannel.socket().getLocalPort()); |
54 |
||
55 |
DatagramChannel rcvChannel = DatagramChannel.open(); |
|
49284
a51ca91c2cde
8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
47216
diff
changeset
|
56 |
rcvChannel.bind(null); |
2 | 57 |
InetSocketAddress receiver = new InetSocketAddress( |
16480
fcad3b6ec812
7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents:
5506
diff
changeset
|
58 |
address, |
2 | 59 |
rcvChannel.socket().getLocalPort()); |
60 |
||
61 |
rcvChannel.connect(sender); |
|
62 |
sndChannel.connect(receiver); |
|
63 |
||
64 |
ByteBuffer bb = ByteBuffer.allocate(256); |
|
65 |
bb.put("hello".getBytes()); |
|
66 |
bb.flip(); |
|
67 |
int sent = sndChannel.send(bb, receiver); |
|
68 |
bb.clear(); |
|
69 |
rcvChannel.receive(bb); |
|
70 |
bb.flip(); |
|
71 |
CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb); |
|
49284
a51ca91c2cde
8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
47216
diff
changeset
|
72 |
assertTrue(cb.toString().startsWith("h"), "Unexpected message content"); |
2 | 73 |
|
74 |
rcvChannel.close(); |
|
75 |
sndChannel.close(); |
|
76 |
} |
|
77 |
||
78 |
// Check if the datagramsocket adaptor can send with a packet |
|
79 |
// that has not been initialized with an address; the legacy |
|
80 |
// datagram socket will send in this case |
|
49284
a51ca91c2cde
8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
47216
diff
changeset
|
81 |
@Test |
a51ca91c2cde
8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
47216
diff
changeset
|
82 |
public static void sendAddressedPacket() throws Exception { |
2 | 83 |
DatagramChannel sndChannel = DatagramChannel.open(); |
49284
a51ca91c2cde
8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
47216
diff
changeset
|
84 |
sndChannel.bind(null); |
16480
fcad3b6ec812
7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents:
5506
diff
changeset
|
85 |
InetAddress address = InetAddress.getLocalHost(); |
fcad3b6ec812
7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents:
5506
diff
changeset
|
86 |
if (address.isLoopbackAddress()) { |
fcad3b6ec812
7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents:
5506
diff
changeset
|
87 |
address = InetAddress.getLoopbackAddress(); |
fcad3b6ec812
7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents:
5506
diff
changeset
|
88 |
} |
2 | 89 |
InetSocketAddress sender = new InetSocketAddress( |
16480
fcad3b6ec812
7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents:
5506
diff
changeset
|
90 |
address, |
2 | 91 |
sndChannel.socket().getLocalPort()); |
92 |
||
93 |
DatagramChannel rcvChannel = DatagramChannel.open(); |
|
49284
a51ca91c2cde
8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
47216
diff
changeset
|
94 |
rcvChannel.bind(null); |
2 | 95 |
InetSocketAddress receiver = new InetSocketAddress( |
16480
fcad3b6ec812
7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents:
5506
diff
changeset
|
96 |
address, |
2 | 97 |
rcvChannel.socket().getLocalPort()); |
98 |
||
99 |
rcvChannel.connect(sender); |
|
100 |
sndChannel.connect(receiver); |
|
101 |
||
102 |
byte b[] = "hello".getBytes("UTF-8"); |
|
103 |
DatagramPacket pkt = new DatagramPacket(b, b.length); |
|
104 |
sndChannel.socket().send(pkt); |
|
105 |
||
106 |
ByteBuffer bb = ByteBuffer.allocate(256); |
|
107 |
rcvChannel.receive(bb); |
|
108 |
bb.flip(); |
|
109 |
CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb); |
|
49284
a51ca91c2cde
8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
47216
diff
changeset
|
110 |
assertTrue(cb.toString().startsWith("h"), "Unexpected message content"); |
2 | 111 |
|
112 |
// Check that the pkt got set with the target address; |
|
113 |
// This is legacy behavior |
|
49284
a51ca91c2cde
8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
47216
diff
changeset
|
114 |
assertEquals(pkt.getSocketAddress(), receiver, |
a51ca91c2cde
8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
47216
diff
changeset
|
115 |
"Unexpected address set on packet"); |
2 | 116 |
|
117 |
rcvChannel.close(); |
|
118 |
sndChannel.close(); |
|
119 |
} |
|
120 |
} |