‪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 
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
26 final class ‪SimpleLockStrategyTest extends UnitTestCase
27 {
32  {
34  new ‪SimpleLockStrategy('999999999');
35  self::assertDirectoryExists(‪Environment::getVarPath() . '/' . ‪SimpleLockStrategy::FILE_LOCK_FOLDER);
36  }
37 
42  {
43  $lock = $this->getAccessibleMock(SimpleLockStrategy::class, null, ['999999999']);
44  self::assertSame(‪Environment::getVarPath() . '/' . ‪SimpleLockStrategy::FILE_LOCK_FOLDER . 'simple_' . md5('999999999'), $lock->_get('filePath'));
45  }
46 
50  public function ‪acquireFixesPermissionsOnLockFile(): void
51  {
53  self::markTestSkipped('Test not available on Windows.');
54  }
55  // Use a very high id to be unique
56  $lock = $this->getAccessibleMock(SimpleLockStrategy::class, null, ['999999999']);
57 
58  $pathOfLockFile = $lock->_get('filePath');
59 
60  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['fileCreateMask'] = '0777';
61 
62  // Acquire lock, get actual file permissions and clean up
63  $lock->acquire();
64  clearstatcache();
65  $resultFilePermissions = substr(decoct(fileperms($pathOfLockFile)), 2);
66  $lock->release();
67  self::assertEquals('0777', $resultFilePermissions);
68  }
69 
74  {
75  $lock = $this->getAccessibleMock(SimpleLockStrategy::class, null, ['999999999']);
76 
77  $pathOfLockFile = $lock->_get('filePath');
78 
79  $lock->acquire();
80  $lock->release();
81 
82  self::assertFalse(is_file($pathOfLockFile));
83  }
84 
86  {
87  return [
88  'not within project path' => [tempnam(sys_get_temp_dir(), 'foo')],
89  'directory traversal' => [‪Environment::getVarPath() . '/../var/lock/foo'],
90  'directory traversal 2' => [‪Environment::getVarPath() . '/lock/../../var/lock/foo'],
91  ];
92  }
93 
99  {
100  // Create test file
101  @touch($file);
102  if (!is_file($file)) {
103  self::markTestIncomplete('releaseDoesNotRemoveFilesNotWithinTypo3TempLocksDirectory() skipped: Test file could not be created');
104  }
105  // Create instance, set lock file to invalid path
106  $lock = $this->getAccessibleMock(SimpleLockStrategy::class, null, ['999999999']);
107  $lock->_set('filePath', $file);
108  $lock->_set('isAcquired', true);
109 
110  // Call release method
111  $lock->release();
112  // Check if file is still there and clean up
113  $fileExists = is_file($file);
114  if (is_file($file)) {
115  unlink($file);
116  }
117  self::assertTrue($fileExists);
118  }
119 
123  public function ‪getPriorityReturnsDefaultPriority(): void
124  {
126  }
127 
131  public function ‪setPriority(): void
132  {
133  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][SimpleLockStrategy::class]['priority'] = 10;
134 
135  self::assertEquals(10, ‪SimpleLockStrategy::getPriority());
136  unset(‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][SimpleLockStrategy::class]['priority']);
137  }
138 }
‪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\getPriorityReturnsDefaultPriority
‪getPriorityReturnsDefaultPriority()
Definition: SimpleLockStrategyTest.php:123
‪TYPO3\CMS\Core\Tests\Unit\Locking\SimpleLockStrategyTest\releaseDoesNotRemoveFilesNotWithinTypo3TempLocksDirectoryDataProvider
‪static releaseDoesNotRemoveFilesNotWithinTypo3TempLocksDirectoryDataProvider()
Definition: SimpleLockStrategyTest.php:85
‪TYPO3\CMS\Core\Tests\Unit\Locking\SimpleLockStrategyTest
Definition: SimpleLockStrategyTest.php:27
‪TYPO3\CMS\Core\Tests\Unit\Locking\SimpleLockStrategyTest\constructorCreatesLockDirectoryIfNotExisting
‪constructorCreatesLockDirectoryIfNotExisting()
Definition: SimpleLockStrategyTest.php:31
‪TYPO3\CMS\Core\Tests\Unit\Locking\SimpleLockStrategyTest\acquireFixesPermissionsOnLockFile
‪acquireFixesPermissionsOnLockFile()
Definition: SimpleLockStrategyTest.php:50
‪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:73
‪TYPO3\CMS\Core\Tests\Unit\Locking\SimpleLockStrategyTest\setPriority
‪setPriority()
Definition: SimpleLockStrategyTest.php:131
‪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\releaseDoesNotRemoveFilesNotWithinTypo3TempLocksDirectory
‪releaseDoesNotRemoveFilesNotWithinTypo3TempLocksDirectory(string $file)
Definition: SimpleLockStrategyTest.php:98
‪$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:1691
‪TYPO3\CMS\Core\Tests\Unit\Locking\SimpleLockStrategyTest\constructorSetsResourceToPathWithIdIfUsingSimpleLocking
‪constructorSetsResourceToPathWithIdIfUsingSimpleLocking()
Definition: SimpleLockStrategyTest.php:41
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪TYPO3\CMS\Core\Core\Environment\isWindows
‪static isWindows()
Definition: Environment.php:276