2014-12-04 18:22:20 +00:00
|
|
|
<?php
|
2022-02-17 02:25:59 +00:00
|
|
|
|
2014-12-04 18:22:20 +00:00
|
|
|
/**
|
|
|
|
* @author Andreas Fischer <bantu@phpbb.com>
|
2014-12-09 23:02:44 +00:00
|
|
|
* @copyright 2014 Andreas Fischer
|
2014-12-04 18:22:20 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
*/
|
|
|
|
|
2022-06-04 15:31:21 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-02-23 02:48:51 +00:00
|
|
|
namespace phpseclib3\Tests\Unit\Crypt;
|
|
|
|
|
2019-11-07 05:41:40 +00:00
|
|
|
use phpseclib3\Crypt\Random;
|
2022-02-23 02:48:51 +00:00
|
|
|
use phpseclib3\Tests\PhpseclibTestCase;
|
2014-12-04 23:20:19 +00:00
|
|
|
|
2022-02-23 02:48:51 +00:00
|
|
|
class RandomTest extends PhpseclibTestCase
|
2014-12-04 18:22:20 +00:00
|
|
|
{
|
2024-02-08 03:17:27 +00:00
|
|
|
public static function stringLengthData(): array
|
2014-12-04 18:22:20 +00:00
|
|
|
{
|
2024-02-08 03:17:27 +00:00
|
|
|
return array_map(fn($x) => [$x], [
|
2014-12-04 18:22:20 +00:00
|
|
|
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 17, 19, 20, 23, 29, 31, 37,
|
|
|
|
41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 111, 128, 1000,
|
2022-07-23 02:45:53 +00:00
|
|
|
1024, 10000, 12345, 100000, 123456,
|
2017-11-27 08:30:14 +00:00
|
|
|
]);
|
2014-12-04 18:22:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @dataProvider stringLengthData */
|
2022-06-04 15:31:21 +00:00
|
|
|
public function testStringLength($length): void
|
2014-12-04 18:22:20 +00:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
|
|
|
$length,
|
2014-12-04 23:20:19 +00:00
|
|
|
strlen(Random::string($length)),
|
2014-12-04 18:22:20 +00:00
|
|
|
'Failed asserting that a string of expected length was generated.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-29 16:07:17 +00:00
|
|
|
* Takes a set of random values of length 128 bits and asserts all taken
|
|
|
|
* values are unique.
|
|
|
|
*/
|
2022-06-04 15:31:21 +00:00
|
|
|
public function testStringUniqueness(): void
|
2014-12-04 18:22:20 +00:00
|
|
|
{
|
2017-11-27 08:30:14 +00:00
|
|
|
$values = [];
|
2014-12-04 18:22:20 +00:00
|
|
|
for ($i = 0; $i < 10000; ++$i) {
|
2014-12-04 23:20:19 +00:00
|
|
|
$rand = Random::string(16);
|
2014-12-04 18:22:20 +00:00
|
|
|
$this->assertSame(16, strlen($rand));
|
2014-12-04 23:18:45 +00:00
|
|
|
$this->assertArrayNotHasKey(
|
|
|
|
$rand,
|
|
|
|
$values,
|
2014-12-04 18:22:20 +00:00
|
|
|
'Failed asserting that generated value does not exist in set.'
|
|
|
|
);
|
|
|
|
$values[$rand] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|