Skip to content

Commit 552298f

Browse files
committed
test: fix flaky TestAESPRNGRandom
- fix flaky TestAESPRNGRandom by retrying on small-buffer collisions
1 parent e6337ab commit 552298f

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

CryptoLib.Tests/src/Security/SecureRandomTests.pas

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,16 @@ procedure TTestSecureRandom.TestAESPRNG;
217217
end;
218218

219219
procedure TTestSecureRandom.TestAESPRNGRandom;
220+
const
221+
// For very small buffers two independent random draws can legitimately be
222+
// equal (e.g. a single byte collides with probability 1/256). Retrying a
223+
// few times distinguishes ordinary randomness (which diverges on retry)
224+
// from a genuinely stuck RNG (which keeps returning identical output).
225+
MaxCollisionRetries = Int32(8);
220226
var
221227
b1, b2: TBytes;
222228
a1, a2: IRandomNumberGenerator;
223-
Idx: Int32;
229+
Idx, Retry: Int32;
224230
begin
225231
// it is hard to validate randomness - we just test the feature set
226232
b1 := nil;
@@ -248,6 +254,14 @@ procedure TTestSecureRandom.TestAESPRNGRandom;
248254
a1.GetBytes(b1);
249255
a2.GetBytes(b2);
250256

257+
Retry := 0;
258+
while AreEqual(b1, b2) and (Retry < MaxCollisionRetries) do
259+
begin
260+
a1.GetBytes(b1);
261+
a2.GetBytes(b2);
262+
System.Inc(Retry);
263+
end;
264+
251265
CheckTrue(not AreEqual(b1, b2));
252266
end;
253267

0 commit comments

Comments
 (0)