EncryptionTrait.php 3.1 KB

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