|
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
|
|
|
class SodiumTest extends \PHPUnit_Framework_TestCase |
|
16
|
|
|
{ |
|
17
|
|
View Code Duplication |
public static function provideGenerate() |
|
18
|
|
|
{ |
|
19
|
|
|
$data = array(); |
|
20
|
|
|
for ($i = 1; $i < 100; $i += 5) { |
|
21
|
|
|
$not = str_repeat(chr(0), $i); |
|
22
|
|
|
$data[] = array($i, $not); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
return $data; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
public function testGetStrength() |
|
30
|
|
|
{ |
|
31
|
|
|
$strength = new Strength(Strength::HIGH); |
|
32
|
|
|
$actual = Sodium::getStrength(); |
|
33
|
|
|
$this->assertEquals($actual, $strength); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @dataProvider provideGenerate |
|
38
|
|
|
*/ |
|
39
|
|
|
public function testGenerate($length, $not) |
|
40
|
|
|
{ |
|
41
|
|
|
if (!extension_loaded('libsodium')) { |
|
42
|
|
|
$this->markTestSkipped('The libsodium extension is not loaded'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$rand = new Sodium(); |
|
46
|
|
|
$stub = $rand->generate($length); |
|
47
|
|
|
$this->assertEquals($length, strlen($stub)); |
|
48
|
|
|
$this->assertNotEquals($not, $stub); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @dataProvider provideGenerate |
|
53
|
|
|
*/ |
|
54
|
|
|
public function testGenerateWithoutLibsodium($length, $not) |
|
55
|
|
|
{ |
|
56
|
|
|
$rand = new Sodium(false); |
|
57
|
|
|
$stub = $rand->generate($length); |
|
58
|
|
|
$this->assertEquals($length, strlen($stub)); |
|
59
|
|
|
$this->assertEquals($not, $stub); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testGenerateWithZeroLength() |
|
63
|
|
|
{ |
|
64
|
|
|
if (!extension_loaded('libsodium')) { |
|
65
|
|
|
$this->markTestSkipped('The libsodium extension is not loaded'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$rand = new Sodium(); |
|
69
|
|
|
$stub = $rand->generate(0); |
|
70
|
|
|
$this->assertEquals(0, strlen($stub)); |
|
71
|
|
|
$this->assertEquals('', $stub); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function testGenerateWithZeroLengthWithoutLibsodium() |
|
75
|
|
|
{ |
|
76
|
|
|
$rand = new Sodium(false); |
|
77
|
|
|
$stub = $rand->generate(0); |
|
78
|
|
|
$this->assertEquals(0, strlen($stub)); |
|
79
|
|
|
$this->assertEquals('', $stub); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|