Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
namespace OCA\DAV\Tests\unit\Connector\Sabre\RequestTest;

use OC\Files\View;
use OCA\Encryption\Listeners\UserEventsListener;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Server;
use OCP\User\Events\UserLoggedInEvent;
use Test\Traits\EncryptionTrait;

/**
Expand All @@ -20,14 +24,29 @@
class EncryptionUploadTest extends UploadTest {
use EncryptionTrait;

protected function setupUser($name, $password) {
$this->createUser($name, $password);
$tmpFolder = \OC::$server->getTempManager()->getTemporaryFolder();
$this->registerMount($name, '\OC\Files\Storage\Local', '/' . $name, ['datadir' => $tmpFolder]);
// we use per-user keys
/** @var callable */
private $loginListener;

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

// Restore the user's keys on DAV login, as the encryption app does in production
$this->loginListener = [$this->encryptionApp->getContainer()->get(UserEventsListener::class), 'handle'];
Server::get(IEventDispatcher::class)->addListener(UserLoggedInEvent::class, $this->loginListener);
}

protected function tearDown(): void {
Server::get(IEventDispatcher::class)->removeListener(UserLoggedInEvent::class, $this->loginListener);
parent::tearDown();
}

protected function setupUser($name, $password): View {
\OC::$server->getConfig()->setAppValue('encryption', 'useMasterKey', '0');
$this->setupForUser($name, $password);
return parent::setupUser($name, $password);
}

protected function loginUser(string $name, string $password): void {
$this->loginWithEncryption($name);
return new View('/' . $name . '/files');
$this->setupForUser($name, $password);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,14 @@ protected function setupUser($name, $password) {
$this->createUser($name, $password);
$tmpFolder = \OC::$server->getTempManager()->getTemporaryFolder();
$this->registerMount($name, '\OC\Files\Storage\Local', '/' . $name, ['datadir' => $tmpFolder]);
$this->loginAsUser($name);
$this->loginUser($name, $password);
return new View('/' . $name . '/files');
}

protected function loginUser(string $user, string $password): void {
self::loginAsUser($user);
}

/**
* @param View $view the view to run the webdav server against
* @param string $user
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ public function testUploadOverWriteReadLocked(): void {
public function testUploadOverWriteWriteLocked(): void {
$user = $this->getUniqueID();
$view = $this->setupUser($user, 'pass');
$this->loginAsUser($user);
// Avoid first login event later
$userObject = \OCP\Server::get(\OCP\IUserManager::class)->get($user);
$userObject->updateLastLoginTimestamp();

$view->file_put_contents('foo.txt', 'bar');

Expand Down
13 changes: 6 additions & 7 deletions apps/files_sharing/tests/EncryptedSizePropagationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
namespace OCA\Files_Sharing\Tests;

use OC\Files\View;
use Test\Traits\EncryptionTrait;

/**
Expand All @@ -15,13 +14,13 @@
class EncryptedSizePropagationTest extends SizePropagationTest {
use EncryptionTrait;

protected function setupUser($name, $password = '') {
$this->createUser($name, $password);
$tmpFolder = \OC::$server->getTempManager()->getTemporaryFolder();
$this->registerMount($name, '\OC\Files\Storage\Local', '/' . $name, ['datadir' => $tmpFolder]);
protected function setUp(): void {
parent::setUp();
$this->config->setAppValue('encryption', 'useMasterKey', '0');
$this->setupForUser($name, $password);
}

protected function loginUser(string $name): void {
$this->loginWithEncryption($name);
return new View('/' . $name . '/files');
$this->setupForUser($name, '');
}
}
18 changes: 11 additions & 7 deletions apps/files_sharing/tests/SizePropagationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ protected function setupUser($name, $password = '') {
$this->createUser($name, $password);
$tmpFolder = \OC::$server->getTempManager()->getTemporaryFolder();
$this->registerMount($name, '\OC\Files\Storage\Local', '/' . $name, ['datadir' => $tmpFolder]);
$this->loginAsUser($name);
$this->loginUser($name);
return new View('/' . $name . '/files');
}

protected function loginUser(string $name): void {
$this->loginAsUser($name);
}

public function testSizePropagationWhenOwnerChangesFile(): void {
$recipientView = $this->setupUser(self::TEST_FILES_SHARING_API_USER1);

Expand All @@ -45,21 +49,21 @@ public function testSizePropagationWhenOwnerChangesFile(): void {
);
$ownerRootInfo = $ownerView->getFileInfo('', false);

$this->loginAsUser(self::TEST_FILES_SHARING_API_USER1);
$this->loginUser(self::TEST_FILES_SHARING_API_USER1);
$this->assertTrue($recipientView->file_exists('/sharedfolder/subfolder/foo.txt'));
$recipientRootInfo = $recipientView->getFileInfo('', false);

// when file changed as owner
$this->loginAsUser(self::TEST_FILES_SHARING_API_USER2);
$this->loginUser(self::TEST_FILES_SHARING_API_USER2);
$ownerView->file_put_contents('/sharedfolder/subfolder/foo.txt', 'foobar');

// size of recipient's root stays the same
$this->loginAsUser(self::TEST_FILES_SHARING_API_USER1);
$this->loginUser(self::TEST_FILES_SHARING_API_USER1);
$newRecipientRootInfo = $recipientView->getFileInfo('', false);
$this->assertEquals($recipientRootInfo->getSize(), $newRecipientRootInfo->getSize());

// size of owner's root increases
$this->loginAsUser(self::TEST_FILES_SHARING_API_USER2);
$this->loginUser(self::TEST_FILES_SHARING_API_USER2);
$newOwnerRootInfo = $ownerView->getFileInfo('', false);
$this->assertEquals($ownerRootInfo->getSize() + 3, $newOwnerRootInfo->getSize());
}
Expand All @@ -80,7 +84,7 @@ public function testSizePropagationWhenRecipientChangesFile(): void {
);
$ownerRootInfo = $ownerView->getFileInfo('', false);

$this->loginAsUser(self::TEST_FILES_SHARING_API_USER1);
$this->loginUser(self::TEST_FILES_SHARING_API_USER1);
$this->assertTrue($recipientView->file_exists('/sharedfolder/subfolder/foo.txt'));
$recipientRootInfo = $recipientView->getFileInfo('', false);
$recipientRootInfoWithMounts = $recipientView->getFileInfo('', true);
Expand All @@ -98,7 +102,7 @@ public function testSizePropagationWhenRecipientChangesFile(): void {
$this->assertEquals($oldRecipientSize + 3, $newRecipientRootInfo->getSize());

// size of owner's root increases
$this->loginAsUser(self::TEST_FILES_SHARING_API_USER2);
$this->loginUser(self::TEST_FILES_SHARING_API_USER2);
$newOwnerRootInfo = $ownerView->getFileInfo('', false);
$this->assertEquals($ownerRootInfo->getSize() + 3, $newOwnerRootInfo->getSize());
}
Expand Down
12 changes: 9 additions & 3 deletions lib/private/User/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,17 @@ public function setSession(ISession $session) {
*
* @param IUser|null $user
*/
public function setUser($user) {
if (is_null($user)) {
#[\Override]
public function setUser($user): void {
$previousUserId = $this->session->get('user_id');
$newUserId = $user?->getUID();
if ($previousUserId !== null && $previousUserId !== $newUserId) {
$this->session->clear();
}
if (is_null($newUserId)) {
$this->session->remove('user_id');
} else {
$this->session->set('user_id', $user->getUID());
$this->session->set('user_id', $newUserId);
}
$this->activeUser = $user;
}
Expand Down
15 changes: 8 additions & 7 deletions tests/lib/Traits/EncryptionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ protected function postLogin() {
}

protected function setUpEncryptionTrait() {
$this->config = \OC::$server->getConfig();
$this->encryptionWasEnabled = $this->config->getAppValue('core', 'encryption_enabled', 'no');
$this->originalEncryptionModule = $this->config->getAppValue('core', 'default_encryption_module');

$this->config->setAppValue('core', 'encryption_enabled', 'yes');
$this->assertTrue(Server::get(\OCP\Encryption\IManager::class)->isEnabled());

$isReady = \OC::$server->getEncryptionManager()->isReady();
if (!$isReady) {
$this->markTestSkipped('Encryption not ready');
Expand All @@ -98,15 +105,9 @@ protected function setUpEncryptionTrait() {
$this->setupManager = \OC::$server->get(SetupManager::class);

\OC_App::loadApp('encryption');
$this->config->setAppValue('core', 'default_encryption_module', Encryption::ID);

$this->encryptionApp = new Application([], $isReady);

$this->config = \OC::$server->getConfig();
$this->encryptionWasEnabled = $this->config->getAppValue('core', 'encryption_enabled', 'no');
$this->originalEncryptionModule = $this->config->getAppValue('core', 'default_encryption_module');
$this->config->setAppValue('core', 'default_encryption_module', \OCA\Encryption\Crypto\Encryption::ID);
$this->config->setAppValue('core', 'encryption_enabled', 'yes');
$this->assertTrue(\OC::$server->getEncryptionManager()->isEnabled());
}

protected function tearDownEncryptionTrait() {
Expand Down
Loading