Security update for Core, with self-updated composer
[yaffs-website] / vendor / doctrine / cache / tests / Doctrine / Tests / Common / Cache / BaseFileCacheTest.php
1 <?php
2
3 namespace Doctrine\Tests\Common\Cache;
4
5 use Doctrine\Common\Cache\FileCache;
6 use RecursiveDirectoryIterator;
7 use RecursiveIteratorIterator;
8
9 abstract class BaseFileCacheTest extends CacheTest
10 {
11     protected $directory;
12
13     protected function setUp()
14     {
15         do {
16             $this->directory = sys_get_temp_dir() . '/doctrine_cache_'. uniqid();
17         } while (file_exists($this->directory));
18     }
19
20     protected function tearDown()
21     {
22         if ( ! is_dir($this->directory)) {
23             return;
24         }
25
26         $iterator = new RecursiveDirectoryIterator($this->directory);
27
28         foreach (new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file) {
29             if ($file->isFile()) {
30                 @unlink($file->getRealPath());
31             } elseif ($file->isDir()) {
32                 @rmdir($file->getRealPath());
33             }
34         }
35
36         @rmdir($this->directory);
37     }
38
39     public function testFlushAllRemovesBalancingDirectories()
40     {
41         $cache = $this->_getCacheDriver();
42
43         $this->assertTrue($cache->save('key1', 1));
44         $this->assertTrue($cache->save('key2', 2));
45         $this->assertTrue($cache->flushAll());
46
47         $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->directory, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST);
48
49         $this->assertCount(0, $iterator);
50     }
51
52     protected function isSharedStorage()
53     {
54         return false;
55     }
56
57     public function getPathLengthsToTest()
58     {
59         // Windows officially supports 260 bytes including null terminator
60         // 258 bytes available to use due to php bug #70943
61         // Windows officially supports 260 bytes including null terminator
62         // 259 characters is too large due to PHP bug (https://bugs.php.net/bug.php?id=70943)
63         // 260 characters is too large - null terminator is included in allowable length
64         return array(
65             array(257, false),
66             array(258, false),
67             array(259, true),
68             array(260, true)
69         );
70     }
71
72     private static function getBasePathForWindowsPathLengthTests($pathLength)
73     {
74         return FileCacheTest::getBasePathForWindowsPathLengthTests($pathLength);
75     }
76
77     /**
78      * @param int    $length
79      * @param string $basePath
80      *
81      * @return array
82      */
83     private static function getKeyAndPathFittingLength($length, $basePath)
84     {
85         $baseDirLength = strlen($basePath);
86         $extensionLength = strlen('.doctrine.cache');
87         $directoryLength = strlen(DIRECTORY_SEPARATOR . 'aa' . DIRECTORY_SEPARATOR);
88         $namespaceAndBracketLength = strlen(bin2hex("[][1]"));
89         $keyLength = $length
90             - ($baseDirLength
91                 + $extensionLength
92                 + $directoryLength
93                 + $namespaceAndBracketLength);
94
95         $key = str_repeat('a', floor($keyLength / 2));
96         $namespacedKey = '[' . $key . '][1]';
97
98         $keyHash = hash('sha256', $namespacedKey);
99
100         $keyPath = $basePath
101             . DIRECTORY_SEPARATOR
102             . substr($keyHash, 0, 2)
103             . DIRECTORY_SEPARATOR
104             . bin2hex($namespacedKey)
105             . '.doctrine.cache';
106
107         $hashedKeyPath = $basePath
108             . DIRECTORY_SEPARATOR
109             . substr($keyHash, 0, 2)
110             . DIRECTORY_SEPARATOR
111             . '_' . $keyHash
112             . '.doctrine.cache';
113
114         return array($key, $keyPath, $hashedKeyPath);
115     }
116
117     /**
118      * @dataProvider getPathLengthsToTest
119      *
120      * @param int  $length
121      * @param bool $pathShouldBeHashed
122      */
123     public function testWindowsPathLengthLimitIsCorrectlyHandled($length, $pathShouldBeHashed)
124     {
125         $this->directory = self::getBasePathForWindowsPathLengthTests($length);
126
127         list($key, $keyPath, $hashedKeyPath) = self::getKeyAndPathFittingLength($length, $this->directory);
128
129         $this->assertEquals($length, strlen($keyPath), 'Unhashed path should be of correct length.');
130
131         $cacheClass = get_class($this->_getCacheDriver());
132         /* @var $cache \Doctrine\Common\Cache\FileCache */
133         $cache = new $cacheClass($this->directory, '.doctrine.cache');
134
135         // Trick it into thinking this is windows.
136         $reflClass = new \ReflectionClass(FileCache::class);
137         $reflProp = $reflClass->getProperty('isRunningOnWindows');
138         $reflProp->setAccessible(true);
139         $reflProp->setValue($cache, true);
140         $reflProp->setAccessible(false);
141
142         $value = uniqid('value', true);
143
144         $cache->save($key, $value);
145         $this->assertEquals($value, $cache->fetch($key));
146
147         if ($pathShouldBeHashed) {
148             $this->assertFileExists($hashedKeyPath, 'Path generated for key should be hashed.');
149             unlink($hashedKeyPath);
150         } else {
151             $this->assertFileExists($keyPath, 'Path generated for key should not be hashed.');
152             unlink($keyPath);
153         }
154     }
155 }