Skip to content

Commit ef6f94f

Browse files
williamrandolphrjernsttlrx
authored
[6.8] Backport some fixes to CharArraysTest#testConstantTimeEquals() (#67168)
* 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). * Fix CharArraysTests.testConstantTimeEquals() (#47346) The change #47238 fixed a first issue (#47076) but introduced another one that can be reproduced using: org.elasticsearch.common.CharArraysTests > testConstantTimeEquals FAILED java.lang.StringIndexOutOfBoundsException: String index out of range: 1 at __randomizedtesting.SeedInfo.seed([DFCA64FE2C786BE3:ED987E883715C63B]:0) at java.lang.String.substring(String.java:1963) at org.elasticsearch.common.CharArraysTests.testConstantTimeEquals(CharArraysTests.java:74) REPRODUCE WITH: ./gradlew ':libs:elasticsearch-core:test' --tests "org.elasticsearch.common.CharArraysTests.testConstantTimeEquals" -Dtests.seed=DFCA64FE2C786BE3 -Dtests.security.manager=true -Dtests.locale=fr-CA -Dtests.timezone=Pacific/Johnston -Dcompiler.java=12 -Druntime.java=8 that happens when the first randomized string has a length of 0. Co-authored-by: Ryan Ernst <[email protected]> Co-authored-by: Tanguy Leroux <[email protected]>
1 parent 3b4ab52 commit ef6f94f

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
import java.nio.charset.StandardCharsets;
2525

26+
import static org.hamcrest.Matchers.is;
27+
2628
public class CharArraysTests extends ESTestCase {
2729

2830
public void testCharsToBytes() {
@@ -69,9 +71,13 @@ public void testConstantTimeEquals() {
6971
assertTrue(CharArrays.constantTimeEquals(value, value));
7072
assertTrue(CharArrays.constantTimeEquals(value.toCharArray(), value.toCharArray()));
7173

72-
final String other = randomAlphaOfLengthBetween(1, 32);
73-
assertFalse(CharArrays.constantTimeEquals(value, other));
74-
assertFalse(CharArrays.constantTimeEquals(value.toCharArray(), other.toCharArray()));
74+
// we want a different string, so ensure the first character is different, but the same overall length
75+
final int length = value.length();
76+
final String other = length > 0 ? new String(randomAlphaOfLengthNotBeginningWith(value.substring(0, 1), length, length)) : "";
77+
final boolean expectedEquals = length == 0;
78+
79+
assertThat("value: " + value + ", other: " + other, CharArrays.constantTimeEquals(value, other), is(expectedEquals));
80+
assertThat(CharArrays.constantTimeEquals(value.toCharArray(), other.toCharArray()), is(expectedEquals));
7581
}
7682

7783
private char[] randomAlphaOfLengthNotBeginningWith(String undesiredPrefix, int min, int max) {

0 commit comments

Comments
 (0)