0
0
Fork 0
mirror of https://github.com/nextcloud/server.git synced 2025-01-28 13:50:37 +00:00
nextcloud_server/tests/lib/Security/SecureRandomTest.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

95 lines
2.1 KiB
PHP
Raw Permalink Normal View History

<?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
*/
2016-05-19 07:02:58 +00:00
namespace Test\Security;
use OC\Security\SecureRandom;
2014-09-03 11:51:44 +00:00
class SecureRandomTest extends \Test\TestCase {
public function stringGenerationProvider() {
return [
[1, 1],
[128, 128],
[256, 256],
[1024, 1024],
[2048, 2048],
[64000, 64000],
];
}
2014-09-03 12:13:12 +00:00
public static function charCombinations() {
return [
['CHAR_LOWER', '[a-z]'],
['CHAR_UPPER', '[A-Z]'],
['CHAR_DIGITS', '[0-9]'],
];
2014-09-03 12:13:12 +00:00
}
2014-09-03 11:51:44 +00:00
/** @var SecureRandom */
protected $rng;
protected function setUp(): void {
parent::setUp();
2014-09-03 11:51:44 +00:00
$this->rng = new \OC\Security\SecureRandom();
}
/**
* @dataProvider stringGenerationProvider
*/
public function testGetLowStrengthGeneratorLength($length, $expectedLength): void {
$generator = $this->rng;
$this->assertEquals($expectedLength, strlen($generator->generate($length)));
}
/**
* @dataProvider stringGenerationProvider
*/
public function testMediumLowStrengthGeneratorLength($length, $expectedLength): void {
$generator = $this->rng;
$this->assertEquals($expectedLength, strlen($generator->generate($length)));
}
/**
2015-12-11 05:17:47 +00:00
* @dataProvider stringGenerationProvider
*/
public function testUninitializedGenerate($length, $expectedLength): void {
2015-12-11 05:17:47 +00:00
$this->assertEquals($expectedLength, strlen($this->rng->generate($length)));
}
2014-09-03 12:13:12 +00:00
/**
* @dataProvider charCombinations
*/
public function testScheme($charName, $chars): void {
$generator = $this->rng;
2014-09-03 12:13:12 +00:00
$scheme = constant('OCP\Security\ISecureRandom::' . $charName);
$randomString = $generator->generate(100, $scheme);
$matchesRegex = preg_match('/^' . $chars . '+$/', $randomString);
$this->assertSame(1, $matchesRegex);
}
public static function invalidLengths() {
return [
[0],
[-1],
];
}
/**
* @dataProvider invalidLengths
*/
public function testInvalidLengths($length): void {
$this->expectException(\LengthException::class);
$generator = $this->rng;
$generator->generate($length);
}
}