Skip to content

Commit 5549398

Browse files
authored
[TEST] Fixes to StringMatcherTests (#88046) (#88103)
This commit fixes two issues with StringMatcherTests **Problem 1** Under some seeds (e.g. 3C93AEF7628D6611) prefix2 would start with the same text as prefix1. This would mean that input that was intended to test against prefix2 would actually match prefix1, and violate the expectations of the test **Problem 2** The code had the equivalent of randomFrom( "abc".toCharArray() ) But Hamcest does not have a randomFrom(char[]) method, so this code was treated as randomFrom( new Object[] { new char[] { 'a', 'b', 'c' } } ) and will return a char[]. Tests that were written assuming they had a random character, actually had an array (char[]) and would then stringify that object into something like [C@5e67f506 Resolves: #88024
1 parent 6a8b801 commit 5549398

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/support/StringMatcherTests.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ public void testSingleRegex() throws Exception {
108108

109109
public void testMultiplePatterns() throws Exception {
110110
final String prefix1 = randomAlphaOfLengthBetween(3, 5);
111-
final String prefix2 = randomAlphaOfLengthBetween(5, 8);
112-
final String prefix3 = randomAlphaOfLengthBetween(10, 12);
111+
final String prefix2 = randomValueOtherThanMany(s -> s.startsWith(prefix1), () -> randomAlphaOfLengthBetween(5, 8));
112+
final String prefix3 = randomValueOtherThanMany(s -> s.startsWith(prefix1), () -> randomAlphaOfLengthBetween(10, 12));
113113
final String suffix1 = randomAlphaOfLengthBetween(5, 10);
114114
final String suffix2 = randomAlphaOfLengthBetween(8, 12);
115115
final String exact1 = randomValueOtherThanMany(
@@ -129,19 +129,32 @@ public void testMultiplePatterns() throws Exception {
129129
List.of(prefix1 + "*", prefix2 + "?", "/" + prefix3 + "@/", "*" + suffix1, "/@" + suffix2 + "/", exact1, exact2, exact3)
130130
);
131131

132+
final List<Character> nonAlpha = "@/#$0123456789()[]{}<>;:%&".chars()
133+
.mapToObj(c -> Character.valueOf((char) c))
134+
.collect(Collectors.toList());
135+
132136
assertMatch(matcher, exact1);
133137
assertMatch(matcher, exact2);
134138
assertMatch(matcher, exact3);
135139
assertMatch(matcher, randomAlphaOfLength(3) + suffix1);
136140
assertMatch(matcher, randomAlphaOfLength(3) + suffix2);
141+
142+
// Prefix 1 uses '*', it should match any number of trailing chars.
137143
assertMatch(matcher, prefix1 + randomAlphaOfLengthBetween(1, 5));
138-
assertMatch(matcher, prefix2 + randomAlphaOfLength(1));
139-
assertMatch(matcher, prefix3 + randomAlphaOfLengthBetween(1, 5));
144+
assertMatch(matcher, prefix1 + randomFrom(nonAlpha));
145+
assertMatch(matcher, prefix1 + randomFrom(nonAlpha) + randomFrom(nonAlpha));
140146

141-
final char[] nonAlpha = "@/#$0123456789()[]{}<>;:%&".toCharArray();
147+
// Prefix 2 uses a `?`, it should match any single trailing char, but not 2 chars.
148+
// (We don't test with a trailing alpha because it might match one of the matched suffixes)
149+
assertMatch(matcher, prefix2 + randomAlphaOfLength(1));
150+
assertMatch(matcher, prefix2 + randomFrom(nonAlpha));
151+
assertNoMatch(matcher, prefix2 + randomFrom(nonAlpha) + randomFrom(nonAlpha));
152+
assertNoMatch(matcher, prefix2 + randomAlphaOfLength(1) + randomFrom(nonAlpha));
142153

143-
// Prefix 2 uses a `?`
144-
assertNoMatch(matcher, prefix2 + randomFrom(nonAlpha));
154+
// Prefix 3 uses a regex with '@', it should match any number of trailing chars.
155+
assertMatch(matcher, prefix3 + randomAlphaOfLengthBetween(1, 5));
156+
assertMatch(matcher, prefix3 + randomFrom(nonAlpha));
157+
assertMatch(matcher, prefix3 + randomFrom(nonAlpha) + randomFrom(nonAlpha));
145158

146159
for (String pattern : List.of(exact1, exact2, exact3, suffix1, suffix2, exact1, exact2, exact3)) {
147160
assertNoMatch(matcher, randomFrom(nonAlpha) + pattern + randomFrom(nonAlpha));

0 commit comments

Comments
 (0)