‪TYPO3CMS  ‪main
SimpleLockStrategyTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\SkippedTestError;
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26 
27 class ‪SimpleLockStrategyTest extends UnitTestCase
28 {
33  {
35  new ‪SimpleLockStrategy('999999999');
36  self::assertDirectoryExists(‪Environment::getVarPath() . '/' . ‪SimpleLockStrategy::FILE_LOCK_FOLDER);
37  }
38 
43  {
44  $lock = $this->getAccessibleMock(SimpleLockStrategy::class, null, ['999999999']);
45  self::assertSame(‪Environment::getVarPath() . '/' . ‪SimpleLockStrategy::FILE_LOCK_FOLDER . 'simple_' . md5('999999999'), $lock->_get('filePath'));
46  }
47 
51  public function ‪acquireFixesPermissionsOnLockFile(): void
52  {
54  self::markTestSkipped('Test not available on Windows.');
55  }
56  // Use a very high id to be unique
57  $lock = $this->getAccessibleMock(SimpleLockStrategy::class, null, ['999999999']);
58 
59  $pathOfLockFile = $lock->_get('filePath');
60 
61  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['fileCreateMask'] = '0777';
62 
63  // Acquire lock, get actual file permissions and clean up
64  $lock->acquire();
65  clearstatcache();
66  $resultFilePermissions = substr(decoct(fileperms($pathOfLockFile)), 2);
67  $lock->release();
68  self::assertEquals('0777', $resultFilePermissions);
69  }
70 
75  {
76  $lock = $this->getAccessibleMock(SimpleLockStrategy::class, null, ['999999999']);
77 
78  $pathOfLockFile = $lock->_get('filePath');
79 
80  $lock->acquire();
81  $lock->release();
82 
83  self::assertFalse(is_file($pathOfLockFile));
84  }
85 
89  public static function ‪invalidFileReferences(): array
90  {
91  return [
92  'not within project path' => [tempnam(sys_get_temp_dir(), 'foo')],
93  'directory traversal' => [‪Environment::getVarPath() . '/../var/lock/foo'],
94  'directory traversal 2' => [‪Environment::getVarPath() . '/lock/../../var/lock/foo'],
95  ];
96  }
97 
105  {
106  // Create test file
107  @touch($file);
108  if (!is_file($file)) {
109  self::markTestIncomplete('releaseDoesNotRemoveFilesNotWithinTypo3TempLocksDirectory() skipped: Test file could not be created');
110  }
111  // Create instance, set lock file to invalid path
112  $lock = $this->getAccessibleMock(SimpleLockStrategy::class, null, ['999999999']);
113  $lock->_set('filePath', $file);
114  $lock->_set('isAcquired', true);
115 
116  // Call release method
117  $lock->release();
118  // Check if file is still there and clean up
119  $fileExists = is_file($file);
120  if (is_file($file)) {
121  unlink($file);
122  }
123  self::assertTrue($fileExists);
124  }
125 
129  public function ‪getPriorityReturnsDefaultPriority(): void
130  {
132  }
133 
137  public function ‪setPriority(): void
138  {
139  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][SimpleLockStrategy::class]['priority'] = 10;
140 
141  self::assertEquals(10, ‪SimpleLockStrategy::getPriority());
142  unset(‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][SimpleLockStrategy::class]['priority']);
143  }
144 }
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy\DEFAULT_PRIORITY
‪const DEFAULT_PRIORITY
Definition: SemaphoreLockStrategy.php:32
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy\getPriority
‪static int getPriority()
Definition: SemaphoreLockStrategy.php:161
‪TYPO3\CMS\Core\Tests\Unit\Locking\SimpleLockStrategyTest\releaseDoesNotRemoveFilesNotWithinTypo3TempLocksDirectory
‪releaseDoesNotRemoveFilesNotWithinTypo3TempLocksDirectory($file)
Definition: SimpleLockStrategyTest.php:104
‪TYPO3\CMS\Core\Tests\Unit\Locking\SimpleLockStrategyTest\getPriorityReturnsDefaultPriority
‪getPriorityReturnsDefaultPriority()
Definition: SimpleLockStrategyTest.php:129
‪TYPO3\CMS\Core\Tests\Unit\Locking\SimpleLockStrategyTest
Definition: SimpleLockStrategyTest.php:28
‪TYPO3\CMS\Core\Tests\Unit\Locking\SimpleLockStrategyTest\constructorCreatesLockDirectoryIfNotExisting
‪constructorCreatesLockDirectoryIfNotExisting()
Definition: SimpleLockStrategyTest.php:32
‪TYPO3\CMS\Core\Tests\Unit\Locking\SimpleLockStrategyTest\acquireFixesPermissionsOnLockFile
‪acquireFixesPermissionsOnLockFile()
Definition: SimpleLockStrategyTest.php:51
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy\getPriority
‪static int getPriority()
Definition: SimpleLockStrategy.php:191
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static getVarPath()
Definition: Environment.php:197
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy
Definition: SemaphoreLockStrategy.php:28
‪TYPO3\CMS\Core\Tests\Unit\Locking\SimpleLockStrategyTest\releaseRemovesLockfileInTypo3TempLocks
‪releaseRemovesLockfileInTypo3TempLocks()
Definition: SimpleLockStrategyTest.php:74
‪TYPO3\CMS\Core\Tests\Unit\Locking\SimpleLockStrategyTest\setPriority
‪setPriority()
Definition: SimpleLockStrategyTest.php:137
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy\FILE_LOCK_FOLDER
‪const FILE_LOCK_FOLDER
Definition: SimpleLockStrategy.php:31
‪TYPO3\CMS\Core\Tests\Unit\Locking
Definition: FileLockStrategyTest.php:18
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy
Definition: SimpleLockStrategy.php:28
‪TYPO3\CMS\Core\Tests\Unit\Locking\SimpleLockStrategyTest\invalidFileReferences
‪static invalidFileReferences()
Definition: SimpleLockStrategyTest.php:89
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Core\Utility\GeneralUtility\rmdir
‪static bool rmdir($path, $removeNonEmpty=false)
Definition: GeneralUtility.php:1806
‪TYPO3\CMS\Core\Tests\Unit\Locking\SimpleLockStrategyTest\constructorSetsResourceToPathWithIdIfUsingSimpleLocking
‪constructorSetsResourceToPathWithIdIfUsingSimpleLocking()
Definition: SimpleLockStrategyTest.php:42
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪TYPO3\CMS\Core\Core\Environment\isWindows
‪static isWindows()
Definition: Environment.php:287