ChangeKeyStorageRootTest.php 12 KB

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