2015-07-20 06:59:04 -04:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2015-07-20 06:59:04 -04:00
|
|
|
/**
|
2024-05-10 09:09:14 -04:00
|
|
|
* SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2015-07-20 06:59:04 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace Test\Session;
|
|
|
|
|
|
|
|
|
|
use OC\Session\CryptoSessionData;
|
2017-10-26 07:46:16 -04:00
|
|
|
use OCP\ISession;
|
2025-06-30 10:56:59 -04:00
|
|
|
use OCP\Security\ICrypto;
|
2015-07-20 06:59:04 -04:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
|
|
class CryptoWrappingTest extends TestCase {
|
2025-06-30 10:56:59 -04:00
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject|ICrypto */
|
2015-07-20 06:59:04 -04:00
|
|
|
protected $crypto;
|
|
|
|
|
|
2025-06-30 10:56:59 -04:00
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject|ISession */
|
2015-07-20 06:59:04 -04:00
|
|
|
protected $wrappedSession;
|
|
|
|
|
|
|
|
|
|
/** @var \OC\Session\CryptoSessionData */
|
|
|
|
|
protected $instance;
|
|
|
|
|
|
2019-11-21 10:40:38 -05:00
|
|
|
protected function setUp(): void {
|
2015-07-20 06:59:04 -04:00
|
|
|
parent::setUp();
|
|
|
|
|
|
2017-10-26 07:46:16 -04:00
|
|
|
$this->wrappedSession = $this->getMockBuilder(ISession::class)
|
2015-07-20 06:59:04 -04:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
|
|
|
|
$this->crypto = $this->getMockBuilder('OCP\Security\ICrypto')
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
|
|
|
|
$this->crypto->expects($this->any())
|
|
|
|
|
->method('encrypt')
|
|
|
|
|
->willReturnCallback(function ($input) {
|
2015-08-21 12:27:52 -04:00
|
|
|
return $input;
|
2015-07-20 06:59:04 -04:00
|
|
|
});
|
|
|
|
|
$this->crypto->expects($this->any())
|
|
|
|
|
->method('decrypt')
|
|
|
|
|
->willReturnCallback(function ($input) {
|
2019-11-22 03:52:58 -05:00
|
|
|
if ($input === '') {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
2015-07-20 06:59:04 -04:00
|
|
|
return substr($input, 1, -1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->instance = new CryptoSessionData($this->wrappedSession, $this->crypto, 'PASS');
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testUnwrappingGet(): void {
|
2015-07-20 06:59:04 -04:00
|
|
|
$unencryptedValue = 'foobar';
|
|
|
|
|
$encryptedValue = $this->crypto->encrypt($unencryptedValue);
|
|
|
|
|
|
|
|
|
|
$this->wrappedSession->expects($this->once())
|
|
|
|
|
->method('get')
|
2015-09-08 15:16:11 -04:00
|
|
|
->with('encrypted_session_data')
|
2015-07-20 06:59:04 -04:00
|
|
|
->willReturnCallback(function () use ($encryptedValue) {
|
|
|
|
|
return $encryptedValue;
|
|
|
|
|
});
|
2015-08-21 12:27:52 -04:00
|
|
|
|
2015-09-08 15:16:11 -04:00
|
|
|
$this->assertSame($unencryptedValue, $this->wrappedSession->get('encrypted_session_data'));
|
2015-07-20 06:59:04 -04:00
|
|
|
}
|
|
|
|
|
}
|