Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #4483 - Cleanup of InetAddressSetTest #4484

Merged
merged 3 commits into from Jan 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
272 changes: 146 additions & 126 deletions jetty-util/src/test/java/org/eclipse/jetty/util/InetAddressSetTest.java
Expand Up @@ -19,27 +19,53 @@
package org.eclipse.jetty.util;

import java.net.InetAddress;
import java.util.Iterator;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import org.eclipse.jetty.toolchain.test.Net;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

public class InetAddressSetTest
{
@Test
public void testInetAddress() throws Exception
public static Stream<String> loopbacks()
{
assertTrue(InetAddress.getByName("127.0.0.1").isLoopbackAddress());
assertTrue(InetAddress.getByName("::1").isLoopbackAddress());
assertTrue(InetAddress.getByName("::0.0.0.1").isLoopbackAddress());
assertTrue(InetAddress.getByName("[::1]").isLoopbackAddress());
assertTrue(InetAddress.getByName("[::0.0.0.1]").isLoopbackAddress());
assertTrue(InetAddress.getByName("[::ffff:127.0.0.1]").isLoopbackAddress());
List<String> loopbacks = new ArrayList<>();

loopbacks.add("127.0.0.1");
loopbacks.add("127.0.0.2");

if (Net.isIpv6InterfaceAvailable())
{
loopbacks.add("::1");
loopbacks.add("::0.0.0.1");
loopbacks.add("[::1]");
loopbacks.add("[::0.0.0.1]");
loopbacks.add("[::ffff:127.0.0.1]");
}

return loopbacks.stream();
}

@ParameterizedTest
@MethodSource("loopbacks")
public void testInetAddressLoopback(String addr) throws Exception
{
InetAddress inetAddress = InetAddress.getByName(addr);
assertNotNull(inetAddress);
assertTrue(inetAddress.isLoopbackAddress());
}

@Test
Expand Down Expand Up @@ -71,32 +97,37 @@ public void testSingleton() throws Exception
assertFalse(set.test(InetAddress.getByName("1::abcd")));
}

@Test
public void testBadSingleton() throws Exception
public static Stream<String> badsingletons()
{
String[] tests = new String[]
{
"unknown",
"1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16",
"a.b.c.d",
"[::1",
"[xxx]",
"[:::1]",
};
List<String> bad = new ArrayList<>();

InetAddressSet set = new InetAddressSet();
bad.add("intentionally invalid hostname");
bad.add("nonexistentdomain.tld");
bad.add("1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16");
bad.add("a.b.c.d");

bad.add("[::1"); // incomplete
bad.add("[xxx]"); // not valid octets
bad.add("[:::1]"); // too many colons

for (String t : tests)
return bad.stream();
}

@ParameterizedTest
@MethodSource("badsingletons")
public void testBadSingleton(final String badAddr)
{
try
{
try
{
set.add(t);
fail(t);
}
catch (IllegalArgumentException e)
{
assertThat(e.getMessage(), containsString(t));
}
InetAddress inetAddress = InetAddress.getByName(badAddr);
Assumptions.assumeTrue(inetAddress == null);
}
catch (UnknownHostException expected)
{
//noinspection MismatchedQueryAndUpdateOfCollection
InetAddressSet inetAddressSet = new InetAddressSet();
IllegalArgumentException cause = assertThrows(IllegalArgumentException.class, () -> inetAddressSet.add(badAddr));
assertThat(cause.getMessage(), containsString(badAddr));
}
}

Expand Down Expand Up @@ -147,34 +178,34 @@ public void testCIDR() throws Exception
assertTrue(set.test(InetAddress.getByName("5.22.192.1")));
}

@Test
public void testBadCIDR() throws Exception
public static Stream<String> badCidrs()
{
String[] tests = new String[]
{
"unknown/8",
"1.2.3.4/-1",
"1.2.3.4/xxx",
"1.2.3.4/33",
"255.255.8.0/16",
"255.255.8.1/17",
"[::1]/129",
};

InetAddressSet set = new InetAddressSet();

for (String t : tests)
List<String> bad = new ArrayList<>();
bad.add("intentionally invalid hostname/8");
bad.add("nonexistentdomain.tld/8");
bad.add("1.2.3.4/-1");
bad.add("1.2.3.4/xxx");
bad.add("1.2.3.4/33");
bad.add("255.255.8.0/16");
bad.add("255.255.8.1/17");

if (Net.isIpv6InterfaceAvailable())
{
try
{
set.add(t);
fail(t);
}
catch (IllegalArgumentException e)
{
assertThat(e.getMessage(), containsString(t));
}
bad.add("[::1]/129");
}

return bad.stream();
}

@ParameterizedTest
@MethodSource("badCidrs")
public void testBadCIDR(String cidr)
{
//noinspection MismatchedQueryAndUpdateOfCollection
InetAddressSet inetAddressSet = new InetAddressSet();

IllegalArgumentException cause = assertThrows(IllegalArgumentException.class, () -> inetAddressSet.add(cidr));
assertThat(cause.getMessage(), containsString(cidr));
}

@Test
Expand All @@ -184,7 +215,11 @@ public void testMinMax() throws Exception

set.add("10.0.0.4-10.0.0.6");
set.add("10.1.0.254-10.1.1.1");
set.add("[abcd:ef::fffe]-[abcd:ef::1:1]");

if (Net.isIpv6InterfaceAvailable())
{
set.add("[abcd:ef::fffe]-[abcd:ef::1:1]");
}

assertFalse(set.test(InetAddress.getByName("10.0.0.3")));
assertTrue(set.test(InetAddress.getByName("10.0.0.4")));
Expand All @@ -199,37 +234,28 @@ public void testMinMax() throws Exception
assertTrue(set.test(InetAddress.getByName("10.1.1.1")));
assertFalse(set.test(InetAddress.getByName("10.1.1.2")));

assertFalse(set.test(InetAddress.getByName("ABCD:EF::FFFD")));
assertTrue(set.test(InetAddress.getByName("ABCD:EF::FFFE")));
assertTrue(set.test(InetAddress.getByName("ABCD:EF::FFFF")));
assertTrue(set.test(InetAddress.getByName("ABCD:EF::1:0")));
assertTrue(set.test(InetAddress.getByName("ABCD:EF::1:1")));
assertFalse(set.test(InetAddress.getByName("ABCD:EF::1:2")));
if (Net.isIpv6InterfaceAvailable())
{
assertFalse(set.test(InetAddress.getByName("ABCD:EF::FFFD")));
assertTrue(set.test(InetAddress.getByName("ABCD:EF::FFFE")));
assertTrue(set.test(InetAddress.getByName("ABCD:EF::FFFF")));
assertTrue(set.test(InetAddress.getByName("ABCD:EF::1:0")));
assertTrue(set.test(InetAddress.getByName("ABCD:EF::1:1")));
assertFalse(set.test(InetAddress.getByName("ABCD:EF::1:2")));
}
}

@Test
public void testBadMinMax() throws Exception
@ParameterizedTest
@ValueSource(strings = {
"10.0.0.0-9.0.0.0",
"9.0.0.0-[::10.0.0.0]"
})
public void testBadMinMax(String bad)
{
String[] tests = new String[]
{
"10.0.0.0-9.0.0.0",
"9.0.0.0-[::10.0.0.0]",
};

InetAddressSet set = new InetAddressSet();

for (String t : tests)
{
try
{
set.add(t);
fail(t);
}
catch (IllegalArgumentException e)
{
assertThat(e.getMessage(), containsString(t));
}
}
//noinspection MismatchedQueryAndUpdateOfCollection
InetAddressSet inetAddressSet = new InetAddressSet();
IllegalArgumentException cause = assertThrows(IllegalArgumentException.class, () -> inetAddressSet.add(bad));
assertThat(cause.getMessage(), containsString(bad));
}

@Test
Expand Down Expand Up @@ -258,30 +284,18 @@ public void testLegacy() throws Exception
assertFalse(set.test(InetAddress.getByName("10.0.244.3")));
}

@Test
public void testBadLegacy() throws Exception
@ParameterizedTest
@ValueSource(strings = {
"9.0-10.0",
"10.0.0--1.1",
"10.0.0-256.1"
})
public void testBadLegacy(String bad)
{
String[] tests = new String[]
{
"9.0-10.0",
"10.0.0--1.1",
"10.0.0-256.1",
};

InetAddressSet set = new InetAddressSet();

for (String t : tests)
{
try
{
set.add(t);
fail(t);
}
catch (IllegalArgumentException e)
{
assertThat(e.getMessage(), containsString(t));
}
}
//noinspection MismatchedQueryAndUpdateOfCollection
InetAddressSet inetAddressSet = new InetAddressSet();
IllegalArgumentException cause = assertThrows(IllegalArgumentException.class, () -> inetAddressSet.add(bad));
assertThat(cause.getMessage(), containsString(bad));
}

@Test
Expand All @@ -299,10 +313,13 @@ public void testRemove() throws Exception
assertTrue(set.test(InetAddress.getByName("1.2.3.4")));
assertTrue(set.test(InetAddress.getByAddress(new byte[]{(byte)1, (byte)2, (byte)3, (byte)4})));
assertTrue(set.test(InetAddress.getByAddress("hostname", new byte[]{(byte)1, (byte)2, (byte)3, (byte)4})));
assertTrue(set.test(InetAddress.getByName("::0:0:abcd")));
assertTrue(set.test(InetAddress.getByName("::abcd")));
assertTrue(set.test(InetAddress.getByName("[::abcd]")));
assertTrue(set.test(InetAddress.getByName("::ffff:1.2.3.4")));
if (Net.isIpv6InterfaceAvailable())
{
assertTrue(set.test(InetAddress.getByName("::0:0:abcd")));
assertTrue(set.test(InetAddress.getByName("::abcd")));
assertTrue(set.test(InetAddress.getByName("[::abcd]")));
assertTrue(set.test(InetAddress.getByName("::ffff:1.2.3.4")));
}
assertTrue(set.test(InetAddress.getByName("10.0.0.4")));
assertTrue(set.test(InetAddress.getByName("10.0.0.5")));
assertTrue(set.test(InetAddress.getByName("10.0.0.6")));
Expand All @@ -313,28 +330,31 @@ public void testRemove() throws Exception
assertFalse(set.test(InetAddress.getByName("1.2.3.4")));
assertFalse(set.test(InetAddress.getByAddress(new byte[]{(byte)1, (byte)2, (byte)3, (byte)4})));
assertFalse(set.test(InetAddress.getByAddress("hostname", new byte[]{(byte)1, (byte)2, (byte)3, (byte)4})));
assertTrue(set.test(InetAddress.getByName("::0:0:abcd")));
assertTrue(set.test(InetAddress.getByName("::abcd")));
assertTrue(set.test(InetAddress.getByName("[::abcd]")));
assertFalse(set.test(InetAddress.getByName("::ffff:1.2.3.4")));
if (Net.isIpv6InterfaceAvailable())
{
assertTrue(set.test(InetAddress.getByName("::0:0:abcd")));
assertTrue(set.test(InetAddress.getByName("::abcd")));
assertTrue(set.test(InetAddress.getByName("[::abcd]")));
assertFalse(set.test(InetAddress.getByName("::ffff:1.2.3.4")));
}
assertTrue(set.test(InetAddress.getByName("10.0.0.4")));
assertTrue(set.test(InetAddress.getByName("10.0.0.5")));
assertTrue(set.test(InetAddress.getByName("10.0.0.6")));

for (Iterator<String> i = set.iterator(); i.hasNext(); )
{
if ("::abcd".equals(i.next()))
i.remove();
}
set.removeIf("::abcd"::equals);

assertTrue(set.test(InetAddress.getByName("webtide.com")));
assertTrue(set.test(InetAddress.getByName(InetAddress.getByName("webtide.com").getHostAddress())));
assertFalse(set.test(InetAddress.getByName("1.2.3.4")));
assertFalse(set.test(InetAddress.getByAddress(new byte[]{(byte)1, (byte)2, (byte)3, (byte)4})));
assertFalse(set.test(InetAddress.getByAddress("hostname", new byte[]{(byte)1, (byte)2, (byte)3, (byte)4})));
assertFalse(set.test(InetAddress.getByName("::0:0:abcd")));
assertFalse(set.test(InetAddress.getByName("::abcd")));
assertFalse(set.test(InetAddress.getByName("[::abcd]")));
assertFalse(set.test(InetAddress.getByName("::ffff:1.2.3.4")));
if (Net.isIpv6InterfaceAvailable())
{
assertFalse(set.test(InetAddress.getByName("::0:0:abcd")));
assertFalse(set.test(InetAddress.getByName("::abcd")));
assertFalse(set.test(InetAddress.getByName("[::abcd]")));
assertFalse(set.test(InetAddress.getByName("::ffff:1.2.3.4")));
}
assertTrue(set.test(InetAddress.getByName("10.0.0.4")));
assertTrue(set.test(InetAddress.getByName("10.0.0.5")));
assertTrue(set.test(InetAddress.getByName("10.0.0.6")));
Expand Down