From 5717d0f6d3f084a51e2d1446138802a3544d0735 Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Fri, 27 Sep 2019 10:20:56 -0700 Subject: [PATCH] Ensure char array test uses different values 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 --- .../test/java/org/elasticsearch/common/CharArraysTests.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/libs/core/src/test/java/org/elasticsearch/common/CharArraysTests.java b/libs/core/src/test/java/org/elasticsearch/common/CharArraysTests.java index 368886c7fd3d6..a3f964bc14cda 100644 --- a/libs/core/src/test/java/org/elasticsearch/common/CharArraysTests.java +++ b/libs/core/src/test/java/org/elasticsearch/common/CharArraysTests.java @@ -69,8 +69,10 @@ public void testConstantTimeEquals() { assertTrue(CharArrays.constantTimeEquals(value, value)); assertTrue(CharArrays.constantTimeEquals(value.toCharArray(), value.toCharArray())); - final String other = randomAlphaOfLengthBetween(1, 32); - assertFalse(CharArrays.constantTimeEquals(value, other)); + // we want a different string, so ensure the first character is different, but the same overall length + final String other = new String( + randomAlphaOfLengthNotBeginningWith(value.substring(0, 1), value.length(), value.length())); + assertFalse("value: " + value + ", other: " + other, CharArrays.constantTimeEquals(value, other)); assertFalse(CharArrays.constantTimeEquals(value.toCharArray(), other.toCharArray())); }