Repository navigation
Expand file tree
/
Copy pathInteractiveExecStreamTest.php
More file actions
231 lines (209 loc) · 8.32 KB
/
Copy pathInteractiveExecStreamTest.php
File metadata and controls
231 lines (209 loc) · 8.32 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
declare(strict_types=1);
namespace Docker\Tests\Stream;
use Docker\Exception\InteractiveExecTimeoutException;
use Docker\Stream\InteractiveExecStream;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
class InteractiveExecStreamTest extends TestCase
{
private array $sockets = [];
private ?InteractiveExecStream $stream = null;
protected function tearDown(): void
{
$this->stream?->close();
foreach ($this->sockets as $socket) {
if (\is_resource($socket)) {
fclose($socket);
}
}
parent::tearDown();
}
private function open(bool $multiplexed = true, ?int $timeoutMs = null): InteractiveExecStream
{
$this->sockets = stream_socket_pair(\STREAM_PF_UNIX, \STREAM_SOCK_STREAM, \STREAM_IPPROTO_IP);
self::assertIsArray($this->sockets);
stream_set_blocking($this->sockets[1], false);
return $this->stream = new InteractiveExecStream($this->sockets[0], $multiplexed, $timeoutMs);
}
public function testHalfCloseKeepsOutputReadable(): void
{
$stream = $this->open();
self::assertSame(5, $stream->writeStdin('input'));
self::assertSame('input', fread($this->sockets[1], 20));
$stream->closeStdin();
$stream->closeStdin();
self::assertSame('', fread($this->sockets[1], 20));
self::assertTrue(feof($this->sockets[1]));
fwrite($this->sockets[1], pack('CxxxN', 1, 6).'result');
fclose($this->sockets[1]);
$output = '';
$stream->onStdout(static function (string $chunk) use (&$output): void { $output .= $chunk; });
$stream->wait();
self::assertSame('result', $output);
self::assertTrue($stream->isFinished());
self::assertFalse($stream->poll());
}
public function testPartialWritesAndBackpressureAreBounded(): void
{
$stream = $this->open();
$payload = str_repeat('x', 1048576);
$offset = 0;
$started = hrtime(true);
do {
$written = $stream->writeStdin(substr($payload, $offset));
self::assertLessThanOrEqual(16384, $written);
$offset += $written;
} while ($written > 0 && $offset < \strlen($payload));
self::assertSame(0, $written, 'The peer must eventually apply backpressure.');
self::assertLessThan(500000000, hrtime(true) - $started, 'writeStdin blocked.');
self::assertLessThan(\strlen($payload), $offset);
$received = '';
while ($offset < \strlen($payload)) {
$received .= stream_get_contents($this->sockets[1]);
$offset += $stream->writeStdin(substr($payload, $offset));
}
$received .= stream_get_contents($this->sockets[1]);
self::assertSame($payload, $received);
}
public function testFragmentedHeadersPayloadsAndEmptyFrames(): void
{
$stream = $this->open();
$stdout = $stderr = '';
$stream->onStdout(static function (string $chunk) use (&$stdout): void { $stdout .= $chunk; });
$stream->onStderr(static function (string $chunk) use (&$stderr): void { $stderr .= $chunk; });
$wire = pack('CxxxN', 1, 0).pack('CxxxN', 1, 5).'hello'.pack('CxxxN', 2, 5).'error'.pack('CxxxN', 0, 4).'echo';
foreach (str_split($wire, 3) as $chunk) {
fwrite($this->sockets[1], $chunk);
self::assertTrue($stream->poll());
}
fclose($this->sockets[1]);
$stream->wait();
self::assertSame('helloecho', $stdout);
self::assertSame('error', $stderr);
}
public function testTtyBytesRemainUnchanged(): void
{
$stream = $this->open(false);
$output = '';
$stream->onStdout(static function (string $chunk) use (&$output): void { $output .= $chunk; });
$stream->onStderr(static function (): void { self::fail('TTY output has no separate stderr.'); });
$bytes = "\0\xffhello\r\n";
fwrite($this->sockets[1], $bytes);
fclose($this->sockets[1]);
$stream->wait();
self::assertSame($bytes, $output);
}
public function testIdlePollIsBoundedAndTicksReachDeadline(): void
{
$stream = $this->open(true, 160);
$started = hrtime(true);
self::assertTrue($stream->poll(30));
self::assertGreaterThan(20000000, hrtime(true) - $started);
$ticks = 0;
try {
$stream->wait(static function () use (&$ticks): void { ++$ticks; }, 30);
self::fail('An idle session exceeded its deadline.');
} catch (InteractiveExecTimeoutException $expected) {
self::assertGreaterThanOrEqual(3, $ticks);
self::assertLessThan(500000000, hrtime(true) - $started);
self::assertFalse($stream->isFinished(), 'A timeout is not successful completion.');
}
}
public static function invalidFrames(): array
{
return [
'partial header' => ["\x01\x00"],
'partial payload' => [pack('CxxxN', 1, 5).'ab'],
'invalid channel' => [pack('CxxxN', 9, 0)],
'invalid padding' => ["\x01\x01\x00\x00\x00\x00\x00\x00"],
'system error frame' => [pack('CxxxN', 3, 0)],
];
}
#[DataProvider('invalidFrames')]
public function testInvalidOrTruncatedFramesFail(string $wire): void
{
$stream = $this->open();
fwrite($this->sockets[1], $wire);
fclose($this->sockets[1]);
$this->expectException(\RuntimeException::class);
$stream->wait();
}
public function testHugeFrameDoesNotAllocateItsDeclaredSize(): void
{
$stream = $this->open();
// Measure used bytes, not allocator pages (which grow in 2 MiB steps),
// and exclude PHPUnit's own assertion/autoload allocations.
$before = memory_get_usage();
fwrite($this->sockets[1], pack('CxxxN', 1, 4294967295).'x');
$open = $stream->poll();
$used = memory_get_usage() - $before;
self::assertTrue($open);
self::assertLessThan(1048576, $used);
}
public function testCallbackFailureClosesConnection(): void
{
$stream = $this->open();
$error = new \RuntimeException('callback failed');
$stream->onStdout(static function () use ($error): void { throw $error; });
fwrite($this->sockets[1], pack('CxxxN', 1, 1).'x');
try {
$stream->poll();
self::fail('Callback error was swallowed.');
} catch (\RuntimeException $caught) {
self::assertSame($error, $caught);
self::assertSame('', fread($this->sockets[1], 1));
self::assertTrue(feof($this->sockets[1]));
}
}
public function testHeartbeatFailureClosesConnection(): void
{
$stream = $this->open();
$this->expectExceptionMessage('lock lost');
try {
$stream->wait(static function (): void { throw new \RuntimeException('lock lost'); });
} finally {
self::assertSame('', fread($this->sockets[1], 1));
self::assertTrue(feof($this->sockets[1]));
}
}
public function testWriteAfterHalfCloseFails(): void
{
$stream = $this->open();
$stream->closeStdin();
$this->expectException(\LogicException::class);
$stream->writeStdin('x');
}
public function testTtyHalfCloseIsRejectedWithoutLosingTheConnection(): void
{
$stream = $this->open(false);
try {
$stream->closeStdin();
self::fail('Unsafe TTY half-close was accepted.');
} catch (\LogicException $expected) {
self::assertSame(1, $stream->writeStdin('x'));
}
}
public function testExplicitCloseIsNotSuccessfulEof(): void
{
$stream = $this->open();
$stream->close();
$stream->close();
self::assertFalse($stream->isFinished());
$this->expectException(\LogicException::class);
$stream->poll();
}
public function testWriteFailureClosesConnection(): void
{
$stream = $this->open();
fclose($this->sockets[1]);
$this->expectException(\RuntimeException::class);
$stream->writeStdin('x');
}
public function testInvalidPollTimeoutIsRejected(): void
{
$stream = $this->open();
$this->expectException(\InvalidArgumentException::class);
$stream->poll(60001);
}
}