Skip to content

Commit 6348e80

Browse files
author
duke
committedAug 13, 2020
Automatic merge of jdk:master into master
2 parents d11e46e + a096c0a commit 6348e80

File tree

2 files changed

+58
-10
lines changed

2 files changed

+58
-10
lines changed
 

‎test/jdk/java/net/DatagramSocket/SendReceiveMaxSize.java

+19-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323

2424
/*
2525
* @test
26-
* @bug 8242885 8250886
26+
* @bug 8242885 8250886 8240901
27+
* @key randomness
2728
* @summary This test verifies that on macOS, the send buffer size is configured
2829
* by default so that none of our implementations of the UDP protocol
2930
* will fail with a "packet too large" exception when trying to send a
@@ -32,7 +33,6 @@
3233
* limit.
3334
* @library /test/lib
3435
* @build jdk.test.lib.net.IPSupport
35-
* @requires os.family == "mac"
3636
* @run testng/othervm SendReceiveMaxSize
3737
* @run testng/othervm -Djava.net.preferIPv4Stack=true SendReceiveMaxSize
3838
* @run testng/othervm -Djava.net.preferIPv6Addresses=true SendReceiveMaxSize
@@ -41,6 +41,8 @@
4141
* @run testng/othervm -Djdk.net.usePlainDatagramSocketImpl -Djava.net.preferIPv6Addresses=true SendReceiveMaxSize
4242
*/
4343

44+
import jdk.test.lib.RandomFactory;
45+
import jdk.test.lib.Platform;
4446
import jdk.test.lib.net.IPSupport;
4547
import org.testng.annotations.BeforeTest;
4648
import org.testng.annotations.DataProvider;
@@ -54,7 +56,9 @@
5456
import java.net.InetSocketAddress;
5557
import java.net.MulticastSocket;
5658
import java.nio.channels.DatagramChannel;
59+
import java.util.Random;
5760

61+
import static org.testng.Assert.assertEquals;
5862
import static org.testng.Assert.expectThrows;
5963

6064
public class SendReceiveMaxSize {
@@ -63,6 +67,7 @@ public class SendReceiveMaxSize {
6367
private final static int IPV4_SNDBUF = 65507;
6468
private final static int IPV6_SNDBUF = 65527;
6569
private final static Class<IOException> IOE = IOException.class;
70+
private final static Random random = RandomFactory.getRandom();
6671

6772
public interface DatagramSocketSupplier {
6873
DatagramSocket open() throws IOException;
@@ -102,14 +107,25 @@ public void testSendReceiveMaxSize(String name, int capacity,
102107
var port = receiver.getLocalPort();
103108
var addr = new InetSocketAddress(HOST_ADDR, port);
104109
try (var sender = supplier.open()) {
105-
var sendPkt = new DatagramPacket(new byte[capacity], capacity, addr);
110+
if (!Platform.isOSX()) {
111+
if (sender.getSendBufferSize() < capacity)
112+
sender.setSendBufferSize(capacity);
113+
}
114+
byte[] testData = new byte[capacity];
115+
random.nextBytes(testData);
116+
var sendPkt = new DatagramPacket(testData, capacity, addr);
117+
106118
if (exception != null) {
107119
Exception ex = expectThrows(IOE, () -> sender.send(sendPkt));
108120
System.out.println(name + " got expected exception: " + ex);
109121
} else {
110122
sender.send(sendPkt);
111123
var receivePkt = new DatagramPacket(new byte[capacity], capacity);
112124
receiver.receive(receivePkt);
125+
126+
// check packet data has been fragmented and re-assembled correctly at receiver
127+
assertEquals(receivePkt.getLength(), capacity);
128+
assertEquals(receivePkt.getData(), testData);
113129
}
114130
}
115131
}

‎test/jdk/java/nio/channels/DatagramChannel/SendReceiveMaxSize.java

+39-7
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,21 @@
2323

2424
/*
2525
* @test
26-
* @bug 8239355 8242885
26+
* @bug 8239355 8242885 8240901
27+
* @key randomness
2728
* @summary Check that it is possible to send and receive datagrams of
2829
* maximum size on macOS.
2930
* @library /test/lib
3031
* @build jdk.test.lib.net.IPSupport
31-
* @requires os.family == "mac"
3232
* @run testng/othervm SendReceiveMaxSize
3333
* @run testng/othervm -Djava.net.preferIPv4Stack=true SendReceiveMaxSize
3434
* @run testng/othervm -Djdk.net.usePlainDatagramSocketImpl SendReceiveMaxSize
3535
* @run testng/othervm -Djdk.net.usePlainDatagramSocketImpl -Djava.net.preferIPv4Stack=true SendReceiveMaxSize
3636
*/
3737

38+
import jdk.test.lib.RandomFactory;
3839
import jdk.test.lib.NetworkConfiguration;
40+
import jdk.test.lib.Platform;
3941
import jdk.test.lib.net.IPSupport;
4042
import org.testng.annotations.BeforeTest;
4143
import org.testng.annotations.DataProvider;
@@ -49,6 +51,7 @@
4951
import java.nio.ByteBuffer;
5052
import java.nio.channels.DatagramChannel;
5153
import java.util.ArrayList;
54+
import java.util.Random;
5255
import java.util.function.Predicate;
5356

5457
import static java.net.StandardProtocolFamily.INET;
@@ -65,6 +68,7 @@ public class SendReceiveMaxSize {
6568
private final static int IPV4_SNDBUF = 65507;
6669
private final static int IPV6_SNDBUF = 65527;
6770
private final static Class<IOException> IOE = IOException.class;
71+
private final static Random random = RandomFactory.getRandom();
6872

6973
public interface DatagramChannelSupplier {
7074
DatagramChannel open() throws IOException;
@@ -118,8 +122,10 @@ public Object[][] invariants() throws IOException {
118122
@Test(dataProvider = "invariants")
119123
public void testGetOption(DatagramChannelSupplier supplier, int capacity, InetAddress host)
120124
throws IOException {
121-
try (var dc = supplier.open()) {
122-
assertTrue(dc.getOption(SO_SNDBUF) >= capacity);
125+
if (Platform.isOSX()) {
126+
try (var dc = supplier.open()){
127+
assertTrue(dc.getOption(SO_SNDBUF) >= capacity);
128+
}
123129
}
124130
}
125131

@@ -133,17 +139,43 @@ public void testSendReceiveMaxSize(DatagramChannelSupplier supplier, int capacit
133139

134140
try (var sender = supplier.open()) {
135141
sender.bind(null);
136-
var sendBuf = ByteBuffer.allocate(capacity);
142+
if (!Platform.isOSX()) {
143+
if (sender.getOption(SO_SNDBUF) < capacity)
144+
sender.setOption(SO_SNDBUF, capacity);
145+
}
146+
byte[] testData = new byte[capacity];
147+
random.nextBytes(testData);
148+
149+
var sendBuf = ByteBuffer.wrap(testData);
137150
sender.send(sendBuf, addr);
138151
var receiveBuf = ByteBuffer.allocate(capacity);
139152
receiver.receive(receiveBuf);
153+
154+
sendBuf.flip();
155+
receiveBuf.flip();
156+
157+
// check that data has been fragmented and re-assembled correctly at receiver
158+
System.out.println("sendBuf: " + sendBuf);
159+
System.out.println("receiveBuf: " + receiveBuf);
140160
assertEquals(sendBuf, receiveBuf);
161+
assertEquals(sendBuf.compareTo(receiveBuf), 0);
141162

142-
sendBuf = ByteBuffer.allocate(capacity - 1);
163+
testData = new byte[capacity - 1];
164+
random.nextBytes(testData);
165+
166+
sendBuf = ByteBuffer.wrap(testData);
143167
sender.send(sendBuf, addr);
144168
receiveBuf = ByteBuffer.allocate(capacity - 1);
145169
receiver.receive(receiveBuf);
146-
assertTrue(sendBuf.compareTo(receiveBuf) == 0);
170+
171+
sendBuf.flip();
172+
receiveBuf.flip();
173+
174+
// check that data has been fragmented and re-assembled correctly at receiver
175+
System.out.println("sendBuf: " + sendBuf);
176+
System.out.println("receiveBuf: " + receiveBuf);
177+
assertEquals(sendBuf, receiveBuf);
178+
assertEquals(sendBuf.compareTo(receiveBuf), 0);
147179

148180
var failSendBuf = ByteBuffer.allocate(capacity + 1);
149181
assertThrows(IOE, () -> sender.send(failSendBuf, addr));

0 commit comments

Comments
 (0)
Please sign in to comment.