EncryptAllTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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('format')->willReturnArgument(0);
  107. $this->outputInterface->expects($this->any())->method('getFormatter')
  108. ->willReturn($outputFormatter);
  109. $this->userManager->expects($this->any())->method('getBackends')->willReturn([$this->userInterface]);
  110. $this->userInterface->expects($this->any())->method('getUsers')->willReturn(['user1', 'user2']);
  111. $this->secureRandom = $this->getMockBuilder(ISecureRandom::class)->disableOriginalConstructor()->getMock();
  112. $this->secureRandom->expects($this->any())->method('generate')->willReturn('12345678');
  113. $this->encryptAll = new EncryptAll(
  114. $this->setupUser,
  115. $this->userManager,
  116. $this->view,
  117. $this->keyManager,
  118. $this->util,
  119. $this->config,
  120. $this->mailer,
  121. $this->l,
  122. $this->l10nFactory,
  123. $this->questionHelper,
  124. $this->secureRandom
  125. );
  126. }
  127. public function testEncryptAll() {
  128. /** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */
  129. $encryptAll = $this->getMockBuilder(EncryptAll::class)
  130. ->setConstructorArgs(
  131. [
  132. $this->setupUser,
  133. $this->userManager,
  134. $this->view,
  135. $this->keyManager,
  136. $this->util,
  137. $this->config,
  138. $this->mailer,
  139. $this->l,
  140. $this->l10nFactory,
  141. $this->questionHelper,
  142. $this->secureRandom
  143. ]
  144. )
  145. ->setMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords'])
  146. ->getMock();
  147. $this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false);
  148. $encryptAll->expects($this->once())->method('createKeyPairs')->with();
  149. $encryptAll->expects($this->once())->method('outputPasswords')->with();
  150. $encryptAll->expects($this->once())->method('encryptAllUsersFiles')->with();
  151. $encryptAll->encryptAll($this->inputInterface, $this->outputInterface);
  152. }
  153. public function testEncryptAllWithMasterKey() {
  154. /** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */
  155. $encryptAll = $this->getMockBuilder(EncryptAll::class)
  156. ->setConstructorArgs(
  157. [
  158. $this->setupUser,
  159. $this->userManager,
  160. $this->view,
  161. $this->keyManager,
  162. $this->util,
  163. $this->config,
  164. $this->mailer,
  165. $this->l,
  166. $this->l10nFactory,
  167. $this->questionHelper,
  168. $this->secureRandom
  169. ]
  170. )
  171. ->setMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords'])
  172. ->getMock();
  173. $this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(true);
  174. $encryptAll->expects($this->never())->method('createKeyPairs');
  175. $this->keyManager->expects($this->once())->method('validateMasterKey');
  176. $encryptAll->expects($this->once())->method('encryptAllUsersFiles')->with();
  177. $encryptAll->expects($this->never())->method('outputPasswords');
  178. $encryptAll->encryptAll($this->inputInterface, $this->outputInterface);
  179. }
  180. public function testCreateKeyPairs() {
  181. /** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */
  182. $encryptAll = $this->getMockBuilder(EncryptAll::class)
  183. ->setConstructorArgs(
  184. [
  185. $this->setupUser,
  186. $this->userManager,
  187. $this->view,
  188. $this->keyManager,
  189. $this->util,
  190. $this->config,
  191. $this->mailer,
  192. $this->l,
  193. $this->l10nFactory,
  194. $this->questionHelper,
  195. $this->secureRandom
  196. ]
  197. )
  198. ->setMethods(['setupUserFS', 'generateOneTimePassword'])
  199. ->getMock();
  200. // set protected property $output
  201. $this->invokePrivate($encryptAll, 'output', [$this->outputInterface]);
  202. $this->keyManager->expects($this->exactly(2))->method('userHasKeys')
  203. ->willReturnCallback(
  204. function ($user) {
  205. if ($user === 'user1') {
  206. return false;
  207. }
  208. return true;
  209. }
  210. );
  211. $encryptAll->expects($this->once())->method('setupUserFS')->with('user1');
  212. $encryptAll->expects($this->once())->method('generateOneTimePassword')->with('user1')->willReturn('password');
  213. $this->setupUser->expects($this->once())->method('setupUser')->with('user1', 'password');
  214. $this->invokePrivate($encryptAll, 'createKeyPairs');
  215. $userPasswords = $this->invokePrivate($encryptAll, 'userPasswords');
  216. // we only expect the skipped user, because generateOneTimePassword which
  217. // would set the user with the new password was mocked.
  218. // This method will be tested separately
  219. $this->assertSame(1, count($userPasswords));
  220. $this->assertSame('', $userPasswords['user2']);
  221. }
  222. public function testEncryptAllUsersFiles() {
  223. /** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */
  224. $encryptAll = $this->getMockBuilder(EncryptAll::class)
  225. ->setConstructorArgs(
  226. [
  227. $this->setupUser,
  228. $this->userManager,
  229. $this->view,
  230. $this->keyManager,
  231. $this->util,
  232. $this->config,
  233. $this->mailer,
  234. $this->l,
  235. $this->l10nFactory,
  236. $this->questionHelper,
  237. $this->secureRandom
  238. ]
  239. )
  240. ->setMethods(['encryptUsersFiles'])
  241. ->getMock();
  242. $this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false);
  243. // set protected property $output
  244. $this->invokePrivate($encryptAll, 'output', [$this->outputInterface]);
  245. $this->invokePrivate($encryptAll, 'userPasswords', [['user1' => 'pwd1', 'user2' => 'pwd2']]);
  246. $encryptAll->expects($this->exactly(2))->method('encryptUsersFiles')
  247. ->withConsecutive(
  248. ['user1'],
  249. ['user2'],
  250. );
  251. $this->invokePrivate($encryptAll, 'encryptAllUsersFiles');
  252. }
  253. public function testEncryptUsersFiles() {
  254. /** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */
  255. $encryptAll = $this->getMockBuilder(EncryptAll::class)
  256. ->setConstructorArgs(
  257. [
  258. $this->setupUser,
  259. $this->userManager,
  260. $this->view,
  261. $this->keyManager,
  262. $this->util,
  263. $this->config,
  264. $this->mailer,
  265. $this->l,
  266. $this->l10nFactory,
  267. $this->questionHelper,
  268. $this->secureRandom
  269. ]
  270. )
  271. ->setMethods(['encryptFile', 'setupUserFS'])
  272. ->getMock();
  273. $this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false);
  274. $this->view->expects($this->exactly(2))->method('getDirectoryContent')
  275. ->withConsecutive(
  276. ['/user1/files'],
  277. ['/user1/files/foo'],
  278. )->willReturnOnConsecutiveCalls(
  279. [
  280. ['name' => 'foo', 'type' => 'dir'],
  281. ['name' => 'bar', 'type' => 'file'],
  282. ],
  283. [
  284. ['name' => 'subfile', 'type' => 'file']
  285. ]
  286. );
  287. $this->view->expects($this->any())->method('is_dir')
  288. ->willReturnCallback(
  289. function ($path) {
  290. if ($path === '/user1/files/foo') {
  291. return true;
  292. }
  293. return false;
  294. }
  295. );
  296. $encryptAll->expects($this->exactly(2))->method('encryptFile')
  297. ->withConsecutive(
  298. ['/user1/files/bar'],
  299. ['/user1/files/foo/subfile'],
  300. );
  301. $this->outputInterface->expects($this->any())
  302. ->method('getFormatter')
  303. ->willReturn($this->createMock(OutputFormatterInterface::class));
  304. $progressBar = new ProgressBar($this->outputInterface);
  305. $this->invokePrivate($encryptAll, 'encryptUsersFiles', ['user1', $progressBar, '']);
  306. }
  307. public function testGenerateOneTimePassword() {
  308. $password = $this->invokePrivate($this->encryptAll, 'generateOneTimePassword', ['user1']);
  309. $this->assertTrue(is_string($password));
  310. $this->assertSame(8, strlen($password));
  311. $userPasswords = $this->invokePrivate($this->encryptAll, 'userPasswords');
  312. $this->assertSame(1, count($userPasswords));
  313. $this->assertSame($password, $userPasswords['user1']);
  314. }
  315. /**
  316. * @dataProvider dataTestEncryptFile
  317. * @param $isEncrypted
  318. */
  319. public function testEncryptFile($isEncrypted) {
  320. $fileInfo = $this->createMock(FileInfo::class);
  321. $fileInfo->expects($this->any())->method('isEncrypted')
  322. ->willReturn($isEncrypted);
  323. $this->view->expects($this->any())->method('getFileInfo')
  324. ->willReturn($fileInfo);
  325. if ($isEncrypted) {
  326. $this->view->expects($this->never())->method('copy');
  327. $this->view->expects($this->never())->method('rename');
  328. } else {
  329. $this->view->expects($this->once())->method('copy');
  330. $this->view->expects($this->once())->method('rename');
  331. }
  332. $this->assertTrue(
  333. $this->invokePrivate($this->encryptAll, 'encryptFile', ['foo.txt'])
  334. );
  335. }
  336. public function dataTestEncryptFile() {
  337. return [
  338. [true],
  339. [false],
  340. ];
  341. }
  342. }