test/jdk/java/nio/channels/DatagramChannel/ConnectedSend.java
changeset 49284 a51ca91c2cde
parent 47216 71c04702a3d5
equal deleted inserted replaced
49283:a14ede52a278 49284:a51ca91c2cde
     1 /*
     1 /*
     2  * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2003, 2018, 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.
    22  */
    22  */
    23 
    23 
    24 /* @test
    24 /* @test
    25  * @bug 4849277 7183800
    25  * @bug 4849277 7183800
    26  * @summary Test DatagramChannel send while connected
    26  * @summary Test DatagramChannel send while connected
       
    27  * @library ..
       
    28  * @run testng ConnectedSend
    27  * @author Mike McCloskey
    29  * @author Mike McCloskey
    28  */
    30  */
    29 
    31 
    30 import java.io.*;
    32 import java.io.*;
    31 import java.net.*;
    33 import java.net.*;
    32 import java.nio.*;
    34 import java.nio.*;
    33 import java.nio.channels.*;
    35 import java.nio.channels.*;
    34 import java.nio.charset.*;
    36 import java.nio.charset.*;
       
    37 import org.testng.annotations.Test;
       
    38 import static org.testng.Assert.*;
    35 
    39 
    36 public class ConnectedSend {
    40 public class ConnectedSend {
    37 
       
    38     public static void main(String[] args) throws Exception {
       
    39         test1();
       
    40         test2();
       
    41     }
       
    42 
       
    43     // Check if DatagramChannel.send while connected can include
    41     // Check if DatagramChannel.send while connected can include
    44     // address without throwing
    42     // address without throwing
    45     private static void test1() throws Exception {
    43     @Test
    46 
    44     public static void sendToConnectedAddress() throws Exception {
    47         DatagramChannel sndChannel = DatagramChannel.open();
    45         DatagramChannel sndChannel = DatagramChannel.open();
    48         sndChannel.socket().bind(null);
    46         sndChannel.bind(null);
    49         InetAddress address = InetAddress.getLocalHost();
    47         InetAddress address = InetAddress.getLocalHost();
    50         if (address.isLoopbackAddress()) {
    48         if (address.isLoopbackAddress()) {
    51             address = InetAddress.getLoopbackAddress();
    49             address = InetAddress.getLoopbackAddress();
    52         }
    50         }
    53         InetSocketAddress sender = new InetSocketAddress(
    51         InetSocketAddress sender = new InetSocketAddress(
    54             address,
    52             address,
    55             sndChannel.socket().getLocalPort());
    53             sndChannel.socket().getLocalPort());
    56 
    54 
    57         DatagramChannel rcvChannel = DatagramChannel.open();
    55         DatagramChannel rcvChannel = DatagramChannel.open();
    58         rcvChannel.socket().bind(null);
    56         rcvChannel.bind(null);
    59         InetSocketAddress receiver = new InetSocketAddress(
    57         InetSocketAddress receiver = new InetSocketAddress(
    60             address,
    58             address,
    61             rcvChannel.socket().getLocalPort());
    59             rcvChannel.socket().getLocalPort());
    62 
    60 
    63         rcvChannel.connect(sender);
    61         rcvChannel.connect(sender);
    69         int sent = sndChannel.send(bb, receiver);
    67         int sent = sndChannel.send(bb, receiver);
    70         bb.clear();
    68         bb.clear();
    71         rcvChannel.receive(bb);
    69         rcvChannel.receive(bb);
    72         bb.flip();
    70         bb.flip();
    73         CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb);
    71         CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb);
    74         if (!cb.toString().startsWith("h"))
    72         assertTrue(cb.toString().startsWith("h"), "Unexpected message content");
    75             throw new RuntimeException("Test failed");
       
    76 
    73 
    77         rcvChannel.close();
    74         rcvChannel.close();
    78         sndChannel.close();
    75         sndChannel.close();
    79     }
    76     }
    80 
    77 
    81     // Check if the datagramsocket adaptor can send with a packet
    78     // Check if the datagramsocket adaptor can send with a packet
    82     // that has not been initialized with an address; the legacy
    79     // that has not been initialized with an address; the legacy
    83     // datagram socket will send in this case
    80     // datagram socket will send in this case
    84     private static void test2() throws Exception {
    81     @Test
       
    82     public static void sendAddressedPacket() throws Exception {
    85         DatagramChannel sndChannel = DatagramChannel.open();
    83         DatagramChannel sndChannel = DatagramChannel.open();
    86         sndChannel.socket().bind(null);
    84         sndChannel.bind(null);
    87         InetAddress address = InetAddress.getLocalHost();
    85         InetAddress address = InetAddress.getLocalHost();
    88         if (address.isLoopbackAddress()) {
    86         if (address.isLoopbackAddress()) {
    89             address = InetAddress.getLoopbackAddress();
    87             address = InetAddress.getLoopbackAddress();
    90         }
    88         }
    91         InetSocketAddress sender = new InetSocketAddress(
    89         InetSocketAddress sender = new InetSocketAddress(
    92             address,
    90             address,
    93             sndChannel.socket().getLocalPort());
    91             sndChannel.socket().getLocalPort());
    94 
    92 
    95         DatagramChannel rcvChannel = DatagramChannel.open();
    93         DatagramChannel rcvChannel = DatagramChannel.open();
    96         rcvChannel.socket().bind(null);
    94         rcvChannel.bind(null);
    97         InetSocketAddress receiver = new InetSocketAddress(
    95         InetSocketAddress receiver = new InetSocketAddress(
    98             address,
    96             address,
    99             rcvChannel.socket().getLocalPort());
    97             rcvChannel.socket().getLocalPort());
   100 
    98 
   101         rcvChannel.connect(sender);
    99         rcvChannel.connect(sender);
   107 
   105 
   108         ByteBuffer bb = ByteBuffer.allocate(256);
   106         ByteBuffer bb = ByteBuffer.allocate(256);
   109         rcvChannel.receive(bb);
   107         rcvChannel.receive(bb);
   110         bb.flip();
   108         bb.flip();
   111         CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb);
   109         CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb);
   112         if (!cb.toString().startsWith("h"))
   110         assertTrue(cb.toString().startsWith("h"), "Unexpected message content");
   113             throw new RuntimeException("Test failed");
       
   114 
   111 
   115         // Check that the pkt got set with the target address;
   112         // Check that the pkt got set with the target address;
   116         // This is legacy behavior
   113         // This is legacy behavior
   117         if (!pkt.getSocketAddress().equals(receiver))
   114         assertEquals(pkt.getSocketAddress(), receiver,
   118             throw new RuntimeException("Test failed");
   115             "Unexpected address set on packet");
   119 
   116 
   120         rcvChannel.close();
   117         rcvChannel.close();
   121         sndChannel.close();
   118         sndChannel.close();
   122     }
   119     }
   123 }
   120 }