2014-08-26 21:02:40 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2014 Lukas Reschke <lukas@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
2016-05-19 10:02:58 +03:00
|
|
|
namespace Test\Security;
|
|
|
|
|
2019-11-22 22:52:10 +03:00
|
|
|
use OC\Security\SecureRandom;
|
2014-09-03 15:51:44 +04:00
|
|
|
|
2014-11-11 01:30:38 +03:00
|
|
|
class SecureRandomTest extends \Test\TestCase {
|
2014-08-26 21:02:40 +04:00
|
|
|
public function stringGenerationProvider() {
|
2020-03-26 11:30:18 +03:00
|
|
|
return [
|
|
|
|
[0, 0],
|
|
|
|
[1, 1],
|
|
|
|
[128, 128],
|
|
|
|
[256, 256],
|
|
|
|
[1024, 1024],
|
|
|
|
[2048, 2048],
|
|
|
|
[64000, 64000],
|
|
|
|
];
|
2014-08-26 21:02:40 +04:00
|
|
|
}
|
|
|
|
|
2014-09-03 16:13:12 +04:00
|
|
|
public static function charCombinations() {
|
2020-03-26 11:30:18 +03:00
|
|
|
return [
|
|
|
|
['CHAR_LOWER', '[a-z]'],
|
|
|
|
['CHAR_UPPER', '[A-Z]'],
|
|
|
|
['CHAR_DIGITS', '[0-9]'],
|
|
|
|
];
|
2014-09-03 16:13:12 +04:00
|
|
|
}
|
|
|
|
|
2014-09-03 15:51:44 +04:00
|
|
|
/** @var SecureRandom */
|
|
|
|
protected $rng;
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2014-11-11 01:30:38 +03:00
|
|
|
parent::setUp();
|
2014-09-03 15:51:44 +04:00
|
|
|
$this->rng = new \OC\Security\SecureRandom();
|
|
|
|
}
|
|
|
|
|
2014-08-26 21:02:40 +04:00
|
|
|
/**
|
|
|
|
* @dataProvider stringGenerationProvider
|
|
|
|
*/
|
2020-04-10 17:51:06 +03:00
|
|
|
public function testGetLowStrengthGeneratorLength($length, $expectedLength) {
|
2016-01-11 21:59:15 +03:00
|
|
|
$generator = $this->rng;
|
2014-08-26 21:02:40 +04:00
|
|
|
|
|
|
|
$this->assertEquals($expectedLength, strlen($generator->generate($length)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider stringGenerationProvider
|
|
|
|
*/
|
2020-04-10 17:51:06 +03:00
|
|
|
public function testMediumLowStrengthGeneratorLength($length, $expectedLength) {
|
2016-01-11 22:05:30 +03:00
|
|
|
$generator = $this->rng;
|
2014-08-26 21:02:40 +04:00
|
|
|
|
|
|
|
$this->assertEquals($expectedLength, strlen($generator->generate($length)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-12-11 08:17:47 +03:00
|
|
|
* @dataProvider stringGenerationProvider
|
2014-08-26 21:02:40 +04:00
|
|
|
*/
|
2020-04-10 17:51:06 +03:00
|
|
|
public function testUninitializedGenerate($length, $expectedLength) {
|
2015-12-11 08:17:47 +03:00
|
|
|
$this->assertEquals($expectedLength, strlen($this->rng->generate($length)));
|
2014-08-26 21:02:40 +04:00
|
|
|
}
|
2014-09-03 16:13:12 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider charCombinations
|
|
|
|
*/
|
|
|
|
public function testScheme($charName, $chars) {
|
2016-01-11 22:05:30 +03:00
|
|
|
$generator = $this->rng;
|
2014-09-03 16:13:12 +04:00
|
|
|
$scheme = constant('OCP\Security\ISecureRandom::' . $charName);
|
|
|
|
$randomString = $generator->generate(100, $scheme);
|
|
|
|
$matchesRegex = preg_match('/^'.$chars.'+$/', $randomString);
|
|
|
|
$this->assertSame(1, $matchesRegex);
|
|
|
|
}
|
2014-08-26 21:02:40 +04:00
|
|
|
}
|