EncryptAllTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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\L10N\IFactory;
  39. use OCP\Mail\IMailer;
  40. use OCP\Security\ISecureRandom;
  41. use OCP\UserInterface;
  42. use Symfony\Component\Console\Formatter\OutputFormatterInterface;
  43. use Symfony\Component\Console\Helper\ProgressBar;
  44. use Symfony\Component\Console\Helper\QuestionHelper;
  45. use Symfony\Component\Console\Input\InputInterface;
  46. use Symfony\Component\Console\Output\OutputInterface;
  47. use Test\TestCase;
  48. class EncryptAllTest extends TestCase {
  49. /** @var \PHPUnit\Framework\MockObject\MockObject | \OCA\Encryption\KeyManager */
  50. protected $keyManager;
  51. /** @var \PHPUnit\Framework\MockObject\MockObject | \OCA\Encryption\Util */
  52. protected $util;
  53. /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\IUserManager */
  54. protected $userManager;
  55. /** @var \PHPUnit\Framework\MockObject\MockObject | \OCA\Encryption\Users\Setup */
  56. protected $setupUser;
  57. /** @var \PHPUnit\Framework\MockObject\MockObject | \OC\Files\View */
  58. protected $view;
  59. /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\IConfig */
  60. protected $config;
  61. /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\Mail\IMailer */
  62. protected $mailer;
  63. /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\IL10N */
  64. protected $l;
  65. /** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Helper\QuestionHelper */
  66. protected $questionHelper;
  67. /** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Input\InputInterface */
  68. protected $inputInterface;
  69. /** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Output\OutputInterface */
  70. protected $outputInterface;
  71. /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\UserInterface */
  72. protected $userInterface;
  73. /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\Security\ISecureRandom */
  74. protected $secureRandom;
  75. /** @var EncryptAll */
  76. protected $encryptAll;
  77. protected function setUp(): void {
  78. parent::setUp();
  79. $this->setupUser = $this->getMockBuilder(Setup::class)
  80. ->disableOriginalConstructor()->getMock();
  81. $this->keyManager = $this->getMockBuilder(KeyManager::class)
  82. ->disableOriginalConstructor()->getMock();
  83. $this->util = $this->getMockBuilder(Util::class)
  84. ->disableOriginalConstructor()->getMock();
  85. $this->userManager = $this->getMockBuilder(IUserManager::class)
  86. ->disableOriginalConstructor()->getMock();
  87. $this->view = $this->getMockBuilder(View::class)
  88. ->disableOriginalConstructor()->getMock();
  89. $this->config = $this->getMockBuilder(IConfig::class)
  90. ->disableOriginalConstructor()->getMock();
  91. $this->mailer = $this->getMockBuilder(IMailer::class)
  92. ->disableOriginalConstructor()->getMock();
  93. $this->l10nFactory = $this->createMock(IFactory::class);
  94. $this->l = $this->getMockBuilder(IL10N::class)
  95. ->disableOriginalConstructor()->getMock();
  96. $this->questionHelper = $this->getMockBuilder(QuestionHelper::class)
  97. ->disableOriginalConstructor()->getMock();
  98. $this->inputInterface = $this->getMockBuilder(InputInterface::class)
  99. ->disableOriginalConstructor()->getMock();
  100. $this->outputInterface = $this->getMockBuilder(OutputInterface::class)
  101. ->disableOriginalConstructor()->getMock();
  102. $this->userInterface = $this->getMockBuilder(UserInterface::class)
  103. ->disableOriginalConstructor()->getMock();
  104. /* We need format method to return a string */
  105. $outputFormatter = $this->createMock(OutputFormatterInterface::class);
  106. $outputFormatter->method('isDecorated')->willReturn(false);
  107. $outputFormatter->method('format')->willReturnArgument(0);
  108. $this->outputInterface->expects($this->any())->method('getFormatter')
  109. ->willReturn($outputFormatter);
  110. $this->userManager->expects($this->any())->method('getBackends')->willReturn([$this->userInterface]);
  111. $this->userInterface->expects($this->any())->method('getUsers')->willReturn(['user1', 'user2']);
  112. $this->secureRandom = $this->getMockBuilder(ISecureRandom::class)->disableOriginalConstructor()->getMock();
  113. $this->secureRandom->expects($this->any())->method('generate')->willReturn('12345678');
  114. $this->encryptAll = new EncryptAll(
  115. $this->setupUser,
  116. $this->userManager,
  117. $this->view,
  118. $this->keyManager,
  119. $this->util,
  120. $this->config,
  121. $this->mailer,
  122. $this->l,
  123. $this->l10nFactory,
  124. $this->questionHelper,
  125. $this->secureRandom
  126. );
  127. }
  128. public function testEncryptAll() {
  129. /** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */
  130. $encryptAll = $this->getMockBuilder(EncryptAll::class)
  131. ->setConstructorArgs(
  132. [
  133. $this->setupUser,
  134. $this->userManager,
  135. $this->view,
  136. $this->keyManager,
  137. $this->util,
  138. $this->config,
  139. $this->mailer,
  140. $this->l,
  141. $this->l10nFactory,
  142. $this->questionHelper,
  143. $this->secureRandom
  144. ]
  145. )
  146. ->setMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords'])
  147. ->getMock();
  148. $this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false);
  149. $encryptAll->expects($this->once())->method('createKeyPairs')->with();
  150. $encryptAll->expects($this->once())->method('outputPasswords')->with();
  151. $encryptAll->expects($this->once())->method('encryptAllUsersFiles')->with();
  152. $encryptAll->encryptAll($this->inputInterface, $this->outputInterface);
  153. }
  154. public function testEncryptAllWithMasterKey() {
  155. /** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */
  156. $encryptAll = $this->getMockBuilder(EncryptAll::class)
  157. ->setConstructorArgs(
  158. [
  159. $this->setupUser,
  160. $this->userManager,
  161. $this->view,
  162. $this->keyManager,
  163. $this->util,
  164. $this->config,
  165. $this->mailer,
  166. $this->l,
  167. $this->l10nFactory,
  168. $this->questionHelper,
  169. $this->secureRandom
  170. ]
  171. )
  172. ->setMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords'])
  173. ->getMock();
  174. $this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(true);
  175. $encryptAll->expects($this->never())->method('createKeyPairs');
  176. $this->keyManager->expects($this->once())->method('validateMasterKey');
  177. $encryptAll->expects($this->once())->method('encryptAllUsersFiles')->with();
  178. $encryptAll->expects($this->never())->method('outputPasswords');
  179. $encryptAll->encryptAll($this->inputInterface, $this->outputInterface);
  180. }
  181. public function testCreateKeyPairs() {
  182. /** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */
  183. $encryptAll = $this->getMockBuilder(EncryptAll::class)
  184. ->setConstructorArgs(
  185. [
  186. $this->setupUser,
  187. $this->userManager,
  188. $this->view,
  189. $this->keyManager,
  190. $this->util,
  191. $this->config,
  192. $this->mailer,
  193. $this->l,
  194. $this->l10nFactory,
  195. $this->questionHelper,
  196. $this->secureRandom
  197. ]
  198. )
  199. ->setMethods(['setupUserFS', 'generateOneTimePassword'])
  200. ->getMock();
  201. // set protected property $output
  202. $this->invokePrivate($encryptAll, 'output', [$this->outputInterface]);
  203. $this->keyManager->expects($this->exactly(2))->method('userHasKeys')
  204. ->willReturnCallback(
  205. function ($user) {
  206. if ($user === 'user1') {
  207. return false;
  208. }
  209. return true;
  210. }
  211. );
  212. $encryptAll->expects($this->once())->method('setupUserFS')->with('user1');
  213. $encryptAll->expects($this->once())->method('generateOneTimePassword')->with('user1')->willReturn('password');
  214. $this->setupUser->expects($this->once())->method('setupUser')->with('user1', 'password');
  215. $this->invokePrivate($encryptAll, 'createKeyPairs');
  216. $userPasswords = $this->invokePrivate($encryptAll, 'userPasswords');
  217. // we only expect the skipped user, because generateOneTimePassword which
  218. // would set the user with the new password was mocked.
  219. // This method will be tested separately
  220. $this->assertSame(1, count($userPasswords));
  221. $this->assertSame('', $userPasswords['user2']);
  222. }
  223. public function testEncryptAllUsersFiles() {
  224. /** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */
  225. $encryptAll = $this->getMockBuilder(EncryptAll::class)
  226. ->setConstructorArgs(
  227. [
  228. $this->setupUser,
  229. $this->userManager,
  230. $this->view,
  231. $this->keyManager,
  232. $this->util,
  233. $this->config,
  234. $this->mailer,
  235. $this->l,
  236. $this->l10nFactory,
  237. $this->questionHelper,
  238. $this->secureRandom
  239. ]
  240. )
  241. ->setMethods(['encryptUsersFiles'])
  242. ->getMock();
  243. $this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false);
  244. // set protected property $output
  245. $this->invokePrivate($encryptAll, 'output', [$this->outputInterface]);
  246. $this->invokePrivate($encryptAll, 'userPasswords', [['user1' => 'pwd1', 'user2' => 'pwd2']]);
  247. $encryptAll->expects($this->exactly(2))->method('encryptUsersFiles')
  248. ->withConsecutive(
  249. ['user1'],
  250. ['user2'],
  251. );
  252. $this->invokePrivate($encryptAll, 'encryptAllUsersFiles');
  253. }
  254. public function testEncryptUsersFiles() {
  255. /** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */
  256. $encryptAll = $this->getMockBuilder(EncryptAll::class)
  257. ->setConstructorArgs(
  258. [
  259. $this->setupUser,
  260. $this->userManager,
  261. $this->view,
  262. $this->keyManager,
  263. $this->util,
  264. $this->config,
  265. $this->mailer,
  266. $this->l,
  267. $this->l10nFactory,
  268. $this->questionHelper,
  269. $this->secureRandom
  270. ]
  271. )
  272. ->setMethods(['encryptFile', 'setupUserFS'])
  273. ->getMock();
  274. $this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false);
  275. $this->view->expects($this->exactly(2))->method('getDirectoryContent')
  276. ->withConsecutive(
  277. ['/user1/files'],
  278. ['/user1/files/foo'],
  279. )->willReturnOnConsecutiveCalls(
  280. [
  281. ['name' => 'foo', 'type' => 'dir'],
  282. ['name' => 'bar', 'type' => 'file'],
  283. ],
  284. [
  285. ['name' => 'subfile', 'type' => 'file']
  286. ]
  287. );
  288. $this->view->expects($this->any())->method('is_dir')
  289. ->willReturnCallback(
  290. function ($path) {
  291. if ($path === '/user1/files/foo') {
  292. return true;
  293. }
  294. return false;
  295. }
  296. );
  297. $encryptAll->expects($this->exactly(2))->method('encryptFile')
  298. ->withConsecutive(
  299. ['/user1/files/bar'],
  300. ['/user1/files/foo/subfile'],
  301. );
  302. $outputFormatter = $this->createMock(OutputFormatterInterface::class);
  303. $outputFormatter->method('isDecorated')->willReturn(false);
  304. $this->outputInterface->expects($this->any())
  305. ->method('getFormatter')
  306. ->willReturn($outputFormatter);
  307. $progressBar = new ProgressBar($this->outputInterface);
  308. $this->invokePrivate($encryptAll, 'encryptUsersFiles', ['user1', $progressBar, '']);
  309. }
  310. public function testGenerateOneTimePassword() {
  311. $password = $this->invokePrivate($this->encryptAll, 'generateOneTimePassword', ['user1']);
  312. $this->assertTrue(is_string($password));
  313. $this->assertSame(8, strlen($password));
  314. $userPasswords = $this->invokePrivate($this->encryptAll, 'userPasswords');
  315. $this->assertSame(1, count($userPasswords));
  316. $this->assertSame($password, $userPasswords['user1']);
  317. }
  318. /**
  319. * @dataProvider dataTestEncryptFile
  320. * @param $isEncrypted
  321. */
  322. public function testEncryptFile($isEncrypted) {
  323. $fileInfo = $this->createMock(FileInfo::class);
  324. $fileInfo->expects($this->any())->method('isEncrypted')
  325. ->willReturn($isEncrypted);
  326. $this->view->expects($this->any())->method('getFileInfo')
  327. ->willReturn($fileInfo);
  328. if ($isEncrypted) {
  329. $this->view->expects($this->never())->method('copy');
  330. $this->view->expects($this->never())->method('rename');
  331. } else {
  332. $this->view->expects($this->once())->method('copy');
  333. $this->view->expects($this->once())->method('rename');
  334. }
  335. $this->assertTrue(
  336. $this->invokePrivate($this->encryptAll, 'encryptFile', ['foo.txt'])
  337. );
  338. }
  339. public function dataTestEncryptFile() {
  340. return [
  341. [true],
  342. [false],
  343. ];
  344. }
  345. }