‪TYPO3CMS  9.5
SimpleLockStrategyTest.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
17 use PHPUnit\Framework\SkippedTestError;
21 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
22 
26 class ‪SimpleLockStrategyTest extends UnitTestCase
27 {
32  {
33  GeneralUtility::rmdir(‪Environment::getVarPath() . '/' . ‪SimpleLockStrategy::FILE_LOCK_FOLDER, true);
34  new ‪SimpleLockStrategy('999999999');
35  $this->assertTrue(is_dir(‪Environment::getVarPath() . '/' . ‪SimpleLockStrategy::FILE_LOCK_FOLDER));
36  }
37 
42  {
43  $lock = $this->getAccessibleMock(SimpleLockStrategy::class, ['dummy'], ['999999999']);
44  $this->assertSame(‪Environment::getVarPath() . '/' . ‪SimpleLockStrategy::FILE_LOCK_FOLDER . 'simple_' . md5('999999999'), $lock->_get('filePath'));
45  }
46 
51  {
53  $this->markTestSkipped('Test not available on Windows.');
54  }
55  // Use a very high id to be unique
57  $lock = $this->getAccessibleMock(SimpleLockStrategy::class, ['dummy'], ['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  $this->assertEquals($resultFilePermissions, '0777');
69  }
70 
75  {
77  $lock = $this->getAccessibleMock(SimpleLockStrategy::class, ['dummy'], ['999999999']);
78 
79  $pathOfLockFile = $lock->_get('filePath');
80 
81  $lock->acquire();
82  $lock->release();
83 
84  $this->assertFalse(is_file($pathOfLockFile));
85  }
86 
90  public function ‪invalidFileReferences()
91  {
92  return [
93  'not within project path' => [tempnam(sys_get_temp_dir(), 'foo')],
94  'directory traversal' => [‪Environment::getVarPath() . '/../var/lock/foo'],
95  'directory traversal 2' => [‪Environment::getVarPath() . '/lock/../../var/lock/foo'],
96  'within uploads' => [‪Environment::getPublicPath() . '/uploads/TYPO3-Lock-Test']
97  ];
98  }
99 
107  {
108  // Create test file
109  @touch($file);
110  if (!is_file($file)) {
111  $this->markTestIncomplete('releaseDoesNotRemoveFilesNotWithinTypo3TempLocksDirectory() skipped: Test file could not be created');
112  }
113  // Create instance, set lock file to invalid path
115  $lock = $this->getAccessibleMock(SimpleLockStrategy::class, ['dummy'], ['999999999']);
116  $lock->_set('filePath', $file);
117  $lock->_set('isAcquired', true);
118 
119  // Call release method
120  $lock->release();
121  // Check if file is still there and clean up
122  $fileExists = is_file($file);
123  if (is_file($file)) {
124  unlink($file);
125  }
126  $this->assertTrue($fileExists);
127  }
128 }
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static string getPublicPath()
Definition: Environment.php:153
‪TYPO3\CMS\Core\Tests\Unit\Locking\SimpleLockStrategyTest\releaseDoesNotRemoveFilesNotWithinTypo3TempLocksDirectory
‪releaseDoesNotRemoveFilesNotWithinTypo3TempLocksDirectory($file)
Definition: SimpleLockStrategyTest.php:106
‪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\Core\Environment\isWindows
‪static bool isWindows()
Definition: Environment.php:266
‪TYPO3\CMS\Core\Tests\Unit\Locking\SimpleLockStrategyTest\releaseRemovesLockfileInTypo3TempLocks
‪releaseRemovesLockfileInTypo3TempLocks()
Definition: SimpleLockStrategyTest.php:74
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy\FILE_LOCK_FOLDER
‪const FILE_LOCK_FOLDER
Definition: SimpleLockStrategy.php:30
‪TYPO3\CMS\Core\Tests\Unit\Locking
Definition: FileLockStrategyTest.php:2
‪TYPO3\CMS\Core\Tests\Unit\Locking\SimpleLockStrategyTest\invalidFileReferences
‪invalidFileReferences()
Definition: SimpleLockStrategyTest.php:90
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy
Definition: SimpleLockStrategy.php:27
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:39
‪TYPO3\CMS\Core\Tests\Unit\Locking\SimpleLockStrategyTest\constructorSetsResourceToPathWithIdIfUsingSimpleLocking
‪constructorSetsResourceToPathWithIdIfUsingSimpleLocking()
Definition: SimpleLockStrategyTest.php:41
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static string getVarPath()
Definition: Environment.php:165