2015-03-30 07:59:48 -04:00
|
|
|
<?php
|
2024-10-01 09:07:36 -04:00
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2024-05-10 09:09:14 -04:00
|
|
|
/**
|
|
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
*/
|
2015-03-30 07:59:48 -04:00
|
|
|
namespace Test\Files\Stream;
|
|
|
|
|
|
2025-05-13 03:46:15 -04:00
|
|
|
use OC\Encryption\File;
|
2025-05-15 02:48:13 -04:00
|
|
|
use OC\Encryption\Util;
|
2018-07-25 04:19:14 -04:00
|
|
|
use OC\Files\Cache\CacheEntry;
|
2025-05-13 03:46:15 -04:00
|
|
|
use OC\Files\Storage\Storage;
|
2024-10-01 09:07:36 -04:00
|
|
|
use OC\Files\Storage\Wrapper\Wrapper;
|
2025-05-15 02:48:13 -04:00
|
|
|
use OC\Files\Stream\Encryption;
|
2015-03-30 07:59:48 -04:00
|
|
|
use OC\Files\View;
|
2016-09-07 14:01:13 -04:00
|
|
|
use OC\Memcache\ArrayCache;
|
2024-10-01 09:07:36 -04:00
|
|
|
use OCP\Encryption\IEncryptionModule;
|
2019-11-20 08:22:00 -05:00
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
2018-07-25 04:19:14 -04:00
|
|
|
use OCP\Files\Cache\ICache;
|
2021-02-02 09:45:34 -05:00
|
|
|
use OCP\ICacheFactory;
|
2017-10-24 09:26:53 -04:00
|
|
|
use OCP\IConfig;
|
2024-10-01 09:07:36 -04:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2024-09-20 05:26:22 -04:00
|
|
|
use Psr\Log\LoggerInterface;
|
2015-03-30 07:59:48 -04:00
|
|
|
|
2016-05-20 09:38:20 -04:00
|
|
|
class EncryptionTest extends \Test\TestCase {
|
2022-12-07 10:27:31 -05:00
|
|
|
public const DEFAULT_WRAPPER = '\OC\Files\Stream\Encryption';
|
2015-03-30 07:59:48 -04:00
|
|
|
|
2024-10-01 09:07:36 -04:00
|
|
|
private IEncryptionModule&MockObject $encryptionModule;
|
2015-05-18 10:40:58 -04:00
|
|
|
|
2015-03-30 07:59:48 -04:00
|
|
|
/**
|
2024-10-01 09:07:36 -04:00
|
|
|
* @param class-string<Wrapper> $wrapper
|
2015-04-17 06:38:54 -04:00
|
|
|
* @return resource
|
2015-03-30 07:59:48 -04:00
|
|
|
*/
|
2024-10-01 09:07:36 -04:00
|
|
|
protected function getStream(string $fileName, string $mode, int $unencryptedSize, string $wrapper = self::DEFAULT_WRAPPER, int $unencryptedSizeOnClose = 0) {
|
2015-04-20 11:30:10 -04:00
|
|
|
clearstatcache();
|
2015-04-02 10:16:06 -04:00
|
|
|
$size = filesize($fileName);
|
2015-04-02 05:07:07 -04:00
|
|
|
$source = fopen($fileName, $mode);
|
|
|
|
|
$internalPath = $fileName;
|
|
|
|
|
$fullPath = $fileName;
|
2015-03-30 07:59:48 -04:00
|
|
|
$header = [];
|
|
|
|
|
$uid = '';
|
2015-05-18 10:40:58 -04:00
|
|
|
$this->encryptionModule = $this->buildMockModule();
|
2018-07-25 04:19:14 -04:00
|
|
|
$cache = $this->createMock(ICache::class);
|
2015-03-30 07:59:48 -04:00
|
|
|
$storage = $this->getMockBuilder('\OC\Files\Storage\Storage')
|
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
|
$encStorage = $this->getMockBuilder('\OC\Files\Storage\Wrapper\Encryption')
|
|
|
|
|
->disableOriginalConstructor()->getMock();
|
2017-10-24 09:26:53 -04:00
|
|
|
$config = $this->getMockBuilder(IConfig::class)
|
2015-03-30 17:19:35 -04:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
2016-09-07 14:01:13 -04:00
|
|
|
$arrayCache = $this->createMock(ArrayCache::class);
|
2015-04-15 07:19:17 -04:00
|
|
|
$groupManager = $this->getMockBuilder('\OC\Group\Manager')
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
2015-04-02 05:07:07 -04:00
|
|
|
$file = $this->getMockBuilder('\OC\Encryption\File')
|
|
|
|
|
->disableOriginalConstructor()
|
2025-05-13 03:46:15 -04:00
|
|
|
->onlyMethods(['getAccessList'])
|
2015-04-02 05:07:07 -04:00
|
|
|
->getMock();
|
2015-04-09 11:45:57 -04:00
|
|
|
$file->expects($this->any())->method('getAccessList')->willReturn([]);
|
2016-09-07 14:01:13 -04:00
|
|
|
$util = $this->getMockBuilder('\OC\Encryption\Util')
|
2025-05-13 03:46:15 -04:00
|
|
|
->onlyMethods(['getUidAndFilename'])
|
2021-02-02 09:45:34 -05:00
|
|
|
->setConstructorArgs([new View(), new \OC\User\Manager(
|
|
|
|
|
$config,
|
|
|
|
|
$this->createMock(ICacheFactory::class),
|
2024-09-20 05:26:22 -04:00
|
|
|
$this->createMock(IEventDispatcher::class),
|
|
|
|
|
$this->createMock(LoggerInterface::class),
|
2021-02-02 09:45:34 -05:00
|
|
|
), $groupManager, $config, $arrayCache])
|
2016-09-07 14:01:13 -04:00
|
|
|
->getMock();
|
2015-04-02 05:07:07 -04:00
|
|
|
$util->expects($this->any())
|
|
|
|
|
->method('getUidAndFilename')
|
|
|
|
|
->willReturn(['user1', $internalPath]);
|
2018-07-25 04:19:14 -04:00
|
|
|
$storage->expects($this->any())->method('getCache')->willReturn($cache);
|
|
|
|
|
$entry = new CacheEntry([
|
|
|
|
|
'fileid' => 5,
|
|
|
|
|
'encryptedVersion' => 2,
|
2022-12-07 10:27:31 -05:00
|
|
|
'unencrypted_size' => $unencryptedSizeOnClose,
|
2018-07-25 04:19:14 -04:00
|
|
|
]);
|
2020-04-09 10:07:47 -04:00
|
|
|
$cache->expects($this->any())->method('get')->willReturn($entry);
|
2022-12-07 10:27:31 -05:00
|
|
|
$cache->expects($this->any())->method('update')->with(5, ['encrypted' => 3, 'encryptedVersion' => 3, 'unencrypted_size' => $unencryptedSizeOnClose]);
|
2015-03-30 07:59:48 -04:00
|
|
|
|
2024-10-01 09:07:36 -04:00
|
|
|
return $wrapper::wrap(
|
|
|
|
|
$source,
|
|
|
|
|
$internalPath,
|
|
|
|
|
$fullPath,
|
|
|
|
|
$header,
|
|
|
|
|
$uid,
|
|
|
|
|
$this->encryptionModule,
|
|
|
|
|
$storage,
|
|
|
|
|
$encStorage,
|
|
|
|
|
$util,
|
|
|
|
|
$file,
|
|
|
|
|
$mode,
|
|
|
|
|
$size,
|
|
|
|
|
$unencryptedSize,
|
|
|
|
|
8192,
|
|
|
|
|
true,
|
|
|
|
|
$wrapper,
|
|
|
|
|
);
|
2015-03-30 07:59:48 -04:00
|
|
|
}
|
|
|
|
|
|
2025-06-30 10:56:59 -04:00
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('dataProviderStreamOpen')]
|
2024-10-01 09:07:36 -04:00
|
|
|
public function testStreamOpen(
|
|
|
|
|
$isMasterKeyUsed,
|
2023-11-23 04:22:34 -05:00
|
|
|
$mode,
|
|
|
|
|
$fullPath,
|
|
|
|
|
$fileExists,
|
|
|
|
|
$expectedSharePath,
|
|
|
|
|
$expectedSize,
|
|
|
|
|
$expectedUnencryptedSize,
|
2024-10-01 09:07:36 -04:00
|
|
|
$expectedReadOnly,
|
|
|
|
|
): void {
|
2015-04-17 06:38:54 -04:00
|
|
|
// build mocks
|
2025-05-13 03:46:15 -04:00
|
|
|
$encryptionModuleMock = $this->createMock(IEncryptionModule::class);
|
2017-05-31 09:26:10 -04:00
|
|
|
$encryptionModuleMock->expects($this->any())->method('needDetailedAccessList')->willReturn(!$isMasterKeyUsed);
|
2015-04-17 06:38:54 -04:00
|
|
|
$encryptionModuleMock->expects($this->once())
|
|
|
|
|
->method('getUnencryptedBlockSize')->willReturn(99);
|
|
|
|
|
$encryptionModuleMock->expects($this->once())
|
2024-10-01 09:07:36 -04:00
|
|
|
->method('begin')->willReturn([]);
|
2015-04-17 06:38:54 -04:00
|
|
|
|
2025-05-13 03:46:15 -04:00
|
|
|
$storageMock = $this->createMock(Storage::class);
|
2015-04-17 06:38:54 -04:00
|
|
|
$storageMock->expects($this->once())->method('file_exists')->willReturn($fileExists);
|
|
|
|
|
|
2025-05-13 03:46:15 -04:00
|
|
|
$fileMock = $this->createMock(File::class);
|
2017-05-31 09:26:10 -04:00
|
|
|
if ($isMasterKeyUsed) {
|
|
|
|
|
$fileMock->expects($this->never())->method('getAccessList');
|
|
|
|
|
} else {
|
|
|
|
|
$fileMock->expects($this->once())->method('getAccessList')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturnCallback(function ($sharePath) use ($expectedSharePath) {
|
2017-05-31 09:26:10 -04:00
|
|
|
$this->assertSame($expectedSharePath, $sharePath);
|
2020-03-26 04:30:18 -04:00
|
|
|
return [];
|
2020-03-25 17:21:27 -04:00
|
|
|
});
|
2017-05-31 09:26:10 -04:00
|
|
|
}
|
2025-05-13 03:46:15 -04:00
|
|
|
$utilMock = $this->getMockBuilder(Util::class)
|
2015-04-24 10:47:27 -04:00
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
|
$utilMock->expects($this->any())
|
|
|
|
|
->method('getHeaderSize')
|
|
|
|
|
->willReturn(8192);
|
|
|
|
|
|
2015-04-17 06:38:54 -04:00
|
|
|
// get a instance of the stream wrapper
|
2025-05-13 03:46:15 -04:00
|
|
|
$streamWrapper = $this->getMockBuilder(Encryption::class)
|
|
|
|
|
->onlyMethods(['loadContext', 'writeHeader', 'skipHeader'])
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
2015-04-17 06:38:54 -04:00
|
|
|
|
|
|
|
|
// set internal properties of the stream wrapper
|
2025-05-13 03:46:15 -04:00
|
|
|
$stream = new \ReflectionClass(Encryption::class);
|
2015-04-17 06:38:54 -04:00
|
|
|
$encryptionModule = $stream->getProperty('encryptionModule');
|
|
|
|
|
$encryptionModule->setAccessible(true);
|
|
|
|
|
$encryptionModule->setValue($streamWrapper, $encryptionModuleMock);
|
|
|
|
|
$encryptionModule->setAccessible(false);
|
|
|
|
|
$storage = $stream->getProperty('storage');
|
|
|
|
|
$storage->setAccessible(true);
|
|
|
|
|
$storage->setValue($streamWrapper, $storageMock);
|
|
|
|
|
$storage->setAccessible(false);
|
|
|
|
|
$file = $stream->getProperty('file');
|
|
|
|
|
$file->setAccessible(true);
|
|
|
|
|
$file->setValue($streamWrapper, $fileMock);
|
|
|
|
|
$file->setAccessible(false);
|
2015-04-24 10:47:27 -04:00
|
|
|
$util = $stream->getProperty('util');
|
|
|
|
|
$util->setAccessible(true);
|
|
|
|
|
$util->setValue($streamWrapper, $utilMock);
|
|
|
|
|
$util->setAccessible(false);
|
2015-04-17 06:38:54 -04:00
|
|
|
$fullPathP = $stream->getProperty('fullPath');
|
|
|
|
|
$fullPathP->setAccessible(true);
|
|
|
|
|
$fullPathP->setValue($streamWrapper, $fullPath);
|
|
|
|
|
$fullPathP->setAccessible(false);
|
|
|
|
|
$header = $stream->getProperty('header');
|
|
|
|
|
$header->setAccessible(true);
|
2020-03-26 04:30:18 -04:00
|
|
|
$header->setValue($streamWrapper, []);
|
2015-04-17 06:38:54 -04:00
|
|
|
$header->setAccessible(false);
|
2016-01-05 13:01:03 -05:00
|
|
|
$this->invokePrivate($streamWrapper, 'signed', [true]);
|
2024-10-01 09:07:36 -04:00
|
|
|
$this->invokePrivate($streamWrapper, 'internalPath', [$fullPath]);
|
|
|
|
|
$this->invokePrivate($streamWrapper, 'uid', ['test']);
|
2015-04-17 06:38:54 -04:00
|
|
|
|
|
|
|
|
// call stream_open, that's the method we want to test
|
|
|
|
|
$dummyVar = 'foo';
|
|
|
|
|
$streamWrapper->stream_open('', $mode, '', $dummyVar);
|
|
|
|
|
|
|
|
|
|
// check internal properties
|
|
|
|
|
$size = $stream->getProperty('size');
|
|
|
|
|
$size->setAccessible(true);
|
2024-10-01 09:07:36 -04:00
|
|
|
$this->assertSame($expectedSize, $size->getValue($streamWrapper));
|
2015-04-17 06:38:54 -04:00
|
|
|
$size->setAccessible(false);
|
|
|
|
|
|
|
|
|
|
$unencryptedSize = $stream->getProperty('unencryptedSize');
|
|
|
|
|
$unencryptedSize->setAccessible(true);
|
2024-10-01 09:07:36 -04:00
|
|
|
$this->assertSame($expectedUnencryptedSize, $unencryptedSize->getValue($streamWrapper));
|
2015-04-17 06:38:54 -04:00
|
|
|
$unencryptedSize->setAccessible(false);
|
|
|
|
|
|
|
|
|
|
$readOnly = $stream->getProperty('readOnly');
|
|
|
|
|
$readOnly->setAccessible(true);
|
2024-10-01 09:07:36 -04:00
|
|
|
$this->assertSame($expectedReadOnly, $readOnly->getValue($streamWrapper));
|
2015-04-17 06:38:54 -04:00
|
|
|
$readOnly->setAccessible(false);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-13 03:46:15 -04:00
|
|
|
public static function dataProviderStreamOpen(): array {
|
2017-05-31 09:26:10 -04:00
|
|
|
return [
|
|
|
|
|
[false, 'r', '/foo/bar/test.txt', true, '/foo/bar/test.txt', null, null, true],
|
|
|
|
|
[false, 'r', '/foo/bar/test.txt', false, '/foo/bar', null, null, true],
|
|
|
|
|
[false, 'w', '/foo/bar/test.txt', true, '/foo/bar/test.txt', 8192, 0, false],
|
|
|
|
|
[true, 'r', '/foo/bar/test.txt', true, '/foo/bar/test.txt', null, null, true],
|
|
|
|
|
[true, 'r', '/foo/bar/test.txt', false, '/foo/bar', null, null, true],
|
|
|
|
|
[true, 'w', '/foo/bar/test.txt', true, '/foo/bar/test.txt', 8192, 0, false],
|
|
|
|
|
];
|
2015-04-17 06:38:54 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testWriteRead(): void {
|
2024-08-23 09:10:27 -04:00
|
|
|
$fileName = tempnam('/tmp', 'FOO');
|
2022-12-07 10:27:31 -05:00
|
|
|
$stream = $this->getStream($fileName, 'w+', 0, self::DEFAULT_WRAPPER, 6);
|
2015-03-30 07:59:48 -04:00
|
|
|
$this->assertEquals(6, fwrite($stream, 'foobar'));
|
2015-04-02 05:07:07 -04:00
|
|
|
fclose($stream);
|
|
|
|
|
|
2015-04-02 10:16:06 -04:00
|
|
|
$stream = $this->getStream($fileName, 'r', 6);
|
2015-03-30 07:59:48 -04:00
|
|
|
$this->assertEquals('foobar', fread($stream, 100));
|
2015-04-02 05:07:07 -04:00
|
|
|
fclose($stream);
|
2015-04-23 07:42:18 -04:00
|
|
|
|
2022-12-07 10:27:31 -05:00
|
|
|
$stream = $this->getStream($fileName, 'r+', 6, self::DEFAULT_WRAPPER, 6);
|
2015-04-22 07:06:02 -04:00
|
|
|
$this->assertEquals(3, fwrite($stream, 'bar'));
|
|
|
|
|
fclose($stream);
|
|
|
|
|
|
|
|
|
|
$stream = $this->getStream($fileName, 'r', 6);
|
|
|
|
|
$this->assertEquals('barbar', fread($stream, 100));
|
|
|
|
|
fclose($stream);
|
2015-05-18 05:32:29 -04:00
|
|
|
|
|
|
|
|
unlink($fileName);
|
2015-04-22 07:06:02 -04:00
|
|
|
}
|
2015-04-21 11:36:25 -04:00
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testRewind(): void {
|
2024-08-23 09:10:27 -04:00
|
|
|
$fileName = tempnam('/tmp', 'FOO');
|
2022-12-07 10:27:31 -05:00
|
|
|
$stream = $this->getStream($fileName, 'w+', 0, self::DEFAULT_WRAPPER, 6);
|
2015-04-22 06:52:01 -04:00
|
|
|
$this->assertEquals(6, fwrite($stream, 'foobar'));
|
2020-03-25 16:53:04 -04:00
|
|
|
$this->assertEquals(true, rewind($stream));
|
2015-04-22 06:52:01 -04:00
|
|
|
$this->assertEquals('foobar', fread($stream, 100));
|
2020-03-25 16:53:04 -04:00
|
|
|
$this->assertEquals(true, rewind($stream));
|
2015-04-22 06:52:01 -04:00
|
|
|
$this->assertEquals(3, fwrite($stream, 'bar'));
|
|
|
|
|
fclose($stream);
|
|
|
|
|
|
|
|
|
|
$stream = $this->getStream($fileName, 'r', 6);
|
|
|
|
|
$this->assertEquals('barbar', fread($stream, 100));
|
|
|
|
|
fclose($stream);
|
2017-05-31 09:26:10 -04:00
|
|
|
|
2015-05-18 05:32:29 -04:00
|
|
|
unlink($fileName);
|
2020-04-10 08:19:56 -04:00
|
|
|
}
|
2015-04-21 11:36:25 -04:00
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testSeek(): void {
|
2024-08-23 09:10:27 -04:00
|
|
|
$fileName = tempnam('/tmp', 'FOO');
|
2018-07-25 04:19:14 -04:00
|
|
|
|
2022-12-07 10:27:31 -05:00
|
|
|
$stream = $this->getStream($fileName, 'w+', 0, self::DEFAULT_WRAPPER, 9);
|
2015-04-02 06:48:41 -04:00
|
|
|
$this->assertEquals(6, fwrite($stream, 'foobar'));
|
|
|
|
|
$this->assertEquals(0, fseek($stream, 3));
|
|
|
|
|
$this->assertEquals(6, fwrite($stream, 'foobar'));
|
|
|
|
|
fclose($stream);
|
|
|
|
|
|
2015-04-02 10:16:06 -04:00
|
|
|
$stream = $this->getStream($fileName, 'r', 9);
|
2015-04-02 06:48:41 -04:00
|
|
|
$this->assertEquals('foofoobar', fread($stream, 100));
|
2015-05-18 05:32:29 -04:00
|
|
|
$this->assertEquals(-1, fseek($stream, 10));
|
|
|
|
|
$this->assertEquals(0, fseek($stream, 9));
|
2015-05-18 05:55:48 -04:00
|
|
|
$this->assertEquals(-1, fseek($stream, -10, SEEK_CUR));
|
|
|
|
|
$this->assertEquals(0, fseek($stream, -9, SEEK_CUR));
|
|
|
|
|
$this->assertEquals(-1, fseek($stream, -10, SEEK_END));
|
|
|
|
|
$this->assertEquals(0, fseek($stream, -9, SEEK_END));
|
2015-04-02 06:48:41 -04:00
|
|
|
fclose($stream);
|
2015-04-23 07:42:18 -04:00
|
|
|
|
|
|
|
|
unlink($fileName);
|
2015-04-02 06:48:41 -04:00
|
|
|
}
|
|
|
|
|
|
2025-05-13 03:46:15 -04:00
|
|
|
public static function dataFilesProvider(): array {
|
2015-04-20 11:30:10 -04:00
|
|
|
return [
|
|
|
|
|
['lorem-big.txt'],
|
|
|
|
|
['block-aligned.txt'],
|
|
|
|
|
['block-aligned-plus-one.txt'],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-30 10:56:59 -04:00
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('dataFilesProvider')]
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testWriteReadBigFile($testFile): void {
|
2015-04-20 11:30:10 -04:00
|
|
|
$expectedData = file_get_contents(\OC::$SERVERROOT . '/tests/data/' . $testFile);
|
2015-04-02 09:19:09 -04:00
|
|
|
// write it
|
2024-08-23 09:10:27 -04:00
|
|
|
$fileName = tempnam('/tmp', 'FOO');
|
2022-12-07 10:27:31 -05:00
|
|
|
$stream = $this->getStream($fileName, 'w+', 0, self::DEFAULT_WRAPPER, strlen($expectedData));
|
2015-05-18 10:40:58 -04:00
|
|
|
// while writing the file from the beginning to the end we should never try
|
|
|
|
|
// to read parts of the file. This should only happen for write operations
|
|
|
|
|
// in the middle of a file
|
|
|
|
|
$this->encryptionModule->expects($this->never())->method('decrypt');
|
2015-04-02 09:19:09 -04:00
|
|
|
fwrite($stream, $expectedData);
|
|
|
|
|
fclose($stream);
|
|
|
|
|
|
|
|
|
|
// read it all
|
2015-04-02 10:16:06 -04:00
|
|
|
$stream = $this->getStream($fileName, 'r', strlen($expectedData));
|
2015-04-02 09:19:09 -04:00
|
|
|
$data = stream_get_contents($stream);
|
|
|
|
|
fclose($stream);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals($expectedData, $data);
|
2015-04-20 11:30:10 -04:00
|
|
|
|
|
|
|
|
// another read test with a loop like we do in several places:
|
|
|
|
|
$stream = $this->getStream($fileName, 'r', strlen($expectedData));
|
|
|
|
|
$data = '';
|
|
|
|
|
while (!feof($stream)) {
|
|
|
|
|
$data .= fread($stream, 8192);
|
|
|
|
|
}
|
|
|
|
|
fclose($stream);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals($expectedData, $data);
|
2015-04-23 07:42:18 -04:00
|
|
|
|
|
|
|
|
unlink($fileName);
|
2015-04-02 09:19:09 -04:00
|
|
|
}
|
|
|
|
|
|
2015-05-20 08:29:20 -04:00
|
|
|
/**
|
|
|
|
|
* simulate a non-seekable storage
|
|
|
|
|
*/
|
2025-06-30 10:56:59 -04:00
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('dataFilesProvider')]
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testWriteToNonSeekableStorage($testFile): void {
|
2025-05-13 03:46:15 -04:00
|
|
|
$wrapper = $this->getMockBuilder(Encryption::class)
|
|
|
|
|
->onlyMethods(['parentStreamSeek'])
|
|
|
|
|
->getMock();
|
|
|
|
|
$wrapper->expects($this->any())
|
|
|
|
|
->method('parentStreamSeek')
|
|
|
|
|
->willReturn(false);
|
2015-05-20 08:29:20 -04:00
|
|
|
|
|
|
|
|
$expectedData = file_get_contents(\OC::$SERVERROOT . '/tests/data/' . $testFile);
|
|
|
|
|
// write it
|
2024-08-23 09:10:27 -04:00
|
|
|
$fileName = tempnam('/tmp', 'FOO');
|
2022-12-07 10:27:31 -05:00
|
|
|
$stream = $this->getStream($fileName, 'w+', 0, '\Test\Files\Stream\DummyEncryptionWrapper', strlen($expectedData));
|
2015-05-20 08:29:20 -04:00
|
|
|
// while writing the file from the beginning to the end we should never try
|
|
|
|
|
// to read parts of the file. This should only happen for write operations
|
|
|
|
|
// in the middle of a file
|
|
|
|
|
$this->encryptionModule->expects($this->never())->method('decrypt');
|
|
|
|
|
fwrite($stream, $expectedData);
|
|
|
|
|
fclose($stream);
|
|
|
|
|
|
|
|
|
|
// read it all
|
2022-12-07 10:27:31 -05:00
|
|
|
$stream = $this->getStream($fileName, 'r', strlen($expectedData), '\Test\Files\Stream\DummyEncryptionWrapper', strlen($expectedData));
|
2015-05-20 08:29:20 -04:00
|
|
|
$data = stream_get_contents($stream);
|
|
|
|
|
fclose($stream);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals($expectedData, $data);
|
|
|
|
|
|
|
|
|
|
// another read test with a loop like we do in several places:
|
|
|
|
|
$stream = $this->getStream($fileName, 'r', strlen($expectedData));
|
|
|
|
|
$data = '';
|
|
|
|
|
while (!feof($stream)) {
|
|
|
|
|
$data .= fread($stream, 8192);
|
|
|
|
|
}
|
|
|
|
|
fclose($stream);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals($expectedData, $data);
|
|
|
|
|
|
|
|
|
|
unlink($fileName);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 09:07:36 -04:00
|
|
|
protected function buildMockModule(): IEncryptionModule&MockObject {
|
2025-05-13 03:46:15 -04:00
|
|
|
$encryptionModule = $this->getMockBuilder(IEncryptionModule::class)
|
2015-04-02 05:07:07 -04:00
|
|
|
->disableOriginalConstructor()
|
2025-05-13 03:46:15 -04:00
|
|
|
->onlyMethods(['getId', 'getDisplayName', 'begin', 'end', 'encrypt', 'decrypt', 'update', 'shouldEncrypt', 'getUnencryptedBlockSize', 'isReadable', 'encryptAll', 'prepareDecryptAll', 'isReadyForUser', 'needDetailedAccessList'])
|
2015-04-02 05:07:07 -04:00
|
|
|
->getMock();
|
|
|
|
|
|
|
|
|
|
$encryptionModule->expects($this->any())->method('getId')->willReturn('UNIT_TEST_MODULE');
|
|
|
|
|
$encryptionModule->expects($this->any())->method('getDisplayName')->willReturn('Unit test module');
|
|
|
|
|
$encryptionModule->expects($this->any())->method('begin')->willReturn([]);
|
|
|
|
|
$encryptionModule->expects($this->any())->method('end')->willReturn('');
|
2015-05-12 12:49:25 -04:00
|
|
|
$encryptionModule->expects($this->any())->method('isReadable')->willReturn(true);
|
2017-05-31 09:26:10 -04:00
|
|
|
$encryptionModule->expects($this->any())->method('needDetailedAccessList')->willReturn(false);
|
2020-04-09 07:53:40 -04:00
|
|
|
$encryptionModule->expects($this->any())->method('encrypt')->willReturnCallback(function ($data) {
|
2015-04-20 11:30:10 -04:00
|
|
|
// simulate different block size by adding some padding to the data
|
|
|
|
|
if (isset($data[6125])) {
|
|
|
|
|
return str_pad($data, 8192, 'X');
|
|
|
|
|
}
|
|
|
|
|
// last block
|
|
|
|
|
return $data;
|
|
|
|
|
});
|
2020-04-09 07:53:40 -04:00
|
|
|
$encryptionModule->expects($this->any())->method('decrypt')->willReturnCallback(function ($data) {
|
2015-04-20 11:30:10 -04:00
|
|
|
if (isset($data[8191])) {
|
|
|
|
|
return substr($data, 0, 6126);
|
|
|
|
|
}
|
|
|
|
|
// last block
|
|
|
|
|
return $data;
|
|
|
|
|
});
|
2015-04-02 05:07:07 -04:00
|
|
|
$encryptionModule->expects($this->any())->method('update')->willReturn(true);
|
|
|
|
|
$encryptionModule->expects($this->any())->method('shouldEncrypt')->willReturn(true);
|
2015-04-20 11:30:10 -04:00
|
|
|
$encryptionModule->expects($this->any())->method('getUnencryptedBlockSize')->willReturn(6126);
|
2015-04-02 05:07:07 -04:00
|
|
|
return $encryptionModule;
|
2015-03-30 07:59:48 -04:00
|
|
|
}
|
|
|
|
|
}
|