From bd4358b3add80a2d3e9085006c65c6f298b522e7 Mon Sep 17 00:00:00 2001 From: norman Date: Tue, 22 May 2012 09:51:45 +0200 Subject: [PATCH] Make sure the test also work on ipv6 interfaces --- .../DefaultNioDatagramChannelConfigTest.java | 37 ++++++++++++++++--- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/transport/src/test/java/io/netty/channel/socket/nio/DefaultNioDatagramChannelConfigTest.java b/transport/src/test/java/io/netty/channel/socket/nio/DefaultNioDatagramChannelConfigTest.java index 5545cb61f9..41d7f5c13e 100644 --- a/transport/src/test/java/io/netty/channel/socket/nio/DefaultNioDatagramChannelConfigTest.java +++ b/transport/src/test/java/io/netty/channel/socket/nio/DefaultNioDatagramChannelConfigTest.java @@ -18,10 +18,12 @@ package io.netty.channel.socket.nio; import io.netty.util.internal.DetectionUtil; import java.io.IOException; +import java.net.Inet4Address; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.StandardProtocolFamily; import java.nio.channels.DatagramChannel; +import java.util.Enumeration; import junit.framework.Assert; @@ -34,20 +36,45 @@ public class DefaultNioDatagramChannelConfigTest { if (DetectionUtil.javaVersion() < 7) { return; } - DefaultNioDatagramChannelConfig config = new DefaultNioDatagramChannelConfig(DatagramChannel.open(StandardProtocolFamily.INET)); - NetworkInterface inf = NetworkInterface.getNetworkInterfaces().nextElement(); + + StandardProtocolFamily family = null; + NetworkInterface inf = null; + Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); + while (interfaces.hasMoreElements()) { + inf = interfaces.nextElement(); + Enumeration addresses = inf.getInetAddresses(); + while(addresses.hasMoreElements()) { + InetAddress addr = addresses.nextElement(); + if (addr instanceof Inet4Address) { + family = StandardProtocolFamily.INET; + break; + } else { + family = StandardProtocolFamily.INET6; + } + } + } + if (inf == null) { + // No usable interface found so just skip the test + return; + } + + DefaultNioDatagramChannelConfig config = new DefaultNioDatagramChannelConfig(DatagramChannel.open(family)); + config.setNetworkInterface(inf); Assert.assertEquals(inf, config.getNetworkInterface()); - + InetAddress localhost = inf.getInetAddresses().nextElement(); config.setInterface(localhost); Assert.assertEquals(localhost, config.getInterface()); - + config.setTimeToLive(100); Assert.assertEquals(100, config.getTimeToLive()); - + config.setLoopbackModeDisabled(false); Assert.assertEquals(false, config.isLoopbackModeDisabled()); + + + } }