Skip to content

Commit

Permalink
8301701: java/net/DatagramSocket/DatagramSocketMulticasting.java shou…
Browse files Browse the repository at this point in the history
…ld be hardened

Reviewed-by: dfuchs
  • Loading branch information
DarraghClarke authored and jaikiran committed Feb 28, 2023
1 parent 1e3c9fd commit 5feb13b
Showing 1 changed file with 47 additions and 45 deletions.
92 changes: 47 additions & 45 deletions test/jdk/java/net/DatagramSocket/DatagramSocketMulticasting.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2023, 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
Expand Down Expand Up @@ -27,8 +27,8 @@
* @library /test/lib
* @build jdk.test.lib.NetworkConfiguration
* jdk.test.lib.net.IPSupport
* @run main/othervm DatagramSocketMulticasting
* @run main/othervm -Djava.net.preferIPv4Stack=true DatagramSocketMulticasting
* @run junit/othervm DatagramSocketMulticasting
* @run junit/othervm -Djava.net.preferIPv4Stack=true DatagramSocketMulticasting
*/

import java.io.IOException;
Expand All @@ -48,6 +48,7 @@

import jdk.test.lib.NetworkConfiguration;
import jdk.test.lib.net.IPSupport;
import org.junit.jupiter.api.Test;

import static java.net.StandardProtocolFamily.INET;
import static java.net.StandardProtocolFamily.INET6;
Expand All @@ -57,11 +58,16 @@
import static java.net.StandardSocketOptions.SO_REUSEADDR;
import static jdk.test.lib.NetworkConfiguration.isSameInterface;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

public class DatagramSocketMulticasting {
static final ProtocolFamily UNSPEC = () -> "UNSPEC";
static final int MAX_TRIES = 3;

public static void main(String[] args) throws IOException {
IPSupport.throwSkippedExceptionIfNonOperational();
@Test
public void main() throws IOException {
assumeTrue(IPSupport.currentConfigurationIsValid(), "Invalid networking configuration");

// IPv4 and IPv6 interfaces that support multicasting
NetworkConfiguration config = NetworkConfiguration.probe();
Expand Down Expand Up @@ -132,13 +138,13 @@ static void testJoinGroup2(ProtocolFamily family,
System.out.format("testJoinGroup2: local socket address: %s%n", s.getLocalSocketAddress());

// check network interface not set
assertTrue(s.getOption(IP_MULTICAST_IF) == null);
assertNull(s.getOption(IP_MULTICAST_IF));

// join on default interface
s.joinGroup(new InetSocketAddress(group, 0), null);

// join should not change the outgoing multicast interface
assertTrue(s.getOption(IP_MULTICAST_IF) == null);
assertNull(s.getOption(IP_MULTICAST_IF));

// already a member (exception not specified)
assertThrows(SocketException.class,
Expand All @@ -155,7 +161,7 @@ static void testJoinGroup2(ProtocolFamily family,
s.joinGroup(new InetSocketAddress(group, 0), ni);

// join should not change the outgoing multicast interface
assertTrue(s.getOption(IP_MULTICAST_IF) == null);
assertNull(s.getOption(IP_MULTICAST_IF));

// already a member (exception not specified)
assertThrows(SocketException.class,
Expand Down Expand Up @@ -232,7 +238,7 @@ static void testJoinGroup2(ProtocolFamily family,
static void testNetworkInterface(DatagramSocket s,
NetworkInterface ni) throws IOException {
// default value
assertTrue(s.getOption(IP_MULTICAST_IF) == null);
assertNull(s.getOption(IP_MULTICAST_IF));

// setOption(IP_MULTICAST_IF)
s.setOption(IP_MULTICAST_IF, ni);
Expand All @@ -251,12 +257,12 @@ static void testNetworkInterface(DatagramSocket s,
*/
static void testTimeToLive(DatagramSocket s) throws IOException {
// should be 1 by default
assertTrue(s.getOption(IP_MULTICAST_TTL) == 1);
assertEquals(1, s.getOption(IP_MULTICAST_TTL));

// setOption(IP_MULTICAST_TTL)
for (int ttl = 0; ttl <= 2; ttl++) {
s.setOption(IP_MULTICAST_TTL, ttl);
assertTrue(s.getOption(IP_MULTICAST_TTL) == ttl);
assertEquals(ttl, s.getOption(IP_MULTICAST_TTL));
}

// bad values for IP_MULTICAST_TTL
Expand All @@ -273,15 +279,15 @@ static void testTimeToLive(DatagramSocket s) throws IOException {
*/
static void testLoopbackMode(DatagramSocket s) throws IOException {
// should be enabled by default
assertTrue(s.getOption(IP_MULTICAST_LOOP) == true);
assertTrue(s.getOption(IP_MULTICAST_LOOP));

// setLoopbackMode

// setOption(IP_MULTICAST_LOOP)
s.setOption(IP_MULTICAST_LOOP, false); // disable
assertTrue(s.getOption(IP_MULTICAST_LOOP) == false);
assertFalse(s.getOption(IP_MULTICAST_LOOP));
s.setOption(IP_MULTICAST_LOOP, true); // enable
assertTrue(s.getOption(IP_MULTICAST_LOOP) == true);
assertTrue(s.getOption(IP_MULTICAST_LOOP));

// bad values for IP_MULTICAST_LOOP
assertThrows(IllegalArgumentException.class,
Expand All @@ -298,23 +304,37 @@ static void testSendReceive(DatagramSocket s, InetAddress group) throws IOExcept
System.out.println("testSendReceive");

// outgoing multicast interface needs to be set
assertTrue(s.getOption(IP_MULTICAST_IF) != null);
assertNotNull(s.getOption(IP_MULTICAST_IF));

SocketAddress target = new InetSocketAddress(group, s.getLocalPort());
byte[] message = "hello".getBytes("UTF-8");
String message = "testSendReceive";
byte[] messageBytes = message.getBytes("UTF-8");

// send message to multicast group
DatagramPacket p = new DatagramPacket(message, message.length);
DatagramPacket p = new DatagramPacket(messageBytes, messageBytes.length);
p.setSocketAddress(target);
s.send(p);

// receive message
// receive message with retry in case of stray messages
s.setSoTimeout(0);
p = new DatagramPacket(new byte[1024], 100);
s.receive(p);
for (int i = 1; i <= MAX_TRIES; i++) {
p = new DatagramPacket(new byte[1024], 100);
s.receive(p);
String messageReceived = new String(p.getData(), 0, p.getLength(), "UTF-8");

System.out.format(
"TestSendReceive iteration [%s], Received DatagramPacket [%s] from [%s]%n",
i, messageReceived, s.getLocalSocketAddress());

if (s.getLocalPort() == p.getPort()) {
assertEquals(message, messageReceived,
String.format("expected message %s, instead received %s%n",
message, messageReceived));
break;
}

assertTrue(p.getLength() == message.length);
assertTrue(p.getPort() == s.getLocalPort());
assertNotEquals(MAX_TRIES, i, "testSendReceive: too many retries");
}
}

/**
Expand All @@ -326,11 +346,11 @@ static void testSendNoReceive(DatagramSocket s, InetAddress group) throws IOExce
System.out.println("testSendNoReceive");

// outgoing multicast interface needs to be set
assertTrue(s.getOption(IP_MULTICAST_IF) != null);
assertNotNull(s.getOption(IP_MULTICAST_IF));

SocketAddress target = new InetSocketAddress(group, s.getLocalPort());
long nano = System.nanoTime();
String text = nano + ": hello";
String text = nano + ": testSendNoReceive";
byte[] message = text.getBytes("UTF-8");

// send datagram to multicast group
Expand All @@ -347,31 +367,13 @@ static void testSendNoReceive(DatagramSocket s, InetAddress group) throws IOExce
if (Arrays.equals(p.getData(), p.getOffset(), p.getLength(), message, 0, message.length)) {
throw new RuntimeException("message shouldn't have been received");
} else {
System.out.format("Received unexpected message from %s%n", p.getSocketAddress());
String messageReceived = new String(p.getData(), 0, p.getLength(), "UTF-8");
System.out.format("Received unexpected message %s from %s%n",
messageReceived, p.getSocketAddress());
}
} catch (SocketTimeoutException expected) {
break;
}
}
}


static void assertTrue(boolean e) {
if (!e) throw new RuntimeException();
}

interface ThrowableRunnable {
void run() throws Exception;
}

static void assertThrows(Class<?> exceptionClass, ThrowableRunnable task) {
try {
task.run();
throw new RuntimeException("Exception not thrown");
} catch (Exception e) {
if (!exceptionClass.isInstance(e)) {
throw new RuntimeException("expected: " + exceptionClass + ", actual: " + e);
}
}
}
}

3 comments on commit 5feb13b

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GoeLin
Copy link
Member

@GoeLin GoeLin commented on 5feb13b Jun 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk17u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 5feb13b Jun 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GoeLin Could not automatically backport 5feb13b5 to openjdk/jdk17u-dev due to conflicts in the following files:

  • test/jdk/java/net/DatagramSocket/DatagramSocketMulticasting.java

Please fetch the appropriate branch/commit and manually resolve these conflicts by using the following commands in your personal fork of openjdk/jdk17u-dev. Note: these commands are just some suggestions and you can use other equivalent commands you know.

# Fetch the up-to-date version of the target branch
$ git fetch --no-tags https://git.openjdk.org/jdk17u-dev.git master:master

# Check out the target branch and create your own branch to backport
$ git checkout master
$ git checkout -b GoeLin-backport-5feb13b5

# Fetch the commit you want to backport
$ git fetch --no-tags https://git.openjdk.org/jdk.git 5feb13b55d32fad8f533f52ee7bd63e2cf2d247c

# Backport the commit
$ git cherry-pick --no-commit 5feb13b55d32fad8f533f52ee7bd63e2cf2d247c
# Resolve conflicts now

# Commit the files you have modified
$ git add files/with/resolved/conflicts
$ git commit -m 'Backport 5feb13b55d32fad8f533f52ee7bd63e2cf2d247c'

Once you have resolved the conflicts as explained above continue with creating a pull request towards the openjdk/jdk17u-dev with the title Backport 5feb13b55d32fad8f533f52ee7bd63e2cf2d247c.

Please sign in to comment.