1 /*
   2  * Copyright (c) 2001, 2013, 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 4313882 4981129
  26  * @summary Unit test for datagram-socket-channel adaptors
  27  * @library ..
  28  */
  29 
  30 import java.net.*;
  31 import java.nio.channels.*;
  32 import java.util.*;
  33 
  34 
  35 public class AdaptDatagramSocket {
  36 
  37     static java.io.PrintStream out = System.out;
  38     static Random rand = new Random();
  39 
  40     static String toString(DatagramPacket dp) {
  41         return ("DatagramPacket[off=" + dp.getOffset()
  42                 + ", len=" + dp.getLength()
  43                 + "]");
  44     }
  45 
  46     static void test(DatagramSocket ds, InetSocketAddress dst,
  47                      boolean shouldTimeout)
  48         throws Exception
  49     {
  50         DatagramPacket op = new DatagramPacket(new byte[100], 13, 42, dst);
  51         rand.nextBytes(op.getData());
  52         DatagramPacket ip = new DatagramPacket(new byte[100], 19, 100 - 19);
  53         out.println("pre  op: " + toString(op) + "  ip: " + toString(ip));
  54 
  55         long start = System.currentTimeMillis();
  56         ds.send(op);
  57 
  58         for (;;) {
  59             try {
  60                 ds.receive(ip);
  61                 if (ip.getLength() == 0) { // ## Not sure why this happens
  62                     ip.setLength(100 - 19);
  63                     continue;
  64                 }
  65             } catch (SocketTimeoutException x) {
  66                 if (shouldTimeout) {
  67                     out.println("Receive timed out, as expected");
  68                     return;
  69                 }
  70                 throw x;
  71             }
  72             break;
  73         }
  74 
  75         out.println("rtt: " + (System.currentTimeMillis() - start));
  76         out.println("post op: " + toString(op) + "  ip: " + toString(ip));
  77 
  78         for (int i = 0; i < ip.getLength(); i++) {
  79             if (ip.getData()[ip.getOffset() + i]
  80                 != op.getData()[op.getOffset() + i])
  81                 throw new Exception("Incorrect data received");
  82         }
  83 
  84         if (!(ip.getSocketAddress().equals(dst))) {
  85             throw new Exception("Incorrect sender address, expected: " + dst
  86                 + " actual: " + ip.getSocketAddress());
  87         }
  88     }
  89 
  90     static void test(InetSocketAddress dst,
  91                      int timeout, boolean shouldTimeout,
  92                      boolean connect)
  93         throws Exception
  94     {
  95         out.println();
  96         out.println("dst: " + dst);
  97 
  98         DatagramSocket ds;
  99         if (false) {
 100             // Original
 101             ds = new DatagramSocket();
 102         } else {
 103             DatagramChannel dc = DatagramChannel.open();
 104             ds = dc.socket();
 105             ds.bind(new InetSocketAddress(0));
 106         }
 107 
 108         out.println("socket: " + ds);
 109         if (connect) {
 110             ds.connect(dst);
 111             out.println("connect: " + ds);
 112         }
 113         InetSocketAddress src = new InetSocketAddress(ds.getLocalAddress(),
 114                                                       ds.getLocalPort());
 115         out.println("src: " + src);
 116 
 117         if (timeout > 0)
 118             ds.setSoTimeout(timeout);
 119         out.println("timeout: " + ds.getSoTimeout());
 120 
 121         for (int i = 0; i < 5; i++) {
 122             test(ds, dst, shouldTimeout);
 123         }
 124 
 125         // Leave the socket open so that we don't reuse the old src address
 126         //ds.close();
 127 
 128     }
 129 
 130     public static void main(String[] args) throws Exception {
 131         // need an UDP echo server
 132         try (TestServers.UdpEchoServer echoServer
 133                 = TestServers.UdpEchoServer.startNewServer(100)) {
 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, 15000, 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         }
 148     }
 149 
 150 }