‪TYPO3CMS  ‪main
RandomTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
26 final class ‪RandomTest extends UnitTestCase
27 {
28  #[Test]
30  {
31  $subject = new ‪Random();
32  self::assertEquals(4, strlen($subject->generateRandomBytes(4)));
33  }
34 
39  {
40  return [
41  [1],
42  [2],
43  [3],
44  [4],
45  [7],
46  [8],
47  [31],
48  [32],
49  [100],
50  [102],
51  [4000],
52  [4095],
53  [4096],
54  [4097],
55  [8000],
56  ];
57  }
58 
62  #[DataProvider('generateRandomHexStringReturnsExpectedAmountOfCharsDataProvider')]
63  #[Test]
64  public function ‪generateRandomHexStringReturnsExpectedAmountOfChars($numberOfChars): void
65  {
66  $subject = new ‪Random();
67  self::assertEquals($numberOfChars, strlen($subject->generateRandomHexString($numberOfChars)));
68  }
69 
71  {
72  yield 'Invalid length' => [
73  [
74  'length' => 4,
75  ],
76  1667557900,
77  ];
78  yield 'Invalid random value' => [
79  [
80  'random' => 'invalid',
81  ],
82  1667557901,
83  ];
84  yield 'Invalid characters definition' => [
85  [
86  'lowerCaseCharacters' => false,
87  'upperCaseCharacters' => false,
88  'digitCharacters' => false,
89  ],
90  1667557902,
91  ];
92  }
93 
94  #[DataProvider('generateRandomPasswordThrowsInvalidPasswordRulesExceptionDataProvider')]
95  #[Test]
97  array $passwordRules,
98  int $exceptionCode
99  ): void {
100  $this->expectException(InvalidPasswordRulesException::class);
101  $this->expectExceptionCode($exceptionCode);
102 
103  (new ‪Random())->generateRandomPassword($passwordRules);
104  }
105 
107  {
108  yield 'Hex with 42 chars' => [
109  [
110  'length' => 42,
111  'random' => 'hex',
112  ],
113  '/^[a-fA-F0-9]{42}$/',
114  ];
115  yield 'Base64 with 37 chars' => [
116  [
117  'length' => 37,
118  'random' => 'base64',
119  'digitCharacters' => false, // Won't be evaluated
120  ],
121  '/^[a-zA-Z0-9\-\_]{37}$/',
122  ];
123  }
124 
125  #[DataProvider('generateRandomPasswordGeneratesRandomWithEncodingDataProvider')]
126  #[Test]
128  array $passwordRules,
129  string $pattern
130  ): void {
131  self::assertMatchesRegularExpression($pattern, (new ‪Random())->generateRandomPassword($passwordRules));
132  }
133 
135  {
136  yield 'lowercase' => [
137  [
138  'lowerCaseCharacters' => true,
139  ],
140  '/[a-z]+/',
141  ];
142  yield 'uppercase' => [
143  [
144  'upperCaseCharacters' => true,
145  ],
146  '/[A-Z]+/',
147  ];
148  yield 'digits' => [
149  [
150  'digitCharacters' => true,
151  ],
152  '/[0-9]+/',
153  ];
154  yield 'special' => [
155  [
156  'specialCharacters' => true,
157  ],
158  '/[\'!"#$%&()*+,\-.\/:;<=>?@\[\]^_`{|}~]+/',
159  ];
160  }
161 
162  #[DataProvider('generateRandomPasswordGeneratesRandomWithCharacterSetsDataProvider')]
163  #[Test]
165  array $passwordRules,
166  string $pattern
167  ): void {
168  self::assertMatchesRegularExpression($pattern, (new ‪Random())->generateRandomPassword($passwordRules));
169  }
170 
172  {
173  yield 'fallback' => [
174  [],
175  16,
176  ];
177  yield 'length=40' => [
178  [
179  'length' => 40,
180  ],
181  40,
182  ];
183  yield 'length=36 with random=hex' => [
184  [
185  'length' => 36,
186  'random' => 'hex',
187  ],
188  36,
189  ];
190  yield 'length=42 with random=hex' => [
191  [
192  'length' => 42,
193  'random' => 'base64',
194  ],
195  42,
196  ];
197  }
198 
199  #[DataProvider('generateRandomPasswordGeneratesRandomWithLengthDataProvider')]
200  #[Test]
202  array $passwordRules,
203  int $length
204  ): void {
205  self::assertEquals($length, strlen((new ‪Random())->generateRandomPassword($passwordRules)));
206  }
207 }
‪TYPO3\CMS\Core\Tests\Unit\Crypto\RandomTest\generateRandomPasswordGeneratesRandomWithEncoding
‪generateRandomPasswordGeneratesRandomWithEncoding(array $passwordRules, string $pattern)
Definition: RandomTest.php:127
‪TYPO3\CMS\Core\Exception\InvalidPasswordRulesException
Definition: InvalidPasswordRulesException.php:25
‪TYPO3\CMS\Core\Tests\Unit\Crypto\RandomTest\generateRandomPasswordThrowsInvalidPasswordRulesExceptionDataProvider
‪static generateRandomPasswordThrowsInvalidPasswordRulesExceptionDataProvider()
Definition: RandomTest.php:70
‪TYPO3\CMS\Core\Tests\Unit\Crypto\RandomTest\generateRandomPasswordGeneratesRandomWithLengthDataProvider
‪static generateRandomPasswordGeneratesRandomWithLengthDataProvider()
Definition: RandomTest.php:171
‪TYPO3\CMS\Core\Tests\Unit\Crypto\RandomTest\generateRandomPasswordGeneratesRandomWithEncodingDataProvider
‪static generateRandomPasswordGeneratesRandomWithEncodingDataProvider()
Definition: RandomTest.php:106
‪TYPO3\CMS\Core\Tests\Unit\Crypto\RandomTest\generateRandomPasswordThrowsInvalidPasswordRulesException
‪generateRandomPasswordThrowsInvalidPasswordRulesException(array $passwordRules, int $exceptionCode)
Definition: RandomTest.php:96
‪TYPO3\CMS\Core\Tests\Unit\Crypto\RandomTest\generateRandomBytesReturnsExpectedAmountOfBytes
‪generateRandomBytesReturnsExpectedAmountOfBytes()
Definition: RandomTest.php:29
‪TYPO3\CMS\Core\Tests\Unit\Crypto\RandomTest\generateRandomPasswordGeneratesRandomWithLength
‪generateRandomPasswordGeneratesRandomWithLength(array $passwordRules, int $length)
Definition: RandomTest.php:201
‪TYPO3\CMS\Core\Tests\Unit\Crypto\RandomTest
Definition: RandomTest.php:27
‪TYPO3\CMS\Core\Tests\Unit\Crypto
Definition: HashServiceTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Crypto\RandomTest\generateRandomHexStringReturnsExpectedAmountOfChars
‪generateRandomHexStringReturnsExpectedAmountOfChars($numberOfChars)
Definition: RandomTest.php:64
‪TYPO3\CMS\Core\Tests\Unit\Crypto\RandomTest\generateRandomPasswordGeneratesRandomWithCharacterSets
‪generateRandomPasswordGeneratesRandomWithCharacterSets(array $passwordRules, string $pattern)
Definition: RandomTest.php:164
‪TYPO3\CMS\Core\Crypto\Random
Definition: Random.php:27
‪TYPO3\CMS\Core\Tests\Unit\Crypto\RandomTest\generateRandomPasswordGeneratesRandomWithCharacterSetsDataProvider
‪static generateRandomPasswordGeneratesRandomWithCharacterSetsDataProvider()
Definition: RandomTest.php:134
‪TYPO3\CMS\Core\Tests\Unit\Crypto\RandomTest\generateRandomHexStringReturnsExpectedAmountOfCharsDataProvider
‪static generateRandomHexStringReturnsExpectedAmountOfCharsDataProvider()
Definition: RandomTest.php:38