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