changeset 49289:a51ca91c2cde

8198753: (dc) DatagramChannel throws unspecified exceptions Reviewed-by: alanb
author bpb
date Thu, 22 Mar 2018 12:29:52 -0700
parents a14ede52a278
children 4d2e3f5abb48
files src/java.base/share/classes/java/nio/channels/DatagramChannel.java src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java test/jdk/java/nio/channels/DatagramChannel/Connect.java test/jdk/java/nio/channels/DatagramChannel/ConnectExceptions.java test/jdk/java/nio/channels/DatagramChannel/ConnectedSend.java test/jdk/java/nio/channels/DatagramChannel/SendExceptions.java test/jdk/java/nio/channels/DatagramChannel/SendToUnresolved.java
diffstat 7 files changed, 217 insertions(+), 75 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/nio/channels/DatagramChannel.java	Sat Dec 09 03:33:39 2017 +0100
+++ b/src/java.base/share/classes/java/nio/channels/DatagramChannel.java	Thu Mar 22 12:29:52 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -279,6 +279,9 @@
      *
      * @return  This datagram channel
      *
+     * @throws  AlreadyConnectedException
+     *          If this channel is already connected
+     *
      * @throws  ClosedChannelException
      *          If this channel is closed
      *
@@ -292,6 +295,12 @@
      *          closing the channel and setting the current thread's
      *          interrupt status
      *
+     * @throws  UnresolvedAddressException
+     *          If the given remote address is not fully resolved
+     *
+     * @throws  UnsupportedAddressTypeException
+     *          If the type of the given remote address is not supported
+     *
      * @throws  SecurityException
      *          If a security manager has been installed
      *          and it does not permit access to the given remote address
@@ -444,6 +453,10 @@
      *           zero if there was insufficient room for the datagram in the
      *           underlying output buffer
      *
+     * @throws  AlreadyConnectedException
+     *          If this channel is connected to a different address
+     *          from that specified by {@code target}
+     *
      * @throws  ClosedChannelException
      *          If this channel is closed
      *
@@ -457,6 +470,12 @@
      *          closing the channel and setting the current thread's
      *          interrupt status
      *
+     * @throws  UnresolvedAddressException
+     *          If the given remote address is not fully resolved
+     *
+     * @throws  UnsupportedAddressTypeException
+     *          If the type of the given remote address is not supported
+     *
      * @throws  SecurityException
      *          If a security manager has been installed
      *          and it does not permit datagrams to be sent
--- a/src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java	Sat Dec 09 03:33:39 2017 +0100
+++ b/src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java	Thu Mar 22 12:29:52 2018 -0700
@@ -508,8 +508,7 @@
                 if (remote != null) {
                     // connected
                     if (!target.equals(remote)) {
-                        throw new IllegalArgumentException(
-                            "Connected address not equal to target address");
+                        throw new AlreadyConnectedException();
                     }
                     do {
                         n = IOUtil.write(fd, src, -1, nd);
--- a/test/jdk/java/nio/channels/DatagramChannel/Connect.java	Sat Dec 09 03:33:39 2017 +0100
+++ b/test/jdk/java/nio/channels/DatagramChannel/Connect.java	Thu Mar 22 12:29:52 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -102,7 +102,7 @@
                 try {
                     dc.send(bb, bogus);
                     throw new RuntimeException("Allowed bogus send while connected");
-                } catch (IllegalArgumentException iae) {
+                } catch (AlreadyConnectedException ace) {
                     // Correct behavior
                 }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/nio/channels/DatagramChannel/ConnectExceptions.java	Thu Mar 22 12:29:52 2018 -0700
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/* @test
+ * @bug 8198753
+ * @summary Test DatagramChannel connect exceptions
+ * @library ..
+ * @run testng ConnectExceptions
+ */
+
+import java.io.*;
+import java.net.*;
+import java.nio.*;
+import java.nio.channels.*;
+import org.testng.annotations.AfterTest;
+import org.testng.annotations.BeforeTest;
+import org.testng.annotations.Test;
+import static org.testng.Assert.*;
+
+public class ConnectExceptions {
+    static DatagramChannel sndChannel;
+    static DatagramChannel rcvChannel;
+    static InetSocketAddress sender;
+    static InetSocketAddress receiver;
+
+    @BeforeTest
+    public static void setup() throws Exception {
+        sndChannel = DatagramChannel.open();
+        sndChannel.bind(null);
+        InetAddress address = InetAddress.getLocalHost();
+        if (address.isLoopbackAddress()) {
+            address = InetAddress.getLoopbackAddress();
+        }
+        sender = new InetSocketAddress(address,
+            sndChannel.socket().getLocalPort());
+
+        rcvChannel = DatagramChannel.open();
+        rcvChannel.bind(null);
+        receiver = new InetSocketAddress(address,
+            rcvChannel.socket().getLocalPort());
+    }
+
+    @Test(expectedExceptions = UnsupportedAddressTypeException.class)
+    public static void unsupportedAddressTypeException() throws Exception {
+        rcvChannel.connect(sender);
+        sndChannel.connect(new SocketAddress() {});
+    }
+
+    @Test(expectedExceptions = UnresolvedAddressException.class)
+    public static void unresolvedAddressException() throws Exception {
+        String host = TestUtil.UNRESOLVABLE_HOST;
+        InetSocketAddress unresolvable = new InetSocketAddress (host, 37);
+        sndChannel.connect(unresolvable);
+    }
+
+    @Test(expectedExceptions = AlreadyConnectedException.class)
+    public static void alreadyConnectedException() throws Exception {
+        sndChannel.connect(receiver);
+        InetSocketAddress random = new InetSocketAddress(0);
+        sndChannel.connect(random);
+    }
+
+    @AfterTest
+    public static void cleanup() throws Exception {
+        rcvChannel.close();
+        sndChannel.close();
+    }
+}
--- a/test/jdk/java/nio/channels/DatagramChannel/ConnectedSend.java	Sat Dec 09 03:33:39 2017 +0100
+++ b/test/jdk/java/nio/channels/DatagramChannel/ConnectedSend.java	Thu Mar 22 12:29:52 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -24,6 +24,8 @@
 /* @test
  * @bug 4849277 7183800
  * @summary Test DatagramChannel send while connected
+ * @library ..
+ * @run testng ConnectedSend
  * @author Mike McCloskey
  */
 
@@ -32,20 +34,16 @@
 import java.nio.*;
 import java.nio.channels.*;
 import java.nio.charset.*;
+import org.testng.annotations.Test;
+import static org.testng.Assert.*;
 
 public class ConnectedSend {
-
-    public static void main(String[] args) throws Exception {
-        test1();
-        test2();
-    }
-
     // Check if DatagramChannel.send while connected can include
     // address without throwing
-    private static void test1() throws Exception {
-
+    @Test
+    public static void sendToConnectedAddress() throws Exception {
         DatagramChannel sndChannel = DatagramChannel.open();
-        sndChannel.socket().bind(null);
+        sndChannel.bind(null);
         InetAddress address = InetAddress.getLocalHost();
         if (address.isLoopbackAddress()) {
             address = InetAddress.getLoopbackAddress();
@@ -55,7 +53,7 @@
             sndChannel.socket().getLocalPort());
 
         DatagramChannel rcvChannel = DatagramChannel.open();
-        rcvChannel.socket().bind(null);
+        rcvChannel.bind(null);
         InetSocketAddress receiver = new InetSocketAddress(
             address,
             rcvChannel.socket().getLocalPort());
@@ -71,8 +69,7 @@
         rcvChannel.receive(bb);
         bb.flip();
         CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb);
-        if (!cb.toString().startsWith("h"))
-            throw new RuntimeException("Test failed");
+        assertTrue(cb.toString().startsWith("h"), "Unexpected message content");
 
         rcvChannel.close();
         sndChannel.close();
@@ -81,9 +78,10 @@
     // Check if the datagramsocket adaptor can send with a packet
     // that has not been initialized with an address; the legacy
     // datagram socket will send in this case
-    private static void test2() throws Exception {
+    @Test
+    public static void sendAddressedPacket() throws Exception {
         DatagramChannel sndChannel = DatagramChannel.open();
-        sndChannel.socket().bind(null);
+        sndChannel.bind(null);
         InetAddress address = InetAddress.getLocalHost();
         if (address.isLoopbackAddress()) {
             address = InetAddress.getLoopbackAddress();
@@ -93,7 +91,7 @@
             sndChannel.socket().getLocalPort());
 
         DatagramChannel rcvChannel = DatagramChannel.open();
-        rcvChannel.socket().bind(null);
+        rcvChannel.bind(null);
         InetSocketAddress receiver = new InetSocketAddress(
             address,
             rcvChannel.socket().getLocalPort());
@@ -109,13 +107,12 @@
         rcvChannel.receive(bb);
         bb.flip();
         CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb);
-        if (!cb.toString().startsWith("h"))
-            throw new RuntimeException("Test failed");
+        assertTrue(cb.toString().startsWith("h"), "Unexpected message content");
 
         // Check that the pkt got set with the target address;
         // This is legacy behavior
-        if (!pkt.getSocketAddress().equals(receiver))
-            throw new RuntimeException("Test failed");
+        assertEquals(pkt.getSocketAddress(), receiver,
+            "Unexpected address set on packet");
 
         rcvChannel.close();
         sndChannel.close();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/nio/channels/DatagramChannel/SendExceptions.java	Thu Mar 22 12:29:52 2018 -0700
@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/* @test
+ * @bug 4675045 8198753
+ * @summary Test DatagramChannel send exceptions
+ * @library ..
+ * @run testng SendExceptions
+ */
+
+import java.io.*;
+import java.net.*;
+import java.nio.*;
+import java.nio.channels.*;
+import org.testng.annotations.AfterTest;
+import org.testng.annotations.BeforeTest;
+import org.testng.annotations.Test;
+import static org.testng.Assert.*;
+
+public class SendExceptions {
+    static DatagramChannel sndChannel;
+    static DatagramChannel rcvChannel;
+    static InetSocketAddress sender;
+    static InetSocketAddress receiver;
+    static ByteBuffer buf = ByteBuffer.allocate(17);
+
+    @BeforeTest
+    public static void setup() throws Exception {
+        sndChannel = DatagramChannel.open();
+        sndChannel.bind(null);
+        InetAddress address = InetAddress.getLocalHost();
+        if (address.isLoopbackAddress()) {
+            address = InetAddress.getLoopbackAddress();
+        }
+        sender = new InetSocketAddress(address,
+            sndChannel.socket().getLocalPort());
+
+        rcvChannel = DatagramChannel.open();
+        rcvChannel.bind(null);
+        receiver = new InetSocketAddress(address,
+            rcvChannel.socket().getLocalPort());
+    }
+
+    @Test(expectedExceptions = UnsupportedAddressTypeException.class)
+    public static void unsupportedAddressTypeException() throws Exception {
+        rcvChannel.connect(sender);
+        sndChannel.send(buf, new SocketAddress() {});
+    }
+
+    @Test(expectedExceptions = UnresolvedAddressException.class)
+    public static void unresolvedAddressException() throws Exception {
+        String host = TestUtil.UNRESOLVABLE_HOST;
+        InetSocketAddress unresolvable = new InetSocketAddress (host, 37);
+        sndChannel.send(buf, unresolvable);
+    }
+
+    @Test(expectedExceptions = AlreadyConnectedException.class)
+    public static void alreadyConnectedException() throws Exception {
+        sndChannel.connect(receiver);
+        InetSocketAddress random = new InetSocketAddress(0);
+        sndChannel.send(buf, random);
+    }
+
+    @AfterTest
+    public static void cleanup() throws Exception {
+        rcvChannel.close();
+        sndChannel.close();
+    }
+}
--- a/test/jdk/java/nio/channels/DatagramChannel/SendToUnresolved.java	Sat Dec 09 03:33:39 2017 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,50 +0,0 @@
-/*
- * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-/* @test
- * @bug 4675045
- * @summary Test DatagramChannel send to unresolved address
- * @library ..
- */
-
-import java.io.*;
-import java.net.*;
-import java.nio.*;
-import java.nio.channels.*;
-
-public class SendToUnresolved {
-    public static void main(String [] argv) throws Exception {
-        String host = TestUtil.UNRESOLVABLE_HOST;
-        DatagramChannel dc = DatagramChannel.open();
-        ByteBuffer bb = ByteBuffer.allocate(4);
-        InetSocketAddress sa = new InetSocketAddress (host, 37);
-        InetAddress inetaddr = sa.getAddress();
-        try {
-            dc.send(bb, sa);
-            throw new RuntimeException("Expected exception not thrown");
-        } catch (IOException | UnresolvedAddressException e) {
-            // Correct result
-        }
-        dc.close();
-    }
-}