-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathSecureRandomTest.php
More file actions
134 lines (111 loc) · 3.85 KB
/
SecureRandomTest.php
File metadata and controls
134 lines (111 loc) · 3.85 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\Security;
use OC\Security\SecureRandom;
class SecureRandomTest extends \Test\TestCase {
public static function stringGenerationProvider(): array {
return [
[1, 1],
[16, 16],
[31, 31],
[64, 64],
[128, 128],
[1024, 1024],
];
}
public static function charCombinations(): array {
return [
['CHAR_LOWER', '[a-z]'],
['CHAR_UPPER', '[A-Z]'],
['CHAR_DIGITS', '[0-9]'],
];
}
/** @var SecureRandom */
protected $rng;
#[\Override]
protected function setUp(): void {
parent::setUp();
$this->rng = new SecureRandom();
}
#[\PHPUnit\Framework\Attributes\DataProvider('stringGenerationProvider')]
public function testGetLowStrengthGeneratorLength($length, $expectedLength): void {
$generator = $this->rng;
$this->assertEquals($expectedLength, strlen($generator->generate($length)));
}
#[\PHPUnit\Framework\Attributes\DataProvider('stringGenerationProvider')]
public function testMediumLowStrengthGeneratorLength($length, $expectedLength): void {
$generator = $this->rng;
$this->assertEquals($expectedLength, strlen($generator->generate($length)));
}
#[\PHPUnit\Framework\Attributes\DataProvider('stringGenerationProvider')]
public function testUninitializedGenerate($length, $expectedLength): void {
$this->assertEquals($expectedLength, strlen($this->rng->generate($length)));
}
#[\PHPUnit\Framework\Attributes\DataProvider('charCombinations')]
public function testScheme($charName, $chars): void {
$generator = $this->rng;
$scheme = constant('OCP\Security\ISecureRandom::' . $charName);
$randomString = $generator->generate(100, $scheme);
$matchesRegex = preg_match('/^' . $chars . '+$/', $randomString);
$this->assertSame(1, $matchesRegex);
}
public static function invalidLengths(): array {
return [
[0],
[-1],
];
}
#[\PHPUnit\Framework\Attributes\DataProvider('invalidLengths')]
public function testInvalidLengths($length): void {
$this->expectException(\LengthException::class);
$generator = $this->rng;
$generator->generate($length);
}
public static function invalidCharProviders(): array {
return [
'invalid_too_short' => ['abc'],
'invalid_duplicates' => ['aabcd'],
'invalid_non_ascii' => ["abcd\xf0"],
];
}
#[\PHPUnit\Framework\Attributes\DataProvider('invalidCharProviders')]
public function testInvalidCharacterSets(string $invalidCharset): void {
$this->expectException(\InvalidArgumentException::class);
$this->rng->generate(10, $invalidCharset);
}
public function testDefaultCharsetBase64Characters(): void {
$randomString = $this->rng->generate(100);
$this->assertMatchesRegularExpression('/^[A-Za-z0-9\+\/]{100}$/', $randomString);
}
public function testAllOutputsAreUnique(): void {
// While collisions are technically possible, extremely unlikely for these sizes
$first = $this->rng->generate(1000);
$second = $this->rng->generate(1000);
$this->assertNotEquals($first, $second, "Random output should not be repeated.");
}
public function testMinimumValidCharset(): void {
$charset = 'abcd';
$randomString = $this->rng->generate(500, $charset);
$this->assertMatchesRegularExpression('/^[abcd]{500}$/', $randomString);
}
public function testLargeCustomCharset(): void {
$charset = '';
for ($i = 32; $i <= 126; $i++) { // all printable ASCII
$charset .= chr($i);
}
$randomString = $this->rng->generate(200, $charset);
foreach (str_split($randomString) as $char) {
$this->assertStringContainsString($char, $charset);
}
}
public function testUserProvidedValidCharset(): void {
$charset = '@#$!';
$randomString = $this->rng->generate(64, $charset);
$this->assertMatchesRegularExpression('/^[@#$!]{64}$/', $randomString);
}
}