blob: 6409f70fe2601f8025b794329b364475a92b0cde [file] [log] [blame]
Wei Li5227e572016-03-04 15:49:17 -08001// 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
dsinclair39c62fd2016-09-29 12:49:17 -070010#include "core/fpdfapi/cpdf_modulemgr.h"
dsinclairc59fa882016-11-08 06:55:40 -080011#include "core/fpdfapi/parser/cpdf_array.h"
dsinclair488b7ad2016-10-04 11:55:50 -070012#include "core/fpdfapi/parser/cpdf_document.h"
13#include "core/fpdfapi/parser/cpdf_name.h"
dsinclairc59fa882016-11-08 06:55:40 -080014#include "core/fpdfapi/parser/cpdf_null.h"
dsinclair488b7ad2016-10-04 11:55:50 -070015#include "core/fpdfapi/parser/cpdf_number.h"
16#include "core/fpdfapi/parser/cpdf_parser.h"
17#include "core/fpdfapi/parser/cpdf_reference.h"
18#include "core/fpdfapi/parser/cpdf_string.h"
dsinclairc59fa882016-11-08 06:55:40 -080019#include "core/fpdfdoc/cpdf_dest.h"
Wei Li5227e572016-03-04 15:49:17 -080020#include "testing/gtest/include/gtest/gtest.h"
21#include "testing/test_support.h"
tsepez36eb4bd2016-10-03 15:24:27 -070022#include "third_party/base/ptr_util.h"
Wei Li5227e572016-03-04 15:49:17 -080023
24#ifdef PDF_ENABLE_XFA
dsinclair521b7502016-11-02 13:02:28 -070025#include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
Wei Li5227e572016-03-04 15:49:17 -080026#endif // PDF_ENABLE_XFA
27
28class CPDF_TestDocument : public CPDF_Document {
29 public:
tsepeze5cb0b12016-10-26 15:06:11 -070030 CPDF_TestDocument() : CPDF_Document(nullptr) {}
thestig931bf372016-04-26 22:24:30 -070031
Wei Li5227e572016-03-04 15:49:17 -080032 void SetRoot(CPDF_Dictionary* root) { m_pRootDict = root; }
33 CPDF_IndirectObjectHolder* GetHolder() { return this; }
34};
35
36#ifdef PDF_ENABLE_XFA
dsinclair521b7502016-11-02 13:02:28 -070037class CPDF_TestXFAContext : public CPDFXFA_Context {
Wei Li5227e572016-03-04 15:49:17 -080038 public:
dsinclair521b7502016-11-02 13:02:28 -070039 CPDF_TestXFAContext()
40 : CPDFXFA_Context(pdfium::MakeUnique<CPDF_TestDocument>()) {}
Wei Li5227e572016-03-04 15:49:17 -080041
42 void SetRoot(CPDF_Dictionary* root) {
43 reinterpret_cast<CPDF_TestDocument*>(GetPDFDoc())->SetRoot(root);
44 }
45
46 CPDF_IndirectObjectHolder* GetHolder() { return GetPDFDoc(); }
47};
dsinclair521b7502016-11-02 13:02:28 -070048using CPDF_TestPdfDocument = CPDF_TestXFAContext;
Wei Li5227e572016-03-04 15:49:17 -080049#else // PDF_ENABLE_XFA
50using CPDF_TestPdfDocument = CPDF_TestDocument;
51#endif // PDF_ENABLE_XFA
52
53class PDFDocTest : public testing::Test {
54 public:
55 struct DictObjInfo {
tsepezc3255f52016-03-25 14:52:27 -070056 uint32_t num;
Wei Li5227e572016-03-04 15:49:17 -080057 CPDF_Dictionary* obj;
58 };
59
60 void SetUp() override {
Lei Zhang76020fc2017-05-18 15:51:20 -070061 CPDF_ModuleMgr::Get()->Init();
Wei Li5227e572016-03-04 15:49:17 -080062
tsepez36eb4bd2016-10-03 15:24:27 -070063 m_pDoc = pdfium::MakeUnique<CPDF_TestPdfDocument>();
Wei Li5227e572016-03-04 15:49:17 -080064 m_pIndirectObjs = m_pDoc->GetHolder();
tsepeza9caab92016-12-14 05:57:10 -080065
Wei Li5227e572016-03-04 15:49:17 -080066 // Setup the root directory.
tsepeza9caab92016-12-14 05:57:10 -080067 m_pRootObj = pdfium::MakeUnique<CPDF_Dictionary>();
Wei Li5227e572016-03-04 15:49:17 -080068 m_pDoc->SetRoot(m_pRootObj.get());
69 }
70
thestigfd36b8f2016-07-11 10:43:48 -070071 void TearDown() override {
72 m_pRootObj.reset();
73 m_pIndirectObjs = nullptr;
74 m_pDoc.reset();
75 CPDF_ModuleMgr::Destroy();
76 }
77
Wei Li5227e572016-03-04 15:49:17 -080078 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.
tsepez70c4afd2016-11-15 11:33:44 -080082 CPDF_Dictionary* obj = m_pIndirectObjs->NewIndirect<CPDF_Dictionary>();
83 info.push_back({obj->GetObjNum(), obj});
Wei Li5227e572016-03-04 15:49:17 -080084 }
85 return info;
86 }
87
88 protected:
89 std::unique_ptr<CPDF_TestPdfDocument> m_pDoc;
Tom Sepezd0409af2017-05-25 15:53:57 -070090 CFX_UnownedPtr<CPDF_IndirectObjectHolder> m_pIndirectObjs;
tsepez33fdebc2016-11-04 11:38:40 -070091 std::unique_ptr<CPDF_Dictionary> m_pRootObj;
Wei Li5227e572016-03-04 15:49:17 -080092};
93
94TEST_F(PDFDocTest, FindBookmark) {
95 {
96 // No bookmark information.
97 std::unique_ptr<unsigned short, pdfium::FreeDeleter> title =
98 GetFPDFWideString(L"");
99 EXPECT_EQ(nullptr, FPDFBookmark_Find(m_pDoc.get(), title.get()));
100
101 title = GetFPDFWideString(L"Preface");
102 EXPECT_EQ(nullptr, FPDFBookmark_Find(m_pDoc.get(), title.get()));
103 }
104 {
105 // Empty bookmark tree.
tsepez0e606b52016-11-18 16:22:41 -0800106 m_pRootObj->SetNewFor<CPDF_Dictionary>("Outlines");
Wei Li5227e572016-03-04 15:49:17 -0800107 std::unique_ptr<unsigned short, pdfium::FreeDeleter> title =
108 GetFPDFWideString(L"");
109 EXPECT_EQ(nullptr, FPDFBookmark_Find(m_pDoc.get(), title.get()));
110
111 title = GetFPDFWideString(L"Preface");
112 EXPECT_EQ(nullptr, FPDFBookmark_Find(m_pDoc.get(), title.get()));
113 }
114 {
115 // Check on a regular bookmark tree.
116 auto bookmarks = CreateDictObjs(3);
117
tsepez0e606b52016-11-18 16:22:41 -0800118 bookmarks[1].obj->SetNewFor<CPDF_String>("Title", L"Chapter 1");
Tom Sepezd0409af2017-05-25 15:53:57 -0700119 bookmarks[1].obj->SetNewFor<CPDF_Reference>("Parent", m_pIndirectObjs.Get(),
tsepez0e606b52016-11-18 16:22:41 -0800120 bookmarks[0].num);
Tom Sepezd0409af2017-05-25 15:53:57 -0700121 bookmarks[1].obj->SetNewFor<CPDF_Reference>("Next", m_pIndirectObjs.Get(),
tsepez0e606b52016-11-18 16:22:41 -0800122 bookmarks[2].num);
Wei Li5227e572016-03-04 15:49:17 -0800123
tsepez0e606b52016-11-18 16:22:41 -0800124 bookmarks[2].obj->SetNewFor<CPDF_String>("Title", L"Chapter 2");
Tom Sepezd0409af2017-05-25 15:53:57 -0700125 bookmarks[2].obj->SetNewFor<CPDF_Reference>("Parent", m_pIndirectObjs.Get(),
tsepez0e606b52016-11-18 16:22:41 -0800126 bookmarks[0].num);
Tom Sepezd0409af2017-05-25 15:53:57 -0700127 bookmarks[2].obj->SetNewFor<CPDF_Reference>("Prev", m_pIndirectObjs.Get(),
tsepez0e606b52016-11-18 16:22:41 -0800128 bookmarks[1].num);
Wei Li5227e572016-03-04 15:49:17 -0800129
tsepez0e606b52016-11-18 16:22:41 -0800130 bookmarks[0].obj->SetNewFor<CPDF_Name>("Type", "Outlines");
131 bookmarks[0].obj->SetNewFor<CPDF_Number>("Count", 2);
Tom Sepezd0409af2017-05-25 15:53:57 -0700132 bookmarks[0].obj->SetNewFor<CPDF_Reference>("First", m_pIndirectObjs.Get(),
tsepez0e606b52016-11-18 16:22:41 -0800133 bookmarks[1].num);
Tom Sepezd0409af2017-05-25 15:53:57 -0700134 bookmarks[0].obj->SetNewFor<CPDF_Reference>("Last", m_pIndirectObjs.Get(),
tsepez0e606b52016-11-18 16:22:41 -0800135 bookmarks[2].num);
Wei Li5227e572016-03-04 15:49:17 -0800136
Tom Sepezd0409af2017-05-25 15:53:57 -0700137 m_pRootObj->SetNewFor<CPDF_Reference>("Outlines", m_pIndirectObjs.Get(),
tsepez0e606b52016-11-18 16:22:41 -0800138 bookmarks[0].num);
Wei Li5227e572016-03-04 15:49:17 -0800139
140 // Title with no match.
141 std::unique_ptr<unsigned short, pdfium::FreeDeleter> title =
142 GetFPDFWideString(L"Chapter 3");
143 EXPECT_EQ(nullptr, FPDFBookmark_Find(m_pDoc.get(), title.get()));
144
145 // Title with partial match only.
146 title = GetFPDFWideString(L"Chapter");
147 EXPECT_EQ(nullptr, FPDFBookmark_Find(m_pDoc.get(), title.get()));
148
149 // Title with a match.
150 title = GetFPDFWideString(L"Chapter 2");
151 EXPECT_EQ(bookmarks[2].obj, FPDFBookmark_Find(m_pDoc.get(), title.get()));
152
153 // Title match is case insensitive.
154 title = GetFPDFWideString(L"cHaPter 2");
155 EXPECT_EQ(bookmarks[2].obj, FPDFBookmark_Find(m_pDoc.get(), title.get()));
156 }
157 {
158 // Circular bookmarks in depth.
159 auto bookmarks = CreateDictObjs(3);
160
tsepez0e606b52016-11-18 16:22:41 -0800161 bookmarks[1].obj->SetNewFor<CPDF_String>("Title", L"Chapter 1");
Tom Sepezd0409af2017-05-25 15:53:57 -0700162 bookmarks[1].obj->SetNewFor<CPDF_Reference>("Parent", m_pIndirectObjs.Get(),
tsepez0e606b52016-11-18 16:22:41 -0800163 bookmarks[0].num);
Tom Sepezd0409af2017-05-25 15:53:57 -0700164 bookmarks[1].obj->SetNewFor<CPDF_Reference>("First", m_pIndirectObjs.Get(),
tsepez0e606b52016-11-18 16:22:41 -0800165 bookmarks[2].num);
Wei Li5227e572016-03-04 15:49:17 -0800166
tsepez0e606b52016-11-18 16:22:41 -0800167 bookmarks[2].obj->SetNewFor<CPDF_String>("Title", L"Chapter 2");
Tom Sepezd0409af2017-05-25 15:53:57 -0700168 bookmarks[2].obj->SetNewFor<CPDF_Reference>("Parent", m_pIndirectObjs.Get(),
tsepez0e606b52016-11-18 16:22:41 -0800169 bookmarks[1].num);
Tom Sepezd0409af2017-05-25 15:53:57 -0700170 bookmarks[2].obj->SetNewFor<CPDF_Reference>("First", m_pIndirectObjs.Get(),
tsepez0e606b52016-11-18 16:22:41 -0800171 bookmarks[1].num);
Wei Li5227e572016-03-04 15:49:17 -0800172
tsepez0e606b52016-11-18 16:22:41 -0800173 bookmarks[0].obj->SetNewFor<CPDF_Name>("Type", "Outlines");
174 bookmarks[0].obj->SetNewFor<CPDF_Number>("Count", 2);
Tom Sepezd0409af2017-05-25 15:53:57 -0700175 bookmarks[0].obj->SetNewFor<CPDF_Reference>("First", m_pIndirectObjs.Get(),
tsepez0e606b52016-11-18 16:22:41 -0800176 bookmarks[1].num);
Tom Sepezd0409af2017-05-25 15:53:57 -0700177 bookmarks[0].obj->SetNewFor<CPDF_Reference>("Last", m_pIndirectObjs.Get(),
tsepez0e606b52016-11-18 16:22:41 -0800178 bookmarks[2].num);
Wei Li5227e572016-03-04 15:49:17 -0800179
Tom Sepezd0409af2017-05-25 15:53:57 -0700180 m_pRootObj->SetNewFor<CPDF_Reference>("Outlines", m_pIndirectObjs.Get(),
tsepez0e606b52016-11-18 16:22:41 -0800181 bookmarks[0].num);
Wei Li5227e572016-03-04 15:49:17 -0800182
183 // Title with no match.
184 std::unique_ptr<unsigned short, pdfium::FreeDeleter> title =
185 GetFPDFWideString(L"Chapter 3");
186 EXPECT_EQ(nullptr, FPDFBookmark_Find(m_pDoc.get(), title.get()));
187
188 // Title with a match.
189 title = GetFPDFWideString(L"Chapter 2");
190 EXPECT_EQ(bookmarks[2].obj, FPDFBookmark_Find(m_pDoc.get(), title.get()));
191 }
192 {
193 // Circular bookmarks in breadth.
194 auto bookmarks = CreateDictObjs(4);
195
tsepez0e606b52016-11-18 16:22:41 -0800196 bookmarks[1].obj->SetNewFor<CPDF_String>("Title", L"Chapter 1");
Tom Sepezd0409af2017-05-25 15:53:57 -0700197 bookmarks[1].obj->SetNewFor<CPDF_Reference>("Parent", m_pIndirectObjs.Get(),
tsepez0e606b52016-11-18 16:22:41 -0800198 bookmarks[0].num);
Tom Sepezd0409af2017-05-25 15:53:57 -0700199 bookmarks[1].obj->SetNewFor<CPDF_Reference>("Next", m_pIndirectObjs.Get(),
tsepez0e606b52016-11-18 16:22:41 -0800200 bookmarks[2].num);
Wei Li5227e572016-03-04 15:49:17 -0800201
tsepez0e606b52016-11-18 16:22:41 -0800202 bookmarks[2].obj->SetNewFor<CPDF_String>("Title", L"Chapter 2");
Tom Sepezd0409af2017-05-25 15:53:57 -0700203 bookmarks[2].obj->SetNewFor<CPDF_Reference>("Parent", m_pIndirectObjs.Get(),
tsepez0e606b52016-11-18 16:22:41 -0800204 bookmarks[0].num);
Tom Sepezd0409af2017-05-25 15:53:57 -0700205 bookmarks[2].obj->SetNewFor<CPDF_Reference>("Next", m_pIndirectObjs.Get(),
tsepez0e606b52016-11-18 16:22:41 -0800206 bookmarks[3].num);
Wei Li5227e572016-03-04 15:49:17 -0800207
tsepez0e606b52016-11-18 16:22:41 -0800208 bookmarks[3].obj->SetNewFor<CPDF_String>("Title", L"Chapter 3");
Tom Sepezd0409af2017-05-25 15:53:57 -0700209 bookmarks[3].obj->SetNewFor<CPDF_Reference>("Parent", m_pIndirectObjs.Get(),
tsepez0e606b52016-11-18 16:22:41 -0800210 bookmarks[0].num);
Tom Sepezd0409af2017-05-25 15:53:57 -0700211 bookmarks[3].obj->SetNewFor<CPDF_Reference>("Next", m_pIndirectObjs.Get(),
tsepez0e606b52016-11-18 16:22:41 -0800212 bookmarks[1].num);
Wei Li5227e572016-03-04 15:49:17 -0800213
tsepez0e606b52016-11-18 16:22:41 -0800214 bookmarks[0].obj->SetNewFor<CPDF_Name>("Type", "Outlines");
215 bookmarks[0].obj->SetNewFor<CPDF_Number>("Count", 2);
Tom Sepezd0409af2017-05-25 15:53:57 -0700216 bookmarks[0].obj->SetNewFor<CPDF_Reference>("First", m_pIndirectObjs.Get(),
tsepez0e606b52016-11-18 16:22:41 -0800217 bookmarks[1].num);
Tom Sepezd0409af2017-05-25 15:53:57 -0700218 bookmarks[0].obj->SetNewFor<CPDF_Reference>("Last", m_pIndirectObjs.Get(),
tsepez0e606b52016-11-18 16:22:41 -0800219 bookmarks[2].num);
Wei Li5227e572016-03-04 15:49:17 -0800220
Tom Sepezd0409af2017-05-25 15:53:57 -0700221 m_pRootObj->SetNewFor<CPDF_Reference>("Outlines", m_pIndirectObjs.Get(),
tsepez0e606b52016-11-18 16:22:41 -0800222 bookmarks[0].num);
Wei Li5227e572016-03-04 15:49:17 -0800223
224 // Title with no match.
225 std::unique_ptr<unsigned short, pdfium::FreeDeleter> title =
226 GetFPDFWideString(L"Chapter 8");
227 EXPECT_EQ(nullptr, FPDFBookmark_Find(m_pDoc.get(), title.get()));
228
229 // Title with a match.
230 title = GetFPDFWideString(L"Chapter 3");
231 EXPECT_EQ(bookmarks[3].obj, FPDFBookmark_Find(m_pDoc.get(), title.get()));
232 }
233}
dsinclairc59fa882016-11-08 06:55:40 -0800234
235TEST_F(PDFDocTest, GetLocationInPage) {
236 auto array = pdfium::MakeUnique<CPDF_Array>();
tsepez8a3aa452016-11-16 12:26:06 -0800237 array->AddNew<CPDF_Number>(0); // Page Index.
238 array->AddNew<CPDF_Name>("XYZ");
239 array->AddNew<CPDF_Number>(4); // X
240 array->AddNew<CPDF_Number>(5); // Y
241 array->AddNew<CPDF_Number>(6); // Zoom.
dsinclairc59fa882016-11-08 06:55:40 -0800242
243 FPDF_BOOL hasX;
244 FPDF_BOOL hasY;
245 FPDF_BOOL hasZoom;
246 FS_FLOAT x;
247 FS_FLOAT y;
248 FS_FLOAT zoom;
249
250 EXPECT_TRUE(FPDFDest_GetLocationInPage(array.get(), &hasX, &hasY, &hasZoom,
251 &x, &y, &zoom));
252 EXPECT_TRUE(hasX);
253 EXPECT_TRUE(hasY);
254 EXPECT_TRUE(hasZoom);
255 EXPECT_EQ(4, x);
256 EXPECT_EQ(5, y);
257 EXPECT_EQ(6, zoom);
258
tsepez8a3aa452016-11-16 12:26:06 -0800259 array->SetNewAt<CPDF_Null>(2);
260 array->SetNewAt<CPDF_Null>(3);
261 array->SetNewAt<CPDF_Null>(4);
dsinclairc59fa882016-11-08 06:55:40 -0800262 EXPECT_TRUE(FPDFDest_GetLocationInPage(array.get(), &hasX, &hasY, &hasZoom,
263 &x, &y, &zoom));
264 EXPECT_FALSE(hasX);
265 EXPECT_FALSE(hasY);
266 EXPECT_FALSE(hasZoom);
267
268 array = pdfium::MakeUnique<CPDF_Array>();
269 EXPECT_FALSE(FPDFDest_GetLocationInPage(array.get(), &hasX, &hasY, &hasZoom,
270 &x, &y, &zoom));
271}