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