Wei Li | 5227e57 | 2016-03-04 15:49:17 -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_doc.h" |
| 6 | |
| 7 | #include <memory> |
| 8 | #include <vector> |
| 9 | |
dsinclair | 39c62fd | 2016-09-29 12:49:17 -0700 | [diff] [blame] | 10 | #include "core/fpdfapi/cpdf_modulemgr.h" |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 11 | #include "core/fpdfapi/parser/cpdf_document.h" |
| 12 | #include "core/fpdfapi/parser/cpdf_name.h" |
| 13 | #include "core/fpdfapi/parser/cpdf_number.h" |
| 14 | #include "core/fpdfapi/parser/cpdf_parser.h" |
| 15 | #include "core/fpdfapi/parser/cpdf_reference.h" |
| 16 | #include "core/fpdfapi/parser/cpdf_string.h" |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 17 | #include "testing/gtest/include/gtest/gtest.h" |
| 18 | #include "testing/test_support.h" |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 19 | #include "third_party/base/ptr_util.h" |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 20 | |
| 21 | #ifdef PDF_ENABLE_XFA |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 22 | #include "fpdfsdk/fpdfxfa/cpdfxfa_context.h" |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 23 | #endif // PDF_ENABLE_XFA |
| 24 | |
| 25 | class CPDF_TestDocument : public CPDF_Document { |
| 26 | public: |
tsepez | e5cb0b1 | 2016-10-26 15:06:11 -0700 | [diff] [blame] | 27 | CPDF_TestDocument() : CPDF_Document(nullptr) {} |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 28 | |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 29 | void SetRoot(CPDF_Dictionary* root) { m_pRootDict = root; } |
| 30 | CPDF_IndirectObjectHolder* GetHolder() { return this; } |
| 31 | }; |
| 32 | |
| 33 | #ifdef PDF_ENABLE_XFA |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 34 | class CPDF_TestXFAContext : public CPDFXFA_Context { |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 35 | public: |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 36 | CPDF_TestXFAContext() |
| 37 | : CPDFXFA_Context(pdfium::MakeUnique<CPDF_TestDocument>()) {} |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 38 | |
| 39 | void SetRoot(CPDF_Dictionary* root) { |
| 40 | reinterpret_cast<CPDF_TestDocument*>(GetPDFDoc())->SetRoot(root); |
| 41 | } |
| 42 | |
| 43 | CPDF_IndirectObjectHolder* GetHolder() { return GetPDFDoc(); } |
| 44 | }; |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 45 | using CPDF_TestPdfDocument = CPDF_TestXFAContext; |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 46 | #else // PDF_ENABLE_XFA |
| 47 | using CPDF_TestPdfDocument = CPDF_TestDocument; |
| 48 | #endif // PDF_ENABLE_XFA |
| 49 | |
| 50 | class PDFDocTest : public testing::Test { |
| 51 | public: |
| 52 | struct DictObjInfo { |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 53 | uint32_t num; |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 54 | CPDF_Dictionary* obj; |
| 55 | }; |
| 56 | |
| 57 | void SetUp() override { |
| 58 | // We don't need page module or render module, but |
| 59 | // initialize them to keep the code sane. |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 60 | CPDF_ModuleMgr* module_mgr = CPDF_ModuleMgr::Get(); |
| 61 | module_mgr->InitPageModule(); |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 62 | |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 63 | m_pDoc = pdfium::MakeUnique<CPDF_TestPdfDocument>(); |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 64 | m_pIndirectObjs = m_pDoc->GetHolder(); |
| 65 | // Setup the root directory. |
tsepez | cd5bca4 | 2016-09-30 10:45:06 -0700 | [diff] [blame] | 66 | m_pRootObj.reset(new CPDF_Dictionary()); |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 67 | m_pDoc->SetRoot(m_pRootObj.get()); |
| 68 | } |
| 69 | |
thestig | fd36b8f | 2016-07-11 10:43:48 -0700 | [diff] [blame] | 70 | void TearDown() override { |
| 71 | m_pRootObj.reset(); |
| 72 | m_pIndirectObjs = nullptr; |
| 73 | m_pDoc.reset(); |
| 74 | CPDF_ModuleMgr::Destroy(); |
| 75 | } |
| 76 | |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 77 | std::vector<DictObjInfo> CreateDictObjs(int num) { |
| 78 | std::vector<DictObjInfo> info; |
| 79 | for (int i = 0; i < num; ++i) { |
| 80 | // Objects created will be released by the document. |
Tom Sepez | c25a421 | 2016-10-14 17:45:56 -0700 | [diff] [blame] | 81 | CPDF_Dictionary* obj = new CPDF_Dictionary(); |
| 82 | info.push_back({m_pIndirectObjs->AddIndirectObject(obj), obj}); |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 83 | } |
| 84 | return info; |
| 85 | } |
| 86 | |
| 87 | protected: |
| 88 | std::unique_ptr<CPDF_TestPdfDocument> m_pDoc; |
| 89 | CPDF_IndirectObjectHolder* m_pIndirectObjs; |
tsepez | 33fdebc | 2016-11-04 11:38:40 -0700 | [diff] [blame^] | 90 | std::unique_ptr<CPDF_Dictionary> m_pRootObj; |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | TEST_F(PDFDocTest, FindBookmark) { |
| 94 | { |
| 95 | // No bookmark information. |
| 96 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> title = |
| 97 | GetFPDFWideString(L""); |
| 98 | EXPECT_EQ(nullptr, FPDFBookmark_Find(m_pDoc.get(), title.get())); |
| 99 | |
| 100 | title = GetFPDFWideString(L"Preface"); |
| 101 | EXPECT_EQ(nullptr, FPDFBookmark_Find(m_pDoc.get(), title.get())); |
| 102 | } |
| 103 | { |
| 104 | // Empty bookmark tree. |
tsepez | cd5bca4 | 2016-09-30 10:45:06 -0700 | [diff] [blame] | 105 | m_pRootObj->SetFor("Outlines", new CPDF_Dictionary()); |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 106 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> title = |
| 107 | GetFPDFWideString(L""); |
| 108 | EXPECT_EQ(nullptr, FPDFBookmark_Find(m_pDoc.get(), title.get())); |
| 109 | |
| 110 | title = GetFPDFWideString(L"Preface"); |
| 111 | EXPECT_EQ(nullptr, FPDFBookmark_Find(m_pDoc.get(), title.get())); |
| 112 | } |
| 113 | { |
| 114 | // Check on a regular bookmark tree. |
| 115 | auto bookmarks = CreateDictObjs(3); |
| 116 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 117 | bookmarks[1].obj->SetFor("Title", new CPDF_String(L"Chapter 1")); |
| 118 | bookmarks[1].obj->SetFor( |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 119 | "Parent", new CPDF_Reference(m_pIndirectObjs, bookmarks[0].num)); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 120 | bookmarks[1].obj->SetFor( |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 121 | "Next", new CPDF_Reference(m_pIndirectObjs, bookmarks[2].num)); |
| 122 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 123 | bookmarks[2].obj->SetFor("Title", new CPDF_String(L"Chapter 2")); |
| 124 | bookmarks[2].obj->SetFor( |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 125 | "Parent", new CPDF_Reference(m_pIndirectObjs, bookmarks[0].num)); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 126 | bookmarks[2].obj->SetFor( |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 127 | "Prev", new CPDF_Reference(m_pIndirectObjs, bookmarks[1].num)); |
| 128 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 129 | bookmarks[0].obj->SetFor("Type", new CPDF_Name("Outlines")); |
| 130 | bookmarks[0].obj->SetFor("Count", new CPDF_Number(2)); |
| 131 | bookmarks[0].obj->SetFor( |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 132 | "First", new CPDF_Reference(m_pIndirectObjs, bookmarks[1].num)); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 133 | bookmarks[0].obj->SetFor( |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 134 | "Last", new CPDF_Reference(m_pIndirectObjs, bookmarks[2].num)); |
| 135 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 136 | m_pRootObj->SetFor("Outlines", |
| 137 | new CPDF_Reference(m_pIndirectObjs, bookmarks[0].num)); |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 138 | |
| 139 | // Title with no match. |
| 140 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> title = |
| 141 | GetFPDFWideString(L"Chapter 3"); |
| 142 | EXPECT_EQ(nullptr, FPDFBookmark_Find(m_pDoc.get(), title.get())); |
| 143 | |
| 144 | // Title with partial match only. |
| 145 | title = GetFPDFWideString(L"Chapter"); |
| 146 | EXPECT_EQ(nullptr, FPDFBookmark_Find(m_pDoc.get(), title.get())); |
| 147 | |
| 148 | // Title with a match. |
| 149 | title = GetFPDFWideString(L"Chapter 2"); |
| 150 | EXPECT_EQ(bookmarks[2].obj, FPDFBookmark_Find(m_pDoc.get(), title.get())); |
| 151 | |
| 152 | // Title match is case insensitive. |
| 153 | title = GetFPDFWideString(L"cHaPter 2"); |
| 154 | EXPECT_EQ(bookmarks[2].obj, FPDFBookmark_Find(m_pDoc.get(), title.get())); |
| 155 | } |
| 156 | { |
| 157 | // Circular bookmarks in depth. |
| 158 | auto bookmarks = CreateDictObjs(3); |
| 159 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 160 | bookmarks[1].obj->SetFor("Title", new CPDF_String(L"Chapter 1")); |
| 161 | bookmarks[1].obj->SetFor( |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 162 | "Parent", new CPDF_Reference(m_pIndirectObjs, bookmarks[0].num)); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 163 | bookmarks[1].obj->SetFor( |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 164 | "First", new CPDF_Reference(m_pIndirectObjs, bookmarks[2].num)); |
| 165 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 166 | bookmarks[2].obj->SetFor("Title", new CPDF_String(L"Chapter 2")); |
| 167 | bookmarks[2].obj->SetFor( |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 168 | "Parent", new CPDF_Reference(m_pIndirectObjs, bookmarks[1].num)); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 169 | bookmarks[2].obj->SetFor( |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 170 | "First", new CPDF_Reference(m_pIndirectObjs, bookmarks[1].num)); |
| 171 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 172 | bookmarks[0].obj->SetFor("Type", new CPDF_Name("Outlines")); |
| 173 | bookmarks[0].obj->SetFor("Count", new CPDF_Number(2)); |
| 174 | bookmarks[0].obj->SetFor( |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 175 | "First", new CPDF_Reference(m_pIndirectObjs, bookmarks[1].num)); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 176 | bookmarks[0].obj->SetFor( |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 177 | "Last", new CPDF_Reference(m_pIndirectObjs, bookmarks[2].num)); |
| 178 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 179 | m_pRootObj->SetFor("Outlines", |
| 180 | new CPDF_Reference(m_pIndirectObjs, bookmarks[0].num)); |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 181 | |
| 182 | // Title with no match. |
| 183 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> title = |
| 184 | GetFPDFWideString(L"Chapter 3"); |
| 185 | EXPECT_EQ(nullptr, FPDFBookmark_Find(m_pDoc.get(), title.get())); |
| 186 | |
| 187 | // Title with a match. |
| 188 | title = GetFPDFWideString(L"Chapter 2"); |
| 189 | EXPECT_EQ(bookmarks[2].obj, FPDFBookmark_Find(m_pDoc.get(), title.get())); |
| 190 | } |
| 191 | { |
| 192 | // Circular bookmarks in breadth. |
| 193 | auto bookmarks = CreateDictObjs(4); |
| 194 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 195 | bookmarks[1].obj->SetFor("Title", new CPDF_String(L"Chapter 1")); |
| 196 | bookmarks[1].obj->SetFor( |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 197 | "Parent", new CPDF_Reference(m_pIndirectObjs, bookmarks[0].num)); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 198 | bookmarks[1].obj->SetFor( |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 199 | "Next", new CPDF_Reference(m_pIndirectObjs, bookmarks[2].num)); |
| 200 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 201 | bookmarks[2].obj->SetFor("Title", new CPDF_String(L"Chapter 2")); |
| 202 | bookmarks[2].obj->SetFor( |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 203 | "Parent", new CPDF_Reference(m_pIndirectObjs, bookmarks[0].num)); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 204 | bookmarks[2].obj->SetFor( |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 205 | "Next", new CPDF_Reference(m_pIndirectObjs, bookmarks[3].num)); |
| 206 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 207 | bookmarks[3].obj->SetFor("Title", new CPDF_String(L"Chapter 3")); |
| 208 | bookmarks[3].obj->SetFor( |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 209 | "Parent", new CPDF_Reference(m_pIndirectObjs, bookmarks[0].num)); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 210 | bookmarks[3].obj->SetFor( |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 211 | "Next", new CPDF_Reference(m_pIndirectObjs, bookmarks[1].num)); |
| 212 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 213 | bookmarks[0].obj->SetFor("Type", new CPDF_Name("Outlines")); |
| 214 | bookmarks[0].obj->SetFor("Count", new CPDF_Number(2)); |
| 215 | bookmarks[0].obj->SetFor( |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 216 | "First", new CPDF_Reference(m_pIndirectObjs, bookmarks[1].num)); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 217 | bookmarks[0].obj->SetFor( |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 218 | "Last", new CPDF_Reference(m_pIndirectObjs, bookmarks[2].num)); |
| 219 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 220 | m_pRootObj->SetFor("Outlines", |
| 221 | new CPDF_Reference(m_pIndirectObjs, bookmarks[0].num)); |
Wei Li | 5227e57 | 2016-03-04 15:49:17 -0800 | [diff] [blame] | 222 | |
| 223 | // Title with no match. |
| 224 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> title = |
| 225 | GetFPDFWideString(L"Chapter 8"); |
| 226 | EXPECT_EQ(nullptr, FPDFBookmark_Find(m_pDoc.get(), title.get())); |
| 227 | |
| 228 | // Title with a match. |
| 229 | title = GetFPDFWideString(L"Chapter 3"); |
| 230 | EXPECT_EQ(bookmarks[3].obj, FPDFBookmark_Find(m_pDoc.get(), title.get())); |
| 231 | } |
| 232 | } |