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

Side by Side Diff: third_party/WebKit/Source/platform/image-decoders/png/PNGImageDecoderTest.cpp

Issue 2618633004: Add support for Animated PNG (Closed)
Patch Set: Fix bug caught by fuzzer Created 3 years, 11 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium 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 "platform/image-decoders/png/PNGImageDecoder.h" 5 #include "platform/image-decoders/png/PNGImageDecoder.h"
6 6
7 #include "platform/image-decoders/ImageDecoderTestHelpers.h" 7 #include "platform/image-decoders/ImageDecoderTestHelpers.h"
8 #include "png.h"
8 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
9 #include <memory> 10 #include <memory>
10 11
12 // /LayoutTests/images/resources/png-animated-idat-part-of-animation.png
13 // is modified in multiple tests to simulate erroneous PNGs. As a reference,
14 // the table below shows how the file is structured.
15 //
16 // Offset | 8 33 95 133 172 210 241 279 314 352 422
17 // -------------------------------------------------------------------------
18 // Chunk | IHDR acTL fcTL IDAT fcTL fdAT fcTL fdAT fcTL fdAT IEND
19 //
20 // In between the acTL and fcTL there are two other chunks, PLTE and tRNS, but
21 // those are not specifically used in this test suite. The same holds for a
22 // tEXT chunk in between the last fdAT and IEND.
23 //
24 // In the current behavior of PNG image decoders, the 4 frames are detected when
25 // respectively 141, 249, 322 and 430 bytes are received. The first frame should
26 // be detected when the IDAT has been received, and non-first frames when the
27 // next fcTL or IEND chunk has been received. Note that all offsets are +8,
28 // because a chunk is identified by byte 4-7.
29
30 // @TODO(joostouwerling) extend test image set with other image encodings, such
31 // as first frame fcTL and multiple chunks per frame.
32
11 namespace blink { 33 namespace blink {
12 34
13 namespace { 35 namespace {
14 36
15 std::unique_ptr<ImageDecoder> createDecoder( 37 std::unique_ptr<ImageDecoder> createDecoder(
16 ImageDecoder::AlphaOption alphaOption) { 38 ImageDecoder::AlphaOption alphaOption) {
17 return WTF::wrapUnique(new PNGImageDecoder( 39 return WTF::wrapUnique(new PNGImageDecoder(
18 alphaOption, ColorBehavior::transformToTargetForTesting(), 40 alphaOption, ColorBehavior::transformToTargetForTesting(),
19 ImageDecoder::noDecodedImageByteLimit)); 41 ImageDecoder::noDecodedImageByteLimit));
20 } 42 }
21 43
22 std::unique_ptr<ImageDecoder> createDecoder() { 44 std::unique_ptr<ImageDecoder> createDecoder() {
23 return createDecoder(ImageDecoder::AlphaNotPremultiplied); 45 return createDecoder(ImageDecoder::AlphaNotPremultiplied);
24 } 46 }
25 47
26 std::unique_ptr<ImageDecoder> createDecoderWithPngData(const char* pngFile) { 48 std::unique_ptr<ImageDecoder> createDecoderWithPngData(const char* pngFile) {
27 auto decoder = createDecoder(); 49 auto decoder = createDecoder();
28 auto data = readFile(pngFile); 50 auto data = readFile(pngFile);
29 EXPECT_FALSE(data->isEmpty()); 51 EXPECT_FALSE(data->isEmpty());
30 decoder->setData(data.get(), true); 52 decoder->setData(data.get(), true);
31 return decoder; 53 return decoder;
32 } 54 }
33 55
34 void testSize(const char* pngFile, IntSize expectedSize) { 56 void testSize(const char* pngFile, IntSize expectedSize) {
35 auto decoder = createDecoderWithPngData(pngFile); 57 auto decoder = createDecoderWithPngData(pngFile);
36 EXPECT_TRUE(decoder->isSizeAvailable()); 58 EXPECT_TRUE(decoder->isSizeAvailable());
37 EXPECT_EQ(expectedSize, decoder->size()); 59 EXPECT_EQ(expectedSize, decoder->size());
38 } 60 }
39 61
62 // Test whether querying for the size of the image works if we present the
63 // data byte by byte.
64 void testSizeByteByByte(const char* pngFile,
65 size_t bytesNeededToDecodeSize,
66 IntSize expectedSize) {
67 auto decoder = createDecoder();
68 auto data = readFile(pngFile);
69 ASSERT_FALSE(data->isEmpty());
70 ASSERT_LT(bytesNeededToDecodeSize, data->size());
71
72 for (size_t length = 1; length <= bytesNeededToDecodeSize; length++) {
73 RefPtr<SharedBuffer> tempData = SharedBuffer::create(data->data(), length);
74 decoder->setData(tempData.get(), false);
75
76 if (length < bytesNeededToDecodeSize) {
77 EXPECT_FALSE(decoder->isSizeAvailable());
78 EXPECT_TRUE(decoder->size().isEmpty());
79 EXPECT_FALSE(decoder->failed());
80 } else {
81 EXPECT_TRUE(decoder->isSizeAvailable());
82 EXPECT_EQ(expectedSize, decoder->size());
83 }
84 }
85 EXPECT_FALSE(decoder->failed());
86 }
87
88 void writeUint32(uint32_t val, png_byte* data) {
89 data[0] = val >> 24;
90 data[1] = val >> 16;
91 data[2] = val >> 8;
92 data[3] = val;
93 }
94
40 void testRepetitionCount(const char* pngFile, int expectedRepetitionCount) { 95 void testRepetitionCount(const char* pngFile, int expectedRepetitionCount) {
41 auto decoder = createDecoderWithPngData(pngFile); 96 auto decoder = createDecoderWithPngData(pngFile);
42 // Decoding the frame count sets the repetition count as well. 97 // Decode frame count should see the number of repetitions as well.
43 decoder->frameCount(); 98 decoder->frameCount();
44 EXPECT_FALSE(decoder->failed()); 99 EXPECT_FALSE(decoder->failed());
45 EXPECT_EQ(expectedRepetitionCount, decoder->repetitionCount()); 100 EXPECT_EQ(expectedRepetitionCount, decoder->repetitionCount());
46 } 101 }
47 102
103 struct PublicFrameInfo {
104 size_t duration;
105 IntRect frameRect;
106 ImageFrame::AlphaBlendSource alphaBlend;
107 ImageFrame::DisposalMethod disposalMethod;
108 };
109
110 // This is the frame data for the following PNG image:
111 // /LayoutTests/images/resources/png-animated-idat-part-of-animation.png
112 static PublicFrameInfo pngAnimatedFrameInfo[] = {
113 {500,
114 {IntPoint(0, 0), IntSize(5, 5)},
115 ImageFrame::BlendAtopBgcolor,
116 ImageFrame::DisposeKeep},
117 {900,
118 {IntPoint(1, 1), IntSize(3, 1)},
119 ImageFrame::BlendAtopBgcolor,
120 ImageFrame::DisposeOverwriteBgcolor},
121 {2000,
122 {IntPoint(1, 2), IntSize(3, 2)},
123 ImageFrame::BlendAtopPreviousFrame,
124 ImageFrame::DisposeKeep},
125 {1500,
126 {IntPoint(1, 2), IntSize(3, 1)},
127 ImageFrame::BlendAtopBgcolor,
128 ImageFrame::DisposeKeep},
129 };
130
131 void compareFrameWithExpectation(const PublicFrameInfo& expected,
132 const ImageFrame* frame) {
133 EXPECT_EQ(expected.duration, frame->duration());
134 EXPECT_EQ(expected.frameRect, frame->originalFrameRect());
135 EXPECT_EQ(expected.disposalMethod, frame->getDisposalMethod());
136 EXPECT_EQ(expected.alphaBlend, frame->getAlphaBlendSource());
137 }
138
139 // This function removes |length| bytes at |offset|, and then calls frameCount.
140 // It assumes the missing bytes should result in a failed decode because the
141 // parser jumps |length| bytes too far in the next chunk.
142 void testMissingDataBreaksDecoding(const char* pngFile,
143 size_t offset,
144 size_t length) {
145 auto decoder = createDecoder();
146 auto data = readFile(pngFile);
147 ASSERT_FALSE(data->isEmpty());
148
149 RefPtr<SharedBuffer> invalidData = SharedBuffer::create(data->data(), offset);
150 invalidData->append(data->data() + offset + length,
151 data->size() - offset - length);
152 ASSERT_EQ(data->size() - length, invalidData->size());
153
154 decoder->setData(invalidData, true);
155 decoder->frameCount();
156 EXPECT_TRUE(decoder->failed());
157 }
158
159 // Decoding up to the indicated fcTL offset and then provide an fcTL with
160 // the wrong chunk size (20 instead of 26). It should break the decoder.
161 void testInvalidFctlSize(const char* pngFile,
162 size_t offsetFctl,
163 size_t expectedFrameCountBeforeFail,
164 bool expectedDecoderFailure) {
165 auto data = readFile(pngFile);
166 ASSERT_FALSE(data->isEmpty());
167
168 auto decoder = createDecoder();
169 RefPtr<SharedBuffer> invalidData =
170 SharedBuffer::create(data->data(), offsetFctl);
171
172 // Test if this gives the correct frame count, before the fcTL is parsed.
173 decoder->setData(invalidData, false);
174 EXPECT_EQ(expectedFrameCountBeforeFail, decoder->frameCount());
175 ASSERT_FALSE(decoder->failed());
176
177 // Append the wrong size to the data stream
178 png_byte sizeChunk[4];
179 writeUint32(20, sizeChunk);
180 invalidData->append(reinterpret_cast<char*>(sizeChunk), 4u);
181
182 // Skip the size in the original data, but provide the rest of the fcTL,
183 // which is 4B of tag, 26B of data and 4B of CRC, totalling 34B.
184 invalidData->append(data->data() + offsetFctl + 4, 34u);
185
186 decoder->setData(invalidData, false);
187 decoder->frameCount();
188 EXPECT_EQ(expectedDecoderFailure, decoder->failed());
189 }
190
191 void testDifferentActlFrameCountIsIgnored(const char* pngFile,
192 size_t offsetActl,
193 size_t injectedFrameCount,
194 size_t expectedFrameCount) {
195 // First make sure that this tests makes sense.
196 ASSERT_NE(injectedFrameCount, expectedFrameCount);
197
198 auto data = readFile(pngFile);
199 auto decoder = createDecoder();
200 ASSERT_FALSE(data->isEmpty());
201
202 RefPtr<SharedBuffer> diffActlData =
203 SharedBuffer::create(data->data(), offsetActl + 8);
204 // Write the injectedFrameCount to the stream
205 png_byte sizeChunk[4];
206 writeUint32(injectedFrameCount, sizeChunk);
207 diffActlData->append(reinterpret_cast<char*>(sizeChunk), 4u);
208 // Append the rest of the data. The first |offsetActl + 12| bytes that are
209 // already in diffActlData should not be appended again.
210 diffActlData->append(data->data() + offsetActl + 12,
211 data->size() - offsetActl - 12);
212
213 decoder->setData(diffActlData, true);
214 EXPECT_EQ(expectedFrameCount, decoder->frameCount());
215 }
216
48 // Verify that the decoder can successfully decode the first frame when 217 // Verify that the decoder can successfully decode the first frame when
49 // initially only half of the frame data is received, resulting in a partially 218 // initially only half of the frame data is received, resulting in a partially
50 // decoded image, and then the rest of the image data is received. Verify that 219 // decoded image, and then the rest of the image data is received. Verify that
51 // the bitmap hashes of the two stages are different. Also verify that the final 220 // the bitmap hashes of the two stages are different. Also verify that the final
52 // bitmap hash is equivalent to the hash when all data is provided at once. 221 // bitmap hash is equivalent to the hash when all data is provided at once.
53 // 222 //
54 // This verifies that decoder correctly keeps track of where it stopped 223 // This verifies that decoder correctly keeps track of where it stopped
55 // decoding when the image was not yet fully received. 224 // decoding when the image was not yet fully received.
56 void testProgressiveDecodingContinuesAfterFullData(const char* pngFile, 225 void testProgressiveDecodingContinuesAfterFullData(const char* pngFile,
57 size_t offsetMidFirstFrame) { 226 size_t offsetMidFirstFrame) {
58 auto fullData = readFile(pngFile); 227 auto fullData = readFile(pngFile);
59 ASSERT_FALSE(fullData->isEmpty()); 228 ASSERT_FALSE(fullData->isEmpty());
60 229
61 auto decoderUpfront = createDecoder(); 230 auto decoderUpfront = createDecoder();
62 decoderUpfront->setData(fullData.get(), true); 231 decoderUpfront->setData(fullData.get(), true);
63 EXPECT_GE(1u, decoderUpfront->frameCount()); 232 EXPECT_GE(decoderUpfront->frameCount(), 1u);
64 const ImageFrame* const frameUpfront = decoderUpfront->frameBufferAtIndex(0); 233 const ImageFrame* const frameUpfront = decoderUpfront->frameBufferAtIndex(0);
65 ASSERT_EQ(ImageFrame::FrameComplete, frameUpfront->getStatus()); 234 ASSERT_EQ(ImageFrame::FrameComplete, frameUpfront->getStatus());
66 const unsigned hashUpfront = hashBitmap(frameUpfront->bitmap()); 235 const unsigned hashUpfront = hashBitmap(frameUpfront->bitmap());
67 236
68 auto decoder = createDecoder(); 237 auto decoder = createDecoder();
69 RefPtr<SharedBuffer> partialData = 238 RefPtr<SharedBuffer> partialData =
70 SharedBuffer::create(fullData->data(), offsetMidFirstFrame); 239 SharedBuffer::create(fullData->data(), offsetMidFirstFrame);
71 decoder->setData(partialData, false); 240 decoder->setData(partialData, false);
72 241
73 EXPECT_EQ(1u, decoder->frameCount()); 242 EXPECT_EQ(1u, decoder->frameCount());
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 // should give an error during decoding. 276 // should give an error during decoding.
108 data->append(fullData->data() + idatOffset, 8u); 277 data->append(fullData->data() + idatOffset, 8u);
109 data->append(fullData->data() + idatOffset + 16u, 278 data->append(fullData->data() + idatOffset + 16u,
110 fullData->size() - idatOffset - 16u); 279 fullData->size() - idatOffset - 16u);
111 280
112 auto decoder = createDecoder(); 281 auto decoder = createDecoder();
113 decoder->setData(data.get(), true); 282 decoder->setData(data.get(), true);
114 283
115 EXPECT_EQ(expectedFrameCountBefore, decoder->frameCount()); 284 EXPECT_EQ(expectedFrameCountBefore, decoder->frameCount());
116 285
117 const ImageFrame* const frame = decoder->frameBufferAtIndex(frameIndex); 286 decoder->frameBufferAtIndex(frameIndex);
118 EXPECT_EQ(expectFailure, decoder->failed()); 287 ASSERT_EQ(expectFailure, decoder->failed());
119 if (!expectFailure) { 288 if (!expectFailure) {
120 EXPECT_EQ(expectedFrameCountAfter, decoder->frameCount()); 289 EXPECT_EQ(expectedFrameCountAfter, decoder->frameCount());
121 EXPECT_EQ(ImageFrame::FrameEmpty, frame->getStatus()); 290 EXPECT_EQ(nullptr, decoder->frameBufferAtIndex(frameIndex));
122 } 291 }
123 } 292 }
124 293
125 } // Anonymous namespace 294 } // Anonymous namespace
126 295
296 // Animated PNG Tests
297
298 TEST(AnimatedPNGTests, sizeTest) {
299 testSize(
300 "/LayoutTests/images/resources/"
301 "png-animated-idat-part-of-animation.png",
302 IntSize(5, 5));
303 testSize(
304 "/LayoutTests/images/resources/"
305 "png-animated-idat-not-part-of-animation.png",
306 IntSize(227, 35));
307 }
308
309 TEST(AnimatedPNGTests, repetitionCountTest) {
310 testRepetitionCount(
311 "/LayoutTests/images/resources/"
312 "png-animated-idat-part-of-animation.png",
313 7u);
314 // This is an "animated" image with only one frame, that is, the IDAT is
315 // ignored and there is one fdAT frame. so it should be considered
316 // non-animated.
317 testRepetitionCount(
318 "/LayoutTests/images/resources/"
319 "png-animated-idat-not-part-of-animation.png",
320 cAnimationNone);
321 }
322
323 // Test if the decoded metdata corresponds to the defined expectations
324 TEST(AnimatedPNGTests, MetaDataTest) {
325 const char* pngFile =
326 "/LayoutTests/images/resources/"
327 "png-animated-idat-part-of-animation.png";
328 constexpr size_t expectedFrameCount = 4;
329
330 auto decoder = createDecoderWithPngData(pngFile);
331 ASSERT_EQ(expectedFrameCount, decoder->frameCount());
332 for (size_t i = 0; i < expectedFrameCount; i++) {
333 compareFrameWithExpectation(pngAnimatedFrameInfo[i],
334 decoder->frameBufferAtIndex(i));
335 }
336 }
337
338 TEST(AnimatedPNGTests, EmptyFrame) {
339 const char* pngFile = "/LayoutTests/images/resources/empty-frame.png";
340 auto decoder = createDecoderWithPngData(pngFile);
341 // Frame 0 is empty. Ensure that decoding frame 1 (which depends on frame 0)
342 // does not crash.
343 decoder->frameBufferAtIndex(1);
344 }
345
346 TEST(AnimatedPNGTests, ByteByByteSizeAvailable) {
347 testSizeByteByByte(
348 "/LayoutTests/images/resources/"
349 "png-animated-idat-part-of-animation.png",
350 141u, IntSize(5, 5));
351 testSizeByteByByte(
352 "/LayoutTests/images/resources/"
353 "png-animated-idat-not-part-of-animation.png",
354 79u, IntSize(227, 35));
355 }
356
357 // Test whether the frame metadata decoding also works when we provide the data
358 // byte by byte. This should cover the case when the client does not provide
359 // all data at once. At given offsets, we expect frames to become available.
360 // This test checks whether that is the case, and if so, if the frame data is
361 // equal to what we expected.
362 TEST(AnimatedPNGTests, ByteByByteMetaData) {
363 const char* pngFile =
364 "/LayoutTests/images/resources/"
365 "png-animated-idat-part-of-animation.png";
366 constexpr size_t expectedFrameCount = 4;
367
368 // These are the byte offsets where each frame should have been parsed.
369 // It boils down to the offset of the first fcTL / IEND after the last
370 // frame data chunk, plus 8 bytes for recognition. The exception on this is
371 // the first frame, which is reported when its first framedata is seen.
372 size_t frameOffsets[expectedFrameCount] = {141, 249, 322, 430};
373
374 auto decoder = createDecoder();
375 auto data = readFile(pngFile);
376 ASSERT_FALSE(data->isEmpty());
377 size_t framesParsed = 0;
378
379 for (size_t length = 1; length <= frameOffsets[expectedFrameCount - 1];
380 length++) {
381 RefPtr<SharedBuffer> tempData = SharedBuffer::create(data->data(), length);
382 decoder->setData(tempData.get(), false);
383 EXPECT_FALSE(decoder->failed());
384 if (length < frameOffsets[framesParsed]) {
385 EXPECT_EQ(framesParsed, decoder->frameCount());
386 } else {
387 ASSERT_EQ(framesParsed + 1, decoder->frameCount());
388 compareFrameWithExpectation(pngAnimatedFrameInfo[framesParsed],
389 decoder->frameBufferAtIndex(framesParsed));
390 framesParsed++;
391 }
392 }
393 EXPECT_EQ(expectedFrameCount, decoder->frameCount());
394 EXPECT_FALSE(decoder->failed());
395 }
396
397 TEST(AnimatedPNGTests, TestRandomFrameDecode) {
398 testRandomFrameDecode(&createDecoder,
399 "/LayoutTests/images/resources/"
400 "png-animated-idat-part-of-animation.png",
401 2u);
402 }
403
404 TEST(AnimatedPNGTests, TestDecodeAfterReallocation) {
405 testDecodeAfterReallocatingData(&createDecoder,
406 "/LayoutTests/images/resources/"
407 "png-animated-idat-part-of-animation.png");
408 }
409
410 TEST(AnimatedPNGTests, ProgressiveDecode) {
411 testProgressiveDecoding(&createDecoder,
412 "/LayoutTests/images/resources/"
413 "png-animated-idat-part-of-animation.png",
414 13u);
415 }
416
417 TEST(AnimatedPNGTests, ParseAndDecodeByteByByte) {
418 testByteByByteDecode(&createDecoder,
419 "/LayoutTests/images/resources/"
420 "png-animated-idat-part-of-animation.png",
421 4u, 7u);
422 }
423
424 TEST(AnimatedPNGTests, FailureDuringParsing) {
425 // Test the first fcTL in the stream. Because no frame data has been set at
426 // this point, the expected frame count is zero. 95 bytes is just before the
427 // first fcTL chunk, at which the first frame is detected. The decoder should
428 // be in the failed state since no frames were parsed before this error.
429 testInvalidFctlSize(
430 "/LayoutTests/images/resources/"
431 "png-animated-idat-part-of-animation.png",
432 95u, 0u, true);
433
434 // Test for the third fcTL in the stream. This should be tested as well,
435 // since the first fcTL is parsed in PNGImageReader::parseSize() whereas
436 // later fcTLs are parsed in PNGImageReader::parse() as part of framecount.
437 // The expected frame count before the fcTL chunk is 1u, since the second
438 // frame is registered when the third fcTL (or IEND) is seen.
439 //
440 // This should *not* set the decoder to the failed state since the first
441 // frame was succesfully parsed, and we still want to show that frame.
442 testInvalidFctlSize(
443 "/LayoutTests/images/resources/"
444 "png-animated-idat-part-of-animation.png",
445 241u, 1u, false);
446 }
447
448 TEST(AnimatedPNGTests, FailureDuringParsingDueToMissingFctlData) {
449 // The fcTL chunk starts at 95u, add 10u to get to the content data, and
450 // remove 5 bytes of data. This sets the decoder to the failed state since
451 // no frames were parsed yet.
452 testMissingDataBreaksDecoding(
453 "/LayoutTests/images/resources/"
454 "png-animated-idat-part-of-animation.png",
455 105u, 5u);
456 }
457
458 TEST(AnimatedPNGTests, MissingActlResultsInNonAnimated) {
459 const char* pngFile =
460 "/LayoutTests/images/resources/"
461 "png-animated-idat-part-of-animation.png";
462 auto data = readFile(pngFile);
463 auto decoder = createDecoder();
464 ASSERT_FALSE(data->isEmpty());
465
466 // Remove the acTL chunk from the stream.
467 const size_t offsetActl = 33;
468 RefPtr<SharedBuffer> noActlData =
469 SharedBuffer::create(data->data(), offsetActl);
470 noActlData->append(data->data() + offsetActl + 20,
471 data->size() - offsetActl - 20);
472
473 decoder->setData(noActlData, true);
474 EXPECT_EQ(1u, decoder->frameCount());
475 EXPECT_FALSE(decoder->failed());
476 }
477
478 // Test if the indicated frame count by the acTL is ignored if the actual
479 // number of frames is different. Test for higher and lower indicated number.
480 TEST(AnimatedPNGTests, differentActlFrameCountIsIgnored) {
481 const char* pngFile =
482 "/LayoutTests/images/resources/"
483 "png-animated-idat-part-of-animation.png";
484 testDifferentActlFrameCountIsIgnored(pngFile, 33u, 2u, 4u);
485 testDifferentActlFrameCountIsIgnored(pngFile, 33u, 8u, 4u);
486 }
487
488 // Originally, the third frame has an offset of (1,2) and a size of (3,2). By
489 // changing the offset to (0,0) and the size to (4,4), a frame with not enough
490 // data is simulated. The expected result is that the decoder will only receive
491 // 1 row of data, since the data contains 6 pixels and each row is 4 pixels.
492 //
493 // When the decoder receives not enough data for a frame, the expected behavior
494 // is that nothing changes: the decoder should not be invalidated, and the
495 // frame should be considered successfully decoded. The reason for this is that
496 // the error could either be an invalid frame size, or an invalid data chunk.
497 // For now, the decoder does not take any action if not enough data is
498 // provided, as described in PNGImageDecoder::complete().
499 TEST(AnimatedPNGTests, VerifyIncompleteFrameDataDoesNotInvalidateDecoder) {
500 const char* pngFile =
501 "/LayoutTests/images/resources/"
502 "png-animated-idat-part-of-animation.png";
503 auto data = readFile(pngFile);
504 auto decoder = createDecoder();
505 ASSERT_FALSE(data->isEmpty());
506
507 // Change the size and the offset of the frame, as described above. The size
508 // is stored in byte 12-19 of the fcTL chunk, and the offset in byte 20-27.
509 const size_t offsetThirdFctl = 241 + 12;
510 RefPtr<SharedBuffer> modifiedData =
511 SharedBuffer::create(data->data(), offsetThirdFctl);
512 png_byte rectChunk[16];
513 writeUint32(4, rectChunk);
514 writeUint32(4, rectChunk + 4);
515 writeUint32(0, rectChunk + 8);
516 writeUint32(0, rectChunk + 12);
517 modifiedData->append(
518 const_cast<const char*>(reinterpret_cast<char*>(rectChunk)), 16u);
519 modifiedData->append(data->data() + offsetThirdFctl + 16,
520 data->size() - offsetThirdFctl - 16);
521
522 decoder->setData(modifiedData, true);
523
524 // Verify that parsing the image results in the expected meta data.
525 IntSize expectedSize(5, 5);
526 const size_t expectedFrameCount = 4;
527 IntRect expectedFrameRect(IntPoint(0, 0), IntSize(4, 4));
528 EXPECT_TRUE(decoder->isSizeAvailable());
529 EXPECT_EQ(expectedSize, decoder->size());
530 EXPECT_EQ(expectedFrameCount, decoder->frameCount());
531 ASSERT_FALSE(decoder->failed());
532
533 // Try to decode frame 3. This should not fail the decoder, and the frame
534 // should be FrameComplete.
535 const ImageFrame* const buffer = decoder->frameBufferAtIndex(2);
536 EXPECT_EQ(expectedFrameRect, buffer->originalFrameRect());
537 EXPECT_FALSE(decoder->failed());
538 EXPECT_EQ(expectedFrameCount, decoder->frameCount());
539 EXPECT_EQ(ImageFrame::FrameComplete, buffer->getStatus());
540 }
541
542 // Originally, the third frame has an offset of (1,2) and a size of (3,2). By
543 // changing the offset to (4,4), the frame rect will extend the image size of
544 // 5x5. Verify that the frame is correctly clipped to a size of (1,1) without
545 // any decoding errors. Since this is the third frame, a decoding failure would
546 // result in the frame count being adjusted to 2, and the decoder would not be
547 // invalidated. Therefore, it is verified that after decoding frame 3 the
548 // decoder is still in a valid state and the frame count is still 4.
549 TEST(AnimatedPNGTests, VerifyFrameExtendsImageSizeClipsCorrectly) {
550 const char* pngFile =
551 "/LayoutTests/images/resources/"
552 "png-animated-idat-part-of-animation.png";
553 auto data = readFile(pngFile);
554 auto decoder = createDecoder();
555 ASSERT_FALSE(data->isEmpty());
556
557 // The offset of the frame rect is located 20 bytes into the fcTL chunk, after
558 // the length, tag, sequence number, width and height fields (all 4B).
559 const size_t offsetThirdFctl = 241 + 20;
560 RefPtr<SharedBuffer> modifiedData =
561 SharedBuffer::create(data->data(), offsetThirdFctl);
562 png_byte offsetChunk[8];
563 writeUint32(4, offsetChunk);
564 writeUint32(4, offsetChunk + 4);
565 modifiedData->append(
566 const_cast<const char*>(reinterpret_cast<char*>(offsetChunk)), 8u);
567 modifiedData->append(data->data() + offsetThirdFctl + 8,
568 data->size() - offsetThirdFctl - 8);
569
570 decoder->setData(modifiedData, true);
571
572 // Verify that parsing the image results in the expected meta data.
573 IntSize expectedSize(5, 5);
574 const size_t expectedFrameCount = 4;
575 IntRect expectedFrameRect(IntPoint(4, 4), IntSize(1, 1));
576 EXPECT_TRUE(decoder->isSizeAvailable());
577 EXPECT_EQ(expectedSize, decoder->size());
578 EXPECT_EQ(expectedFrameCount, decoder->frameCount());
579 ASSERT_FALSE(decoder->failed());
580
581 // Try to decode frame 3. This should not fail the decoder, and the frame
582 // should be FrameComplete.
583 const ImageFrame* const buffer = decoder->frameBufferAtIndex(2);
584 EXPECT_EQ(expectedFrameRect, buffer->originalFrameRect());
585 EXPECT_FALSE(decoder->failed());
586 EXPECT_EQ(expectedFrameCount, decoder->frameCount());
587 EXPECT_EQ(ImageFrame::FrameComplete, buffer->getStatus());
588 }
589
590 TEST(AnimatedPNGTests, ProgressiveDecodingContinuesAfterFullData) {
591 // 160u is a randomly chosen offset in the IDAT chunk of the first frame.
592 testProgressiveDecodingContinuesAfterFullData(
593 "/LayoutTests/images/resources/"
594 "png-animated-idat-part-of-animation.png",
595 160u);
596 }
597
598 TEST(AnimatedPNGTests, RandomDecodeAfterClearFrameBufferCache) {
599 testRandomDecodeAfterClearFrameBufferCache(
600 &createDecoder,
601 "/LayoutTests/images/resources/"
602 "png-animated-idat-part-of-animation.png",
603 2u);
604 }
605
606 TEST(AnimatedPNGTests, VerifyAlphaBlending) {
607 testAlphaBlending(&createDecoder,
608 "/LayoutTests/images/resources/"
609 "png-animated-idat-part-of-animation.png");
610 }
611
612 // This tests if the frame count gets set correctly when parsing frameCount
613 // fails in one of the parsing queries.
614 //
615 // First, enough data is provided such that two frames should be registered.
616 // The decoder should at this point not be in the failed status.
617 //
618 // Then, we provide the rest of the data except for the last IEND chunk, but
619 // tell the decoder that this is all the data we have. The frame count should
620 // be three, since one extra frame should be discovered. The fourth frame
621 // should *not* be registered since the reader should not be able to determine
622 // where the frame ends. The decoder should *not* be in the failed state since
623 // there are three frames which can be shown.
624 TEST(AnimatedPNGTests, FailureMissingIendChunk) {
625 RefPtr<SharedBuffer> fullData = readFile(
626 "/LayoutTests/images/resources/"
627 "png-animated-idat-part-of-animation.png");
628 ASSERT_FALSE(fullData->isEmpty());
629 auto decoder = createDecoder();
630
631 const size_t offsetTwoFrames = 249;
632 const size_t expectedFramesAfter249Bytes = 2;
633 RefPtr<SharedBuffer> tempData =
634 SharedBuffer::create(fullData->data(), offsetTwoFrames);
635 decoder->setData(tempData.get(), false);
636 EXPECT_EQ(expectedFramesAfter249Bytes, decoder->frameCount());
637 EXPECT_FALSE(decoder->failed());
638
639 // Provide the rest of the data except for the last IEND chunk.
640 const size_t expectedFramesAfterAllExpect12Bytes = 3;
641 tempData = SharedBuffer::create(fullData->data(), fullData->size() - 12);
642 decoder->setData(tempData.get(), true);
643 EXPECT_EQ(expectedFramesAfterAllExpect12Bytes, decoder->frameCount());
644 EXPECT_FALSE(decoder->failed());
645 }
646
647 TEST(AnimatedPNGTests, VerifyFrameCountChangesOnDecodingFailure) {
648 testFailureDuringDecode(
649 "/LayoutTests/images/resources/"
650 "png-animated-idat-part-of-animation.png",
651 279u, // idat offset for frame index 2
652 2u, // try to decode frame index 2
653 false, // expect the decoder to *not* be invalidated after the failure
654 4u, // expected frame count before failure
655 2u); // expected frame count after failure
656
657 testFailureDuringDecode(
658 "/LayoutTests/images/resources/"
659 "png-animated-idat-part-of-animation.png",
660 133u, // idat offset for frame index 0
661 0u, // try to decode frame index 0
662 true, // expect the decoder to be invalidated after the failure
663 4u); // expected frame count before failure
664 }
665
666 // Verify that a malformatted PNG, where the IEND appears before any frame data
667 // (IDAT), invalidates the decoder.
668 TEST(AnimatedPNGTests, VerifyIENDBeforeIDATInvalidatesDecoder) {
669 RefPtr<SharedBuffer> fullData = readFile(
670 "/LayoutTests/images/resources/"
671 "png-animated-idat-part-of-animation.png");
672 ASSERT_FALSE(fullData->isEmpty());
673 auto decoder = createDecoder();
674
675 const size_t offsetIDAT = 133;
676 RefPtr<SharedBuffer> data =
677 SharedBuffer::create(fullData->data(), offsetIDAT);
678 data->append(fullData->data() + fullData->size() - 12u, 12u);
679 data->append(fullData->data() + offsetIDAT, fullData->size() - offsetIDAT);
680 decoder->setData(data.get(), true);
681
682 const size_t expectedFrameCount = 0u;
683 EXPECT_EQ(expectedFrameCount, decoder->frameCount());
684 EXPECT_TRUE(decoder->failed());
685 }
686
687 // For animated images, frameIsCompleteAtIndex(i) should return true if the
688 // i-th frame is fully received. The frames don't need to be successfully
689 // decoded.
690 TEST(AnimatedPNGTests, VerifyFrameCompleteBehavior) {
691 const char* pngFile =
692 "/LayoutTests/images/resources/"
693 "png-animated-idat-part-of-animation.png";
694 RefPtr<SharedBuffer> fullData = readFile(pngFile);
695 ASSERT_FALSE(fullData->isEmpty());
696 auto decoder = createDecoder();
697
698 // When not all data for the first frame has been received,
699 // frameIsCompleteAtIndex(0) should return false.
700 const size_t offsetMidwayFirstFrame = 160;
701 RefPtr<SharedBuffer> data =
702 SharedBuffer::create(fullData->data(), offsetMidwayFirstFrame);
703 decoder->setData(data.get(), false);
704 EXPECT_EQ(1u, decoder->frameCount());
705 ASSERT_FALSE(decoder->failed());
706 EXPECT_FALSE(decoder->frameIsCompleteAtIndex(0));
707
708 // When all image data is received, every frame should be complete.
709 const size_t expectedFrameCount = 4;
710 decoder->setData(fullData.get(), true);
711 EXPECT_EQ(expectedFrameCount, decoder->frameCount());
712 for (size_t index = 0; index < expectedFrameCount; index++)
713 EXPECT_TRUE(decoder->frameIsCompleteAtIndex(index));
714 }
715
716 // Verify that erroneous values for the disposal method and alpha blending
717 // are parsed into the expected default values, respectively DisposeNotSpecified
718 // and BlendAtopBgcolor.
719 TEST(AnimatedPNGTests, VerifyInvalidDisposalAndBlendingDefaultCorrectly) {
720 const char* pngFile =
721 "/LayoutTests/images/resources/"
722 "png-animated-idat-part-of-animation.png";
723 RefPtr<SharedBuffer> fullData = readFile(pngFile);
724 ASSERT_FALSE(fullData->isEmpty());
725 auto decoder = createDecoder();
726
727 // The disposal byte in the frame control chunk is the 24th byte, alpha
728 // blending the 25th. |offsetDisposalOp| is 241 bytes to get to the third
729 // fctl chunk, 8 bytes to skip the length and tag bytes, and 24 bytes to get
730 // to the disposal op.
731 //
732 // Write random values to the disposal and alpha blending byte, and append
733 // the rest of the buffer.
734 const size_t offsetDisposalOp = 241 + 8 + 24;
735 RefPtr<SharedBuffer> data =
736 SharedBuffer::create(fullData->data(), offsetDisposalOp);
737 png_byte disposalAndBlending[2];
738 disposalAndBlending[0] = 7;
739 disposalAndBlending[1] = 9;
740 data->append(reinterpret_cast<char*>(disposalAndBlending), 2u);
741 data->append(fullData->data() + offsetDisposalOp + 2u,
742 fullData->size() - offsetDisposalOp - 2u);
743
744 decoder->setData(data.get(), true);
745 decoder->frameCount();
746 ASSERT_FALSE(decoder->failed());
747 EXPECT_EQ(ImageFrame::DisposalMethod::DisposeNotSpecified,
748 decoder->frameBufferAtIndex(2)->getDisposalMethod());
749 EXPECT_EQ(ImageFrame::AlphaBlendSource::BlendAtopBgcolor,
750 decoder->frameBufferAtIndex(2)->getAlphaBlendSource());
751 }
752
753 // This test verifies that the following situation does not invalidate the
754 // decoder:
755 // - Frame 0 is decoded progressively, but there's not enough data to fully
756 // decode it.
757 // - The rest of the image data is received.
758 // - Frame X, with X > 0, and X does not depend on frame 0, is decoded.
759 // - Frame 0 is decoded.
760 // This is a tricky case since the decoder resets the png struct for each frame,
761 // and this test verifies that it does not break the decoding of frame 0, even
762 // though it already started in the first call.
763 TEST(AnimatedPNGTests, VerifySuccessfulFirstFrameDecodeAfterLaterFrame) {
764 const char* pngFile =
765 "/LayoutTests/images/resources/"
766 "png-animated-three-independent-frames.png";
767 auto decoder = createDecoder();
768 auto fullData = readFile(pngFile);
769 ASSERT_FALSE(fullData->isEmpty());
770
771 // 160u is a randomly chosen offset in the IDAT chunk of the first frame.
772 const size_t middleFirstFrame = 160u;
773 RefPtr<SharedBuffer> data =
774 SharedBuffer::create(fullData->data(), middleFirstFrame);
775 decoder->setData(data.get(), false);
776
777 ASSERT_EQ(1u, decoder->frameCount());
778 ASSERT_EQ(ImageFrame::FramePartial,
779 decoder->frameBufferAtIndex(0)->getStatus());
780
781 decoder->setData(fullData.get(), true);
782 ASSERT_EQ(3u, decoder->frameCount());
783 ASSERT_EQ(ImageFrame::FrameComplete,
784 decoder->frameBufferAtIndex(1)->getStatus());
785 // The point is that this call does not decode frame 0, which it won't do if
786 // it does not have it as its required previous frame.
787 ASSERT_EQ(kNotFound,
788 decoder->frameBufferAtIndex(1)->requiredPreviousFrameIndex());
789
790 EXPECT_EQ(ImageFrame::FrameComplete,
791 decoder->frameBufferAtIndex(0)->getStatus());
792 EXPECT_FALSE(decoder->failed());
793 }
794
127 // Static PNG tests 795 // Static PNG tests
128 796
129 TEST(StaticPNGTests, repetitionCountTest) { 797 TEST(StaticPNGTests, repetitionCountTest) {
130 testRepetitionCount("/LayoutTests/images/resources/png-simple.png", 798 testRepetitionCount("/LayoutTests/images/resources/png-simple.png",
131 cAnimationNone); 799 cAnimationNone);
132 } 800 }
133 801
134 TEST(StaticPNGTests, sizeTest) { 802 TEST(StaticPNGTests, sizeTest) {
135 testSize("/LayoutTests/images/resources/png-simple.png", IntSize(111, 29)); 803 testSize("/LayoutTests/images/resources/png-simple.png", IntSize(111, 29));
136 } 804 }
137 805
138 TEST(StaticPNGTests, MetaDataTest) { 806 TEST(StaticPNGTests, MetaDataTest) {
139 const size_t expectedFrameCount = 1; 807 const size_t expectedFrameCount = 1;
140 const size_t expectedDuration = 0; 808 const size_t expectedDuration = 0;
141 auto decoder = 809 auto decoder =
142 createDecoderWithPngData("/LayoutTests/images/resources/png-simple.png"); 810 createDecoderWithPngData("/LayoutTests/images/resources/png-simple.png");
143 EXPECT_EQ(expectedFrameCount, decoder->frameCount()); 811 EXPECT_EQ(expectedFrameCount, decoder->frameCount());
144 EXPECT_EQ(expectedDuration, decoder->frameDurationAtIndex(0)); 812 EXPECT_EQ(expectedDuration, decoder->frameDurationAtIndex(0));
145 } 813 }
146 814
815 TEST(StaticPNGTests, InvalidIHDRChunk) {
816 testMissingDataBreaksDecoding("/LayoutTests/images/resources/png-simple.png",
817 20u, 2u);
818 }
819
147 TEST(StaticPNGTests, ProgressiveDecoding) { 820 TEST(StaticPNGTests, ProgressiveDecoding) {
148 testProgressiveDecoding(&createDecoder, 821 testProgressiveDecoding(&createDecoder,
149 "/LayoutTests/images/resources/png-simple.png", 11u); 822 "/LayoutTests/images/resources/png-simple.png", 11u);
150 } 823 }
151 824
152 TEST(StaticPNGTests, ProgressiveDecodingContinuesAfterFullData) { 825 TEST(StaticPNGTests, ProgressiveDecodingContinuesAfterFullData) {
153 testProgressiveDecodingContinuesAfterFullData( 826 testProgressiveDecodingContinuesAfterFullData(
154 "/LayoutTests/images/resources/png-simple.png", 1000u); 827 "/LayoutTests/images/resources/png-simple.png", 1000u);
155 } 828 }
156 829
(...skipping 12 matching lines...) Expand all
169 auto decoder = 842 auto decoder =
170 createDecoderWithPngData("/LayoutTests/images/resources/png-simple.png"); 843 createDecoderWithPngData("/LayoutTests/images/resources/png-simple.png");
171 EXPECT_EQ(1u, decoder->frameCount()); 844 EXPECT_EQ(1u, decoder->frameCount());
172 EXPECT_FALSE(decoder->frameIsCompleteAtIndex(0)); 845 EXPECT_FALSE(decoder->frameIsCompleteAtIndex(0));
173 EXPECT_EQ(ImageFrame::FrameComplete, 846 EXPECT_EQ(ImageFrame::FrameComplete,
174 decoder->frameBufferAtIndex(0)->getStatus()); 847 decoder->frameBufferAtIndex(0)->getStatus());
175 EXPECT_TRUE(decoder->frameIsCompleteAtIndex(0)); 848 EXPECT_TRUE(decoder->frameIsCompleteAtIndex(0));
176 } 849 }
177 850
178 }; // namespace blink 851 }; // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698