aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/Files/Mount/ObjectStorePreviewCacheMountProviderTest.php
blob: ef2ede729cea8f7257e9650d41f87bb277e58dd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php

declare(strict_types=1);
/**
 * @copyright Copyright (c) 2020, Morris Jobke <hey@morrisjobke.de>
 *
 * @author Morris Jobke <hey@morrisjobke.de>
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * 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
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */

namespace Test\Files\Mount;

use OC\Files\Mount\ObjectStorePreviewCacheMountProvider;
use OC\Files\ObjectStore\S3;
use OC\Files\Storage\StorageFactory;
use OCP\Files\Storage\IStorageFactory;
use OCP\IConfig;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;

/**
 * @group DB
 *
 * The DB permission is needed for the fake root storage initialization
 */
class ObjectStorePreviewCacheMountProviderTest extends \Test\TestCase {

	/** @var ObjectStorePreviewCacheMountProvider */
	protected $provider;

	/** @var LoggerInterface|MockObject */
	protected $logger;
	/** @var IConfig|MockObject */
	protected $config;
	/** @var IStorageFactory|MockObject */
	protected $loader;


	protected function setUp(): void {
		parent::setUp();

		$this->logger = $this->createMock(LoggerInterface::class);
		$this->config = $this->createMock(IConfig::class);
		$this->loader = $this->createMock(StorageFactory::class);

		$this->provider = new ObjectStorePreviewCacheMountProvider($this->logger, $this->config);
	}

	public function testNoMultibucketObjectStorage() {
		$this->config->expects($this->once())
			->method('getSystemValue')
			->with('objectstore_multibucket')
			->willReturn(null);

		$this->assertEquals([], $this->provider->getRootMounts($this->loader));
	}

	public function testMultibucketObjectStorage() {
		$objectstoreConfig = [
			'class' => S3::class,
			'arguments' => [
				'bucket' => 'abc',
				'num_buckets' => 64,
				'key' => 'KEY',
				'secret' => 'SECRET',
				'hostname' => 'IP',
				'port' => 'PORT',
				'use_ssl' => false,
				'use_path_style' => true,
			],
		];
		$this->config->expects($this->any())
			->method('getSystemValue')
			->willReturnCallback(function ($config) use ($objectstoreConfig) {
				if ($config === 'objectstore_multibucket') {
					return $objectstoreConfig;
				} elseif ($config === 'objectstore.multibucket.preview-distribution') {
					return true;
				}
				return null;
			});
		$this->config->expects($this->once())
			->method('getSystemValueString')
			->with('instanceid')
			->willReturn('INSTANCEID');

		$mounts = $this->provider->getRootMounts($this->loader);

		// 256 mounts for the subfolders and 1 for the fake root
		$this->assertCount(257, $mounts);

		// do some sanity checks if they have correct mount point paths
		$this->assertEquals('/appdata_INSTANCEID/preview/0/0/', $mounts[0]->getMountPoint());
		$this->assertEquals('/appdata_INSTANCEID/preview/2/5/', $mounts[37]->getMountPoint());
		// also test the path of the fake bucket
		$this->assertEquals('/appdata_INSTANCEID/preview/old-multibucket/', $mounts[256]->getMountPoint());
	}
}
\PHPUnit\Framework\MockObject\MockObject | View */ protected $view; /** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Input\InputInterface */ protected $inputInterface; /** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Output\OutputInterface */ protected $outputInterface; /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\UserInterface */ protected $userInterface; /** @var DecryptAll */ protected $instance; protected function setUp(): void { parent::setUp(); $this->userManager = $this->getMockBuilder(IUserManager::class) ->disableOriginalConstructor()->getMock(); $this->encryptionManager = $this->getMockBuilder('OC\Encryption\Manager') ->disableOriginalConstructor()->getMock(); $this->view = $this->getMockBuilder(View::class) ->disableOriginalConstructor()->getMock(); $this->inputInterface = $this->getMockBuilder(InputInterface::class) ->disableOriginalConstructor()->getMock(); $this->outputInterface = $this->getMockBuilder(OutputInterface::class) ->disableOriginalConstructor()->getMock(); $this->userInterface = $this->getMockBuilder(UserInterface::class) ->disableOriginalConstructor()->getMock(); /* We need format method to return a string */ $outputFormatter = $this->createMock(OutputFormatterInterface::class); $outputFormatter->method('format')->willReturn('foo'); $this->outputInterface->expects($this->any())->method('getFormatter') ->willReturn($outputFormatter); $this->instance = new DecryptAll($this->encryptionManager, $this->userManager, $this->view); $this->invokePrivate($this->instance, 'input', [$this->inputInterface]); $this->invokePrivate($this->instance, 'output', [$this->outputInterface]); } public function dataDecryptAll() { return [ [true, 'user1', true], [false, 'user1', true], [true, '0', true], [false, '0', true], [true, '', false], ]; } /** * @dataProvider dataDecryptAll * @param bool $prepareResult * @param string $user * @param bool $userExistsChecked */ public function testDecryptAll($prepareResult, $user, $userExistsChecked) { if ($userExistsChecked) { $this->userManager->expects($this->once())->method('userExists')->willReturn(true); } else { $this->userManager->expects($this->never())->method('userExists'); } /** @var DecryptAll | \PHPUnit\Framework\MockObject\MockObject | $instance */ $instance = $this->getMockBuilder('OC\Encryption\DecryptAll') ->setConstructorArgs( [ $this->encryptionManager, $this->userManager, $this->view ] ) ->setMethods(['prepareEncryptionModules', 'decryptAllUsersFiles']) ->getMock(); $instance->expects($this->once()) ->method('prepareEncryptionModules') ->with($user) ->willReturn($prepareResult); if ($prepareResult) { $instance->expects($this->once()) ->method('decryptAllUsersFiles') ->with($user); } else { $instance->expects($this->never())->method('decryptAllUsersFiles'); } $instance->decryptAll($this->inputInterface, $this->outputInterface, $user); } /** * test decrypt all call with a user who doesn't exists */ public function testDecryptAllWrongUser() { $this->userManager->expects($this->once())->method('userExists')->willReturn(false); $this->outputInterface->expects($this->once())->method('writeln') ->with('User "user1" does not exist. Please check the username and try again'); $this->assertFalse( $this->instance->decryptAll($this->inputInterface, $this->outputInterface, 'user1') ); } public function dataTrueFalse() { return [ [true], [false], ]; } /** * @dataProvider dataTrueFalse * @param bool $success */ public function testPrepareEncryptionModules($success) { $user = 'user1'; $dummyEncryptionModule = $this->getMockBuilder('OCP\Encryption\IEncryptionModule') ->disableOriginalConstructor()->getMock(); $dummyEncryptionModule->expects($this->once()) ->method('prepareDecryptAll') ->with($this->inputInterface, $this->outputInterface, $user) ->willReturn($success); $callback = function () use ($dummyEncryptionModule) { return $dummyEncryptionModule; }; $moduleDescription = [ 'id' => 'id', 'displayName' => 'displayName', 'callback' => $callback ]; $this->encryptionManager->expects($this->once()) ->method('getEncryptionModules') ->willReturn([$moduleDescription]); $this->assertSame($success, $this->invokePrivate($this->instance, 'prepareEncryptionModules', [$user]) ); } /** * @dataProvider dataTestDecryptAllUsersFiles */ public function testDecryptAllUsersFiles($user) { /** @var DecryptAll | \PHPUnit\Framework\MockObject\MockObject | $instance */ $instance = $this->getMockBuilder('OC\Encryption\DecryptAll') ->setConstructorArgs( [ $this->encryptionManager, $this->userManager, $this->view ] ) ->setMethods(['decryptUsersFiles']) ->getMock(); $this->invokePrivate($instance, 'input', [$this->inputInterface]); $this->invokePrivate($instance, 'output', [$this->outputInterface]); if (empty($user)) { $this->userManager->expects($this->once()) ->method('getBackends') ->willReturn([$this->userInterface]); $this->userInterface->expects($this->any()) ->method('getUsers') ->willReturn(['user1', 'user2']); $instance->expects($this->exactly(2)) ->method('decryptUsersFiles') ->withConsecutive( ['user1'], ['user2'], ); } else { $instance->expects($this->once()) ->method('decryptUsersFiles') ->with($user); } $this->invokePrivate($instance, 'decryptAllUsersFiles', [$user]); } public function dataTestDecryptAllUsersFiles() { return [ ['user1'], [''] ]; } public function testDecryptUsersFiles() { /** @var DecryptAll | \PHPUnit\Framework\MockObject\MockObject $instance */ $instance = $this->getMockBuilder('OC\Encryption\DecryptAll') ->setConstructorArgs( [ $this->encryptionManager, $this->userManager, $this->view ] ) ->setMethods(['decryptFile']) ->getMock(); $storage = $this->getMockBuilder(Storage::class) ->disableOriginalConstructor()->getMock(); $sharedStorage = $this->getMockBuilder(Storage::class) ->disableOriginalConstructor()->getMock(); $sharedStorage->expects($this->once())->method('instanceOfStorage') ->with('OCA\Files_Sharing\SharedStorage')->willReturn(true); $this->view->expects($this->exactly(2)) ->method('getDirectoryContent') ->withConsecutive( ['/user1/files'], ['/user1/files/foo'] ) ->willReturnOnConsecutiveCalls( [ new FileInfo('path', $storage, 'intPath', ['name' => 'foo', 'type' => 'dir'], null), new FileInfo('path', $storage, 'intPath', ['name' => 'bar', 'type' => 'file', 'encrypted' => true], null), new FileInfo('path', $sharedStorage, 'intPath', ['name' => 'shared', 'type' => 'file', 'encrypted' => true], null), ], [ new FileInfo('path', $storage, 'intPath', ['name' => 'subfile', 'type' => 'file', 'encrypted' => true], null) ] ); $this->view->expects($this->any())->method('is_dir') ->willReturnCallback( function ($path) { if ($path === '/user1/files/foo') { return true; } return false; } ); $instance->expects($this->exactly(2)) ->method('decryptFile') ->withConsecutive( ['/user1/files/bar'], ['/user1/files/foo/subfile'], ); /* We need format method to return a string */ $outputFormatter = $this->createMock(OutputFormatterInterface::class); $outputFormatter->method('format')->willReturn('foo'); $output = $this->createMock(OutputInterface::class); $output->expects($this->any()) ->method('getFormatter') ->willReturn($outputFormatter); $progressBar = new ProgressBar($output); $this->invokePrivate($instance, 'decryptUsersFiles', ['user1', $progressBar, '']); } /** * @dataProvider dataTrueFalse */ public function testDecryptFile($isEncrypted) { $path = 'test.txt'; /** @var DecryptAll | \PHPUnit\Framework\MockObject\MockObject $instance */ $instance = $this->getMockBuilder('OC\Encryption\DecryptAll') ->setConstructorArgs( [ $this->encryptionManager, $this->userManager, $this->view ] ) ->setMethods(['getTimestamp']) ->getMock(); $fileInfo = $this->createMock(FileInfo::class); $fileInfo->expects($this->any())->method('isEncrypted') ->willReturn($isEncrypted); $this->view->expects($this->any())->method('getFileInfo') ->willReturn($fileInfo); if ($isEncrypted) { $instance->expects($this->any())->method('getTimestamp')->willReturn(42); $this->view->expects($this->once()) ->method('copy') ->with($path, $path . '.decrypted.42'); $this->view->expects($this->once()) ->method('rename') ->with($path . '.decrypted.42', $path); } else { $instance->expects($this->never())->method('getTimestamp'); $this->view->expects($this->never())->method('copy'); $this->view->expects($this->never())->method('rename'); } $this->assertTrue( $this->invokePrivate($instance, 'decryptFile', [$path]) ); } public function testDecryptFileFailure() { $path = 'test.txt'; /** @var DecryptAll | \PHPUnit\Framework\MockObject\MockObject $instance */ $instance = $this->getMockBuilder('OC\Encryption\DecryptAll') ->setConstructorArgs( [ $this->encryptionManager, $this->userManager, $this->view ] ) ->setMethods(['getTimestamp']) ->getMock(); $fileInfo = $this->createMock(FileInfo::class); $fileInfo->expects($this->any())->method('isEncrypted') ->willReturn(true); $this->view->expects($this->any())->method('getFileInfo') ->willReturn($fileInfo); $instance->expects($this->any())->method('getTimestamp')->willReturn(42); $this->view->expects($this->once()) ->method('copy') ->with($path, $path . '.decrypted.42') ->willReturnCallback(function () { throw new DecryptionFailedException(); }); $this->view->expects($this->never())->method('rename'); $this->view->expects($this->once()) ->method('file_exists') ->with($path . '.decrypted.42') ->willReturn(true); $this->view->expects($this->once()) ->method('unlink') ->with($path . '.decrypted.42'); $this->assertFalse( $this->invokePrivate($instance, 'decryptFile', [$path]) ); } }