From d8355e0281e19cb2cf9fff070f3c7540de7bcb95 Mon Sep 17 00:00:00 2001 From: Rahul Yadav Date: Thu, 13 Aug 2020 17:48:15 +0100 Subject: [PATCH] 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. Reviewed-by: alanb, dfuchs --- .../channels/DatagramChannel/ReceiveISA.java | 123 +++++++++++------- 1 file changed, 78 insertions(+), 45 deletions(-) diff --git a/test/jdk/java/nio/channels/DatagramChannel/ReceiveISA.java b/test/jdk/java/nio/channels/DatagramChannel/ReceiveISA.java index ea646cf80a2..69df204e915 100644 --- a/test/jdk/java/nio/channels/DatagramChannel/ReceiveISA.java +++ b/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,7 +23,7 @@ /* * @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. @@ -31,58 +31,91 @@ 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"); + } } } - }