UtilTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 Clark Tomlinson <fallen013@gmail.com>
  9. * @author Joas Schilling <coding@schilljs.com>
  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;
  29. use OC\Files\View;
  30. use OCA\Encryption\Crypto\Crypt;
  31. use OCA\Encryption\Util;
  32. use OCP\Files\Mount\IMountPoint;
  33. use OCP\Files\Storage;
  34. use OCP\IConfig;
  35. use OCP\IUser;
  36. use OCP\IUserManager;
  37. use OCP\IUserSession;
  38. use PHPUnit\Framework\MockObject\MockObject;
  39. use Test\TestCase;
  40. class UtilTest extends TestCase {
  41. private static $tempStorage = [];
  42. /** @var \OCP\IConfig|\PHPUnit\Framework\MockObject\MockObject */
  43. private $configMock;
  44. /** @var \OC\Files\View|\PHPUnit\Framework\MockObject\MockObject */
  45. private $filesMock;
  46. /** @var \OCP\IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  47. private $userManagerMock;
  48. /** @var \OCP\Files\Mount\IMountPoint|\PHPUnit\Framework\MockObject\MockObject */
  49. private $mountMock;
  50. /** @var Util */
  51. private $instance;
  52. public function testSetRecoveryForUser() {
  53. $this->instance->setRecoveryForUser('1');
  54. $this->assertArrayHasKey('recoveryEnabled', self::$tempStorage);
  55. }
  56. public function testIsRecoveryEnabledForUser() {
  57. $this->assertTrue($this->instance->isRecoveryEnabledForUser('admin'));
  58. // Assert recovery will return default value if not set
  59. unset(self::$tempStorage['recoveryEnabled']);
  60. $this->assertEquals(0, $this->instance->isRecoveryEnabledForUser('admin'));
  61. }
  62. public function testUserHasFiles() {
  63. $this->filesMock->expects($this->once())
  64. ->method('file_exists')
  65. ->willReturn(true);
  66. $this->assertTrue($this->instance->userHasFiles('admin'));
  67. }
  68. protected function setUp(): void {
  69. parent::setUp();
  70. $this->mountMock = $this->createMock(IMountPoint::class);
  71. $this->filesMock = $this->createMock(View::class);
  72. $this->userManagerMock = $this->createMock(IUserManager::class);
  73. /** @var \OCA\Encryption\Crypto\Crypt $cryptMock */
  74. $cryptMock = $this->getMockBuilder(Crypt::class)
  75. ->disableOriginalConstructor()
  76. ->getMock();
  77. $user = $this->createMock(IUser::class);
  78. $user->expects($this->any())
  79. ->method('getUID')
  80. ->willReturn('admin');
  81. /** @var IUserSession|MockObject $userSessionMock */
  82. $userSessionMock = $this->createMock(IUserSession::class);
  83. $userSessionMock->expects($this->any())
  84. ->method('getUser')
  85. ->willReturn($user);
  86. $userSessionMock->expects($this->any())
  87. ->method('isLoggedIn')
  88. ->willReturn(true);
  89. $this->configMock = $this->createMock(IConfig::class);
  90. $this->configMock->expects($this->any())
  91. ->method('getUserValue')
  92. ->willReturnCallback([$this, 'getValueTester']);
  93. $this->configMock->expects($this->any())
  94. ->method('setUserValue')
  95. ->willReturnCallback([$this, 'setValueTester']);
  96. $this->instance = new Util($this->filesMock, $cryptMock, $userSessionMock, $this->configMock, $this->userManagerMock);
  97. }
  98. /**
  99. * @param $userId
  100. * @param $app
  101. * @param $key
  102. * @param $value
  103. */
  104. public function setValueTester($userId, $app, $key, $value) {
  105. self::$tempStorage[$key] = $value;
  106. }
  107. /**
  108. * @param $userId
  109. * @param $app
  110. * @param $key
  111. * @param $default
  112. * @return mixed
  113. */
  114. public function getValueTester($userId, $app, $key, $default) {
  115. if (!empty(self::$tempStorage[$key])) {
  116. return self::$tempStorage[$key];
  117. }
  118. return $default ?: null;
  119. }
  120. /**
  121. * @dataProvider dataTestIsMasterKeyEnabled
  122. *
  123. * @param string $value
  124. * @param bool $expect
  125. */
  126. public function testIsMasterKeyEnabled($value, $expect) {
  127. $this->configMock->expects($this->once())->method('getAppValue')
  128. ->with('encryption', 'useMasterKey', '1')->willReturn($value);
  129. $this->assertSame($expect,
  130. $this->instance->isMasterKeyEnabled()
  131. );
  132. }
  133. public function dataTestIsMasterKeyEnabled() {
  134. return [
  135. ['0', false],
  136. ['1', true]
  137. ];
  138. }
  139. /**
  140. * @dataProvider dataTestShouldEncryptHomeStorage
  141. * @param string $returnValue return value from getAppValue()
  142. * @param bool $expected
  143. */
  144. public function testShouldEncryptHomeStorage($returnValue, $expected) {
  145. $this->configMock->expects($this->once())->method('getAppValue')
  146. ->with('encryption', 'encryptHomeStorage', '1')
  147. ->willReturn($returnValue);
  148. $this->assertSame($expected,
  149. $this->instance->shouldEncryptHomeStorage());
  150. }
  151. public function dataTestShouldEncryptHomeStorage() {
  152. return [
  153. ['1', true],
  154. ['0', false]
  155. ];
  156. }
  157. /**
  158. * @dataProvider dataTestSetEncryptHomeStorage
  159. * @param $value
  160. * @param $expected
  161. */
  162. public function testSetEncryptHomeStorage($value, $expected) {
  163. $this->configMock->expects($this->once())->method('setAppValue')
  164. ->with('encryption', 'encryptHomeStorage', $expected);
  165. $this->instance->setEncryptHomeStorage($value);
  166. }
  167. public function dataTestSetEncryptHomeStorage() {
  168. return [
  169. [true, '1'],
  170. [false, '0']
  171. ];
  172. }
  173. public function testGetStorage() {
  174. $return = $this->getMockBuilder(Storage::class)
  175. ->disableOriginalConstructor()
  176. ->getMock();
  177. $path = '/foo/bar.txt';
  178. $this->filesMock->expects($this->once())->method('getMount')->with($path)
  179. ->willReturn($this->mountMock);
  180. $this->mountMock->expects($this->once())->method('getStorage')->willReturn($return);
  181. $this->assertEquals($return, $this->instance->getStorage($path));
  182. }
  183. }