aboutsummaryrefslogtreecommitdiffstats
path: root/tests/random/gen_rand_string_test.php
blob: 044fd3cc15efd8428649209dc5098841a75a611c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/

class phpbb_random_gen_rand_string_test extends phpbb_test_case
{
	const TEST_COUNT = 100;
	const MIN_STRING_LENGTH = 1;
	const MAX_STRING_LENGTH = 15;

	public function setUp(): void
	{
		global $config;

		if (!is_array($config))
		{
			$config = array();
		}

		$config['rand_seed'] = '';
		$config['rand_seed_last_update'] = time() + 600;
	}

	public function test_gen_rand_string()
	{
		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($num_chars);
				$random_string_length = strlen($random_string);

				$this->assertTrue($random_string_length >= self::MIN_STRING_LENGTH);
				$this->assertTrue(
					$random_string_length == $num_chars,
					sprintf('Failed asserting that random string length matches expected length. Expected %1$u, Actual %2$u', $num_chars, $random_string_length)
				);
				$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,
					sprintf('Failed asserting that random string length matches expected length. Expected %1$u, Actual %2$u', $num_chars, $random_string_length)
				);
				$this->assertRegExp('#^[A-NP-Z1-9]+$#', $random_string);
			}
		}
	}
}