summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSammyK <sammyk@sammykmedia.com>2015-05-09 21:45:22 +0200
committerNikita Popov <nikic@php.net>2015-05-09 22:29:26 +0200
commitdd2692621dcc236fd463d045e737f4914ab53612 (patch)
tree351acf3ce3ac508aba4381c60e28a07a2dd1e9df
parent5f1b83e9bb1e2fa2fe798b12264c5032168fe3a0 (diff)
downloadphp-git-dd2692621dcc236fd463d045e737f4914ab53612.tar.gz
Add tests for CSPRNG, fix C99 comments
Also replace one return; with RETURN_FALSE; for consistency.
-rw-r--r--ext/standard/random.c29
-rw-r--r--ext/standard/tests/random/random_bytes.phpt14
-rw-r--r--ext/standard/tests/random/random_bytes_error.phpt17
-rw-r--r--ext/standard/tests/random/random_int.phpt18
-rw-r--r--ext/standard/tests/random/random_int_error.phpt22
5 files changed, 84 insertions, 16 deletions
diff --git a/ext/standard/random.c b/ext/standard/random.c
index 22531cf24c..12c25031d8 100644
--- a/ext/standard/random.c
+++ b/ext/standard/random.c
@@ -68,6 +68,8 @@ PHP_MSHUTDOWN_FUNCTION(random)
#ifndef ZTS
random_globals_dtor(&random_globals);
#endif
+
+ return SUCCESS;
}
/* }}} */
@@ -80,8 +82,7 @@ static int php_random_bytes(void *bytes, size_t size)
php_error_docref(NULL, E_WARNING, "Could not gather sufficient random data");
return FAILURE;
}
-#else
-#if HAVE_DECL_ARC4RANDOM_BUF
+#elif HAVE_DECL_ARC4RANDOM_BUF
arc4random_buf(bytes, size);
#else
int fd = RANDOM_G(fd);
@@ -90,11 +91,9 @@ static int php_random_bytes(void *bytes, size_t size)
if (fd < 0) {
#if HAVE_DEV_ARANDOM
fd = open("/dev/arandom", O_RDONLY);
-#else
-#if HAVE_DEV_URANDOM
+#elif HAVE_DEV_URANDOM
fd = open("/dev/urandom", O_RDONLY);
-#endif // URANDOM
-#endif // ARANDOM
+#endif
if (fd < 0) {
php_error_docref(NULL, E_WARNING, "Cannot open source device");
return FAILURE;
@@ -115,8 +114,7 @@ static int php_random_bytes(void *bytes, size_t size)
php_error_docref(NULL, E_WARNING, "Could not gather sufficient random data");
return FAILURE;
}
-#endif // !ARC4RANDOM_BUF
-#endif // !WIN32
+#endif
return SUCCESS;
}
@@ -157,7 +155,6 @@ PHP_FUNCTION(random_int)
{
zend_long min;
zend_long max;
- zend_ulong limit;
zend_ulong umax;
zend_ulong result;
@@ -176,23 +173,23 @@ PHP_FUNCTION(random_int)
RETURN_FALSE;
}
- // Special case where no modulus is required
+ /* Special case where no modulus is required */
if (umax == ZEND_ULONG_MAX) {
RETURN_LONG((zend_long)result);
}
- // Increment the max so the range is inclusive of max
+ /* Increment the max so the range is inclusive of max */
umax++;
- // Powers of two are not biased
+ /* Powers of two are not biased */
if ((umax & ~umax) != umax) {
- // Ceiling under which ZEND_LONG_MAX % max == 0
- limit = ZEND_ULONG_MAX - (ZEND_ULONG_MAX % umax) - 1;
+ /* Ceiling under which ZEND_LONG_MAX % max == 0 */
+ zend_ulong limit = ZEND_ULONG_MAX - (ZEND_ULONG_MAX % umax) - 1;
- // Discard numbers over the limit to avoid modulo bias
+ /* Discard numbers over the limit to avoid modulo bias */
while (result > limit) {
if (php_random_bytes(&result, sizeof(result)) == FAILURE) {
- return;
+ RETURN_FALSE;
}
}
}
diff --git a/ext/standard/tests/random/random_bytes.phpt b/ext/standard/tests/random/random_bytes.phpt
new file mode 100644
index 0000000000..86391383e4
--- /dev/null
+++ b/ext/standard/tests/random/random_bytes.phpt
@@ -0,0 +1,14 @@
+--TEST--
+Test normal operation of random_bytes()
+--FILE--
+<?php
+//-=-=-=-
+
+var_dump(strlen(bin2hex(random_bytes(16))));
+
+var_dump(is_string(random_bytes(10)));
+
+?>
+--EXPECT--
+int(32)
+bool(true)
diff --git a/ext/standard/tests/random/random_bytes_error.phpt b/ext/standard/tests/random/random_bytes_error.phpt
new file mode 100644
index 0000000000..466a3ac3bf
--- /dev/null
+++ b/ext/standard/tests/random/random_bytes_error.phpt
@@ -0,0 +1,17 @@
+--TEST--
+Test error operation of random_bytes()
+--FILE--
+<?php
+//-=-=-=-
+
+var_dump(random_bytes());
+
+var_dump(random_bytes(-1));
+
+?>
+--EXPECTF--
+Warning: random_bytes() expects exactly 1 parameter, 0 given in %s on line %d
+NULL
+
+Warning: random_bytes(): Length must be greater than 0 in %s on line %d
+bool(false)
diff --git a/ext/standard/tests/random/random_int.phpt b/ext/standard/tests/random/random_int.phpt
new file mode 100644
index 0000000000..0c3081452c
--- /dev/null
+++ b/ext/standard/tests/random/random_int.phpt
@@ -0,0 +1,18 @@
+--TEST--
+Test normal operation of random_int()
+--FILE--
+<?php
+//-=-=-=-
+
+var_dump(is_int(random_int(10, 100)));
+
+$x = random_int(10, 100);
+var_dump($x >= 10 && $x <= 100);
+
+var_dump(random_int(-1000, -1) < 0);
+
+?>
+--EXPECT--
+bool(true)
+bool(true)
+bool(true)
diff --git a/ext/standard/tests/random/random_int_error.phpt b/ext/standard/tests/random/random_int_error.phpt
new file mode 100644
index 0000000000..5f7a69b215
--- /dev/null
+++ b/ext/standard/tests/random/random_int_error.phpt
@@ -0,0 +1,22 @@
+--TEST--
+Test error operation of random_int()
+--FILE--
+<?php
+//-=-=-=-
+
+var_dump(random_int());
+
+var_dump(random_int(10));
+
+var_dump(random_int(10, 0));
+
+?>
+--EXPECTF--
+Warning: random_int() expects exactly 2 parameters, 0 given in %s on line %d
+NULL
+
+Warning: random_int() expects exactly 2 parameters, 1 given in %s on line %d
+NULL
+
+Warning: random_int(): Minimum value must be less than the maximum value in %s on line %d
+bool(false)