|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter\Session\Handlers; |
| 15 | + |
| 16 | +use CodeIgniter\Test\CIUnitTestCase; |
| 17 | +use Config\Session as SessionConfig; |
| 18 | +use PHPUnit\Framework\Attributes\Group; |
| 19 | + |
| 20 | +/** |
| 21 | + * @internal |
| 22 | + */ |
| 23 | +#[Group('Others')] |
| 24 | +final class FileHandlerTest extends CIUnitTestCase |
| 25 | +{ |
| 26 | + private string $savePath; |
| 27 | + |
| 28 | + protected function setUp(): void |
| 29 | + { |
| 30 | + parent::setUp(); |
| 31 | + |
| 32 | + $this->savePath = WRITEPATH . 'session_test'; |
| 33 | + if (! is_dir($this->savePath)) { |
| 34 | + mkdir($this->savePath, 0700, true); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + protected function tearDown(): void |
| 39 | + { |
| 40 | + parent::tearDown(); |
| 41 | + |
| 42 | + // clean up |
| 43 | + if (is_dir($this->savePath)) { |
| 44 | + $files = array_diff(scandir($this->savePath), ['.', '..']); |
| 45 | + |
| 46 | + foreach ($files as $file) { |
| 47 | + $path = $this->savePath . DIRECTORY_SEPARATOR . $file; |
| 48 | + if (is_link($path) || is_file($path)) { |
| 49 | + @unlink($path); |
| 50 | + } |
| 51 | + } |
| 52 | + rmdir($this->savePath); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public function testGcIgnoresSymlinks(): void |
| 57 | + { |
| 58 | + $config = new SessionConfig(); |
| 59 | + $config->savePath = $this->savePath; |
| 60 | + $config->cookieName = 'ci_session'; |
| 61 | + |
| 62 | + $handler = new FileHandler($config, '127.0.0.1'); |
| 63 | + |
| 64 | + $sessionId = '1234567890abcdef1234567890abcdef'; |
| 65 | + $sessionFile = $this->savePath . DIRECTORY_SEPARATOR . 'ci_session' . $sessionId; |
| 66 | + |
| 67 | + $targetFile = $this->savePath . DIRECTORY_SEPARATOR . 'target.txt'; |
| 68 | + file_put_contents($targetFile, 'target'); |
| 69 | + touch($targetFile, time() - 10000); |
| 70 | + |
| 71 | + symlink($targetFile, $sessionFile); |
| 72 | + |
| 73 | + $this->assertTrue(is_link($sessionFile)); |
| 74 | + |
| 75 | + $collected = $handler->gc(5000); |
| 76 | + |
| 77 | + $this->assertTrue(is_link($sessionFile)); |
| 78 | + $this->assertSame(0, $collected); |
| 79 | + } |
| 80 | + |
| 81 | + public function testDestroyIgnoresSymlinks(): void |
| 82 | + { |
| 83 | + $config = new SessionConfig(); |
| 84 | + $config->savePath = $this->savePath; |
| 85 | + $config->cookieName = 'ci_session'; |
| 86 | + |
| 87 | + $handler = new FileHandler($config, '127.0.0.1'); |
| 88 | + |
| 89 | + $sessionId = '1234567890abcdef1234567890abcdef'; |
| 90 | + $handler->open($this->savePath, 'ci_session'); |
| 91 | + |
| 92 | + $sessionFile = $this->savePath . DIRECTORY_SEPARATOR . 'ci_session' . $sessionId; |
| 93 | + |
| 94 | + $targetFile = $this->savePath . DIRECTORY_SEPARATOR . 'target.txt'; |
| 95 | + file_put_contents($targetFile, 'target'); |
| 96 | + |
| 97 | + symlink($targetFile, $sessionFile); |
| 98 | + |
| 99 | + $this->assertTrue(is_link($sessionFile)); |
| 100 | + |
| 101 | + $result = $handler->destroy($sessionId); |
| 102 | + |
| 103 | + $this->assertTrue(is_link($sessionFile)); |
| 104 | + } |
| 105 | +} |
0 commit comments