Skip to content

Commit

Permalink
8264975: java/net/DatagramSocket/DatagramSocketMulticasting.java fail…
Browse files Browse the repository at this point in the history
…s infrequently

Reviewed-by: alanb, chegar
  • Loading branch information
dfuch committed Jun 3, 2021
1 parent a52a08d commit 1783437
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
Expand Up @@ -38,14 +38,12 @@
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MulticastSocket;
import java.net.NetworkInterface;
import java.net.ProtocolFamily;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.SocketOption;
import java.net.SocketTimeoutException;
import java.nio.channels.DatagramChannel;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -59,6 +57,7 @@
import static java.net.StandardSocketOptions.IP_MULTICAST_LOOP;
import static java.net.StandardSocketOptions.IP_MULTICAST_TTL;
import static java.net.StandardSocketOptions.SO_REUSEADDR;
import static jdk.test.lib.NetworkConfiguration.isSameInterface;

public class DatagramSocketMulticasting {
static final ProtocolFamily UNSPEC = () -> "UNSPEC";
Expand Down Expand Up @@ -239,7 +238,7 @@ static void testNetworkInterface(DatagramSocket s,

// setOption(IP_MULTICAST_IF)
s.setOption(IP_MULTICAST_IF, ni);
assertTrue(s.getOption(IP_MULTICAST_IF).equals(ni));
assertTrue(isSameInterface(s.getOption(IP_MULTICAST_IF), ni));

// bad values for IP_MULTICAST_IF
assertThrows(IllegalArgumentException.class,
Expand Down
1 change: 1 addition & 0 deletions test/jdk/java/net/DatagramSocket/SendReceiveMaxSize.java
Expand Up @@ -79,6 +79,7 @@ public void setUp() throws IOException {
IPSupport.throwSkippedExceptionIfNonOperational();
HOST_ADDR = InetAddress.getLocalHost();
BUF_LIMIT = (HOST_ADDR instanceof Inet6Address) ? IPV6_SNDBUF : IPV4_SNDBUF;
System.out.printf("Host address: %s, Buffer limit: %d%n", HOST_ADDR, BUF_LIMIT);
}

@DataProvider
Expand Down
35 changes: 34 additions & 1 deletion test/lib/jdk/test/lib/NetworkConfiguration.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2021, 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 @@ -35,6 +35,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -92,6 +93,38 @@ private static boolean isIPv6LinkLocal(InetAddress a) {
return Inet6Address.class.isInstance(a) && a.isLinkLocalAddress();
}

/**
* Returns true if the two interfaces point to the same network interface.
*
* @implNote
* This method first looks at whether the two interfaces are
* {@linkplain NetworkInterface#equals(Object) equals}, and if so it returns
* true. Otherwise, it looks at whether the two interfaces have the same
* {@linkplain NetworkInterface#getName() name} and
* {@linkplain NetworkInterface#getIndex() index}, and if so returns true.
* Otherwise, it returns false.
*
* @apiNote
* This method ignores differences in the addresses to which the network
* interfaces are bound, to cater for possible reconfiguration that might
* have happened between the time at which each interface configuration
* was looked up.
*
* @param ni1 A network interface, may be {@code null}
* @param ni2 An other network interface, may be {@code null}
* @return {@code true} if the two network interfaces have the same name
* and index, {@code false} otherwise.
*/
public static boolean isSameInterface(NetworkInterface ni1, NetworkInterface ni2) {
if (Objects.equals(ni1, ni2)) return true;
// Objects equals has taken care of the case where
// ni1 == ni2 so either they are both non-null or only
// one of them is null - in which case they can't be equal.
if (ni1 == null || ni2 == null) return false;
if (ni1.getIndex() != ni2.getIndex()) return false;
return Objects.equals(ni1.getName(), ni2.getName());
}

public static boolean isTestable(NetworkInterface nif) {
if (Platform.isOSX()) {
if (nif.getName().contains("awdl")) {
Expand Down

1 comment on commit 1783437

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.