Files
sysPass/tests/SP/Core/Crypt/SessionTest.php
2024-05-13 07:52:32 +02:00

141 lines
3.7 KiB
PHP

<?php
declare(strict_types=1);
/*
* sysPass
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, 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\Core\Crypt;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\MockObject\MockObject;
use SP\Core\Crypt\Session;
use SP\Domain\Core\Context\SessionContext;
use SP\Domain\Core\Crypt\VaultInterface;
use SP\Domain\Core\Exceptions\CryptException;
use SP\Domain\Core\Exceptions\SPException;
use SP\Tests\UnitaryTestCase;
/**
* Class SessionTest
*
*/
#[Group('unitary')]
class SessionTest extends UnitaryTestCase
{
private SessionContext|MockObject $sessionContext;
/**
* @throws Exception
* @throws CryptException
*/
public function testGetSessionKey()
{
$sidStartTime = self::$faker->unixTime;
$key = sha1(session_id() . $sidStartTime);
$vault = $this->createMock(VaultInterface::class);
$vault->expects(self::once())
->method('getData')
->with($key)
->willReturn('data');
$this->sessionContext
->expects(self::once())
->method('getVault')
->willReturn($vault);
$this->sessionContext
->expects(self::once())
->method('getSidStartTime')
->willReturn($sidStartTime);
self::assertEquals('data', Session::getSessionKey($this->sessionContext));
}
/**
* @throws CryptException
* @throws Exception
* @throws SPException
*/
public function testReKey()
{
$sidStartTime = self::$faker->unixTime;
$key = sha1(session_id() . $sidStartTime);
$vault = $this->createMock(VaultInterface::class);
$vault->expects(self::once())
->method('reKey')
->with(self::anything(), $key);
$this->sessionContext
->expects(self::once())
->method('getVault')
->willReturn($vault);
$this->sessionContext
->expects(self::once())
->method('getSidStartTime')
->willReturn($sidStartTime);
$this->sessionContext
->expects(self::once())
->method('setSidStartTime')
->with(self::anything());
$this->sessionContext
->expects(self::once())
->method('setVault');
Session::reKey($this->sessionContext);
}
/**
* @throws CryptException
*/
public function testSaveSessionKey()
{
$this->sessionContext
->expects(self::once())
->method('getSidStartTime')
->willReturn(self::$faker->unixTime);
$this->sessionContext
->expects(self::once())
->method('setVault');
Session::saveSessionKey(self::$faker->name, $this->sessionContext);
}
protected function setUp(): void
{
parent::setUp();
$this->sessionContext = $this->createMock(SessionContext::class);
}
}