66fbe464d0a98d0e8167e19f4fee344a252f3b23
[pdfium.git] / fpdfsdk / src / fpdfview_embeddertest.cpp
1 // Copyright 2015 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 <limits>
6 #include <string>
7
8 #include "../../testing/embedder_test.h"
9 #include "../../fpdfsdk/include/fpdfview.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 class FPDFViewEmbeddertest : public EmbedderTest {
13 };
14
15 TEST_F(FPDFViewEmbeddertest, Document) {
16   EXPECT_TRUE(OpenDocument("testing/resources/about_blank.pdf"));
17   EXPECT_EQ(1, GetPageCount());
18   EXPECT_EQ(0, GetFirstPageNum());
19
20   int version;
21   EXPECT_TRUE(FPDF_GetFileVersion(document(), &version));
22   EXPECT_EQ(14, version);
23
24   EXPECT_EQ(0xFFFFFFFF, FPDF_GetDocPermissions(document()));
25   EXPECT_EQ(-1, FPDF_GetSecurityHandlerRevision(document()));
26 }
27
28 TEST_F(FPDFViewEmbeddertest, Page) {
29   EXPECT_TRUE(OpenDocument("testing/resources/about_blank.pdf"));
30   FPDF_PAGE page = LoadPage(0);
31   EXPECT_NE(nullptr, page);
32   EXPECT_EQ(612.0, FPDF_GetPageWidth(page));
33   EXPECT_EQ(792.0, FPDF_GetPageHeight(page));
34   UnloadPage(page);
35   EXPECT_EQ(nullptr, LoadPage(1));
36 }
37
38 TEST_F(FPDFViewEmbeddertest, ViewerRef) {
39   EXPECT_TRUE(OpenDocument("testing/resources/about_blank.pdf"));
40   EXPECT_TRUE(FPDF_VIEWERREF_GetPrintScaling(document()));
41   EXPECT_EQ(1, FPDF_VIEWERREF_GetNumCopies(document()));
42   EXPECT_EQ(DuplexUndefined, FPDF_VIEWERREF_GetDuplex(document()));
43 }
44
45 TEST_F(FPDFViewEmbeddertest, NamedDests) {
46   EXPECT_TRUE(OpenDocument("testing/resources/named_dests.pdf"));
47   long buffer_size;
48   char fixed_buffer[512];
49   FPDF_DEST dest;
50
51   // Query the size of the first item.
52   buffer_size = 2000000; // Absurdly large, check not used for this case.
53   dest = FPDF_GetNamedDest(document(), 0, nullptr, buffer_size);
54   EXPECT_NE(nullptr, dest);
55   EXPECT_EQ(12u, buffer_size);
56
57   // Try to retrieve the first item with too small a buffer.
58   buffer_size = 10;
59   dest = FPDF_GetNamedDest(document(), 0, fixed_buffer, buffer_size);
60   EXPECT_NE(nullptr, dest);
61   EXPECT_EQ(-1, buffer_size);
62
63   // Try to retrieve the first item with correctly sized buffer. Item is
64   // taken from Dests NameTree in named_dests.pdf.
65   buffer_size = 12;
66   dest = FPDF_GetNamedDest(document(), 0, fixed_buffer, buffer_size);
67   EXPECT_NE(nullptr, dest);
68   EXPECT_EQ(12u, buffer_size);
69   EXPECT_EQ(std::string("F\0i\0r\0s\0t\0\0\0", 12),
70             std::string(fixed_buffer, buffer_size));
71
72   // Try to retrieve the second item with ample buffer. Item is taken
73   // from Dests NameTree but has a sub-dictionary in named_dests.pdf.
74   buffer_size = sizeof(fixed_buffer);
75   dest = FPDF_GetNamedDest(document(), 1, fixed_buffer, buffer_size);
76   EXPECT_NE(nullptr, dest);
77   EXPECT_EQ(10u, buffer_size);
78   EXPECT_EQ(std::string("N\0e\0x\0t\0\0\0", 10),
79             std::string(fixed_buffer, buffer_size));
80
81   // Try to retrieve third item with ample buffer. Item is taken
82   // from Dests NameTree but has a bad sub-dictionary in named_dests.pdf.
83   // in named_dests.pdf).
84   buffer_size = sizeof(fixed_buffer);
85   dest = FPDF_GetNamedDest(document(), 2, fixed_buffer, buffer_size);
86   EXPECT_EQ(nullptr, dest);
87   EXPECT_EQ(sizeof(fixed_buffer), buffer_size);  // unmodified.
88
89   // Try to retrieve the forth item with ample buffer. Item is taken
90   // from Dests NameTree but has a vale of the wrong type in named_dests.pdf.
91   buffer_size = sizeof(fixed_buffer);
92   dest = FPDF_GetNamedDest(document(), 3, fixed_buffer, buffer_size);
93   EXPECT_EQ(nullptr, dest);
94   EXPECT_EQ(sizeof(fixed_buffer), buffer_size);  // unmodified.
95
96   // Try to retrieve fifth item with ample buffer. Item taken from the
97   // old-style Dests dictionary object in named_dests.pdf.
98   buffer_size = sizeof(fixed_buffer);
99   dest = FPDF_GetNamedDest(document(), 4, fixed_buffer, buffer_size);
100   EXPECT_NE(nullptr, dest);
101   EXPECT_EQ(30u, buffer_size);
102   EXPECT_EQ(
103       std::string("F\0i\0r\0s\0t\0A\0l\0t\0e\0r\0n\0a\0t\0e\0\0\0", 30),
104       std::string(fixed_buffer, buffer_size));
105
106   // Try to retrieve sixth item with ample buffer. Item istaken from the
107   // old-style Dests dictionary object but has a sub-dictionary in
108   // named_dests.pdf.
109   buffer_size = sizeof(fixed_buffer);
110   dest = FPDF_GetNamedDest(document(), 5, fixed_buffer, buffer_size);
111   EXPECT_NE(nullptr, dest);
112   EXPECT_EQ(28u, buffer_size);
113   EXPECT_EQ(
114       std::string("L\0a\0s\0t\0A\0l\0t\0e\0r\0n\0a\0t\0e\0\0\0", 28),
115       std::string(fixed_buffer, buffer_size));
116
117   // Try to retrieve non-existent item with ample buffer.
118   buffer_size = sizeof(fixed_buffer);
119   dest = FPDF_GetNamedDest(document(), 6, fixed_buffer, buffer_size);
120   EXPECT_EQ(nullptr, dest);
121   EXPECT_EQ(sizeof(fixed_buffer), buffer_size);  // unmodified.
122
123   // Try to underflow/overflow the integer index.
124   buffer_size = sizeof(fixed_buffer);
125   dest = FPDF_GetNamedDest(document(), std::numeric_limits<int>::max(),
126                            fixed_buffer, buffer_size);
127   EXPECT_EQ(nullptr, dest);
128   EXPECT_EQ(sizeof(fixed_buffer), buffer_size);  // unmodified.
129
130   buffer_size = sizeof(fixed_buffer);
131   dest = FPDF_GetNamedDest(document(), std::numeric_limits<int>::min(),
132                            fixed_buffer, buffer_size);
133   EXPECT_EQ(nullptr, dest);
134   EXPECT_EQ(sizeof(fixed_buffer), buffer_size);  // unmodified.
135
136   buffer_size = sizeof(fixed_buffer);
137   dest = FPDF_GetNamedDest(document(), -1, fixed_buffer, buffer_size);
138   EXPECT_EQ(nullptr, dest);
139   EXPECT_EQ(sizeof(fixed_buffer), buffer_size);  // unmodified.
140 }
141
142 TEST_F(FPDFViewEmbeddertest, NamedDestsByName) {
143   EXPECT_TRUE(OpenDocument("testing/resources/named_dests.pdf"));
144
145   // Null pointer returns NULL.
146   FPDF_DEST dest = FPDF_GetNamedDestByName(document(), nullptr);
147   EXPECT_EQ(nullptr, dest);
148
149   // Empty string returns NULL.
150   dest = FPDF_GetNamedDestByName(document(), "");
151   EXPECT_EQ(nullptr, dest);
152
153   // Item from Dests NameTree.
154   dest = FPDF_GetNamedDestByName(document(), "First");
155   EXPECT_NE(nullptr, dest);
156
157   long ignore_len = 0;
158   FPDF_DEST dest_by_index =
159       FPDF_GetNamedDest(document(), 0, nullptr, ignore_len);
160   EXPECT_EQ(dest_by_index, dest);
161
162   // Item from Dests dictionary.
163   dest = FPDF_GetNamedDestByName(document(), "FirstAlternate");
164   EXPECT_NE(nullptr, dest);
165
166   ignore_len = 0;
167   dest_by_index = FPDF_GetNamedDest(document(), 4, nullptr, ignore_len);
168   EXPECT_EQ(dest_by_index, dest);
169
170   // Bad value type for item from Dests NameTree array.
171   dest = FPDF_GetNamedDestByName(document(), "WrongType");
172   EXPECT_EQ(nullptr, dest);
173
174   // No such destination in either Dest NameTree or dictionary.
175   dest = FPDF_GetNamedDestByName(document(), "Bogus");
176   EXPECT_EQ(nullptr, dest);
177 }
178
179 // The following tests pass if the document opens without crashing.
180 TEST_F(FPDFViewEmbeddertest, Crasher_113) {
181   EXPECT_TRUE(OpenDocument("testing/resources/bug_113.pdf"));
182 }
183
184 TEST_F(FPDFViewEmbeddertest, Crasher_451830) {
185   // XFA branch detects this document as bad.
186   EXPECT_FALSE(OpenDocument("testing/resources/bug_451830.pdf"));
187 }
188
189 TEST_F(FPDFViewEmbeddertest, Crasher_452455) {
190   EXPECT_TRUE(OpenDocument("testing/resources/bug_452455.pdf"));
191   FPDF_PAGE page = LoadPage(0);
192   EXPECT_NE(nullptr, page);
193 }
194
195 TEST_F(FPDFViewEmbeddertest, Crasher_454695) {
196   // XFA branch detects this document as bad.
197   EXPECT_FALSE(OpenDocument("testing/resources/bug_454695.pdf"));
198 }