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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Kenneth Newwood <kenneth@newwood.name>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\Encryption\Tests\Crypto;
  27. use OC\Files\View;
  28. use OCA\Encryption\Crypto\EncryptAll;
  29. use OCA\Encryption\KeyManager;
  30. use OCA\Encryption\Users\Setup;
  31. use OCA\Encryption\Util;
  32. use OCP\Files\FileInfo;
  33. use OCP\IConfig;
  34. use OCP\IL10N;
  35. use OCP\IUserManager;
  36. use OCP\Mail\IMailer;
  37. use OCP\Security\ISecureRandom;
  38. use OCP\UserInterface;
  39. use Symfony\Component\Console\Formatter\OutputFormatterInterface;
  40. use Symfony\Component\Console\Helper\ProgressBar;
  41. use Symfony\Component\Console\Helper\QuestionHelper;
  42. use Symfony\Component\Console\Input\InputInterface;
  43. use Symfony\Component\Console\Output\OutputInterface;
  44. use Test\TestCase;
  45. class EncryptAllTest extends TestCase {
  46. /** @var \PHPUnit_Framework_MockObject_MockObject | \OCA\Encryption\KeyManager */
  47. protected $keyManager;
  48. /** @var \PHPUnit_Framework_MockObject_MockObject | \OCA\Encryption\Util */
  49. protected $util;
  50. /** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\IUserManager */
  51. protected $userManager;
  52. /** @var \PHPUnit_Framework_MockObject_MockObject | \OCA\Encryption\Users\Setup */
  53. protected $setupUser;
  54. /** @var \PHPUnit_Framework_MockObject_MockObject | \OC\Files\View */
  55. protected $view;
  56. /** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\IConfig */
  57. protected $config;
  58. /** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\Mail\IMailer */
  59. protected $mailer;
  60. /** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\IL10N */
  61. protected $l;
  62. /** @var \PHPUnit_Framework_MockObject_MockObject | \Symfony\Component\Console\Helper\QuestionHelper */
  63. protected $questionHelper;
  64. /** @var \PHPUnit_Framework_MockObject_MockObject | \Symfony\Component\Console\Input\InputInterface */
  65. protected $inputInterface;
  66. /** @var \PHPUnit_Framework_MockObject_MockObject | \Symfony\Component\Console\Output\OutputInterface */
  67. protected $outputInterface;
  68. /** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\UserInterface */
  69. protected $userInterface;
  70. /** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\Security\ISecureRandom */
  71. protected $secureRandom;
  72. /** @var EncryptAll */
  73. protected $encryptAll;
  74. function setUp() {
  75. parent::setUp();
  76. $this->setupUser = $this->getMockBuilder(Setup::class)
  77. ->disableOriginalConstructor()->getMock();
  78. $this->keyManager = $this->getMockBuilder(KeyManager::class)
  79. ->disableOriginalConstructor()->getMock();
  80. $this->util = $this->getMockBuilder(Util::class)
  81. ->disableOriginalConstructor()->getMock();
  82. $this->userManager = $this->getMockBuilder(IUserManager::class)
  83. ->disableOriginalConstructor()->getMock();
  84. $this->view = $this->getMockBuilder(View::class)
  85. ->disableOriginalConstructor()->getMock();
  86. $this->config = $this->getMockBuilder(IConfig::class)
  87. ->disableOriginalConstructor()->getMock();
  88. $this->mailer = $this->getMockBuilder(IMailer::class)
  89. ->disableOriginalConstructor()->getMock();
  90. $this->l = $this->getMockBuilder(IL10N::class)
  91. ->disableOriginalConstructor()->getMock();
  92. $this->questionHelper = $this->getMockBuilder(QuestionHelper::class)
  93. ->disableOriginalConstructor()->getMock();
  94. $this->inputInterface = $this->getMockBuilder(InputInterface::class)
  95. ->disableOriginalConstructor()->getMock();
  96. $this->outputInterface = $this->getMockBuilder(OutputInterface::class)
  97. ->disableOriginalConstructor()->getMock();
  98. $this->userInterface = $this->getMockBuilder(UserInterface::class)
  99. ->disableOriginalConstructor()->getMock();
  100. $this->outputInterface->expects($this->any())->method('getFormatter')
  101. ->willReturn($this->createMock(OutputFormatterInterface::class));
  102. $this->userManager->expects($this->any())->method('getBackends')->willReturn([$this->userInterface]);
  103. $this->userInterface->expects($this->any())->method('getUsers')->willReturn(['user1', 'user2']);
  104. $this->secureRandom = $this->getMockBuilder(ISecureRandom::class)->disableOriginalConstructor()->getMock();
  105. $this->secureRandom->expects($this->any())->method('generate')->willReturn('12345678');
  106. $this->encryptAll = new EncryptAll(
  107. $this->setupUser,
  108. $this->userManager,
  109. $this->view,
  110. $this->keyManager,
  111. $this->util,
  112. $this->config,
  113. $this->mailer,
  114. $this->l,
  115. $this->questionHelper,
  116. $this->secureRandom
  117. );
  118. }
  119. public function testEncryptAll() {
  120. /** @var EncryptAll | \PHPUnit_Framework_MockObject_MockObject $encryptAll */
  121. $encryptAll = $this->getMockBuilder(EncryptAll::class)
  122. ->setConstructorArgs(
  123. [
  124. $this->setupUser,
  125. $this->userManager,
  126. $this->view,
  127. $this->keyManager,
  128. $this->util,
  129. $this->config,
  130. $this->mailer,
  131. $this->l,
  132. $this->questionHelper,
  133. $this->secureRandom
  134. ]
  135. )
  136. ->setMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords'])
  137. ->getMock();
  138. $this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false);
  139. $encryptAll->expects($this->at(0))->method('createKeyPairs')->with();
  140. $encryptAll->expects($this->at(1))->method('outputPasswords')->with();
  141. $encryptAll->expects($this->at(2))->method('encryptAllUsersFiles')->with();
  142. $encryptAll->encryptAll($this->inputInterface, $this->outputInterface);
  143. }
  144. public function testEncryptAllWithMasterKey() {
  145. /** @var EncryptAll | \PHPUnit_Framework_MockObject_MockObject $encryptAll */
  146. $encryptAll = $this->getMockBuilder(EncryptAll::class)
  147. ->setConstructorArgs(
  148. [
  149. $this->setupUser,
  150. $this->userManager,
  151. $this->view,
  152. $this->keyManager,
  153. $this->util,
  154. $this->config,
  155. $this->mailer,
  156. $this->l,
  157. $this->questionHelper,
  158. $this->secureRandom
  159. ]
  160. )
  161. ->setMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords'])
  162. ->getMock();
  163. $this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(true);
  164. $encryptAll->expects($this->never())->method('createKeyPairs');
  165. $this->keyManager->expects($this->once())->method('validateMasterKey');
  166. $encryptAll->expects($this->at(0))->method('encryptAllUsersFiles')->with();
  167. $encryptAll->expects($this->never())->method('outputPasswords');
  168. $encryptAll->encryptAll($this->inputInterface, $this->outputInterface);
  169. }
  170. public function testCreateKeyPairs() {
  171. /** @var EncryptAll | \PHPUnit_Framework_MockObject_MockObject $encryptAll */
  172. $encryptAll = $this->getMockBuilder(EncryptAll::class)
  173. ->setConstructorArgs(
  174. [
  175. $this->setupUser,
  176. $this->userManager,
  177. $this->view,
  178. $this->keyManager,
  179. $this->util,
  180. $this->config,
  181. $this->mailer,
  182. $this->l,
  183. $this->questionHelper,
  184. $this->secureRandom
  185. ]
  186. )
  187. ->setMethods(['setupUserFS', 'generateOneTimePassword'])
  188. ->getMock();
  189. // set protected property $output
  190. $this->invokePrivate($encryptAll, 'output', [$this->outputInterface]);
  191. $this->keyManager->expects($this->exactly(2))->method('userHasKeys')
  192. ->willReturnCallback(
  193. function ($user) {
  194. if ($user === 'user1') {
  195. return false;
  196. }
  197. return true;
  198. }
  199. );
  200. $encryptAll->expects($this->once())->method('setupUserFS')->with('user1');
  201. $encryptAll->expects($this->once())->method('generateOneTimePassword')->with('user1')->willReturn('password');
  202. $this->setupUser->expects($this->once())->method('setupUser')->with('user1', 'password');
  203. $this->invokePrivate($encryptAll, 'createKeyPairs');
  204. $userPasswords = $this->invokePrivate($encryptAll, 'userPasswords');
  205. // we only expect the skipped user, because generateOneTimePassword which
  206. // would set the user with the new password was mocked.
  207. // This method will be tested separately
  208. $this->assertSame(1, count($userPasswords));
  209. $this->assertSame('', $userPasswords['user2']);
  210. }
  211. public function testEncryptAllUsersFiles() {
  212. /** @var EncryptAll | \PHPUnit_Framework_MockObject_MockObject $encryptAll */
  213. $encryptAll = $this->getMockBuilder(EncryptAll::class)
  214. ->setConstructorArgs(
  215. [
  216. $this->setupUser,
  217. $this->userManager,
  218. $this->view,
  219. $this->keyManager,
  220. $this->util,
  221. $this->config,
  222. $this->mailer,
  223. $this->l,
  224. $this->questionHelper,
  225. $this->secureRandom
  226. ]
  227. )
  228. ->setMethods(['encryptUsersFiles'])
  229. ->getMock();
  230. $this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false);
  231. // set protected property $output
  232. $this->invokePrivate($encryptAll, 'output', [$this->outputInterface]);
  233. $this->invokePrivate($encryptAll, 'userPasswords', [['user1' => 'pwd1', 'user2' => 'pwd2']]);
  234. $encryptAll->expects($this->at(0))->method('encryptUsersFiles')->with('user1');
  235. $encryptAll->expects($this->at(1))->method('encryptUsersFiles')->with('user2');
  236. $this->invokePrivate($encryptAll, 'encryptAllUsersFiles');
  237. }
  238. public function testEncryptUsersFiles() {
  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->questionHelper,
  252. $this->secureRandom
  253. ]
  254. )
  255. ->setMethods(['encryptFile', 'setupUserFS'])
  256. ->getMock();
  257. $this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false);
  258. $this->view->expects($this->at(0))->method('getDirectoryContent')
  259. ->with('/user1/files')->willReturn(
  260. [
  261. ['name' => 'foo', 'type'=>'dir'],
  262. ['name' => 'bar', 'type'=>'file'],
  263. ]
  264. );
  265. $this->view->expects($this->at(3))->method('getDirectoryContent')
  266. ->with('/user1/files/foo')->willReturn(
  267. [
  268. ['name' => 'subfile', 'type'=>'file']
  269. ]
  270. );
  271. $this->view->expects($this->any())->method('is_dir')
  272. ->willReturnCallback(
  273. function($path) {
  274. if ($path === '/user1/files/foo') {
  275. return true;
  276. }
  277. return false;
  278. }
  279. );
  280. $encryptAll->expects($this->at(1))->method('encryptFile')->with('/user1/files/bar');
  281. $encryptAll->expects($this->at(2))->method('encryptFile')->with('/user1/files/foo/subfile');
  282. $this->outputInterface->expects($this->any())
  283. ->method('getFormatter')
  284. ->willReturn($this->createMock(OutputFormatterInterface::class));
  285. $progressBar = new ProgressBar($this->outputInterface);
  286. $this->invokePrivate($encryptAll, 'encryptUsersFiles', ['user1', $progressBar, '']);
  287. }
  288. public function testGenerateOneTimePassword() {
  289. $password = $this->invokePrivate($this->encryptAll, 'generateOneTimePassword', ['user1']);
  290. $this->assertTrue(is_string($password));
  291. $this->assertSame(8, strlen($password));
  292. $userPasswords = $this->invokePrivate($this->encryptAll, 'userPasswords');
  293. $this->assertSame(1, count($userPasswords));
  294. $this->assertSame($password, $userPasswords['user1']);
  295. }
  296. /**
  297. * @dataProvider dataTestEncryptFile
  298. * @param $isEncrypted
  299. */
  300. public function testEncryptFile($isEncrypted) {
  301. $fileInfo = $this->createMock(FileInfo::class);
  302. $fileInfo->expects($this->any())->method('isEncrypted')
  303. ->willReturn($isEncrypted);
  304. $this->view->expects($this->any())->method('getFileInfo')
  305. ->willReturn($fileInfo);
  306. if($isEncrypted) {
  307. $this->view->expects($this->never())->method('copy');
  308. $this->view->expects($this->never())->method('rename');
  309. } else {
  310. $this->view->expects($this->once())->method('copy');
  311. $this->view->expects($this->once())->method('rename');
  312. }
  313. $this->assertTrue(
  314. $this->invokePrivate($this->encryptAll, 'encryptFile', ['foo.txt'])
  315. );
  316. }
  317. public function dataTestEncryptFile() {
  318. return [
  319. [true],
  320. [false],
  321. ];
  322. }
  323. }