1
0

EncryptionTrait.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Copyright (c) 2015 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\Traits;
  9. use OC\Encryption\EncryptionWrapper;
  10. use OC\Files\SetupManager;
  11. use OC\Memcache\ArrayCache;
  12. use OCA\Encryption\AppInfo\Application;
  13. use OCA\Encryption\KeyManager;
  14. use OCA\Encryption\Users\Setup;
  15. use OCP\Encryption\IManager;
  16. use OCP\IUserManager;
  17. use Psr\Log\LoggerInterface;
  18. /**
  19. * Enables encryption
  20. */
  21. trait EncryptionTrait {
  22. // from MountProviderTrait
  23. abstract protected function registerStorageWrapper($name, $wrapper);
  24. // from phpunit
  25. abstract protected static function markTestSkipped(string $message = ''): void;
  26. abstract protected static function assertTrue($condition, string $message = ''): void;
  27. private $encryptionWasEnabled;
  28. private $originalEncryptionModule;
  29. /** @var IUserManager */
  30. private $userManager;
  31. /** @var SetupManager */
  32. private $setupManager;
  33. /**
  34. * @var \OCP\IConfig
  35. */
  36. private $config;
  37. /**
  38. * @var \OCA\Encryption\AppInfo\Application
  39. */
  40. private $encryptionApp;
  41. protected function loginWithEncryption($user = '') {
  42. \OC_Util::tearDownFS();
  43. \OC_User::setUserId('');
  44. // needed for fully logout
  45. \OC::$server->getUserSession()->setUser(null);
  46. $this->setupManager->tearDown();
  47. \OC_User::setUserId($user);
  48. $this->postLogin();
  49. \OC_Util::setupFS($user);
  50. if ($this->userManager->userExists($user)) {
  51. \OC::$server->getUserFolder($user);
  52. }
  53. }
  54. protected function setupForUser($name, $password) {
  55. $this->setupManager->tearDown();
  56. $this->setupManager->setupForUser($this->userManager->get($name));
  57. $container = $this->encryptionApp->getContainer();
  58. /** @var KeyManager $keyManager */
  59. $keyManager = $container->query(KeyManager::class);
  60. /** @var Setup $userSetup */
  61. $userSetup = $container->query(Setup::class);
  62. $userSetup->setupUser($name, $password);
  63. $encryptionManager = $container->query(IManager::class);
  64. $this->encryptionApp->setUp($encryptionManager);
  65. $keyManager->init($name, $password);
  66. }
  67. protected function postLogin() {
  68. $encryptionWrapper = new EncryptionWrapper(
  69. new ArrayCache(),
  70. \OC::$server->getEncryptionManager(),
  71. \OC::$server->get(LoggerInterface::class)
  72. );
  73. $this->registerStorageWrapper('oc_encryption', [$encryptionWrapper, 'wrapStorage']);
  74. }
  75. protected function setUpEncryptionTrait() {
  76. $isReady = \OC::$server->getEncryptionManager()->isReady();
  77. if (!$isReady) {
  78. $this->markTestSkipped('Encryption not ready');
  79. }
  80. $this->userManager = \OC::$server->get(IUserManager::class);
  81. $this->setupManager = \OC::$server->get(SetupManager::class);
  82. \OC_App::loadApp('encryption');
  83. $this->encryptionApp = new Application([], $isReady);
  84. $this->config = \OC::$server->getConfig();
  85. $this->encryptionWasEnabled = $this->config->getAppValue('core', 'encryption_enabled', 'no');
  86. $this->originalEncryptionModule = $this->config->getAppValue('core', 'default_encryption_module');
  87. $this->config->setAppValue('core', 'default_encryption_module', \OCA\Encryption\Crypto\Encryption::ID);
  88. $this->config->setAppValue('core', 'encryption_enabled', 'yes');
  89. $this->assertTrue(\OC::$server->getEncryptionManager()->isEnabled());
  90. }
  91. protected function tearDownEncryptionTrait() {
  92. if ($this->config) {
  93. $this->config->setAppValue('core', 'encryption_enabled', $this->encryptionWasEnabled);
  94. $this->config->setAppValue('core', 'default_encryption_module', $this->originalEncryptionModule);
  95. $this->config->deleteAppValue('encryption', 'useMasterKey');
  96. }
  97. }
  98. }