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
|
<?php
declare(strict_types=1);
/**
* Copyright (c) 2014 Lukas Reschke <lukas@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace Test\Security;
use OC\Security\SecureRandom;
class SecureRandomTest extends \Test\TestCase {
public function stringGenerationProvider() {
return [
[0, 0],
[1, 1],
[128, 128],
[256, 256],
[1024, 1024],
[2048, 2048],
[64000, 64000],
];
}
public static function charCombinations() {
return [
['CHAR_LOWER', '[a-z]'],
['CHAR_UPPER', '[A-Z]'],
['CHAR_DIGITS', '[0-9]'],
];
}
/** @var SecureRandom */
protected $rng;
protected function setUp(): void {
parent::setUp();
$this->rng = new \OC\Security\SecureRandom();
}
/**
* @dataProvider stringGenerationProvider
*/
public function testGetLowStrengthGeneratorLength($length, $expectedLength) {
$generator = $this->rng;
$this->assertEquals($expectedLength, strlen($generator->generate($length)));
}
/**
* @dataProvider stringGenerationProvider
*/
public function testMediumLowStrengthGeneratorLength($length, $expectedLength) {
$generator = $this->rng;
$this->assertEquals($expectedLength, strlen($generator->generate($length)));
}
/**
* @dataProvider stringGenerationProvider
*/
public function testUninitializedGenerate($length, $expectedLength) {
$this->assertEquals($expectedLength, strlen($this->rng->generate($length)));
}
/**
* @dataProvider charCombinations
*/
public function testScheme($charName, $chars) {
$generator = $this->rng;
$scheme = constant('OCP\Security\ISecureRandom::' . $charName);
$randomString = $generator->generate(100, $scheme);
$matchesRegex = preg_match('/^'.$chars.'+$/', $randomString);
$this->assertSame(1, $matchesRegex);
}
}
|