Home | History | Annotate | Download | only in fpdfsdk
      1 // Copyright 2016 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 #include <string>
      5 
      6 #include "public/fpdf_edit.h"
      7 #include "public/fpdf_ppo.h"
      8 #include "public/fpdf_save.h"
      9 #include "public/fpdfview.h"
     10 #include "testing/embedder_test.h"
     11 #include "testing/gtest/include/gtest/gtest.h"
     12 #include "testing/test_support.h"
     13 
     14 namespace {
     15 
     16 class FPDFPPOEmbeddertest : public EmbedderTest {};
     17 
     18 int FakeBlockWriter(FPDF_FILEWRITE* pThis,
     19                     const void* pData,
     20                     unsigned long size) {
     21   return size;
     22 }
     23 
     24 }  // namespace
     25 
     26 TEST_F(FPDFPPOEmbeddertest, NoViewerPreferences) {
     27   EXPECT_TRUE(OpenDocument("hello_world.pdf"));
     28 
     29   FPDF_DOCUMENT output_doc = FPDF_CreateNewDocument();
     30   EXPECT_TRUE(output_doc);
     31   EXPECT_FALSE(FPDF_CopyViewerPreferences(output_doc, document()));
     32   FPDF_CloseDocument(output_doc);
     33 }
     34 
     35 TEST_F(FPDFPPOEmbeddertest, ViewerPreferences) {
     36   EXPECT_TRUE(OpenDocument("viewer_ref.pdf"));
     37 
     38   FPDF_DOCUMENT output_doc = FPDF_CreateNewDocument();
     39   EXPECT_TRUE(output_doc);
     40   EXPECT_TRUE(FPDF_CopyViewerPreferences(output_doc, document()));
     41   FPDF_CloseDocument(output_doc);
     42 }
     43 
     44 TEST_F(FPDFPPOEmbeddertest, ImportPages) {
     45   ASSERT_TRUE(OpenDocument("viewer_ref.pdf"));
     46 
     47   FPDF_PAGE page = LoadPage(0);
     48   EXPECT_TRUE(page);
     49 
     50   FPDF_DOCUMENT output_doc = FPDF_CreateNewDocument();
     51   ASSERT_TRUE(output_doc);
     52   EXPECT_TRUE(FPDF_CopyViewerPreferences(output_doc, document()));
     53   EXPECT_TRUE(FPDF_ImportPages(output_doc, document(), "1", 0));
     54   EXPECT_EQ(1, FPDF_GetPageCount(output_doc));
     55   FPDF_CloseDocument(output_doc);
     56 
     57   UnloadPage(page);
     58 }
     59 
     60 TEST_F(FPDFPPOEmbeddertest, BadRepeatViewerPref) {
     61   ASSERT_TRUE(OpenDocument("repeat_viewer_ref.pdf"));
     62 
     63   FPDF_DOCUMENT output_doc = FPDF_CreateNewDocument();
     64   EXPECT_TRUE(output_doc);
     65   EXPECT_TRUE(FPDF_CopyViewerPreferences(output_doc, document()));
     66 
     67   FPDF_FILEWRITE writer;
     68   writer.version = 1;
     69   writer.WriteBlock = FakeBlockWriter;
     70 
     71   EXPECT_TRUE(FPDF_SaveAsCopy(output_doc, &writer, 0));
     72   FPDF_CloseDocument(output_doc);
     73 }
     74 
     75 TEST_F(FPDFPPOEmbeddertest, BadCircularViewerPref) {
     76   ASSERT_TRUE(OpenDocument("circular_viewer_ref.pdf"));
     77 
     78   FPDF_DOCUMENT output_doc = FPDF_CreateNewDocument();
     79   EXPECT_TRUE(output_doc);
     80   EXPECT_TRUE(FPDF_CopyViewerPreferences(output_doc, document()));
     81 
     82   FPDF_FILEWRITE writer;
     83   writer.version = 1;
     84   writer.WriteBlock = FakeBlockWriter;
     85 
     86   EXPECT_TRUE(FPDF_SaveAsCopy(output_doc, &writer, 0));
     87   FPDF_CloseDocument(output_doc);
     88 }
     89 
     90 TEST_F(FPDFPPOEmbeddertest, BadRanges) {
     91   EXPECT_TRUE(OpenDocument("viewer_ref.pdf"));
     92 
     93   FPDF_PAGE page = LoadPage(0);
     94   EXPECT_TRUE(page);
     95 
     96   FPDF_DOCUMENT output_doc = FPDF_CreateNewDocument();
     97   EXPECT_TRUE(output_doc);
     98   EXPECT_FALSE(FPDF_ImportPages(output_doc, document(), "clams", 0));
     99   EXPECT_FALSE(FPDF_ImportPages(output_doc, document(), "0", 0));
    100   EXPECT_FALSE(FPDF_ImportPages(output_doc, document(), "42", 0));
    101   EXPECT_FALSE(FPDF_ImportPages(output_doc, document(), "1,2", 0));
    102   EXPECT_FALSE(FPDF_ImportPages(output_doc, document(), "1-2", 0));
    103   EXPECT_FALSE(FPDF_ImportPages(output_doc, document(), ",1", 0));
    104   EXPECT_FALSE(FPDF_ImportPages(output_doc, document(), "1,", 0));
    105   EXPECT_FALSE(FPDF_ImportPages(output_doc, document(), "1-", 0));
    106   EXPECT_FALSE(FPDF_ImportPages(output_doc, document(), "-1", 0));
    107   EXPECT_FALSE(FPDF_ImportPages(output_doc, document(), "-,0,,,1-", 0));
    108   FPDF_CloseDocument(output_doc);
    109 
    110   UnloadPage(page);
    111 }
    112 
    113 TEST_F(FPDFPPOEmbeddertest, GoodRanges) {
    114   EXPECT_TRUE(OpenDocument("viewer_ref.pdf"));
    115 
    116   FPDF_PAGE page = LoadPage(0);
    117   EXPECT_TRUE(page);
    118 
    119   FPDF_DOCUMENT output_doc = FPDF_CreateNewDocument();
    120   EXPECT_TRUE(output_doc);
    121   EXPECT_TRUE(FPDF_CopyViewerPreferences(output_doc, document()));
    122   EXPECT_TRUE(FPDF_ImportPages(output_doc, document(), "1,1,1,1", 0));
    123   EXPECT_TRUE(FPDF_ImportPages(output_doc, document(), "1-1", 0));
    124   EXPECT_EQ(5, FPDF_GetPageCount(output_doc));
    125   FPDF_CloseDocument(output_doc);
    126 
    127   UnloadPage(page);
    128 }
    129 
    130 TEST_F(FPDFPPOEmbeddertest, BUG_664284) {
    131   EXPECT_TRUE(OpenDocument("bug_664284.pdf"));
    132 
    133   FPDF_PAGE page = LoadPage(0);
    134   ASSERT_NE(nullptr, page);
    135 
    136   FPDF_DOCUMENT output_doc = FPDF_CreateNewDocument();
    137   EXPECT_TRUE(output_doc);
    138   EXPECT_TRUE(FPDF_ImportPages(output_doc, document(), "1", 0));
    139   FPDF_CloseDocument(output_doc);
    140 
    141   UnloadPage(page);
    142 }
    143 
    144 TEST_F(FPDFPPOEmbeddertest, BUG_750568) {
    145   const char* const kHashes[] = {
    146       "64ad08132a1c5a166768298c8a578f57", "83b83e2f6bc80707d0a917c7634140b9",
    147       "913cd3723a451e4e46fbc2c05702d1ee", "81fb7cfd4860f855eb468f73dfeb6d60"};
    148 
    149   ASSERT_TRUE(OpenDocument("bug_750568.pdf"));
    150   ASSERT_EQ(4, FPDF_GetPageCount(document()));
    151 
    152   for (size_t i = 0; i < 4; ++i) {
    153     FPDF_PAGE page = LoadPage(i);
    154     ASSERT_NE(nullptr, page);
    155 
    156     FPDF_BITMAP bitmap = RenderPage(page);
    157     ASSERT_EQ(200, FPDFBitmap_GetWidth(bitmap));
    158     ASSERT_EQ(200, FPDFBitmap_GetHeight(bitmap));
    159     ASSERT_EQ(800, FPDFBitmap_GetStride(bitmap));
    160 
    161     std::string digest = HashBitmap(bitmap);
    162     FPDFBitmap_Destroy(bitmap);
    163     UnloadPage(page);
    164     EXPECT_EQ(kHashes[i], digest);
    165   }
    166 
    167   FPDF_DOCUMENT output_doc = FPDF_CreateNewDocument();
    168   ASSERT_TRUE(output_doc);
    169   EXPECT_TRUE(FPDF_ImportPages(output_doc, document(), "1,2,3,4", 0));
    170   ASSERT_EQ(4, FPDF_GetPageCount(output_doc));
    171   for (size_t i = 0; i < 4; ++i) {
    172     FPDF_PAGE page = FPDF_LoadPage(output_doc, i);
    173     ASSERT_NE(nullptr, page);
    174 
    175     FPDF_BITMAP bitmap = RenderPage(page);
    176     ASSERT_EQ(200, FPDFBitmap_GetWidth(bitmap));
    177     ASSERT_EQ(200, FPDFBitmap_GetHeight(bitmap));
    178     ASSERT_EQ(800, FPDFBitmap_GetStride(bitmap));
    179 
    180     std::string digest = HashBitmap(bitmap);
    181     FPDFBitmap_Destroy(bitmap);
    182     FPDF_ClosePage(page);
    183     EXPECT_EQ(kHashes[i], digest);
    184   }
    185   FPDF_CloseDocument(output_doc);
    186 }
    187 
    188 TEST_F(FPDFPPOEmbeddertest, ImportWithZeroLengthStream) {
    189   EXPECT_TRUE(OpenDocument("zero_length_stream.pdf"));
    190   FPDF_PAGE page = LoadPage(0);
    191   ASSERT_NE(nullptr, page);
    192 
    193   FPDF_BITMAP bitmap = RenderPage(page);
    194   ASSERT_EQ(200, FPDFBitmap_GetWidth(bitmap));
    195   ASSERT_EQ(200, FPDFBitmap_GetHeight(bitmap));
    196   ASSERT_EQ(800, FPDFBitmap_GetStride(bitmap));
    197 
    198   std::string digest = HashBitmap(bitmap);
    199   FPDFBitmap_Destroy(bitmap);
    200   FPDF_ClosePage(page);
    201 
    202   FPDF_DOCUMENT new_doc = FPDF_CreateNewDocument();
    203   EXPECT_TRUE(new_doc);
    204   EXPECT_TRUE(FPDF_ImportPages(new_doc, document(), "1", 0));
    205 
    206   EXPECT_EQ(1, FPDF_GetPageCount(new_doc));
    207   FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
    208   ASSERT_NE(nullptr, new_page);
    209   FPDF_BITMAP new_bitmap = RenderPage(new_page);
    210   ASSERT_EQ(200, FPDFBitmap_GetWidth(new_bitmap));
    211   ASSERT_EQ(200, FPDFBitmap_GetHeight(new_bitmap));
    212   ASSERT_EQ(800, FPDFBitmap_GetStride(new_bitmap));
    213 
    214   std::string new_digest = HashBitmap(new_bitmap);
    215   FPDFBitmap_Destroy(new_bitmap);
    216   FPDF_ClosePage(new_page);
    217   FPDF_CloseDocument(new_doc);
    218 
    219   EXPECT_EQ(digest, new_digest);
    220 }
    221