-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
do not allow multiple uses of the same key
- Loading branch information
1 parent
8fd672f
commit 6e83277
Showing
5 changed files
with
169 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
/** | ||
* @author Semih Serhat Karakaya <karakayasemi@itu.edu.tr> | ||
* | ||
* Two-factor TOTP | ||
* | ||
* This code is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License, version 3, | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License, version 3, | ||
* along with this program. If not, see <http://www.gnu.org/licenses/> | ||
* | ||
*/ | ||
|
||
namespace OCA\TwoFactor_Totp\Tests\Service; | ||
|
||
use OCA\TwoFactor_Totp\Db\TotpSecret; | ||
use OCA\TwoFactor_Totp\Db\TotpSecretMapper; | ||
use OCA\TwoFactor_Totp\Exception\NoTotpSecretFoundException; | ||
use OCA\TwoFactor_Totp\Service\Totp; | ||
use OCP\AppFramework\Db\DoesNotExistException; | ||
use OCP\IUser; | ||
use OCP\Security\ICrypto; | ||
use Otp\Otp; | ||
use Test\TestCase; | ||
|
||
/** | ||
* Class TotpTest | ||
*/ | ||
class TotpTest extends TestCase { | ||
|
||
/** @var TotpSecretMapper | \PHPUnit_Framework_MockObject_MockObject */ | ||
private $secretMapper; | ||
|
||
/** @var ICrypto | \PHPUnit_Framework_MockObject_MockObject */ | ||
private $crypto; | ||
|
||
/** @var Otp | \PHPUnit_Framework_MockObject_MockObject */ | ||
private $otp; | ||
|
||
/** @var Totp */ | ||
private $totp; | ||
|
||
protected function setUp() { | ||
parent::setUp(); | ||
|
||
$this->secretMapper = $this->createMock(TotpSecretMapper::class); | ||
$this->crypto = $this->createMock(ICrypto::class); | ||
$this->otp = $this->createMock(Otp::class); | ||
|
||
$this->totp = new Totp($this->secretMapper, $this->crypto, $this->otp); | ||
|
||
} | ||
|
||
/** | ||
* @dataProvider validationProvider | ||
* @param string $lastKey | ||
* @param string $key | ||
* @param boolean $validationResult | ||
* @param boolean $expectedResult | ||
*/ | ||
public function testValidateSecret($lastKey, $key, $validationResult, $expectedResult) { | ||
/** @var IUser | \PHPUnit_Framework_MockObject_MockObject $user */ | ||
$user = $this->createMock(IUser::class); | ||
$dbSecret = $this->createMock(TotpSecret::class); | ||
|
||
$dbSecret->expects($this->once()) | ||
->method('getLastValidatedKey') | ||
->will($this->returnValue($lastKey)); | ||
$this->secretMapper->expects($this->once()) | ||
->method('getSecret') | ||
->with($user) | ||
->will($this->returnValue($dbSecret)); | ||
|
||
if($lastKey !== $key) { | ||
$dbSecret->expects($this->once()) | ||
->method('getSecret') | ||
->will($this->returnValue('secret')); | ||
$this->otp->expects($this->once()) | ||
->method('checkTotp') | ||
->will($this->returnValue($validationResult)); | ||
} | ||
if($expectedResult === true) { | ||
$dbSecret->expects($this->once()) | ||
->method('setLastValidatedKey') | ||
->with($key); | ||
$this->secretMapper->expects($this->once()) | ||
->method('update') | ||
->with($dbSecret); | ||
} | ||
$this->assertEquals($this->totp->validateSecret($user, $key), $expectedResult); | ||
} | ||
|
||
public function validationProvider() { | ||
return [ | ||
[ '000000', '000000', true, false ], | ||
[ '000000', '000000', false, false ], | ||
[ '000000', '000001', false, false ], | ||
[ '000000', '000001', true, true ] | ||
]; | ||
} | ||
|
||
public function testValidateSecretNoSecret() { | ||
/** @var IUser | \PHPUnit_Framework_MockObject_MockObject $user */ | ||
$user = $this->createMock(IUser::class); | ||
|
||
$this->secretMapper->expects($this->once()) | ||
->method('getSecret') | ||
->with($user) | ||
->will($this->throwException(new DoesNotExistException(''))); | ||
$this->expectException(NoTotpSecretFoundException::class); | ||
$this->totp->validateSecret($user, 'testkey'); | ||
} | ||
|
||
} |