1
0

EncryptionTrait.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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_User::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. $keyManager->init($name, $password);
  57. }
  58. protected function postLogin() {
  59. $encryptionWrapper = new EncryptionWrapper(
  60. new ArrayCache(),
  61. \OC::$server->getEncryptionManager(),
  62. \OC::$server->getLogger()
  63. );
  64. $this->registerStorageWrapper('oc_encryption', array($encryptionWrapper, 'wrapStorage'));
  65. }
  66. protected function setUpEncryptionTrait() {
  67. $isReady = \OC::$server->getEncryptionManager()->isReady();
  68. if (!$isReady) {
  69. $this->markTestSkipped('Encryption not ready');
  70. }
  71. \OC_App::loadApp('encryption');
  72. $this->encryptionApp = new Application([], $isReady);
  73. $this->config = \OC::$server->getConfig();
  74. $this->encryptionWasEnabled = $this->config->getAppValue('core', 'encryption_enabled', 'no');
  75. $this->originalEncryptionModule = $this->config->getAppValue('core', 'default_encryption_module');
  76. $this->config->setAppValue('core', 'default_encryption_module', \OCA\Encryption\Crypto\Encryption::ID);
  77. $this->config->setAppValue('core', 'encryption_enabled', 'yes');
  78. $this->assertTrue(\OC::$server->getEncryptionManager()->isEnabled());
  79. }
  80. protected function tearDownEncryptionTrait() {
  81. if ($this->config) {
  82. $this->config->setAppValue('core', 'encryption_enabled', $this->encryptionWasEnabled);
  83. $this->config->setAppValue('core', 'default_encryption_module', $this->originalEncryptionModule);
  84. }
  85. }
  86. }