Add IPv6 multicast test to testsuite (#9037)

Motivation:

We currently only cover ipv4 multicast in the testsuite but we should also have tests for ipv6.

Modifications:

- Add test for ipv6
- Ensure we only try to run multicast test for ipv4 / ipv6 if the loopback interface supports it.

Result:

Better test coverage
This commit is contained in:
Norman Maurer 2019-04-12 12:29:08 +02:00 committed by GitHub
parent 6ed203b7ba
commit 778ff2057e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 87 additions and 17 deletions

View File

@ -18,6 +18,7 @@ package io.netty.testsuite.transport.socket;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelOption;
import io.netty.channel.socket.InternetProtocolFamily;
import io.netty.testsuite.transport.AbstractComboTestsuiteTest;
import io.netty.testsuite.transport.TestsuitePermutation;
import io.netty.util.NetUtil;
@ -34,7 +35,7 @@ public abstract class AbstractDatagramTest extends AbstractComboTestsuiteTest<Bo
@Override
protected List<TestsuitePermutation.BootstrapComboFactory<Bootstrap, Bootstrap>> newFactories() {
return SocketTestPermutation.INSTANCE.datagram();
return SocketTestPermutation.INSTANCE.datagram(InternetProtocolFamily.IPv4);
}
@Override
@ -44,11 +45,17 @@ public abstract class AbstractDatagramTest extends AbstractComboTestsuiteTest<Bo
}
protected SocketAddress newSocketAddress() {
// We use LOCALHOST4 as we use InternetProtocolFamily.IPv4 when creating the DatagramChannel and its
// not supported to bind to and IPV6 address in this case.
//
// See also http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/e74259b3eadc/
// src/share/classes/sun/nio/ch/DatagramChannelImpl.java#l684
return new InetSocketAddress(NetUtil.LOCALHOST4, 0);
switch (internetProtocolFamily()) {
case IPv4:
return new InetSocketAddress(NetUtil.LOCALHOST4, 0);
case IPv6:
return new InetSocketAddress(NetUtil.LOCALHOST6, 0);
default:
throw new AssertionError();
}
}
protected InternetProtocolFamily internetProtocolFamily() {
return InternetProtocolFamily.IPv4;
}
}

View File

@ -0,0 +1,26 @@
/*
* Copyright 2019 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.testsuite.transport.socket;
import io.netty.channel.socket.InternetProtocolFamily;
public class DatagramMulticastIPv6Test extends DatagramMulticastTest {
@Override
protected InternetProtocolFamily internetProtocolFamily() {
return InternetProtocolFamily.IPv6;
}
}

View File

@ -23,12 +23,22 @@ import io.netty.channel.ChannelOption;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramChannel;
import io.netty.channel.socket.DatagramPacket;
import io.netty.channel.socket.InternetProtocolFamily;
import io.netty.channel.socket.oio.OioDatagramChannel;
import io.netty.testsuite.transport.TestsuitePermutation;
import io.netty.util.NetUtil;
import io.netty.util.internal.SocketUtils;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Enumeration;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@ -72,7 +82,8 @@ public class DatagramMulticastTest extends AbstractDatagramTest {
}
DatagramChannel cc = (DatagramChannel) cb.bind().sync().channel();
String group = "230.0.0.1";
String group = internetProtocolFamily() == InternetProtocolFamily.IPv4 ?
"230.0.0.1" : "FF01:0:0:0:0:0:0:101";
InetSocketAddress groupAddress = SocketUtils.socketAddress(group, addr.getPort());
cc.joinGroup(groupAddress, NetUtil.LOOPBACK_IF).sync();
@ -123,4 +134,25 @@ public class DatagramMulticastTest extends AbstractDatagramTest {
return success;
}
}
@Before
public void assumeInternetProtocolFamilySupported() {
Assume.assumeTrue(isSupported(internetProtocolFamily() == InternetProtocolFamily.IPv4 ?
Inet4Address.class : Inet6Address.class));
}
private static boolean isSupported(Class<? extends InetAddress> addressType) {
Enumeration<InetAddress> addresses = NetUtil.LOOPBACK_IF.getInetAddresses();
while (addresses.hasMoreElements()) {
if (addressType.isAssignableFrom(addresses.nextElement().getClass())) {
return true;
}
}
return false;
}
@Override
protected List<TestsuitePermutation.BootstrapComboFactory<Bootstrap, Bootstrap>> newFactories() {
return SocketTestPermutation.INSTANCE.datagram(internetProtocolFamily());
}
}

View File

@ -112,7 +112,7 @@ public class SocketTestPermutation {
return list;
}
public List<BootstrapComboFactory<Bootstrap, Bootstrap>> datagram() {
public List<BootstrapComboFactory<Bootstrap, Bootstrap>> datagram(final InternetProtocolFamily family) {
// Make the list of Bootstrap factories.
List<BootstrapFactory<Bootstrap>> bfs = Arrays.asList(
new BootstrapFactory<Bootstrap>() {
@ -121,7 +121,7 @@ public class SocketTestPermutation {
return new Bootstrap().group(nioWorkerGroup).channelFactory(new ChannelFactory<Channel>() {
@Override
public Channel newChannel() {
return new NioDatagramChannel(InternetProtocolFamily.IPv4);
return new NioDatagramChannel(family);
}
@Override

View File

@ -16,6 +16,7 @@
package io.netty.channel.epoll;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.socket.InternetProtocolFamily;
import io.netty.testsuite.transport.TestsuitePermutation;
import io.netty.testsuite.transport.socket.DatagramMulticastTest;
@ -24,7 +25,7 @@ import java.util.List;
public class EpollDatagramMulticastTest extends DatagramMulticastTest {
@Override
protected List<TestsuitePermutation.BootstrapComboFactory<Bootstrap, Bootstrap>> newFactories() {
return EpollSocketTestPermutation.INSTANCE.datagram();
return EpollSocketTestPermutation.INSTANCE.datagram(InternetProtocolFamily.IPv4);
}
public void testMulticast(Bootstrap sb, Bootstrap cb) throws Throwable {

View File

@ -16,6 +16,7 @@
package io.netty.channel.epoll;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.socket.InternetProtocolFamily;
import io.netty.testsuite.transport.TestsuitePermutation;
import io.netty.testsuite.transport.socket.DatagramUnicastTest;
@ -24,7 +25,7 @@ import java.util.List;
public class EpollDatagramUnicastTest extends DatagramUnicastTest {
@Override
protected List<TestsuitePermutation.BootstrapComboFactory<Bootstrap, Bootstrap>> newFactories() {
return EpollSocketTestPermutation.INSTANCE.datagram();
return EpollSocketTestPermutation.INSTANCE.datagram(InternetProtocolFamily.IPv4);
}
public void testSimpleSendWithConnect(Bootstrap sb, Bootstrap cb) throws Throwable {

View File

@ -119,7 +119,8 @@ class EpollSocketTestPermutation extends SocketTestPermutation {
}
@Override
public List<TestsuitePermutation.BootstrapComboFactory<Bootstrap, Bootstrap>> datagram() {
public List<TestsuitePermutation.BootstrapComboFactory<Bootstrap, Bootstrap>> datagram(
final InternetProtocolFamily family) {
// Make the list of Bootstrap factories.
@SuppressWarnings("unchecked")
List<BootstrapFactory<Bootstrap>> bfs = Arrays.asList(
@ -129,7 +130,7 @@ class EpollSocketTestPermutation extends SocketTestPermutation {
return new Bootstrap().group(nioWorkerGroup).channelFactory(new ChannelFactory<Channel>() {
@Override
public Channel newChannel() {
return new NioDatagramChannel(InternetProtocolFamily.IPv4);
return new NioDatagramChannel(family);
}
@Override

View File

@ -16,6 +16,7 @@
package io.netty.channel.kqueue;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.socket.InternetProtocolFamily;
import io.netty.testsuite.transport.TestsuitePermutation;
import io.netty.testsuite.transport.socket.DatagramUnicastTest;
@ -24,6 +25,6 @@ import java.util.List;
public class KQueueDatagramUnicastTest extends DatagramUnicastTest {
@Override
protected List<TestsuitePermutation.BootstrapComboFactory<Bootstrap, Bootstrap>> newFactories() {
return KQueueSocketTestPermutation.INSTANCE.datagram();
return KQueueSocketTestPermutation.INSTANCE.datagram(InternetProtocolFamily.IPv4);
}
}

View File

@ -103,7 +103,8 @@ class KQueueSocketTestPermutation extends SocketTestPermutation {
}
@Override
public List<TestsuitePermutation.BootstrapComboFactory<Bootstrap, Bootstrap>> datagram() {
public List<TestsuitePermutation.BootstrapComboFactory<Bootstrap, Bootstrap>> datagram(
final InternetProtocolFamily family) {
// Make the list of Bootstrap factories.
@SuppressWarnings("unchecked")
List<BootstrapFactory<Bootstrap>> bfs = Arrays.asList(
@ -113,7 +114,7 @@ class KQueueSocketTestPermutation extends SocketTestPermutation {
return new Bootstrap().group(nioWorkerGroup).channelFactory(new ChannelFactory<Channel>() {
@Override
public Channel newChannel() {
return new NioDatagramChannel(InternetProtocolFamily.IPv4);
return new NioDatagramChannel(family);
}
@Override