From 3be98927a03fc7bd13b0792baddf1b7492f8855c Mon Sep 17 00:00:00 2001 From: Ruflin Date: Tue, 21 Jul 2015 13:53:16 +0200 Subject: [PATCH] Fix cidr mask conversion issue for 0.0.0.0/0 and add tests --- .../bucket/range/ipv4/IPv4RangeBuilder.java | 6 +- .../aggregations/bucket/IPv4RangeTests.java | 70 +++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/ipv4/IPv4RangeBuilder.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/ipv4/IPv4RangeBuilder.java index 037afb3f1f9..86eb7eb2bcb 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/ipv4/IPv4RangeBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/ipv4/IPv4RangeBuilder.java @@ -139,6 +139,10 @@ public class IPv4RangeBuilder extends AbstractRangeBuilder { int mask = (-1) << (32 - Integer.parseInt(parts[4])); + if (Integer.parseInt(parts[4]) == 0) { + mask = 0 << 32; + } + int from = addr & mask; long longFrom = intIpToLongIp(from); if (longFrom == 0) { @@ -148,7 +152,7 @@ public class IPv4RangeBuilder extends AbstractRangeBuilder { int to = from + (~mask); 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) { - longTo = -1; + longTo = InternalIPv4Range.MAX_IP - 1; } return new long[] { longFrom, longTo }; diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/IPv4RangeTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/IPv4RangeTests.java index 1b72e5bfa63..244df1303ea 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/IPv4RangeTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/IPv4RangeTests.java @@ -25,6 +25,7 @@ import org.elasticsearch.script.Script; import org.elasticsearch.search.aggregations.bucket.histogram.Histogram; import org.elasticsearch.search.aggregations.bucket.range.Range; import org.elasticsearch.search.aggregations.bucket.range.Range.Bucket; +import org.elasticsearch.search.aggregations.bucket.range.ipv4.IPv4RangeBuilder; import org.elasticsearch.search.aggregations.metrics.max.Max; import org.elasticsearch.search.aggregations.metrics.sum.Sum; import org.elasticsearch.test.ElasticsearchIntegrationTest; @@ -869,4 +870,73 @@ public class IPv4RangeTests extends ElasticsearchIntegrationTest { assertThat(buckets.get(0).getToAsString(), equalTo("10.0.0.10")); assertThat(buckets.get(0).getDocCount(), equalTo(0l)); } + + @Test + public void cidr0Mask0() { + SearchResponse response = client().prepareSearch("idx_unmapped") + .addAggregation(ipRange("range") + .field("ip") + .addMaskRange("0.0.0.0/0")) + .execute().actionGet(); + + assertSearchResponse(response); + + + Range range = response.getAggregations().get("range"); + assertThat(range, notNullValue()); + assertThat(range.getName(), equalTo("range")); + List buckets = range.getBuckets(); + assertThat(range.getBuckets().size(), equalTo(1)); + + Range.Bucket bucket = buckets.get(0); + assertThat((String) bucket.getKey(), equalTo("0.0.0.0/0")); + + assertThat(bucket.getFromAsString(), nullValue()); + assertThat(bucket.getToAsString(), equalTo("255.255.255.255")); + } + + @Test + public void cidr0Mask1() { + SearchResponse response = client().prepareSearch("idx_unmapped") + .addAggregation(ipRange("range") + .field("ip") + .addMaskRange("0.0.0.0/1")) + .execute().actionGet(); + + assertSearchResponse(response); + + + Range range = response.getAggregations().get("range"); + assertThat(range, notNullValue()); + assertThat(range.getName(), equalTo("range")); + List buckets = range.getBuckets(); + assertThat(range.getBuckets().size(), equalTo(1)); + + Range.Bucket bucket = buckets.get(0); + assertThat((String) bucket.getKey(), equalTo("0.0.0.0/1")); + 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 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")); + } }