DecryptAllTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Tests\Core\Command\Encryption;
  22. use OC\Core\Command\Encryption\DecryptAll;
  23. use OCP\App\IAppManager;
  24. use OCP\Encryption\IManager;
  25. use OCP\IConfig;
  26. use Symfony\Component\Console\Helper\QuestionHelper;
  27. use Symfony\Component\Console\Input\InputInterface;
  28. use Symfony\Component\Console\Output\OutputInterface;
  29. use Test\TestCase;
  30. class DecryptAllTest extends TestCase {
  31. /** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\IConfig */
  32. protected $config;
  33. /** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\Encryption\IManager */
  34. protected $encryptionManager;
  35. /** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\App\IAppManager */
  36. protected $appManager;
  37. /** @var \PHPUnit_Framework_MockObject_MockObject | \Symfony\Component\Console\Input\InputInterface */
  38. protected $consoleInput;
  39. /** @var \PHPUnit_Framework_MockObject_MockObject | \Symfony\Component\Console\Output\OutputInterface */
  40. protected $consoleOutput;
  41. /** @var \PHPUnit_Framework_MockObject_MockObject | \Symfony\Component\Console\Helper\QuestionHelper */
  42. protected $questionHelper;
  43. /** @var \PHPUnit_Framework_MockObject_MockObject | \OC\Encryption\DecryptAll */
  44. protected $decryptAll;
  45. public function setUp() {
  46. parent::setUp();
  47. $this->config = $this->getMockBuilder(IConfig::class)
  48. ->disableOriginalConstructor()
  49. ->getMock();
  50. $this->encryptionManager = $this->getMockBuilder(IManager::class)
  51. ->disableOriginalConstructor()
  52. ->getMock();
  53. $this->appManager = $this->getMockBuilder(IAppManager::class)
  54. ->disableOriginalConstructor()
  55. ->getMock();
  56. $this->questionHelper = $this->getMockBuilder(QuestionHelper::class)
  57. ->disableOriginalConstructor()
  58. ->getMock();
  59. $this->decryptAll = $this->getMockBuilder(\OC\Encryption\DecryptAll::class)
  60. ->disableOriginalConstructor()->getMock();
  61. $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock();
  62. $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
  63. $this->config->expects($this->any())
  64. ->method('getSystemValue')
  65. ->with('maintenance', false)
  66. ->willReturn(false);
  67. $this->appManager->expects($this->any())
  68. ->method('isEnabledForUser')
  69. ->with('files_trashbin')->willReturn(true);
  70. }
  71. public function testMaintenanceAndTrashbin() {
  72. // on construct we enable single-user-mode and disable the trash bin
  73. $this->config->expects($this->at(1))
  74. ->method('setSystemValue')
  75. ->with('maintenance', true);
  76. $this->appManager->expects($this->once())
  77. ->method('disableApp')
  78. ->with('files_trashbin');
  79. // on destruct wi disable single-user-mode again and enable the trash bin
  80. $this->config->expects($this->at(2))
  81. ->method('setSystemValue')
  82. ->with('maintenance', false);
  83. $this->appManager->expects($this->once())
  84. ->method('enableApp')
  85. ->with('files_trashbin');
  86. $instance = new DecryptAll(
  87. $this->encryptionManager,
  88. $this->appManager,
  89. $this->config,
  90. $this->decryptAll,
  91. $this->questionHelper
  92. );
  93. $this->invokePrivate($instance, 'forceMaintenanceAndTrashbin');
  94. $this->assertTrue(
  95. $this->invokePrivate($instance, 'wasTrashbinEnabled')
  96. );
  97. $this->assertFalse(
  98. $this->invokePrivate($instance, 'wasMaintenanceModeEnabled')
  99. );
  100. $this->invokePrivate($instance, 'resetMaintenanceAndTrashbin');
  101. }
  102. /**
  103. * @dataProvider dataTestExecute
  104. */
  105. public function testExecute($encryptionEnabled, $continue) {
  106. $instance = new DecryptAll(
  107. $this->encryptionManager,
  108. $this->appManager,
  109. $this->config,
  110. $this->decryptAll,
  111. $this->questionHelper
  112. );
  113. $this->encryptionManager->expects($this->once())
  114. ->method('isEnabled')
  115. ->willReturn($encryptionEnabled);
  116. $this->consoleInput->expects($this->any())
  117. ->method('getArgument')
  118. ->with('user')
  119. ->willReturn('user1');
  120. if ($encryptionEnabled) {
  121. $this->config->expects($this->at(0))
  122. ->method('setAppValue')
  123. ->with('core', 'encryption_enabled', 'no');
  124. $this->questionHelper->expects($this->once())
  125. ->method('ask')
  126. ->willReturn($continue);
  127. if ($continue) {
  128. $this->decryptAll->expects($this->once())
  129. ->method('decryptAll')
  130. ->with($this->consoleInput, $this->consoleOutput, 'user1');
  131. } else {
  132. $this->decryptAll->expects($this->never())->method('decryptAll');
  133. $this->config->expects($this->at(1))
  134. ->method('setAppValue')
  135. ->with('core', 'encryption_enabled', 'yes');
  136. }
  137. } else {
  138. $this->config->expects($this->never())->method('setAppValue');
  139. $this->decryptAll->expects($this->never())->method('decryptAll');
  140. $this->questionHelper->expects($this->never())->method('ask');
  141. }
  142. $this->invokePrivate($instance, 'execute', [$this->consoleInput, $this->consoleOutput]);
  143. }
  144. public function dataTestExecute() {
  145. return [
  146. [true, true],
  147. [true, false],
  148. [false, true],
  149. [false, false]
  150. ];
  151. }
  152. /**
  153. * @expectedException \Exception
  154. */
  155. public function testExecuteFailure() {
  156. $instance = new DecryptAll(
  157. $this->encryptionManager,
  158. $this->appManager,
  159. $this->config,
  160. $this->decryptAll,
  161. $this->questionHelper
  162. );
  163. $this->config->expects($this->at(0))
  164. ->method('setAppValue')
  165. ->with('core', 'encryption_enabled', 'no');
  166. // make sure that we enable encryption again after a exception was thrown
  167. $this->config->expects($this->at(3))
  168. ->method('setAppValue')
  169. ->with('core', 'encryption_enabled', 'yes');
  170. $this->encryptionManager->expects($this->once())
  171. ->method('isEnabled')
  172. ->willReturn(true);
  173. $this->consoleInput->expects($this->any())
  174. ->method('getArgument')
  175. ->with('user')
  176. ->willReturn('user1');
  177. $this->questionHelper->expects($this->once())
  178. ->method('ask')
  179. ->willReturn(true);
  180. $this->decryptAll->expects($this->once())
  181. ->method('decryptAll')
  182. ->with($this->consoleInput, $this->consoleOutput, 'user1')
  183. ->willReturnCallback(function() { throw new \Exception(); });
  184. $this->invokePrivate($instance, 'execute', [$this->consoleInput, $this->consoleOutput]);
  185. }
  186. }