| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- <?php
- declare(strict_types=1);
- final class SessionTest extends \PHPUnit\Framework\TestCase {
- private string $originalSavePath = '';
- private string $testSavePath = '';
- #[\Override]
- protected function setUp(): void {
- $this->originalSavePath = (string)ini_get('session.save_path');
- }
- #[\Override]
- protected function tearDown(): void {
- if (is_dir($this->testSavePath)) {
- rmdir($this->testSavePath);
- }
- ini_set('session.save_path', $this->originalSavePath);
- }
- public function testRegenerateIDOnHealthyStorage(): void {
- $save_path = session_save_path() ?: sys_get_temp_dir();
- Minz_Session::init('FreshRSS', volatile: false);
- self::assertTrue(file_exists($save_path . '/sess_' . session_id()));
- $previous_id = session_id();
- Minz_Session::_param('probe', 'ok');
- Minz_Session::regenerateID('FreshRSS');
- self::assertNotSame($previous_id, session_id());
- self::assertFalse(file_exists($save_path . '/sess_' . $previous_id));
- self::assertTrue(file_exists($save_path . '/sess_' . session_id()));
- self::assertSame('ok', Minz_Session::paramString('probe'));
- session_unset();
- session_write_close();
- unlink($save_path . '/sess_' . session_id());
- }
- public function testRegenerateIDOnBrokenStorage(): void {
- $this->testSavePath = sys_get_temp_dir() . '/frss_test_sessions_' . bin2hex(random_bytes(4));
- self::assertNotFalse(mkdir($this->testSavePath, 0700));
- ini_set('session.save_path', $this->testSavePath . '/missing_subdir');
- $this->expectException(RuntimeException::class);
- Minz_Session::regenerateID('FreshRSS');
- }
- public function testDeleteLegacyCookie(): void {
- $requestUri = $_SERVER['REQUEST_URI'] ?? null;
- $cookie = $_COOKIE['FreshRSS'] ?? null;
- $_SERVER['REQUEST_URI'] = '/i/index.php';
- unset($_COOKIE['FreshRSS']);
- self::assertFalse(Minz_Session::deleteLegacyCookie('FreshRSS'));
- $_COOKIE['FreshRSS'] = 'stale';
- self::assertTrue(Minz_Session::deleteLegacyCookie('FreshRSS'));
- if ($cookie === null) {
- unset($_COOKIE['FreshRSS']);
- } else {
- $_COOKIE['FreshRSS'] = $cookie;
- }
- if ($requestUri === null) {
- unset($_SERVER['REQUEST_URI']);
- } else {
- $_SERVER['REQUEST_URI'] = $requestUri;
- }
- }
- public function testDetectDuplicatedSessionCookie(): void {
- $hasDuplicatedSessionCookie = new ReflectionMethod(Minz_Session::class, 'hasDuplicatedSessionCookie');
- $header = $_SERVER['HTTP_COOKIE'] ?? null;
- $_SERVER['HTTP_COOKIE'] = 'FreshRSS_login=token; FreshRSS=only-one';
- self::assertFalse($hasDuplicatedSessionCookie->invoke(null, 'FreshRSS'));
- $_SERVER['HTTP_COOKIE'] = 'FreshRSS=stale; FreshRSS_login=token; FreshRSS=current';
- self::assertTrue($hasDuplicatedSessionCookie->invoke(null, 'FreshRSS'));
- if ($header === null) {
- unset($_SERVER['HTTP_COOKIE']);
- } else {
- $_SERVER['HTTP_COOKIE'] = $header;
- }
- }
- }
|