This commit is contained in:
2025-10-20 14:10:54 +02:00
parent 75ca8fd840
commit d2c1970ef8
732 changed files with 101915 additions and 2 deletions

View File

@@ -0,0 +1,58 @@
<?php
/*
* The RandomLib library for securely generating random numbers and strings in PHP
*
* @author Anthony Ferrara <ircmaxell@ircmaxell.com>
* @copyright 2011 The Authors
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version Build @@version@@
*/
namespace RandomLib;
use SecurityLib\Strength;
class FactoryTest extends \PHPUnit_Framework_TestCase
{
public function testConstruct()
{
$factory = new \RandomLib\Factory();
$this->assertTrue($factory instanceof \RandomLib\Factory);
}
public function testGetGeneratorFallback()
{
$factory = new \RandomLib\Factory();
$generator = $factory->getGenerator(new Strength(Strength::VERYLOW));
$mixer = call_user_func(array(get_class($generator->getMixer()), 'getStrength'));
$this->assertTrue($mixer->compare(new Strength(Strength::VERYLOW)) <= 0);
}
/**
* @covers RandomLib\Factory::getMediumStrengthGenerator
* @covers RandomLib\Factory::getGenerator
* @covers RandomLib\Factory::findMixer
* @covers RandomLib\Factory::findSources
*/
public function testGetMediumStrengthGenerator()
{
$factory = new \RandomLib\Factory();
$generator = $factory->getMediumStrengthGenerator();
$this->assertTrue($generator instanceof \RandomLib\Generator);
$mixer = call_user_func(array(get_class($generator->getMixer()), 'getStrength'));
$this->assertTrue($mixer->compare(new Strength(Strength::MEDIUM)) <= 0);
foreach ($generator->getSources() as $source) {
$strength = call_user_func(array(get_class($source), 'getStrength'));
$this->assertTrue($strength->compare(new Strength(Strength::MEDIUM)) >= 0);
}
}
/**
* @expectedException RuntimeException
* @expectedExceptionMessage Could not find sources
*/
public function testNoAvailableSource()
{
$factory = new \RandomLib\Factory();
$sources = new \ReflectionProperty($factory, 'sources');
$sources->setAccessible(\true);
$sources->setValue($factory, array());
$factory->getMediumStrengthGenerator();
}
}

View File

@@ -0,0 +1,75 @@
<?php
/*
* The RandomLib library for securely generating random numbers and strings in PHP
*
* @author Anthony Ferrara <ircmaxell@ircmaxell.com>
* @copyright 2011 The Authors
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version Build @@version@@
*/
namespace RandomLib;
class GeneratorStringTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Generator
*/
protected $generator = null;
/**
* @var Mixer
*/
protected $mixer = null;
/**
* @var array<int, Source>
*/
protected $sources = array();
public static function provideCharCombinations()
{
return array(array("CHAR_LOWER", implode("", range("a", "z"))), array("CHAR_UPPER", implode("", range("A", "Z"))), array("CHAR_DIGITS", implode("", range(0, 9))), array("CHAR_UPPER_HEX", "0123456789ABCDEF"), array("CHAR_LOWER_HEX", "0123456789abcdef"), array("CHAR_BASE64", "+/0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"), array("EASY_TO_READ", "3479ACEFHJKLMNPRTUVWXYabcdefghijkmnopqrstuvwxyz"), array("CHAR_BRACKETS", "()<>[]{}"), array("CHAR_SYMBOLS", " !\"#\$%&'()*+,-./:;<=>?@[\\]^_`{|}~"), array("CHAR_PUNCT", ",.:;"), array("CHAR_ALPHA", implode("", array_merge(range("A", "Z"), range("a", "z")))), array("CHAR_ALNUM", implode("", array_merge(range(0, 9), range("A", "Z"), range("a", "z")))), array("CHAR_ALPHA | PUNCT", ",.:;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", \RandomLib\Generator::CHAR_ALPHA | \RandomLib\Generator::CHAR_PUNCT), array("CHAR_LOWER | EASY_TO_READ", "abcdefghijkmnopqrstuvwxyz", \RandomLib\Generator::CHAR_LOWER | \RandomLib\Generator::EASY_TO_READ), array("CHAR_DIGITS | EASY_TO_READ", "3479", \RandomLib\Generator::CHAR_DIGITS | \RandomLib\Generator::EASY_TO_READ));
}
public function setUp()
{
$source1 = $this->getMock('RandomLib\Source');
$source1->expects($this->any())->method('generate')->will($this->returnCallback(function ($size) {
$r = '';
for ($i = 0; $i < $size; $i++) {
$r .= chr($i % 256);
}
return $r;
}));
$source2 = $this->getMock('RandomLib\Source');
$source2->expects($this->any())->method('generate')->will($this->returnCallback(function ($size) {
$r = '';
for ($i = 0; $i < $size; $i++) {
$r .= chr(0);
}
return $r;
}));
$this->mixer = $this->getMock('RandomLib\Mixer');
$this->mixer->expects($this->any())->method('mix')->will($this->returnCallback(function (array $sources) {
if (empty($sources)) {
return '';
}
$start = array_pop($sources);
// throw new \Exception('test');
return array_reduce($sources, function ($el1, $el2) {
return $el1 ^ $el2;
}, $start);
}));
$this->sources = array($source1, $source2);
$this->generator = new \RandomLib\Generator($this->sources, $this->mixer);
}
/**
* @dataProvider provideCharCombinations
*/
public function testScheme($schemeName, $expected, $scheme = 0)
{
// test for overspecification by doubling the expected amount
if (!$scheme) {
$scheme = constant("RandomLib\\Generator::{$schemeName}");
}
$chars = $this->generator->generateString(strlen($expected) * 2, $scheme);
$this->assertEquals($expected . $expected, $chars, sprintf("Testing Generator::%s failed", $schemeName));
}
}

View File

@@ -0,0 +1,138 @@
<?php
/*
* The RandomLib library for securely generating random numbers and strings in PHP
*
* @author Anthony Ferrara <ircmaxell@ircmaxell.com>
* @copyright 2011 The Authors
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version Build @@version@@
*/
namespace RandomLib;
class GeneratorTest extends \PHPUnit_Framework_TestCase
{
protected $generator = null;
protected $mixer = null;
protected $sources = array();
public static function provideGenerate()
{
return array(array(0, ''), array(1, chr(0)), array(2, chr(1) . chr(1)), array(3, chr(2) . chr(0) . chr(2)), array(4, chr(3) . chr(3) . chr(3) . chr(3)));
}
public static function provideGenerateInt()
{
return array(array(1, 1, 1), array(0, 1, 0), array(0, 255, 0), array(400, 655, 400), array(0, 65535, 257), array(65535, 131070, 65792), array(0, 16777215, (2 << 16) + 2), array(-10, 0, -10), array(-655, -400, -655), array(-131070, -65535, -130813));
}
public static function provideGenerateIntRangeTest()
{
return array(array(0, 0), array(0, 1), array(1, 10000), array(100000, \PHP_INT_MAX));
}
public static function provideGenerateStringTest()
{
return array(array(0, 'ab', ''), array(1, 'ab', 'a'), array(1, 'a', ''), array(2, 'ab', 'bb'), array(3, 'abc', 'cac'), array(8, '0123456789abcdef', '77777777'), array(16, '0123456789abcdef', 'ffffffffffffffff'), array(16, '', 'DDDDDDDDDDDDDDDD'));
}
public function setUp()
{
$source1 = $this->getMock('RandomLib\Source');
$source1->expects($this->any())->method('generate')->will($this->returnCallback(function ($size) {
$r = '';
for ($i = 0; $i < $size; $i++) {
$r .= chr($i);
}
return $r;
}));
$source2 = $this->getMock('RandomLib\Source');
$source2->expects($this->any())->method('generate')->will($this->returnCallback(function ($size) {
$r = '';
for ($i = $size - 1; $i >= 0; $i--) {
$r .= chr($i);
}
return $r;
}));
$this->mixer = $this->getMock('RandomLib\Mixer');
$this->mixer->expects($this->any())->method('mix')->will($this->returnCallback(function (array $sources) {
if (empty($sources)) {
return '';
}
$start = array_pop($sources);
return array_reduce($sources, function ($el1, $el2) {
return $el1 ^ $el2;
}, $start);
}));
$this->sources = array($source1, $source2);
$this->generator = new \RandomLib\Generator($this->sources, $this->mixer);
}
public function testConstruct()
{
$this->assertTrue($this->generator instanceof \RandomLib\Generator);
}
public function testGetMixer()
{
$this->assertSame($this->mixer, $this->generator->getMixer());
}
public function testGetSources()
{
$this->assertSame($this->sources, $this->generator->getSources());
}
/**
* @dataProvider provideGenerate
*/
public function testGenerate($size, $expect)
{
$this->assertEquals($expect, $this->generator->generate($size));
}
/**
* @dataProvider provideGenerateInt
*/
public function testGenerateInt($min, $max, $expect)
{
$this->assertEquals($expect, $this->generator->generateInt($min, $max));
}
/**
* @dataProvider provideGenerateIntRangeTest
*/
public function testGenerateIntRange($min, $max)
{
$n = $this->generator->generateInt($min, $max);
$this->assertTrue($min <= $n);
$this->assertTrue($max >= $n);
}
/**
* @expectedException RangeException
*/
public function testGenerateIntFail()
{
$n = $this->generator->generateInt(-1, \PHP_INT_MAX);
}
public function testGenerateIntLargeTest()
{
$bits = 30;
$expected = 50529027;
if (\PHP_INT_MAX > 4000000000) {
$bits = 55;
$expected = 1693273676973062;
}
$n = $this->generator->generateInt(0, (int) pow(2, $bits));
$this->assertEquals($expected, $n);
}
/**
* @dataProvider provideGenerateStringTest
*/
public function testGenerateString($length, $chars, $expected)
{
$n = $this->generator->generateString($length, $chars);
$this->assertEquals($expected, $n);
}
/**
* This test checks for issue #22:
*
* @see https://github.com/ircmaxell/RandomLib/issues/22
*/
public function testGenerateLargeRange()
{
if (\PHP_INT_MAX < pow(2, 32)) {
$this->markTestSkipped("Only test on 64 bit platforms");
}
$this->assertEquals(506381209866536711, $this->generator->generateInt(0, \PHP_INT_MAX));
}
}

View File

@@ -0,0 +1,57 @@
<?php
/*
* The RandomLib library for securely generating random numbers and strings in PHP
*
* @author Anthony Ferrara <ircmaxell@ircmaxell.com>
* @copyright 2011 The Authors
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version Build @@version@@
*/
namespace RandomLib\Mixer;
use SecurityLib\Strength;
class HashTest extends \PHPUnit_Framework_TestCase
{
public static function provideMix()
{
$data = array(
array(array(), ''),
array(array('1', '1'), '0d'),
array(array('a'), '61'),
// This expects 'b' because of how the mock hmac function works
array(array('a', 'b'), '9a'),
array(array('aa', 'ba'), '6e84'),
array(array('ab', 'bb'), 'b0cb'),
array(array('aa', 'bb'), 'ae8d'),
array(array('aa', 'bb', 'cc'), 'a14c'),
array(array('aabbcc', 'bbccdd', 'ccddee'), 'a8aff3939934'),
);
return $data;
}
public function testConstructWithoutArgument()
{
$hash = new \RandomLib\Mixer\Hash();
$this->assertTrue($hash instanceof \RandomLib\Mixer);
}
public function testGetStrength()
{
$strength = new Strength(Strength::MEDIUM);
$actual = \RandomLib\Mixer\Hash::getStrength();
$this->assertEquals($actual, $strength);
}
public function testTest()
{
$actual = \RandomLib\Mixer\Hash::test();
$this->assertTrue($actual);
}
/**
* @dataProvider provideMix
*/
public function testMix($parts, $result)
{
$mixer = new \RandomLib\Mixer\Hash('md5');
$actual = $mixer->mix($parts);
$this->assertEquals($result, bin2hex($actual));
}
}

View File

@@ -0,0 +1,52 @@
<?php
/*
* The RandomLib library for securely generating random numbers and strings in PHP
*
* @author Anthony Ferrara <ircmaxell@ircmaxell.com>
* @copyright 2011 The Authors
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version Build @@version@@
*/
namespace RandomLib\Mixer;
use SecurityLib\Strength;
class McryptRijndael128Test extends \PHPUnit_Framework_TestCase
{
public static function provideMix()
{
$data = array(array(array(), ''), array(array('', ''), ''), array(array('a'), '61'), array(array('a', 'b'), '6a'), array(array('aa', 'ba'), '688d'), array(array('ab', 'bb'), 'f8bc'), array(array('aa', 'bb'), 'a0f3'), array(array('aa', 'bb', 'cc'), '87c3'), array(array('aabbcc', 'bbccdd', 'ccddee'), '7cf2273e46c7'));
return $data;
}
protected function setUp()
{
if (!\extension_loaded('mcrypt') || \PHP_VERSION_ID >= 70100) {
$this->markTestSkipped('mcrypt extension is not available');
}
}
public function testConstructWithoutArgument()
{
$hash = new \RandomLib\Mixer\McryptRijndael128();
$this->assertTrue($hash instanceof \RandomLib\Mixer);
}
public function testGetStrength()
{
$strength = new Strength(Strength::HIGH);
$actual = \RandomLib\Mixer\McryptRijndael128::getStrength();
$this->assertEquals($actual, $strength);
}
public function testTest()
{
$actual = \RandomLib\Mixer\McryptRijndael128::test();
$this->assertTrue($actual);
}
/**
* @dataProvider provideMix
*/
public function testMix($parts, $result)
{
$mixer = new \RandomLib\Mixer\McryptRijndael128();
$actual = $mixer->mix($parts);
$this->assertEquals($result, bin2hex($actual));
}
}

View File

@@ -0,0 +1,52 @@
<?php
/*
* The RandomLib library for securely generating random numbers and strings in PHP
*
* @author Anthony Ferrara <ircmaxell@ircmaxell.com>
* @copyright 2011 The Authors
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version Build @@version@@
*/
namespace RandomLib\Mixer;
use SecurityLib\Strength;
class SodiumTest extends \PHPUnit_Framework_TestCase
{
public static function provideMix()
{
$data = array(array(array(), ''), array(array('', ''), ''), array(array('a'), '61'), array(array('a', 'b'), '44'), array(array('aa', 'ba'), '6967'), array(array('ab', 'bb'), '73a6'), array(array('aa', 'bb'), 'bc7b'), array(array('aa', 'bb', 'cc'), '0cbd'), array(array('aabbcc', 'bbccdd', 'ccddee'), '5f0005cacd7c'));
return $data;
}
protected function setUp()
{
if (!\is_callable('sodium_crypto_generichash') || defined('HHVM_VERSION')) {
$this->markTestSkipped('sodium extension is not available');
}
}
public function testConstructWithoutArgument()
{
$hash = new \RandomLib\Mixer\SodiumMixer();
$this->assertTrue($hash instanceof \RandomLib\Mixer);
}
public function testGetStrength()
{
$strength = new Strength(Strength::HIGH);
$actual = \RandomLib\Mixer\SodiumMixer::getStrength();
$this->assertEquals($actual, $strength);
}
public function testTest()
{
$actual = \RandomLib\Mixer\SodiumMixer::test();
$this->assertTrue($actual);
}
/**
* @dataProvider provideMix
*/
public function testMix($parts, $result)
{
$mixer = new \RandomLib\Mixer\SodiumMixer();
$actual = $mixer->mix($parts);
$this->assertEquals($result, bin2hex($actual));
}
}

View File

@@ -0,0 +1,59 @@
<?php
/*
* The RandomLib library for securely generating random numbers and strings in PHP
*
* @author Anthony Ferrara <ircmaxell@ircmaxell.com>
* @copyright 2011 The Authors
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version Build @@version@@
*/
namespace RandomLib\Source;
use SecurityLib\Strength;
abstract class AbstractSourceTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
$class = static::getTestedClass();
if (!$class::isSupported()) {
$this->markTestSkipped();
}
}
protected static function getTestedClass()
{
return preg_replace('/Test$/', '', get_called_class());
}
protected static function getExpectedStrength()
{
return new Strength(Strength::VERYLOW);
}
public static function provideGenerate()
{
$data = array();
for ($i = 0; $i < 100; $i += 5) {
$not = $i > 0 ? str_repeat(chr(0), $i) : chr(0);
$data[] = array($i, $not);
}
return $data;
}
public function testGetStrength()
{
$class = static::getTestedClass();
$strength = static::getExpectedStrength();
$actual = $class::getStrength();
$this->assertEquals($actual, $strength);
}
/**
* @dataProvider provideGenerate
* @group slow
*/
public function testGenerate($length, $not)
{
$class = static::getTestedClass();
$rand = new $class();
$stub = $rand->generate($length);
$this->assertEquals($length, strlen($stub));
$this->assertNotEquals($not, $stub);
}
}

View File

@@ -0,0 +1,20 @@
<?php
/*
* The RandomLib library for securely generating random numbers and strings in PHP
*
* @author Anthony Ferrara <ircmaxell@ircmaxell.com>
* @copyright 2011 The Authors
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version Build @@version@@
*/
namespace RandomLib\Source;
use SecurityLib\Strength;
class CAPICOMTest extends \RandomLib\Source\AbstractSourceTest
{
protected static function getExpectedStrength()
{
return new Strength(Strength::MEDIUM);
}
}

View File

@@ -0,0 +1,24 @@
<?php
/*
* The RandomLib library for securely generating random numbers and strings in PHP
*
* @author Anthony Ferrara <ircmaxell@ircmaxell.com>
* @copyright 2011 The Authors
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version Build @@version@@
*/
namespace RandomLib\Source;
use SecurityLib\Strength;
class MTRandTest extends \RandomLib\Source\AbstractSourceTest
{
protected static function getExpectedStrength()
{
if (defined('S_ALL')) {
return new Strength(Strength::LOW);
} else {
return new Strength(Strength::VERYLOW);
}
}
}

View File

@@ -0,0 +1,31 @@
<?php
/*
* The RandomLib library for securely generating random numbers and strings in PHP
*
* @author Anthony Ferrara <ircmaxell@ircmaxell.com>
* @copyright 2011 The Authors
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version Build @@version@@
*/
namespace RandomLib\Source;
use SecurityLib\Strength;
class MicroTimeTest extends \RandomLib\Source\AbstractSourceTest
{
protected static function getExpectedStrength()
{
return new Strength(Strength::VERYLOW);
}
/**
* Test the initialization of the static counter (!== 0)
*/
public function testCounterNotNull()
{
$class = static::getTestedClass();
$rand = new $class();
$reflection_class = new \ReflectionClass($class);
$static = $reflection_class->getStaticProperties();
$this->assertTrue($static['counter'] !== 0);
}
}

View File

@@ -0,0 +1,24 @@
<?php
/*
* The RandomLib library for securely generating random numbers and strings in PHP
*
* @author Anthony Ferrara <ircmaxell@ircmaxell.com>
* @copyright 2011 The Authors
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version Build @@version@@
*/
namespace RandomLib\Source;
use SecurityLib\Strength;
class RandTest extends \RandomLib\Source\AbstractSourceTest
{
protected static function getExpectedStrength()
{
if (defined('S_ALL')) {
return new Strength(Strength::LOW);
} else {
return new Strength(Strength::VERYLOW);
}
}
}

View File

@@ -0,0 +1,77 @@
<?php
/*
* The RandomLib library for securely generating random numbers and strings in PHP
*
* @author Anthony Ferrara <ircmaxell@ircmaxell.com>
* @copyright 2011 The Authors
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version Build @@version@@
*/
namespace RandomLib\Source;
use SecurityLib\Strength;
class SodiumTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
if (!extension_loaded('libsodium')) {
$this->markTestSkipped('The libsodium extension is not loaded');
}
}
public static function provideGenerate()
{
$data = array();
for ($i = 1; $i < 100; $i += 5) {
$not = str_repeat(chr(0), $i);
$data[] = array($i, $not);
}
return $data;
}
public function testGetStrength()
{
$strength = new Strength(Strength::HIGH);
$actual = \RandomLib\Source\Sodium::getStrength();
$this->assertEquals($actual, $strength);
}
/**
* @dataProvider provideGenerate
*/
public function testGenerate($length, $not)
{
if (!extension_loaded('libsodium')) {
$this->markTestSkipped('The libsodium extension is not loaded');
}
$rand = new \RandomLib\Source\Sodium();
$stub = $rand->generate($length);
$this->assertEquals($length, strlen($stub));
$this->assertNotEquals($not, $stub);
}
/**
* @dataProvider provideGenerate
*/
public function testGenerateWithoutLibsodium($length, $not)
{
$rand = new \RandomLib\Source\Sodium(\false);
$stub = $rand->generate($length);
$this->assertEquals($length, strlen($stub));
$this->assertEquals($not, $stub);
}
public function testGenerateWithZeroLength()
{
if (!extension_loaded('libsodium')) {
$this->markTestSkipped('The libsodium extension is not loaded');
}
$rand = new \RandomLib\Source\Sodium();
$stub = $rand->generate(0);
$this->assertEquals(0, strlen($stub));
$this->assertEquals('', $stub);
}
public function testGenerateWithZeroLengthWithoutLibsodium()
{
$rand = new \RandomLib\Source\Sodium(\false);
$stub = $rand->generate(0);
$this->assertEquals(0, strlen($stub));
$this->assertEquals('', $stub);
}
}

View File

@@ -0,0 +1,20 @@
<?php
/*
* The RandomLib library for securely generating random numbers and strings in PHP
*
* @author Anthony Ferrara <ircmaxell@ircmaxell.com>
* @copyright 2011 The Authors
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version Build @@version@@
*/
namespace RandomLib\Source;
use SecurityLib\Strength;
class URandomTest extends \RandomLib\Source\AbstractSourceTest
{
protected static function getExpectedStrength()
{
return new Strength(Strength::HIGH);
}
}

View File

@@ -0,0 +1,20 @@
<?php
/*
* The RandomLib library for securely generating random numbers and strings in PHP
*
* @author Anthony Ferrara <ircmaxell@ircmaxell.com>
* @copyright 2011 The Authors
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version Build @@version@@
*/
namespace RandomLib\Source;
use SecurityLib\Strength;
class UniqIDTest extends \RandomLib\Source\AbstractSourceTest
{
protected static function getExpectedStrength()
{
return new Strength(Strength::LOW);
}
}