TYPO3 CMS  TYPO3_6-2
LockerTest.php
Go to the documentation of this file.
1 <?php
3 
17 
24 
26  // tests concerning __construct
28 
32  $instance = new Locker('999999999');
33  $this->assertSame(Locker::LOCKING_METHOD_SIMPLE, $instance->getMethod());
34  }
35 
40  $instance = new Locker('999999999', Locker::LOCKING_METHOD_FLOCK);
41  $this->assertSame(Locker::LOCKING_METHOD_FLOCK, $instance->getMethod());
42  }
43 
49  new Locker('999999999', 'foo');
50  }
51 
56  $GLOBALS['TYPO3_CONF_VARS']['SYS']['lockingMode'] = Locker::LOCKING_METHOD_SIMPLE;
57  $instance = new Locker('999999999', '');
58  $this->assertSame(Locker::LOCKING_METHOD_SIMPLE, $instance->getMethod());
59  }
60 
65  $instance = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Locking\\Locker', array('dummy'), array('999999999', Locker::LOCKING_METHOD_DISABLED));
66  $this->assertSame(150, $instance->_get('loops'));
67  }
68 
73  $instance = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Locking\\Locker', array('dummy'), array('999999999', Locker::LOCKING_METHOD_DISABLED, 10));
74  $this->assertSame(10, $instance->_get('loops'));
75  }
76 
81  $instance = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Locking\\Locker', array('dummy'), array('999999999', Locker::LOCKING_METHOD_DISABLED));
82  $this->assertSame(200, $instance->_get('step'));
83  }
84 
89  $instance = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Locking\\Locker', array('dummy'), array('999999999', Locker::LOCKING_METHOD_DISABLED, 0, 10));
90  $this->assertSame(10, $instance->_get('step'));
91  }
92 
98  new Locker('999999999', Locker::LOCKING_METHOD_SIMPLE);
99  $this->assertTrue(is_dir(PATH_site . Locker::FILE_LOCK_FOLDER));
100  }
101 
106  $instance = new Locker('999999999', Locker::LOCKING_METHOD_SIMPLE);
107  $this->assertSame(md5('999999999'), $instance->getId());
108  }
109 
114  $instance = new Locker('999999999', Locker::LOCKING_METHOD_SIMPLE);
115  $this->assertSame(PATH_site . Locker::FILE_LOCK_FOLDER . md5('999999999'), $instance->getResource());
116  }
117 
122  if (!function_exists('sem_get')) {
123  $this->markTestSkipped('The system does not support semaphore base locking.');
124  }
125  $instance = new Locker('999999999', Locker::LOCKING_METHOD_SEMAPHORE);
126  $this->assertSame(abs(crc32('999999999')), $instance->getId());
127  }
128 
130  // tests concerning acquire
132 
136  if (TYPO3_OS == 'WIN') {
137  $this->markTestSkipped('acquireFixesPermissionsOnLockFileIfUsingSimpleLogging() test not available on Windows.');
138  }
139  // Use a very high id to be unique
140  $instance = new Locker(999999999, Locker::LOCKING_METHOD_SIMPLE);
141  $instance->setEnableLogging(FALSE);
142  $pathOfLockFile = $instance->getResource();
143  $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0777';
144  // Acquire lock, get actual file permissions and clean up
145  $instance->acquireExclusiveLock();
146  clearstatcache();
147  $resultFilePermissions = substr(decoct(fileperms($pathOfLockFile)), 2);
148  $instance->release();
149  $this->assertEquals($resultFilePermissions, '0777');
150  }
151 
153  // tests concerning release
155 
160  // Use a very high id to be unique
161  $instance = new Locker(999999999, Locker::LOCKING_METHOD_SIMPLE);
162  // Disable logging
163  $instance->setEnableLogging(FALSE);
164  // File pointer to current lock file
165  $lockFile = $instance->getResource();
166  $instance->acquireExclusiveLock();
167  $instance->release();
168  $this->assertFalse(is_file($lockFile));
169  }
170 
174  public function invalidFileReferences() {
175  return array(
176  'simple not within PATH_site' => array('simple', '/tmp/TYPO3-Lock-Test'),
177  'flock not withing PATH_site' => array('flock', '/tmp/TYPO3-Lock-Test'),
178  'simple directory traversal' => array('simple', PATH_site . 'typo3temp/../typo3temp/locks/foo'),
179  'flock directory traversal' => array('flock', PATH_site . 'typo3temp/../typo3temp/locks/foo'),
180  'simple directory traversal 2' => array('simple', PATH_site . 'typo3temp/locks/../locks/foo'),
181  'flock directory traversal 2' => array('flock', PATH_site . 'typo3temp/locks/../locks/foo'),
182  'simple within uploads' => array('simple', PATH_site . 'uploads/TYPO3-Lock-Test'),
183  'flock within uploads' => array('flock', PATH_site . 'uploads/TYPO3-Lock-Test')
184  );
185  }
186 
191  public function releaseDoesNotRemoveFilesNotWithinTypo3TempLocksDirectory($lockMethod, $file) {
192  if (TYPO3_OS === 'WIN') {
193  $this->markTestSkipped('releaseDoesNotRemoveFilesNotWithinTypo3TempLocksDirectory() test not available on Windows.');
194  }
195  // Create test file
196  touch($file);
197  if (!is_file($file)) {
198  $this->markTestSkipped('releaseDoesNotRemoveFilesNotWithinTypo3TempLocksDirectory() skipped: Test file could not be created');
199  }
200  // Create instance, set lockfile to invalid path
201  $instance = new Locker(999999999, $lockMethod);
202  $instance->setEnableLogging(FALSE);
203  $t3libLockReflection = new \ReflectionClass('TYPO3\\CMS\\Core\\Locking\\Locker');
204  $t3libLockReflectionResourceProperty = $t3libLockReflection->getProperty('resource');
205  $t3libLockReflectionResourceProperty->setAccessible(TRUE);
206  $t3libLockReflectionResourceProperty->setValue($instance, $file);
207  $t3libLockReflectionAcquiredProperty = $t3libLockReflection->getProperty('isAcquired');
208  $t3libLockReflectionAcquiredProperty->setAccessible(TRUE);
209  $t3libLockReflectionAcquiredProperty->setValue($instance, TRUE);
210  // Call release method
211  $instance->release();
212  // Check if file is still there and clean up
213  $fileExists = is_file($file);
214  if (is_file($file)) {
215  unlink($file);
216  }
217  $this->assertTrue($fileExists);
218  }
219 
220 }
static rmdir($path, $removeNonEmpty=FALSE)
getAccessibleMock( $originalClassName, array $methods=array(), array $arguments=array(), $mockClassName='', $callOriginalConstructor=TRUE, $callOriginalClone=TRUE, $callAutoload=TRUE)
releaseDoesNotRemoveFilesNotWithinTypo3TempLocksDirectory($lockMethod, $file)
Definition: LockerTest.php:191
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]