| 1 | <?php |
||
| 9 | class URandomTest extends \PHPUnit_Framework_TestCase { |
||
| 10 | |||
| 11 | public static function provideGenerate() { |
||
| 12 | $data = array(); |
||
| 13 | for ($i = 0; $i < 10; $i += 5) { |
||
| 14 | $not = $i > 0 ? str_repeat(chr(0), $i) : chr(0); |
||
| 15 | $data[] = array($i, $not); |
||
| 16 | } |
||
| 17 | return $data; |
||
| 18 | } |
||
| 19 | |||
| 20 | /** |
||
| 21 | */ |
||
| 22 | public function testGetStrength() { |
||
| 23 | $strength = new Strength(Strength::MEDIUM); |
||
| 24 | $actual = URandom::getStrength(); |
||
| 25 | $this->assertEquals($actual, $strength); |
||
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @dataProvider provideGenerate |
||
| 30 | * @group slow |
||
| 31 | */ |
||
| 32 | public function testGenerate($length, $not) { |
||
| 33 | $rand = new URandom; |
||
| 34 | $stub = $rand->generate($length); |
||
| 35 | $this->assertEquals($length, strlen($stub)); |
||
| 36 | if (file_exists('/dev/urandom')) { |
||
| 37 | $this->assertNotEquals($not, $stub); |
||
| 38 | } else { |
||
| 39 | $this->assertEquals(str_repeat(chr(0), $length), $stub); |
||
| 40 | } |
||
| 41 | } |
||
| 42 | |||
| 43 | } |
||
| 44 |