This commit is contained in:
Xes
2025-08-14 22:41:49 +02:00
parent 2de81ccc46
commit 8ce45119b6
39774 changed files with 4309466 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
<?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@@
*/
/**
* The interface that all hash implementations must implement
*
* PHP version 5.3
*
* @category PHPPasswordLib
* @package Hash
*
* @author Anthony Ferrara <ircmaxell@ircmaxell.com>
* @copyright 2011 The Authors
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @license http://www.gnu.org/licenses/lgpl-2.1.html LGPL v 2.1
*/
namespace RandomLibtest\Mocks;
/**
* The interface that all hash implementations must implement
*
* @category PHPPasswordLib
* @package Hash
*
* @author Anthony Ferrara <ircmaxell@ircmaxell.com>
*/
class AbstractMock
{
protected $callbacks = array();
public static function init()
{
}
public function __construct(array $callbacks = array())
{
$this->callbacks = $callbacks;
}
public function __call($name, array $args = array())
{
if (isset($this->callbacks[$name])) {
return call_user_func_array($this->callbacks[$name], $args);
}
return null;
}
}

View File

@@ -0,0 +1,80 @@
<?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@@
*/
/**
* The Mixer strategy interface.
*
* All mixing strategies must implement this interface
*
* PHP version 5.3
*
* @category PHPPasswordLib
* @package Random
*
* @author Anthony Ferrara <ircmaxell@ircmaxell.com>
* @copyright 2011 The Authors
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @license http://www.gnu.org/licenses/lgpl-2.1.html LGPL v 2.1
*/
namespace RandomLibtest\Mocks\Random;
/**
* The Mixer strategy interface.
*
* All mixing strategies must implement this interface
*
* @category PHPPasswordLib
* @package Random
*
* @author Anthony Ferrara <ircmaxell@ircmaxell.com>
*/
class Generator extends \RandomLib\Generator
{
protected $callbacks = array();
public static function init()
{
}
public function __construct(array $callbacks = array())
{
$this->callbacks = $callbacks;
}
public function __call($name, array $args = array())
{
if (isset($this->callbacks[$name])) {
return call_user_func_array($this->callbacks[$name], $args);
}
return null;
}
public function addSource(\PasswordLib\Random\Source $source)
{
return $this->__call('addSource', array($source));
}
public function generate($size)
{
return $this->__call('generate', array($size));
}
public function generateInt($min = 0, $max = \PHP_INT_MAX)
{
return $this->__call('generateInt', array($min, $max));
}
public function generateString($length, $chars = '')
{
return $this->__call('generateString', array($length, $chars));
}
}

View File

@@ -0,0 +1,86 @@
<?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@@
*/
/**
* The Mixer strategy interface.
*
* All mixing strategies must implement this interface
*
* PHP version 5.3
*
* @category PHPPasswordLib
* @package Random
*
* @author Anthony Ferrara <ircmaxell@ircmaxell.com>
* @copyright 2011 The Authors
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @license http://www.gnu.org/licenses/lgpl-2.1.html LGPL v 2.1
*/
namespace RandomLibtest\Mocks\Random;
use SecurityLib\Strength;
/**
* The Mixer strategy interface.
*
* All mixing strategies must implement this interface
*
* @category PHPPasswordLib
* @package Random
*
* @author Anthony Ferrara <ircmaxell@ircmaxell.com>
*/
class Mixer extends \RandomLibTest\Mocks\AbstractMock implements \RandomLib\Mixer
{
public static $strength = null;
public static $test = true;
public static function init()
{
static::$strength = new Strength(Strength::HIGH);
static::$test = true;
}
/**
* Return an instance of Strength indicating the strength of the mixer
*
* @return \SecurityLib\Strength An instance of one of the strength classes
*/
public static function getStrength()
{
return static::$strength;
}
/**
* Test to see if the mixer is available
*
* @return bool If the mixer is available on the system
*/
public static function test()
{
return static::$test;
}
/**
* Mix the provided array of strings into a single output of the same size
*
* All elements of the array should be the same size.
*
* @param array $parts The parts to be mixed
*
* @return string The mixed result
*/
public function mix(array $parts)
{
return $this->__call('mix', array($parts));
}
}

View File

@@ -0,0 +1,85 @@
<?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@@
*/
/**
* The Random Number Source interface.
*
* All random number sources must implement this interface
*
* PHP version 5.3
*
* @category PHPPasswordLib
* @package Random
*
* @author Anthony Ferrara <ircmaxell@ircmaxell.com>
* @copyright 2011 The Authors
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @license http://www.gnu.org/licenses/lgpl-2.1.html LGPL v 2.1
*/
namespace RandomLibtest\Mocks\Random;
use SecurityLib\Strength;
/**
* The Random Number Source interface.
*
* All random number sources must implement this interface
*
* @category PHPPasswordLib
* @package Random
*
* @author Anthony Ferrara <ircmaxell@ircmaxell.com>
*/
class Source extends \RandomLibTest\Mocks\AbstractMock implements \RandomLib\Source
{
public static $strength = null;
public static function init()
{
static::$strength = new Strength(Strength::VERYLOW);
}
/**
* Return an instance of Strength indicating the strength of the source
*
* @return \SecurityLib\Strength An instance of one of the strength classes
*/
public static function getStrength()
{
return static::$strength;
}
/**
* If the source is currently available.
* Reasons might be because the library is not installed
*
* @return bool
*/
public static function isSupported()
{
return true;
}
/**
* Generate a random string of the specified size
*
* Note: If the source fails to generate enough data, the result must be
* padded to the requested length.
*
* @param int $size The size of the requested random string
*
* @return string A string of the requested size
*/
public function generate($size)
{
return $this->__call('generate', array($size));
}
}

View File

@@ -0,0 +1,68 @@
<?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 Factory();
$this->assertTrue($factory instanceof Factory);
}
public function testGetGeneratorFallback()
{
$factory = new 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 Factory();
$generator = $factory->getMediumStrengthGenerator();
$this->assertTrue($generator instanceof 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 Factory();
$sources = new \ReflectionProperty($factory, 'sources');
$sources->setAccessible(true);
$sources->setValue($factory, array());
$factory->getMediumStrengthGenerator();
}
}

View File

@@ -0,0 +1,111 @@
<?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", Generator::CHAR_ALPHA | Generator::CHAR_PUNCT),
array("CHAR_LOWER | EASY_TO_READ", "abcdefghijkmnopqrstuvwxyz", Generator::CHAR_LOWER | Generator::EASY_TO_READ),
array("CHAR_DIGITS | EASY_TO_READ", "3479", Generator::CHAR_DIGITS | 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 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,202 @@
<?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 Generator($this->sources, $this->mixer);
}
public function testConstruct()
{
$this->assertTrue($this->generator instanceof 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,63 @@
<?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 Hash();
$this->assertTrue($hash instanceof \RandomLib\Mixer);
}
public function testGetStrength()
{
$strength = new Strength(Strength::MEDIUM);
$actual = Hash::getStrength();
$this->assertEquals($actual, $strength);
}
public function testTest()
{
$actual = Hash::test();
$this->assertTrue($actual);
}
/**
* @dataProvider provideMix
*/
public function testMix($parts, $result)
{
$mixer = new Hash('md5');
$actual = $mixer->mix($parts);
$this->assertEquals($result, bin2hex($actual));
}
}

View File

@@ -0,0 +1,69 @@
<?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 McryptRijndael128();
$this->assertTrue($hash instanceof \RandomLib\Mixer);
}
public function testGetStrength()
{
$strength = new Strength(Strength::HIGH);
$actual = McryptRijndael128::getStrength();
$this->assertEquals($actual, $strength);
}
public function testTest()
{
$actual = McryptRijndael128::test();
$this->assertTrue($actual);
}
/**
* @dataProvider provideMix
*/
public function testMix($parts, $result)
{
$mixer = new McryptRijndael128();
$actual = $mixer->mix($parts);
$this->assertEquals($result, bin2hex($actual));
}
}

View File

@@ -0,0 +1,69 @@
<?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 SodiumMixer();
$this->assertTrue($hash instanceof \RandomLib\Mixer);
}
public function testGetStrength()
{
$strength = new Strength(Strength::HIGH);
$actual = SodiumMixer::getStrength();
$this->assertEquals($actual, $strength);
}
public function testTest()
{
$actual = SodiumMixer::test();
$this->assertTrue($actual);
}
/**
* @dataProvider provideMix
*/
public function testMix($parts, $result)
{
$mixer = new SodiumMixer();
$actual = $mixer->mix($parts);
$this->assertEquals($result, bin2hex($actual));
}
}

View File

@@ -0,0 +1,67 @@
<?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,21 @@
<?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 AbstractSourceTest
{
protected static function getExpectedStrength()
{
return new Strength(Strength::MEDIUM);
}
}

View File

@@ -0,0 +1,25 @@
<?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 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,33 @@
<?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 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,25 @@
<?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 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,88 @@
<?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 = 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 Sodium();
$stub = $rand->generate($length);
$this->assertEquals($length, strlen($stub));
$this->assertNotEquals($not, $stub);
}
/**
* @dataProvider provideGenerate
*/
public function testGenerateWithoutLibsodium($length, $not)
{
$rand = new 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 Sodium();
$stub = $rand->generate(0);
$this->assertEquals(0, strlen($stub));
$this->assertEquals('', $stub);
}
public function testGenerateWithZeroLengthWithoutLibsodium()
{
$rand = new Sodium(false);
$stub = $rand->generate(0);
$this->assertEquals(0, strlen($stub));
$this->assertEquals('', $stub);
}
}

View File

@@ -0,0 +1,21 @@
<?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 AbstractSourceTest
{
protected static function getExpectedStrength()
{
return new Strength(Strength::HIGH);
}
}

View File

@@ -0,0 +1,21 @@
<?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 AbstractSourceTest
{
protected static function getExpectedStrength()
{
return new Strength(Strength::LOW);
}
}

View File

@@ -0,0 +1,168 @@
<?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@@
*/
use RandomLib\Generator;
use RandomLibTest\Mocks\Random\Mixer;
use RandomLibTest\Mocks\Random\Source;
class Vectors_Random_GeneratorTest extends PHPUnit_Framework_TestCase
{
public static function provideGenerateInt()
{
return array(
// First, lets test each offset based range
array(0, 7),
array(0, 15),
array(0, 31),
array(0, 63),
array(0, 127),
array(0, 255),
array(0, 511),
array(0, 1023),
// Let's try a range not starting at 0
array(8, 15),
// Let's try a range with a negative number
array(-18, -11),
// Let's try a non-power-of-2 range
array(10, 100),
// Finally, let's try two large numbers
array(100000, 100007),
array(100000000, 100002047),
// Now, let's force a few loops by setting a valid offset
array(0, 5, 2),
array(0, 9, 5),
array(0, 27, 4),
);
}
public static function provideGenerators()
{
$factory = new \RandomLib\Factory();
$generator = $factory->getLowStrengthGenerator();
$sources = $generator->getSources();
$ret = array();
$ret[] = array(new Generator($sources, new \RandomLib\Mixer\Hash()), 10000, 'hash');
return $ret;
}
/**
* This test asserts that the algorithm that generates the integers does not
* actually introduce any bias into the generated numbers. If this test
* passes, the generated integers from the generator will be as unbiased as
* the sources that provide the data.
*
* @dataProvider provideGenerateInt
*/
public function testGenerateInt($min, $max, $offset = 0)
{
$generator = $this->getGenerator($max - $min + $offset);
for ($i = $max; $i >= $min; $i--) {
$this->assertEquals($i, $generator->generateInt($min, $max));
}
}
/**
* This generator generates two bytes at a time, and uses each 8 bit segment of
* the generated byte as a coordinate on a grid (so 01011010 would be the
* coordinate (0101, 1010) or (5, 10). These are used as inputs to a MonteCarlo
* algorithm for the integral of y=x over a 15x15 grid. The expected answer is
* 1/2 * 15 * 15 (or 1/2 * base * height, since the result is a triangle).
* Therefore, if we get an answer close to that, we know the generator is good.
*
* Now, since the area under the line should be equal to the area above the line.
* Therefore, the ratio of the two areas should be equal. This way, we can avoid
* computing total to figure out the areas.
*
* I have set the bounds on the test to be 80% and 120%. Meaning that I will
* consider the test valid and unbiased if the number of random elements that
* fall under (inside) of the line and the number that fall outside of the line
* are at most 20% apart.
*
* Since testing randomness is not reliable or repeatable, I will only fail the
* test in two different scenarios. The first is if after the iterations the
* outside or the inside is 0. The chances of that happening are so low that
* if it happens, it's relatively safe to assume that something bad happened. The
* second scenario happens when the ratio is outside of the 20% tolerance. If
* that happens, I will re-run the entire test. If that test is outside of the 20%
* tolerance, then the test will fail
*
*
* @dataProvider provideGenerators
*/
public function testGenerate(\RandomLib\Generator $generator, $times)
{
$ratio = $this->doTestGenerate($generator, $times);
if ($ratio < 0.8 || $ratio > 1.2) {
$ratio2 = $this->doTestGenerate($generator, $times);
if ($ratio2 > 1.2 || $ratio2 < 0.8) {
$this->fail(
sprintf(
'The test failed multiple runs with final ratios %f and %f',
$ratio,
$ratio2
)
);
}
}
}
protected function doTestGenerate(\RandomLib\Generator $generator, $times)
{
$inside = 0;
$outside = 0;
$on = 0;
for ($i = 0; $i < $times; $i++) {
$byte = $generator->generate(2);
$byte = unpack('n', $byte);
$byte = array_shift($byte);
$xCoord = ($byte >> 8);
$yCoord = ($byte & 0xFF);
if ($xCoord < $yCoord) {
$outside++;
} elseif ($xCoord == $yCoord) {
$on++;
} else {
$inside++;
}
}
$this->assertGreaterThan(0, $outside, 'Outside Is 0');
$this->assertGreaterThan(0, $inside, 'Inside Is 0');
$ratio = $inside / $outside;
return $ratio;
}
public function getGenerator($random)
{
$source1 = new Source(array(
'generate' => function ($size) use (&$random) {
$ret = pack('N', $random);
$random--;
return substr($ret, -1 * $size);
},
));
$sources = array($source1);
$mixer = new Mixer(array(
'mix'=> function (array $sources) {
if (empty($sources)) {
return '';
}
return array_pop($sources);
},
));
return new Generator($sources, $mixer);
}
}

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@@
*/
/**
* Bootstrap the library. This registers a simple autoloader for autoloading
* classes
*
* If you are using this library inside of another that uses a similar
* autoloading system, you can use that autoloader instead of this file.
*
* PHP version 5.3
*
* @category PHPPasswordLib
* @package test
*
* @author Anthony Ferrara <ircmaxell@ircmaxell.com>
* @copyright 2011 The Authors
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @license http://www.gnu.org/licenses/lgpl-2.1.html LGPL v 2.1
*/
namespace RandomLibTest;
ini_set('memory_limit', '1G');
/**
* The simple autoloader for the PasswordLibTest libraries.
*
* This does not use the PRS-0 standards due to the namespace prefix and directory
* structure
*
* @param string $class The class name to load
*
* @return void
*/
spl_autoload_register(function ($class) {
$nslen = strlen(__NAMESPACE__);
if (substr($class, 0, $nslen) != __NAMESPACE__) {
//Only autoload libraries from this package
return;
}
$path = substr(str_replace('\\', '/', $class), $nslen);
$path = __DIR__ . $path . '.php';
if (file_exists($path)) {
require $path;
}
});
define('PATH_ROOT', dirname(__DIR__));
require_once dirname(__DIR__) . '/vendor/autoload.php';