Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(361)

Side by Side Diff: core/src/fpdfapi/fpdf_parser/fpdf_parser_objects_unittest.cpp

Issue 1776913007: Split fpdf_parser_objects.cpp into per-class .cpp/.h files. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Address comments Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 "core/include/fpdfapi/fpdf_objects.h"
6
7 #include <memory>
8 #include <string>
9 #include <vector>
10
11 #include "core/include/fxcrt/fx_basic.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace {
15
16 using ScopedArray = std::unique_ptr<CPDF_Array, ReleaseDeleter<CPDF_Array>>;
17
18 void TestArrayAccessors(const CPDF_Array* arr,
19 size_t index,
20 const char* str_val,
21 const char* const_str_val,
22 int int_val,
23 float float_val,
24 CPDF_Array* arr_val,
25 CPDF_Dictionary* dict_val,
26 CPDF_Stream* stream_val) {
27 EXPECT_STREQ(str_val, arr->GetStringAt(index).c_str());
28 EXPECT_STREQ(const_str_val, arr->GetConstStringAt(index).GetCStr());
29 EXPECT_EQ(int_val, arr->GetIntegerAt(index));
30 EXPECT_EQ(float_val, arr->GetNumberAt(index));
31 EXPECT_EQ(float_val, arr->GetFloatAt(index));
32 EXPECT_EQ(arr_val, arr->GetArrayAt(index));
33 EXPECT_EQ(dict_val, arr->GetDictAt(index));
34 EXPECT_EQ(stream_val, arr->GetStreamAt(index));
35 }
36
37 } // namespace
38
39 class PDFObjectsTest : public testing::Test {
40 public:
41 void SetUp() override {
42 // Initialize different kinds of objects.
43 // Boolean objects.
44 CPDF_Boolean* boolean_false_obj = new CPDF_Boolean(false);
45 CPDF_Boolean* boolean_true_obj = new CPDF_Boolean(true);
46 // Number objects.
47 CPDF_Number* number_int_obj = new CPDF_Number(1245);
48 CPDF_Number* number_float_obj = new CPDF_Number(9.00345f);
49 // String objects.
50 CPDF_String* str_reg_obj = new CPDF_String(L"A simple test");
51 CPDF_String* str_spec_obj = new CPDF_String(L"\t\n");
52 // Name object.
53 CPDF_Name* name_obj = new CPDF_Name("space");
54 // Array object.
55 m_ArrayObj = new CPDF_Array;
56 m_ArrayObj->InsertAt(0, new CPDF_Number(8902));
57 m_ArrayObj->InsertAt(1, new CPDF_Name("address"));
58 // Dictionary object.
59 m_DictObj = new CPDF_Dictionary;
60 m_DictObj->SetAt("bool", new CPDF_Boolean(false));
61 m_DictObj->SetAt("num", new CPDF_Number(0.23f));
62 // Stream object.
63 const char content[] = "abcdefghijklmnopqrstuvwxyz";
64 size_t buf_len = FX_ArraySize(content);
65 uint8_t* buf = reinterpret_cast<uint8_t*>(malloc(buf_len));
66 memcpy(buf, content, buf_len);
67 m_StreamDictObj = new CPDF_Dictionary;
68 m_StreamDictObj->SetAt("key1", new CPDF_String(L" test dict"));
69 m_StreamDictObj->SetAt("key2", new CPDF_Number(-1));
70 CPDF_Stream* stream_obj = new CPDF_Stream(buf, buf_len, m_StreamDictObj);
71 // Null Object.
72 CPDF_Null* null_obj = new CPDF_Null;
73 // All direct objects.
74 CPDF_Object* objs[] = {boolean_false_obj, boolean_true_obj, number_int_obj,
75 number_float_obj, str_reg_obj, str_spec_obj,
76 name_obj, m_ArrayObj, m_DictObj,
77 stream_obj, null_obj};
78 m_DirectObjTypes = {
79 CPDF_Object::BOOLEAN, CPDF_Object::BOOLEAN, CPDF_Object::NUMBER,
80 CPDF_Object::NUMBER, CPDF_Object::STRING, CPDF_Object::STRING,
81 CPDF_Object::NAME, CPDF_Object::ARRAY, CPDF_Object::DICTIONARY,
82 CPDF_Object::STREAM, CPDF_Object::NULLOBJ};
83 for (size_t i = 0; i < FX_ArraySize(objs); ++i)
84 m_DirectObjs.emplace_back(objs[i]);
85
86 // Indirect references to indirect objects.
87 m_ObjHolder.reset(new CPDF_IndirectObjectHolder(nullptr));
88 m_IndirectObjs = {boolean_true_obj, number_int_obj, str_spec_obj, name_obj,
89 m_ArrayObj, m_DictObj, stream_obj};
90 for (size_t i = 0; i < m_IndirectObjs.size(); ++i) {
91 m_ObjHolder->AddIndirectObject(m_IndirectObjs[i]);
92 m_RefObjs.emplace_back(new CPDF_Reference(
93 m_ObjHolder.get(), m_IndirectObjs[i]->GetObjNum()));
94 }
95 }
96
97 bool Equal(CPDF_Object* obj1, CPDF_Object* obj2) {
98 if (obj1 == obj2)
99 return true;
100 if (!obj1 || !obj2 || obj1->GetType() != obj2->GetType())
101 return false;
102 switch (obj1->GetType()) {
103 case CPDF_Object::BOOLEAN:
104 return obj1->GetInteger() == obj2->GetInteger();
105 case CPDF_Object::NUMBER:
106 return obj1->AsNumber()->IsInteger() == obj2->AsNumber()->IsInteger() &&
107 obj1->GetInteger() == obj2->GetInteger();
108 case CPDF_Object::STRING:
109 case CPDF_Object::NAME:
110 return obj1->GetString() == obj2->GetString();
111 case CPDF_Object::ARRAY: {
112 const CPDF_Array* array1 = obj1->AsArray();
113 const CPDF_Array* array2 = obj2->AsArray();
114 if (array1->GetCount() != array2->GetCount())
115 return false;
116 for (size_t i = 0; i < array1->GetCount(); ++i) {
117 if (!Equal(array1->GetElement(i), array2->GetElement(i)))
118 return false;
119 }
120 return true;
121 }
122 case CPDF_Object::DICTIONARY: {
123 const CPDF_Dictionary* dict1 = obj1->AsDictionary();
124 const CPDF_Dictionary* dict2 = obj2->AsDictionary();
125 if (dict1->GetCount() != dict2->GetCount())
126 return false;
127 for (CPDF_Dictionary::const_iterator it = dict1->begin();
128 it != dict1->end(); ++it) {
129 if (!Equal(it->second, dict2->GetElement(it->first)))
130 return false;
131 }
132 return true;
133 }
134 case CPDF_Object::NULLOBJ:
135 return true;
136 case CPDF_Object::STREAM: {
137 const CPDF_Stream* stream1 = obj1->AsStream();
138 const CPDF_Stream* stream2 = obj2->AsStream();
139 if (!stream1->GetDict() && !stream2->GetDict())
140 return true;
141 // Compare dictionaries.
142 if (!Equal(stream1->GetDict(), stream2->GetDict()))
143 return false;
144 // Compare sizes.
145 if (stream1->GetRawSize() != stream2->GetRawSize())
146 return false;
147 // Compare contents.
148 // Since this function is used for testing Clone(), only memory based
149 // streams need to be handled.
150 if (!stream1->IsMemoryBased() || !stream2->IsMemoryBased())
151 return false;
152 return FXSYS_memcmp(stream1->GetRawData(), stream2->GetRawData(),
153 stream1->GetRawSize()) == 0;
154 }
155 case CPDF_Object::REFERENCE:
156 return obj1->AsReference()->GetRefObjNum() ==
157 obj2->AsReference()->GetRefObjNum();
158 }
159 return false;
160 }
161
162 protected:
163 using ScopedObj = std::unique_ptr<CPDF_Object, ReleaseDeleter<CPDF_Object>>;
164
165 // m_ObjHolder needs to be declared first and destructed last since it also
166 // refers to some objects in m_DirectObjs.
167 std::unique_ptr<CPDF_IndirectObjectHolder> m_ObjHolder;
168 std::vector<ScopedObj> m_DirectObjs;
169 std::vector<int> m_DirectObjTypes;
170 std::vector<ScopedObj> m_RefObjs;
171 CPDF_Dictionary* m_DictObj;
172 CPDF_Dictionary* m_StreamDictObj;
173 CPDF_Array* m_ArrayObj;
174 std::vector<CPDF_Object*> m_IndirectObjs;
175 };
176
177 TEST_F(PDFObjectsTest, GetString) {
178 const char* direct_obj_results[] = {
179 "false", "true", "1245", "9.00345", "A simple test", "\t\n", "space",
180 "", "", "", ""};
181 // Check for direct objects.
182 for (size_t i = 0; i < m_DirectObjs.size(); ++i)
183 EXPECT_STREQ(direct_obj_results[i], m_DirectObjs[i]->GetString().c_str());
184
185 // Check indirect references.
186 const char* indirect_obj_results[] = {"true", "1245", "\t\n", "space",
187 "", "", ""};
188 for (size_t i = 0; i < m_RefObjs.size(); ++i) {
189 EXPECT_STREQ(indirect_obj_results[i], m_RefObjs[i]->GetString().c_str());
190 }
191 }
192
193 TEST_F(PDFObjectsTest, GetConstString) {
194 const char* direct_obj_results[] = {
195 nullptr, nullptr, nullptr, nullptr, "A simple test", "\t\n",
196 "space", nullptr, nullptr, nullptr, nullptr};
197 // Check for direct objects.
198 for (size_t i = 0; i < m_DirectObjs.size(); ++i) {
199 if (!direct_obj_results[i]) {
200 EXPECT_EQ(direct_obj_results[i],
201 m_DirectObjs[i]->GetConstString().GetCStr());
202 } else {
203 EXPECT_STREQ(direct_obj_results[i],
204 m_DirectObjs[i]->GetConstString().GetCStr());
205 }
206 }
207 // Check indirect references.
208 const char* indirect_obj_results[] = {nullptr, nullptr, "\t\n", "space",
209 nullptr, nullptr, nullptr};
210 for (size_t i = 0; i < m_RefObjs.size(); ++i) {
211 if (!indirect_obj_results[i]) {
212 EXPECT_EQ(nullptr, m_RefObjs[i]->GetConstString().GetCStr());
213 } else {
214 EXPECT_STREQ(indirect_obj_results[i],
215 m_RefObjs[i]->GetConstString().GetCStr());
216 }
217 }
218 }
219
220 TEST_F(PDFObjectsTest, GetUnicodeText) {
221 const wchar_t* direct_obj_results[] = {
222 L"", L"", L"", L"", L"A simple test",
223 L"\t\n", L"space", L"", L"", L"abcdefghijklmnopqrstuvwxyz",
224 L""};
225 // Check for direct objects.
226 for (size_t i = 0; i < m_DirectObjs.size(); ++i)
227 EXPECT_STREQ(direct_obj_results[i],
228 m_DirectObjs[i]->GetUnicodeText().c_str());
229
230 // Check indirect references.
231 for (const auto& it : m_RefObjs)
232 EXPECT_STREQ(L"", it->GetUnicodeText().c_str());
233 }
234
235 TEST_F(PDFObjectsTest, GetNumber) {
236 const FX_FLOAT direct_obj_results[] = {0, 0, 1245, 9.00345f, 0, 0,
237 0, 0, 0, 0, 0};
238 // Check for direct objects.
239 for (size_t i = 0; i < m_DirectObjs.size(); ++i)
240 EXPECT_EQ(direct_obj_results[i], m_DirectObjs[i]->GetNumber());
241
242 // Check indirect references.
243 const FX_FLOAT indirect_obj_results[] = {0, 1245, 0, 0, 0, 0, 0};
244 for (size_t i = 0; i < m_RefObjs.size(); ++i)
245 EXPECT_EQ(indirect_obj_results[i], m_RefObjs[i]->GetNumber());
246 }
247
248 TEST_F(PDFObjectsTest, GetInteger) {
249 const int direct_obj_results[] = {0, 1, 1245, 9, 0, 0, 0, 0, 0, 0, 0};
250 // Check for direct objects.
251 for (size_t i = 0; i < m_DirectObjs.size(); ++i)
252 EXPECT_EQ(direct_obj_results[i], m_DirectObjs[i]->GetInteger());
253
254 // Check indirect references.
255 const int indirect_obj_results[] = {1, 1245, 0, 0, 0, 0, 0};
256 for (size_t i = 0; i < m_RefObjs.size(); ++i)
257 EXPECT_EQ(indirect_obj_results[i], m_RefObjs[i]->GetInteger());
258 }
259
260 TEST_F(PDFObjectsTest, GetDict) {
261 const CPDF_Dictionary* direct_obj_results[] = {
262 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
263 nullptr, nullptr, m_DictObj, m_StreamDictObj, nullptr};
264 // Check for direct objects.
265 for (size_t i = 0; i < m_DirectObjs.size(); ++i)
266 EXPECT_EQ(direct_obj_results[i], m_DirectObjs[i]->GetDict());
267
268 // Check indirect references.
269 const CPDF_Dictionary* indirect_obj_results[] = {
270 nullptr, nullptr, nullptr, nullptr, nullptr, m_DictObj, m_StreamDictObj};
271 for (size_t i = 0; i < m_RefObjs.size(); ++i)
272 EXPECT_EQ(indirect_obj_results[i], m_RefObjs[i]->GetDict());
273 }
274
275 TEST_F(PDFObjectsTest, GetArray) {
276 const CPDF_Array* direct_obj_results[] = {
277 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
278 nullptr, m_ArrayObj, nullptr, nullptr, nullptr};
279 // Check for direct objects.
280 for (size_t i = 0; i < m_DirectObjs.size(); ++i)
281 EXPECT_EQ(direct_obj_results[i], m_DirectObjs[i]->GetArray());
282
283 // Check indirect references.
284 for (const auto& it : m_RefObjs)
285 EXPECT_EQ(nullptr, it->GetArray());
286 }
287
288 TEST_F(PDFObjectsTest, Clone) {
289 // Check for direct objects.
290 for (size_t i = 0; i < m_DirectObjs.size(); ++i) {
291 ScopedObj obj(m_DirectObjs[i]->Clone());
292 EXPECT_TRUE(Equal(m_DirectObjs[i].get(), obj.get()));
293 }
294
295 // Check indirect references.
296 for (const auto& it : m_RefObjs) {
297 ScopedObj obj(it->Clone());
298 EXPECT_TRUE(Equal(it.get(), obj.get()));
299 }
300 }
301
302 TEST_F(PDFObjectsTest, GetType) {
303 // Check for direct objects.
304 for (size_t i = 0; i < m_DirectObjs.size(); ++i)
305 EXPECT_EQ(m_DirectObjTypes[i], m_DirectObjs[i]->GetType());
306
307 // Check indirect references.
308 for (const auto& it : m_RefObjs)
309 EXPECT_EQ(CPDF_Object::REFERENCE, it->GetType());
310 }
311
312 TEST_F(PDFObjectsTest, GetDirect) {
313 // Check for direct objects.
314 for (size_t i = 0; i < m_DirectObjs.size(); ++i)
315 EXPECT_EQ(m_DirectObjs[i].get(), m_DirectObjs[i]->GetDirect());
316
317 // Check indirect references.
318 for (size_t i = 0; i < m_RefObjs.size(); ++i)
319 EXPECT_EQ(m_IndirectObjs[i], m_RefObjs[i]->GetDirect());
320 }
321
322 TEST_F(PDFObjectsTest, SetString) {
323 // Check for direct objects.
324 const char* const set_values[] = {"true", "fake", "3.125f", "097",
325 "changed", "", "NewName"};
326 const char* expected[] = {"true", "false", "3.125", "97",
327 "changed", "", "NewName"};
328 for (size_t i = 0; i < FX_ArraySize(set_values); ++i) {
329 m_DirectObjs[i]->SetString(set_values[i]);
330 EXPECT_STREQ(expected[i], m_DirectObjs[i]->GetString().c_str());
331 }
332 }
333
334 TEST_F(PDFObjectsTest, IsTypeAndAsType) {
335 // Check for direct objects.
336 for (size_t i = 0; i < m_DirectObjs.size(); ++i) {
337 if (m_DirectObjTypes[i] == CPDF_Object::ARRAY) {
338 EXPECT_TRUE(m_DirectObjs[i]->IsArray());
339 EXPECT_EQ(m_DirectObjs[i].get(), m_DirectObjs[i]->AsArray());
340 } else {
341 EXPECT_FALSE(m_DirectObjs[i]->IsArray());
342 EXPECT_EQ(nullptr, m_DirectObjs[i]->AsArray());
343 }
344
345 if (m_DirectObjTypes[i] == CPDF_Object::BOOLEAN) {
346 EXPECT_TRUE(m_DirectObjs[i]->IsBoolean());
347 EXPECT_EQ(m_DirectObjs[i].get(), m_DirectObjs[i]->AsBoolean());
348 } else {
349 EXPECT_FALSE(m_DirectObjs[i]->IsBoolean());
350 EXPECT_EQ(nullptr, m_DirectObjs[i]->AsBoolean());
351 }
352
353 if (m_DirectObjTypes[i] == CPDF_Object::NAME) {
354 EXPECT_TRUE(m_DirectObjs[i]->IsName());
355 EXPECT_EQ(m_DirectObjs[i].get(), m_DirectObjs[i]->AsName());
356 } else {
357 EXPECT_FALSE(m_DirectObjs[i]->IsName());
358 EXPECT_EQ(nullptr, m_DirectObjs[i]->AsName());
359 }
360
361 if (m_DirectObjTypes[i] == CPDF_Object::NUMBER) {
362 EXPECT_TRUE(m_DirectObjs[i]->IsNumber());
363 EXPECT_EQ(m_DirectObjs[i].get(), m_DirectObjs[i]->AsNumber());
364 } else {
365 EXPECT_FALSE(m_DirectObjs[i]->IsNumber());
366 EXPECT_EQ(nullptr, m_DirectObjs[i]->AsNumber());
367 }
368
369 if (m_DirectObjTypes[i] == CPDF_Object::STRING) {
370 EXPECT_TRUE(m_DirectObjs[i]->IsString());
371 EXPECT_EQ(m_DirectObjs[i].get(), m_DirectObjs[i]->AsString());
372 } else {
373 EXPECT_FALSE(m_DirectObjs[i]->IsString());
374 EXPECT_EQ(nullptr, m_DirectObjs[i]->AsString());
375 }
376
377 if (m_DirectObjTypes[i] == CPDF_Object::DICTIONARY) {
378 EXPECT_TRUE(m_DirectObjs[i]->IsDictionary());
379 EXPECT_EQ(m_DirectObjs[i].get(), m_DirectObjs[i]->AsDictionary());
380 } else {
381 EXPECT_FALSE(m_DirectObjs[i]->IsDictionary());
382 EXPECT_EQ(nullptr, m_DirectObjs[i]->AsDictionary());
383 }
384
385 if (m_DirectObjTypes[i] == CPDF_Object::STREAM) {
386 EXPECT_TRUE(m_DirectObjs[i]->IsStream());
387 EXPECT_EQ(m_DirectObjs[i].get(), m_DirectObjs[i]->AsStream());
388 } else {
389 EXPECT_FALSE(m_DirectObjs[i]->IsStream());
390 EXPECT_EQ(nullptr, m_DirectObjs[i]->AsStream());
391 }
392
393 EXPECT_FALSE(m_DirectObjs[i]->IsReference());
394 EXPECT_EQ(nullptr, m_DirectObjs[i]->AsReference());
395 }
396 // Check indirect references.
397 for (size_t i = 0; i < m_RefObjs.size(); ++i) {
398 EXPECT_TRUE(m_RefObjs[i]->IsReference());
399 EXPECT_EQ(m_RefObjs[i].get(), m_RefObjs[i]->AsReference());
400 }
401 }
402
403 TEST(PDFArrayTest, GetMatrix) {
404 float elems[][6] = {{0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f},
405 {1, 2, 3, 4, 5, 6},
406 {2.3f, 4.05f, 3, -2, -3, 0.0f},
407 {0.05f, 0.1f, 0.56f, 0.67f, 1.34f, 99.9f}};
408 for (size_t i = 0; i < FX_ArraySize(elems); ++i) {
409 ScopedArray arr(new CPDF_Array);
410 CFX_Matrix matrix(elems[i][0], elems[i][1], elems[i][2], elems[i][3],
411 elems[i][4], elems[i][5]);
412 for (size_t j = 0; j < 6; ++j)
413 arr->AddNumber(elems[i][j]);
414 CFX_Matrix arr_matrix = arr->GetMatrix();
415 EXPECT_EQ(matrix.GetA(), arr_matrix.GetA());
416 EXPECT_EQ(matrix.GetB(), arr_matrix.GetB());
417 EXPECT_EQ(matrix.GetC(), arr_matrix.GetC());
418 EXPECT_EQ(matrix.GetD(), arr_matrix.GetD());
419 EXPECT_EQ(matrix.GetE(), arr_matrix.GetE());
420 EXPECT_EQ(matrix.GetF(), arr_matrix.GetF());
421 }
422 }
423
424 TEST(PDFArrayTest, GetRect) {
425 float elems[][4] = {{0.0f, 0.0f, 0.0f, 0.0f},
426 {1, 2, 5, 6},
427 {2.3f, 4.05f, -3, 0.0f},
428 {0.05f, 0.1f, 1.34f, 99.9f}};
429 for (size_t i = 0; i < FX_ArraySize(elems); ++i) {
430 ScopedArray arr(new CPDF_Array);
431 CFX_FloatRect rect(elems[i]);
432 for (size_t j = 0; j < 4; ++j)
433 arr->AddNumber(elems[i][j]);
434 CFX_FloatRect arr_rect = arr->GetRect();
435 EXPECT_EQ(rect.left, arr_rect.left);
436 EXPECT_EQ(rect.right, arr_rect.right);
437 EXPECT_EQ(rect.bottom, arr_rect.bottom);
438 EXPECT_EQ(rect.top, arr_rect.top);
439 }
440 }
441
442 TEST(PDFArrayTest, GetTypeAt) {
443 {
444 // Boolean array.
445 bool vals[] = {true, false, false, true, true};
446 ScopedArray arr(new CPDF_Array);
447 for (size_t i = 0; i < FX_ArraySize(vals); ++i)
448 arr->InsertAt(i, new CPDF_Boolean(vals[i]));
449 for (size_t i = 0; i < FX_ArraySize(vals); ++i) {
450 TestArrayAccessors(arr.get(), i, // Array and index.
451 vals[i] ? "true" : "false", // String value.
452 nullptr, // Const string value.
453 vals[i] ? 1 : 0, // Integer value.
454 0, // Float value.
455 nullptr, // Array value.
456 nullptr, // Dictionary value.
457 nullptr); // Stream value.
458 }
459 }
460 {
461 // Integer array.
462 int vals[] = {10, 0, -345, 2089345456, -1000000000, 567, 93658767};
463 ScopedArray arr(new CPDF_Array);
464 for (size_t i = 0; i < FX_ArraySize(vals); ++i)
465 arr->InsertAt(i, new CPDF_Number(vals[i]));
466 for (size_t i = 0; i < FX_ArraySize(vals); ++i) {
467 char buf[33];
468 TestArrayAccessors(arr.get(), i, // Array and index.
469 FXSYS_itoa(vals[i], buf, 10), // String value.
470 nullptr, // Const string value.
471 vals[i], // Integer value.
472 vals[i], // Float value.
473 nullptr, // Array value.
474 nullptr, // Dictionary value.
475 nullptr); // Stream value.
476 }
477 }
478 {
479 // Float array.
480 float vals[] = {0.0f, 0, 10, 10.0f, 0.0345f,
481 897.34f, -2.5f, -1.0f, -345.0f, -0.0f};
482 const char* expected_str[] = {"0", "0", "10", "10", "0.0345",
483 "897.34", "-2.5", "-1", "-345", "0"};
484 ScopedArray arr(new CPDF_Array);
485 for (size_t i = 0; i < FX_ArraySize(vals); ++i) {
486 arr->InsertAt(i, new CPDF_Number(vals[i]));
487 }
488 for (size_t i = 0; i < FX_ArraySize(vals); ++i) {
489 TestArrayAccessors(arr.get(), i, // Array and index.
490 expected_str[i], // String value.
491 nullptr, // Const string value.
492 vals[i], // Integer value.
493 vals[i], // Float value.
494 nullptr, // Array value.
495 nullptr, // Dictionary value.
496 nullptr); // Stream value.
497 }
498 }
499 {
500 // String and name array
501 const char* const vals[] = {"this", "adsde$%^", "\r\t", "\"012",
502 ".", "EYREW", "It is a joke :)"};
503 ScopedArray string_array(new CPDF_Array);
504 ScopedArray name_array(new CPDF_Array);
505 for (size_t i = 0; i < FX_ArraySize(vals); ++i) {
506 string_array->InsertAt(i, new CPDF_String(vals[i], false));
507 name_array->InsertAt(i, new CPDF_Name(vals[i]));
508 }
509 for (size_t i = 0; i < FX_ArraySize(vals); ++i) {
510 TestArrayAccessors(string_array.get(), i, // Array and index.
511 vals[i], // String value.
512 vals[i], // Const string value.
513 0, // Integer value.
514 0, // Float value.
515 nullptr, // Array value.
516 nullptr, // Dictionary value.
517 nullptr); // Stream value.
518 TestArrayAccessors(name_array.get(), i, // Array and index.
519 vals[i], // String value.
520 vals[i], // Const string value.
521 0, // Integer value.
522 0, // Float value.
523 nullptr, // Array value.
524 nullptr, // Dictionary value.
525 nullptr); // Stream value.
526 }
527 }
528 {
529 // Null element array.
530 ScopedArray arr(new CPDF_Array);
531 for (size_t i = 0; i < 3; ++i)
532 arr->InsertAt(i, new CPDF_Null);
533 for (size_t i = 0; i < 3; ++i) {
534 TestArrayAccessors(arr.get(), i, // Array and index.
535 "", // String value.
536 nullptr, // Const string value.
537 0, // Integer value.
538 0, // Float value.
539 nullptr, // Array value.
540 nullptr, // Dictionary value.
541 nullptr); // Stream value.
542 }
543 }
544 {
545 // Array of array.
546 CPDF_Array* vals[3];
547 ScopedArray arr(new CPDF_Array);
548 for (size_t i = 0; i < 3; ++i) {
549 vals[i] = new CPDF_Array;
550 for (size_t j = 0; j < 3; ++j) {
551 int value = j + 100;
552 vals[i]->InsertAt(i, new CPDF_Number(value));
553 }
554 arr->InsertAt(i, vals[i]);
555 }
556 for (size_t i = 0; i < 3; ++i) {
557 TestArrayAccessors(arr.get(), i, // Array and index.
558 "", // String value.
559 nullptr, // Const string value.
560 0, // Integer value.
561 0, // Float value.
562 vals[i], // Array value.
563 nullptr, // Dictionary value.
564 nullptr); // Stream value.
565 }
566 }
567 {
568 // Dictionary array.
569 CPDF_Dictionary* vals[3];
570 ScopedArray arr(new CPDF_Array);
571 for (size_t i = 0; i < 3; ++i) {
572 vals[i] = new CPDF_Dictionary;
573 for (size_t j = 0; j < 3; ++j) {
574 std::string key("key");
575 char buf[33];
576 key.append(FXSYS_itoa(j, buf, 10));
577 int value = j + 200;
578 vals[i]->SetAt(CFX_ByteStringC(key.c_str()), new CPDF_Number(value));
579 }
580 arr->InsertAt(i, vals[i]);
581 }
582 for (size_t i = 0; i < 3; ++i) {
583 TestArrayAccessors(arr.get(), i, // Array and index.
584 "", // String value.
585 nullptr, // Const string value.
586 0, // Integer value.
587 0, // Float value.
588 nullptr, // Array value.
589 vals[i], // Dictionary value.
590 nullptr); // Stream value.
591 }
592 }
593 {
594 // Stream array.
595 CPDF_Dictionary* vals[3];
596 CPDF_Stream* stream_vals[3];
597 ScopedArray arr(new CPDF_Array);
598 for (size_t i = 0; i < 3; ++i) {
599 vals[i] = new CPDF_Dictionary;
600 for (size_t j = 0; j < 3; ++j) {
601 std::string key("key");
602 char buf[33];
603 key.append(FXSYS_itoa(j, buf, 10));
604 int value = j + 200;
605 vals[i]->SetAt(CFX_ByteStringC(key.c_str()), new CPDF_Number(value));
606 }
607 uint8_t content[] = "content: this is a stream";
608 size_t data_size = FX_ArraySize(content);
609 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(data_size));
610 memcpy(data, content, data_size);
611 stream_vals[i] = new CPDF_Stream(data, data_size, vals[i]);
612 arr->InsertAt(i, stream_vals[i]);
613 }
614 for (size_t i = 0; i < 3; ++i) {
615 TestArrayAccessors(arr.get(), i, // Array and index.
616 "", // String value.
617 nullptr, // Const string value.
618 0, // Integer value.
619 0, // Float value.
620 nullptr, // Array value.
621 vals[i], // Dictionary value.
622 stream_vals[i]); // Stream value.
623 }
624 }
625 {
626 // Mixed array.
627 ScopedArray arr(new CPDF_Array);
628 // Array arr will take ownership of all the objects inserted.
629 arr->InsertAt(0, new CPDF_Boolean(true));
630 arr->InsertAt(1, new CPDF_Boolean(false));
631 arr->InsertAt(2, new CPDF_Number(0));
632 arr->InsertAt(3, new CPDF_Number(-1234));
633 arr->InsertAt(4, new CPDF_Number(2345.0f));
634 arr->InsertAt(5, new CPDF_Number(0.05f));
635 arr->InsertAt(6, new CPDF_String("", false));
636 arr->InsertAt(7, new CPDF_String("It is a test!", false));
637 arr->InsertAt(8, new CPDF_Name("NAME"));
638 arr->InsertAt(9, new CPDF_Name("test"));
639 arr->InsertAt(10, new CPDF_Null());
640 CPDF_Array* arr_val = new CPDF_Array;
641 arr_val->AddNumber(1);
642 arr_val->AddNumber(2);
643 arr->InsertAt(11, arr_val);
644 CPDF_Dictionary* dict_val = new CPDF_Dictionary;
645 dict_val->SetAt("key1", new CPDF_String("Linda", false));
646 dict_val->SetAt("key2", new CPDF_String("Zoe", false));
647 arr->InsertAt(12, dict_val);
648 CPDF_Dictionary* stream_dict = new CPDF_Dictionary;
649 stream_dict->SetAt("key1", new CPDF_String("John", false));
650 stream_dict->SetAt("key2", new CPDF_String("King", false));
651 uint8_t data[] = "A stream for test";
652 // The data buffer will be owned by stream object, so it needs to be
653 // dynamically allocated.
654 size_t buf_size = sizeof(data);
655 uint8_t* buf = reinterpret_cast<uint8_t*>(malloc(buf_size));
656 memcpy(buf, data, buf_size);
657 CPDF_Stream* stream_val = new CPDF_Stream(buf, buf_size, stream_dict);
658 arr->InsertAt(13, stream_val);
659 const char* const expected_str[] = {
660 "true", "false", "0", "-1234", "2345", "0.05", "",
661 "It is a test!", "NAME", "test", "", "", "", ""};
662 const char* const expected_cstr[] = {
663 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
664 "It is a test!", "NAME", "test", nullptr, nullptr, nullptr, nullptr};
665 const int expected_int[] = {1, 0, 0, -1234, 2345, 0, 0,
666 0, 0, 0, 0, 0, 0, 0};
667 const float expected_float[] = {0, 0, 0, -1234, 2345, 0.05f, 0,
668 0, 0, 0, 0, 0, 0, 0};
669 for (size_t i = 0; i < arr->GetCount(); ++i) {
670 EXPECT_STREQ(expected_str[i], arr->GetStringAt(i).c_str());
671 EXPECT_STREQ(expected_cstr[i], arr->GetConstStringAt(i).GetCStr());
672 EXPECT_EQ(expected_int[i], arr->GetIntegerAt(i));
673 EXPECT_EQ(expected_float[i], arr->GetNumberAt(i));
674 EXPECT_EQ(expected_float[i], arr->GetFloatAt(i));
675 if (i == 11)
676 EXPECT_EQ(arr_val, arr->GetArrayAt(i));
677 else
678 EXPECT_EQ(nullptr, arr->GetArrayAt(i));
679 if (i == 13) {
680 EXPECT_EQ(stream_dict, arr->GetDictAt(i));
681 EXPECT_EQ(stream_val, arr->GetStreamAt(i));
682 } else {
683 EXPECT_EQ(nullptr, arr->GetStreamAt(i));
684 if (i == 12)
685 EXPECT_EQ(dict_val, arr->GetDictAt(i));
686 else
687 EXPECT_EQ(nullptr, arr->GetDictAt(i));
688 }
689 }
690 }
691 }
692
693 TEST(PDFArrayTest, AddNumber) {
694 float vals[] = {1.0f, -1.0f, 0, 0.456734f,
695 12345.54321f, 0.5f, 1000, 0.000045f};
696 ScopedArray arr(new CPDF_Array);
697 for (size_t i = 0; i < FX_ArraySize(vals); ++i)
698 arr->AddNumber(vals[i]);
699 for (size_t i = 0; i < FX_ArraySize(vals); ++i) {
700 EXPECT_EQ(CPDF_Object::NUMBER, arr->GetElement(i)->GetType());
701 EXPECT_EQ(vals[i], arr->GetElement(i)->GetNumber());
702 }
703 }
704
705 TEST(PDFArrayTest, AddInteger) {
706 int vals[] = {0, 1, 934435456, 876, 10000, -1, -24354656, -100};
707 ScopedArray arr(new CPDF_Array);
708 for (size_t i = 0; i < FX_ArraySize(vals); ++i)
709 arr->AddInteger(vals[i]);
710 for (size_t i = 0; i < FX_ArraySize(vals); ++i) {
711 EXPECT_EQ(CPDF_Object::NUMBER, arr->GetElement(i)->GetType());
712 EXPECT_EQ(vals[i], arr->GetElement(i)->GetNumber());
713 }
714 }
715
716 TEST(PDFArrayTest, AddStringAndName) {
717 const char* vals[] = {"", "a", "ehjhRIOYTTFdfcdnv", "122323",
718 "$#%^&**", " ", "This is a test.\r\n"};
719 ScopedArray string_array(new CPDF_Array);
720 ScopedArray name_array(new CPDF_Array);
721 for (size_t i = 0; i < FX_ArraySize(vals); ++i) {
722 string_array->AddString(vals[i]);
723 name_array->AddName(vals[i]);
724 }
725 for (size_t i = 0; i < FX_ArraySize(vals); ++i) {
726 EXPECT_EQ(CPDF_Object::STRING, string_array->GetElement(i)->GetType());
727 EXPECT_STREQ(vals[i], string_array->GetElement(i)->GetString().c_str());
728 EXPECT_EQ(CPDF_Object::NAME, name_array->GetElement(i)->GetType());
729 EXPECT_STREQ(vals[i], name_array->GetElement(i)->GetString().c_str());
730 }
731 }
732
733 TEST(PDFArrayTest, AddReferenceAndGetElement) {
734 std::unique_ptr<CPDF_IndirectObjectHolder> holder(
735 new CPDF_IndirectObjectHolder(nullptr));
736 CPDF_Boolean* boolean_obj = new CPDF_Boolean(true);
737 CPDF_Number* int_obj = new CPDF_Number(-1234);
738 CPDF_Number* float_obj = new CPDF_Number(2345.089f);
739 CPDF_String* str_obj = new CPDF_String("Adsfdsf 343434 %&&*\n", false);
740 CPDF_Name* name_obj = new CPDF_Name("Title:");
741 CPDF_Null* null_obj = new CPDF_Null();
742 CPDF_Object* indirect_objs[] = {boolean_obj, int_obj, float_obj,
743 str_obj, name_obj, null_obj};
744 unsigned int obj_nums[] = {2, 4, 7, 2345, 799887, 1};
745 ScopedArray arr(new CPDF_Array);
746 ScopedArray arr1(new CPDF_Array);
747 // Create two arrays of references by different AddReference() APIs.
748 for (size_t i = 0; i < FX_ArraySize(indirect_objs); ++i) {
749 // All the indirect objects inserted will be owned by holder.
750 holder->InsertIndirectObject(obj_nums[i], indirect_objs[i]);
751 arr->AddReference(holder.get(), obj_nums[i]);
752 arr1->AddReference(holder.get(), indirect_objs[i]);
753 }
754 // Check indirect objects.
755 for (size_t i = 0; i < FX_ArraySize(obj_nums); ++i)
756 EXPECT_EQ(indirect_objs[i], holder->GetIndirectObject(obj_nums[i]));
757 // Check arrays.
758 EXPECT_EQ(arr->GetCount(), arr1->GetCount());
759 for (size_t i = 0; i < arr->GetCount(); ++i) {
760 EXPECT_EQ(CPDF_Object::REFERENCE, arr->GetElement(i)->GetType());
761 EXPECT_EQ(indirect_objs[i], arr->GetElement(i)->GetDirect());
762 EXPECT_EQ(indirect_objs[i], arr->GetElementValue(i));
763 EXPECT_EQ(CPDF_Object::REFERENCE, arr1->GetElement(i)->GetType());
764 EXPECT_EQ(indirect_objs[i], arr1->GetElement(i)->GetDirect());
765 EXPECT_EQ(indirect_objs[i], arr1->GetElementValue(i));
766 }
767 }
OLDNEW
« no previous file with comments | « core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp ('k') | core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698