|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* The RandomLib library for securely generating random numbers and strings in PHP |
|
5
|
|
|
* |
|
6
|
|
|
* @author Anthony Ferrara <[email protected]> |
|
7
|
|
|
* @copyright 2011 The Authors |
|
8
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
|
9
|
|
|
* @version Build @@version@@ |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace RandomLib\Source; |
|
12
|
|
|
|
|
13
|
|
|
use SecurityLib\Strength; |
|
14
|
|
|
|
|
15
|
|
|
abstract class AbstractSourceTest extends \PHPUnit_Framework_TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
public function setUp() |
|
18
|
|
|
{ |
|
19
|
|
|
$class = static::getTestedClass(); |
|
20
|
|
|
|
|
21
|
|
|
if (!$class::isSupported()) { |
|
22
|
|
|
$this->markTestSkipped(); |
|
23
|
|
|
} |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
protected static function getTestedClass() |
|
27
|
|
|
{ |
|
28
|
|
|
return preg_replace('/Test$/', '', get_called_class()); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
protected static function getExpectedStrength() |
|
32
|
|
|
{ |
|
33
|
|
|
return new Strength(Strength::VERYLOW); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
View Code Duplication |
public static function provideGenerate() |
|
37
|
|
|
{ |
|
38
|
|
|
$data = array(); |
|
39
|
|
|
for ($i = 0; $i < 100; $i += 5) { |
|
40
|
|
|
$not = $i > 0 ? str_repeat(chr(0), $i) : chr(0); |
|
41
|
|
|
$data[] = array($i, $not); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return $data; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function testGetStrength() |
|
48
|
|
|
{ |
|
49
|
|
|
$class = static::getTestedClass(); |
|
50
|
|
|
$strength = static::getExpectedStrength(); |
|
51
|
|
|
$actual = $class::getStrength(); |
|
52
|
|
|
$this->assertEquals($actual, $strength); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @dataProvider provideGenerate |
|
57
|
|
|
* @group slow |
|
58
|
|
|
*/ |
|
59
|
|
|
public function testGenerate($length, $not) |
|
60
|
|
|
{ |
|
61
|
|
|
$class = static::getTestedClass(); |
|
62
|
|
|
$rand = new $class(); |
|
63
|
|
|
$stub = $rand->generate($length); |
|
64
|
|
|
$this->assertEquals($length, strlen($stub)); |
|
65
|
|
|
$this->assertNotEquals($not, $stub); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|