1
0

EncryptAllTest.php 13 KB

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