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