Skip to content

Commit

Permalink
Test many calls continue to return random data
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Jan 28, 2020
1 parent 532f4b2 commit b8c8598
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Random.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private function getIv(int $size):string {
$originalAesCounter & 0xFF
) . $iv;

$originalAesCounter--;
$originalAesCounter >>= 8;
}

return str_pad(
Expand Down
22 changes: 22 additions & 0 deletions test/phpunit/RandomTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,26 @@ public function testSequenceIsNotSame() {

self::assertNotSame($output1, $output2);
}

public function testManyCalls() {
$sut = new Random();
$totalBytes = "";
$expectedLength = 0;
$previousByteArray = [];

for($i = 0; $i < 500; $i++) {
$newBytes = $sut->getBytes(16);
$expectedLength += 16;

$totalBytes .= $newBytes;
self::assertNotContains(
$newBytes,
$previousByteArray,
"Random bytes should never provide the same sequence twice"
);
$previousByteArray []= $totalBytes;
}

self::assertEquals($expectedLength, strlen($totalBytes));
}
}

0 comments on commit b8c8598

Please sign in to comment.