UtilTest.php 6.2 KB

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