-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPngParserTest.php
More file actions
191 lines (149 loc) · 5.91 KB
/
PngParserTest.php
File metadata and controls
191 lines (149 loc) · 5.91 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
<?php
// SPDX-FileCopyrightText: 2026 LibreSign
// SPDX-License-Identifier: AGPL-3.0-or-later
declare(strict_types=1);
namespace LibreSign\XObjectTemplate\Tests\Unit\Pdf\Png;
use LibreSign\XObjectTemplate\Pdf\Png\PngHeaderUnpackerInterface;
use LibreSign\XObjectTemplate\Pdf\Png\PngParser;
use LibreSign\XObjectTemplate\Tests\Support\PngFixtureFactory;
use PHPUnit\Framework\TestCase;
final class PngParserTest extends TestCase
{
public function testParseReturnsStructuredImageData(): void
{
$parser = new PngParser();
$compressed = PngFixtureFactory::compressScanlines("\x00\xff\x00\x00");
$png = PngFixtureFactory::createPngFromCompressedIdatChunks(
width: 1,
height: 1,
colorType: 2,
idatChunks: [$compressed],
);
$parsed = $parser->parse($png);
self::assertSame(1, $parsed->width);
self::assertSame(1, $parsed->height);
self::assertSame(2, $parsed->colorType);
self::assertSame($compressed, $parsed->idat);
}
public function testParseRejectsInvalidSignature(): void
{
$parser = new PngParser();
$png = PngFixtureFactory::createPng(
width: 1,
height: 1,
colorType: 2,
scanlines: "\x00\xff\x00\x00",
);
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid PNG signature.');
$parser->parse('BROKEN!!' . substr($png, 8));
}
public function testParseRejectsTrailingDataAfterIendChunk(): void
{
$parser = new PngParser();
$png = PngFixtureFactory::createPng(
width: 1,
height: 1,
colorType: 2,
scanlines: "\x00\xff\x00\x00",
) . PngFixtureFactory::createChunk('tEXt', 'tail');
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('PNG data after IEND is not supported.');
$parser->parse($png);
}
public function testParseRejectsTrailingBytesAfterIend(): void
{
$parser = new PngParser();
$png = PngFixtureFactory::createPng(
width: 1,
height: 1,
colorType: 2,
scanlines: "\x00\xff\x00\x00",
) . "\x00\x00\x00";
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('PNG data after IEND is not supported.');
$parser->parse($png);
}
public function testReadChunkRejectsTruncatedLengthField(): void
{
$parser = new PngParser();
$offset = 0;
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid PNG chunk length.');
$parser->readChunk("\x00\x00\x00", $offset);
}
public function testParseChunkLengthPreservesAllFourBytes(): void
{
$parser = new PngParser();
self::assertSame(0x01020304, $parser->parseChunkLength("\x01\x02\x03\x04"));
}
public function testParseRejectsMissingMetadataWhenIhdrChunkIsAbsent(): void
{
$parser = new PngParser();
$png = "\x89PNG\r\n\x1a\n"
. PngFixtureFactory::createChunk('IDAT', PngFixtureFactory::compressScanlines("\x00\xff\x00\x00"))
. PngFixtureFactory::createChunk('IEND', '');
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('PNG metadata is incomplete.');
$parser->parse($png);
}
public function testParseRejectsMissingImageDataWhenIdatChunkIsAbsent(): void
{
$parser = new PngParser();
$ihdr = pack('NNCCCCC', 1, 1, 8, 2, 0, 0, 0);
$png = "\x89PNG\r\n\x1a\n"
. PngFixtureFactory::createChunk('IHDR', $ihdr)
. PngFixtureFactory::createChunk('IEND', '');
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('PNG image data is missing.');
$parser->parse($png);
}
public function testReadChunkRejectsInvalidChunkTypeLength(): void
{
$parser = new PngParser();
$offset = 0;
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid PNG chunk type.');
$parser->readChunk(pack('N', 0) . 'ABC', $offset);
}
public function testReadChunkRejectsTruncatedChunkPayload(): void
{
$parser = new PngParser();
$offset = 0;
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('PNG chunk data is truncated.');
$parser->readChunk(pack('N', 1) . 'IHDR', $offset);
}
public function testParseHeaderRejectsUnexpectedHeaderLength(): void
{
$parser = new PngParser();
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Unable to parse the PNG IHDR chunk.');
$parser->parseHeader('short-header');
}
public function testParseHeaderRejectsUnpackFailures(): void
{
$parser = new PngParser(new class implements PngHeaderUnpackerInterface {
public function unpack(string $data): array|false
{
return false;
}
});
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Unable to parse the PNG IHDR chunk.');
$parser->parseHeader(pack('NNCCCCC', 1, 1, 8, 2, 0, 0, 0));
}
public function testParseRejectsMissingTrailerChunkWhenTrailingBytesAreTooShort(): void
{
$parser = new PngParser();
$png = PngFixtureFactory::createPng(
width: 1,
height: 1,
colorType: 2,
scanlines: "\x00\xff\x00\x00",
);
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('PNG trailer chunk is missing.');
$parser->parse(substr($png, 0, -12) . "\x00\x00\x00");
}
}