Added an service for generating Backup codes and added some tests.

This commit is contained in:
Jan Böhmer 2019-12-27 15:20:06 +01:00
parent 452fc3e78a
commit fba5f9794f
16 changed files with 245 additions and 7 deletions

View file

@ -0,0 +1,56 @@
<?php
namespace App\Tests\Services\TFA;
use App\Services\TFA\BackupCodeGenerator;
use PHPUnit\Framework\TestCase;
class BackupCodeGeneratorTest extends TestCase
{
/**
* Test if an exception is thrown if you are using a too high code length
*/
public function testLengthUpperLimit()
{
$this->expectException(\RuntimeException::class);
new BackupCodeGenerator(33, 10);
}
/**
* Test if an exception is thrown if you are using a too high code length
*/
public function testLengthLowerLimit()
{
$this->expectException(\RuntimeException::class);
new BackupCodeGenerator(4, 10);
}
public function codeLengthDataProvider()
{
return [[6], [8], [10], [16]];
}
/**
* @dataProvider codeLengthDataProvider
*/
public function testGenerateSingleCode(int $code_length)
{
$generator = new BackupCodeGenerator($code_length, 10);
$this->assertRegExp("/^([a-f0-9]){{$code_length}}\$/", $generator->generateSingleCode());
}
public function codeCountDataProvider()
{
return [[2], [8], [10]];
}
/**
* @dataProvider codeCountDataProvider
*/
public function testGenerateCodeSet(int $code_count)
{
$generator = new BackupCodeGenerator(8, $code_count);
$this->assertCount($code_count, $generator->generateCodeSet());
}
}