OLD | NEW |
1 // Copyright 2016 PDFium Authors. All rights reserved. | 1 // Copyright 2016 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/fpdfapi/fpdf_parser/cpdf_array.h" | 5 #include "core/fpdfapi/fpdf_parser/cpdf_array.h" |
6 #include "core/fpdfapi/fpdf_parser/cpdf_boolean.h" | 6 #include "core/fpdfapi/fpdf_parser/cpdf_boolean.h" |
7 #include "core/fpdfapi/fpdf_parser/cpdf_dictionary.h" | 7 #include "core/fpdfapi/fpdf_parser/cpdf_dictionary.h" |
8 #include "core/fpdfapi/fpdf_parser/cpdf_name.h" | 8 #include "core/fpdfapi/fpdf_parser/cpdf_name.h" |
9 #include "core/fpdfapi/fpdf_parser/cpdf_null.h" | 9 #include "core/fpdfapi/fpdf_parser/cpdf_null.h" |
10 #include "core/fpdfapi/fpdf_parser/cpdf_number.h" | 10 #include "core/fpdfapi/fpdf_parser/cpdf_number.h" |
11 #include "core/fpdfapi/fpdf_parser/cpdf_reference.h" | 11 #include "core/fpdfapi/fpdf_parser/cpdf_reference.h" |
12 #include "core/fpdfapi/fpdf_parser/cpdf_stream.h" | 12 #include "core/fpdfapi/fpdf_parser/cpdf_stream.h" |
13 #include "core/fpdfapi/fpdf_parser/cpdf_string.h" | 13 #include "core/fpdfapi/fpdf_parser/cpdf_string.h" |
14 | 14 |
15 #include <memory> | 15 #include <memory> |
16 #include <string> | 16 #include <string> |
17 #include <vector> | 17 #include <vector> |
18 | 18 |
19 #include "core/fpdfapi/fpdf_parser/cpdf_indirect_object_holder.h" | 19 #include "core/fpdfapi/fpdf_parser/cpdf_indirect_object_holder.h" |
20 #include "core/fxcrt/fx_basic.h" | 20 #include "core/fxcrt/fx_basic.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 using ScopedArray = std::unique_ptr<CPDF_Array, ReleaseDeleter<CPDF_Array>>; | 25 using ScopedArray = std::unique_ptr<CPDF_Array, ReleaseDeleter<CPDF_Array>>; |
26 using ScopedDict = | 26 using ScopedDict = |
27 std::unique_ptr<CPDF_Dictionary, ReleaseDeleter<CPDF_Dictionary>>; | 27 std::unique_ptr<CPDF_Dictionary, ReleaseDeleter<CPDF_Dictionary>>; |
| 28 using ScopedStream = std::unique_ptr<CPDF_Stream, ReleaseDeleter<CPDF_Stream>>; |
28 | 29 |
29 void TestArrayAccessors(const CPDF_Array* arr, | 30 void TestArrayAccessors(const CPDF_Array* arr, |
30 size_t index, | 31 size_t index, |
31 const char* str_val, | 32 const char* str_val, |
32 const char* const_str_val, | 33 const char* const_str_val, |
33 int int_val, | 34 int int_val, |
34 float float_val, | 35 float float_val, |
35 CPDF_Array* arr_val, | 36 CPDF_Array* arr_val, |
36 CPDF_Dictionary* dict_val, | 37 CPDF_Dictionary* dict_val, |
37 CPDF_Stream* stream_val) { | 38 CPDF_Stream* stream_val) { |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 m_DirectObjTypes = { | 89 m_DirectObjTypes = { |
89 CPDF_Object::BOOLEAN, CPDF_Object::BOOLEAN, CPDF_Object::NUMBER, | 90 CPDF_Object::BOOLEAN, CPDF_Object::BOOLEAN, CPDF_Object::NUMBER, |
90 CPDF_Object::NUMBER, CPDF_Object::STRING, CPDF_Object::STRING, | 91 CPDF_Object::NUMBER, CPDF_Object::STRING, CPDF_Object::STRING, |
91 CPDF_Object::NAME, CPDF_Object::ARRAY, CPDF_Object::DICTIONARY, | 92 CPDF_Object::NAME, CPDF_Object::ARRAY, CPDF_Object::DICTIONARY, |
92 CPDF_Object::STREAM, CPDF_Object::NULLOBJ}; | 93 CPDF_Object::STREAM, CPDF_Object::NULLOBJ}; |
93 for (size_t i = 0; i < FX_ArraySize(objs); ++i) | 94 for (size_t i = 0; i < FX_ArraySize(objs); ++i) |
94 m_DirectObjs.emplace_back(objs[i]); | 95 m_DirectObjs.emplace_back(objs[i]); |
95 | 96 |
96 // Indirect references to indirect objects. | 97 // Indirect references to indirect objects. |
97 m_ObjHolder.reset(new CPDF_IndirectObjectHolder()); | 98 m_ObjHolder.reset(new CPDF_IndirectObjectHolder()); |
98 m_IndirectObjs = {boolean_true_obj, number_int_obj, str_spec_obj, name_obj, | 99 m_IndirectObjs = {boolean_true_obj->Clone(), number_int_obj->Clone(), |
99 m_ArrayObj, m_DictObj, stream_obj}; | 100 str_spec_obj->Clone(), name_obj->Clone(), |
| 101 m_ArrayObj->Clone(), m_DictObj->Clone(), |
| 102 stream_obj->Clone()}; |
100 for (size_t i = 0; i < m_IndirectObjs.size(); ++i) { | 103 for (size_t i = 0; i < m_IndirectObjs.size(); ++i) { |
101 m_ObjHolder->AddIndirectObject(m_IndirectObjs[i]); | 104 m_ObjHolder->AddIndirectObject(m_IndirectObjs[i]); |
102 m_RefObjs.emplace_back(new CPDF_Reference( | 105 m_RefObjs.emplace_back(new CPDF_Reference( |
103 m_ObjHolder.get(), m_IndirectObjs[i]->GetObjNum())); | 106 m_ObjHolder.get(), m_IndirectObjs[i]->GetObjNum())); |
104 } | 107 } |
105 } | 108 } |
106 | 109 |
107 bool Equal(CPDF_Object* obj1, CPDF_Object* obj2) { | 110 bool Equal(const CPDF_Object* obj1, const CPDF_Object* obj2) { |
108 if (obj1 == obj2) | 111 if (obj1 == obj2) |
109 return true; | 112 return true; |
110 if (!obj1 || !obj2 || obj1->GetType() != obj2->GetType()) | 113 if (!obj1 || !obj2 || obj1->GetType() != obj2->GetType()) |
111 return false; | 114 return false; |
112 switch (obj1->GetType()) { | 115 switch (obj1->GetType()) { |
113 case CPDF_Object::BOOLEAN: | 116 case CPDF_Object::BOOLEAN: |
114 return obj1->GetInteger() == obj2->GetInteger(); | 117 return obj1->GetInteger() == obj2->GetInteger(); |
115 case CPDF_Object::NUMBER: | 118 case CPDF_Object::NUMBER: |
116 return obj1->AsNumber()->IsInteger() == obj2->AsNumber()->IsInteger() && | 119 return obj1->AsNumber()->IsInteger() == obj2->AsNumber()->IsInteger() && |
117 obj1->GetInteger() == obj2->GetInteger(); | 120 obj1->GetInteger() == obj2->GetInteger(); |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, | 249 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, |
247 nullptr, nullptr, m_DictObj, m_StreamDictObj, nullptr}; | 250 nullptr, nullptr, m_DictObj, m_StreamDictObj, nullptr}; |
248 // Check for direct objects. | 251 // Check for direct objects. |
249 for (size_t i = 0; i < m_DirectObjs.size(); ++i) | 252 for (size_t i = 0; i < m_DirectObjs.size(); ++i) |
250 EXPECT_EQ(direct_obj_results[i], m_DirectObjs[i]->GetDict()); | 253 EXPECT_EQ(direct_obj_results[i], m_DirectObjs[i]->GetDict()); |
251 | 254 |
252 // Check indirect references. | 255 // Check indirect references. |
253 const CPDF_Dictionary* const indirect_obj_results[] = { | 256 const CPDF_Dictionary* const indirect_obj_results[] = { |
254 nullptr, nullptr, nullptr, nullptr, nullptr, m_DictObj, m_StreamDictObj}; | 257 nullptr, nullptr, nullptr, nullptr, nullptr, m_DictObj, m_StreamDictObj}; |
255 for (size_t i = 0; i < m_RefObjs.size(); ++i) | 258 for (size_t i = 0; i < m_RefObjs.size(); ++i) |
256 EXPECT_EQ(indirect_obj_results[i], m_RefObjs[i]->GetDict()); | 259 EXPECT_TRUE(Equal(indirect_obj_results[i], m_RefObjs[i]->GetDict())); |
257 } | 260 } |
258 | 261 |
259 TEST_F(PDFObjectsTest, GetArray) { | 262 TEST_F(PDFObjectsTest, GetArray) { |
260 const CPDF_Array* const direct_obj_results[] = { | 263 const CPDF_Array* const direct_obj_results[] = { |
261 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, | 264 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, |
262 nullptr, m_ArrayObj, nullptr, nullptr, nullptr}; | 265 nullptr, m_ArrayObj, nullptr, nullptr, nullptr}; |
263 // Check for direct objects. | 266 // Check for direct objects. |
264 for (size_t i = 0; i < m_DirectObjs.size(); ++i) | 267 for (size_t i = 0; i < m_DirectObjs.size(); ++i) |
265 EXPECT_EQ(direct_obj_results[i], m_DirectObjs[i]->AsArray()); | 268 EXPECT_EQ(direct_obj_results[i], m_DirectObjs[i]->AsArray()); |
266 | 269 |
(...skipping 513 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
780 ASSERT_TRUE(cloned_dict_object->IsDictionary()); | 783 ASSERT_TRUE(cloned_dict_object->IsDictionary()); |
781 | 784 |
782 ScopedDict cloned_dict(cloned_dict_object->AsDictionary()); | 785 ScopedDict cloned_dict(cloned_dict_object->AsDictionary()); |
783 ASSERT_EQ(1U, cloned_dict->GetCount()); | 786 ASSERT_EQ(1U, cloned_dict->GetCount()); |
784 CPDF_Object* cloned_obj = cloned_dict->GetObjectFor("foo"); | 787 CPDF_Object* cloned_obj = cloned_dict->GetObjectFor("foo"); |
785 EXPECT_FALSE(cloned_obj); | 788 EXPECT_FALSE(cloned_obj); |
786 } | 789 } |
787 | 790 |
788 TEST(PDFObjectTest, CloneCheckLoop) { | 791 TEST(PDFObjectTest, CloneCheckLoop) { |
789 { | 792 { |
790 // Create an object with a reference loop. | 793 // Create a dictionary/array pair with a reference loop. |
| 794 CPDF_Dictionary* dict_obj = new CPDF_Dictionary(); |
791 ScopedArray arr_obj(new CPDF_Array); | 795 ScopedArray arr_obj(new CPDF_Array); |
792 // Dictionary object. | |
793 CPDF_Dictionary* dict_obj = new CPDF_Dictionary(); | |
794 dict_obj->SetFor("arr", arr_obj.get()); | 796 dict_obj->SetFor("arr", arr_obj.get()); |
795 arr_obj->InsertAt(0, dict_obj); | 797 arr_obj->InsertAt(0, dict_obj); |
796 | 798 |
797 // Clone this object to see whether stack overflow will be triggered. | 799 // Clone this object to see whether stack overflow will be triggered. |
798 ScopedArray cloned_array(arr_obj->Clone()->AsArray()); | 800 ScopedArray cloned_array(arr_obj->Clone()->AsArray()); |
799 // Cloned object should be the same as the original. | 801 // Cloned object should be the same as the original. |
800 ASSERT_TRUE(cloned_array); | 802 ASSERT_TRUE(cloned_array); |
801 EXPECT_EQ(1u, cloned_array->GetCount()); | 803 EXPECT_EQ(1u, cloned_array->GetCount()); |
802 CPDF_Object* cloned_dict = cloned_array->GetObjectAt(0); | 804 CPDF_Object* cloned_dict = cloned_array->GetObjectAt(0); |
803 ASSERT_TRUE(cloned_dict); | 805 ASSERT_TRUE(cloned_dict); |
804 ASSERT_TRUE(cloned_dict->IsDictionary()); | 806 ASSERT_TRUE(cloned_dict->IsDictionary()); |
805 // Recursively referenced object is not cloned. | 807 // Recursively referenced object is not cloned. |
806 EXPECT_EQ(nullptr, cloned_dict->AsDictionary()->GetObjectFor("arr")); | 808 EXPECT_EQ(nullptr, cloned_dict->AsDictionary()->GetObjectFor("arr")); |
807 } | 809 } |
808 { | 810 { |
| 811 // Create a dictionary/stream pair with a reference loop. |
| 812 CPDF_Dictionary* dict_obj = new CPDF_Dictionary(); |
| 813 ScopedStream stream_obj(new CPDF_Stream(nullptr, 0, dict_obj)); |
| 814 dict_obj->SetFor("stream", stream_obj.get()); |
| 815 |
| 816 // Clone this object to see whether stack overflow will be triggered. |
| 817 ScopedStream cloned_stream(stream_obj->Clone()->AsStream()); |
| 818 // Cloned object should be the same as the original. |
| 819 ASSERT_TRUE(cloned_stream); |
| 820 CPDF_Object* cloned_dict = cloned_stream->GetDict(); |
| 821 ASSERT_TRUE(cloned_dict); |
| 822 ASSERT_TRUE(cloned_dict->IsDictionary()); |
| 823 // Recursively referenced object is not cloned. |
| 824 EXPECT_EQ(nullptr, cloned_dict->AsDictionary()->GetObjectFor("stream")); |
| 825 } |
| 826 { |
809 CPDF_IndirectObjectHolder objects_holder; | 827 CPDF_IndirectObjectHolder objects_holder; |
810 // Create an object with a reference loop. | 828 // Create an object with a reference loop. |
811 CPDF_Dictionary* dict_obj = new CPDF_Dictionary(); | 829 CPDF_Dictionary* dict_obj = new CPDF_Dictionary(); |
812 CPDF_Array* arr_obj = new CPDF_Array; | 830 CPDF_Array* arr_obj = new CPDF_Array; |
813 objects_holder.AddIndirectObject(dict_obj); | 831 objects_holder.AddIndirectObject(dict_obj); |
814 EXPECT_EQ(1u, dict_obj->GetObjNum()); | 832 EXPECT_EQ(1u, dict_obj->GetObjNum()); |
815 dict_obj->SetFor("arr", arr_obj); | 833 dict_obj->SetFor("arr", arr_obj); |
816 arr_obj->InsertAt( | 834 arr_obj->InsertAt( |
817 0, new CPDF_Reference(&objects_holder, dict_obj->GetObjNum())); | 835 0, new CPDF_Reference(&objects_holder, dict_obj->GetObjNum())); |
818 CPDF_Object* elem0 = arr_obj->GetObjectAt(0); | 836 CPDF_Object* elem0 = arr_obj->GetObjectAt(0); |
819 ASSERT_TRUE(elem0); | 837 ASSERT_TRUE(elem0); |
820 ASSERT_TRUE(elem0->IsReference()); | 838 ASSERT_TRUE(elem0->IsReference()); |
821 EXPECT_EQ(1u, elem0->AsReference()->GetRefObjNum()); | 839 EXPECT_EQ(1u, elem0->AsReference()->GetRefObjNum()); |
822 EXPECT_EQ(dict_obj, elem0->AsReference()->GetDirect()); | 840 EXPECT_EQ(dict_obj, elem0->AsReference()->GetDirect()); |
823 | 841 |
824 // Clone this object to see whether stack overflow will be triggered. | 842 // Clone this object to see whether stack overflow will be triggered. |
825 ScopedDict cloned_dict(ToDictionary(dict_obj->CloneDirectObject())); | 843 ScopedDict cloned_dict(ToDictionary(dict_obj->CloneDirectObject())); |
826 // Cloned object should be the same as the original. | 844 // Cloned object should be the same as the original. |
827 ASSERT_TRUE(cloned_dict); | 845 ASSERT_TRUE(cloned_dict); |
828 CPDF_Object* cloned_arr = cloned_dict->GetObjectFor("arr"); | 846 CPDF_Object* cloned_arr = cloned_dict->GetObjectFor("arr"); |
829 ASSERT_TRUE(cloned_arr); | 847 ASSERT_TRUE(cloned_arr); |
830 ASSERT_TRUE(cloned_arr->IsArray()); | 848 ASSERT_TRUE(cloned_arr->IsArray()); |
831 EXPECT_EQ(1u, cloned_arr->AsArray()->GetCount()); | 849 EXPECT_EQ(1u, cloned_arr->AsArray()->GetCount()); |
832 // Recursively referenced object is not cloned. | 850 // Recursively referenced object is not cloned. |
833 EXPECT_EQ(nullptr, cloned_arr->AsArray()->GetObjectAt(0)); | 851 EXPECT_EQ(nullptr, cloned_arr->AsArray()->GetObjectAt(0)); |
834 } | 852 } |
835 } | 853 } |
OLD | NEW |