Fix cidr mask conversion issue for 0.0.0.0/0 and add tests
This commit is contained in:
parent
95f56d71bd
commit
3be98927a0
|
@ -139,6 +139,10 @@ public class IPv4RangeBuilder extends AbstractRangeBuilder<IPv4RangeBuilder> {
|
||||||
|
|
||||||
int mask = (-1) << (32 - Integer.parseInt(parts[4]));
|
int mask = (-1) << (32 - Integer.parseInt(parts[4]));
|
||||||
|
|
||||||
|
if (Integer.parseInt(parts[4]) == 0) {
|
||||||
|
mask = 0 << 32;
|
||||||
|
}
|
||||||
|
|
||||||
int from = addr & mask;
|
int from = addr & mask;
|
||||||
long longFrom = intIpToLongIp(from);
|
long longFrom = intIpToLongIp(from);
|
||||||
if (longFrom == 0) {
|
if (longFrom == 0) {
|
||||||
|
@ -148,7 +152,7 @@ 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 = -1;
|
longTo = InternalIPv4Range.MAX_IP - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new long[] { longFrom, longTo };
|
return new long[] { longFrom, longTo };
|
||||||
|
|
|
@ -25,6 +25,7 @@ import org.elasticsearch.script.Script;
|
||||||
import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
|
import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
|
||||||
import org.elasticsearch.search.aggregations.bucket.range.Range;
|
import org.elasticsearch.search.aggregations.bucket.range.Range;
|
||||||
import org.elasticsearch.search.aggregations.bucket.range.Range.Bucket;
|
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.max.Max;
|
||||||
import org.elasticsearch.search.aggregations.metrics.sum.Sum;
|
import org.elasticsearch.search.aggregations.metrics.sum.Sum;
|
||||||
import org.elasticsearch.test.ElasticsearchIntegrationTest;
|
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).getToAsString(), equalTo("10.0.0.10"));
|
||||||
assertThat(buckets.get(0).getDocCount(), equalTo(0l));
|
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<? extends Bucket> 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<? extends Bucket> 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<? 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"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue