Skip to content

Commit 758e099

Browse files
committed
Ensure char array test uses different values (#47238)
The test of constantTimeEquals could get unlucky and randomly produce the same two strings. This commit tweaks the test to ensure the two string are unique, and the loop inside constantTimeEquals is actually executed (which requires the strings be of the same length). fixes #47076
1 parent 9b1ae8b commit 758e099

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ public void testConstantTimeEquals() {
6969
assertTrue(CharArrays.constantTimeEquals(value, value));
7070
assertTrue(CharArrays.constantTimeEquals(value.toCharArray(), value.toCharArray()));
7171

72-
final String other = randomAlphaOfLengthBetween(1, 32);
73-
assertFalse(CharArrays.constantTimeEquals(value, other));
72+
// we want a different string, so ensure the first character is different, but the same overall length
73+
final String other = new String(
74+
randomAlphaOfLengthNotBeginningWith(value.substring(0, 1), value.length(), value.length()));
75+
assertFalse("value: " + value + ", other: " + other, CharArrays.constantTimeEquals(value, other));
7476
assertFalse(CharArrays.constantTimeEquals(value.toCharArray(), other.toCharArray()));
7577
}
7678

0 commit comments

Comments
 (0)