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