Skip to content
Permalink
Browse files
8249773: Upgrade ReceiveISA.java test to be resilient to failure due …
…to stray packets and interference

This fix upgrades java/nio/channels/DatagramChannel/ReceiveISA.java so it can handle interference from stray packets.

Backport-of: d8355e0
  • Loading branch information
shipilev committed Jul 11, 2021
1 parent 2c49cca commit 3869a19fbda7559102cb7c0c64eab87a28bb34ee
Showing with 78 additions and 45 deletions.
  1. +78 −45 test/jdk/java/nio/channels/DatagramChannel/ReceiveISA.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -23,66 +23,99 @@

/*
* @test
* @bug 4503641 8130394
* @bug 4503641 8130394 8249773
* @summary Check that DatagramChannel.receive returns a new SocketAddress
* when it receives a packet from the same source address but
* different endpoint.
*/
import java.nio.*;
import java.nio.channels.*;
import java.net.*;
import static java.lang.System.out;

public class ReceiveISA {

public static void main(String args[]) throws Exception {

// clients
DatagramChannel dc1 = DatagramChannel.open();
DatagramChannel dc2 = DatagramChannel.open();

// bind server to any port
DatagramChannel dc3 = DatagramChannel.open();
dc3.socket().bind((SocketAddress)null);

// get server address
InetAddress lh = InetAddress.getLocalHost();
InetSocketAddress isa
= new InetSocketAddress( lh, dc3.socket().getLocalPort() );

ByteBuffer bb = ByteBuffer.allocateDirect(100);
bb.put("Dia duit!".getBytes());
bb.flip();

dc1.send(bb, isa); // packet 1 from dc1
dc1.send(bb, isa); // packet 2 from dc1
dc2.send(bb, isa); // packet 3 from dc1

// receive 3 packets
dc3.socket().setSoTimeout(1000);
ByteBuffer rb = ByteBuffer.allocateDirect(100);
SocketAddress sa[] = new SocketAddress[3];
for (int i=0; i<3; i++) {
sa[i] = dc3.receive(rb);
System.out.println("received "+ sa[i] );
rb.clear();
}
String regex = "Dia duit![0-2]";

dc1.close();
dc2.close();
dc3.close();
try (DatagramChannel dc1 = DatagramChannel.open(); // client
DatagramChannel dc2 = DatagramChannel.open(); // client
DatagramChannel dc3 = DatagramChannel.open();
DatagramChannel dc4 = DatagramChannel.open()) { // client

/*
* Check that sa[0] equals sa[1] (both from dc1)
* Check that sa[1] not equal to sa[2] (one from dc1, one from dc2)
*/
dc3.socket().bind((SocketAddress) null); // bind server to any port

if (!sa[0].equals(sa[1])) {
throw new Exception("Source address for packets 1 & 2 should be equal");
}
// get server address
InetAddress lh = InetAddress.getLocalHost();
InetSocketAddress isa = new InetSocketAddress(lh, dc3.socket().getLocalPort());

ByteBuffer bb = ByteBuffer.allocateDirect(100);
bb.put("Dia duit!0".getBytes());
bb.flip();

ByteBuffer bb1 = ByteBuffer.allocateDirect(100);
bb1.put("Dia duit!1".getBytes());
bb1.flip();

ByteBuffer bb2 = ByteBuffer.allocateDirect(100);
bb2.put("Dia duit!2".getBytes());
bb2.flip();

ByteBuffer bb3 = ByteBuffer.allocateDirect(100);
bb3.put("garbage".getBytes());
bb3.flip();

dc1.send(bb, isa); // packet 1 from dc1
dc4.send(bb3, isa); // interference, packet 4 from dc4
dc1.send(bb1, isa); // packet 2 from dc1
dc2.send(bb2, isa); // packet 3 from dc2

if (sa[1].equals(sa[2])) {
throw new Exception("Source address for packets 2 & 3 should be different");

// receive 4 packets
dc3.socket().setSoTimeout(1000);
ByteBuffer rb = ByteBuffer.allocateDirect(100);
SocketAddress sa[] = new SocketAddress[3];

for (int i = 0; i < 3;) {
SocketAddress receiver = dc3.receive(rb);
rb.flip();
byte[] bytes = new byte[rb.limit()];
rb.get(bytes, 0, rb.limit());
String msg = new String(bytes);

if (msg.matches("Dia duit![0-2]")) {
if (msg.equals("Dia duit!0")) {
sa[0] = receiver;
i++;
}
if (msg.equals("Dia duit!1")) {
sa[1] = receiver;
i++;
}
if (msg.equals("Dia duit!2")) {
sa[2] = receiver;
i++;
}
} else {
out.println("Interfered packet sender address is : " + receiver);
out.println("random interfered packet is : " + msg);
}
rb.clear();
}

/*
* Check that sa[0] equals sa[1] (both from dc1)
* Check that sa[1] not equal to sa[2] (one from dc1, one from dc2)
*/

if (!sa[0].equals(sa[1])) {
throw new Exception("Source address for packets 1 & 2 should be equal");
}

if (sa[1].equals(sa[2])) {
throw new Exception("Source address for packets 2 & 3 should be different");
}
}
}

}

1 comment on commit 3869a19

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on 3869a19 Jul 11, 2021

Please sign in to comment.