--- a/test/jdk/java/net/MulticastSocket/NoLoopbackPackets.java Thu Oct 17 20:27:44 2019 +0100
+++ b/test/jdk/java/net/MulticastSocket/NoLoopbackPackets.java Thu Oct 17 20:53:35 2019 +0100
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 2019, 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
@@ -29,6 +29,7 @@
*/
import java.util.*;
import java.net.*;
+import jdk.test.lib.NetworkConfiguration;
import jdk.test.lib.net.IPSupport;
public class NoLoopbackPackets {
@@ -40,6 +41,7 @@
return osname.contains("Windows");
}
+ private static final String MESSAGE = "hello world (" + System.nanoTime() + ")";
public static void main(String[] args) throws Exception {
if (isWindows()) {
System.out.println("The test only run on non-Windows OS. Bye.");
@@ -49,6 +51,7 @@
MulticastSocket msock = null;
List<SocketAddress> failedGroups = new ArrayList<SocketAddress>();
Sender sender = null;
+ Thread senderThread = null;
try {
msock = new MulticastSocket();
int port = msock.getLocalPort();
@@ -60,7 +63,9 @@
if (IPSupport.hasIPv4()) {
groups.add(new InetSocketAddress(InetAddress.getByName("224.1.1.1"), port));
}
- if (IPSupport.hasIPv6()) {
+
+ NetworkConfiguration nc = NetworkConfiguration.probe();
+ if (IPSupport.hasIPv6() && nc.hasTestableIPv6Address()) {
groups.add(new InetSocketAddress(InetAddress.getByName("::ffff:224.1.1.2"), port));
groups.add(new InetSocketAddress(InetAddress.getByName("ff02::1:1"), port));
}
@@ -69,7 +74,8 @@
}
sender = new Sender(groups);
- new Thread(sender).start();
+ senderThread = new Thread(sender);
+ senderThread.start();
// Now try to receive multicast packets. we should not see any of them
// since we disable loopback mode.
@@ -77,20 +83,41 @@
msock.setSoTimeout(5000); // 5 seconds
byte[] buf = new byte[1024];
+ for (int i = 0; i < buf.length; i++) {
+ buf[i] = (byte) 'z';
+ }
DatagramPacket packet = new DatagramPacket(buf, 0, buf.length);
+ byte[] expected = MESSAGE.getBytes();
+ assert expected.length <= buf.length;
for (SocketAddress group : groups) {
+ System.out.println("joining group: " + group);
msock.joinGroup(group, null);
try {
- msock.receive(packet);
+ do {
+ for (int i = 0; i < buf.length; i++) {
+ buf[i] = (byte) 'a';
+ }
+ msock.receive(packet);
+ byte[] data = packet.getData();
+ int len = packet.getLength();
- // it is an error if we receive something
- failedGroups.add(group);
+ if (expected(data, len, expected)) {
+ failedGroups.add(group);
+ break;
+ } else {
+ System.err.println("WARNING: Unexpected packet received from "
+ + group + ": "
+ + len + " bytes");
+ System.err.println("\t as text: " + new String(data, 0, len));
+ }
+ } while (true);
} catch (SocketTimeoutException e) {
// we expect this
+ System.out.println("Received expected exception from " + group + ": " + e);
+ } finally {
+ msock.leaveGroup(group, null);
}
-
- msock.leaveGroup(group, null);
}
} finally {
if (msock != null) try { msock.close(); } catch (Exception e) {}
@@ -98,13 +125,26 @@
sender.stop();
}
}
+ try {
+ if (failedGroups.size() > 0) {
+ System.out.println("We should not receive anything from following groups, but we did:");
+ for (SocketAddress group : failedGroups)
+ System.out.println(group);
+ throw new RuntimeException("test failed.");
+ }
+ } finally {
+ if (senderThread != null) {
+ senderThread.join();
+ }
+ }
+ }
- if (failedGroups.size() > 0) {
- System.out.println("We should not receive anything from following groups, but we did:");
- for (SocketAddress group : failedGroups)
- System.out.println(group);
- throw new RuntimeException("test failed.");
+ static boolean expected(byte[] data, int len, byte[] expected) {
+ if (len != expected.length) return false;
+ for (int i = 0; i < len; i++) {
+ if (data[i] != expected[i]) return false;
}
+ return true;
}
static class Sender implements Runnable {
@@ -116,16 +156,15 @@
}
public void run() {
- byte[] buf = "hello world".getBytes();
+ byte[] buf = MESSAGE.getBytes();
List<DatagramPacket> packets = new ArrayList<DatagramPacket>();
- try {
+ try (MulticastSocket msock = new MulticastSocket()) {
for (SocketAddress group : sendToGroups) {
DatagramPacket packet = new DatagramPacket(buf, buf.length, group);
packets.add(packet);
}
- MulticastSocket msock = new MulticastSocket();
msock.setLoopbackMode(true); // disable loopback mode
while (!stop) {
for (DatagramPacket packet : packets) {