< prev index next >

test/jdk/java/nio/channels/DatagramChannel/ManySenders.java

Print this page
rev 57162 : 8235169: [TESTBUG] java/nio/channels/DatagramChannel/ManySenders.java fails on Ubuntu18.04
Reviewed-by:


  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 8234805
  26  * @summary Test that DatagramChannel.receive returns the expected sender address
  27  * @run main ManySenders
  28  * @run main/othervm -Djava.net.preferIPv4Stack=true ManySenders
  29  */
  30 
  31 import java.io.ByteArrayInputStream;
  32 import java.io.ByteArrayOutputStream;
  33 import java.io.ObjectInputStream;
  34 import java.io.ObjectOutputStream;
  35 import java.net.InetAddress;
  36 import java.net.InetSocketAddress;
  37 import java.net.NetworkInterface;
  38 import java.net.SocketAddress;
  39 import java.nio.ByteBuffer;
  40 import java.nio.channels.DatagramChannel;

  41 import java.util.List;
  42 import java.util.stream.Collectors;
  43 import java.util.stream.Stream;
  44 
  45 public class ManySenders {
  46     public static void main(String[] args) throws Exception {
  47 
  48         // use addresses on interfaces that have the loopback and local host
  49         InetAddress lh = InetAddress.getLocalHost();

  50         InetAddress lb = InetAddress.getLoopbackAddress();
  51         List<InetAddress> addresses = Stream.concat(
  52                 NetworkInterface.getByInetAddress(lh).inetAddresses(),
  53                 NetworkInterface.getByInetAddress(lb).inetAddresses())
  54                 .filter(ia -> !ia.isAnyLocalAddress())
  55                 .distinct()
  56                 .collect(Collectors.toList());
  57 
  58         // bind DatagramChannel to wildcard address so it can receive from any address
  59         try (DatagramChannel reader = DatagramChannel.open()) {
  60             reader.bind(new InetSocketAddress(0));
  61             for (InetAddress address : addresses) {
  62                 System.out.format("%n-- %s --%n", address.getHostAddress());
  63 
  64                 // send 3 datagrams from the given address to the reader
  65                 test(3, address, reader);
  66             }
  67         }
  68     }

  69 
  70     static void test(int count, InetAddress address, DatagramChannel reader) throws Exception {
  71         int remotePort = reader.socket().getLocalPort();
  72         InetSocketAddress remote = new InetSocketAddress(address, remotePort);
  73 
  74         try (DatagramChannel sender = DatagramChannel.open()) {
  75             sender.bind(new InetSocketAddress(address, 0));
  76 
  77             SocketAddress local = sender.getLocalAddress();
  78             byte[] bytes = serialize(local);
  79 
  80             SocketAddress previousSource = null;
  81             for (int i = 0; i < count; i++) {
  82                 System.out.format("send %s -> %s%n", local, remote);
  83                 sender.send(ByteBuffer.wrap(bytes), remote);
  84 
  85                 ByteBuffer bb = ByteBuffer.allocate(1000);
  86                 SocketAddress source = reader.receive(bb);
  87                 System.out.format("received datagram from %s%n", source);
  88 
  89                 // check source address and payload
  90                 SocketAddress payload = deserialize(bb.array());
  91                 if (!source.equals(local))
  92                     throw new RuntimeException("source=" + source + ", expected=" + local);
  93                 if (!payload.equals(local))
  94                     throw new RuntimeException("payload=" + payload + ", expected=" + local);
  95 
  96                 // check that cached source was used
  97                 if (previousSource == null) {
  98                     previousSource = source;
  99                 } else if (source != previousSource) {
 100                     throw new RuntimeException("Cached SocketAddress not returned");
 101                 }
 102             }
 103         }
 104     }
 105 















 106     private static byte[] serialize(SocketAddress address) throws Exception {
 107         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 108         ObjectOutputStream oos = new ObjectOutputStream(baos);
 109         oos.writeObject(address);
 110         oos.close();
 111         return baos.toByteArray();
 112     }
 113 
 114     private static SocketAddress deserialize(byte[] bytes) throws Exception {
 115         ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
 116         ObjectInputStream ois = new ObjectInputStream(bais);
 117         return (SocketAddress) ois.readObject();
 118     }
 119 }


  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 8234805
  26  * @summary Test that DatagramChannel.receive returns the expected sender address
  27  * @run main ManySenders
  28  * @run main/othervm -Djava.net.preferIPv4Stack=true ManySenders
  29  */
  30 
  31 import java.io.ByteArrayInputStream;
  32 import java.io.ByteArrayOutputStream;
  33 import java.io.ObjectInputStream;
  34 import java.io.ObjectOutputStream;
  35 import java.net.InetAddress;
  36 import java.net.InetSocketAddress;
  37 import java.net.NetworkInterface;
  38 import java.net.SocketAddress;
  39 import java.nio.ByteBuffer;
  40 import java.nio.channels.DatagramChannel;
  41 import java.util.Collections;
  42 import java.util.List;
  43 import java.util.stream.Collectors;
  44 import java.util.stream.Stream;
  45 
  46 public class ManySenders {
  47     public static void main(String[] args) throws Exception {
  48 
  49         // use addresses on interfaces that have the loopback and local host
  50         InetAddress lh = getLocalHost();
  51         if (lh != null) {
  52             InetAddress lb = InetAddress.getLoopbackAddress();
  53             List<InetAddress> addresses = Stream.concat(
  54                     NetworkInterface.getByInetAddress(lh).inetAddresses(),
  55                     NetworkInterface.getByInetAddress(lb).inetAddresses())
  56                     .filter(ia -> !ia.isAnyLocalAddress())
  57                     .distinct()
  58                     .collect(Collectors.toList());
  59 
  60             // bind DatagramChannel to wildcard address so it can receive from any address
  61             try (DatagramChannel reader = DatagramChannel.open()) {
  62                 reader.bind(new InetSocketAddress(0));
  63                 for (InetAddress address : addresses) {
  64                     System.out.format("%n-- %s --%n", address.getHostAddress());
  65 
  66                     // send 3 datagrams from the given address to the reader
  67                     test(3, address, reader);
  68                 }
  69             }
  70         }
  71     }
  72 
  73     static void test(int count, InetAddress address, DatagramChannel reader) throws Exception {
  74         int remotePort = reader.socket().getLocalPort();
  75         InetSocketAddress remote = new InetSocketAddress(address, remotePort);
  76 
  77         try (DatagramChannel sender = DatagramChannel.open()) {
  78             sender.bind(new InetSocketAddress(address, 0));
  79 
  80             SocketAddress local = sender.getLocalAddress();
  81             byte[] bytes = serialize(local);
  82 
  83             SocketAddress previousSource = null;
  84             for (int i = 0; i < count; i++) {
  85                 System.out.format("send %s -> %s%n", local, remote);
  86                 sender.send(ByteBuffer.wrap(bytes), remote);
  87 
  88                 ByteBuffer bb = ByteBuffer.allocate(1000);
  89                 SocketAddress source = reader.receive(bb);
  90                 System.out.format("received datagram from %s%n", source);
  91 
  92                 // check source address and payload
  93                 SocketAddress payload = deserialize(bb.array());
  94                 if (!source.equals(local))
  95                     throw new RuntimeException("source=" + source + ", expected=" + local);
  96                 if (!payload.equals(local))
  97                     throw new RuntimeException("payload=" + payload + ", expected=" + local);
  98 
  99                 // check that cached source was used
 100                 if (previousSource == null) {
 101                     previousSource = source;
 102                 } else if (source != previousSource) {
 103                     throw new RuntimeException("Cached SocketAddress not returned");
 104                 }
 105             }
 106         }
 107     }
 108 
 109     private static InetAddress getLocalHost() throws Exception {
 110         List<NetworkInterface> nics = Collections.list(NetworkInterface.getNetworkInterfaces());
 111         for (NetworkInterface nic : nics) {
 112             if (nic.isLoopback())
 113                 continue;
 114 
 115             List<InetAddress> addrs = Collections.list(nic.getInetAddresses());
 116             for (InetAddress addr : addrs) {
 117                 if (NetworkInterface.getByInetAddress(addr) != null)
 118                     return addr;
 119             }
 120         }
 121         return null;
 122     }
 123 
 124     private static byte[] serialize(SocketAddress address) throws Exception {
 125         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 126         ObjectOutputStream oos = new ObjectOutputStream(baos);
 127         oos.writeObject(address);
 128         oos.close();
 129         return baos.toByteArray();
 130     }
 131 
 132     private static SocketAddress deserialize(byte[] bytes) throws Exception {
 133         ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
 134         ObjectInputStream ois = new ObjectInputStream(bais);
 135         return (SocketAddress) ois.readObject();
 136     }
 137 }
< prev index next >