Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(93)

Side by Side Diff: core/fxcodec/codec/fx_codec_jpx_unittest.cpp

Issue 2071693002: Remove openjpeg write support. (Closed) Base URL: https://pdfium.googlesource.com/pdfium@master
Patch Set: clean up struct definition Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « core/fxcodec/codec/fx_codec_jpx_opj.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 PDFium Authors. All rights reserved. 1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stdint.h> 5 #include <stdint.h>
6 6
7 #include <limits> 7 #include <limits>
8 8
9 #include "core/fxcodec/codec/codec_int.h" 9 #include "core/fxcodec/codec/codec_int.h"
10 #include "testing/fx_string_testhelpers.h" 10 #include "testing/fx_string_testhelpers.h"
11 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
12 12
13 static const OPJ_OFF_T kSkipError = static_cast<OPJ_OFF_T>(-1); 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); 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 15
17 static unsigned char stream_data[] = { 16 static const uint8_t stream_data[] = {
18 0x00, 0x01, 0x02, 0x03, 17 0x00, 0x01, 0x02, 0x03,
19 0x84, 0x85, 0x86, 0x87, // Include some hi-bytes, too. 18 0x84, 0x85, 0x86, 0x87, // Include some hi-bytes, too.
20 }; 19 };
21 20
22 union Float_t { 21 union Float_t {
23 Float_t(float num = 0.0f) : f(num) {} 22 Float_t(float num = 0.0f) : f(num) {}
24 23
25 int32_t i; 24 int32_t i;
26 FX_FLOAT f; 25 FX_FLOAT f;
27 }; 26 };
28 27
29 TEST(fxcodec, CMYK_Rounding) { 28 TEST(fxcodec, CMYK_Rounding) {
30 // Testing all floats from 0.0 to 1.0 takes about 35 seconds in release 29 // Testing all floats from 0.0 to 1.0 takes about 35 seconds in release
31 // builds and much longer in debug builds, so just test the known-dangerous 30 // builds and much longer in debug builds, so just test the known-dangerous
32 // range. 31 // range.
33 const FX_FLOAT startValue = 0.001f; 32 const FX_FLOAT startValue = 0.001f;
34 const FX_FLOAT endValue = 0.003f; 33 const FX_FLOAT endValue = 0.003f;
35 FX_FLOAT R = 0.0f, G = 0.0f, B = 0.0f; 34 FX_FLOAT R = 0.0f, G = 0.0f, B = 0.0f;
36 // Iterate through floats by incrementing the representation, as discussed in 35 // Iterate through floats by incrementing the representation, as discussed in
37 // https://randomascii.wordpress.com/2012/01/23/stupid-float-tricks-2/ 36 // https://randomascii.wordpress.com/2012/01/23/stupid-float-tricks-2/
38 for (Float_t f = startValue; f.f < endValue; f.i++) { 37 for (Float_t f = startValue; f.f < endValue; f.i++) {
39 AdobeCMYK_to_sRGB(f.f, f.f, f.f, f.f, R, G, B); 38 AdobeCMYK_to_sRGB(f.f, f.f, f.f, f.f, R, G, B);
40 } 39 }
41 // Check various other 'special' numbers. 40 // Check various other 'special' numbers.
42 AdobeCMYK_to_sRGB(0.0f, 0.25f, 0.5f, 1.0f, R, G, B); 41 AdobeCMYK_to_sRGB(0.0f, 0.25f, 0.5f, 1.0f, R, G, B);
43 } 42 }
44 43
45 TEST(fxcodec, DecodeDataNullDecodeData) { 44 TEST(fxcodec, DecodeDataNullDecodeData) {
46 unsigned char buffer[16]; 45 uint8_t buffer[16];
47 DecodeData* ptr = nullptr; 46 DecodeData* ptr = nullptr;
48 47
49 // Error codes, not segvs, should callers pass us a nullptr pointer. 48 // Error codes, not segvs, should callers pass us a nullptr pointer.
50 EXPECT_EQ(kReadError, opj_read_from_memory(buffer, sizeof(buffer), ptr)); 49 EXPECT_EQ(kReadError, opj_read_from_memory(buffer, sizeof(buffer), ptr));
51 EXPECT_EQ(kWriteError, opj_write_from_memory(buffer, sizeof(buffer), ptr));
52 EXPECT_EQ(kSkipError, opj_skip_from_memory(1, ptr)); 50 EXPECT_EQ(kSkipError, opj_skip_from_memory(1, ptr));
53 EXPECT_FALSE(opj_seek_from_memory(1, ptr)); 51 EXPECT_FALSE(opj_seek_from_memory(1, ptr));
54 } 52 }
55 53
56 TEST(fxcodec, DecodeDataNullStream) { 54 TEST(fxcodec, DecodeDataNullStream) {
57 DecodeData dd(nullptr, 0); 55 DecodeData dd(nullptr, 0);
58 unsigned char buffer[16]; 56 uint8_t buffer[16];
59 57
60 // Reads of size 0 do nothing but return an error code. 58 // Reads of size 0 do nothing but return an error code.
61 memset(buffer, 0xbd, sizeof(buffer)); 59 memset(buffer, 0xbd, sizeof(buffer));
62 EXPECT_EQ(kReadError, opj_read_from_memory(buffer, 0, &dd)); 60 EXPECT_EQ(kReadError, opj_read_from_memory(buffer, 0, &dd));
63 EXPECT_EQ(0xbd, buffer[0]); 61 EXPECT_EQ(0xbd, buffer[0]);
64 62
65 // Reads of nonzero size do nothing but return an error code. 63 // Reads of nonzero size do nothing but return an error code.
66 memset(buffer, 0xbd, sizeof(buffer)); 64 memset(buffer, 0xbd, sizeof(buffer));
67 EXPECT_EQ(kReadError, opj_read_from_memory(buffer, sizeof(buffer), &dd)); 65 EXPECT_EQ(kReadError, opj_read_from_memory(buffer, sizeof(buffer), &dd));
68 EXPECT_EQ(0xbd, buffer[0]); 66 EXPECT_EQ(0xbd, buffer[0]);
69 67
70 // writes of size 0 do nothing but return an error code.
71 EXPECT_EQ(kWriteError, opj_write_from_memory(buffer, 0, &dd));
72
73 // writes of nonzero size do nothing but return an error code.
74 EXPECT_EQ(kWriteError, opj_write_from_memory(buffer, sizeof(buffer), &dd));
75
76 // Skips of size 0 always return an error code. 68 // Skips of size 0 always return an error code.
77 EXPECT_EQ(kSkipError, opj_skip_from_memory(0, &dd)); 69 EXPECT_EQ(kSkipError, opj_skip_from_memory(0, &dd));
78 70
79 // Skips of nonzero size always return an error code. 71 // Skips of nonzero size always return an error code.
80 EXPECT_EQ(kSkipError, opj_skip_from_memory(1, &dd)); 72 EXPECT_EQ(kSkipError, opj_skip_from_memory(1, &dd));
81 73
82 // Seeks to 0 offset return in error. 74 // Seeks to 0 offset return in error.
83 EXPECT_FALSE(opj_seek_from_memory(0, &dd)); 75 EXPECT_FALSE(opj_seek_from_memory(0, &dd));
84 76
85 // Seeks to non-zero offsets return in error. 77 // Seeks to non-zero offsets return in error.
86 EXPECT_FALSE(opj_seek_from_memory(1, &dd)); 78 EXPECT_FALSE(opj_seek_from_memory(1, &dd));
87 } 79 }
88 80
89 TEST(fxcodec, DecodeDataZeroSize) { 81 TEST(fxcodec, DecodeDataZeroSize) {
90 DecodeData dd(stream_data, 0); 82 DecodeData dd(stream_data, 0);
91 unsigned char buffer[16]; 83 uint8_t buffer[16];
92 84
93 // Reads of size 0 do nothing but return an error code. 85 // Reads of size 0 do nothing but return an error code.
94 memset(buffer, 0xbd, sizeof(buffer)); 86 memset(buffer, 0xbd, sizeof(buffer));
95 EXPECT_EQ(kReadError, opj_read_from_memory(buffer, 0, &dd)); 87 EXPECT_EQ(kReadError, opj_read_from_memory(buffer, 0, &dd));
96 EXPECT_EQ(0xbd, buffer[0]); 88 EXPECT_EQ(0xbd, buffer[0]);
97 89
98 // Reads of nonzero size do nothing but return an error code. 90 // Reads of nonzero size do nothing but return an error code.
99 memset(buffer, 0xbd, sizeof(buffer)); 91 memset(buffer, 0xbd, sizeof(buffer));
100 EXPECT_EQ(kReadError, opj_read_from_memory(buffer, sizeof(buffer), &dd)); 92 EXPECT_EQ(kReadError, opj_read_from_memory(buffer, sizeof(buffer), &dd));
101 EXPECT_EQ(0xbd, buffer[0]); 93 EXPECT_EQ(0xbd, buffer[0]);
102 94
103 // writes of size 0 do nothing but return an error code.
104 EXPECT_EQ(kWriteError, opj_write_from_memory(buffer, 0, &dd));
105
106 // writes of nonzero size do nothing but return an error code.
107 EXPECT_EQ(kWriteError, opj_write_from_memory(buffer, sizeof(buffer), &dd));
108
109 // Skips of size 0 always return an error code. 95 // Skips of size 0 always return an error code.
110 EXPECT_EQ(kSkipError, opj_skip_from_memory(0, &dd)); 96 EXPECT_EQ(kSkipError, opj_skip_from_memory(0, &dd));
111 97
112 // Skips of nonzero size always return an error code. 98 // Skips of nonzero size always return an error code.
113 EXPECT_EQ(kSkipError, opj_skip_from_memory(1, &dd)); 99 EXPECT_EQ(kSkipError, opj_skip_from_memory(1, &dd));
114 100
115 // Seeks to 0 offset return in error. 101 // Seeks to 0 offset return in error.
116 EXPECT_FALSE(opj_seek_from_memory(0, &dd)); 102 EXPECT_FALSE(opj_seek_from_memory(0, &dd));
117 103
118 // Seeks to non-zero offsets return in error. 104 // Seeks to non-zero offsets return in error.
119 EXPECT_FALSE(opj_seek_from_memory(1, &dd)); 105 EXPECT_FALSE(opj_seek_from_memory(1, &dd));
120 } 106 }
121 107
122 TEST(fxcodec, DecodeDataReadInBounds) { 108 TEST(fxcodec, DecodeDataReadInBounds) {
123 unsigned char buffer[16]; 109 uint8_t buffer[16];
124 { 110 {
125 DecodeData dd(stream_data, sizeof(stream_data)); 111 DecodeData dd(stream_data, sizeof(stream_data));
126 112
127 // Exact sized read in a single call. 113 // Exact sized read in a single call.
128 memset(buffer, 0xbd, sizeof(buffer)); 114 memset(buffer, 0xbd, sizeof(buffer));
129 EXPECT_EQ(8u, opj_read_from_memory(buffer, sizeof(buffer), &dd)); 115 EXPECT_EQ(8u, opj_read_from_memory(buffer, sizeof(buffer), &dd));
130 EXPECT_EQ(0x00, buffer[0]); 116 EXPECT_EQ(0x00, buffer[0]);
131 EXPECT_EQ(0x01, buffer[1]); 117 EXPECT_EQ(0x01, buffer[1]);
132 EXPECT_EQ(0x02, buffer[2]); 118 EXPECT_EQ(0x02, buffer[2]);
133 EXPECT_EQ(0x03, buffer[3]); 119 EXPECT_EQ(0x03, buffer[3]);
(...skipping 30 matching lines...) Expand all
164 EXPECT_EQ(0xbd, buffer[6]); 150 EXPECT_EQ(0xbd, buffer[6]);
165 151
166 // Read of size 0 at EOF is still an error. 152 // Read of size 0 at EOF is still an error.
167 memset(buffer, 0xbd, sizeof(buffer)); 153 memset(buffer, 0xbd, sizeof(buffer));
168 EXPECT_EQ(kReadError, opj_read_from_memory(buffer, 0, &dd)); 154 EXPECT_EQ(kReadError, opj_read_from_memory(buffer, 0, &dd));
169 EXPECT_EQ(0xbd, buffer[0]); 155 EXPECT_EQ(0xbd, buffer[0]);
170 } 156 }
171 } 157 }
172 158
173 TEST(fxcodec, DecodeDataReadBeyondBounds) { 159 TEST(fxcodec, DecodeDataReadBeyondBounds) {
174 unsigned char buffer[16]; 160 uint8_t buffer[16];
175 { 161 {
176 DecodeData dd(stream_data, sizeof(stream_data)); 162 DecodeData dd(stream_data, sizeof(stream_data));
177 163
178 // Read beyond bounds in a single step. 164 // Read beyond bounds in a single step.
179 memset(buffer, 0xbd, sizeof(buffer)); 165 memset(buffer, 0xbd, sizeof(buffer));
180 EXPECT_EQ(8u, opj_read_from_memory(buffer, sizeof(buffer) + 1, &dd)); 166 EXPECT_EQ(8u, opj_read_from_memory(buffer, sizeof(buffer) + 1, &dd));
181 EXPECT_EQ(0x00, buffer[0]); 167 EXPECT_EQ(0x00, buffer[0]);
182 EXPECT_EQ(0x01, buffer[1]); 168 EXPECT_EQ(0x01, buffer[1]);
183 EXPECT_EQ(0x02, buffer[2]); 169 EXPECT_EQ(0x02, buffer[2]);
184 EXPECT_EQ(0x03, buffer[3]); 170 EXPECT_EQ(0x03, buffer[3]);
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 EXPECT_EQ(0x87, buffer[1]); 213 EXPECT_EQ(0x87, buffer[1]);
228 EXPECT_EQ(0xbd, buffer[2]); 214 EXPECT_EQ(0xbd, buffer[2]);
229 215
230 // Read of 6 more gets nothing and leaves rest of buffer intact. 216 // Read of 6 more gets nothing and leaves rest of buffer intact.
231 memset(buffer, 0xbd, sizeof(buffer)); 217 memset(buffer, 0xbd, sizeof(buffer));
232 EXPECT_EQ(kReadError, opj_read_from_memory(buffer, 6, &dd)); 218 EXPECT_EQ(kReadError, opj_read_from_memory(buffer, 6, &dd));
233 EXPECT_EQ(0xbd, buffer[0]); 219 EXPECT_EQ(0xbd, buffer[0]);
234 } 220 }
235 } 221 }
236 222
237 TEST(fxcodec, DecodeDataWriteInBounds) {
238 unsigned char stream[16];
239 static unsigned char buffer_data[] = {
240 0x00, 0x01, 0x02, 0x03, 0x80, 0x80, 0x81, 0x82, 0x83, 0x84,
241 };
242 {
243 // Pretend the stream can only hold 4 bytes.
244 DecodeData dd(stream, 4);
245
246 memset(stream, 0xbd, sizeof(stream));
247 EXPECT_EQ(4u, opj_write_from_memory(buffer_data, 4, &dd));
248 EXPECT_EQ(0x00, stream[0]);
249 EXPECT_EQ(0x01, stream[1]);
250 EXPECT_EQ(0x02, stream[2]);
251 EXPECT_EQ(0x03, stream[3]);
252 EXPECT_EQ(0xbd, stream[4]);
253 }
254 {
255 // Pretend the stream can only hold 4 bytes.
256 DecodeData dd(stream, 4);
257
258 memset(stream, 0xbd, sizeof(stream));
259 EXPECT_EQ(2u, opj_write_from_memory(buffer_data, 2, &dd));
260 EXPECT_EQ(2u, opj_write_from_memory(buffer_data, 2, &dd));
261 EXPECT_EQ(0x00, stream[0]);
262 EXPECT_EQ(0x01, stream[1]);
263 EXPECT_EQ(0x00, stream[2]);
264 EXPECT_EQ(0x01, stream[3]);
265 EXPECT_EQ(0xbd, stream[4]);
266 }
267 }
268
269 TEST(fxcodec, DecodeDataWriteBeyondBounds) {
270 unsigned char stream[16];
271 static unsigned char buffer_data[] = {
272 0x10, 0x11, 0x12, 0x13, 0x94, 0x95, 0x96, 0x97,
273 };
274 {
275 // Pretend the stream can only hold 4 bytes.
276 DecodeData dd(stream, 4);
277
278 // Write ending past EOF transfers up til EOF.
279 memset(stream, 0xbd, sizeof(stream));
280 EXPECT_EQ(4u, opj_write_from_memory(buffer_data, 5, &dd));
281 EXPECT_EQ(0x10, stream[0]);
282 EXPECT_EQ(0x11, stream[1]);
283 EXPECT_EQ(0x12, stream[2]);
284 EXPECT_EQ(0x13, stream[3]);
285 EXPECT_EQ(0xbd, stream[4]);
286
287 // Subsequent writes fail.
288 memset(stream, 0xbd, sizeof(stream));
289 EXPECT_EQ(kWriteError, opj_write_from_memory(buffer_data, 5, &dd));
290 EXPECT_EQ(0xbd, stream[0]);
291 }
292 {
293 // Pretend the stream can only hold 4 bytes.
294 DecodeData dd(stream, 4);
295
296 // Write ending past EOF (two steps) transfers up til EOF.
297 memset(stream, 0xbd, sizeof(stream));
298 EXPECT_EQ(2u, opj_write_from_memory(buffer_data, 2, &dd));
299 EXPECT_EQ(2u, opj_write_from_memory(buffer_data, 4, &dd));
300 EXPECT_EQ(0x10, stream[0]);
301 EXPECT_EQ(0x11, stream[1]);
302 EXPECT_EQ(0x10, stream[2]);
303 EXPECT_EQ(0x11, stream[3]);
304 EXPECT_EQ(0xbd, stream[4]);
305
306 // Subsequent writes fail.
307 memset(stream, 0xbd, sizeof(stream));
308 EXPECT_EQ(kWriteError, opj_write_from_memory(buffer_data, 5, &dd));
309 EXPECT_EQ(0xbd, stream[0]);
310 }
311 }
312
313 // Note: Some care needs to be taken here because the skip/seek functions 223 // Note: Some care needs to be taken here because the skip/seek functions
314 // take OPJ_OFF_T's as arguments, which are typically a signed type. 224 // take OPJ_OFF_T's as arguments, which are typically a signed type.
315 TEST(fxcodec, DecodeDataSkip) { 225 TEST(fxcodec, DecodeDataSkip) {
316 unsigned char buffer[16]; 226 uint8_t buffer[16];
317 { 227 {
318 DecodeData dd(stream_data, sizeof(stream_data)); 228 DecodeData dd(stream_data, sizeof(stream_data));
319 229
320 // Skiping within buffer is allowed. 230 // Skiping within buffer is allowed.
321 memset(buffer, 0xbd, sizeof(buffer)); 231 memset(buffer, 0xbd, sizeof(buffer));
322 EXPECT_EQ(1u, opj_skip_from_memory(1, &dd)); 232 EXPECT_EQ(1u, opj_skip_from_memory(1, &dd));
323 EXPECT_EQ(1u, opj_read_from_memory(buffer, 1, &dd)); 233 EXPECT_EQ(1u, opj_read_from_memory(buffer, 1, &dd));
324 EXPECT_EQ(0x01, buffer[0]); 234 EXPECT_EQ(0x01, buffer[0]);
325 EXPECT_EQ(0xbd, buffer[1]); 235 EXPECT_EQ(0xbd, buffer[1]);
326 236
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
423 EXPECT_EQ(8u, opj_skip_from_memory(8, &dd)); 333 EXPECT_EQ(8u, opj_skip_from_memory(8, &dd));
424 EXPECT_EQ(kSkipError, opj_skip_from_memory(-4, &dd)); 334 EXPECT_EQ(kSkipError, opj_skip_from_memory(-4, &dd));
425 335
426 // Next read fails. 336 // Next read fails.
427 EXPECT_EQ(kReadError, opj_read_from_memory(buffer, 1, &dd)); 337 EXPECT_EQ(kReadError, opj_read_from_memory(buffer, 1, &dd));
428 EXPECT_EQ(0xbd, buffer[0]); 338 EXPECT_EQ(0xbd, buffer[0]);
429 } 339 }
430 } 340 }
431 341
432 TEST(fxcodec, DecodeDataSeek) { 342 TEST(fxcodec, DecodeDataSeek) {
433 unsigned char buffer[16]; 343 uint8_t buffer[16];
434 DecodeData dd(stream_data, sizeof(stream_data)); 344 DecodeData dd(stream_data, sizeof(stream_data));
435 345
436 // Seeking within buffer is allowed and read succeeds 346 // Seeking within buffer is allowed and read succeeds
437 memset(buffer, 0xbd, sizeof(buffer)); 347 memset(buffer, 0xbd, sizeof(buffer));
438 EXPECT_TRUE(opj_seek_from_memory(1, &dd)); 348 EXPECT_TRUE(opj_seek_from_memory(1, &dd));
439 EXPECT_EQ(1u, opj_read_from_memory(buffer, 1, &dd)); 349 EXPECT_EQ(1u, opj_read_from_memory(buffer, 1, &dd));
440 EXPECT_EQ(0x01, buffer[0]); 350 EXPECT_EQ(0x01, buffer[0]);
441 EXPECT_EQ(0xbd, buffer[1]); 351 EXPECT_EQ(0xbd, buffer[1]);
442 352
443 // Seeking before start returns error leaving position unchanged. 353 // Seeking before start returns error leaving position unchanged.
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
546 EXPECT_NE(img.comps[0].h, img.comps[1].h); 456 EXPECT_NE(img.comps[0].h, img.comps[1].h);
547 EXPECT_NE(img.comps[0].w, img.comps[2].w); 457 EXPECT_NE(img.comps[0].w, img.comps[2].w);
548 EXPECT_NE(img.comps[0].h, img.comps[2].h); 458 EXPECT_NE(img.comps[0].h, img.comps[2].h);
549 } 459 }
550 FX_Free(img.comps[0].data); 460 FX_Free(img.comps[0].data);
551 FX_Free(img.comps[1].data); 461 FX_Free(img.comps[1].data);
552 FX_Free(img.comps[2].data); 462 FX_Free(img.comps[2].data);
553 } 463 }
554 FX_Free(img.comps); 464 FX_Free(img.comps);
555 } 465 }
OLDNEW
« no previous file with comments | « core/fxcodec/codec/fx_codec_jpx_opj.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698