changekeystorageroottest.php 11 KB

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