AdaptDatagramSocket.java revision 9330:8b1f1c2a400f
1219019Sgabor/*
2219019Sgabor * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
3219019Sgabor * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4219019Sgabor *
5219019Sgabor * This code is free software; you can redistribute it and/or modify it
6219019Sgabor * under the terms of the GNU General Public License version 2 only, as
7219019Sgabor * published by the Free Software Foundation.
8219019Sgabor *
9219019Sgabor * This code is distributed in the hope that it will be useful, but WITHOUT
10219019Sgabor * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11219019Sgabor * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12219019Sgabor * version 2 for more details (a copy is included in the LICENSE file that
13219019Sgabor * accompanied this code).
14219019Sgabor *
15219019Sgabor * You should have received a copy of the GNU General Public License version
16219019Sgabor * 2 along with this work; if not, write to the Free Software Foundation,
17219019Sgabor * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18219019Sgabor *
19219019Sgabor * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20219019Sgabor * or visit www.oracle.com if you need additional information or have any
21219019Sgabor * questions.
22219019Sgabor */
23219019Sgabor
24219019Sgabor/* @test
25219019Sgabor * @bug 4313882 4981129
26219019Sgabor * @summary Unit test for datagram-socket-channel adaptors
27219019Sgabor * @library ..
28219019Sgabor */
29219019Sgabor
30219019Sgaborimport java.net.*;
31219019Sgaborimport java.nio.channels.*;
32219019Sgaborimport java.util.*;
33219019Sgabor
34219019Sgabor
35219019Sgaborpublic class AdaptDatagramSocket {
36219019Sgabor
37219019Sgabor    static java.io.PrintStream out = System.out;
38219019Sgabor    static Random rand = new Random();
39219019Sgabor
40219019Sgabor    static String toString(DatagramPacket dp) {
41219019Sgabor        return ("DatagramPacket[off=" + dp.getOffset()
42219019Sgabor                + ", len=" + dp.getLength()
43219019Sgabor                + "]");
44219019Sgabor    }
45219019Sgabor
46219019Sgabor    static void test(DatagramSocket ds, InetSocketAddress dst,
47219019Sgabor                     boolean shouldTimeout)
48219019Sgabor        throws Exception
49219019Sgabor    {
50219019Sgabor        DatagramPacket op = new DatagramPacket(new byte[100], 13, 42, dst);
51219019Sgabor        rand.nextBytes(op.getData());
52219019Sgabor        DatagramPacket ip = new DatagramPacket(new byte[100], 19, 100 - 19);
53219019Sgabor        out.println("pre  op: " + toString(op) + "  ip: " + toString(ip));
54219019Sgabor
55219019Sgabor        long start = System.currentTimeMillis();
56219019Sgabor        ds.send(op);
57219019Sgabor
58219019Sgabor        for (;;) {
59219019Sgabor            try {
60219019Sgabor                ds.receive(ip);
61219019Sgabor                if (ip.getLength() == 0) { // ## Not sure why this happens
62219019Sgabor                    ip.setLength(100 - 19);
63219019Sgabor                    continue;
64219019Sgabor                }
65219019Sgabor            } catch (SocketTimeoutException x) {
66219019Sgabor                if (shouldTimeout) {
67219019Sgabor                    out.println("Receive timed out, as expected");
68219019Sgabor                    return;
69219019Sgabor                }
70219019Sgabor                throw x;
71219019Sgabor            }
72219019Sgabor            break;
73219019Sgabor        }
74219019Sgabor
75219019Sgabor        out.println("rtt: " + (System.currentTimeMillis() - start));
76219019Sgabor        out.println("post op: " + toString(op) + "  ip: " + toString(ip));
77219019Sgabor
78219019Sgabor        for (int i = 0; i < ip.getLength(); i++) {
79219019Sgabor            if (ip.getData()[ip.getOffset() + i]
80219019Sgabor                != op.getData()[op.getOffset() + i])
81219019Sgabor                throw new Exception("Incorrect data received");
82219019Sgabor        }
83219019Sgabor
84219019Sgabor        if (!(ip.getSocketAddress().equals(dst))) {
85219019Sgabor            throw new Exception("Incorrect sender address, expected: " + dst
86219019Sgabor                + " actual: " + ip.getSocketAddress());
87219019Sgabor        }
88219019Sgabor    }
89219019Sgabor
90219019Sgabor    static void test(InetSocketAddress dst,
91219019Sgabor                     int timeout, boolean shouldTimeout,
92219019Sgabor                     boolean connect)
93219019Sgabor        throws Exception
94219019Sgabor    {
95219019Sgabor        out.println();
96219019Sgabor        out.println("dst: " + dst);
97219019Sgabor
98219019Sgabor        DatagramSocket ds;
99219019Sgabor        if (false) {
100219019Sgabor            // Original
101219019Sgabor            ds = new DatagramSocket();
102219019Sgabor        } else {
103219019Sgabor            DatagramChannel dc = DatagramChannel.open();
104219019Sgabor            ds = dc.socket();
105219019Sgabor            ds.bind(new InetSocketAddress(0));
106219019Sgabor        }
107219019Sgabor
108219019Sgabor        out.println("socket: " + ds);
109219019Sgabor        if (connect) {
110219019Sgabor            ds.connect(dst);
111219019Sgabor            out.println("connect: " + ds);
112219019Sgabor        }
113219019Sgabor        InetSocketAddress src = new InetSocketAddress(ds.getLocalAddress(),
114219019Sgabor                                                      ds.getLocalPort());
115219019Sgabor        out.println("src: " + src);
116219019Sgabor
117219019Sgabor        if (timeout > 0)
118219019Sgabor            ds.setSoTimeout(timeout);
119219019Sgabor        out.println("timeout: " + ds.getSoTimeout());
120219019Sgabor
121219019Sgabor        for (int i = 0; i < 5; i++) {
122264497Stijl            test(ds, dst, shouldTimeout);
123219019Sgabor        }
124219019Sgabor
125219019Sgabor        // Leave the socket open so that we don't reuse the old src address
126219019Sgabor        //ds.close();
127219019Sgabor
128219019Sgabor    }
129219019Sgabor
130219019Sgabor    public static void main(String[] args) throws Exception {
131219019Sgabor        // need an UDP echo server
132219019Sgabor        try (TestServers.UdpEchoServer echoServer
133219019Sgabor                = TestServers.UdpEchoServer.startNewServer(100)) {
134219019Sgabor            final InetSocketAddress address
135219019Sgabor                = new InetSocketAddress(echoServer.getAddress(),
136219019Sgabor                                        echoServer.getPort());
137219019Sgabor            test(address, 0, false, false);
138219019Sgabor            test(address, 0, false, true);
139260264Sdim            test(address, 15000, false, false);
140219019Sgabor        }
141219019Sgabor        try (TestServers.UdpDiscardServer discardServer
142219019Sgabor                = TestServers.UdpDiscardServer.startNewServer()) {
143219019Sgabor            final InetSocketAddress address
144219019Sgabor                = new InetSocketAddress(discardServer.getAddress(),
145219019Sgabor                                        discardServer.getPort());
146219019Sgabor            test(address, 10, true, false);
147219019Sgabor        }
148219019Sgabor    }
149219019Sgabor
150219019Sgabor}
151219019Sgabor