ChangeKeyStorageRootTest.php 12 KB

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