EncryptionTrait.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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\Filesystem;
  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. /**
  17. * Enables encryption
  18. */
  19. trait EncryptionTrait {
  20. // from MountProviderTrait
  21. abstract protected function registerStorageWrapper($name, $wrapper);
  22. // from phpunit
  23. abstract protected static function markTestSkipped(string $message = ''): void;
  24. abstract protected static function assertTrue($condition, string $message = ''): void;
  25. private $encryptionWasEnabled;
  26. private $originalEncryptionModule;
  27. /**
  28. * @var \OCP\IConfig
  29. */
  30. private $config;
  31. /**
  32. * @var \OCA\Encryption\AppInfo\Application
  33. */
  34. private $encryptionApp;
  35. protected function loginWithEncryption($user = '') {
  36. \OC_Util::tearDownFS();
  37. \OC_User::setUserId('');
  38. // needed for fully logout
  39. \OC::$server->getUserSession()->setUser(null);
  40. Filesystem::tearDown();
  41. \OC_User::setUserId($user);
  42. $this->postLogin();
  43. \OC_Util::setupFS($user);
  44. if (\OC::$server->getUserManager()->userExists($user)) {
  45. \OC::$server->getUserFolder($user);
  46. }
  47. }
  48. protected function setupForUser($name, $password) {
  49. \OC_Util::tearDownFS();
  50. \OC_Util::setupFS($name);
  51. $container = $this->encryptionApp->getContainer();
  52. /** @var KeyManager $keyManager */
  53. $keyManager = $container->query(KeyManager::class);
  54. /** @var Setup $userSetup */
  55. $userSetup = $container->query(Setup::class);
  56. $userSetup->setupUser($name, $password);
  57. $encryptionManager = $container->query(IManager::class);
  58. $this->encryptionApp->setUp($encryptionManager);
  59. $keyManager->init($name, $password);
  60. }
  61. protected function postLogin() {
  62. $encryptionWrapper = new EncryptionWrapper(
  63. new ArrayCache(),
  64. \OC::$server->getEncryptionManager(),
  65. \OC::$server->getLogger()
  66. );
  67. $this->registerStorageWrapper('oc_encryption', [$encryptionWrapper, 'wrapStorage']);
  68. }
  69. protected function setUpEncryptionTrait() {
  70. $isReady = \OC::$server->getEncryptionManager()->isReady();
  71. if (!$isReady) {
  72. $this->markTestSkipped('Encryption not ready');
  73. }
  74. \OC_App::loadApp('encryption');
  75. $this->encryptionApp = new Application([], $isReady);
  76. $this->config = \OC::$server->getConfig();
  77. $this->encryptionWasEnabled = $this->config->getAppValue('core', 'encryption_enabled', 'no');
  78. $this->originalEncryptionModule = $this->config->getAppValue('core', 'default_encryption_module');
  79. $this->config->setAppValue('core', 'default_encryption_module', \OCA\Encryption\Crypto\Encryption::ID);
  80. $this->config->setAppValue('core', 'encryption_enabled', 'yes');
  81. $this->assertTrue(\OC::$server->getEncryptionManager()->isEnabled());
  82. }
  83. protected function tearDownEncryptionTrait() {
  84. if ($this->config) {
  85. $this->config->setAppValue('core', 'encryption_enabled', $this->encryptionWasEnabled);
  86. $this->config->setAppValue('core', 'default_encryption_module', $this->originalEncryptionModule);
  87. $this->config->deleteAppValue('encryption', 'useMasterKey');
  88. }
  89. }
  90. }