23
23
24
24
/*
25
25
* @test
26
- * @bug 8239355 8242885
26
+ * @bug 8239355 8242885 8240901
27
+ * @key randomness
27
28
* @summary Check that it is possible to send and receive datagrams of
28
29
* maximum size on macOS.
29
30
* @library /test/lib
30
31
* @build jdk.test.lib.net.IPSupport
31
- * @requires os.family == "mac"
32
32
* @run testng/othervm SendReceiveMaxSize
33
33
* @run testng/othervm -Djava.net.preferIPv4Stack=true SendReceiveMaxSize
34
34
* @run testng/othervm -Djdk.net.usePlainDatagramSocketImpl SendReceiveMaxSize
35
35
* @run testng/othervm -Djdk.net.usePlainDatagramSocketImpl -Djava.net.preferIPv4Stack=true SendReceiveMaxSize
36
36
*/
37
37
38
+ import jdk .test .lib .RandomFactory ;
38
39
import jdk .test .lib .NetworkConfiguration ;
40
+ import jdk .test .lib .Platform ;
39
41
import jdk .test .lib .net .IPSupport ;
40
42
import org .testng .annotations .BeforeTest ;
41
43
import org .testng .annotations .DataProvider ;
49
51
import java .nio .ByteBuffer ;
50
52
import java .nio .channels .DatagramChannel ;
51
53
import java .util .ArrayList ;
54
+ import java .util .Random ;
52
55
import java .util .function .Predicate ;
53
56
54
57
import static java .net .StandardProtocolFamily .INET ;
@@ -65,6 +68,7 @@ public class SendReceiveMaxSize {
65
68
private final static int IPV4_SNDBUF = 65507 ;
66
69
private final static int IPV6_SNDBUF = 65527 ;
67
70
private final static Class <IOException > IOE = IOException .class ;
71
+ private final static Random random = RandomFactory .getRandom ();
68
72
69
73
public interface DatagramChannelSupplier {
70
74
DatagramChannel open () throws IOException ;
@@ -118,8 +122,10 @@ public Object[][] invariants() throws IOException {
118
122
@ Test (dataProvider = "invariants" )
119
123
public void testGetOption (DatagramChannelSupplier supplier , int capacity , InetAddress host )
120
124
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
+ }
123
129
}
124
130
}
125
131
@@ -133,17 +139,43 @@ public void testSendReceiveMaxSize(DatagramChannelSupplier supplier, int capacit
133
139
134
140
try (var sender = supplier .open ()) {
135
141
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 );
137
150
sender .send (sendBuf , addr );
138
151
var receiveBuf = ByteBuffer .allocate (capacity );
139
152
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 );
140
160
assertEquals (sendBuf , receiveBuf );
161
+ assertEquals (sendBuf .compareTo (receiveBuf ), 0 );
141
162
142
- sendBuf = ByteBuffer .allocate (capacity - 1 );
163
+ testData = new byte [capacity - 1 ];
164
+ random .nextBytes (testData );
165
+
166
+ sendBuf = ByteBuffer .wrap (testData );
143
167
sender .send (sendBuf , addr );
144
168
receiveBuf = ByteBuffer .allocate (capacity - 1 );
145
169
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 );
147
179
148
180
var failSendBuf = ByteBuffer .allocate (capacity + 1 );
149
181
assertThrows (IOE , () -> sender .send (failSendBuf , addr ));
0 commit comments