summaryrefslogtreecommitdiff
path: root/vendor/paragonie/random_compat/tests
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/paragonie/random_compat/tests')
-rw-r--r--vendor/paragonie/random_compat/tests/full/DieHardTest.php61
-rw-r--r--vendor/paragonie/random_compat/tests/full/StatTest.php52
-rw-r--r--vendor/paragonie/random_compat/tests/specific/capicom.php7
-rw-r--r--vendor/paragonie/random_compat/tests/specific/dev_urandom.php7
-rw-r--r--vendor/paragonie/random_compat/tests/specific/libsodium.php7
-rw-r--r--vendor/paragonie/random_compat/tests/specific/mcrypt.php7
-rw-r--r--vendor/paragonie/random_compat/tests/specific/openssl.php7
-rw-r--r--vendor/paragonie/random_compat/tests/unit/RandomBytesTest.php52
-rw-r--r--vendor/paragonie/random_compat/tests/unit/RandomIntTest.php52
-rw-r--r--vendor/paragonie/random_compat/tests/unit/UtilityTest.php95
10 files changed, 347 insertions, 0 deletions
diff --git a/vendor/paragonie/random_compat/tests/full/DieHardTest.php b/vendor/paragonie/random_compat/tests/full/DieHardTest.php
new file mode 100644
index 0000000000..2e6dc70051
--- /dev/null
+++ b/vendor/paragonie/random_compat/tests/full/DieHardTest.php
@@ -0,0 +1,61 @@
+<?php
+class DieHardTest extends PHPUnit_Framework_TestCase
+{
+ /**
+ * Birthday spacings: Choose random points on a large interval.
+ * The spacings between the points should be asymptotically exponentially
+ * distributed.
+ */
+ public function testBirthday()
+ {
+ // Number of buckets to make
+ $num_buckets = 17;
+ // How much tolerance should we allow? 0.01 = 1% 0.50 = 50%, etc.
+ $tolerance = 0.03;
+ $rand_min = 200000;
+ $rand_max = 600000;
+ $rand_step = 100000;
+
+ $minT = (1.00 - $tolerance);
+ $maxT = (1.00 + $tolerance);
+
+ for ($nums_to_generate = $rand_min; $nums_to_generate < $rand_max; $nums_to_generate += $rand_step) {
+ $buckets = array_fill(0, $num_buckets, 0);
+
+ // The number of ints we expect per bucket +/- 2%;
+ $min = (int) ceil($minT * $nums_to_generate / $num_buckets);
+ $max = (int) floor($maxT * $nums_to_generate / $num_buckets);
+
+ for ($i = 0; $i < $nums_to_generate; ++$i) {
+ $random = random_int(0, 999);
+ $bucket = $random % $num_buckets;
+ $buckets[$bucket]++;
+ }
+ for ($i = 0; $i < $num_buckets; ++$i) {
+
+ // Debugging code:
+
+ if ($buckets[$i] <= $min ) {
+ var_dump([
+ 'bucket' => $i,
+ 'value' => $buckets[$i],
+ 'min' => $min,
+ 'nums' => $nums_to_generate,
+ 'reason' => 'below min'
+ ]);
+ }
+ if ($buckets[$i] >= $max ) {
+ var_dump([
+ 'bucket' => $i,
+ 'value' => $buckets[$i],
+ 'maax' => $max,
+ 'nums' => $nums_to_generate,
+ 'reason' => 'above max'
+ ]);
+ }
+
+ $this->assertTrue($buckets[$i] < $max && $buckets[$i] > $min);
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/vendor/paragonie/random_compat/tests/full/StatTest.php b/vendor/paragonie/random_compat/tests/full/StatTest.php
new file mode 100644
index 0000000000..e78eb14633
--- /dev/null
+++ b/vendor/paragonie/random_compat/tests/full/StatTest.php
@@ -0,0 +1,52 @@
+<?php
+
+class StatTest extends PHPUnit_Framework_TestCase
+{
+ /**
+ * All possible values should be > 30% but less than 170%
+ *
+ * This also catches 0 and 1000
+ */
+ public function testDistribution()
+ {
+ $integers = array_fill(0, 100, 0);
+ for ($i = 0; $i < 10000; ++$i) {
+ ++$integers[random_int(0,99)];
+ }
+ for ($i = 0; $i < 100; ++$i) {
+ $this->assertFalse($integers[$i] < 30);
+ $this->assertFalse($integers[$i] > 170);
+ }
+ }
+
+ /**
+ * This should be between 55% and 75%, always
+ */
+ public function testCoverage()
+ {
+ $integers = array_fill(0, 2000, 0);
+ for ($i = 0; $i < 2000; ++$i) {
+ ++$integers[random_int(0,1999)];
+ }
+ $coverage = 0;
+ for ($i = 0; $i < 2000; ++$i) {
+ if ($integers[$i] > 0) {
+ ++$coverage;
+ }
+ }
+ $this->assertTrue($coverage >= 1150);
+ $this->assertTrue($coverage <= 1350);
+ }
+
+ public function testCompressionRatios()
+ {
+ $some_bytes = random_bytes(65536);
+ $compressed = gzcompress($some_bytes, 9);
+ if (function_exists('mb_strlen')) {
+ $length = mb_strlen($compressed, '8bit');
+ } else {
+ $length = strlen($compressed);
+ }
+ $this->assertTrue($length >= 65000 && $length <= 67000);
+ }
+} \ No newline at end of file
diff --git a/vendor/paragonie/random_compat/tests/specific/capicom.php b/vendor/paragonie/random_compat/tests/specific/capicom.php
new file mode 100644
index 0000000000..640be1b5b8
--- /dev/null
+++ b/vendor/paragonie/random_compat/tests/specific/capicom.php
@@ -0,0 +1,7 @@
+<?php
+$ut_dir = dirname(dirname(__DIR__));
+require_once $ut_dir.'/lib/byte_safe_strings.php';
+require_once $ut_dir.'/lib/cast_to_int.php';
+require_once $ut_dir.'/lib/error_polyfill.php';
+require_once $ut_dir.'/lib/random_bytes_com_dotnet.php';
+require_once $ut_dir.'/lib/random_int.php'; \ No newline at end of file
diff --git a/vendor/paragonie/random_compat/tests/specific/dev_urandom.php b/vendor/paragonie/random_compat/tests/specific/dev_urandom.php
new file mode 100644
index 0000000000..bc8bfc968f
--- /dev/null
+++ b/vendor/paragonie/random_compat/tests/specific/dev_urandom.php
@@ -0,0 +1,7 @@
+<?php
+$ut_dir = dirname(dirname(__DIR__));
+require_once $ut_dir.'/lib/byte_safe_strings.php';
+require_once $ut_dir.'/lib/cast_to_int.php';
+require_once $ut_dir.'/lib/error_polyfill.php';
+require_once $ut_dir.'/lib/random_bytes_dev_urandom.php';
+require_once $ut_dir.'/lib/random_int.php'; \ No newline at end of file
diff --git a/vendor/paragonie/random_compat/tests/specific/libsodium.php b/vendor/paragonie/random_compat/tests/specific/libsodium.php
new file mode 100644
index 0000000000..3283c0f109
--- /dev/null
+++ b/vendor/paragonie/random_compat/tests/specific/libsodium.php
@@ -0,0 +1,7 @@
+<?php
+$ut_dir = dirname(dirname(__DIR__));
+require_once $ut_dir.'/lib/byte_safe_strings.php';
+require_once $ut_dir.'/lib/cast_to_int.php';
+require_once $ut_dir.'/lib/error_polyfill.php';
+require_once $ut_dir.'/lib/random_bytes_libsodium.php';
+require_once $ut_dir.'/lib/random_int.php'; \ No newline at end of file
diff --git a/vendor/paragonie/random_compat/tests/specific/mcrypt.php b/vendor/paragonie/random_compat/tests/specific/mcrypt.php
new file mode 100644
index 0000000000..fe0ffa70f8
--- /dev/null
+++ b/vendor/paragonie/random_compat/tests/specific/mcrypt.php
@@ -0,0 +1,7 @@
+<?php
+$ut_dir = dirname(dirname(__DIR__));
+require_once $ut_dir.'/lib/byte_safe_strings.php';
+require_once $ut_dir.'/lib/cast_to_int.php';
+require_once $ut_dir.'/lib/error_polyfill.php';
+require_once $ut_dir.'/lib/random_bytes_mcrypt.php';
+require_once $ut_dir.'/lib/random_int.php'; \ No newline at end of file
diff --git a/vendor/paragonie/random_compat/tests/specific/openssl.php b/vendor/paragonie/random_compat/tests/specific/openssl.php
new file mode 100644
index 0000000000..2f4acce515
--- /dev/null
+++ b/vendor/paragonie/random_compat/tests/specific/openssl.php
@@ -0,0 +1,7 @@
+<?php
+$ut_dir = dirname(dirname(__DIR__));
+require_once $ut_dir.'/lib/byte_safe_strings.php';
+require_once $ut_dir.'/lib/cast_to_int.php';
+require_once $ut_dir.'/lib/error_polyfill.php';
+require_once $ut_dir.'/lib/random_bytes_openssl.php';
+require_once $ut_dir.'/lib/random_int.php'; \ No newline at end of file
diff --git a/vendor/paragonie/random_compat/tests/unit/RandomBytesTest.php b/vendor/paragonie/random_compat/tests/unit/RandomBytesTest.php
new file mode 100644
index 0000000000..0b3c3eb88b
--- /dev/null
+++ b/vendor/paragonie/random_compat/tests/unit/RandomBytesTest.php
@@ -0,0 +1,52 @@
+<?php
+class RandomBytesTest extends PHPUnit_Framework_TestCase
+{
+ public function testFuncExists()
+ {
+ $this->assertTrue(function_exists('random_bytes'));
+ }
+
+ public function testOutput()
+ {
+ $bytes = array(
+ random_bytes(12),
+ random_bytes(64),
+ random_bytes(64),
+ random_bytes(1.5)
+ );
+
+ $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->assertTrue(false);
+ } 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->assertTrue(false);
+ } catch (TypeError $ex) {
+ $this->assertTrue(true);
+ } catch (Error $ex) {
+ $this->assertTrue(true);
+ } catch (Exception $ex) {
+ $this->assertTrue(true);
+ }
+ }
+}
diff --git a/vendor/paragonie/random_compat/tests/unit/RandomIntTest.php b/vendor/paragonie/random_compat/tests/unit/RandomIntTest.php
new file mode 100644
index 0000000000..f9fc3c30c2
--- /dev/null
+++ b/vendor/paragonie/random_compat/tests/unit/RandomIntTest.php
@@ -0,0 +1,52 @@
+<?php
+class RandomIntTest extends PHPUnit_Framework_TestCase
+{
+ public function testFuncExists()
+ {
+ $this->assertTrue(function_exists('random_int'));
+ }
+
+ public function testOutput()
+ {
+ $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);
+
+ 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->assertFalse(true);
+ } catch (Error $ex) {
+ $this->assertTrue($ex instanceof Error);
+ } catch (Exception $ex) {
+ $this->assertTrue($ex instanceof Exception);
+ }
+ }
+}
diff --git a/vendor/paragonie/random_compat/tests/unit/UtilityTest.php b/vendor/paragonie/random_compat/tests/unit/UtilityTest.php
new file mode 100644
index 0000000000..294a8019db
--- /dev/null
+++ b/vendor/paragonie/random_compat/tests/unit/UtilityTest.php
@@ -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))
+ );
+ }
+ }
+}