1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file This file is part of the PdfParser library. |
5
|
|
|
* |
6
|
|
|
* @author Konrad Abicht <[email protected]> |
7
|
|
|
* |
8
|
|
|
* @date 2020-06-01 |
9
|
|
|
* |
10
|
|
|
* @author Sébastien MALOT <[email protected]> |
11
|
|
|
* |
12
|
|
|
* @date 2017-01-03 |
13
|
|
|
* |
14
|
|
|
* @license LGPLv3 |
15
|
|
|
* |
16
|
|
|
* @url <https://github.com/smalot/pdfparser> |
17
|
|
|
* |
18
|
|
|
* PdfParser is a pdf library written in PHP, extraction oriented. |
19
|
|
|
* Copyright (C) 2017 - Sébastien MALOT <[email protected]> |
20
|
|
|
* |
21
|
|
|
* This program is free software: you can redistribute it and/or modify |
22
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
23
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
24
|
|
|
* (at your option) any later version. |
25
|
|
|
* |
26
|
|
|
* This program is distributed in the hope that it will be useful, |
27
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
28
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
29
|
|
|
* GNU Lesser General Public License for more details. |
30
|
|
|
* |
31
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
32
|
|
|
* along with this program. |
33
|
|
|
* If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>. |
34
|
|
|
*/ |
35
|
|
|
|
36
|
|
|
namespace PHPUnitTests\Integration\RawData; |
37
|
|
|
|
38
|
|
|
use PHPUnitTests\TestCase; |
39
|
|
|
use Smalot\PdfParser\Parser; |
40
|
|
|
use Smalot\PdfParser\RawData\FilterHelper; |
41
|
|
|
|
42
|
|
|
class FilterHelperTest extends TestCase |
43
|
|
|
{ |
44
|
|
|
protected function setUp(): void |
45
|
|
|
{ |
46
|
|
|
parent::setUp(); |
47
|
|
|
|
48
|
|
|
$this->fixture = new FilterHelper(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/* |
52
|
|
|
* Tests for filter ASCII85Decode |
53
|
|
|
*/ |
54
|
|
|
|
55
|
|
|
public function testDecodeFilterASCII85Decode(): void |
56
|
|
|
{ |
57
|
|
|
$compressed = '6Z6g\Eb0<5ARlp)FE2)5B)'; // = Compressed string |
58
|
|
|
$result = $this->fixture->decodeFilter('ASCII85Decode', $compressed); |
59
|
|
|
|
60
|
|
|
$this->assertEquals('Compressed string', $result); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testDecodeFilterASCII85DecodeInitSequence(): void |
64
|
|
|
{ |
65
|
|
|
$compressed = '<~6Z6g\Eb0<5ARlp)FE2)5B)'; // = Compressed string |
66
|
|
|
$result = $this->fixture->decodeFilter('ASCII85Decode', $compressed); |
67
|
|
|
|
68
|
|
|
$this->assertEquals('Compressed string', $result); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testDecodeFilterASCII85DecodeEndSequence(): void |
72
|
|
|
{ |
73
|
|
|
$compressed = '6Z6g\Eb0<5ARlp)FE2)5B)~>'; // = Compressed string |
74
|
|
|
$result = $this->fixture->decodeFilter('ASCII85Decode', $compressed); |
75
|
|
|
|
76
|
|
|
$this->assertEquals('Compressed string', $result); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testDecodeFilterASCII85DecodeSpecificEndSequence(): void |
80
|
|
|
{ |
81
|
|
|
$compressed = '+^6b<~>'; // = 0x215B33C0 = "![3\xC0" |
82
|
|
|
$result = $this->fixture->decodeFilter('ASCII85Decode', $compressed); |
83
|
|
|
|
84
|
|
|
$this->assertEquals("\x21\x5B\x33\xC0", $result); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/* |
88
|
|
|
* Tests for filter ASCIIHexDecode |
89
|
|
|
*/ |
90
|
|
|
|
91
|
|
|
public function testDecodeFilterASCIIHexDecode(): void |
92
|
|
|
{ |
93
|
|
|
$compressed = '43 6f 6d 70 72 65 73 73 65 64 20 73 74 72 69 6e 67'; // = Compressed string |
94
|
|
|
$result = $this->fixture->decodeFilter('ASCIIHexDecode', $compressed); |
95
|
|
|
|
96
|
|
|
$this->assertEquals('Compressed string', $result); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/* |
100
|
|
|
* Tests for filter FlateDecode |
101
|
|
|
*/ |
102
|
|
|
|
103
|
|
|
public function testDecodeFilterFlateDecode(): void |
104
|
|
|
{ |
105
|
|
|
$compressed = gzcompress('Compress me', 9); |
106
|
|
|
$result = $this->fixture->decodeFilter('FlateDecode', $compressed); |
107
|
|
|
|
108
|
|
|
$this->assertEquals('Compress me', $result); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* How does function behave if an empty string was given. |
113
|
|
|
*/ |
114
|
|
|
public function testDecodeFilterFlateDecodeEmptyString(): void |
115
|
|
|
{ |
116
|
|
|
$this->expectException(\Exception::class); |
117
|
|
|
$this->expectExceptionMessage('decodeFilterFlateDecode: invalid data'); |
118
|
|
|
|
119
|
|
|
$this->fixture->decodeFilter('FlateDecode', ''); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* How does function behave if an uncompressed string was given. |
124
|
|
|
*/ |
125
|
|
|
public function testDecodeFilterFlateDecodeUncompressedString(): void |
126
|
|
|
{ |
127
|
|
|
$this->expectException(\Exception::class); |
128
|
|
|
$this->expectExceptionMessage('decodeFilterFlateDecode: invalid data'); |
129
|
|
|
|
130
|
|
|
$this->fixture->decodeFilter('FlateDecode', 'something'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* How does function behave if compression checksum is CRC32 instead of Adler-32. |
135
|
|
|
* See: https://github.com/smalot/pdfparser/issues/592 |
136
|
|
|
*/ |
137
|
|
|
public function testDecodeFilterFlateDecodeCRC32Checksum(): void |
138
|
|
|
{ |
139
|
|
|
$document = (new Parser())->parseFile($this->rootDir.'/samples/bugs/Issue592.pdf'); |
140
|
|
|
|
141
|
|
|
self::assertStringContainsString('Two Westbrook Corporate Center Suite 500', $document->getText()); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* How does function behave if an unknown filter name was given. |
146
|
|
|
*/ |
147
|
|
|
public function testDecodeFilterUnknownFilter(): void |
148
|
|
|
{ |
149
|
|
|
$result = $this->fixture->decodeFilter('a string '.rand(), 'something'); |
150
|
|
|
$this->assertEquals('something', $result); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/* |
154
|
|
|
* Test for filters not being implemented yet. |
155
|
|
|
*/ |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* CCITTFaxDecode |
159
|
|
|
*/ |
160
|
|
|
public function testDecodeFilterCCITTFaxDecode(): void |
161
|
|
|
{ |
162
|
|
|
$this->expectException(\Exception::class); |
163
|
|
|
$this->expectExceptionMessage('Decode CCITTFaxDecode not implemented yet.'); |
164
|
|
|
|
165
|
|
|
$this->fixture->decodeFilter('CCITTFaxDecode', ''); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Crypt |
170
|
|
|
*/ |
171
|
|
|
public function testDecodeFilterCrypt(): void |
172
|
|
|
{ |
173
|
|
|
$this->expectException(\Exception::class); |
174
|
|
|
$this->expectExceptionMessage('Decode Crypt not implemented yet.'); |
175
|
|
|
|
176
|
|
|
$this->fixture->decodeFilter('Crypt', ''); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* DCTDecode |
181
|
|
|
*/ |
182
|
|
|
public function testDecodeFilterDCTDecode(): void |
183
|
|
|
{ |
184
|
|
|
$this->expectException(\Exception::class); |
185
|
|
|
$this->expectExceptionMessage('Decode DCTDecode not implemented yet.'); |
186
|
|
|
|
187
|
|
|
$this->fixture->decodeFilter('DCTDecode', ''); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* JBIG2Decode |
192
|
|
|
*/ |
193
|
|
|
public function testDecodeFilterJBIG2Decode(): void |
194
|
|
|
{ |
195
|
|
|
$this->expectException(\Exception::class); |
196
|
|
|
$this->expectExceptionMessage('Decode JBIG2Decode not implemented yet.'); |
197
|
|
|
|
198
|
|
|
$this->fixture->decodeFilter('JBIG2Decode', ''); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* JPXDecode |
203
|
|
|
*/ |
204
|
|
|
public function testDecodeFilterJPXDecode(): void |
205
|
|
|
{ |
206
|
|
|
$this->expectException(\Exception::class); |
207
|
|
|
$this->expectExceptionMessage('Decode JPXDecode not implemented yet.'); |
208
|
|
|
|
209
|
|
|
$this->fixture->decodeFilter('JPXDecode', ''); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|