Path: blob/master/test/jdk/java/net/MulticastSocket/NoLoopbackPackets.java
41149 views
/*1* Copyright (c) 2007, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 474217726* @library /test/lib27* @summary Re-test IPv6 (and specifically MulticastSocket) with latest Linux & USAGI code28*/29import java.util.*;30import java.net.*;31import jdk.test.lib.NetworkConfiguration;32import jdk.test.lib.net.IPSupport;3334public class NoLoopbackPackets {35private static String osname;3637static boolean isWindows() {38if (osname == null)39osname = System.getProperty("os.name");40return osname.contains("Windows");41}4243private static final String MESSAGE = "hello world (" + System.nanoTime() + ")";44public static void main(String[] args) throws Exception {45if (isWindows()) {46System.out.println("The test only run on non-Windows OS. Bye.");47return;48}4950MulticastSocket msock = null;51List<SocketAddress> failedGroups = new ArrayList<SocketAddress>();52Sender sender = null;53Thread senderThread = null;54try {55msock = new MulticastSocket();56int port = msock.getLocalPort();5758// we will send packets to three multicast groups :-59// 224.1.1.1, ::ffff:224.1.1.2, and ff02::1:160//61List<SocketAddress> groups = new ArrayList<SocketAddress>();62if (IPSupport.hasIPv4()) {63groups.add(new InetSocketAddress(InetAddress.getByName("224.1.1.1"), port));64}6566NetworkConfiguration nc = NetworkConfiguration.probe();67if (IPSupport.hasIPv6() && nc.hasTestableIPv6Address()) {68groups.add(new InetSocketAddress(InetAddress.getByName("::ffff:224.1.1.2"), port));69groups.add(new InetSocketAddress(InetAddress.getByName("ff02::1:1"), port));70}71if (groups.isEmpty()) {72System.err.println("Nothing to test: there are no network interfaces");73}7475sender = new Sender(groups);76senderThread = new Thread(sender);77senderThread.start();7879// Now try to receive multicast packets. we should not see any of them80// since we disable loopback mode.81//82msock.setSoTimeout(5000); // 5 seconds8384byte[] buf = new byte[1024];85for (int i = 0; i < buf.length; i++) {86buf[i] = (byte) 'z';87}88DatagramPacket packet = new DatagramPacket(buf, 0, buf.length);89byte[] expected = MESSAGE.getBytes();90assert expected.length <= buf.length;91for (SocketAddress group : groups) {92System.out.println("joining group: " + group);93msock.joinGroup(group, null);9495try {96do {97for (int i = 0; i < buf.length; i++) {98buf[i] = (byte) 'a';99}100msock.receive(packet);101byte[] data = packet.getData();102int len = packet.getLength();103104if (expected(data, len, expected)) {105failedGroups.add(group);106break;107} else {108System.err.println("WARNING: Unexpected packet received from "109+ group + ": "110+ len + " bytes");111System.err.println("\t as text: " + new String(data, 0, len));112}113} while (true);114} catch (SocketTimeoutException e) {115// we expect this116System.out.println("Received expected exception from " + group + ": " + e);117} finally {118msock.leaveGroup(group, null);119}120}121} finally {122if (msock != null) try { msock.close(); } catch (Exception e) {}123if (sender != null) {124sender.stop();125}126}127try {128if (failedGroups.size() > 0) {129System.out.println("We should not receive anything from following groups, but we did:");130for (SocketAddress group : failedGroups)131System.out.println(group);132throw new RuntimeException("test failed.");133}134} finally {135if (senderThread != null) {136senderThread.join();137}138}139}140141static boolean expected(byte[] data, int len, byte[] expected) {142if (len != expected.length) return false;143for (int i = 0; i < len; i++) {144if (data[i] != expected[i]) return false;145}146return true;147}148149static class Sender implements Runnable {150private List<SocketAddress> sendToGroups;151private volatile boolean stop;152153public Sender(List<SocketAddress> groups) {154sendToGroups = groups;155}156157public void run() {158byte[] buf = MESSAGE.getBytes();159List<DatagramPacket> packets = new ArrayList<DatagramPacket>();160161try (MulticastSocket msock = new MulticastSocket()) {162for (SocketAddress group : sendToGroups) {163DatagramPacket packet = new DatagramPacket(buf, buf.length, group);164packets.add(packet);165}166167msock.setLoopbackMode(true); // disable loopback mode168while (!stop) {169for (DatagramPacket packet : packets) {170msock.send(packet);171}172173Thread.sleep(1000); // 1 second174}175} catch (Exception e) {176throw new RuntimeException(e);177}178}179180void stop() {181stop = true;182}183}184}185186187