Skip to content
Permalink
Browse files
8243999: DatagramSocket and MulticastSocket constructors don't specif…
…y how a null InetAddress is handled

This fix clarifies the behaviours of constructors from DatagramSocket and MulticastSocket when no address or a null address is supplied.

Reviewed-by: dfuchs
  • Loading branch information
pconcannon committed Jun 9, 2020
1 parent c47f27e commit ac906168c884ef7e5df3129491b213489dd3e3c1
Showing 4 changed files with 92 additions and 23 deletions.
@@ -138,16 +138,15 @@ DatagramSocket delegate() {
/**
* Constructs a datagram socket and binds it to any available port
* on the local host machine. The socket will be bound to the
* {@link InetAddress#isAnyLocalAddress wildcard} address,
* an IP address chosen by the kernel.
* {@link InetAddress#isAnyLocalAddress wildcard} address.
*
* <p>If there is a security manager,
* its {@code checkListen} method is first called
* with 0 as its argument to ensure the operation is allowed.
* This could result in a SecurityException.
*
* @throws SocketException if the socket could not be opened,
* or the socket could not bind to the specified local port.
* or the socket could not be bound.
* @throws SecurityException if a security manager exists and its
* {@code checkListen} method doesn't allow the operation.
*
@@ -173,7 +172,7 @@ protected DatagramSocket(DatagramSocketImpl impl) {
* Creates a datagram socket, bound to the specified local
* socket address.
* <p>
* If, if the address is {@code null}, creates an unbound socket.
* If the address is {@code null} an unbound socket will be created.
*
* <p>If there is a security manager,
* its {@code checkListen} method is first called
@@ -201,8 +200,7 @@ public DatagramSocket(SocketAddress bindaddr) throws SocketException {
/**
* Constructs a datagram socket and binds it to the specified port
* on the local host machine. The socket will be bound to the
* {@link InetAddress#isAnyLocalAddress wildcard} address,
* an IP address chosen by the kernel.
* {@link InetAddress#isAnyLocalAddress wildcard} address.
*
* <p>If there is a security manager,
* its {@code checkListen} method is first called
@@ -231,9 +229,9 @@ public DatagramSocket(int port) throws SocketException {
* 65535 inclusive. A port number of {@code zero} will let the system pick
* up an ephemeral port in a {@code bind} operation.
* <p>
* If the IP address is 0.0.0.0, the socket will be bound to the
* {@link InetAddress#isAnyLocalAddress wildcard} address,
* an IP address chosen by the kernel.
* If the IP address is a {@link InetAddress#isAnyLocalAddress wildcard}
* address, or is {@code null}, the socket will be bound to the wildcard
* address.
*
* <p>If there is a security manager,
* its {@code checkListen} method is first called
@@ -242,7 +240,7 @@ public DatagramSocket(int port) throws SocketException {
* This could result in a SecurityException.
*
* @param port local port to use in the bind operation.
* @param laddr local address to bind
* @param laddr local address to bind (can be {@code null})
*
* @throws SocketException if the socket could not be opened,
* or the socket could not bind to the specified local port.
@@ -147,7 +147,9 @@ final MulticastSocket delegate() {


/**
* Create a multicast socket.
* Constructs a multicast socket and binds it to any available port
* on the local host machine. The socket will be bound to the
* {@link InetAddress#isAnyLocalAddress wildcard} address.
*
* <p>
* If there is a security manager, its {@code checkListen} method is first
@@ -171,7 +173,9 @@ public MulticastSocket() throws IOException {
}

/**
* Create a multicast socket and bind it to a specific port.
* Constructs a multicast socket and binds it to the specified port
* on the local host machine. The socket will be bound to the
* {@link InetAddress#isAnyLocalAddress wildcard} address.
*
* <p>If there is a security manager,
* its {@code checkListen} method is first called
@@ -188,6 +192,9 @@ public MulticastSocket() throws IOException {
* while creating the MulticastSocket
* @throws SecurityException if a security manager exists and its
* {@code checkListen} method doesn't allow the operation.
* @throws IllegalArgumentException if port is <a href="DatagramSocket.html#PortRange">
* out of range.</a>
*
* @see SecurityManager#checkListen
* @see java.net.DatagramSocket#setReuseAddress(boolean)
*/
@@ -196,9 +203,10 @@ public MulticastSocket(int port) throws IOException {
}

/**
* Create a MulticastSocket bound to the specified socket address.
* Creates a multicast socket, bound to the specified local
* socket address.
* <p>
* Or, if the address is {@code null}, create an unbound socket.
* If the address is {@code null} an unbound socket will be created.
*
* <p>If there is a security manager,
* its {@code checkListen} method is first called
@@ -22,28 +22,28 @@
*/

/* @test
* @bug 8243507
* @summary Checks to ensure that DatagramSocket constructors throw expected
* exceptions.
* @bug 8243507 8243999
* @summary Checks to ensure that DatagramSocket constructors behave as expected
* @run testng Constructor
*/

import org.testng.annotations.Test;

import java.io.IOException;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketAddress;

import static org.testng.Assert.assertThrows;
import static org.testng.Assert.assertTrue;

public class Constructor {
private static final InetAddress LOOPBACK =
InetAddress.getLoopbackAddress();
private static final Class<IllegalArgumentException> IAE =
IllegalArgumentException.class;
private static final InetAddress LOOPBACK = InetAddress.getLoopbackAddress();
private static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class;

private class TestSocketAddress extends SocketAddress {
TestSocketAddress() {}
TestSocketAddress() {
}
}

@Test
@@ -55,10 +55,25 @@ public void testBindAddress() {

@Test
public void testInvalidPortRange() {
var invalidPortValues = new int[] {-1, 65536, Integer.MAX_VALUE};
var invalidPortValues = new int[]{-1, 65536, Integer.MAX_VALUE};
for (int i : invalidPortValues) {
assertThrows(IAE, () -> new DatagramSocket(i));
assertThrows(IAE, () -> new DatagramSocket(i, LOOPBACK));
}
}

@Test
public void testDSNullAddress() throws IOException {
try (var ds = new DatagramSocket()) {
assertTrue(ds.getLocalAddress().isAnyLocalAddress());
}

try (var ds1 = new DatagramSocket(null)) {
assertTrue(ds1.getLocalAddress().isAnyLocalAddress());
}

try (var ds2 = new DatagramSocket(0, null)) {
assertTrue(ds2.getLocalAddress().isAnyLocalAddress());
}
}
}
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2020, 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 8243999
* @summary Checks to ensure that Multicast constructors behave as expected
* @run testng Constructor
*/

import org.testng.annotations.Test;

import java.io.IOException;
import java.net.MulticastSocket;

import static org.testng.Assert.assertTrue;

public class Constructor {
@Test
public void testMSNullAddress() throws IOException {
try (var ms = new MulticastSocket()) {
assertTrue(ms.getLocalAddress().isAnyLocalAddress());
}

try (var ms1 = new MulticastSocket(null)) {
assertTrue(ms1.getLocalAddress().isAnyLocalAddress());
}
}
}

0 comments on commit ac90616

Please sign in to comment.