123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 |
- <?php
- namespace OCA\Encryption\Tests\Crypto;
- use OC\Files\View;
- use OCA\Encryption\Crypto\EncryptAll;
- use OCA\Encryption\KeyManager;
- use OCA\Encryption\Users\Setup;
- use OCA\Encryption\Util;
- use OCP\Files\FileInfo;
- use OCP\IConfig;
- use OCP\IL10N;
- use OCP\IUserManager;
- use OCP\L10N\IFactory;
- use OCP\Mail\IMailer;
- use OCP\Security\ISecureRandom;
- use OCP\UserInterface;
- use Symfony\Component\Console\Formatter\OutputFormatterInterface;
- use Symfony\Component\Console\Helper\ProgressBar;
- use Symfony\Component\Console\Helper\QuestionHelper;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Output\OutputInterface;
- use Test\TestCase;
- class EncryptAllTest extends TestCase {
-
- protected $keyManager;
-
- protected $util;
-
- protected $userManager;
-
- protected $setupUser;
-
- protected $view;
-
- protected $config;
-
- protected $mailer;
-
- protected $l;
-
- protected $questionHelper;
-
- protected $inputInterface;
-
- protected $outputInterface;
-
- protected $userInterface;
-
- protected $secureRandom;
-
- protected $encryptAll;
- protected function setUp(): void {
- parent::setUp();
- $this->setupUser = $this->getMockBuilder(Setup::class)
- ->disableOriginalConstructor()->getMock();
- $this->keyManager = $this->getMockBuilder(KeyManager::class)
- ->disableOriginalConstructor()->getMock();
- $this->util = $this->getMockBuilder(Util::class)
- ->disableOriginalConstructor()->getMock();
- $this->userManager = $this->getMockBuilder(IUserManager::class)
- ->disableOriginalConstructor()->getMock();
- $this->view = $this->getMockBuilder(View::class)
- ->disableOriginalConstructor()->getMock();
- $this->config = $this->getMockBuilder(IConfig::class)
- ->disableOriginalConstructor()->getMock();
- $this->mailer = $this->getMockBuilder(IMailer::class)
- ->disableOriginalConstructor()->getMock();
- $this->l10nFactory = $this->createMock(IFactory::class);
- $this->l = $this->getMockBuilder(IL10N::class)
- ->disableOriginalConstructor()->getMock();
- $this->questionHelper = $this->getMockBuilder(QuestionHelper::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();
-
- $outputFormatter = $this->createMock(OutputFormatterInterface::class);
- $outputFormatter->method('isDecorated')->willReturn(false);
- $outputFormatter->method('format')->willReturnArgument(0);
- $this->outputInterface->expects($this->any())->method('getFormatter')
- ->willReturn($outputFormatter);
- $this->userManager->expects($this->any())->method('getBackends')->willReturn([$this->userInterface]);
- $this->userInterface->expects($this->any())->method('getUsers')->willReturn(['user1', 'user2']);
- $this->secureRandom = $this->getMockBuilder(ISecureRandom::class)->disableOriginalConstructor()->getMock();
- $this->secureRandom->expects($this->any())->method('generate')->willReturn('12345678');
- $this->encryptAll = new EncryptAll(
- $this->setupUser,
- $this->userManager,
- $this->view,
- $this->keyManager,
- $this->util,
- $this->config,
- $this->mailer,
- $this->l,
- $this->l10nFactory,
- $this->questionHelper,
- $this->secureRandom
- );
- }
- public function testEncryptAll() {
-
- $encryptAll = $this->getMockBuilder(EncryptAll::class)
- ->setConstructorArgs(
- [
- $this->setupUser,
- $this->userManager,
- $this->view,
- $this->keyManager,
- $this->util,
- $this->config,
- $this->mailer,
- $this->l,
- $this->l10nFactory,
- $this->questionHelper,
- $this->secureRandom
- ]
- )
- ->setMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords'])
- ->getMock();
- $this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false);
- $encryptAll->expects($this->once())->method('createKeyPairs')->with();
- $encryptAll->expects($this->once())->method('outputPasswords')->with();
- $encryptAll->expects($this->once())->method('encryptAllUsersFiles')->with();
- $encryptAll->encryptAll($this->inputInterface, $this->outputInterface);
- }
- public function testEncryptAllWithMasterKey() {
-
- $encryptAll = $this->getMockBuilder(EncryptAll::class)
- ->setConstructorArgs(
- [
- $this->setupUser,
- $this->userManager,
- $this->view,
- $this->keyManager,
- $this->util,
- $this->config,
- $this->mailer,
- $this->l,
- $this->l10nFactory,
- $this->questionHelper,
- $this->secureRandom
- ]
- )
- ->setMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords'])
- ->getMock();
- $this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(true);
- $encryptAll->expects($this->never())->method('createKeyPairs');
- $this->keyManager->expects($this->once())->method('validateMasterKey');
- $encryptAll->expects($this->once())->method('encryptAllUsersFiles')->with();
- $encryptAll->expects($this->never())->method('outputPasswords');
- $encryptAll->encryptAll($this->inputInterface, $this->outputInterface);
- }
- public function testCreateKeyPairs() {
-
- $encryptAll = $this->getMockBuilder(EncryptAll::class)
- ->setConstructorArgs(
- [
- $this->setupUser,
- $this->userManager,
- $this->view,
- $this->keyManager,
- $this->util,
- $this->config,
- $this->mailer,
- $this->l,
- $this->l10nFactory,
- $this->questionHelper,
- $this->secureRandom
- ]
- )
- ->setMethods(['setupUserFS', 'generateOneTimePassword'])
- ->getMock();
-
- $this->invokePrivate($encryptAll, 'output', [$this->outputInterface]);
- $this->keyManager->expects($this->exactly(2))->method('userHasKeys')
- ->willReturnCallback(
- function ($user) {
- if ($user === 'user1') {
- return false;
- }
- return true;
- }
- );
- $encryptAll->expects($this->once())->method('setupUserFS')->with('user1');
- $encryptAll->expects($this->once())->method('generateOneTimePassword')->with('user1')->willReturn('password');
- $this->setupUser->expects($this->once())->method('setupUser')->with('user1', 'password');
- $this->invokePrivate($encryptAll, 'createKeyPairs');
- $userPasswords = $this->invokePrivate($encryptAll, 'userPasswords');
-
-
-
- $this->assertSame(1, count($userPasswords));
- $this->assertSame('', $userPasswords['user2']);
- }
- public function testEncryptAllUsersFiles() {
-
- $encryptAll = $this->getMockBuilder(EncryptAll::class)
- ->setConstructorArgs(
- [
- $this->setupUser,
- $this->userManager,
- $this->view,
- $this->keyManager,
- $this->util,
- $this->config,
- $this->mailer,
- $this->l,
- $this->l10nFactory,
- $this->questionHelper,
- $this->secureRandom
- ]
- )
- ->setMethods(['encryptUsersFiles'])
- ->getMock();
- $this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false);
-
- $this->invokePrivate($encryptAll, 'output', [$this->outputInterface]);
- $this->invokePrivate($encryptAll, 'userPasswords', [['user1' => 'pwd1', 'user2' => 'pwd2']]);
- $encryptAll->expects($this->exactly(2))->method('encryptUsersFiles')
- ->withConsecutive(
- ['user1'],
- ['user2'],
- );
- $this->invokePrivate($encryptAll, 'encryptAllUsersFiles');
- }
- public function testEncryptUsersFiles() {
-
- $encryptAll = $this->getMockBuilder(EncryptAll::class)
- ->setConstructorArgs(
- [
- $this->setupUser,
- $this->userManager,
- $this->view,
- $this->keyManager,
- $this->util,
- $this->config,
- $this->mailer,
- $this->l,
- $this->l10nFactory,
- $this->questionHelper,
- $this->secureRandom
- ]
- )
- ->setMethods(['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(
- [
- ['name' => 'foo', 'type' => 'dir'],
- ['name' => 'bar', 'type' => 'file'],
- ],
- [
- ['name' => 'subfile', 'type' => 'file']
- ]
- );
- $this->view->expects($this->any())->method('is_dir')
- ->willReturnCallback(
- function ($path) {
- if ($path === '/user1/files/foo') {
- return true;
- }
- return false;
- }
- );
- $encryptAll->expects($this->exactly(2))->method('encryptFile')
- ->withConsecutive(
- ['/user1/files/bar'],
- ['/user1/files/foo/subfile'],
- );
- $outputFormatter = $this->createMock(OutputFormatterInterface::class);
- $outputFormatter->method('isDecorated')->willReturn(false);
- $this->outputInterface->expects($this->any())
- ->method('getFormatter')
- ->willReturn($outputFormatter);
- $progressBar = new ProgressBar($this->outputInterface);
- $this->invokePrivate($encryptAll, 'encryptUsersFiles', ['user1', $progressBar, '']);
- }
- public function testGenerateOneTimePassword() {
- $password = $this->invokePrivate($this->encryptAll, 'generateOneTimePassword', ['user1']);
- $this->assertTrue(is_string($password));
- $this->assertSame(8, strlen($password));
- $userPasswords = $this->invokePrivate($this->encryptAll, 'userPasswords');
- $this->assertSame(1, count($userPasswords));
- $this->assertSame($password, $userPasswords['user1']);
- }
-
- public function testEncryptFile($isEncrypted) {
- $fileInfo = $this->createMock(FileInfo::class);
- $fileInfo->expects($this->any())->method('isEncrypted')
- ->willReturn($isEncrypted);
- $this->view->expects($this->any())->method('getFileInfo')
- ->willReturn($fileInfo);
- if ($isEncrypted) {
- $this->view->expects($this->never())->method('copy');
- $this->view->expects($this->never())->method('rename');
- } else {
- $this->view->expects($this->once())->method('copy');
- $this->view->expects($this->once())->method('rename');
- }
- $this->assertTrue(
- $this->invokePrivate($this->encryptAll, 'encryptFile', ['foo.txt'])
- );
- }
- public function dataTestEncryptFile() {
- return [
- [true],
- [false],
- ];
- }
- }
|