Permalink
Browse files

Added feedbacks on #36

  • Loading branch information...
ezimuel committed Jul 10, 2018
1 parent 8a4bedd commit 54ecd562c95b0321e04f21f8ebcc8446b62a9c6f
Showing with 26 additions and 10 deletions.
  1. +5 −0 src/Rand.php
  2. +10 −7 test/RandTest.php
  3. +11 −3 test/TestAsset/random_bytes.php
View
@@ -12,6 +12,11 @@
*/
abstract class Rand
{
/**
* @deprecated No longer used internally
*/
protected static $generator = null;
/**
* Generate random bytes using different approaches
* If PHP 7 is running we use the random_bytes() function
View
@@ -8,15 +8,18 @@
use Exception;
use PHPUnit\Framework\TestCase;
use Zend\Math\Exception\DomainException;
use Zend\Math\Exception\InvalidArgumentException;
use Zend\Math\Exception\RuntimeException;
use Zend\Math\Rand;
class RandTest extends TestCase
{
public static $custom_random_bytes = false;
public static $customRandomBytes = false;
public function tearDown()
{
self::$custom_random_bytes = false;
self::$customRandomBytes = false;
}
public static function provideRandInt()
@@ -38,29 +41,29 @@ public function testRandBytes()
public function testWrongRandBytesParam()
{
$this->expectException('Zend\Math\Exception\InvalidArgumentException');
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid parameter provided to getBytes(length)');
Rand::getBytes('foo');
}
public function testZeroRandBytesParam()
{
$this->expectException('Zend\Math\Exception\DomainException');
$this->expectException(DomainException::class);
$this->expectExceptionMessage('The length must be a positive number in getBytes(length)');
Rand::getBytes(0);
}
public function testNegativeRandBytesParam()
{
$this->expectException('Zend\Math\Exception\DomainException');
$this->expectException(DomainException::class);
$this->expectExceptionMessage('The length must be a positive number in getBytes(length)');
Rand::getBytes(-1);
}
public function testUnsupportedPlatform()
{
self::$custom_random_bytes = true;
$this->expectException('Zend\Math\Exception\RuntimeException');
self::$customRandomBytes = true;
$this->expectException(RuntimeException::class);
$rand = Rand::getBytes(2);
}
@@ -1,17 +1,25 @@
<?php
/**
* @link http://github.com/zendframework/zend-math for the canonical source repository
* @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Math;
use Exception;
use ZendTest\Math\RandTest;
/**
* Generate random bytes with $length size or throw an Exception,
* to test a PHP platform without secure random number generator installed
*
* @param int $length
* @return string
*/
function random_bytes($length)
{
if (RandTest::$custom_random_bytes) {
throw new \Exception("Random is not supported");
if (RandTest::$customRandomBytes) {
throw new Exception("Random is not supported");
}
return \random_bytes($length);
}

0 comments on commit 54ecd56

Please sign in to comment.