JDK-8231261 : (dc) Write a test to check local and remote addresses after calling DatagramChannel::connect and DatagramChannel::disconnect
  • Type: Task
  • Component: core-libs
  • Sub-Component: java.nio
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2019-09-19
  • Updated: 2019-09-20
  • Resolved: 2019-09-20
Related Reports
Relates :  
Relates :  
Relates :  
Comments
See test above in comment.
20-09-2019

The test above shows the issues logged in JDK-8231258, JDK-8231259, JDK-8231260.
19-09-2019

/* * Copyright (c) 2019, 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 * @library /test/lib * @summary Test DatagramChannel local address after disconnect. * @run main DisconnectAddresses * @run main/othervm -Djava.net.preferIPv6Addresses=true DisconnectAddresses * @run main/othervm -Djava.net.preferIPv4Stack=true DisconnectAddresses */ import jdk.test.lib.net.IPSupport; import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.SocketAddress; import java.net.StandardProtocolFamily; import java.nio.ByteBuffer; import java.nio.channels.DatagramChannel; import java.nio.channels.NotYetConnectedException; import java.util.Objects; public class DisconnectAddresses { public static void main(String[] args) throws IOException { IPSupport.throwSkippedExceptionIfNonOperational(); boolean preferIPv6 = Boolean.getBoolean("java.net.preferIPv6Addresses"); // test with default protocol family try (DatagramChannel dc = DatagramChannel.open()) { System.out.println("Test with default"); dc.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); test(dc); test(dc); } if (IPSupport.hasIPv6()) { // test with IPv6 only System.out.println("Test with IPv6 only"); try (DatagramChannel dc = DatagramChannel.open(StandardProtocolFamily.INET6)) { dc.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); test(dc); test(dc); } } if (IPSupport.hasIPv4() && !preferIPv6) { // test with IPv4 only System.out.println("Test with IPv4 only"); try (DatagramChannel dc = DatagramChannel.open(StandardProtocolFamily.INET)) { dc.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); test(dc); test(dc); } } } /** * Connect DatagramChannel to a server, write a datagram and disconnect. Invoke * a second or subsequent time with the same DatagramChannel instance to check * that disconnect works as expected. */ static void test(DatagramChannel dc) throws IOException { SocketAddress local = dc.getLocalAddress(); try (DatagramChannel server = DatagramChannel.open()) { server.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); SocketAddress remote = server.getLocalAddress(); dc.connect(remote); assertTrue(dc.isConnected()); // comment the following two lines on OS X to see JDK-8231259 assertEquals("local address after connect", dc.getLocalAddress(), local); assertEquals("remote address after connect", dc.getRemoteAddress(), remote); dc.disconnect(); assertFalse(dc.isConnected()); assertEquals("local address after disconnect", dc.getLocalAddress(), local); assertEquals("remote address after disconnect", dc.getRemoteAddress(), null); } } static void assertTrue(boolean val) { assertEquals(val, true); } static void assertFalse(boolean val) { assertEquals(val, false); } static void assertEquals(Object actual, Object expected) { assertEquals("", actual, expected); } static void assertEquals(String name, Object actual, Object expected) { if (!Objects.equals(actual, expected)) { String message = name + " should be " + expected + " found " + actual; throw new AssertionError(message); } } }
19-09-2019