Skip to content

Commit d824cbe

Browse files
authored
Test: ensure char[] doesn't being with prefix (#34816)
The testCharsBeginsWith test has a check that a random prefix of length 2 is not the prefix of a char[]. However, there is no check that the char[] is not randomly generated with the same two characters as the prefix. This change ensures that the char[] does not begin with the prefix. Closes #34765
1 parent 24df2eb commit d824cbe

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

libs/core/src/test/java/org/elasticsearch/common/CharArraysTests.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ public void testBytesToUtf8Chars() {
4343
assertArrayEquals(expectedChars, convertedChars);
4444
}
4545

46-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/34765")
4746
public void testCharsBeginsWith() {
4847
assertFalse(CharArrays.charsBeginsWith(randomAlphaOfLength(4), null));
4948
assertFalse(CharArrays.charsBeginsWith(null, null));
5049
assertFalse(CharArrays.charsBeginsWith(null, randomAlphaOfLength(4).toCharArray()));
51-
assertFalse(CharArrays.charsBeginsWith(randomAlphaOfLength(2), randomAlphaOfLengthBetween(3, 8).toCharArray()));
50+
final String undesiredPrefix = randomAlphaOfLength(2);
51+
assertFalse(CharArrays.charsBeginsWith(undesiredPrefix, randomAlphaOfLengthNotBeginningWith(undesiredPrefix, 3, 8)));
5252

5353
final String prefix = randomAlphaOfLengthBetween(2, 4);
5454
assertTrue(CharArrays.charsBeginsWith(prefix, prefix.toCharArray()));
@@ -73,4 +73,12 @@ public void testConstantTimeEquals() {
7373
assertFalse(CharArrays.constantTimeEquals(value, other));
7474
assertFalse(CharArrays.constantTimeEquals(value.toCharArray(), other.toCharArray()));
7575
}
76+
77+
private char[] randomAlphaOfLengthNotBeginningWith(String undesiredPrefix, int min, int max) {
78+
char[] nonMatchingValue;
79+
do {
80+
nonMatchingValue = randomAlphaOfLengthBetween(min, max).toCharArray();
81+
} while (new String(nonMatchingValue).startsWith(undesiredPrefix));
82+
return nonMatchingValue;
83+
}
7684
}

0 commit comments

Comments
 (0)