1 /* |
1 /* |
2 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2001, 2012, 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. |
25 * @bug 4313882 4981129 |
25 * @bug 4313882 4981129 |
26 * @summary Unit test for datagram-socket-channel adaptors |
26 * @summary Unit test for datagram-socket-channel adaptors |
27 * @library .. |
27 * @library .. |
28 */ |
28 */ |
29 |
29 |
30 import java.io.*; |
|
31 import java.net.*; |
30 import java.net.*; |
32 import java.nio.*; |
|
33 import java.nio.channels.*; |
31 import java.nio.channels.*; |
34 import java.nio.charset.*; |
|
35 import java.util.*; |
32 import java.util.*; |
36 |
33 |
37 |
34 |
38 public class AdaptDatagramSocket { |
35 public class AdaptDatagramSocket { |
39 |
36 |
40 static java.io.PrintStream out = System.out; |
37 static java.io.PrintStream out = System.out; |
41 |
|
42 static Random rand = new Random(); |
38 static Random rand = new Random(); |
43 |
|
44 static final int ECHO_PORT = 7; |
|
45 static final int DISCARD_PORT = 9; |
|
46 static final String REMOTE_HOST = TestUtil.HOST; |
|
47 |
|
48 static final InetSocketAddress echoAddress |
|
49 = new InetSocketAddress(REMOTE_HOST, ECHO_PORT); |
|
50 static final InetSocketAddress discardAddress |
|
51 = new InetSocketAddress(REMOTE_HOST, DISCARD_PORT); |
|
52 |
39 |
53 static String toString(DatagramPacket dp) { |
40 static String toString(DatagramPacket dp) { |
54 return ("DatagramPacket[off=" + dp.getOffset() |
41 return ("DatagramPacket[off=" + dp.getOffset() |
55 + ", len=" + dp.getLength() |
42 + ", len=" + dp.getLength() |
56 + "]"); |
43 + "]"); |
86 } |
73 } |
87 |
74 |
88 out.println("rtt: " + (System.currentTimeMillis() - start)); |
75 out.println("rtt: " + (System.currentTimeMillis() - start)); |
89 out.println("post op: " + toString(op) + " ip: " + toString(ip)); |
76 out.println("post op: " + toString(op) + " ip: " + toString(ip)); |
90 |
77 |
91 for (int i = 0; i < ip.getLength(); i++) |
78 for (int i = 0; i < ip.getLength(); i++) { |
92 if (ip.getData()[ip.getOffset() + i] |
79 if (ip.getData()[ip.getOffset() + i] |
93 != op.getData()[op.getOffset() + i]) |
80 != op.getData()[op.getOffset() + i]) |
94 throw new Exception("Incorrect data received"); |
81 throw new Exception("Incorrect data received"); |
|
82 } |
95 |
83 |
96 if (!(ip.getSocketAddress().equals(dst))) { |
84 if (!(ip.getSocketAddress().equals(dst))) { |
97 throw new Exception("Incorrect sender address, expected: " + dst |
85 throw new Exception("Incorrect sender address, expected: " + dst |
98 + " actual: " + ip.getSocketAddress()); |
86 + " actual: " + ip.getSocketAddress()); |
99 } |
87 } |
128 |
116 |
129 if (timeout > 0) |
117 if (timeout > 0) |
130 ds.setSoTimeout(timeout); |
118 ds.setSoTimeout(timeout); |
131 out.println("timeout: " + ds.getSoTimeout()); |
119 out.println("timeout: " + ds.getSoTimeout()); |
132 |
120 |
133 for (int i = 0; i < 5; i++) |
121 for (int i = 0; i < 5; i++) { |
134 test(ds, dst, shouldTimeout); |
122 test(ds, dst, shouldTimeout); |
|
123 } |
135 |
124 |
136 // Leave the socket open so that we don't reuse the old src address |
125 // Leave the socket open so that we don't reuse the old src address |
137 //ds.close(); |
126 //ds.close(); |
138 |
127 |
139 } |
128 } |
140 |
129 |
141 public static void main(String[] args) throws Exception { |
130 public static void main(String[] args) throws Exception { |
142 test(echoAddress, 0, false, false); |
131 // need an UDP echo server |
143 test(echoAddress, 0, false, true); |
132 try (TestServers.UdpEchoServer echoServer |
144 test(echoAddress, 5000, false, false); |
133 = TestServers.UdpEchoServer.startNewServer(100)) { |
145 test(discardAddress, 10, true, false); |
134 final InetSocketAddress address |
|
135 = new InetSocketAddress(echoServer.getAddress(), |
|
136 echoServer.getPort()); |
|
137 test(address, 0, false, false); |
|
138 test(address, 0, false, true); |
|
139 test(address, 5000, false, false); |
|
140 } |
|
141 try (TestServers.UdpDiscardServer discardServer |
|
142 = TestServers.UdpDiscardServer.startNewServer()) { |
|
143 final InetSocketAddress address |
|
144 = new InetSocketAddress(discardServer.getAddress(), |
|
145 discardServer.getPort()); |
|
146 test(address, 10, true, false); |
|
147 } |
146 } |
148 } |
147 |
149 |
148 } |
150 } |