diff options
author | halcanary <halcanary@google.com> | 2016-09-09 11:41:59 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-09-09 11:41:59 -0700 |
commit | 57f744e3030fec4d1a2b3e9119011904b149a4da (patch) | |
tree | 62e2dbe5a2550e0fccfcf085663ada6a8a3d8756 /tests/PDFJpegEmbedTest.cpp | |
parent | 8d914908d8ad37a73c39ba8f7ef298bfee457388 (diff) |
SkPDF/Tests: imporve test coverage.
Also: make sure that all SkPDF unit tests are named SkPDF_* to
make testing changes to SkPDF easier. Other cleanup.
Add test: SkPDF_pdfa_document to verify that flag in public API
works.
SkPDF_JpegIdentification test: test slightly malformed JPEGs to
verify that all code paths work.
BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2322133003
Review-Url: https://codereview.chromium.org/2322133003
Diffstat (limited to 'tests/PDFJpegEmbedTest.cpp')
-rw-r--r-- | tests/PDFJpegEmbedTest.cpp | 110 |
1 files changed, 103 insertions, 7 deletions
diff --git a/tests/PDFJpegEmbedTest.cpp b/tests/PDFJpegEmbedTest.cpp index b199c80e75..93c204401d 100644 --- a/tests/PDFJpegEmbedTest.cpp +++ b/tests/PDFJpegEmbedTest.cpp @@ -5,10 +5,11 @@ * found in the LICENSE file. */ -#include "SkDocument.h" #include "SkCanvas.h" -#include "SkImageGenerator.h" #include "SkData.h" +#include "SkDocument.h" +#include "SkImageGenerator.h" +#include "SkJpegInfo.h" #include "SkStream.h" #include "Resources.h" @@ -54,8 +55,9 @@ static sk_sp<SkData> load_resource( * directly embedded into the PDF (without re-encoding) when that * makes sense. */ -DEF_TEST(PDFJpegEmbedTest, r) { - const char test[] = "PDFJpegEmbedTest"; +DEF_TEST(SkPDF_JpegEmbedTest, r) { + REQUIRE_PDF_DOCUMENT(SkPDF_JpegEmbedTest, r); + const char test[] = "SkPDF_JpegEmbedTest"; sk_sp<SkData> mandrillData(load_resource(r, test, "mandrill_512_q075.jpg")); sk_sp<SkData> cmykData(load_resource(r, test, "CMYK.jpg")); if (!mandrillData || !cmykData) { @@ -111,9 +113,7 @@ DEF_TEST(PDFJpegEmbedTest, r) { REPORTER_ASSERT(r, !is_subset_of(cmykData.get(), pdfData.get())); } -#include "SkJpegInfo.h" - -DEF_TEST(JpegIdentification, r) { +DEF_TEST(SkPDF_JpegIdentification, r) { static struct { const char* path; bool isJfif; @@ -144,4 +144,100 @@ DEF_TEST(JpegIdentification, r) { INFOF(r, "\nJpegIdentification: %s [%d x %d]\n", kTests[i].path, info.fSize.width(), info.fSize.height()); } + + // Test several malformed jpegs. + SkJFIFInfo info; + { + static const char goodJpeg[] = + "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7" + "\6\5\10\7\7\7\t\t\10\n\14\24\r\14\13\13\14\31\22\23\17\24\35\32\37" + "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t" + "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222" + "22222222222222\377\300\0\21\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001"; + size_t goodJpegLength = 177; + auto data = SkData::MakeWithoutCopy(goodJpeg, goodJpegLength); + REPORTER_ASSERT(r, SkIsJFIF(data.get(), &info)); + REPORTER_ASSERT(r, info.fSize == SkISize::Make(512, 512)); + REPORTER_ASSERT(r, info.fType == SkJFIFInfo::kYCbCr); + + // Not long enough to read first (SOI) segment marker. + data = SkData::MakeWithoutCopy(goodJpeg, 1); + REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info)); + + // Not long enough to read second segment (APP0) marker. + data = SkData::MakeWithoutCopy(goodJpeg, 3); + REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info)); + + // Not long enough to read second segment's length. + data = SkData::MakeWithoutCopy(goodJpeg, 5); + REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info)); + + // APP0 segment is truncated. + data = SkData::MakeWithoutCopy(goodJpeg, 7); + REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info)); + + // Missing SOF segment. + data = SkData::MakeWithoutCopy(goodJpeg, 89); + REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info)); + } + { + // JFIF tag missing. + static const char jpeg[] = + "\377\330\377\340\0\20JFIX\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7" + "\6\5\10\7\7\7\t\t\10\n\14\24\r\14\13\13\14\31\22\23\17\24\35\32\37" + "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t" + "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222" + "22222222222222\377\300\0\21\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001"; + size_t jpegLength = 177; + auto data = SkData::MakeWithoutCopy(jpeg, jpegLength); + REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info)); + } + { + // APP0 segment short (byte 6 changed). + static const char jpeg[] = + "\377\330\377\340\0\5JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7" + "\6\5\10\7\7\7\t\t\10\n\14\24\r\14\13\13\14\31\22\23\17\24\35\32\37" + "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t" + "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222" + "22222222222222\377\300\0\21\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001"; + size_t jpegLength = 177; + auto data = SkData::MakeWithoutCopy(jpeg, jpegLength); + REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info)); + } + { + // SOF segment short. ('\21' replaced with '\5') + static const char jpeg[] = + "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7" + "\6\5\10\7\7\7\t\t\10\n\14\24\r\14\13\13\14\31\22\23\17\24\35\32\37" + "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t" + "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222" + "22222222222222\377\300\0\5\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001"; + size_t jpegLength = 177; + auto data = SkData::MakeWithoutCopy(jpeg, jpegLength); + REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info)); + } + { + // Unsupported 12-bit components. ('\10' replaced with '\14') + static const char jpeg[] = + "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7" + "\6\5\10\7\7\7\t\t\10\n\14\24\r\14\13\13\14\31\22\23\17\24\35\32\37" + "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t" + "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222" + "22222222222222\377\300\0\21\14\2\0\2\0\3\1\"\0\2\21\1\3\21\001"; + size_t jpegLength = 177; + auto data = SkData::MakeWithoutCopy(jpeg, jpegLength); + REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info)); + } + { + // Two color channels. ('\3' replaced with '\2') + static const char jpeg[] = + "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7" + "\6\5\10\7\7\7\t\t\10\n\14\24\r\14\13\13\14\31\22\23\17\24\35\32\37" + "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t" + "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222" + "22222222222222\377\300\0\21\10\2\0\2\0\2\1\"\0\2\21\1\3\21\001"; + size_t jpegLength = 177; + auto data = SkData::MakeWithoutCopy(jpeg, jpegLength); + REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info)); + } } |