]> granicus.if.org Git - php/commitdiff
Add tests for CSPRNG, fix C99 comments
authorSammyK <sammyk@sammykmedia.com>
Sat, 9 May 2015 19:45:22 +0000 (21:45 +0200)
committerNikita Popov <nikic@php.net>
Sat, 9 May 2015 20:29:26 +0000 (22:29 +0200)
Also replace one return; with RETURN_FALSE; for consistency.

ext/standard/random.c
ext/standard/tests/random/random_bytes.phpt [new file with mode: 0644]
ext/standard/tests/random/random_bytes_error.phpt [new file with mode: 0644]
ext/standard/tests/random/random_int.phpt [new file with mode: 0644]
ext/standard/tests/random/random_int_error.phpt [new file with mode: 0644]

index 22531cf24c1bd62411a93532881bbfc5e6078ee0..12c25031d83a446585b9b50fefdef30df58bc3be 100644 (file)
@@ -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 (file)
index 0000000..8639138
--- /dev/null
@@ -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 (file)
index 0000000..466a3ac
--- /dev/null
@@ -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 (file)
index 0000000..0c30814
--- /dev/null
@@ -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 (file)
index 0000000..5f7a69b
--- /dev/null
@@ -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)