ChangeKeyStorageRootTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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\ChangeKeyStorageRoot;
  23. use OC\Encryption\Util;
  24. use OC\Files\View;
  25. use OCP\IConfig;
  26. use OCP\IUserManager;
  27. use OCP\UserInterface;
  28. use Symfony\Component\Console\Formatter\OutputFormatterInterface;
  29. use Symfony\Component\Console\Helper\QuestionHelper;
  30. use Symfony\Component\Console\Input\InputInterface;
  31. use Symfony\Component\Console\Output\OutputInterface;
  32. use Test\TestCase;
  33. class ChangeKeyStorageRootTest extends TestCase {
  34. /** @var ChangeKeyStorageRoot */
  35. protected $changeKeyStorageRoot;
  36. /** @var View | \PHPUnit\Framework\MockObject\MockObject */
  37. protected $view;
  38. /** @var IUserManager | \PHPUnit\Framework\MockObject\MockObject */
  39. protected $userManager;
  40. /** @var IConfig | \PHPUnit\Framework\MockObject\MockObject */
  41. protected $config;
  42. /** @var Util | \PHPUnit\Framework\MockObject\MockObject */
  43. protected $util;
  44. /** @var QuestionHelper | \PHPUnit\Framework\MockObject\MockObject */
  45. protected $questionHelper;
  46. /** @var InputInterface | \PHPUnit\Framework\MockObject\MockObject */
  47. protected $inputInterface;
  48. /** @var OutputInterface | \PHPUnit\Framework\MockObject\MockObject */
  49. protected $outputInterface;
  50. /** @var \OCP\UserInterface | \PHPUnit\Framework\MockObject\MockObject */
  51. protected $userInterface;
  52. protected function setUp(): void {
  53. parent::setUp();
  54. $this->view = $this->getMockBuilder(View::class)->getMock();
  55. $this->userManager = $this->getMockBuilder(IUserManager::class)->getMock();
  56. $this->config = $this->getMockBuilder(IConfig::class)->getMock();
  57. $this->util = $this->getMockBuilder('OC\Encryption\Util')->disableOriginalConstructor()->getMock();
  58. $this->questionHelper = $this->getMockBuilder(QuestionHelper::class)->getMock();
  59. $this->inputInterface = $this->getMockBuilder(InputInterface::class)->getMock();
  60. $this->outputInterface = $this->getMockBuilder(OutputInterface::class)->getMock();
  61. $this->userInterface = $this->getMockBuilder(UserInterface::class)->getMock();
  62. /* We need format method to return a string */
  63. $outputFormatter = $this->createMock(OutputFormatterInterface::class);
  64. $outputFormatter->method('isDecorated')->willReturn(false);
  65. $outputFormatter->method('format')->willReturnArgument(0);
  66. $this->outputInterface->expects($this->any())->method('getFormatter')
  67. ->willReturn($outputFormatter);
  68. $this->changeKeyStorageRoot = new ChangeKeyStorageRoot(
  69. $this->view,
  70. $this->userManager,
  71. $this->config,
  72. $this->util,
  73. $this->questionHelper
  74. );
  75. }
  76. /**
  77. * @dataProvider dataTestExecute
  78. */
  79. public function testExecute($newRoot, $answer, $successMoveKey) {
  80. $changeKeyStorageRoot = $this->getMockBuilder('OC\Core\Command\Encryption\ChangeKeyStorageRoot')
  81. ->setConstructorArgs(
  82. [
  83. $this->view,
  84. $this->userManager,
  85. $this->config,
  86. $this->util,
  87. $this->questionHelper
  88. ]
  89. )->setMethods(['moveAllKeys'])->getMock();
  90. $this->util->expects($this->once())->method('getKeyStorageRoot')
  91. ->willReturn('');
  92. $this->inputInterface->expects($this->once())->method('getArgument')
  93. ->with('newRoot')->willReturn($newRoot);
  94. if ($answer === true || $newRoot !== null) {
  95. $changeKeyStorageRoot->expects($this->once())->method('moveAllKeys')
  96. ->willReturn($successMoveKey);
  97. } else {
  98. $changeKeyStorageRoot->expects($this->never())->method('moveAllKeys');
  99. }
  100. if ($successMoveKey === true) {
  101. $this->util->expects($this->once())->method('setKeyStorageRoot');
  102. } else {
  103. $this->util->expects($this->never())->method('setKeyStorageRoot');
  104. }
  105. if ($newRoot === null) {
  106. $this->questionHelper->expects($this->once())->method('ask')->willReturn($answer);
  107. } else {
  108. $this->questionHelper->expects($this->never())->method('ask');
  109. }
  110. $this->invokePrivate(
  111. $changeKeyStorageRoot,
  112. 'execute',
  113. [$this->inputInterface, $this->outputInterface]
  114. );
  115. }
  116. public function dataTestExecute() {
  117. return [
  118. [null, true, true],
  119. [null, true, false],
  120. [null, false, null],
  121. ['/newRoot', null, true],
  122. ['/newRoot', null, false]
  123. ];
  124. }
  125. public function testMoveAllKeys() {
  126. /** @var \OC\Core\Command\Encryption\ChangeKeyStorageRoot $changeKeyStorageRoot */
  127. $changeKeyStorageRoot = $this->getMockBuilder('OC\Core\Command\Encryption\ChangeKeyStorageRoot')
  128. ->setConstructorArgs(
  129. [
  130. $this->view,
  131. $this->userManager,
  132. $this->config,
  133. $this->util,
  134. $this->questionHelper
  135. ]
  136. )->setMethods(['prepareNewRoot', 'moveSystemKeys', 'moveUserKeys'])->getMock();
  137. $changeKeyStorageRoot->expects($this->once())->method('prepareNewRoot')->with('newRoot');
  138. $changeKeyStorageRoot->expects($this->once())->method('moveSystemKeys')->with('oldRoot', 'newRoot');
  139. $changeKeyStorageRoot->expects($this->once())->method('moveUserKeys')->with('oldRoot', 'newRoot', $this->outputInterface);
  140. $this->invokePrivate($changeKeyStorageRoot, 'moveAllKeys', ['oldRoot', 'newRoot', $this->outputInterface]);
  141. }
  142. public function testPrepareNewRoot() {
  143. $this->view->expects($this->once())->method('is_dir')->with('newRoot')
  144. ->willReturn(true);
  145. $this->view->expects($this->once())->method('file_put_contents')
  146. ->with('newRoot/' . \OC\Encryption\Keys\Storage::KEY_STORAGE_MARKER,
  147. 'Nextcloud will detect this folder as key storage root only if this file exists')->willReturn(true);
  148. $this->invokePrivate($this->changeKeyStorageRoot, 'prepareNewRoot', ['newRoot']);
  149. }
  150. /**
  151. * @dataProvider dataTestPrepareNewRootException
  152. *
  153. * @param bool $dirExists
  154. * @param bool $couldCreateFile
  155. */
  156. public function testPrepareNewRootException($dirExists, $couldCreateFile) {
  157. $this->expectException(\Exception::class);
  158. $this->view->expects($this->once())->method('is_dir')->with('newRoot')
  159. ->willReturn($dirExists);
  160. $this->view->expects($this->any())->method('file_put_contents')->willReturn($couldCreateFile);
  161. $this->invokePrivate($this->changeKeyStorageRoot, 'prepareNewRoot', ['newRoot']);
  162. }
  163. public function dataTestPrepareNewRootException() {
  164. return [
  165. [true, false],
  166. [true, null],
  167. [false, true]
  168. ];
  169. }
  170. /**
  171. * @dataProvider dataTestMoveSystemKeys
  172. *
  173. * @param bool $dirExists
  174. * @param bool $targetExists
  175. * @param bool $executeRename
  176. */
  177. public function testMoveSystemKeys($dirExists, $targetExists, $executeRename) {
  178. $changeKeyStorageRoot = $this->getMockBuilder('OC\Core\Command\Encryption\ChangeKeyStorageRoot')
  179. ->setConstructorArgs(
  180. [
  181. $this->view,
  182. $this->userManager,
  183. $this->config,
  184. $this->util,
  185. $this->questionHelper
  186. ]
  187. )->setMethods(['targetExists'])->getMock();
  188. $this->view->expects($this->once())->method('is_dir')
  189. ->with('oldRoot/files_encryption')->willReturn($dirExists);
  190. $changeKeyStorageRoot->expects($this->any())->method('targetExists')
  191. ->with('newRoot/files_encryption')->willReturn($targetExists);
  192. if ($executeRename) {
  193. $this->view->expects($this->once())->method('rename')
  194. ->with('oldRoot/files_encryption', 'newRoot/files_encryption');
  195. } else {
  196. $this->view->expects($this->never())->method('rename');
  197. }
  198. $this->invokePrivate($changeKeyStorageRoot, 'moveSystemKeys', ['oldRoot', 'newRoot']);
  199. }
  200. public function dataTestMoveSystemKeys() {
  201. return [
  202. [true, false, true],
  203. [false, true, false],
  204. [true, true, false],
  205. [false, false, false]
  206. ];
  207. }
  208. public function testMoveUserKeys() {
  209. $changeKeyStorageRoot = $this->getMockBuilder('OC\Core\Command\Encryption\ChangeKeyStorageRoot')
  210. ->setConstructorArgs(
  211. [
  212. $this->view,
  213. $this->userManager,
  214. $this->config,
  215. $this->util,
  216. $this->questionHelper
  217. ]
  218. )->setMethods(['setupUserFS', 'moveUserEncryptionFolder'])->getMock();
  219. $this->userManager->expects($this->once())->method('getBackends')
  220. ->willReturn([$this->userInterface]);
  221. $this->userInterface->expects($this->once())->method('getUsers')
  222. ->willReturn(['user1', 'user2']);
  223. $changeKeyStorageRoot->expects($this->exactly(2))->method('setupUserFS');
  224. $changeKeyStorageRoot->expects($this->exactly(2))->method('moveUserEncryptionFolder');
  225. $this->invokePrivate($changeKeyStorageRoot, 'moveUserKeys', ['oldRoot', 'newRoot', $this->outputInterface]);
  226. }
  227. /**
  228. * @dataProvider dataTestMoveUserEncryptionFolder
  229. *
  230. * @param bool $userExists
  231. * @param bool $isDir
  232. * @param bool $targetExists
  233. * @param bool $shouldRename
  234. */
  235. public function testMoveUserEncryptionFolder($userExists, $isDir, $targetExists, $shouldRename) {
  236. $changeKeyStorageRoot = $this->getMockBuilder('OC\Core\Command\Encryption\ChangeKeyStorageRoot')
  237. ->setConstructorArgs(
  238. [
  239. $this->view,
  240. $this->userManager,
  241. $this->config,
  242. $this->util,
  243. $this->questionHelper
  244. ]
  245. )->setMethods(['targetExists', 'prepareParentFolder'])->getMock();
  246. $this->userManager->expects($this->once())->method('userExists')
  247. ->willReturn($userExists);
  248. $this->view->expects($this->any())->method('is_dir')
  249. ->willReturn($isDir);
  250. $changeKeyStorageRoot->expects($this->any())->method('targetExists')
  251. ->willReturn($targetExists);
  252. if ($shouldRename) {
  253. $changeKeyStorageRoot->expects($this->once())->method('prepareParentFolder')
  254. ->with('newRoot/user1');
  255. $this->view->expects($this->once())->method('rename')
  256. ->with('oldRoot/user1/files_encryption', 'newRoot/user1/files_encryption');
  257. } else {
  258. $changeKeyStorageRoot->expects($this->never())->method('prepareParentFolder');
  259. $this->view->expects($this->never())->method('rename');
  260. }
  261. $this->invokePrivate($changeKeyStorageRoot, 'moveUserEncryptionFolder', ['user1', 'oldRoot', 'newRoot']);
  262. }
  263. public function dataTestMoveUserEncryptionFolder() {
  264. return [
  265. [true, true, false, true],
  266. [true, false, true, false],
  267. [false, true, true, false],
  268. [false, false, true, false],
  269. [false, true, false, false],
  270. [false, true, true, false],
  271. [false, false, false, false]
  272. ];
  273. }
  274. /**
  275. * @dataProvider dataTestPrepareParentFolder
  276. */
  277. public function testPrepareParentFolder($path, $pathExists) {
  278. $this->view->expects($this->any())->method('file_exists')
  279. ->willReturnCallback(
  280. function ($fileExistsPath) use ($path, $pathExists) {
  281. if ($path === $fileExistsPath) {
  282. return $pathExists;
  283. }
  284. return false;
  285. }
  286. );
  287. if ($pathExists === false) {
  288. $subDirs = explode('/', ltrim($path, '/'));
  289. $this->view->expects($this->exactly(count($subDirs)))->method('mkdir');
  290. } else {
  291. $this->view->expects($this->never())->method('mkdir');
  292. }
  293. $this->invokePrivate(
  294. $this->changeKeyStorageRoot,
  295. 'prepareParentFolder',
  296. [$path]
  297. );
  298. }
  299. public function dataTestPrepareParentFolder() {
  300. return [
  301. ['/user/folder/sub_folder/keystorage', true],
  302. ['/user/folder/sub_folder/keystorage', false]
  303. ];
  304. }
  305. public function testTargetExists() {
  306. $this->view->expects($this->once())->method('file_exists')->with('path')
  307. ->willReturn(false);
  308. $this->assertFalse(
  309. $this->invokePrivate($this->changeKeyStorageRoot, 'targetExists', ['path'])
  310. );
  311. }
  312. public function testTargetExistsException() {
  313. $this->expectException(\Exception::class);
  314. $this->view->expects($this->once())->method('file_exists')->with('path')
  315. ->willReturn(true);
  316. $this->invokePrivate($this->changeKeyStorageRoot, 'targetExists', ['path']);
  317. }
  318. }