1
0

EncryptAllTest.php 12 KB

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