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

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