test/jdk/java/net/MulticastSocket/NoLoopbackPackets.java
branchdatagramsocketimpl-branch
changeset 58678 9cf78a70fa4f
parent 54770 62b6e7587b1f
child 58679 9c3209ff7550
equal deleted inserted replaced
58677:13588c901957 58678:9cf78a70fa4f
     1 /*
     1 /*
     2  * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2007, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    27  * @library /test/lib
    27  * @library /test/lib
    28  * @summary Re-test IPv6 (and specifically MulticastSocket) with latest Linux & USAGI code
    28  * @summary Re-test IPv6 (and specifically MulticastSocket) with latest Linux & USAGI code
    29  */
    29  */
    30 import java.util.*;
    30 import java.util.*;
    31 import java.net.*;
    31 import java.net.*;
       
    32 import jdk.test.lib.NetworkConfiguration;
    32 import jdk.test.lib.net.IPSupport;
    33 import jdk.test.lib.net.IPSupport;
    33 
    34 
    34 public class NoLoopbackPackets {
    35 public class NoLoopbackPackets {
    35     private static String osname;
    36     private static String osname;
    36 
    37 
    38         if (osname == null)
    39         if (osname == null)
    39             osname = System.getProperty("os.name");
    40             osname = System.getProperty("os.name");
    40         return osname.contains("Windows");
    41         return osname.contains("Windows");
    41     }
    42     }
    42 
    43 
       
    44     private static final String MESSAGE = "hello world (" + System.nanoTime() + ")";
    43     public static void main(String[] args) throws Exception {
    45     public static void main(String[] args) throws Exception {
    44         if (isWindows()) {
    46         if (isWindows()) {
    45             System.out.println("The test only run on non-Windows OS. Bye.");
    47             System.out.println("The test only run on non-Windows OS. Bye.");
    46             return;
    48             return;
    47         }
    49         }
    48 
    50 
    49         MulticastSocket msock = null;
    51         MulticastSocket msock = null;
    50         List<SocketAddress> failedGroups = new ArrayList<SocketAddress>();
    52         List<SocketAddress> failedGroups = new ArrayList<SocketAddress>();
    51         Sender sender = null;
    53         Sender sender = null;
       
    54         Thread senderThread = null;
    52         try {
    55         try {
    53             msock = new MulticastSocket();
    56             msock = new MulticastSocket();
    54             int port = msock.getLocalPort();
    57             int port = msock.getLocalPort();
    55 
    58 
    56             // we will send packets to three multicast groups :-
    59             // we will send packets to three multicast groups :-
    58             //
    61             //
    59             List<SocketAddress> groups = new ArrayList<SocketAddress>();
    62             List<SocketAddress> groups = new ArrayList<SocketAddress>();
    60             if (IPSupport.hasIPv4()) {
    63             if (IPSupport.hasIPv4()) {
    61                 groups.add(new InetSocketAddress(InetAddress.getByName("224.1.1.1"), port));
    64                 groups.add(new InetSocketAddress(InetAddress.getByName("224.1.1.1"), port));
    62             }
    65             }
    63             if (IPSupport.hasIPv6()) {
    66 
       
    67             NetworkConfiguration nc = NetworkConfiguration.probe();
       
    68             if (IPSupport.hasIPv6() && nc.hasTestableIPv6Address()) {
    64                 groups.add(new InetSocketAddress(InetAddress.getByName("::ffff:224.1.1.2"), port));
    69                 groups.add(new InetSocketAddress(InetAddress.getByName("::ffff:224.1.1.2"), port));
    65                 groups.add(new InetSocketAddress(InetAddress.getByName("ff02::1:1"), port));
    70                 groups.add(new InetSocketAddress(InetAddress.getByName("ff02::1:1"), port));
    66             }
    71             }
    67             if (groups.isEmpty()) {
    72             if (groups.isEmpty()) {
    68                 System.err.println("Nothing to test: there are no network interfaces");
    73                 System.err.println("Nothing to test: there are no network interfaces");
    69             }
    74             }
    70 
    75 
    71             sender = new Sender(groups);
    76             sender = new Sender(groups);
    72             new Thread(sender).start();
    77             senderThread = new Thread(sender);
       
    78             senderThread.start();
    73 
    79 
    74             // Now try to receive multicast packets. we should not see any of them
    80             // Now try to receive multicast packets. we should not see any of them
    75             // since we disable loopback mode.
    81             // since we disable loopback mode.
    76             //
    82             //
    77             msock.setSoTimeout(5000);       // 5 seconds
    83             msock.setSoTimeout(5000);       // 5 seconds
    78 
    84 
    79             byte[] buf = new byte[1024];
    85             byte[] buf = new byte[1024];
       
    86             for (int i = 0; i < buf.length; i++) {
       
    87                 buf[i] = (byte) 'z';
       
    88             }
    80             DatagramPacket packet = new DatagramPacket(buf, 0, buf.length);
    89             DatagramPacket packet = new DatagramPacket(buf, 0, buf.length);
       
    90             byte[] expected = MESSAGE.getBytes();
       
    91             assert expected.length <= buf.length;
    81             for (SocketAddress group : groups) {
    92             for (SocketAddress group : groups) {
       
    93                 System.out.println("joining group: " + group);
    82                 msock.joinGroup(group, null);
    94                 msock.joinGroup(group, null);
    83 
    95 
    84                 try {
    96                 try {
    85                     msock.receive(packet);
    97                     do {
       
    98                         for (int i = 0; i < buf.length; i++) {
       
    99                             buf[i] = (byte) 'a';
       
   100                         }
       
   101                         msock.receive(packet);
       
   102                         byte[] data = packet.getData();
       
   103                         int len = packet.getLength();
    86 
   104 
    87                     // it is an error if we receive something
   105                         if (expected(data, len, expected)) {
    88                     failedGroups.add(group);
   106                             failedGroups.add(group);
       
   107                             break;
       
   108                         } else {
       
   109                             System.err.println("WARNING: Unexpected packet received from "
       
   110                                                + group + ": "
       
   111                                                + len + " bytes");
       
   112                             System.err.println("\t as text: " + new String(data, 0, len));
       
   113                         }
       
   114                     } while (true);
    89                 } catch (SocketTimeoutException e) {
   115                 } catch (SocketTimeoutException e) {
    90                     // we expect this
   116                     // we expect this
       
   117                     System.out.println("Received expected exception from " + group + ": " + e);
       
   118                 } finally {
       
   119                     msock.leaveGroup(group, null);
    91                 }
   120                 }
    92 
       
    93                 msock.leaveGroup(group, null);
       
    94             }
   121             }
    95         } finally {
   122         } finally {
    96             if (msock != null) try { msock.close(); } catch (Exception e) {}
   123             if (msock != null) try { msock.close(); } catch (Exception e) {}
    97             if (sender != null) {
   124             if (sender != null) {
    98                 sender.stop();
   125                 sender.stop();
    99             }
   126             }
   100         }
   127         }
       
   128         try {
       
   129             if (failedGroups.size() > 0) {
       
   130                 System.out.println("We should not receive anything from following groups, but we did:");
       
   131                 for (SocketAddress group : failedGroups)
       
   132                     System.out.println(group);
       
   133                 throw new RuntimeException("test failed.");
       
   134             }
       
   135         } finally {
       
   136             if (senderThread != null) {
       
   137                 senderThread.join();
       
   138             }
       
   139         }
       
   140     }
   101 
   141 
   102         if (failedGroups.size() > 0) {
   142     static boolean expected(byte[] data, int len, byte[] expected) {
   103             System.out.println("We should not receive anything from following groups, but we did:");
   143         if (len != expected.length) return false;
   104             for (SocketAddress group : failedGroups)
   144         for (int i = 0; i < len; i++) {
   105                 System.out.println(group);
   145             if (data[i] != expected[i]) return false;
   106             throw new RuntimeException("test failed.");
       
   107         }
   146         }
       
   147         return true;
   108     }
   148     }
   109 
   149 
   110     static class Sender implements Runnable {
   150     static class Sender implements Runnable {
   111         private List<SocketAddress> sendToGroups;
   151         private List<SocketAddress> sendToGroups;
   112         private volatile boolean stop;
   152         private volatile boolean stop;
   114         public Sender(List<SocketAddress> groups) {
   154         public Sender(List<SocketAddress> groups) {
   115             sendToGroups = groups;
   155             sendToGroups = groups;
   116         }
   156         }
   117 
   157 
   118         public void run() {
   158         public void run() {
   119             byte[] buf = "hello world".getBytes();
   159             byte[] buf = MESSAGE.getBytes();
   120             List<DatagramPacket> packets = new ArrayList<DatagramPacket>();
   160             List<DatagramPacket> packets = new ArrayList<DatagramPacket>();
   121 
   161 
   122             try {
   162             try (MulticastSocket msock = new MulticastSocket()) {
   123                 for (SocketAddress group : sendToGroups) {
   163                 for (SocketAddress group : sendToGroups) {
   124                     DatagramPacket packet = new DatagramPacket(buf, buf.length, group);
   164                     DatagramPacket packet = new DatagramPacket(buf, buf.length, group);
   125                     packets.add(packet);
   165                     packets.add(packet);
   126                 }
   166                 }
   127 
   167 
   128                 MulticastSocket msock = new MulticastSocket();
       
   129                 msock.setLoopbackMode(true);    // disable loopback mode
   168                 msock.setLoopbackMode(true);    // disable loopback mode
   130                 while (!stop) {
   169                 while (!stop) {
   131                     for (DatagramPacket packet : packets) {
   170                     for (DatagramPacket packet : packets) {
   132                         msock.send(packet);
   171                         msock.send(packet);
   133                     }
   172                     }