aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/AppFramework/Middleware/Security/Mock
ModeNameSize
-rw-r--r--CORSMiddlewareController.php3236logstatsplain
-rw-r--r--NormalController.php1035logstatsplain
-rw-r--r--OCSController.php1035logstatsplain
-rw-r--r--PasswordConfirmationMiddlewareController.php1386logstatsplain
-rw-r--r--SecurityMiddlewareController.php3869logstatsplain
>138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
<?php
/**
 * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace Test;

use OC\Files\Storage\Temporary;
use OCP\Files\Mount\IMountManager;
use OCP\IConfig;
use Test\Traits\UserTrait;

/**
 * Test the storage functions of OC_Helper
 *
 * @group DB
 */
class HelperStorageTest extends \Test\TestCase {
	use UserTrait;

	/** @var string */
	private $user;
	/** @var \OC\Files\Storage\Storage */
	private $storageMock;
	/** @var \OC\Files\Storage\Storage */
	private $storage;
	private bool $savedQuotaIncludeExternalStorage;

	protected function setUp(): void {
		parent::setUp();

		$this->user = $this->getUniqueID('user_');
		$this->createUser($this->user, $this->user);
		$this->savedQuotaIncludeExternalStorage = $this->getIncludeExternalStorage();

		\OC\Files\Filesystem::tearDown();
		\OC_User::setUserId($this->user);
		\OC\Files\Filesystem::init($this->user, '/' . $this->user . '/files');

		/** @var IMountManager $manager */
		$manager = \OC::$server->get(IMountManager::class);
		$manager->removeMount('/' . $this->user);

		$this->storageMock = null;
	}

	protected function tearDown(): void {
		$this->setIncludeExternalStorage($this->savedQuotaIncludeExternalStorage);
		$this->user = null;

		if ($this->storageMock) {
			$this->storageMock->getCache()->clear();
			$this->storageMock = null;
		}
		\OC\Files\Filesystem::tearDown();

		\OC_User::setUserId('');
		\OC::$server->getConfig()->deleteAllUserValues($this->user);

		parent::tearDown();
	}

	/**
	 * Returns a storage mock that returns the given value as
	 * free space
	 *
	 * @param int $freeSpace free space value
	 * @return \OC\Files\Storage\Storage
	 */
	private function getStorageMock($freeSpace = 12) {
		$this->storageMock = $this->getMockBuilder(Temporary::class)
			->onlyMethods(['free_space'])
			->setConstructorArgs([[]])
			->getMock();

		$this->storageMock->expects($this->once())
			->method('free_space')
			->willReturn($freeSpace);
		return $this->storageMock;
	}

	/**
	 * Test getting the storage info
	 */
	public function testGetStorageInfo(): void {
		$homeStorage = $this->getStorageMock(12);
		\OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files');
		$homeStorage->file_put_contents('test.txt', '01234');

		$storageInfo = \OC_Helper::getStorageInfo('');
		$this->assertEquals(12, $storageInfo['free']);
		$this->assertEquals(5, $storageInfo['used']);
		$this->assertEquals(17, $storageInfo['total']);
	}
	private function getIncludeExternalStorage(): bool {
		$class = new \ReflectionClass(\OC_Helper::class);
		$prop = $class->getProperty('quotaIncludeExternalStorage');
		$prop->setAccessible(true);
		return $prop->getValue(null) ?? false;
	}

	private function setIncludeExternalStorage(bool $include) {
		$class = new \ReflectionClass(\OC_Helper::class);
		$prop = $class->getProperty('quotaIncludeExternalStorage');
		$prop->setAccessible(true);
		$prop->setValue(null, $include);
	}

	/**
	 * Test getting the storage info, ignoring extra mount points
	 */
	public function testGetStorageInfoExcludingExtStorage(): void {
		$homeStorage = $this->getStorageMock(12);
		\OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files');
		$homeStorage->file_put_contents('test.txt', '01234');

		$extStorage = new \OC\Files\Storage\Temporary([]);
		$extStorage->file_put_contents('extfile.txt', 'abcdefghijklmnopq');
		$extStorage->getScanner()->scan(''); // update root size

		$this->setIncludeExternalStorage(false);

		\OC\Files\Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext');

		$storageInfo = \OC_Helper::getStorageInfo('');
		$this->assertEquals(12, $storageInfo['free']);
		$this->assertEquals(5, $storageInfo['used']);
		$this->assertEquals(17, $storageInfo['total']);
	}

	/**
	 * Test getting the storage info, including extra mount points
	 */
	public function testGetStorageInfoIncludingExtStorage(): void {
		$homeStorage = new \OC\Files\Storage\Temporary([]);
		\OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files');
		$homeStorage->file_put_contents('test.txt', '01234');

		$extStorage = new \OC\Files\Storage\Temporary([]);
		$extStorage->file_put_contents('extfile.txt', 'abcdefghijklmnopq');
		$extStorage->getScanner()->scan(''); // update root size

		\OC\Files\Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext');

		$this->setIncludeExternalStorage(true);

		$config = \OC::$server->get(IConfig::class);
		$config->setUserValue($this->user, 'files', 'quota', '25');

		$storageInfo = \OC_Helper::getStorageInfo('');
		$this->assertEquals(3, $storageInfo['free']);
		$this->assertEquals(22, $storageInfo['used']);
		$this->assertEquals(25, $storageInfo['total']);

		$config->setUserValue($this->user, 'files', 'quota', 'default');
	}

	/**
	 * Test getting the storage info excluding extra mount points
	 * when user has no quota set, even when quota ext storage option
	 * was set
	 */
	public function testGetStorageInfoIncludingExtStorageWithNoUserQuota(): void {
		$homeStorage = $this->getStorageMock(12);
		\OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files');
		$homeStorage->file_put_contents('test.txt', '01234');

		$extStorage = new \OC\Files\Storage\Temporary([]);
		$extStorage->file_put_contents('extfile.txt', 'abcdefghijklmnopq');
		$extStorage->getScanner()->scan(''); // update root size

		\OC\Files\Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext');

		$config = \OC::$server->getConfig();
		$this->setIncludeExternalStorage(true);

		$storageInfo = \OC_Helper::getStorageInfo('');
		$this->assertEquals(12, $storageInfo['free'], '12 bytes free in home storage');
		$this->assertEquals(22, $storageInfo['used'], '5 bytes of home storage and 17 bytes of the temporary storage are used');
		$this->assertEquals(34, $storageInfo['total'], '5 bytes used and 12 bytes free in home storage as well as 17 bytes used in temporary storage');
	}


	/**
	 * Test getting the storage info with quota enabled
	 */
	public function testGetStorageInfoWithQuota(): void {
		$homeStorage = $this->getStorageMock(12);
		$homeStorage->file_put_contents('test.txt', '01234');
		$homeStorage = new \OC\Files\Storage\Wrapper\Quota(
			[
				'storage' => $homeStorage,
				'quota' => 7
			]
		);
		\OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files');

		$storageInfo = \OC_Helper::getStorageInfo('');
		$this->assertEquals(2, $storageInfo['free']);
		$this->assertEquals(5, $storageInfo['used']);
		$this->assertEquals(7, $storageInfo['total']);
	}

	/**
	 * Test getting the storage info when data exceeds quota
	 */
	public function testGetStorageInfoWhenSizeExceedsQuota(): void {
		$homeStorage = $this->getStorageMock(12);
		$homeStorage->file_put_contents('test.txt', '0123456789');
		$homeStorage = new \OC\Files\Storage\Wrapper\Quota(
			[
				'storage' => $homeStorage,
				'quota' => 7
			]
		);
		\OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files');

		$storageInfo = \OC_Helper::getStorageInfo('');
		$this->assertEquals(0, $storageInfo['free']);
		$this->assertEquals(10, $storageInfo['used']);
		// total = quota
		$this->assertEquals(7, $storageInfo['total']);
	}

	/**
	 * Test getting the storage info when the remaining
	 * free storage space is less than the quota
	 */
	public function testGetStorageInfoWhenFreeSpaceLessThanQuota(): void {
		$homeStorage = $this->getStorageMock(12);
		$homeStorage->file_put_contents('test.txt', '01234');
		$homeStorage = new \OC\Files\Storage\Wrapper\Quota(
			[
				'storage' => $homeStorage,
				'quota' => 18
			]
		);
		\OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files');

		$storageInfo = \OC_Helper::getStorageInfo('');
		$this->assertEquals(12, $storageInfo['free']);
		$this->assertEquals(5, $storageInfo['used']);
		// total = free + used (because quota > total)
		$this->assertEquals(17, $storageInfo['total']);
	}
}