1
0

UtilTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 Clark Tomlinson <fallen013@gmail.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\Encryption\Tests;
  28. use OC\Files\View;
  29. use OCA\Encryption\Crypto\Crypt;
  30. use OCA\Encryption\Util;
  31. use OCP\Files\Mount\IMountPoint;
  32. use OCP\Files\Storage;
  33. use OCP\IConfig;
  34. use OCP\ILogger;
  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. ->will($this->returnValue(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. /** @var \OCP\ILogger $loggerMock */
  78. $loggerMock = $this->createMock(ILogger::class);
  79. $user = $this->createMock(IUser::class);
  80. $user->expects($this->any())
  81. ->method('getUID')
  82. ->willReturn('admin');
  83. /** @var IUserSession|MockObject $userSessionMock */
  84. $userSessionMock = $this->createMock(IUserSession::class);
  85. $userSessionMock->expects($this->any())
  86. ->method('getUser')
  87. ->willReturn($user);
  88. $userSessionMock->expects($this->any())
  89. ->method('isLoggedIn')
  90. ->willReturn(true);
  91. $this->configMock = $this->createMock(IConfig::class);
  92. $this->configMock->expects($this->any())
  93. ->method('getUserValue')
  94. ->will($this->returnCallback([$this, 'getValueTester']));
  95. $this->configMock->expects($this->any())
  96. ->method('setUserValue')
  97. ->will($this->returnCallback([$this, 'setValueTester']));
  98. $this->instance = new Util($this->filesMock, $cryptMock, $loggerMock, $userSessionMock, $this->configMock, $this->userManagerMock);
  99. }
  100. /**
  101. * @param $userId
  102. * @param $app
  103. * @param $key
  104. * @param $value
  105. */
  106. public function setValueTester($userId, $app, $key, $value) {
  107. self::$tempStorage[$key] = $value;
  108. }
  109. /**
  110. * @param $userId
  111. * @param $app
  112. * @param $key
  113. * @param $default
  114. * @return mixed
  115. */
  116. public function getValueTester($userId, $app, $key, $default) {
  117. if (!empty(self::$tempStorage[$key])) {
  118. return self::$tempStorage[$key];
  119. }
  120. return $default ?: null;
  121. }
  122. /**
  123. * @dataProvider dataTestIsMasterKeyEnabled
  124. *
  125. * @param string $value
  126. * @param bool $expect
  127. */
  128. public function testIsMasterKeyEnabled($value, $expect) {
  129. $this->configMock->expects($this->once())->method('getAppValue')
  130. ->with('encryption', 'useMasterKey', '1')->willReturn($value);
  131. $this->assertSame($expect,
  132. $this->instance->isMasterKeyEnabled()
  133. );
  134. }
  135. public function dataTestIsMasterKeyEnabled() {
  136. return [
  137. ['0', false],
  138. ['1', true]
  139. ];
  140. }
  141. /**
  142. * @dataProvider dataTestShouldEncryptHomeStorage
  143. * @param string $returnValue return value from getAppValue()
  144. * @param bool $expected
  145. */
  146. public function testShouldEncryptHomeStorage($returnValue, $expected) {
  147. $this->configMock->expects($this->once())->method('getAppValue')
  148. ->with('encryption', 'encryptHomeStorage', '1')
  149. ->willReturn($returnValue);
  150. $this->assertSame($expected,
  151. $this->instance->shouldEncryptHomeStorage());
  152. }
  153. public function dataTestShouldEncryptHomeStorage() {
  154. return [
  155. ['1', true],
  156. ['0', false]
  157. ];
  158. }
  159. /**
  160. * @dataProvider dataTestSetEncryptHomeStorage
  161. * @param $value
  162. * @param $expected
  163. */
  164. public function testSetEncryptHomeStorage($value, $expected) {
  165. $this->configMock->expects($this->once())->method('setAppValue')
  166. ->with('encryption', 'encryptHomeStorage', $expected);
  167. $this->instance->setEncryptHomeStorage($value);
  168. }
  169. public function dataTestSetEncryptHomeStorage() {
  170. return [
  171. [true, '1'],
  172. [false, '0']
  173. ];
  174. }
  175. public function testGetStorage() {
  176. $return = $this->getMockBuilder(Storage::class)
  177. ->disableOriginalConstructor()
  178. ->getMock();
  179. $path = '/foo/bar.txt';
  180. $this->filesMock->expects($this->once())->method('getMount')->with($path)
  181. ->willReturn($this->mountMock);
  182. $this->mountMock->expects($this->once())->method('getStorage')->willReturn($return);
  183. $this->assertEquals($return, $this->instance->getStorage($path));
  184. }
  185. }