Path: blob/master/test/jdk/java/net/MulticastSocket/PromiscuousIPv6.java
41149 views
/*1* Copyright (c) 2018, 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 821529426* @requires os.family == "linux"27* @library /test/lib28* @build jdk.test.lib.NetworkConfiguration29* PromiscuousIPv630* @run main/othervm PromiscuousIPv631* @key randomness32*/3334import java.io.IOException;35import java.net.DatagramPacket;36import java.net.DatagramSocket;37import java.net.InetAddress;38import java.net.Inet6Address;39import java.net.InetSocketAddress;40import java.net.MulticastSocket;41import java.net.NetworkInterface;42import java.net.SocketTimeoutException;43import java.net.StandardSocketOptions;44import java.util.List;45import java.util.Random;46import jdk.test.lib.NetworkConfiguration;47import jtreg.SkippedException;48import static java.lang.System.out;49import static java.nio.charset.StandardCharsets.UTF_8;50import static java.util.stream.Collectors.toList;5152/*53* This test was created as a clone of the Promiscuous test and adapted for54* IPv6 node-local and link-local multicast addresses on Linux.55*/56public class PromiscuousIPv6 {5758static final Random rand = new Random();5960static final int TIMEOUT = 5 * 1000; // 5 secs6162static int sendDatagram(NetworkInterface nif, InetAddress group, int port)63throws IOException64{65try (MulticastSocket mc = new MulticastSocket()) {66mc.setOption(StandardSocketOptions.IP_MULTICAST_IF, nif);6768int id = rand.nextInt();69byte[] msg = Integer.toString(id).getBytes(UTF_8);70DatagramPacket p = new DatagramPacket(msg, msg.length);71p.setAddress(group);72p.setPort(port);7374out.printf("Sending datagram to: %s/%d\n", group, port);75mc.send(p);76return id;77}78}7980static void receiveDatagram(DatagramSocket mc, boolean datagramExpected, int id)81throws IOException82{83byte[] ba = new byte[100];84DatagramPacket p = new DatagramPacket(ba, ba.length);85try {86mc.receive(p);87int recvId = Integer.parseInt(88new String(p.getData(), 0, p.getLength(), UTF_8));89if (datagramExpected) {90if (recvId != id)91throw new RuntimeException("Unexpected id, got " + recvId92+ ", expected: " + id);93out.printf("Received message as expected, %s\n", p.getAddress());94} else {95throw new RuntimeException("Unexpected message received, "96+ p.getAddress());97}98} catch (SocketTimeoutException e) {99if (datagramExpected)100throw new RuntimeException("Expected message not received, "101+ e.getMessage());102else103out.printf("Message not received, as expected\n");104}105}106107static void test(NetworkInterface nif, InetAddress group1, InetAddress group2)108throws IOException109{110// Bind addresses should include the same network interface / scope, so111// as to not reply on the default route when there are multiple interfaces112InetAddress bindAddr1 = Inet6Address.getByAddress(null, group1.getAddress(), nif);113InetAddress bindAddr2 = Inet6Address.getByAddress(null, group2.getAddress(), nif);114115try (MulticastSocket mc1 = new MulticastSocket(new InetSocketAddress(bindAddr1, 0));116MulticastSocket mc2 = new MulticastSocket(new InetSocketAddress(bindAddr2, mc1.getLocalPort()))) {117118final int port = mc1.getLocalPort();119out.printf("Using port: %d\n", port);120121mc1.setSoTimeout(TIMEOUT);122mc2.setSoTimeout(TIMEOUT);123124mc1.joinGroup(new InetSocketAddress(group1, 0), nif);125out.printf("mc1 joined the MC group: %s\n", group1);126mc2.joinGroup(new InetSocketAddress(group2, 0), nif);127out.printf("mc2 joined the MC group: %s\n", group2);128129out.printf("Sending datagram to: %s/%d\n", group1, port);130int id = sendDatagram(nif, group1, port);131132// the packet should be received by mc1 only133receiveDatagram(mc1, true, id);134receiveDatagram(mc2, false, 0);135136137out.printf("Sending datagram to: %s/%d\n", group2, port);138id = sendDatagram(nif, group2, port);139140// the packet should be received by mc2 only141receiveDatagram(mc2, true, id);142receiveDatagram(mc1, false, 0);143144mc1.leaveGroup(new InetSocketAddress(group1, 0), nif);145mc2.leaveGroup(new InetSocketAddress(group2, 0), nif);146}147}148149public static void main(String args[]) throws IOException {150String os = System.getProperty("os.name");151152if (!os.equals("Linux")) {153throw new SkippedException("This test should be run only on Linux");154} else {155String osVersion = System.getProperty("os.version");156String prefix = "3.10.0";157if (osVersion.startsWith(prefix)) {158throw new SkippedException(159String.format("The behavior under test is known NOT to work on '%s' kernels", prefix));160}161}162163NetworkConfiguration.printSystemConfiguration(System.out);164List<NetworkInterface> nifs = NetworkConfiguration.probe()165.ip6MulticastInterfaces()166.collect(toList());167168if (nifs.size() == 0) {169throw new SkippedException(170"No IPv6 interfaces that support multicast found");171}172173InetAddress interfaceLocal1 = InetAddress.getByName("ff11::3.4.5.6");174InetAddress interfaceLocal2 = InetAddress.getByName("ff11::5.6.7.8");175176InetAddress linkLocal1 = InetAddress.getByName("ff12::4.5.6.7");177InetAddress linkLocal2 = InetAddress.getByName("ff12::7.8.9.10");178179for (NetworkInterface nif : nifs) {180test(nif, interfaceLocal1, interfaceLocal2);181test(nif, linkLocal1, linkLocal2);182}183}184}185186187