-
-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathCompactSerializerTest.php
More file actions
52 lines (44 loc) · 1.63 KB
/
Copy pathCompactSerializerTest.php
File metadata and controls
52 lines (44 loc) · 1.63 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
<?php
declare(strict_types=1);
namespace Jose\Tests\Component\Encryption;
use InvalidArgumentException;
use Jose\Component\Encryption\Serializer\CompactSerializer;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use function memory_get_peak_usage;
use function memory_get_usage;
use function memory_reset_peak_usage;
use function str_repeat;
/**
* @internal
*/
final class CompactSerializerTest extends TestCase
{
#[Test]
public function aTokenWithTooManySegmentsIsRejected(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Unsupported input');
(new CompactSerializer())->unserialize('eyJhbGciOiJkaXIiLCJlbmMiOiJBMTI4R0NNIn0.....');
}
/**
* A delimiter-heavy token must be rejected without expanding it into one array entry per delimiter. The
* threshold is deliberately generous: the unbounded split of this input allocates roughly twenty-five times its
* size, while the bounded one stays proportional to it.
*/
#[Test]
public function aDelimiterHeavyTokenIsRejectedWithoutExhaustingMemory(): void
{
$token = str_repeat('.', 2_000_000);
$serializer = new CompactSerializer();
memory_reset_peak_usage();
$before = memory_get_usage();
try {
$serializer->unserialize($token);
static::fail('The token should have been rejected.');
} catch (InvalidArgumentException $e) {
static::assertSame('Unsupported input', $e->getMessage());
}
static::assertLessThan(8_000_000, memory_get_peak_usage() - $before);
}
}