-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Since open_basedir no longer falls through to OpenSSL, expect a failure.
- Loading branch information
1 parent
1c435ec
commit 4663262
Showing
4 changed files
with
279 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
class RandomBytesTest extends PHPUnit_Framework_TestCase | ||
{ | ||
const NO_BASEDIR = 'There is no suitable CSPRNG installed on your system'; | ||
|
||
public function testFuncExists() | ||
{ | ||
$this->assertTrue(function_exists('random_bytes')); | ||
} | ||
|
||
public function testInvalidParams() | ||
{ | ||
try { | ||
$bytes = random_bytes('good morning'); | ||
$this->fail("random_bytes() should accept only an integer"); | ||
} catch (TypeError $ex) { | ||
$this->assertTrue(true); | ||
} catch (Error $ex) { | ||
$this->assertTrue(true); | ||
} catch (Exception $ex) { | ||
$this->assertTrue(true); | ||
if ($ex->getMessage() === self::NO_BASEDIR) { | ||
return; | ||
} | ||
} | ||
|
||
try { | ||
$bytes = random_bytes(array(12)); | ||
$this->fail("random_bytes() should accept only an integer"); | ||
} catch (TypeError $ex) { | ||
$this->assertTrue(true); | ||
} catch (Error $ex) { | ||
$this->assertTrue(true); | ||
} catch (Exception $ex) { | ||
$this->assertTrue(true); | ||
} | ||
|
||
// This should succeed: | ||
try { | ||
$bytes = random_bytes('123456'); | ||
} catch (Exception $ex) { | ||
$this->assertEquals( | ||
$ex->getMessage(), | ||
self::NO_BASEDIR | ||
); | ||
} | ||
} | ||
|
||
public function testOutput() | ||
{ | ||
try { | ||
$bytes = array( | ||
random_bytes(12), | ||
random_bytes(64), | ||
random_bytes(64), | ||
random_bytes(1.5) | ||
); | ||
} catch (Exception $ex) { | ||
$this->assertEquals( | ||
$ex->getMessage(), | ||
self::NO_BASEDIR | ||
); | ||
return; | ||
} | ||
|
||
$this->assertTrue( | ||
strlen(bin2hex($bytes[0])) === 24 | ||
); | ||
$this->assertTrue( | ||
strlen(bin2hex($bytes[3])) === 2 | ||
); | ||
|
||
// This should never generate identical byte strings | ||
$this->assertFalse( | ||
$bytes[1] === $bytes[2] | ||
); | ||
|
||
try { | ||
$x = random_bytes(~PHP_INT_MAX - 1000000000); | ||
$this->fail("Integer overflow (~PHP_INT_MAX - 1000000000)."); | ||
} catch (TypeError $ex) { | ||
$this->assertTrue(true); | ||
} catch (Error $ex) { | ||
$this->assertTrue(true); | ||
} catch (Exception $ex) { | ||
$this->assertTrue(true); | ||
} | ||
|
||
try { | ||
$x = random_bytes(PHP_INT_MAX + 1000000000); | ||
$this->fail("Requesting too many bytes should fail."); | ||
} catch (TypeError $ex) { | ||
$this->assertTrue(true); | ||
} catch (Error $ex) { | ||
$this->assertTrue(true); | ||
} catch (Exception $ex) { | ||
$this->assertTrue(true); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
class RandomIntTest extends PHPUnit_Framework_TestCase | ||
{ | ||
const NO_BASEDIR = 'There is no suitable CSPRNG installed on your system'; | ||
|
||
public function testFuncExists() | ||
{ | ||
$this->assertTrue(function_exists('random_int')); | ||
} | ||
|
||
public function testOutput() | ||
{ | ||
try { | ||
$half_neg_max = (~PHP_INT_MAX / 2); | ||
|
||
$integers = array( | ||
random_int(0, 1000), | ||
random_int(1001,2000), | ||
random_int(-100, -10), | ||
random_int(-1000, 1000), | ||
random_int(~PHP_INT_MAX, PHP_INT_MAX), | ||
random_int("0", "1"), | ||
random_int(0.11111, 0.99999), | ||
random_int($half_neg_max, PHP_INT_MAX), | ||
random_int(0.0, 255.0), | ||
random_int(-4.5, -4.5), | ||
random_int("1337e3","1337e3") | ||
); | ||
|
||
$this->assertFalse($integers[0] === $integers[1]); | ||
$this->assertTrue($integers[0] >= 0 && $integers[0] <= 1000); | ||
$this->assertTrue($integers[1] >= 1001 && $integers[1] <= 2000); | ||
$this->assertTrue($integers[2] >= -100 && $integers[2] <= -10); | ||
$this->assertTrue($integers[3] >= -1000 && $integers[3] <= 1000); | ||
$this->assertTrue($integers[4] >= ~PHP_INT_MAX && $integers[4] <= PHP_INT_MAX); | ||
$this->assertTrue($integers[5] >= 0 && $integers[5] <= 1); | ||
$this->assertTrue($integers[6] === 0); | ||
$this->assertTrue($integers[7] >= $half_neg_max && $integers[7] <= PHP_INT_MAX); | ||
$this->assertTrue($integers[8] >= 0 && $integers[8] <= 255); | ||
$this->assertTrue($integers[9] === -4); | ||
$this->assertTrue($integers[10] === 1337000); | ||
} catch (Exception $ex) { | ||
$this->assertEquals( | ||
$ex->getMessage(), | ||
self::NO_BASEDIR | ||
); | ||
return; | ||
} | ||
|
||
try { | ||
$h = random_int("2147483648", "2147483647"); | ||
$i = random_int("9223372036854775808", "9223372036854775807"); | ||
$this->assertFalse(is_int($i)); | ||
$h = random_int("-2147483648", "2147483647"); | ||
$i = random_int("-9223372036854775808", "9223372036854775807"); | ||
$this->fail("One of these options should have thrown an exception."); | ||
} catch (Error $ex) { | ||
$this->assertTrue($ex instanceof Error); | ||
} catch (Exception $ex) { | ||
$this->assertTrue($ex instanceof Exception); | ||
} | ||
} | ||
|
||
public function testRandomRange() | ||
{ | ||
try { | ||
$try = 64; | ||
$maxLen = strlen(~PHP_INT_MAX); | ||
do { | ||
$rand = random_int(~PHP_INT_MAX, PHP_INT_MAX); | ||
} while (strlen($rand) !== $maxLen && $try--); | ||
|
||
$this->assertGreaterThan(0, $try); | ||
} catch (Exception $ex) { | ||
$this->assertEquals( | ||
$ex->getMessage(), | ||
self::NO_BASEDIR | ||
); | ||
return; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
class UtilityTest extends PHPUnit_Framework_TestCase | ||
{ | ||
public function testStrlen() | ||
{ | ||
if (!function_exists('RandomCompat_strlen')) { | ||
return $this->markTestSkipped( | ||
'We don\' need to test this in PHP 7.' | ||
); | ||
} | ||
$this->assertEquals(RandomCompat_strlen("\xF0\x9D\x92\xB3"), 4); | ||
} | ||
|
||
public function testIntval() | ||
{ | ||
if (!function_exists('RandomCompat_intval')) { | ||
return $this->markTestSkipped( | ||
'We don\' need to test this in PHP 7.' | ||
); | ||
} | ||
// Equals | ||
$this->assertEquals( | ||
abs(RandomCompat_intval(-4.5)), | ||
abs(RandomCompat_intval(4.5)) | ||
); | ||
|
||
// True | ||
$this->assertTrue( | ||
is_int(RandomCompat_intval(PHP_INT_MAX, true)) | ||
); | ||
$this->assertTrue( | ||
is_int(RandomCompat_intval(~PHP_INT_MAX, true)) | ||
); | ||
$this->assertTrue( | ||
is_int(RandomCompat_intval(~PHP_INT_MAX + 1, true)) | ||
); | ||
$this->assertTrue( | ||
is_int(RandomCompat_intval("1337e3", true)) | ||
); | ||
$this->assertTrue( | ||
is_int(RandomCompat_intval("1.", true)) | ||
); | ||
|
||
// False | ||
$this->assertFalse( | ||
is_int(RandomCompat_intval((float) PHP_INT_MAX, true)) | ||
); | ||
$this->assertFalse( | ||
is_int(RandomCompat_intval((float) ~PHP_INT_MAX, true)) | ||
); | ||
$this->assertFalse( | ||
is_int(RandomCompat_intval(PHP_INT_MAX + 1, true)) | ||
); | ||
$this->assertFalse( | ||
is_int(RandomCompat_intval(~PHP_INT_MAX - 1, true)) | ||
); | ||
$this->assertFalse( | ||
is_int(RandomCompat_intval(~PHP_INT_MAX - 0.1, true)) | ||
); | ||
$this->assertFalse( | ||
is_int(RandomCompat_intval(PHP_INT_MAX + 0.1, true)) | ||
); | ||
$this->assertFalse( | ||
is_int(RandomCompat_intval("hello", true)) | ||
); | ||
|
||
if (PHP_INT_SIZE === 8) { | ||
$this->assertFalse( | ||
is_int(RandomCompat_intval("-9223372036854775809", true)) | ||
); | ||
$this->assertTrue( | ||
is_int(RandomCompat_intval("-9223372036854775808", true)) | ||
); | ||
$this->assertFalse( | ||
is_int(RandomCompat_intval("9223372036854775808", true)) | ||
); | ||
$this->assertTrue( | ||
is_int(RandomCompat_intval("9223372036854775807", true)) | ||
); | ||
} else { | ||
$this->assertFalse( | ||
is_int(RandomCompat_intval("2147483648", true)) | ||
); | ||
$this->assertTrue( | ||
is_int(RandomCompat_intval("2147483647", true)) | ||
); | ||
$this->assertFalse( | ||
is_int(RandomCompat_intval("-2147483649", true)) | ||
); | ||
$this->assertTrue( | ||
is_int(RandomCompat_intval("-2147483648", true)) | ||
); | ||
} | ||
} | ||
} |