Revert change to set longTo to "MAX_IP -1" and improve test suite to check for range

This commit is contained in:
Ruflin 2015-07-27 09:11:04 +02:00
parent 3be98927a0
commit b8c2f05ff5
2 changed files with 40 additions and 35 deletions

View File

@ -151,8 +151,9 @@ public class IPv4RangeBuilder extends AbstractRangeBuilder<IPv4RangeBuilder> {
int to = from + (~mask); int to = from + (~mask);
long longTo = intIpToLongIp(to) + 1; // we have to +1 here as the range is non-inclusive on the "to" side long longTo = intIpToLongIp(to) + 1; // we have to +1 here as the range is non-inclusive on the "to" side
if (longTo == InternalIPv4Range.MAX_IP) { if (longTo == InternalIPv4Range.MAX_IP) {
longTo = InternalIPv4Range.MAX_IP - 1; longTo = -1;
} }
return new long[] { longFrom, longTo }; return new long[] { longFrom, longTo };

View File

@ -872,8 +872,8 @@ public class IPv4RangeTests extends ElasticsearchIntegrationTest {
} }
@Test @Test
public void cidr0Mask0() { public void mask0() {
SearchResponse response = client().prepareSearch("idx_unmapped") SearchResponse response = client().prepareSearch("idx")
.addAggregation(ipRange("range") .addAggregation(ipRange("range")
.field("ip") .field("ip")
.addMaskRange("0.0.0.0/0")) .addMaskRange("0.0.0.0/0"))
@ -881,7 +881,6 @@ public class IPv4RangeTests extends ElasticsearchIntegrationTest {
assertSearchResponse(response); assertSearchResponse(response);
Range range = response.getAggregations().get("range"); Range range = response.getAggregations().get("range");
assertThat(range, notNullValue()); assertThat(range, notNullValue());
assertThat(range.getName(), equalTo("range")); assertThat(range.getName(), equalTo("range"));
@ -890,53 +889,58 @@ public class IPv4RangeTests extends ElasticsearchIntegrationTest {
Range.Bucket bucket = buckets.get(0); Range.Bucket bucket = buckets.get(0);
assertThat((String) bucket.getKey(), equalTo("0.0.0.0/0")); assertThat((String) bucket.getKey(), equalTo("0.0.0.0/0"));
assertThat(bucket.getFromAsString(), nullValue()); assertThat(bucket.getFromAsString(), nullValue());
assertThat(bucket.getToAsString(), equalTo("255.255.255.255")); assertThat(bucket.getToAsString(), nullValue());
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(Double.POSITIVE_INFINITY));
assertEquals(255l, bucket.getDocCount());
} }
@Test @Test
public void cidr0Mask1() { public void mask0SpecialIps() throws Exception {
SearchResponse response = client().prepareSearch("idx_unmapped") assertAcked(prepareCreate("idx_range")
.addMapping("type", "ip", "type=ip", "ips", "type=ip"));
IndexRequestBuilder[] builders = new IndexRequestBuilder[4];
builders[0] = client().prepareIndex("idx_range", "type").setSource(jsonBuilder()
.startObject()
.field("ip", "0.0.0.0")
.endObject());
builders[1] = client().prepareIndex("idx_range", "type").setSource(jsonBuilder()
.startObject()
.field("ip", "0.0.0.255")
.endObject());
builders[2] = client().prepareIndex("idx_range", "type").setSource(jsonBuilder()
.startObject()
.field("ip", "255.255.255.0")
.endObject());
builders[3] = client().prepareIndex("idx_range", "type").setSource(jsonBuilder()
.startObject()
.field("ip", "255.255.255.255")
.endObject());
indexRandom(true, builders);
ensureSearchable();
SearchResponse response = client().prepareSearch("idx_range")
.addAggregation(ipRange("range") .addAggregation(ipRange("range")
.field("ip") .field("ip")
.addMaskRange("0.0.0.0/1")) .addMaskRange("0.0.0.0/0"))
.execute().actionGet(); .execute().actionGet();
assertSearchResponse(response); assertSearchResponse(response);
Range range = response.getAggregations().get("range"); Range range = response.getAggregations().get("range");
assertThat(range, notNullValue()); assertThat(range, notNullValue());
assertThat(range.getName(), equalTo("range")); assertThat(range.getName(), equalTo("range"));
List<? extends Bucket> buckets = range.getBuckets(); List<? extends Bucket> buckets = range.getBuckets();
assertThat(range.getBuckets().size(), equalTo(1)); assertThat(range.getBuckets().size(), equalTo(1));
Range.Bucket bucket = buckets.get(0); Range.Bucket bucket = buckets.get(0);
assertThat((String) bucket.getKey(), equalTo("0.0.0.0/1")); assertEquals(4l, bucket.getDocCount());
assertThat(bucket.getFromAsString(), nullValue());
assertThat(bucket.getToAsString(), equalTo("128.0.0.0"));
}
@Test
public void cidr0Mask2() {
SearchResponse response = client().prepareSearch("idx_unmapped")
.addAggregation(ipRange("range")
.field("ip")
.addMaskRange("0.0.0.0/2"))
.execute().actionGet();
assertSearchResponse(response);
Range range = response.getAggregations().get("range");
assertThat(range, notNullValue());
assertThat(range.getName(), equalTo("range"));
List<? extends Bucket> buckets = range.getBuckets();
assertThat(range.getBuckets().size(), equalTo(1));
Range.Bucket bucket = buckets.get(0);
assertThat(bucket.getFromAsString(), nullValue());
assertThat(bucket.getToAsString(), equalTo("64.0.0.0"));
} }
} }