Files
sysPass/tests/Services/Crypt/MasterPassServiceTest.php
nuxsmin cfa2b20e8b * [ADD] Unit testing. Work in progress
* [MOD] Code refactoring
2018-07-15 20:39:11 +02:00

194 lines
6.8 KiB
PHP

<?php
/**
* sysPass
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
* sysPass is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* sysPass 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
*/
namespace SP\Tests\Services\Crypt;
use SP\Core\Crypt\Crypt;
use SP\Services\Account\AccountService;
use SP\Services\Crypt\MasterPassService;
use SP\Services\Crypt\UpdateMasterPassRequest;
use SP\Services\CustomField\CustomFieldService;
use SP\Storage\Database\DatabaseConnectionData;
use SP\Tests\DatabaseTestCase;
use SP\Tests\Services\Account\AccountCryptServiceTest;
use function SP\Tests\setupContext;
/**
* Class MasterPassServiceTest
*
* @package SP\Tests\Services
*/
class MasterPassServiceTest extends DatabaseTestCase
{
/**
* @var CustomFieldService
*/
private static $customFieldService;
/**
* @var AccountService
*/
private static $accountService;
/**
* @var MasterPassService
*/
private static $service;
/**
* @throws \DI\NotFoundException
* @throws \SP\Core\Context\ContextException
* @throws \DI\DependencyException
*/
public static function setUpBeforeClass()
{
$dic = setupContext();
self::$dataset = 'syspass_accountCrypt.xml';
// Datos de conexión a la BBDD
self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class);
// Inicializar el repositorio
self::$service = $dic->get(MasterPassService::class);
self::$accountService = $dic->get(AccountService::class);
self::$customFieldService = $dic->get(CustomFieldService::class);
}
/**
* @throws \Defuse\Crypto\Exception\CryptoException
* @throws \Exception
*/
public function testChangeMasterPassword()
{
$request = new UpdateMasterPassRequest(AccountCryptServiceTest::CURRENT_MASTERPASS, AccountCryptServiceTest::NEW_MASTERPASS, AccountCryptServiceTest::CURRENT_HASH);
self::$service->changeMasterPassword($request);
$this->checckAccounts($request);
$this->checkAccountsHistory($request);
$this->checkCustomFields();
$this->assertTrue(self::$service->checkMasterPassword(AccountCryptServiceTest::NEW_MASTERPASS));
$this->assertTrue(self::$service->checkUserUpdateMPass(time()));
$this->assertFalse(self::$service->checkUserUpdateMPass(time() - 10));
}
/**
* @param UpdateMasterPassRequest $request
*
* @throws \Defuse\Crypto\Exception\CryptoException
* @throws \SP\Core\Exceptions\ConstraintException
* @throws \SP\Core\Exceptions\QueryException
* @throws \SP\Repositories\NoSuchItemException
*/
private function checckAccounts(UpdateMasterPassRequest $request)
{
$account = self::$accountService->getPasswordForId(1);
$pass = Crypt::decrypt($account->getPass(), $account->getKey(), $request->getNewMasterPass());
$this->assertEquals('&¿\'f!i$XwSwc', $pass);
$account = self::$accountService->getPasswordForId(2);
$pass = Crypt::decrypt($account->getPass(), $account->getKey(), $request->getNewMasterPass());
$this->assertEquals('&¿\'f!i$XwSwc', $pass);
}
/**
* @param UpdateMasterPassRequest $request
*
* @throws \Defuse\Crypto\Exception\CryptoException
* @throws \SP\Core\Exceptions\ConstraintException
* @throws \SP\Core\Exceptions\QueryException
* @throws \SP\Repositories\NoSuchItemException
*/
private function checkAccountsHistory(UpdateMasterPassRequest $request)
{
// Verify accounts' password history data
$account = self::$accountService->getPasswordHistoryForId(3);
$pass = Crypt::decrypt($account->getPass(), $account->getKey(), $request->getNewMasterPass());
$this->assertEquals($request->getHash(), $account->getMPassHash());
$this->assertEquals('_{/uHL\>\'Oj0', $pass);
$account = self::$accountService->getPasswordHistoryForId(4);
$pass = Crypt::decrypt($account->getPass(), $account->getKey(), $request->getNewMasterPass());
$this->assertEquals($request->getHash(), $account->getMPassHash());
$this->assertEquals('-{?^··\mjC<c', $pass);
$account = self::$accountService->getPasswordHistoryForId(5);
$pass = Crypt::decrypt($account->getPass(), $account->getKey(), $request->getNewMasterPass());
$this->assertEquals($request->getHash(), $account->getMPassHash());
$this->assertEquals('-{?^··\mjC<c', $pass);
$account = self::$accountService->getPasswordHistoryForId(6);
$pass = Crypt::decrypt($account->getPass(), $account->getKey(), $request->getNewMasterPass());
$this->assertEquals($request->getHash(), $account->getMPassHash());
$this->assertEquals('-{?^··\mjC<c', $pass);
$account = self::$accountService->getPasswordHistoryForId(7);
$pass = Crypt::decrypt($account->getPass(), $account->getKey(), $request->getNewMasterPass());
$this->assertEquals($request->getHash(), $account->getMPassHash());
$this->assertEquals('-{?^··\mjC<c', $pass);
}
/**
* @throws \Defuse\Crypto\Exception\CryptoException
* @throws \SP\Core\Exceptions\ConstraintException
* @throws \SP\Core\Exceptions\QueryException
*/
private function checkCustomFields()
{
$result = self::$customFieldService->getAllEncrypted();
$data = Crypt::decrypt($result[0]->getData(), $result[0]->getKey(), AccountCryptServiceTest::NEW_MASTERPASS);
$this->assertEquals('1234', $data);
}
/**
* @throws \SP\Repositories\NoSuchItemException
* @throws \SP\Services\ServiceException
*/
public function testCheckUserUpdateMPass()
{
$this->assertTrue(self::$service->checkUserUpdateMPass(time()));
$this->assertFalse(self::$service->checkUserUpdateMPass(1528236611 - 10));
}
/**
* @throws \SP\Repositories\NoSuchItemException
* @throws \SP\Services\ServiceException
*/
public function testCheckMasterPassword()
{
$this->assertTrue(self::$service->checkMasterPassword(AccountCryptServiceTest::CURRENT_MASTERPASS));
$this->assertFalse(self::$service->checkMasterPassword(AccountCryptServiceTest::NEW_MASTERPASS));
}
}