Skip to content

Commit

Permalink
Merge pull request #59 from oittaa/patch-6
Browse files Browse the repository at this point in the history
Updated UtilityTest.php 64bit tests
  • Loading branch information
paragonie-scott committed Oct 16, 2015
2 parents 57d0261 + df3a673 commit df12076
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
11 changes: 2 additions & 9 deletions lib/cast_to_int.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,15 @@
*/
function RandomCompat_intval($number, $fail_open = false)
{
if (
is_string($number) &&
preg_match('#^\-?[0-9]+\.?[0-9]*$#', $number)
) {
if (is_numeric($number)) {
$number += 0;
}
if (
is_float($number) &&
$number > ~PHP_INT_MAX &&
$number < PHP_INT_MAX
) {
$number = (int) (
$number < 0
? ceil($number)
: floor($number)
);
$number = (int) $number;
}
if (is_int($number) || $fail_open) {
return $number;
Expand Down
1 change: 1 addition & 0 deletions lib/random_bytes_libsodium.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ function random_bytes($bytes)
* generated in one invocation.
*/
if ($bytes > 2147483647) {
$buf = '';
for ($i = 0; $i < $bytes; $i += 1073741824) {
$n = ($bytes - $i) > 1073741824
? 1073741824
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/RandomIntTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public function testOutput()
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(-4.5, -4.5),
random_int("1337e3","1337e3")
);

$this->assertFalse($integers[0] === $integers[1]);
Expand All @@ -33,6 +34,7 @@ public function testOutput()
$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);

try {
$h = random_int("2147483648", "2147483647");
Expand Down
19 changes: 17 additions & 2 deletions tests/unit/UtilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public function testIntval()
$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(
Expand All @@ -54,13 +60,22 @@ public function testIntval()
$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("-9223372036854775807", true))
is_int(RandomCompat_intval("-9223372036854775808", true))
);
$this->assertFalse(
is_int(RandomCompat_intval("9223372036854775808", true))
);
$this->assertTrue(
is_int(RandomCompat_intval("9223372036854775806", true))
is_int(RandomCompat_intval("9223372036854775807", true))
);
} else {
$this->assertFalse(
Expand Down

0 comments on commit df12076

Please sign in to comment.