From 46632621fbfb54ce88243d4e465a9f529b36aad9 Mon Sep 17 00:00:00 2001 From: Scott Date: Wed, 16 Mar 2016 22:52:02 -0400 Subject: [PATCH] Since open_basedir no longer falls through to OpenSSL, expect a failure. --- phpunit.sh | 4 +- tests/unit_with_basedir/RandomBytesTest.php | 100 ++++++++++++++++++++ tests/unit_with_basedir/RandomIntTest.php | 82 ++++++++++++++++ tests/unit_with_basedir/UtilityTest.php | 95 +++++++++++++++++++ 4 files changed, 279 insertions(+), 2 deletions(-) create mode 100644 tests/unit_with_basedir/RandomBytesTest.php create mode 100644 tests/unit_with_basedir/RandomIntTest.php create mode 100644 tests/unit_with_basedir/UtilityTest.php diff --git a/phpunit.sh b/phpunit.sh index c4c6a9f..2dce4b2 100755 --- a/phpunit.sh +++ b/phpunit.sh @@ -20,13 +20,13 @@ if [ $? -ne 0 ]; then exit 1 fi echo "With open_basedir enabled:" -php -d open_basedir=`pwd` vendor/bin/phpunit tests/unit +php -d open_basedir=`pwd` vendor/bin/phpunit tests/unit_with_basedir if [ $? -ne 0 ]; then # Test failure exit 1 fi echo "With open_basedir enabled, allowing /dev:" -php -d open_basedir=`pwd`:/dev vendor/bin/phpunit tests/unit +php -d open_basedir=`pwd`:/dev vendor/bin/phpunit tests/unit_with_basedir if [ $? -ne 0 ]; then # Test failure exit 1 diff --git a/tests/unit_with_basedir/RandomBytesTest.php b/tests/unit_with_basedir/RandomBytesTest.php new file mode 100644 index 0000000..69964ff --- /dev/null +++ b/tests/unit_with_basedir/RandomBytesTest.php @@ -0,0 +1,100 @@ +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); + } + } +} diff --git a/tests/unit_with_basedir/RandomIntTest.php b/tests/unit_with_basedir/RandomIntTest.php new file mode 100644 index 0000000..56197b0 --- /dev/null +++ b/tests/unit_with_basedir/RandomIntTest.php @@ -0,0 +1,82 @@ +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; + } + } +} diff --git a/tests/unit_with_basedir/UtilityTest.php b/tests/unit_with_basedir/UtilityTest.php new file mode 100644 index 0000000..294a801 --- /dev/null +++ b/tests/unit_with_basedir/UtilityTest.php @@ -0,0 +1,95 @@ +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)) + ); + } + } +}