Remove trailing whitespaces in core.
[pdfium.git] / core / src / fxcodec / codec / fx_codec_jpx_unittest.cpp
1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <stdint.h>
6
7 #include <limits>
8
9 #include "codec_int.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "../../../testing/fx_string_testhelpers.h"
12
13 static const OPJ_OFF_T kSkipError = static_cast<OPJ_OFF_T>(-1);
14 static const OPJ_SIZE_T kReadError = static_cast<OPJ_SIZE_T>(-1);
15 static const OPJ_SIZE_T kWriteError = static_cast<OPJ_SIZE_T>(-1);
16
17 static unsigned char stream_data[] = {
18     0x00, 0x01, 0x02, 0x03,
19     0x84, 0x85, 0x86, 0x87,  // Include some hi-bytes, too.
20 };
21
22 TEST(fxcodec, DecodeDataNullDecodeData) {
23     unsigned char buffer[16];
24     DecodeData* ptr = nullptr;
25
26     // Error codes, not segvs, should callers pass us a NULL pointer.
27     EXPECT_EQ(kReadError, opj_read_from_memory(buffer, sizeof(buffer), ptr));
28     EXPECT_EQ(kWriteError, opj_write_from_memory(buffer, sizeof(buffer), ptr));
29     EXPECT_EQ(kSkipError, opj_skip_from_memory(1, ptr));
30     EXPECT_FALSE(opj_seek_from_memory(1, ptr));
31 }
32
33 TEST(fxcodec, DecodeDataNullStream) {
34     DecodeData dd(nullptr, 0);
35     unsigned char buffer[16];
36
37     // Reads of size 0 do nothing but return an error code.
38     memset(buffer, 0xbd, sizeof(buffer));
39     EXPECT_EQ(kReadError, opj_read_from_memory(buffer, 0, &dd));
40     EXPECT_EQ(0xbd, buffer[0]);
41
42     // Reads of nonzero size do nothing but return an error code.
43     memset(buffer, 0xbd, sizeof(buffer));
44     EXPECT_EQ(kReadError, opj_read_from_memory(buffer, sizeof(buffer), &dd));
45     EXPECT_EQ(0xbd, buffer[0]);
46
47     // writes of size 0 do nothing but return an error code.
48     EXPECT_EQ(kWriteError, opj_write_from_memory(buffer, 0, &dd));
49
50     // writes of nonzero size do nothing but return an error code.
51     EXPECT_EQ(kWriteError, opj_write_from_memory(buffer, sizeof(buffer), &dd));
52
53     // Skips of size 0 always return an error code.
54     EXPECT_EQ(kSkipError, opj_skip_from_memory(0, &dd));
55
56     // Skips of nonzero size always return an error code.
57     EXPECT_EQ(kSkipError, opj_skip_from_memory(1, &dd));
58
59     // Seeks to 0 offset return in error.
60     EXPECT_FALSE(opj_seek_from_memory(0, &dd));
61
62     // Seeks to non-zero offsets return in error.
63     EXPECT_FALSE(opj_seek_from_memory(1, &dd));
64 }
65
66 TEST(fxcodec, DecodeDataZeroSize) {
67     DecodeData dd(stream_data, 0);
68     unsigned char buffer[16];
69
70     // Reads of size 0 do nothing but return an error code.
71     memset(buffer, 0xbd, sizeof(buffer));
72     EXPECT_EQ(kReadError, opj_read_from_memory(buffer, 0, &dd));
73     EXPECT_EQ(0xbd, buffer[0]);
74
75     // Reads of nonzero size do nothing but return an error code.
76     memset(buffer, 0xbd, sizeof(buffer));
77     EXPECT_EQ(kReadError, opj_read_from_memory(buffer, sizeof(buffer), &dd));
78     EXPECT_EQ(0xbd, buffer[0]);
79
80     // writes of size 0 do nothing but return an error code.
81     EXPECT_EQ(kWriteError, opj_write_from_memory(buffer, 0, &dd));
82
83     // writes of nonzero size do nothing but return an error code.
84     EXPECT_EQ(kWriteError, opj_write_from_memory(buffer, sizeof(buffer), &dd));
85
86     // Skips of size 0 always return an error code.
87     EXPECT_EQ(kSkipError, opj_skip_from_memory(0, &dd));
88
89     // Skips of nonzero size always return an error code.
90     EXPECT_EQ(kSkipError, opj_skip_from_memory(1, &dd));
91
92     // Seeks to 0 offset return in error.
93     EXPECT_FALSE(opj_seek_from_memory(0, &dd));
94
95     // Seeks to non-zero offsets return in error.
96     EXPECT_FALSE(opj_seek_from_memory(1, &dd));
97 }
98
99 TEST(fxcodec, DecodeDataReadInBounds) {
100     unsigned char buffer[16];
101     {
102         DecodeData dd(stream_data, sizeof(stream_data));
103
104         // Exact sized read in a single call.
105         memset(buffer, 0xbd, sizeof(buffer));
106         EXPECT_EQ(8u, opj_read_from_memory(buffer, sizeof(buffer), &dd));
107         EXPECT_EQ(0x00, buffer[0]);
108         EXPECT_EQ(0x01, buffer[1]);
109         EXPECT_EQ(0x02, buffer[2]);
110         EXPECT_EQ(0x03, buffer[3]);
111         EXPECT_EQ(0x84, buffer[4]);
112         EXPECT_EQ(0x85, buffer[5]);
113         EXPECT_EQ(0x86, buffer[6]);
114         EXPECT_EQ(0x87, buffer[7]);
115         EXPECT_EQ(0xbd, buffer[8]);
116     }
117     {
118         DecodeData dd(stream_data, sizeof(stream_data));
119
120         // Simple read.
121         memset(buffer, 0xbd, sizeof(buffer));
122         EXPECT_EQ(2u, opj_read_from_memory(buffer, 2, &dd));
123         EXPECT_EQ(0x00, buffer[0]);
124         EXPECT_EQ(0x01, buffer[1]);
125         EXPECT_EQ(0xbd, buffer[2]);
126
127         // Read of size 0 doesn't affect things.
128         memset(buffer, 0xbd, sizeof(buffer));
129         EXPECT_EQ(0u, opj_read_from_memory(buffer, 0, &dd));
130         EXPECT_EQ(0xbd, buffer[0]);
131
132         // Read exactly up to end of data.
133         memset(buffer, 0xbd, sizeof(buffer));
134         EXPECT_EQ(6u, opj_read_from_memory(buffer, 6, &dd));
135         EXPECT_EQ(0x02, buffer[0]);
136         EXPECT_EQ(0x03, buffer[1]);
137         EXPECT_EQ(0x84, buffer[2]);
138         EXPECT_EQ(0x85, buffer[3]);
139         EXPECT_EQ(0x86, buffer[4]);
140         EXPECT_EQ(0x87, buffer[5]);
141         EXPECT_EQ(0xbd, buffer[6]);
142
143         // Read of size 0 at EOF is still an error.
144         memset(buffer, 0xbd, sizeof(buffer));
145         EXPECT_EQ(kReadError, opj_read_from_memory(buffer, 0, &dd));
146         EXPECT_EQ(0xbd, buffer[0]);
147     }
148 }
149
150 TEST(fxcodec, DecodeDataReadBeyondBounds) {
151     unsigned char buffer[16];
152     {
153         DecodeData dd(stream_data, sizeof(stream_data));
154
155         // Read beyond bounds in a single step.
156         memset(buffer, 0xbd, sizeof(buffer));
157         EXPECT_EQ(8u, opj_read_from_memory(buffer, sizeof(buffer) + 1, &dd));
158         EXPECT_EQ(0x00, buffer[0]);
159         EXPECT_EQ(0x01, buffer[1]);
160         EXPECT_EQ(0x02, buffer[2]);
161         EXPECT_EQ(0x03, buffer[3]);
162         EXPECT_EQ(0x84, buffer[4]);
163         EXPECT_EQ(0x85, buffer[5]);
164         EXPECT_EQ(0x86, buffer[6]);
165         EXPECT_EQ(0x87, buffer[7]);
166         EXPECT_EQ(0xbd, buffer[8]);
167     }
168     {
169         DecodeData dd(stream_data, sizeof(stream_data));
170
171         // Read well beyond bounds in a single step.
172         memset(buffer, 0xbd, sizeof(buffer));
173         EXPECT_EQ(8u, opj_read_from_memory(
174             buffer, std::numeric_limits<OPJ_SIZE_T>::max(), &dd));
175         EXPECT_EQ(0x00, buffer[0]);
176         EXPECT_EQ(0x01, buffer[1]);
177         EXPECT_EQ(0x02, buffer[2]);
178         EXPECT_EQ(0x03, buffer[3]);
179         EXPECT_EQ(0x84, buffer[4]);
180         EXPECT_EQ(0x85, buffer[5]);
181         EXPECT_EQ(0x86, buffer[6]);
182         EXPECT_EQ(0x87, buffer[7]);
183         EXPECT_EQ(0xbd, buffer[8]);
184     }
185     {
186         DecodeData dd(stream_data, sizeof(stream_data));
187
188         // Read of size 6 gets first 6 bytes.
189         // rest of buffer intact.
190         memset(buffer, 0xbd, sizeof(buffer));
191         EXPECT_EQ(6u, opj_read_from_memory(buffer, 6, &dd));
192         EXPECT_EQ(0x00, buffer[0]);
193         EXPECT_EQ(0x01, buffer[1]);
194         EXPECT_EQ(0x02, buffer[2]);
195         EXPECT_EQ(0x03, buffer[3]);
196         EXPECT_EQ(0x84, buffer[4]);
197         EXPECT_EQ(0x85, buffer[5]);
198         EXPECT_EQ(0xbd, buffer[6]);
199
200         // Read of size 6 gets remaining two bytes.
201         memset(buffer, 0xbd, sizeof(buffer));
202         EXPECT_EQ(2u, opj_read_from_memory(buffer, 6, &dd));
203         EXPECT_EQ(0x86, buffer[0]);
204         EXPECT_EQ(0x87, buffer[1]);
205         EXPECT_EQ(0xbd, buffer[2]);
206
207         // Read of 6 more gets nothing and leaves rest of buffer intact.
208         memset(buffer, 0xbd, sizeof(buffer));
209         EXPECT_EQ(kReadError, opj_read_from_memory(buffer, 6, &dd));
210         EXPECT_EQ(0xbd, buffer[0]);
211     }
212 }
213
214 TEST(fxcodec, DecodeDataWriteInBounds) {
215     unsigned char stream[16];
216     static unsigned char buffer_data[] = {
217         0x00, 0x01, 0x02, 0x03, 0x80,
218         0x80, 0x81, 0x82, 0x83, 0x84,
219     };
220     {
221         // Pretend the stream can only hold 4 bytes.
222         DecodeData dd(stream, 4);
223
224         memset(stream, 0xbd, sizeof(stream));
225         EXPECT_EQ(4u, opj_write_from_memory(buffer_data, 4, &dd));
226         EXPECT_EQ(0x00, stream[0]);
227         EXPECT_EQ(0x01, stream[1]);
228         EXPECT_EQ(0x02, stream[2]);
229         EXPECT_EQ(0x03, stream[3]);
230         EXPECT_EQ(0xbd, stream[4]);
231     }
232     {
233         // Pretend the stream can only hold 4 bytes.
234         DecodeData dd(stream, 4);
235
236         memset(stream, 0xbd, sizeof(stream));
237         EXPECT_EQ(2u, opj_write_from_memory(buffer_data, 2, &dd));
238         EXPECT_EQ(2u, opj_write_from_memory(buffer_data, 2, &dd));
239         EXPECT_EQ(0x00, stream[0]);
240         EXPECT_EQ(0x01, stream[1]);
241         EXPECT_EQ(0x00, stream[2]);
242         EXPECT_EQ(0x01, stream[3]);
243         EXPECT_EQ(0xbd, stream[4]);
244     }
245 }
246
247 TEST(fxcodec, DecodeDataWriteBeyondBounds) {
248     unsigned char stream[16];
249     static unsigned char buffer_data[] = {
250         0x10, 0x11, 0x12, 0x13,
251         0x94, 0x95, 0x96, 0x97,
252     };
253     {
254         // Pretend the stream can only hold 4 bytes.
255         DecodeData dd(stream, 4);
256
257         // Write ending past EOF transfers up til EOF.
258         memset(stream, 0xbd, sizeof(stream));
259         EXPECT_EQ(4u, opj_write_from_memory(buffer_data, 5, &dd));
260         EXPECT_EQ(0x10, stream[0]);
261         EXPECT_EQ(0x11, stream[1]);
262         EXPECT_EQ(0x12, stream[2]);
263         EXPECT_EQ(0x13, stream[3]);
264         EXPECT_EQ(0xbd, stream[4]);
265
266         // Subsequent writes fail.
267         memset(stream, 0xbd, sizeof(stream));
268         EXPECT_EQ(kWriteError, opj_write_from_memory(buffer_data, 5, &dd));
269         EXPECT_EQ(0xbd, stream[0]);
270     }
271     {
272         // Pretend the stream can only hold 4 bytes.
273         DecodeData dd(stream, 4);
274
275         // Write ending past EOF (two steps) transfers up til EOF.
276         memset(stream, 0xbd, sizeof(stream));
277         EXPECT_EQ(2u, opj_write_from_memory(buffer_data, 2, &dd));
278         EXPECT_EQ(2u, opj_write_from_memory(buffer_data, 4, &dd));
279         EXPECT_EQ(0x10, stream[0]);
280         EXPECT_EQ(0x11, stream[1]);
281         EXPECT_EQ(0x10, stream[2]);
282         EXPECT_EQ(0x11, stream[3]);
283         EXPECT_EQ(0xbd, stream[4]);
284
285         // Subsequent writes fail.
286         memset(stream, 0xbd, sizeof(stream));
287         EXPECT_EQ(kWriteError, opj_write_from_memory(buffer_data, 5, &dd));
288         EXPECT_EQ(0xbd, stream[0]);
289     }
290 }
291
292 // Note: Some care needs to be taken here because the skip/seek functions
293 // take OPJ_OFF_T's as arguments, which are typically a signed type.
294 TEST(fxcodec, DecodeDataSkip) {
295     unsigned char buffer[16];
296     {
297         DecodeData dd(stream_data, sizeof(stream_data));
298
299         // Skiping within buffer is allowed.
300         memset(buffer, 0xbd, sizeof(buffer));
301         EXPECT_EQ(1, opj_skip_from_memory(1, &dd));
302         EXPECT_EQ(1u, opj_read_from_memory(buffer, 1, &dd));
303         EXPECT_EQ(0x01, buffer[0]);
304         EXPECT_EQ(0xbd, buffer[1]);
305
306         // Skiping 0 bytes changes nothing.
307         memset(buffer, 0xbd, sizeof(buffer));
308         EXPECT_EQ(0, opj_skip_from_memory(0, &dd));
309         EXPECT_EQ(1u, opj_read_from_memory(buffer, 1, &dd));
310         EXPECT_EQ(0x02, buffer[0]);
311         EXPECT_EQ(0xbd, buffer[1]);
312
313         // Skiping to EOS-1 is possible.
314         memset(buffer, 0xbd, sizeof(buffer));
315         EXPECT_EQ(4, opj_skip_from_memory(4, &dd));
316         EXPECT_EQ(1u, opj_read_from_memory(buffer, 1, &dd));
317         EXPECT_EQ(0x87, buffer[0]);
318         EXPECT_EQ(0xbd, buffer[1]);
319
320         // Next read fails.
321         memset(buffer, 0xbd, sizeof(buffer));
322         EXPECT_EQ(kReadError, opj_read_from_memory(buffer, 1, &dd));
323         EXPECT_EQ(0xbd, buffer[0]);
324     }
325     {
326         DecodeData dd(stream_data, sizeof(stream_data));
327
328         // Skiping directly to EOS is allowed.
329         memset(buffer, 0xbd, sizeof(buffer));
330         EXPECT_EQ(8, opj_skip_from_memory(8, &dd));
331
332         // Next read fails.
333         EXPECT_EQ(kReadError, opj_read_from_memory(buffer, 1, &dd));
334         EXPECT_EQ(0xbd, buffer[0]);
335     }
336     {
337         DecodeData dd(stream_data, sizeof(stream_data));
338
339         // Skipping beyond end of stream is allowed and returns full distance.
340         memset(buffer, 0xbd, sizeof(buffer));
341         EXPECT_EQ(9, opj_skip_from_memory(9, &dd));
342
343         // Next read fails.
344         EXPECT_EQ(kReadError, opj_read_from_memory(buffer, 1, &dd));
345         EXPECT_EQ(0xbd, buffer[0]);
346     }
347     {
348         DecodeData dd(stream_data, sizeof(stream_data));
349
350         // Skipping way beyond EOS is allowd, doesn't wrap, and returns
351         // full distance.
352         memset(buffer, 0xbd, sizeof(buffer));
353         EXPECT_EQ(4, opj_skip_from_memory(4, &dd));
354         EXPECT_EQ(std::numeric_limits<OPJ_OFF_T>::max(), opj_skip_from_memory(
355             std::numeric_limits<OPJ_OFF_T>::max(), &dd));
356
357         // Next read fails. If it succeeds, it may mean we wrapped.
358         EXPECT_EQ(kReadError, opj_read_from_memory(buffer, 1, &dd));
359         EXPECT_EQ(0xbd, buffer[0]);
360     }
361     {
362         DecodeData dd(stream_data, sizeof(stream_data));
363
364         // Negative skip within buffer not is allowed, position unchanged.
365         memset(buffer, 0xbd, sizeof(buffer));
366         EXPECT_EQ(4, opj_skip_from_memory(4, &dd));
367         EXPECT_EQ(kSkipError, opj_skip_from_memory(-2, &dd));
368
369         // Next read succeeds as if nothing has happenned.
370         EXPECT_EQ(1u, opj_read_from_memory(buffer, 1, &dd));
371         EXPECT_EQ(0x84, buffer[0]);
372         EXPECT_EQ(0xbd, buffer[1]);
373
374         // Negative skip before buffer is not allowed, position unchanged.
375         memset(buffer, 0xbd, sizeof(buffer));
376         EXPECT_EQ(kSkipError, opj_skip_from_memory(-4, &dd));
377
378         // Next read succeeds as if nothing has happenned.
379         EXPECT_EQ(1u, opj_read_from_memory(buffer, 1, &dd));
380         EXPECT_EQ(0x85, buffer[0]);
381         EXPECT_EQ(0xbd, buffer[1]);
382     }
383     {
384         DecodeData dd(stream_data, sizeof(stream_data));
385
386         // Negative skip way before buffer is not allowed, doesn't wrap
387         memset(buffer, 0xbd, sizeof(buffer));
388         EXPECT_EQ(4, opj_skip_from_memory(4, &dd));
389         EXPECT_EQ(kSkipError, opj_skip_from_memory(
390             std::numeric_limits<OPJ_OFF_T>::min(), &dd));
391
392         // Next read succeeds. If it fails, it may mean we wrapped.
393         EXPECT_EQ(1, opj_read_from_memory(buffer, 1, &dd));
394         EXPECT_EQ(0x84, buffer[0]);
395         EXPECT_EQ(0xbd, buffer[1]);
396     }
397     {
398         DecodeData dd(stream_data, sizeof(stream_data));
399
400         // Negative skip after EOS isn't alowed, still EOS.
401         memset(buffer, 0xbd, sizeof(buffer));
402         EXPECT_EQ(8, opj_skip_from_memory(8, &dd));
403         EXPECT_EQ(kSkipError, opj_skip_from_memory(-4, &dd));
404
405         // Next read fails.
406         EXPECT_EQ(kReadError, opj_read_from_memory(buffer, 1, &dd));
407         EXPECT_EQ(0xbd, buffer[0]);
408     }
409
410 }
411
412 TEST(fxcodec, DecodeDataSeek) {
413     unsigned char buffer[16];
414     DecodeData dd(stream_data, sizeof(stream_data));
415
416     // Seeking within buffer is allowed and read succeeds
417     memset(buffer, 0xbd, sizeof(buffer));
418     EXPECT_TRUE(opj_seek_from_memory(1, &dd));
419     EXPECT_EQ(1u, opj_read_from_memory(buffer, 1, &dd));
420     EXPECT_EQ(0x01, buffer[0]);
421     EXPECT_EQ(0xbd, buffer[1]);
422
423     // Seeking before start returns error leaving position unchanged.
424     memset(buffer, 0xbd, sizeof(buffer));
425     EXPECT_FALSE(opj_seek_from_memory(-1, &dd));
426     EXPECT_EQ(1, opj_read_from_memory(buffer, 1, &dd));
427     EXPECT_EQ(0x02, buffer[0]);
428     EXPECT_EQ(0xbd, buffer[1]);
429
430     // Seeking way before start returns error leaving position unchanged.
431     memset(buffer, 0xbd, sizeof(buffer));
432     EXPECT_FALSE(opj_seek_from_memory(
433         std::numeric_limits<OPJ_OFF_T>::min(), &dd));
434     EXPECT_EQ(1, opj_read_from_memory(buffer, 1, &dd));
435     EXPECT_EQ(0x03, buffer[0]);
436     EXPECT_EQ(0xbd, buffer[1]);
437
438     // Seeking exactly to EOS is allowed but read fails.
439     memset(buffer, 0xbd, sizeof(buffer));
440     EXPECT_TRUE(opj_seek_from_memory(8, &dd));
441     EXPECT_EQ(kReadError, opj_read_from_memory(buffer, 1, &dd));
442     EXPECT_EQ(0xbd, buffer[0]);
443
444     // Seeking back to zero offset is allowed and read succeeds.
445     memset(buffer, 0xbd, sizeof(buffer));
446     EXPECT_TRUE(opj_seek_from_memory(0, &dd));
447     EXPECT_EQ(1u, opj_read_from_memory(buffer, 1, &dd));
448     EXPECT_EQ(0x00, buffer[0]);
449     EXPECT_EQ(0xbd, buffer[1]);
450
451     // Seeking beyond end of stream is allowed but read fails.
452     memset(buffer, 0xbd, sizeof(buffer));
453     EXPECT_TRUE(opj_seek_from_memory(16, &dd));
454     EXPECT_EQ(kReadError, opj_read_from_memory(buffer, 1, &dd));
455     EXPECT_EQ(0xbd, buffer[0]);
456
457     // Seeking within buffer after seek past EOF restores good state.
458     memset(buffer, 0xbd, sizeof(buffer));
459     EXPECT_TRUE(opj_seek_from_memory(4, &dd));
460     EXPECT_EQ(1u, opj_read_from_memory(buffer, 1, &dd));
461     EXPECT_EQ(0x84, buffer[0]);
462     EXPECT_EQ(0xbd, buffer[1]);
463
464     // Seeking way beyond EOS is allowed, doesn't wrap, and read fails.
465     memset(buffer, 0xbd, sizeof(buffer));
466     EXPECT_TRUE(opj_seek_from_memory(
467         std::numeric_limits<OPJ_OFF_T>::max(), &dd));
468     EXPECT_EQ(kReadError, opj_read_from_memory(buffer, 1, &dd));
469     EXPECT_EQ(0xbd, buffer[0]);
470 }