IP range masks exclude the maximum address of the range. (#22018)

Closes #22005
This commit is contained in:
Adrien Grand 2016-12-08 15:58:32 +01:00 committed by GitHub
parent 30bcb06606
commit 182e119699
2 changed files with 50 additions and 2 deletions

View File

@ -55,6 +55,7 @@ public final class IpRangeAggregationBuilder
private static final InternalAggregation.Type TYPE = new InternalAggregation.Type(NAME); private static final InternalAggregation.Type TYPE = new InternalAggregation.Type(NAME);
public static class Range implements ToXContent { public static class Range implements ToXContent {
private final String key; private final String key;
private final String from; private final String from;
private final String to; private final String to;
@ -94,8 +95,18 @@ public final class IpRangeAggregationBuilder
} }
this.key = key; this.key = key;
try { try {
this.from = InetAddresses.toAddrString(InetAddress.getByAddress(lower)); InetAddress fromAddress = InetAddress.getByAddress(lower);
this.to = InetAddresses.toAddrString(InetAddress.getByAddress(upper)); if (fromAddress.equals(InetAddressPoint.MIN_VALUE)) {
this.from = null;
} else {
this.from = InetAddresses.toAddrString(fromAddress);
}
InetAddress inclusiveToAddress = InetAddress.getByAddress(upper);
if (inclusiveToAddress.equals(InetAddressPoint.MAX_VALUE)) {
this.to = null;
} else {
this.to = InetAddresses.toAddrString(InetAddressPoint.nextUp(inclusiveToAddress));
}
} catch (UnknownHostException bogus) { } catch (UnknownHostException bogus) {
throw new AssertionError(bogus); throw new AssertionError(bogus);
} }

View File

@ -87,4 +87,41 @@ public class IpRangeTests extends BaseAggregationTestCase<IpRangeAggregationBuil
return factory; return factory;
} }
public void testMask() {
IpRangeAggregationBuilder b1 = new IpRangeAggregationBuilder("foo");
IpRangeAggregationBuilder b2 = new IpRangeAggregationBuilder("foo");
b1.addMaskRange("bar", "192.168.10.12/16");
b2.addRange("bar", "192.168.0.0", "192.169.0.0");
assertEquals(b1, b2);
b1 = new IpRangeAggregationBuilder("foo");
b2 = new IpRangeAggregationBuilder("foo");
b1.addMaskRange("bar", "192.168.0.0/31");
b2.addRange("bar", "192.168.0.0", "192.168.0.2");
assertEquals(b1, b2);
b1 = new IpRangeAggregationBuilder("foo");
b2 = new IpRangeAggregationBuilder("foo");
b1.addMaskRange("bar", "0.0.0.0/0");
b2.addRange("bar", "0.0.0.0", "::1:0:0:0");
assertEquals(b1, b2);
b1 = new IpRangeAggregationBuilder("foo");
b2 = new IpRangeAggregationBuilder("foo");
b1.addMaskRange("bar", "fe80::821f:2ff:fe4a:c5bd/64");
b2.addRange("bar", "fe80::", "fe80:0:0:1::");
assertEquals(b1, b2);
b1 = new IpRangeAggregationBuilder("foo");
b2 = new IpRangeAggregationBuilder("foo");
b1.addMaskRange("bar", "::/16");
b2.addRange("bar", null, "1::");
assertEquals(b1, b2);
b1 = new IpRangeAggregationBuilder("foo");
b2 = new IpRangeAggregationBuilder("foo");
b1.addMaskRange("bar", "::/0");
b2.addRange("bar", null, null);
assertEquals(b1, b2);
}
} }