DecryptAllTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 Test\Encryption;
  22. use OC\Encryption\DecryptAll;
  23. use OC\Encryption\Exceptions\DecryptionFailedException;
  24. use OC\Encryption\Manager;
  25. use OC\Files\FileInfo;
  26. use OC\Files\View;
  27. use OCP\Files\Storage;
  28. use OCP\IUserManager;
  29. use OCP\UserInterface;
  30. use Symfony\Component\Console\Formatter\OutputFormatterInterface;
  31. use Symfony\Component\Console\Helper\ProgressBar;
  32. use Symfony\Component\Console\Input\InputInterface;
  33. use Symfony\Component\Console\Output\ConsoleOutputInterface;
  34. use Symfony\Component\Console\Output\OutputInterface;
  35. use Test\TestCase;
  36. /**
  37. * Class DecryptAllTest
  38. *
  39. * @group DB
  40. *
  41. * @package Test\Encryption
  42. */
  43. class DecryptAllTest extends TestCase {
  44. /** @var \PHPUnit_Framework_MockObject_MockObject | IUserManager */
  45. protected $userManager;
  46. /** @var \PHPUnit_Framework_MockObject_MockObject | Manager */
  47. protected $encryptionManager;
  48. /** @var \PHPUnit_Framework_MockObject_MockObject | View */
  49. protected $view;
  50. /** @var \PHPUnit_Framework_MockObject_MockObject | \Symfony\Component\Console\Input\InputInterface */
  51. protected $inputInterface;
  52. /** @var \PHPUnit_Framework_MockObject_MockObject | \Symfony\Component\Console\Output\OutputInterface */
  53. protected $outputInterface;
  54. /** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\UserInterface */
  55. protected $userInterface;
  56. /** @var DecryptAll */
  57. protected $instance;
  58. public function setUp() {
  59. parent::setUp();
  60. $this->userManager = $this->getMockBuilder(IUserManager::class)
  61. ->disableOriginalConstructor()->getMock();
  62. $this->encryptionManager = $this->getMockBuilder('OC\Encryption\Manager')
  63. ->disableOriginalConstructor()->getMock();
  64. $this->view = $this->getMockBuilder(View::class)
  65. ->disableOriginalConstructor()->getMock();
  66. $this->inputInterface = $this->getMockBuilder(InputInterface::class)
  67. ->disableOriginalConstructor()->getMock();
  68. $this->outputInterface = $this->getMockBuilder(OutputInterface::class)
  69. ->disableOriginalConstructor()->getMock();
  70. $this->userInterface = $this->getMockBuilder(UserInterface::class)
  71. ->disableOriginalConstructor()->getMock();
  72. $this->outputInterface->expects($this->any())->method('getFormatter')
  73. ->willReturn($this->createMock(OutputFormatterInterface::class));
  74. $this->instance = new DecryptAll($this->encryptionManager, $this->userManager, $this->view);
  75. $this->invokePrivate($this->instance, 'input', [$this->inputInterface]);
  76. $this->invokePrivate($this->instance, 'output', [$this->outputInterface]);
  77. }
  78. public function dataDecryptAll() {
  79. return [
  80. [true, 'user1', true],
  81. [false, 'user1', true],
  82. [true, '0', true],
  83. [false, '0', true],
  84. [true, '', false],
  85. ];
  86. }
  87. /**
  88. * @dataProvider dataDecryptAll
  89. * @param bool $prepareResult
  90. * @param string $user
  91. * @param bool $userExistsChecked
  92. */
  93. public function testDecryptAll($prepareResult, $user, $userExistsChecked) {
  94. if ($userExistsChecked) {
  95. $this->userManager->expects($this->once())->method('userExists')->willReturn(true);
  96. } else {
  97. $this->userManager->expects($this->never())->method('userExists');
  98. }
  99. /** @var DecryptAll | \PHPUnit_Framework_MockObject_MockObject | $instance */
  100. $instance = $this->getMockBuilder('OC\Encryption\DecryptAll')
  101. ->setConstructorArgs(
  102. [
  103. $this->encryptionManager,
  104. $this->userManager,
  105. $this->view
  106. ]
  107. )
  108. ->setMethods(['prepareEncryptionModules', 'decryptAllUsersFiles'])
  109. ->getMock();
  110. $instance->expects($this->once())
  111. ->method('prepareEncryptionModules')
  112. ->with($user)
  113. ->willReturn($prepareResult);
  114. if ($prepareResult) {
  115. $instance->expects($this->once())
  116. ->method('decryptAllUsersFiles')
  117. ->with($user);
  118. } else {
  119. $instance->expects($this->never())->method('decryptAllUsersFiles');
  120. }
  121. $instance->decryptAll($this->inputInterface, $this->outputInterface, $user);
  122. }
  123. /**
  124. * test decrypt all call with a user who doesn't exists
  125. */
  126. public function testDecryptAllWrongUser() {
  127. $this->userManager->expects($this->once())->method('userExists')->willReturn(false);
  128. $this->outputInterface->expects($this->once())->method('writeln')
  129. ->with('User "user1" does not exist. Please check the username and try again');
  130. $this->assertFalse(
  131. $this->instance->decryptAll($this->inputInterface, $this->outputInterface, 'user1')
  132. );
  133. }
  134. public function dataTrueFalse() {
  135. return [
  136. [true],
  137. [false],
  138. ];
  139. }
  140. /**
  141. * @dataProvider dataTrueFalse
  142. * @param bool $success
  143. */
  144. public function testPrepareEncryptionModules($success) {
  145. $user = 'user1';
  146. $dummyEncryptionModule = $this->getMockBuilder('OCP\Encryption\IEncryptionModule')
  147. ->disableOriginalConstructor()->getMock();
  148. $dummyEncryptionModule->expects($this->once())
  149. ->method('prepareDecryptAll')
  150. ->with($this->inputInterface, $this->outputInterface, $user)
  151. ->willReturn($success);
  152. $callback = function() use ($dummyEncryptionModule) {return $dummyEncryptionModule;};
  153. $moduleDescription = [
  154. 'id' => 'id',
  155. 'displayName' => 'displayName',
  156. 'callback' => $callback
  157. ];
  158. $this->encryptionManager->expects($this->once())
  159. ->method('getEncryptionModules')
  160. ->willReturn([$moduleDescription]);
  161. $this->assertSame($success,
  162. $this->invokePrivate($this->instance, 'prepareEncryptionModules', [$user])
  163. );
  164. }
  165. /**
  166. * @dataProvider dataTestDecryptAllUsersFiles
  167. */
  168. public function testDecryptAllUsersFiles($user) {
  169. /** @var DecryptAll | \PHPUnit_Framework_MockObject_MockObject | $instance */
  170. $instance = $this->getMockBuilder('OC\Encryption\DecryptAll')
  171. ->setConstructorArgs(
  172. [
  173. $this->encryptionManager,
  174. $this->userManager,
  175. $this->view
  176. ]
  177. )
  178. ->setMethods(['decryptUsersFiles'])
  179. ->getMock();
  180. $this->invokePrivate($instance, 'input', [$this->inputInterface]);
  181. $this->invokePrivate($instance, 'output', [$this->outputInterface]);
  182. if (empty($user)) {
  183. $this->userManager->expects($this->once())
  184. ->method('getBackends')
  185. ->willReturn([$this->userInterface]);
  186. $this->userInterface->expects($this->any())
  187. ->method('getUsers')
  188. ->willReturn(['user1', 'user2']);
  189. $instance->expects($this->at(0))
  190. ->method('decryptUsersFiles')
  191. ->with('user1');
  192. $instance->expects($this->at(1))
  193. ->method('decryptUsersFiles')
  194. ->with('user2');
  195. } else {
  196. $instance->expects($this->once())
  197. ->method('decryptUsersFiles')
  198. ->with($user);
  199. }
  200. $this->invokePrivate($instance, 'decryptAllUsersFiles', [$user]);
  201. }
  202. public function dataTestDecryptAllUsersFiles() {
  203. return [
  204. ['user1'],
  205. ['']
  206. ];
  207. }
  208. public function testDecryptUsersFiles() {
  209. /** @var DecryptAll | \PHPUnit_Framework_MockObject_MockObject $instance */
  210. $instance = $this->getMockBuilder('OC\Encryption\DecryptAll')
  211. ->setConstructorArgs(
  212. [
  213. $this->encryptionManager,
  214. $this->userManager,
  215. $this->view
  216. ]
  217. )
  218. ->setMethods(['decryptFile'])
  219. ->getMock();
  220. $storage = $this->getMockBuilder(Storage::class)
  221. ->disableOriginalConstructor()->getMock();
  222. $sharedStorage = $this->getMockBuilder(Storage::class)
  223. ->disableOriginalConstructor()->getMock();
  224. $sharedStorage->expects($this->once())->method('instanceOfStorage')
  225. ->with('OCA\Files_Sharing\SharedStorage')->willReturn(true);
  226. $this->view->expects($this->at(0))->method('getDirectoryContent')
  227. ->with('/user1/files')->willReturn(
  228. [
  229. new FileInfo('path', $storage, 'intPath', ['name' => 'foo', 'type'=>'dir'], null),
  230. new FileInfo('path', $storage, 'intPath', ['name' => 'bar', 'type'=>'file', 'encrypted'=>true], null),
  231. new FileInfo('path', $sharedStorage, 'intPath', ['name' => 'shared', 'type'=>'file', 'encrypted'=>true], null),
  232. ]
  233. );
  234. $this->view->expects($this->at(3))->method('getDirectoryContent')
  235. ->with('/user1/files/foo')->willReturn(
  236. [
  237. new FileInfo('path', $storage, 'intPath', ['name' => 'subfile', 'type'=>'file', 'encrypted'=>true], null)
  238. ]
  239. );
  240. $this->view->expects($this->any())->method('is_dir')
  241. ->willReturnCallback(
  242. function($path) {
  243. if ($path === '/user1/files/foo') {
  244. return true;
  245. }
  246. return false;
  247. }
  248. );
  249. $instance->expects($this->at(0))
  250. ->method('decryptFile')
  251. ->with('/user1/files/bar');
  252. $instance->expects($this->at(1))
  253. ->method('decryptFile')
  254. ->with('/user1/files/foo/subfile');
  255. $output = $this->createMock(OutputInterface::class);
  256. $output->expects($this->any())
  257. ->method('getFormatter')
  258. ->willReturn($this->createMock(OutputFormatterInterface::class));
  259. $progressBar = new ProgressBar($output);
  260. $this->invokePrivate($instance, 'decryptUsersFiles', ['user1', $progressBar, '']);
  261. }
  262. public function testDecryptFile() {
  263. $path = 'test.txt';
  264. /** @var DecryptAll | \PHPUnit_Framework_MockObject_MockObject $instance */
  265. $instance = $this->getMockBuilder('OC\Encryption\DecryptAll')
  266. ->setConstructorArgs(
  267. [
  268. $this->encryptionManager,
  269. $this->userManager,
  270. $this->view
  271. ]
  272. )
  273. ->setMethods(['getTimestamp'])
  274. ->getMock();
  275. $instance->expects($this->any())->method('getTimestamp')->willReturn(42);
  276. $this->view->expects($this->once())
  277. ->method('copy')
  278. ->with($path, $path . '.decrypted.42');
  279. $this->view->expects($this->once())
  280. ->method('rename')
  281. ->with($path . '.decrypted.42', $path);
  282. $this->assertTrue(
  283. $this->invokePrivate($instance, 'decryptFile', [$path])
  284. );
  285. }
  286. public function testDecryptFileFailure() {
  287. $path = 'test.txt';
  288. /** @var DecryptAll | \PHPUnit_Framework_MockObject_MockObject $instance */
  289. $instance = $this->getMockBuilder('OC\Encryption\DecryptAll')
  290. ->setConstructorArgs(
  291. [
  292. $this->encryptionManager,
  293. $this->userManager,
  294. $this->view
  295. ]
  296. )
  297. ->setMethods(['getTimestamp'])
  298. ->getMock();
  299. $instance->expects($this->any())->method('getTimestamp')->willReturn(42);
  300. $this->view->expects($this->once())
  301. ->method('copy')
  302. ->with($path, $path . '.decrypted.42')
  303. ->willReturnCallback(function() { throw new DecryptionFailedException();});
  304. $this->view->expects($this->never())->method('rename');
  305. $this->view->expects($this->once())
  306. ->method('file_exists')
  307. ->with($path . '.decrypted.42')
  308. ->willReturn(true);
  309. $this->view->expects($this->once())
  310. ->method('unlink')
  311. ->with($path . '.decrypted.42');
  312. $this->assertFalse(
  313. $this->invokePrivate($instance, 'decryptFile', [$path])
  314. );
  315. }
  316. }