Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions libs/core/src/test/java/org/elasticsearch/core/GlobTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void testMatchLiteral() {
str = randomAlphanumericOfLength(randomIntBetween(1, 12));
assertMatch(str, str);

str = randomAsciiString(randomIntBetween(1, 24), ch -> ch >= ' ' && ch <= '~' && ch != '*');
str = randomAsciiStringNoAsterisks(randomIntBetween(1, 24));
assertMatch(str, str);
}

Expand Down Expand Up @@ -67,7 +67,7 @@ public void testPrefixMatch() {
assertNonMatch("123*", "abc123");
assertNonMatch("123*", "abc123def");

var prefix = randomAsciiString(randomIntBetween(2, 12));
var prefix = randomAsciiStringNoAsterisks(randomIntBetween(2, 12));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was testing (potentially) things with multiple * before, like a "prefix" 123*45* (and I think the assertions were correct too?); are we sure this is not something we want to test?

Copy link
Contributor Author

@tvernum tvernum Aug 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If prefix was *a then pattern would be *a*

That works (logically) for the matches

  • *a* matches *a
  • *a* matches *a + random

But it breaks the logic for the non-matches
For example:

assertNonMatch(pattern, prefix.substring(1));

That would test ("*a*" , "a") which would match, when it is not supposed to.
The intent of that test is that prefix is a literal (e.g. ab) so that ab* does not match b

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see, there are randoms for which the test would fail.
Seems fair, if we feel the need for a test with multiple * we can add it explicitly, and be more precise in these prefix/suffix tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the range in these tests randomIntBetween(2, 12)? That seems an odd choice that explicitly avoids an important corner case: shouldn't 1 also work? And, what's the benefit from testing lengths greater than 2? I think we ought to use something more like randomIntBetween(1,2) here.

var pattern = prefix + "*";
assertMatch(pattern, prefix);
assertMatch(pattern, prefix + randomAsciiString(randomIntBetween(1, 30)));
Expand All @@ -92,7 +92,7 @@ public void testSuffixMatch() {
assertNonMatch("*123", "123abc");
assertNonMatch("*123", "abc123def");

var suffix = randomAsciiString(randomIntBetween(2, 12));
var suffix = randomAsciiStringNoAsterisks(randomIntBetween(2, 12));
var pattern = "*" + suffix;
assertMatch(pattern, suffix);
assertMatch(pattern, randomAsciiString(randomIntBetween(1, 30)) + suffix);
Expand All @@ -114,7 +114,7 @@ public void testInfixStringMatch() {
assertNonMatch("*123*", "12*");
assertNonMatch("*123*", "1.2.3");

var infix = randomAsciiString(randomIntBetween(2, 12));
var infix = randomAsciiStringNoAsterisks(randomIntBetween(2, 12));
var pattern = "*" + infix + "*";
assertMatch(pattern, infix);
assertMatch(pattern, randomAsciiString(randomIntBetween(1, 30)) + infix + randomAsciiString(randomIntBetween(1, 30)));
Expand All @@ -138,8 +138,8 @@ public void testInfixAsteriskMatch() {
assertMatch("123*321", "12345678987654321");
assertNonMatch("123*321", "12321");

var prefix = randomAsciiString(randomIntBetween(2, 12));
var suffix = randomAsciiString(randomIntBetween(2, 12));
var prefix = randomAsciiStringNoAsterisks(randomIntBetween(2, 12));
var suffix = randomAsciiStringNoAsterisks(randomIntBetween(2, 12));
var pattern = prefix + "*" + suffix;
assertMatch(pattern, prefix + suffix);
assertMatch(pattern, prefix + randomAsciiString(randomIntBetween(1, 30)) + suffix);
Expand Down Expand Up @@ -180,6 +180,10 @@ private String randomAsciiString(int length) {
return randomAsciiString(length, ch -> ch >= ' ' && ch <= '~');
}

private String randomAsciiStringNoAsterisks(final int length) {
return randomAsciiString(length, ch -> ch >= ' ' && ch <= '~' && ch != '*');
}

private String randomAsciiString(int length, CharPredicate validCharacters) {
StringBuilder str = new StringBuilder(length);
nextChar: for (int i = 0; i < length; i++) {
Expand Down