test/jdk/java/nio/channels/DatagramChannel/ConnectedSend.java
author phh
Sat, 30 Nov 2019 14:33:05 -0800
changeset 59330 5b96c12f909d
parent 49284 a51ca91c2cde
permissions -rw-r--r--
8234541: C1 emits an empty message when it inlines successfully Summary: Use "inline" as the message when successfull Reviewed-by: thartmann, mdoerr Contributed-by: navy.xliu@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49284
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
16480
fcad3b6ec812 7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents: 5506
diff changeset
    25
 * @bug 4849277 7183800
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Test DatagramChannel send while connected
49284
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents: 47216
diff changeset
    27
 * @library ..
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents: 47216
diff changeset
    28
 * @run testng ConnectedSend
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Mike McCloskey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.nio.charset.*;
49284
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents: 47216
diff changeset
    37
import org.testng.annotations.Test;
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents: 47216
diff changeset
    38
import static org.testng.Assert.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class ConnectedSend {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    // Check if DatagramChannel.send while connected can include
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    // address without throwing
49284
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents: 47216
diff changeset
    43
    @Test
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents: 47216
diff changeset
    44
    public static void sendToConnectedAddress() throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        DatagramChannel sndChannel = DatagramChannel.open();
49284
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents: 47216
diff changeset
    46
        sndChannel.bind(null);
16480
fcad3b6ec812 7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents: 5506
diff changeset
    47
        InetAddress address = InetAddress.getLocalHost();
fcad3b6ec812 7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents: 5506
diff changeset
    48
        if (address.isLoopbackAddress()) {
fcad3b6ec812 7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents: 5506
diff changeset
    49
            address = InetAddress.getLoopbackAddress();
fcad3b6ec812 7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents: 5506
diff changeset
    50
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        InetSocketAddress sender = new InetSocketAddress(
16480
fcad3b6ec812 7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents: 5506
diff changeset
    52
            address,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            sndChannel.socket().getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        DatagramChannel rcvChannel = DatagramChannel.open();
49284
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents: 47216
diff changeset
    56
        rcvChannel.bind(null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        InetSocketAddress receiver = new InetSocketAddress(
16480
fcad3b6ec812 7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents: 5506
diff changeset
    58
            address,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            rcvChannel.socket().getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        rcvChannel.connect(sender);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        sndChannel.connect(receiver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        ByteBuffer bb = ByteBuffer.allocate(256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        bb.put("hello".getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        int sent = sndChannel.send(bb, receiver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        bb.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        rcvChannel.receive(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb);
49284
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents: 47216
diff changeset
    72
        assertTrue(cb.toString().startsWith("h"), "Unexpected message content");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        rcvChannel.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        sndChannel.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    // Check if the datagramsocket adaptor can send with a packet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    // that has not been initialized with an address; the legacy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // datagram socket will send in this case
49284
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents: 47216
diff changeset
    81
    @Test
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents: 47216
diff changeset
    82
    public static void sendAddressedPacket() throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        DatagramChannel sndChannel = DatagramChannel.open();
49284
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents: 47216
diff changeset
    84
        sndChannel.bind(null);
16480
fcad3b6ec812 7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents: 5506
diff changeset
    85
        InetAddress address = InetAddress.getLocalHost();
fcad3b6ec812 7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents: 5506
diff changeset
    86
        if (address.isLoopbackAddress()) {
fcad3b6ec812 7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents: 5506
diff changeset
    87
            address = InetAddress.getLoopbackAddress();
fcad3b6ec812 7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents: 5506
diff changeset
    88
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        InetSocketAddress sender = new InetSocketAddress(
16480
fcad3b6ec812 7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents: 5506
diff changeset
    90
            address,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            sndChannel.socket().getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        DatagramChannel rcvChannel = DatagramChannel.open();
49284
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents: 47216
diff changeset
    94
        rcvChannel.bind(null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        InetSocketAddress receiver = new InetSocketAddress(
16480
fcad3b6ec812 7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents: 5506
diff changeset
    96
            address,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            rcvChannel.socket().getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        rcvChannel.connect(sender);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        sndChannel.connect(receiver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        byte b[] = "hello".getBytes("UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        DatagramPacket pkt = new DatagramPacket(b, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        sndChannel.socket().send(pkt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        ByteBuffer bb = ByteBuffer.allocate(256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        rcvChannel.receive(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb);
49284
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents: 47216
diff changeset
   110
        assertTrue(cb.toString().startsWith("h"), "Unexpected message content");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        // Check that the pkt got set with the target address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        // This is legacy behavior
49284
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents: 47216
diff changeset
   114
        assertEquals(pkt.getSocketAddress(), receiver,
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents: 47216
diff changeset
   115
            "Unexpected address set on packet");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        rcvChannel.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        sndChannel.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
}