From a01f451ef707c16d2b99f84290a94095493e6c8d Mon Sep 17 00:00:00 2001 From: Alan Woodward Date: Mon, 29 Apr 2019 13:19:56 +0100 Subject: [PATCH] Limit complexity of IntervalQueryBuilderTests#testRandomSource() (#41538) IntervalsSources can throw IllegalArgumentExceptions if they would produce too many disjunctions. To mitigate against this when building random sources, we limit the depth of the randomly generated source to four nested sources Fixes #41402 --- .../query/IntervalQueryBuilderTests.java | 63 ++++++++++--------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/server/src/test/java/org/elasticsearch/index/query/IntervalQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/IntervalQueryBuilderTests.java index d01cc174c30..2e809875c45 100644 --- a/server/src/test/java/org/elasticsearch/index/query/IntervalQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/IntervalQueryBuilderTests.java @@ -49,12 +49,7 @@ public class IntervalQueryBuilderTests extends AbstractQueryTestCase 18) { - return new IntervalsSourceProvider.IntervalFilter(createRandomSource(), randomFrom(filters)); - } - return null; - } - private static final String MASKED_FIELD = "masked_field"; private static final String NO_POSITIONS_FIELD = "no_positions_field"; @@ -88,42 +76,56 @@ public class IntervalQueryBuilderTests extends AbstractQueryTestCase 3) { + return createRandomMatch(depth + 1); + } switch (randomInt(20)) { case 0: case 1: int orCount = randomInt(4) + 1; List orSources = new ArrayList<>(); for (int i = 0; i < orCount; i++) { - orSources.add(createRandomSource()); + orSources.add(createRandomSource(depth + 1)); } - return new IntervalsSourceProvider.Disjunction(orSources, createRandomFilter()); + return new IntervalsSourceProvider.Disjunction(orSources, createRandomFilter(depth + 1)); case 2: case 3: int count = randomInt(5) + 1; List subSources = new ArrayList<>(); for (int i = 0; i < count; i++) { - subSources.add(createRandomSource()); + subSources.add(createRandomSource(depth + 1)); } boolean ordered = randomBoolean(); int maxGaps = randomInt(5) - 1; - IntervalsSourceProvider.IntervalFilter filter = createRandomFilter(); + IntervalsSourceProvider.IntervalFilter filter = createRandomFilter(depth + 1); return new IntervalsSourceProvider.Combine(subSources, ordered, maxGaps, filter); default: - int wordCount = randomInt(4) + 1; - List words = new ArrayList<>(); - for (int i = 0; i < wordCount; i++) { - words.add(randomRealisticUnicodeOfLengthBetween(4, 20)); - } - String text = String.join(" ", words); - boolean mOrdered = randomBoolean(); - int maxMGaps = randomInt(5) - 1; - String analyzer = randomFrom("simple", "keyword", "whitespace"); - return new IntervalsSourceProvider.Match(text, maxMGaps, mOrdered, analyzer, createRandomFilter(), useField); + return createRandomMatch(depth + 1); } } + private IntervalsSourceProvider.IntervalFilter createRandomFilter(int depth) { + if (depth < 3 && randomInt(20) > 18) { + return new IntervalsSourceProvider.IntervalFilter(createRandomSource(depth + 1), randomFrom(filters)); + } + return null; + } + + private IntervalsSourceProvider createRandomMatch(int depth) { + String useField = rarely() ? MASKED_FIELD : null; + int wordCount = randomInt(4) + 1; + List words = new ArrayList<>(); + for (int i = 0; i < wordCount; i++) { + words.add(randomRealisticUnicodeOfLengthBetween(4, 20)); + } + String text = String.join(" ", words); + boolean mOrdered = randomBoolean(); + int maxMGaps = randomInt(5) - 1; + String analyzer = randomFrom("simple", "keyword", "whitespace"); + return new IntervalsSourceProvider.Match(text, maxMGaps, mOrdered, analyzer, createRandomFilter(depth + 1), useField); + } + @Override protected void doAssertLuceneQuery(IntervalQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException { assertThat(query, instanceOf(IntervalQuery.class)); @@ -382,6 +384,5 @@ public class IntervalQueryBuilderTests extends AbstractQueryTestCase