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,41 @@
<?php
/**
* 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 PasswordLibTest\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,31 @@
<?php
/**
* The interface that all hash implementations must implement
*
* PHP version 5.3
*
* @category PHPSecurityLib
* @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 SecurityLibTest\Mocks;
/**
* The interface that all hash implementations must implement
*
* @category PHPSecurityLib
* @package Hash
* @author Anthony Ferrara <ircmaxell@ircmaxell.com>
*/
class Enum extends \SecurityLib\Enum {
const Value1 = 1;
const Value2 = 2;
const Value3 = 3;
const Value4 = 4;
}

View File

@@ -0,0 +1,49 @@
<?php
/**
* The interface that all hash implementations must implement
*
* PHP version 5.3
*
* @category PHPSecurityLib
* @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 SecurityLibTest\Mocks;
/**
* The interface that all hash implementations must implement
*
* @category PHPSecurityLib
* @package Hash
* @author Anthony Ferrara <ircmaxell@ircmaxell.com>
*/
class Factory extends \SecurityLib\AbstractFactory {
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 registerType($a1, $a2, $a3, $a4, $a5 = false) {
return parent::registerType($a1, $a2, $a3, $a4, $a5);
}
public function loadFiles($dir, $name, $method) {
return parent::loadFiles($dir, $name, $method);
}
}

View File

@@ -0,0 +1,29 @@
<?php
/**
* The interface that all hash implementations must implement
*
* PHP version 5.3
*
* @category PHPSecurityLib
* @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 SecurityLibTest\Mocks;
/**
* The interface that all hash implementations must implement
*
* @category PHPSecurityLib
* @package Hash
* @author Anthony Ferrara <ircmaxell@ircmaxell.com>
*/
class Strength extends \SecurityLib\Strength {
const MEDIUMLOW = 4;
const SUPERHIGH = 999;
}

View File

@@ -0,0 +1,67 @@
<?php
use SecurityLibTest\Mocks\Factory;
use org\bovigo\vfs\vfsStream;
class Unit_Core_AbstractFactoryTest extends PHPUnit_Framework_TestCase {
protected function setUp() {
$root = vfsStream::setup('SecurityLibTest');
//Setup Folders
$core = vfsStream::newDirectory('Core')->at($root);
$af = vfsStream::newDirectory('AbstractFactory')->at($core);
// Create Files
vfsStream::newFile('test.php')->at($af);
vfsStream::newFile('Some234Foo234Bar98Name.php')->at($af);
vfsStream::newFile('Invalid.csv')->at($af);
vfsStream::newFile('badlocation.php')->at($core);
}
/**
* @covers SecurityLib\AbstractFactory::registerType
*/
public function testRegisterType() {
$factory = new Factory;
$factory->registerType('test', 'iteratoraggregate', 'foo', 'ArrayObject', false);
}
/**
* @covers SecurityLib\AbstractFactory::registerType
* @expectedException InvalidArgumentException
*/
public function testRegisterTypeFail() {
$factory = new Factory;
$factory->registerType('test', 'iterator', 'foo', 'ArrayObject', false);
}
/**
* @covers SecurityLib\AbstractFactory::registerType
*/
public function testRegisterTypeInstantiate() {
$factory = new Factory;
$factory->registerType('test', 'iteratoraggregate', 'foo', 'ArrayObject', true);
}
public function testLoadFiles() {
$dir = vfsStream::url('SecurityLibTest/Core/AbstractFactory');
$result = array();
$callback = function($name, $class) use (&$result) {
$result[$name] = $class;
};
$factory = new Factory();
$factory->loadFiles($dir, 'foo\\', $callback);
$expect = array(
'test' => 'foo\\test',
'Some234Foo234Bar98Name' => 'foo\\Some234Foo234Bar98Name'
);
$this->assertEquals($expect, $result);
}
}

View File

@@ -0,0 +1,69 @@
<?php
use SecurityLib\BaseConverter;
class Unit_Core_BaseConverterTest extends PHPUnit_Framework_TestCase {
public static function provideConvertFromBinary() {
$return = array(
array('', '', ''),
array(chr(0), '012', '0'),
array(chr(9), '01', '1001'),
array(chr(1) . chr(2) . chr(3), '0123456789', '66051'),
);
return $return;
}
public static function provideConvertToFromBinary() {
$return = array();
$str = chr(1) . chr(0);
for ($i = 2; $i < 256; $i++) {
$str .= chr($i);
$return[] = array($str, strrev($str));
}
return $return;
}
/**
* @covers SecurityLib\BaseConverter::convertFromBinary
* @covers SecurityLib\BaseConverter::baseConvert
* @dataProvider provideConvertFromBinary
*/
public function testConvertFromBinary($from, $to, $expect) {
$result = BaseConverter::convertFromBinary($from, $to);
$this->assertEquals($expect, $result);
}
/**
* @covers SecurityLib\BaseConverter::convertToBinary
* @covers SecurityLib\BaseConverter::baseConvert
* @dataProvider provideConvertFromBinary
*/
public function testConvertToBinary($expect, $from, $str) {
$result = BaseConverter::convertToBinary($str, $from);
$result = ltrim($result, chr(0));
$expect = ltrim($expect, chr(0));
$this->assertEquals($expect, $result);
}
/**
* @covers SecurityLib\BaseConverter::convertToBinary
* @covers SecurityLib\BaseConverter::convertFromBinary
* @covers SecurityLib\BaseConverter::baseConvert
* @dataProvider provideConvertToFromBinary
*/
public function testConvertToAndFromBinary($str, $from) {
return false;
$result1 = BaseConverter::convertFromBinary($str, $from);
$result = BaseConverter::convertToBinary($result1, $from);
$this->assertEquals($str, $result);
}
/**
* @covers SecurityLib\BaseConverter::baseConvert
* @expectedException InvalidArgumentException
*/
public function testBaseConvertFailure() {
BaseConverter::baseConvert(array(1), 1, 1);
}
}

View File

@@ -0,0 +1,30 @@
<?php
require_once __DIR__ . '/../BigMathTest.php';
class Unit_Core_BigMath_BCMathTest extends Unit_Core_BigMathTest {
protected static $mathImplementations = array();
protected function setUp() {
if (!extension_loaded('bcmath')) {
$this->markTestSkipped('BCMath is not loaded');
}
}
/**
* @dataProvider provideAddTest
*/
public function testAdd($left, $right, $expected) {
$obj = new \SecurityLib\BigMath\BCMath;
$this->assertEquals($expected, $obj->add($left, $right));
}
/**
* @dataProvider provideSubtractTest
*/
public function testSubtract($left, $right, $expected) {
$obj = new \SecurityLib\BigMath\BCMath;
$this->assertEquals($expected, $obj->subtract($left, $right));
}
}

View File

@@ -0,0 +1,30 @@
<?php
require_once __DIR__ . '/../BigMathTest.php';
class Unit_Core_BigMath_GMPTest extends Unit_Core_BigMathTest {
protected static $mathImplementations = array();
protected function setUp() {
if (!extension_loaded('gmp')) {
$this->markTestSkipped('BCMath is not loaded');
}
}
/**
* @dataProvider provideAddTest
*/
public function testAdd($left, $right, $expected) {
$obj = new \SecurityLib\BigMath\GMP;
$this->assertEquals($expected, $obj->add($left, $right));
}
/**
* @dataProvider provideSubtractTest
*/
public function testSubtract($left, $right, $expected) {
$obj = new \SecurityLib\BigMath\GMP;
$this->assertEquals($expected, $obj->subtract($left, $right));
}
}

View File

@@ -0,0 +1,24 @@
<?php
require_once __DIR__ . '/../BigMathTest.php';
class Unit_Core_BigMath_PHPMathTest extends Unit_Core_BigMathTest {
protected static $mathImplementations = array();
/**
* @dataProvider provideAddTest
*/
public function testAdd($left, $right, $expected) {
$obj = new \SecurityLib\BigMath\PHPMath;
$this->assertEquals($expected, $obj->add($left, $right));
}
/**
* @dataProvider provideSubtractTest
*/
public function testSubtract($left, $right, $expected) {
$obj = new \SecurityLib\BigMath\PHPMath;
$this->assertEquals($expected, $obj->subtract($left, $right));
}
}

View File

@@ -0,0 +1,46 @@
<?php
class Unit_Core_BigMathTest extends PHPUnit_Framework_TestCase {
protected static $mathImplementations = array();
public static function provideAddTest() {
$ret = array(
array('1', '1', '2'),
array('11', '11', '22'),
array('1111111111', '1111111111', '2222222222'),
array('555', '555', '1110'),
array('-10', '10', '0'),
array('10', '-10', '0'),
array('-10', '-10', '-20'),
array('0', '0', '0'),
array('5', '0', '5'),
);
return $ret;
}
public static function provideSubtractTest() {
return array(
array('1', '1', '0'),
array('6', '3', '3'),
array('200', '250', '-50'),
array('10', '300', '-290'),
array('-1', '-1', '0'),
array('-5', '5', '-10'),
array('5', '-5', '10'),
array('0', '0', '0'),
array('5', '0', '5'),
);
}
public function testCreateFromServerConfiguration() {
$instance = \SecurityLib\BigMath::createFromServerConfiguration();
if (extension_loaded('bcmath')) {
$this->assertEquals('SecurityLib\\BigMath\\BCMath', get_class($instance));
} elseif (extension_loaded('gmp')) {
$this->assertEquals('SecurityLib\\BigMath\\GMP', get_class($instance));
} else {
$this->assertEquals('SecurityLib\\BigMath\\PHPMath', get_class($instance));
}
}
}

View File

@@ -0,0 +1,61 @@
<?php
use SecurityLibTest\Mocks\Enum;
class Unit_Core_EnumTest extends PHPUnit_Framework_TestCase {
public static function provideTestCompare() {
return array(
array(new Enum(Enum::Value1), new Enum(Enum::Value1), 0),
array(new Enum(Enum::Value2), new Enum(Enum::Value1), -1),
array(new Enum(Enum::Value1), new Enum(Enum::Value2), 1),
);
}
/**
* @expectedException UnexpectedValueException
*/
public function testConstructFail() {
$obj = new Enum();
}
public function testConstruct() {
$obj = new Enum(Enum::Value3);
$this->assertTrue($obj instanceof \SecurityLib\Enum);
}
public function testToString() {
$obj = new Enum(Enum::Value3);
$this->assertEquals('3', (string) $obj);
}
/**
* @covers SecurityLib\Core\Enum::compare
* @dataProvider provideTestCompare
*/
public function testCompare(Enum $from, Enum $to, $expected) {
$this->assertEquals($expected, $from->compare($to));
}
public function testGetConstList() {
$obj = new Enum(Enum::Value3);
$const = $obj->getConstList();
$this->assertEquals(array(
'Value1' => 1,
'Value2' => 2,
'Value3' => 3,
'Value4' => 4,
), $const);
}
public function testGetConstListWithDefault() {
$obj = new Enum(Enum::Value3);
$const = $obj->getConstList(true);
$this->assertEquals(array(
'__DEFAULT' => null,
'Value1' => 1,
'Value2' => 2,
'Value3' => 3,
'Value4' => 4,
), $const);
}
}

View File

@@ -0,0 +1,35 @@
<?php
use SecurityLib\Strength;
class Unit_Core_StrengthTest extends PHPUnit_Framework_TestCase {
public function testConstruct() {
$obj = new Strength(Strength::LOW);
$this->assertTrue($obj instanceof \SecurityLib\Strength);
$this->assertTrue($obj instanceof \SecurityLib\Enum);
}
public function testGetConstList() {
$obj = new Strength();
$const = $obj->getConstList();
$this->assertEquals(array(
'VERYLOW' => 1,
'LOW' => 3,
'MEDIUM' => 5,
'HIGH' => 7,
), $const);
}
public function testGetConstListWithDefault() {
$obj = new Strength();
$const = $obj->getConstList(true);
$this->assertEquals(array(
'__DEFAULT' => 1,
'VERYLOW' => 1,
'LOW' => 3,
'MEDIUM' => 5,
'HIGH' => 7,
), $const);
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace SecurityLib;
/**
* Highly recommended that his be run with and without setting
* mbstring.func_overload = 7
* in your php.ini file to verify its correctness.
*/
class UtilTest extends \PHPUnit_Framework_TestCase {
public function testSafeStrlen() {
$this->assertEquals(Util::safeStrlen("\x03\x3f"), 2);
}
public function testSafeSubstr() {
$a = "abcdefg\x03\x3fhijk";
$b = "\x03\x3f";
$this->assertEquals(Util::safeSubstr($a, 7, 2), $b);
}
}

View File

@@ -0,0 +1,49 @@
<?php
/**
* 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 SecurityLibTest;
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';