‪TYPO3CMS  10.4
SimpleLockStrategyTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use PHPUnit\Framework\SkippedTestError;
23 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
24 
28 class ‪SimpleLockStrategyTest extends UnitTestCase
29 {
34  {
36  new ‪SimpleLockStrategy('999999999');
37  self::assertTrue(is_dir(‪Environment::getVarPath() . '/' . ‪SimpleLockStrategy::FILE_LOCK_FOLDER));
38  }
39 
44  {
45  $lock = $this->getAccessibleMock(SimpleLockStrategy::class, ['dummy'], ['999999999']);
46  self::assertSame(‪Environment::getVarPath() . '/' . ‪SimpleLockStrategy::FILE_LOCK_FOLDER . 'simple_' . md5('999999999'), $lock->_get('filePath'));
47  }
48 
53  {
55  self::markTestSkipped('Test not available on Windows.');
56  }
57  // Use a very high id to be unique
59  $lock = $this->getAccessibleMock(SimpleLockStrategy::class, ['dummy'], ['999999999']);
60 
61  $pathOfLockFile = $lock->_get('filePath');
62 
63  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['fileCreateMask'] = '0777';
64 
65  // Acquire lock, get actual file permissions and clean up
66  $lock->acquire();
67  clearstatcache();
68  $resultFilePermissions = substr(decoct(fileperms($pathOfLockFile)), 2);
69  $lock->release();
70  self::assertEquals($resultFilePermissions, '0777');
71  }
72 
77  {
79  $lock = $this->getAccessibleMock(SimpleLockStrategy::class, ['dummy'], ['999999999']);
80 
81  $pathOfLockFile = $lock->_get('filePath');
82 
83  $lock->acquire();
84  $lock->release();
85 
86  self::assertFalse(is_file($pathOfLockFile));
87  }
88 
92  public function ‪invalidFileReferences()
93  {
94  return [
95  'not within project path' => [tempnam(sys_get_temp_dir(), 'foo')],
96  'directory traversal' => [‪Environment::getVarPath() . '/../var/lock/foo'],
97  'directory traversal 2' => [‪Environment::getVarPath() . '/lock/../../var/lock/foo'],
98  ];
99  }
100 
108  {
109  // Create test file
110  @touch($file);
111  if (!is_file($file)) {
112  self::markTestIncomplete('releaseDoesNotRemoveFilesNotWithinTypo3TempLocksDirectory() skipped: Test file could not be created');
113  }
114  // Create instance, set lock file to invalid path
116  $lock = $this->getAccessibleMock(SimpleLockStrategy::class, ['dummy'], ['999999999']);
117  $lock->_set('filePath', $file);
118  $lock->_set('isAcquired', true);
119 
120  // Call release method
121  $lock->release();
122  // Check if file is still there and clean up
123  $fileExists = is_file($file);
124  if (is_file($file)) {
125  unlink($file);
126  }
127  self::assertTrue($fileExists);
128  }
129 
134  {
136  }
137 
141  public function ‪setPriority()
142  {
143  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][SimpleLockStrategy::class]['priority'] = 10;
144 
145  self::assertEquals(10, ‪SimpleLockStrategy::getPriority());
146  unset(‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][SimpleLockStrategy::class]['priority']);
147  }
148 }
‪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:160
‪TYPO3\CMS\Core\Tests\Unit\Locking\SimpleLockStrategyTest\releaseDoesNotRemoveFilesNotWithinTypo3TempLocksDirectory
‪releaseDoesNotRemoveFilesNotWithinTypo3TempLocksDirectory($file)
Definition: SimpleLockStrategyTest.php:107
‪TYPO3\CMS\Core\Tests\Unit\Locking\SimpleLockStrategyTest\getPriorityReturnsDefaultPriority
‪getPriorityReturnsDefaultPriority()
Definition: SimpleLockStrategyTest.php:133
‪TYPO3\CMS\Core\Tests\Unit\Locking\SimpleLockStrategyTest
Definition: SimpleLockStrategyTest.php:29
‪TYPO3\CMS\Core\Tests\Unit\Locking\SimpleLockStrategyTest\constructorCreatesLockDirectoryIfNotExisting
‪constructorCreatesLockDirectoryIfNotExisting()
Definition: SimpleLockStrategyTest.php:33
‪TYPO3\CMS\Core\Tests\Unit\Locking\SimpleLockStrategyTest\acquireFixesPermissionsOnLockFile
‪acquireFixesPermissionsOnLockFile()
Definition: SimpleLockStrategyTest.php:52
‪TYPO3\CMS\Core\Core\Environment\isWindows
‪static bool isWindows()
Definition: Environment.php:292
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy\getPriority
‪static int getPriority()
Definition: SimpleLockStrategy.php:191
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy
Definition: SemaphoreLockStrategy.php:28
‪TYPO3\CMS\Core\Tests\Unit\Locking\SimpleLockStrategyTest\releaseRemovesLockfileInTypo3TempLocks
‪releaseRemovesLockfileInTypo3TempLocks()
Definition: SimpleLockStrategyTest.php:76
‪TYPO3\CMS\Core\Tests\Unit\Locking\SimpleLockStrategyTest\setPriority
‪setPriority()
Definition: SimpleLockStrategyTest.php:141
‪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:16
‪TYPO3\CMS\Core\Tests\Unit\Locking\SimpleLockStrategyTest\invalidFileReferences
‪invalidFileReferences()
Definition: SimpleLockStrategyTest.php:92
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy
Definition: SimpleLockStrategy.php:28
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40
‪TYPO3\CMS\Core\Utility\GeneralUtility\rmdir
‪static bool rmdir($path, $removeNonEmpty=false)
Definition: GeneralUtility.php:2075
‪TYPO3\CMS\Core\Tests\Unit\Locking\SimpleLockStrategyTest\constructorSetsResourceToPathWithIdIfUsingSimpleLocking
‪constructorSetsResourceToPathWithIdIfUsingSimpleLocking()
Definition: SimpleLockStrategyTest.php:43
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static string getVarPath()
Definition: Environment.php:192