2014-12-04 19:22:20 +01:00
|
|
|
<?php
|
2022-02-17 03:25:59 +01:00
|
|
|
|
2014-12-04 19:22:20 +01:00
|
|
|
/**
|
|
|
|
* @author Andreas Fischer <bantu@phpbb.com>
|
2014-12-10 00:02:44 +01:00
|
|
|
* @copyright 2014 Andreas Fischer
|
2014-12-04 19:22:20 +01:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
*/
|
|
|
|
|
2019-11-07 06:41:40 +01:00
|
|
|
use phpseclib3\Crypt\Random;
|
2014-12-05 00:20:19 +01:00
|
|
|
|
2014-12-04 19:22:20 +01:00
|
|
|
class Unit_Crypt_RandomTest extends PhpseclibTestCase
|
|
|
|
{
|
|
|
|
public function stringLengthData()
|
|
|
|
{
|
2017-11-27 09:30:14 +01:00
|
|
|
return array_map([$this, 'wrap'], [
|
2014-12-04 19:22:20 +01: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,
|
|
|
|
1024, 10000, 12345, 100000, 123456
|
2017-11-27 09:30:14 +01:00
|
|
|
]);
|
2014-12-04 19:22:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @dataProvider stringLengthData */
|
|
|
|
public function testStringLength($length)
|
|
|
|
{
|
|
|
|
$this->assertSame(
|
|
|
|
$length,
|
2014-12-05 00:20:19 +01:00
|
|
|
strlen(Random::string($length)),
|
2014-12-04 19:22:20 +01:00
|
|
|
'Failed asserting that a string of expected length was generated.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-29 18:07:17 +02:00
|
|
|
* Takes a set of random values of length 128 bits and asserts all taken
|
|
|
|
* values are unique.
|
|
|
|
*/
|
2014-12-04 19:22:20 +01:00
|
|
|
public function testStringUniqueness()
|
|
|
|
{
|
2017-11-27 09:30:14 +01:00
|
|
|
$values = [];
|
2014-12-04 19:22:20 +01:00
|
|
|
for ($i = 0; $i < 10000; ++$i) {
|
2014-12-05 00:20:19 +01:00
|
|
|
$rand = Random::string(16);
|
2014-12-04 19:22:20 +01:00
|
|
|
$this->assertSame(16, strlen($rand));
|
2014-12-05 00:18:45 +01:00
|
|
|
$this->assertArrayNotHasKey(
|
|
|
|
$rand,
|
|
|
|
$values,
|
2014-12-04 19:22:20 +01:00
|
|
|
'Failed asserting that generated value does not exist in set.'
|
|
|
|
);
|
|
|
|
$values[$rand] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function wrap($x)
|
|
|
|
{
|
|
|
|
// array() is not a function, but $this->wrap() is.
|
2017-11-27 09:30:14 +01:00
|
|
|
return [$x];
|
2014-12-04 19:22:20 +01:00
|
|
|
}
|
|
|
|
}
|