From d53312174169f311714da389b6a36fe6a4bc8f9a Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 20 Aug 2010 09:21:15 -0400 Subject: [PATCH 1/2] [ticket/9780] Add length check back to gen_rand_string(). PHPBB3-9780 --- phpBB/includes/functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 980a26d525..36b9e18176 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -201,7 +201,7 @@ function set_config_count($config_name, $increment, $is_dynamic = false) function gen_rand_string($num_chars = 8) { // [a, z] + [0, 9] = 36 - return strtoupper(base_convert(unique_id(), 16, 36)); + return substr(strtoupper(base_convert(unique_id(), 16, 36)), 0, $num_chars); } /** From e09d6c6d7139375d8b645664bef295c82d585653 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Fri, 20 Aug 2010 21:12:18 +0200 Subject: [PATCH 2/2] [ticket/9780] Adding unit tests for gen_rand_string(). PHPBB3-9780 --- tests/all_tests.php | 2 + tests/random/all_tests.php | 40 ++++++++++++++++++++ tests/random/gen_rand_string.php | 63 ++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 tests/random/all_tests.php create mode 100644 tests/random/gen_rand_string.php diff --git a/tests/all_tests.php b/tests/all_tests.php index 8dc07872e8..bae7725ee7 100644 --- a/tests/all_tests.php +++ b/tests/all_tests.php @@ -25,6 +25,7 @@ require_once 'text_processing/all_tests.php'; require_once 'dbal/all_tests.php'; require_once 'regex/all_tests.php'; require_once 'network/all_tests.php'; +require_once 'random/all_tests.php'; // exclude the test directory from code coverage reports PHPUnit_Util_Filter::addDirectoryToFilter('./'); @@ -48,6 +49,7 @@ class phpbb_all_tests $suite->addTest(phpbb_dbal_all_tests::suite()); $suite->addTest(phpbb_regex_all_tests::suite()); $suite->addTest(phpbb_network_all_tests::suite()); + $suite->addTest(phpbb_random_all_tests::suite()); return $suite; } diff --git a/tests/random/all_tests.php b/tests/random/all_tests.php new file mode 100644 index 0000000000..c6ffe78024 --- /dev/null +++ b/tests/random/all_tests.php @@ -0,0 +1,40 @@ +addTestSuite('phpbb_random_gen_rand_string_test'); + + return $suite; + } +} + +if (PHPUnit_MAIN_METHOD == 'phpbb_random_all_tests::main') +{ + phpbb_random_all_tests::main(); +} diff --git a/tests/random/gen_rand_string.php b/tests/random/gen_rand_string.php new file mode 100644 index 0000000000..cd58d14ed3 --- /dev/null +++ b/tests/random/gen_rand_string.php @@ -0,0 +1,63 @@ +assertTrue($random_string_length >= self::MIN_STRING_LENGTH); + $this->assertTrue($random_string_length <= $num_chars); + $this->assertRegExp('#^[A-Z0-9]+$#', $random_string); + } + } + } + + public function test_gen_rand_string_friendly() + { + for ($tests = 0; $tests <= self::TEST_COUNT; ++$tests) + { + for ($num_chars = self::MIN_STRING_LENGTH; $num_chars <= self::MAX_STRING_LENGTH; ++$num_chars) + { + $random_string = gen_rand_string_friendly($num_chars); + $random_string_length = strlen($random_string); + + $this->assertTrue($random_string_length >= self::MIN_STRING_LENGTH); + $this->assertTrue($random_string_length <= $num_chars); + $this->assertRegExp('#^[A-NP-Z1-9]+$#', $random_string); + } + } + } +}