You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EncryptAllTest.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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->at(0))->method('createKeyPairs')->with();
  145. $encryptAll->expects($this->at(1))->method('outputPasswords')->with();
  146. $encryptAll->expects($this->at(2))->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->at(0))->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->at(0))->method('encryptUsersFiles')->with('user1');
  240. $encryptAll->expects($this->at(1))->method('encryptUsersFiles')->with('user2');
  241. $this->invokePrivate($encryptAll, 'encryptAllUsersFiles');
  242. }
  243. public function testEncryptUsersFiles() {
  244. /** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */
  245. $encryptAll = $this->getMockBuilder(EncryptAll::class)
  246. ->setConstructorArgs(
  247. [
  248. $this->setupUser,
  249. $this->userManager,
  250. $this->view,
  251. $this->keyManager,
  252. $this->util,
  253. $this->config,
  254. $this->mailer,
  255. $this->l,
  256. $this->questionHelper,
  257. $this->secureRandom
  258. ]
  259. )
  260. ->setMethods(['encryptFile', 'setupUserFS'])
  261. ->getMock();
  262. $this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false);
  263. $this->view->expects($this->at(0))->method('getDirectoryContent')
  264. ->with('/user1/files')->willReturn(
  265. [
  266. ['name' => 'foo', 'type' => 'dir'],
  267. ['name' => 'bar', 'type' => 'file'],
  268. ]
  269. );
  270. $this->view->expects($this->at(3))->method('getDirectoryContent')
  271. ->with('/user1/files/foo')->willReturn(
  272. [
  273. ['name' => 'subfile', 'type' => 'file']
  274. ]
  275. );
  276. $this->view->expects($this->any())->method('is_dir')
  277. ->willReturnCallback(
  278. function ($path) {
  279. if ($path === '/user1/files/foo') {
  280. return true;
  281. }
  282. return false;
  283. }
  284. );
  285. $encryptAll->expects($this->at(1))->method('encryptFile')->with('/user1/files/bar');
  286. $encryptAll->expects($this->at(2))->method('encryptFile')->with('/user1/files/foo/subfile');
  287. $this->outputInterface->expects($this->any())
  288. ->method('getFormatter')
  289. ->willReturn($this->createMock(OutputFormatterInterface::class));
  290. $progressBar = new ProgressBar($this->outputInterface);
  291. $this->invokePrivate($encryptAll, 'encryptUsersFiles', ['user1', $progressBar, '']);
  292. }
  293. public function testGenerateOneTimePassword() {
  294. $password = $this->invokePrivate($this->encryptAll, 'generateOneTimePassword', ['user1']);
  295. $this->assertTrue(is_string($password));
  296. $this->assertSame(8, strlen($password));
  297. $userPasswords = $this->invokePrivate($this->encryptAll, 'userPasswords');
  298. $this->assertSame(1, count($userPasswords));
  299. $this->assertSame($password, $userPasswords['user1']);
  300. }
  301. /**
  302. * @dataProvider dataTestEncryptFile
  303. * @param $isEncrypted
  304. */
  305. public function testEncryptFile($isEncrypted) {
  306. $fileInfo = $this->createMock(FileInfo::class);
  307. $fileInfo->expects($this->any())->method('isEncrypted')
  308. ->willReturn($isEncrypted);
  309. $this->view->expects($this->any())->method('getFileInfo')
  310. ->willReturn($fileInfo);
  311. if ($isEncrypted) {
  312. $this->view->expects($this->never())->method('copy');
  313. $this->view->expects($this->never())->method('rename');
  314. } else {
  315. $this->view->expects($this->once())->method('copy');
  316. $this->view->expects($this->once())->method('rename');
  317. }
  318. $this->assertTrue(
  319. $this->invokePrivate($this->encryptAll, 'encryptFile', ['foo.txt'])
  320. );
  321. }
  322. public function dataTestEncryptFile() {
  323. return [
  324. [true],
  325. [false],
  326. ];
  327. }
  328. }