Home | History | Annotate | Download | only in parser
      1 // Copyright 2015 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 "core/fpdfapi/parser/fpdf_parser_decode.h"
      6 
      7 #include <cstring>
      8 #include <string>
      9 
     10 #include "core/fxcrt/fx_basic.h"
     11 #include "testing/embedder_test.h"
     12 #include "testing/fx_string_testhelpers.h"
     13 #include "testing/gtest/include/gtest/gtest.h"
     14 #include "testing/test_support.h"
     15 
     16 class FPDFParserDecodeEmbeddertest : public EmbedderTest {};
     17 
     18 // NOTE: python's zlib.compress() and zlib.decompress() may be useful for
     19 // external validation of the FlateEncode/FlateDecode test cases.
     20 
     21 TEST_F(FPDFParserDecodeEmbeddertest, FlateEncode) {
     22   pdfium::StrFuncTestData flate_encode_cases[] = {
     23       STR_IN_OUT_CASE("", "\x78\x9c\x03\x00\x00\x00\x00\x01"),
     24       STR_IN_OUT_CASE(" ", "\x78\x9c\x53\x00\x00\x00\x21\x00\x21"),
     25       STR_IN_OUT_CASE("123", "\x78\x9c\x33\x34\x32\x06\x00\01\x2d\x00\x97"),
     26       STR_IN_OUT_CASE("\x00\xff", "\x78\x9c\x63\xf8\x0f\x00\x01\x01\x01\x00"),
     27       STR_IN_OUT_CASE(
     28           "1 0 0 -1 29 763 cm\n0 0 555 735 re\nW n\nq\n0 0 555 734.394 re\n"
     29           "W n\nq\n0.8009 0 0 0.8009 0 0 cm\n1 1 1 RG 1 1 1 rg\n/G0 gs\n"
     30           "0 0 693 917 re\nf\nQ\nQ\n",
     31           "\x78\x9c\x33\x54\x30\x00\x42\x5d\x43\x05\x23\x4b\x05\x73\x33\x63"
     32           "\x85\xe4\x5c\x2e\x90\x80\xa9\xa9\xa9\x82\xb9\xb1\xa9\x42\x51\x2a"
     33           "\x57\xb8\x42\x1e\x57\x21\x92\xa0\x89\x9e\xb1\xa5\x09\x92\x84\x9e"
     34           "\x85\x81\x81\x25\xd8\x14\x24\x26\xd0\x18\x43\x05\x10\x0c\x72\x57"
     35           "\x80\x30\x8a\xd2\xb9\xf4\xdd\x0d\x14\xd2\x8b\xc1\x46\x99\x59\x1a"
     36           "\x2b\x58\x1a\x9a\x83\x8c\x49\xe3\x0a\x04\x42\x00\x37\x4c\x1b\x42"),
     37   };
     38 
     39   for (size_t i = 0; i < FX_ArraySize(flate_encode_cases); ++i) {
     40     const pdfium::StrFuncTestData& data = flate_encode_cases[i];
     41     unsigned char* buf = nullptr;
     42     unsigned int buf_size;
     43     EXPECT_TRUE(FlateEncode(data.input, data.input_size, &buf, &buf_size));
     44     ASSERT_TRUE(buf);
     45     EXPECT_EQ(std::string((const char*)data.expected, data.expected_size),
     46               std::string((const char*)buf, buf_size))
     47         << " for case " << i;
     48     FX_Free(buf);
     49   }
     50 }
     51 
     52 TEST_F(FPDFParserDecodeEmbeddertest, FlateDecode) {
     53   pdfium::DecodeTestData flate_decode_cases[] = {
     54       STR_IN_OUT_CASE("", "", 0),
     55       STR_IN_OUT_CASE("preposterous nonsense", "", 2),
     56       STR_IN_OUT_CASE("\x78\x9c\x03\x00\x00\x00\x00\x01", "", 8),
     57       STR_IN_OUT_CASE("\x78\x9c\x53\x00\x00\x00\x21\x00\x21", " ", 9),
     58       STR_IN_OUT_CASE("\x78\x9c\x33\x34\x32\x06\x00\01\x2d\x00\x97", "123", 11),
     59       STR_IN_OUT_CASE("\x78\x9c\x63\xf8\x0f\x00\x01\x01\x01\x00", "\x00\xff",
     60                       10),
     61       STR_IN_OUT_CASE(
     62           "\x78\x9c\x33\x54\x30\x00\x42\x5d\x43\x05\x23\x4b\x05\x73\x33\x63"
     63           "\x85\xe4\x5c\x2e\x90\x80\xa9\xa9\xa9\x82\xb9\xb1\xa9\x42\x51\x2a"
     64           "\x57\xb8\x42\x1e\x57\x21\x92\xa0\x89\x9e\xb1\xa5\x09\x92\x84\x9e"
     65           "\x85\x81\x81\x25\xd8\x14\x24\x26\xd0\x18\x43\x05\x10\x0c\x72\x57"
     66           "\x80\x30\x8a\xd2\xb9\xf4\xdd\x0d\x14\xd2\x8b\xc1\x46\x99\x59\x1a"
     67           "\x2b\x58\x1a\x9a\x83\x8c\x49\xe3\x0a\x04\x42\x00\x37\x4c\x1b\x42",
     68           "1 0 0 -1 29 763 cm\n0 0 555 735 re\nW n\nq\n0 0 555 734.394 re\n"
     69           "W n\nq\n0.8009 0 0 0.8009 0 0 cm\n1 1 1 RG 1 1 1 rg\n/G0 gs\n"
     70           "0 0 693 917 re\nf\nQ\nQ\n",
     71           96),
     72   };
     73 
     74   for (size_t i = 0; i < FX_ArraySize(flate_decode_cases); ++i) {
     75     const pdfium::DecodeTestData& data = flate_decode_cases[i];
     76     unsigned char* result = nullptr;
     77     unsigned int result_size;
     78     EXPECT_EQ(data.processed_size,
     79               FlateDecode(data.input, data.input_size, result, result_size))
     80         << " for case " << i;
     81     ASSERT_TRUE(result);
     82     EXPECT_EQ(std::string((const char*)data.expected, data.expected_size),
     83               std::string((const char*)result, result_size))
     84         << " for case " << i;
     85     FX_Free(result);
     86   }
     87 }
     88 
     89 TEST_F(FPDFParserDecodeEmbeddertest, Bug_552046) {
     90   // Tests specifying multiple image filters for a stream. Should not cause a
     91   // crash when rendered.
     92   EXPECT_TRUE(OpenDocument("bug_552046.pdf"));
     93   FPDF_PAGE page = LoadPage(0);
     94   FPDF_BITMAP bitmap = RenderPage(page);
     95   CompareBitmap(bitmap, 612, 792, "1940568c9ba33bac5d0b1ee9558c76b3");
     96   FPDFBitmap_Destroy(bitmap);
     97   UnloadPage(page);
     98 }
     99 
    100 TEST_F(FPDFParserDecodeEmbeddertest, Bug_555784) {
    101   // Tests bad input to the run length decoder that caused a heap overflow.
    102   // Should not cause a crash when rendered.
    103   EXPECT_TRUE(OpenDocument("bug_555784.pdf"));
    104   FPDF_PAGE page = LoadPage(0);
    105   FPDF_BITMAP bitmap = RenderPage(page);
    106   CompareBitmap(bitmap, 612, 792, "1940568c9ba33bac5d0b1ee9558c76b3");
    107   FPDFBitmap_Destroy(bitmap);
    108   UnloadPage(page);
    109 }
    110 
    111 TEST_F(FPDFParserDecodeEmbeddertest, Bug_455199) {
    112   // Tests object numbers with a value > 01000000.
    113   // Should open successfully.
    114   EXPECT_TRUE(OpenDocument("bug_455199.pdf"));
    115   FPDF_PAGE page = LoadPage(0);
    116   FPDF_BITMAP bitmap = RenderPage(page);
    117 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
    118   // Rendering on Mac is inconsistent, so skip this check.
    119   const char* const kExpectedMd5sum = nullptr;
    120 #else
    121   const char kExpectedMd5sum[] = "6f9f0fd903da177babb24dd50a806a56";
    122 #endif
    123   CompareBitmap(bitmap, 200, 200, kExpectedMd5sum);
    124   FPDFBitmap_Destroy(bitmap);
    125   UnloadPage(page);
    126 }
    127