|
1 /* |
|
2 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
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 |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
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 } |