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
parent 806dace32d
commit 9ef5c0946a
9 changed files with 86 additions and 19 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,11 +23,20 @@ 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.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.util.Enumeration;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@ -64,7 +73,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();
@ -115,4 +125,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

@ -100,13 +100,13 @@ public class SocketTestPermutation {
return combo(sbfs, cbfs);
}
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 = Collections.singletonList(
() -> new Bootstrap().group(nioWorkerGroup).channelFactory(new ChannelFactory<Channel>() {
@Override
public Channel newChannel(EventLoop eventLoop) {
return new NioDatagramChannel(eventLoop, InternetProtocolFamily.IPv4);
return new NioDatagramChannel(eventLoop, 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

@ -99,14 +99,15 @@ 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(
() -> new Bootstrap().group(nioWorkerGroup).channelFactory(new ChannelFactory<Channel>() {
@Override
public Channel newChannel(EventLoop eventLoop) {
return new NioDatagramChannel(eventLoop, InternetProtocolFamily.IPv4);
return new NioDatagramChannel(eventLoop, 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

@ -51,8 +51,6 @@ class KQueueSocketTestPermutation extends SocketTestPermutation {
new MultithreadEventLoopGroup(WORKERS, new DefaultThreadFactory("testsuite-KQueue-worker", true),
KQueueHandler.newFactory());
private static final InternalLogger logger = InternalLoggerFactory.getInstance(KQueueSocketTestPermutation.class);
@Override
public List<TestsuitePermutation.BootstrapComboFactory<ServerBootstrap, Bootstrap>> socket() {
@ -87,14 +85,15 @@ 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(
() -> new Bootstrap().group(nioWorkerGroup).channelFactory(new ChannelFactory<Channel>() {
@Override
public Channel newChannel(EventLoop eventLoop) {
return new NioDatagramChannel(eventLoop, InternetProtocolFamily.IPv4);
return new NioDatagramChannel(eventLoop, family);
}
@Override