UtilTest.php 6.1 KB

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