|
|
|
|
@ -1,5 +1,7 @@
|
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
@ -30,6 +32,7 @@ use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
|
|
class EncryptAllTest extends TestCase {
|
|
|
|
|
|
|
|
|
|
protected KeyManager&MockObject $keyManager;
|
|
|
|
|
protected Util&MockObject $util;
|
|
|
|
|
protected IUserManager&MockObject $userManager;
|
|
|
|
|
@ -46,8 +49,7 @@ class EncryptAllTest extends TestCase {
|
|
|
|
|
protected ISecureRandom&MockObject $secureRandom;
|
|
|
|
|
protected LoggerInterface&MockObject $logger;
|
|
|
|
|
|
|
|
|
|
/** @var EncryptAll */
|
|
|
|
|
protected $encryptAll;
|
|
|
|
|
protected EncryptAll $encryptAll;
|
|
|
|
|
|
|
|
|
|
protected function setUp(): void {
|
|
|
|
|
parent::setUp();
|
|
|
|
|
@ -80,7 +82,7 @@ class EncryptAllTest extends TestCase {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* We need format method to return a string
|
|
|
|
|
* @var OutputFormatterInterface|\PHPUnit\Framework\MockObject\MockObject
|
|
|
|
|
* @var OutputFormatterInterface&MockObject
|
|
|
|
|
*/
|
|
|
|
|
$outputFormatter = $this->createMock(OutputFormatterInterface::class);
|
|
|
|
|
$outputFormatter->method('isDecorated')->willReturn(false);
|
|
|
|
|
@ -112,6 +114,13 @@ class EncryptAllTest extends TestCase {
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function createFileInfoMock($type, string $name): FileInfo&MockObject {
|
|
|
|
|
$fileInfo = $this->createMock(FileInfo::class);
|
|
|
|
|
$fileInfo->method('getType')->willReturn($type);
|
|
|
|
|
$fileInfo->method('getName')->willReturn($name);
|
|
|
|
|
return $fileInfo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testEncryptAll(): void {
|
|
|
|
|
/** @var EncryptAll&MockObject $encryptAll */
|
|
|
|
|
$encryptAll = $this->getMockBuilder(EncryptAll::class)
|
|
|
|
|
@ -131,7 +140,7 @@ class EncryptAllTest extends TestCase {
|
|
|
|
|
$this->logger,
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
->setMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords'])
|
|
|
|
|
->onlyMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords'])
|
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
|
|
$this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false);
|
|
|
|
|
@ -161,7 +170,7 @@ class EncryptAllTest extends TestCase {
|
|
|
|
|
$this->logger,
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
->setMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords'])
|
|
|
|
|
->onlyMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords'])
|
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
|
|
$this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(true);
|
|
|
|
|
@ -192,7 +201,7 @@ class EncryptAllTest extends TestCase {
|
|
|
|
|
$this->logger,
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
->setMethods(['setupUserFS', 'generateOneTimePassword'])
|
|
|
|
|
->onlyMethods(['setupUserFS', 'generateOneTimePassword'])
|
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -225,7 +234,7 @@ class EncryptAllTest extends TestCase {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testEncryptAllUsersFiles(): void {
|
|
|
|
|
/** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */
|
|
|
|
|
/** @var EncryptAll&MockObject $encryptAll */
|
|
|
|
|
$encryptAll = $this->getMockBuilder(EncryptAll::class)
|
|
|
|
|
->setConstructorArgs(
|
|
|
|
|
[
|
|
|
|
|
@ -243,7 +252,7 @@ class EncryptAllTest extends TestCase {
|
|
|
|
|
$this->logger,
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
->setMethods(['encryptUsersFiles'])
|
|
|
|
|
->onlyMethods(['encryptUsersFiles'])
|
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
|
|
$this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false);
|
|
|
|
|
@ -252,17 +261,22 @@ class EncryptAllTest extends TestCase {
|
|
|
|
|
$this->invokePrivate($encryptAll, 'output', [$this->outputInterface]);
|
|
|
|
|
$this->invokePrivate($encryptAll, 'userPasswords', [['user1' => 'pwd1', 'user2' => 'pwd2']]);
|
|
|
|
|
|
|
|
|
|
$encryptAll->expects($this->exactly(2))->method('encryptUsersFiles')
|
|
|
|
|
->withConsecutive(
|
|
|
|
|
['user1'],
|
|
|
|
|
['user2'],
|
|
|
|
|
);
|
|
|
|
|
$encryptAllCalls = [];
|
|
|
|
|
$encryptAll->expects($this->exactly(2))
|
|
|
|
|
->method('encryptUsersFiles')
|
|
|
|
|
->willReturnCallback(function ($uid) use (&$encryptAllCalls): void {
|
|
|
|
|
$encryptAllCalls[] = $uid;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->invokePrivate($encryptAll, 'encryptAllUsersFiles');
|
|
|
|
|
self::assertEquals([
|
|
|
|
|
'user1',
|
|
|
|
|
'user2',
|
|
|
|
|
], $encryptAllCalls);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testEncryptUsersFiles(): void {
|
|
|
|
|
/** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */
|
|
|
|
|
/** @var EncryptAll&MockObject $encryptAll */
|
|
|
|
|
$encryptAll = $this->getMockBuilder(EncryptAll::class)
|
|
|
|
|
->setConstructorArgs(
|
|
|
|
|
[
|
|
|
|
|
@ -280,40 +294,39 @@ class EncryptAllTest extends TestCase {
|
|
|
|
|
$this->logger,
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
->setMethods(['encryptFile', 'setupUserFS'])
|
|
|
|
|
->onlyMethods(['encryptFile', 'setupUserFS'])
|
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
|
|
$this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false);
|
|
|
|
|
|
|
|
|
|
$this->view->expects($this->exactly(2))->method('getDirectoryContent')
|
|
|
|
|
->withConsecutive(
|
|
|
|
|
['/user1/files'],
|
|
|
|
|
['/user1/files/foo'],
|
|
|
|
|
)->willReturnOnConsecutiveCalls(
|
|
|
|
|
->willReturnMap([
|
|
|
|
|
[
|
|
|
|
|
['name' => 'foo', 'type' => 'dir'],
|
|
|
|
|
['name' => 'bar', 'type' => 'file'],
|
|
|
|
|
'/user1/files',
|
|
|
|
|
'',
|
|
|
|
|
null,
|
|
|
|
|
[
|
|
|
|
|
$this->createFileInfoMock(FileInfo::TYPE_FOLDER, 'foo'),
|
|
|
|
|
$this->createFileInfoMock(FileInfo::TYPE_FILE, 'bar'),
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
['name' => 'subfile', 'type' => 'file']
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->view->expects($this->any())->method('is_dir')
|
|
|
|
|
->willReturnCallback(
|
|
|
|
|
function ($path) {
|
|
|
|
|
if ($path === '/user1/files/foo') {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
'/user1/files/foo',
|
|
|
|
|
'',
|
|
|
|
|
null,
|
|
|
|
|
[
|
|
|
|
|
$this->createFileInfoMock(FileInfo::TYPE_FILE, 'subfile'),
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$encryptAll->expects($this->exactly(2))->method('encryptFile')
|
|
|
|
|
->withConsecutive(
|
|
|
|
|
['/user1/files/bar'],
|
|
|
|
|
['/user1/files/foo/subfile'],
|
|
|
|
|
);
|
|
|
|
|
$encryptAllCalls = [];
|
|
|
|
|
$encryptAll->expects($this->exactly(2))
|
|
|
|
|
->method('encryptFile')
|
|
|
|
|
->willReturnCallback(function (FileInfo $file, string $path) use (&$encryptAllCalls): bool {
|
|
|
|
|
$encryptAllCalls[] = $path;
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$outputFormatter = $this->createMock(OutputFormatterInterface::class);
|
|
|
|
|
$outputFormatter->method('isDecorated')->willReturn(false);
|
|
|
|
|
@ -323,6 +336,10 @@ class EncryptAllTest extends TestCase {
|
|
|
|
|
$progressBar = new ProgressBar($this->outputInterface);
|
|
|
|
|
|
|
|
|
|
$this->invokePrivate($encryptAll, 'encryptUsersFiles', ['user1', $progressBar, '']);
|
|
|
|
|
self::assertEquals([
|
|
|
|
|
'/user1/files/bar',
|
|
|
|
|
'/user1/files/foo/subfile',
|
|
|
|
|
], $encryptAllCalls);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGenerateOneTimePassword(): void {
|
|
|
|
|
@ -343,8 +360,7 @@ class EncryptAllTest extends TestCase {
|
|
|
|
|
$fileInfo = $this->createMock(FileInfo::class);
|
|
|
|
|
$fileInfo->expects($this->any())->method('isEncrypted')
|
|
|
|
|
->willReturn($isEncrypted);
|
|
|
|
|
$this->view->expects($this->any())->method('getFileInfo')
|
|
|
|
|
->willReturn($fileInfo);
|
|
|
|
|
$this->view->expects($this->never())->method('getFileInfo');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($isEncrypted) {
|
|
|
|
|
@ -356,11 +372,11 @@ class EncryptAllTest extends TestCase {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->assertTrue(
|
|
|
|
|
$this->invokePrivate($this->encryptAll, 'encryptFile', ['foo.txt'])
|
|
|
|
|
$this->invokePrivate($this->encryptAll, 'encryptFile', [$fileInfo, 'foo.txt'])
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function dataTestEncryptFile() {
|
|
|
|
|
public static function dataTestEncryptFile(): array {
|
|
|
|
|
return [
|
|
|
|
|
[true],
|
|
|
|
|
[false],
|
|
|
|
|
|