-
Notifications
You must be signed in to change notification settings - Fork 167
Expand file tree
/
Copy pathFileSessionStoreTest.php
More file actions
205 lines (164 loc) · 6.8 KB
/
Copy pathFileSessionStoreTest.php
File metadata and controls
205 lines (164 loc) · 6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
/*
* This file is part of the official PHP MCP SDK.
*
* A collaboration between Symfony and the PHP Foundation.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mcp\Tests\Unit\Server\Session;
use Mcp\Exception\ExceptionInterface;
use Mcp\Exception\RuntimeException;
use Mcp\Server\Session\FileSessionStore;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
use Psr\Log\AbstractLogger;
use Psr\Log\LogLevel;
use Symfony\Component\Uid\UuidV4;
class FileSessionStoreTest extends TestCase
{
private string $directory;
protected function setUp(): void
{
$this->directory = sys_get_temp_dir().'/mcp-file-session-store-'.bin2hex(random_bytes(6));
}
protected function tearDown(): void
{
if (!is_dir($this->directory)) {
return;
}
@chmod($this->directory, 0775);
foreach (glob($this->directory.'/*') ?: [] as $file) {
@unlink($file);
}
@rmdir($this->directory);
}
#[TestDox('creates the session directory when it does not exist yet')]
public function testCreatesMissingDirectory(): void
{
new FileSessionStore($this->directory);
$this->assertDirectoryExists($this->directory);
}
#[TestDox('round-trips a session payload through the filesystem')]
public function testWriteThenRead(): void
{
$store = new FileSessionStore($this->directory);
$id = new UuidV4();
$store->write($id, 'payload');
$this->assertTrue($store->exists($id));
$this->assertSame('payload', $store->read($id));
}
#[TestDox('gc() only deletes expired session files, never foreign files in the directory')]
public function testGcLeavesForeignFilesAlone(): void
{
$store = new FileSessionStore($this->directory, ttl: 60);
$id = new UuidV4();
$store->write($id, 'payload');
$foreign = $this->directory.'/important.lock';
file_put_contents($foreign, 'not a session');
// Make everything stale
$expired = time() - 120;
touch($this->directory.'/'.$id->toRfc4122(), $expired);
touch($foreign, $expired);
$deleted = $store->gc();
$this->assertEquals([$id], $deleted);
$this->assertFileDoesNotExist($this->directory.'/'.$id->toRfc4122());
$this->assertFileExists($foreign);
}
#[TestDox('rejects an unwritable directory with the SDK\'s own exception')]
public function testUnwritableDirectoryThrowsPackageException(): void
{
mkdir($this->directory, 0775, true);
chmod($this->directory, 0555);
clearstatcache(true, $this->directory);
if (is_writable($this->directory)) {
$this->markTestSkipped('Permission bits do not restrict writes here (running as root, or a filesystem that ignores them).');
}
// The store's only throw must stay inside the package hierarchy, so a
// consumer catching ExceptionInterface sees it rather than a bare SPL
// RuntimeException escaping the SDK.
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage(\sprintf('Session directory "%s" is not writable.', $this->directory));
try {
new FileSessionStore($this->directory);
} catch (RuntimeException $e) {
$this->assertInstanceOf(ExceptionInterface::class, $e);
throw $e;
}
}
#[TestDox('logs a warning when a session file cannot be read')]
public function testUnreadableSessionFileLogsWarning(): void
{
$logger = new WarningCollectingLogger();
$store = new FileSessionStore($this->directory, logger: $logger);
$id = new UuidV4();
$store->write($id, 'payload');
$path = $this->directory.\DIRECTORY_SEPARATOR.$id->toRfc4122();
chmod($path, 0000);
clearstatcache(true, $path);
if (is_readable($path)) {
$this->markTestSkipped('Permission bits do not restrict reads here (running as root, or a filesystem that ignores them).');
}
$this->assertFalse($store->read($id));
$this->assertCount(1, $logger->warnings);
$this->assertSame('Failed to read session file.', $logger->warnings[0]['message']);
$this->assertSame($path, $logger->warnings[0]['context']['path']);
}
#[TestDox('logs a warning when a session file cannot be written')]
public function testUnwritableSessionFileLogsWarning(): void
{
$logger = new WarningCollectingLogger();
$store = new FileSessionStore($this->directory, logger: $logger);
chmod($this->directory, 0555);
clearstatcache(true, $this->directory);
if (is_writable($this->directory)) {
$this->markTestSkipped('Permission bits do not restrict writes here (running as root, or a filesystem that ignores them).');
}
$this->assertFalse($store->write(new UuidV4(), 'payload'));
$this->assertCount(1, $logger->warnings);
$this->assertSame('Failed to write session file.', $logger->warnings[0]['message']);
}
#[TestDox('logs a warning when the session directory cannot be opened for garbage collection')]
public function testGcLogsWarningWhenDirectoryVanished(): void
{
$logger = new WarningCollectingLogger();
$store = new FileSessionStore($this->directory, logger: $logger);
rmdir($this->directory);
$this->assertSame([], $store->gc());
$this->assertCount(1, $logger->warnings);
$this->assertSame('Failed to open session directory for garbage collection.', $logger->warnings[0]['message']);
$this->assertSame($this->directory, $logger->warnings[0]['context']['directory']);
}
#[TestDox('stays silent on the happy path')]
public function testHappyPathLogsNothing(): void
{
$logger = new WarningCollectingLogger();
$store = new FileSessionStore($this->directory, logger: $logger);
$id = new UuidV4();
$store->write($id, 'payload');
$store->read($id);
$store->destroy($id);
$store->gc();
$this->assertSame([], $logger->warnings);
}
}
/**
* Keeps every warning with its context, so a silent failure can be told apart
* from one the operator was told about.
*/
final class WarningCollectingLogger extends AbstractLogger
{
/** @var list<array{message: string, context: array<string, mixed>}> */
public array $warnings = [];
/**
* @param string|\Stringable $message
* @param array<string, mixed> $context
*/
public function log($level, $message, array $context = []): void
{
if (LogLevel::WARNING === $level) {
$this->warnings[] = ['message' => (string) $message, 'context' => $context];
}
}
}