2014-08-26 17:02:40 +00: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.
|
|
|
|
*/
|
|
|
|
|
2014-09-03 11:51:44 +00:00
|
|
|
use \OC\Security\SecureRandom;
|
|
|
|
|
2014-11-10 22:30:38 +00:00
|
|
|
class SecureRandomTest extends \Test\TestCase {
|
2014-08-26 17:02:40 +00:00
|
|
|
|
|
|
|
public function stringGenerationProvider() {
|
|
|
|
return array(
|
|
|
|
array(0, 0),
|
|
|
|
array(1, 1),
|
|
|
|
array(128, 128),
|
|
|
|
array(256, 256),
|
|
|
|
array(1024, 1024),
|
|
|
|
array(2048, 2048),
|
|
|
|
array(64000, 64000),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-09-03 12:13:12 +00:00
|
|
|
public static function charCombinations() {
|
|
|
|
return array(
|
|
|
|
array('CHAR_LOWER', '[a-z]'),
|
|
|
|
array('CHAR_UPPER', '[A-Z]'),
|
|
|
|
array('CHAR_DIGITS', '[0-9]'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-09-03 11:51:44 +00:00
|
|
|
/** @var SecureRandom */
|
|
|
|
protected $rng;
|
|
|
|
|
|
|
|
protected function setUp() {
|
2014-11-10 22:30:38 +00:00
|
|
|
parent::setUp();
|
2014-09-03 11:51:44 +00:00
|
|
|
$this->rng = new \OC\Security\SecureRandom();
|
|
|
|
}
|
|
|
|
|
2014-08-26 17:02:40 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider stringGenerationProvider
|
|
|
|
*/
|
|
|
|
function testGetLowStrengthGeneratorLength($length, $expectedLength) {
|
2016-01-11 18:59:15 +00:00
|
|
|
$generator = $this->rng;
|
2014-08-26 17:02:40 +00:00
|
|
|
|
|
|
|
$this->assertEquals($expectedLength, strlen($generator->generate($length)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider stringGenerationProvider
|
|
|
|
*/
|
|
|
|
function testMediumLowStrengthGeneratorLength($length, $expectedLength) {
|
2016-01-11 19:05:30 +00:00
|
|
|
$generator = $this->rng;
|
2014-08-26 17:02:40 +00:00
|
|
|
|
|
|
|
$this->assertEquals($expectedLength, strlen($generator->generate($length)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-12-11 05:17:47 +00:00
|
|
|
* @dataProvider stringGenerationProvider
|
2014-08-26 17:02:40 +00:00
|
|
|
*/
|
2015-12-11 05:17:47 +00:00
|
|
|
function testUninitializedGenerate($length, $expectedLength) {
|
|
|
|
$this->assertEquals($expectedLength, strlen($this->rng->generate($length)));
|
2014-08-26 17:02:40 +00:00
|
|
|
}
|
2014-09-03 12:13:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider charCombinations
|
|
|
|
*/
|
|
|
|
public function testScheme($charName, $chars) {
|
2016-01-11 19:05:30 +00:00
|
|
|
$generator = $this->rng;
|
2014-09-03 12:13:12 +00:00
|
|
|
$scheme = constant('OCP\Security\ISecureRandom::' . $charName);
|
|
|
|
$randomString = $generator->generate(100, $scheme);
|
|
|
|
$matchesRegex = preg_match('/^'.$chars.'+$/', $randomString);
|
|
|
|
$this->assertSame(1, $matchesRegex);
|
|
|
|
}
|
2014-08-26 17:02:40 +00:00
|
|
|
}
|