EncryptAllTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Kenneth Newwood <kenneth@newwood.name>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\Encryption\Tests\Crypto;
  29. use OC\Files\View;
  30. use OCA\Encryption\Crypto\EncryptAll;
  31. use OCA\Encryption\KeyManager;
  32. use OCA\Encryption\Users\Setup;
  33. use OCA\Encryption\Util;
  34. use OCP\Files\FileInfo;
  35. use OCP\IConfig;
  36. use OCP\IL10N;
  37. use OCP\IUserManager;
  38. use OCP\Mail\IMailer;
  39. use OCP\Security\ISecureRandom;
  40. use OCP\UserInterface;
  41. use Symfony\Component\Console\Formatter\OutputFormatterInterface;
  42. use Symfony\Component\Console\Helper\ProgressBar;
  43. use Symfony\Component\Console\Helper\QuestionHelper;
  44. use Symfony\Component\Console\Input\InputInterface;
  45. use Symfony\Component\Console\Output\OutputInterface;
  46. use Test\TestCase;
  47. class EncryptAllTest extends TestCase {
  48. /** @var \PHPUnit\Framework\MockObject\MockObject | \OCA\Encryption\KeyManager */
  49. protected $keyManager;
  50. /** @var \PHPUnit\Framework\MockObject\MockObject | \OCA\Encryption\Util */
  51. protected $util;
  52. /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\IUserManager */
  53. protected $userManager;
  54. /** @var \PHPUnit\Framework\MockObject\MockObject | \OCA\Encryption\Users\Setup */
  55. protected $setupUser;
  56. /** @var \PHPUnit\Framework\MockObject\MockObject | \OC\Files\View */
  57. protected $view;
  58. /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\IConfig */
  59. protected $config;
  60. /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\Mail\IMailer */
  61. protected $mailer;
  62. /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\IL10N */
  63. protected $l;
  64. /** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Helper\QuestionHelper */
  65. protected $questionHelper;
  66. /** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Input\InputInterface */
  67. protected $inputInterface;
  68. /** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Output\OutputInterface */
  69. protected $outputInterface;
  70. /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\UserInterface */
  71. protected $userInterface;
  72. /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\Security\ISecureRandom */
  73. protected $secureRandom;
  74. /** @var EncryptAll */
  75. protected $encryptAll;
  76. protected function setUp(): void {
  77. parent::setUp();
  78. $this->setupUser = $this->getMockBuilder(Setup::class)
  79. ->disableOriginalConstructor()->getMock();
  80. $this->keyManager = $this->getMockBuilder(KeyManager::class)
  81. ->disableOriginalConstructor()->getMock();
  82. $this->util = $this->getMockBuilder(Util::class)
  83. ->disableOriginalConstructor()->getMock();
  84. $this->userManager = $this->getMockBuilder(IUserManager::class)
  85. ->disableOriginalConstructor()->getMock();
  86. $this->view = $this->getMockBuilder(View::class)
  87. ->disableOriginalConstructor()->getMock();
  88. $this->config = $this->getMockBuilder(IConfig::class)
  89. ->disableOriginalConstructor()->getMock();
  90. $this->mailer = $this->getMockBuilder(IMailer::class)
  91. ->disableOriginalConstructor()->getMock();
  92. $this->l = $this->getMockBuilder(IL10N::class)
  93. ->disableOriginalConstructor()->getMock();
  94. $this->questionHelper = $this->getMockBuilder(QuestionHelper::class)
  95. ->disableOriginalConstructor()->getMock();
  96. $this->inputInterface = $this->getMockBuilder(InputInterface::class)
  97. ->disableOriginalConstructor()->getMock();
  98. $this->outputInterface = $this->getMockBuilder(OutputInterface::class)
  99. ->disableOriginalConstructor()->getMock();
  100. $this->userInterface = $this->getMockBuilder(UserInterface::class)
  101. ->disableOriginalConstructor()->getMock();
  102. /* We need format method to return a string */
  103. $outputFormatter = $this->createMock(OutputFormatterInterface::class);
  104. $outputFormatter->method('format')->willReturnArgument(0);
  105. $this->outputInterface->expects($this->any())->method('getFormatter')
  106. ->willReturn($outputFormatter);
  107. $this->userManager->expects($this->any())->method('getBackends')->willReturn([$this->userInterface]);
  108. $this->userInterface->expects($this->any())->method('getUsers')->willReturn(['user1', 'user2']);
  109. $this->secureRandom = $this->getMockBuilder(ISecureRandom::class)->disableOriginalConstructor()->getMock();
  110. $this->secureRandom->expects($this->any())->method('generate')->willReturn('12345678');
  111. $this->encryptAll = new EncryptAll(
  112. $this->setupUser,
  113. $this->userManager,
  114. $this->view,
  115. $this->keyManager,
  116. $this->util,
  117. $this->config,
  118. $this->mailer,
  119. $this->l,
  120. $this->questionHelper,
  121. $this->secureRandom
  122. );
  123. }
  124. public function testEncryptAll() {
  125. /** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */
  126. $encryptAll = $this->getMockBuilder(EncryptAll::class)
  127. ->setConstructorArgs(
  128. [
  129. $this->setupUser,
  130. $this->userManager,
  131. $this->view,
  132. $this->keyManager,
  133. $this->util,
  134. $this->config,
  135. $this->mailer,
  136. $this->l,
  137. $this->questionHelper,
  138. $this->secureRandom
  139. ]
  140. )
  141. ->setMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords'])
  142. ->getMock();
  143. $this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false);
  144. $encryptAll->expects($this->once())->method('createKeyPairs')->with();
  145. $encryptAll->expects($this->once())->method('outputPasswords')->with();
  146. $encryptAll->expects($this->once())->method('encryptAllUsersFiles')->with();
  147. $encryptAll->encryptAll($this->inputInterface, $this->outputInterface);
  148. }
  149. public function testEncryptAllWithMasterKey() {
  150. /** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */
  151. $encryptAll = $this->getMockBuilder(EncryptAll::class)
  152. ->setConstructorArgs(
  153. [
  154. $this->setupUser,
  155. $this->userManager,
  156. $this->view,
  157. $this->keyManager,
  158. $this->util,
  159. $this->config,
  160. $this->mailer,
  161. $this->l,
  162. $this->questionHelper,
  163. $this->secureRandom
  164. ]
  165. )
  166. ->setMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords'])
  167. ->getMock();
  168. $this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(true);
  169. $encryptAll->expects($this->never())->method('createKeyPairs');
  170. $this->keyManager->expects($this->once())->method('validateMasterKey');
  171. $encryptAll->expects($this->once())->method('encryptAllUsersFiles')->with();
  172. $encryptAll->expects($this->never())->method('outputPasswords');
  173. $encryptAll->encryptAll($this->inputInterface, $this->outputInterface);
  174. }
  175. public function testCreateKeyPairs() {
  176. /** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */
  177. $encryptAll = $this->getMockBuilder(EncryptAll::class)
  178. ->setConstructorArgs(
  179. [
  180. $this->setupUser,
  181. $this->userManager,
  182. $this->view,
  183. $this->keyManager,
  184. $this->util,
  185. $this->config,
  186. $this->mailer,
  187. $this->l,
  188. $this->questionHelper,
  189. $this->secureRandom
  190. ]
  191. )
  192. ->setMethods(['setupUserFS', 'generateOneTimePassword'])
  193. ->getMock();
  194. // set protected property $output
  195. $this->invokePrivate($encryptAll, 'output', [$this->outputInterface]);
  196. $this->keyManager->expects($this->exactly(2))->method('userHasKeys')
  197. ->willReturnCallback(
  198. function ($user) {
  199. if ($user === 'user1') {
  200. return false;
  201. }
  202. return true;
  203. }
  204. );
  205. $encryptAll->expects($this->once())->method('setupUserFS')->with('user1');
  206. $encryptAll->expects($this->once())->method('generateOneTimePassword')->with('user1')->willReturn('password');
  207. $this->setupUser->expects($this->once())->method('setupUser')->with('user1', 'password');
  208. $this->invokePrivate($encryptAll, 'createKeyPairs');
  209. $userPasswords = $this->invokePrivate($encryptAll, 'userPasswords');
  210. // we only expect the skipped user, because generateOneTimePassword which
  211. // would set the user with the new password was mocked.
  212. // This method will be tested separately
  213. $this->assertSame(1, count($userPasswords));
  214. $this->assertSame('', $userPasswords['user2']);
  215. }
  216. public function testEncryptAllUsersFiles() {
  217. /** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */
  218. $encryptAll = $this->getMockBuilder(EncryptAll::class)
  219. ->setConstructorArgs(
  220. [
  221. $this->setupUser,
  222. $this->userManager,
  223. $this->view,
  224. $this->keyManager,
  225. $this->util,
  226. $this->config,
  227. $this->mailer,
  228. $this->l,
  229. $this->questionHelper,
  230. $this->secureRandom
  231. ]
  232. )
  233. ->setMethods(['encryptUsersFiles'])
  234. ->getMock();
  235. $this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false);
  236. // set protected property $output
  237. $this->invokePrivate($encryptAll, 'output', [$this->outputInterface]);
  238. $this->invokePrivate($encryptAll, 'userPasswords', [['user1' => 'pwd1', 'user2' => 'pwd2']]);
  239. $encryptAll->expects($this->exactly(2))->method('encryptUsersFiles')
  240. ->withConsecutive(
  241. ['user1'],
  242. ['user2'],
  243. );
  244. $this->invokePrivate($encryptAll, 'encryptAllUsersFiles');
  245. }
  246. public function testEncryptUsersFiles() {
  247. /** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */
  248. $encryptAll = $this->getMockBuilder(EncryptAll::class)
  249. ->setConstructorArgs(
  250. [
  251. $this->setupUser,
  252. $this->userManager,
  253. $this->view,
  254. $this->keyManager,
  255. $this->util,
  256. $this->config,
  257. $this->mailer,
  258. $this->l,
  259. $this->questionHelper,
  260. $this->secureRandom
  261. ]
  262. )
  263. ->setMethods(['encryptFile', 'setupUserFS'])
  264. ->getMock();
  265. $this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false);
  266. $this->view->expects($this->exactly(2))->method('getDirectoryContent')
  267. ->withConsecutive(
  268. ['/user1/files'],
  269. ['/user1/files/foo'],
  270. )->willReturnOnConsecutiveCalls(
  271. [
  272. ['name' => 'foo', 'type' => 'dir'],
  273. ['name' => 'bar', 'type' => 'file'],
  274. ],
  275. [
  276. ['name' => 'subfile', 'type' => 'file']
  277. ]
  278. );
  279. $this->view->expects($this->any())->method('is_dir')
  280. ->willReturnCallback(
  281. function ($path) {
  282. if ($path === '/user1/files/foo') {
  283. return true;
  284. }
  285. return false;
  286. }
  287. );
  288. $encryptAll->expects($this->exactly(2))->method('encryptFile')
  289. ->withConsecutive(
  290. ['/user1/files/bar'],
  291. ['/user1/files/foo/subfile'],
  292. );
  293. $this->outputInterface->expects($this->any())
  294. ->method('getFormatter')
  295. ->willReturn($this->createMock(OutputFormatterInterface::class));
  296. $progressBar = new ProgressBar($this->outputInterface);
  297. $this->invokePrivate($encryptAll, 'encryptUsersFiles', ['user1', $progressBar, '']);
  298. }
  299. public function testGenerateOneTimePassword() {
  300. $password = $this->invokePrivate($this->encryptAll, 'generateOneTimePassword', ['user1']);
  301. $this->assertTrue(is_string($password));
  302. $this->assertSame(8, strlen($password));
  303. $userPasswords = $this->invokePrivate($this->encryptAll, 'userPasswords');
  304. $this->assertSame(1, count($userPasswords));
  305. $this->assertSame($password, $userPasswords['user1']);
  306. }
  307. /**
  308. * @dataProvider dataTestEncryptFile
  309. * @param $isEncrypted
  310. */
  311. public function testEncryptFile($isEncrypted) {
  312. $fileInfo = $this->createMock(FileInfo::class);
  313. $fileInfo->expects($this->any())->method('isEncrypted')
  314. ->willReturn($isEncrypted);
  315. $this->view->expects($this->any())->method('getFileInfo')
  316. ->willReturn($fileInfo);
  317. if ($isEncrypted) {
  318. $this->view->expects($this->never())->method('copy');
  319. $this->view->expects($this->never())->method('rename');
  320. } else {
  321. $this->view->expects($this->once())->method('copy');
  322. $this->view->expects($this->once())->method('rename');
  323. }
  324. $this->assertTrue(
  325. $this->invokePrivate($this->encryptAll, 'encryptFile', ['foo.txt'])
  326. );
  327. }
  328. public function dataTestEncryptFile() {
  329. return [
  330. [true],
  331. [false],
  332. ];
  333. }
  334. }