PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/apps/encryption/tests/lib/crypto/encryptalltest.php

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