tsepez | 211d4ed | 2016-11-11 17:23:48 -0800 | [diff] [blame] | 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 | |
| 5 | #include "public/fpdf_ppo.h" |
| 6 | |
| 7 | #include "core/fxcrt/fx_basic.h" |
| 8 | #include "public/fpdf_edit.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 | |
Wei Li | 6bdd824 | 2017-03-23 09:45:04 -0700 | [diff] [blame^] | 18 | int FakeBlockWriter(FPDF_FILEWRITE* pThis, |
| 19 | const void* pData, |
| 20 | unsigned long size) { |
| 21 | return size; |
| 22 | } |
| 23 | |
tsepez | 211d4ed | 2016-11-11 17:23:48 -0800 | [diff] [blame] | 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) { |
Wei Li | 6bdd824 | 2017-03-23 09:45:04 -0700 | [diff] [blame^] | 45 | ASSERT_TRUE(OpenDocument("viewer_ref.pdf")); |
tsepez | 211d4ed | 2016-11-11 17:23:48 -0800 | [diff] [blame] | 46 | |
| 47 | FPDF_PAGE page = LoadPage(0); |
| 48 | EXPECT_TRUE(page); |
| 49 | |
| 50 | FPDF_DOCUMENT output_doc = FPDF_CreateNewDocument(); |
Wei Li | 6bdd824 | 2017-03-23 09:45:04 -0700 | [diff] [blame^] | 51 | ASSERT_TRUE(output_doc); |
tsepez | 211d4ed | 2016-11-11 17:23:48 -0800 | [diff] [blame] | 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 | |
Wei Li | 6bdd824 | 2017-03-23 09:45:04 -0700 | [diff] [blame^] | 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 | |
tsepez | 211d4ed | 2016-11-11 17:23:48 -0800 | [diff] [blame] | 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 | } |
tsepez | 6e1d603 | 2016-11-11 17:55:40 -0800 | [diff] [blame] | 129 | |
| 130 | TEST_F(FPDFPPOEmbeddertest, BUG_664284) { |
| 131 | EXPECT_TRUE(OpenDocument("bug_664284.pdf")); |
| 132 | |
| 133 | FPDF_PAGE page = LoadPage(0); |
| 134 | EXPECT_TRUE(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 | } |