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_boolean.h" | 5 #include "core/fpdfapi/fpdf_parser/cpdf_boolean.h" |
6 #include "core/fpdfapi/fpdf_parser/cpdf_null.h" | 6 #include "core/fpdfapi/fpdf_parser/cpdf_null.h" |
7 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" | 7 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" |
8 #include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h" | 8 #include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h" |
9 #include "core/fpdfapi/fpdf_parser/include/cpdf_name.h" | 9 #include "core/fpdfapi/fpdf_parser/include/cpdf_name.h" |
10 #include "core/fpdfapi/fpdf_parser/include/cpdf_number.h" | 10 #include "core/fpdfapi/fpdf_parser/include/cpdf_number.h" |
(...skipping 729 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
740 for (size_t i = 0; i < arr->GetCount(); ++i) { | 740 for (size_t i = 0; i < arr->GetCount(); ++i) { |
741 EXPECT_EQ(CPDF_Object::REFERENCE, arr->GetObjectAt(i)->GetType()); | 741 EXPECT_EQ(CPDF_Object::REFERENCE, arr->GetObjectAt(i)->GetType()); |
742 EXPECT_EQ(indirect_objs[i], arr->GetObjectAt(i)->GetDirect()); | 742 EXPECT_EQ(indirect_objs[i], arr->GetObjectAt(i)->GetDirect()); |
743 EXPECT_EQ(indirect_objs[i], arr->GetDirectObjectAt(i)); | 743 EXPECT_EQ(indirect_objs[i], arr->GetDirectObjectAt(i)); |
744 EXPECT_EQ(CPDF_Object::REFERENCE, arr1->GetObjectAt(i)->GetType()); | 744 EXPECT_EQ(CPDF_Object::REFERENCE, arr1->GetObjectAt(i)->GetType()); |
745 EXPECT_EQ(indirect_objs[i], arr1->GetObjectAt(i)->GetDirect()); | 745 EXPECT_EQ(indirect_objs[i], arr1->GetObjectAt(i)->GetDirect()); |
746 EXPECT_EQ(indirect_objs[i], arr1->GetDirectObjectAt(i)); | 746 EXPECT_EQ(indirect_objs[i], arr1->GetDirectObjectAt(i)); |
747 } | 747 } |
748 } | 748 } |
749 | 749 |
| 750 TEST(PDFArrayTest, CloneDirectObject) { |
| 751 CPDF_IndirectObjectHolder objects_holder; |
| 752 ScopedArray array(new CPDF_Array); |
| 753 array->AddReference(&objects_holder, 1234); |
| 754 ASSERT_EQ(1U, array->GetCount()); |
| 755 CPDF_Object* obj = array->GetObjectAt(0); |
| 756 ASSERT_TRUE(obj); |
| 757 EXPECT_TRUE(obj->IsReference()); |
| 758 |
| 759 CPDF_Object* cloned_array_object = array->CloneDirectObject(); |
| 760 ASSERT_TRUE(cloned_array_object); |
| 761 ASSERT_TRUE(cloned_array_object->IsArray()); |
| 762 |
| 763 ScopedArray cloned_array(cloned_array_object->AsArray()); |
| 764 ASSERT_EQ(1U, cloned_array->GetCount()); |
| 765 CPDF_Object* cloned_obj = cloned_array->GetObjectAt(0); |
| 766 EXPECT_FALSE(cloned_obj); |
| 767 } |
| 768 |
| 769 TEST(PDFDictionaryTest, CloneDirectObject) { |
| 770 CPDF_IndirectObjectHolder objects_holder; |
| 771 ScopedDict dict(new CPDF_Dictionary); |
| 772 dict->SetAtReference("foo", &objects_holder, 1234); |
| 773 ASSERT_EQ(1U, dict->GetCount()); |
| 774 CPDF_Object* obj = dict->GetObjectBy("foo"); |
| 775 ASSERT_TRUE(obj); |
| 776 EXPECT_TRUE(obj->IsReference()); |
| 777 |
| 778 CPDF_Object* cloned_dict_object = dict->CloneDirectObject(); |
| 779 ASSERT_TRUE(cloned_dict_object); |
| 780 ASSERT_TRUE(cloned_dict_object->IsDictionary()); |
| 781 |
| 782 ScopedDict cloned_dict(cloned_dict_object->AsDictionary()); |
| 783 ASSERT_EQ(1U, cloned_dict->GetCount()); |
| 784 CPDF_Object* cloned_obj = cloned_dict->GetObjectBy("foo"); |
| 785 EXPECT_FALSE(cloned_obj); |
| 786 } |
| 787 |
750 TEST(PDFObjectTest, CloneCheckLoop) { | 788 TEST(PDFObjectTest, CloneCheckLoop) { |
751 { | 789 { |
752 // Create an object with a reference loop. | 790 // Create an object with a reference loop. |
753 ScopedArray arr_obj(new CPDF_Array); | 791 ScopedArray arr_obj(new CPDF_Array); |
754 // Dictionary object. | 792 // Dictionary object. |
755 CPDF_Dictionary* dict_obj = new CPDF_Dictionary; | 793 CPDF_Dictionary* dict_obj = new CPDF_Dictionary; |
756 dict_obj->SetAt("arr", arr_obj.get()); | 794 dict_obj->SetAt("arr", arr_obj.get()); |
757 arr_obj->InsertAt(0, dict_obj); | 795 arr_obj->InsertAt(0, dict_obj); |
758 | 796 |
759 // Clone this object to see whether stack overflow will be triggered. | 797 // Clone this object to see whether stack overflow will be triggered. |
760 ScopedArray cloned_array(arr_obj->Clone()->AsArray()); | 798 ScopedArray cloned_array(arr_obj->Clone()->AsArray()); |
761 // Cloned object should be the same as the original. | 799 // Cloned object should be the same as the original. |
762 ASSERT_TRUE(cloned_array); | 800 ASSERT_TRUE(cloned_array); |
763 EXPECT_EQ(1u, cloned_array->GetCount()); | 801 EXPECT_EQ(1u, cloned_array->GetCount()); |
764 CPDF_Object* cloned_dict = cloned_array->GetObjectAt(0); | 802 CPDF_Object* cloned_dict = cloned_array->GetObjectAt(0); |
765 ASSERT_TRUE(cloned_dict); | 803 ASSERT_TRUE(cloned_dict); |
766 ASSERT_TRUE(cloned_dict->IsDictionary()); | 804 ASSERT_TRUE(cloned_dict->IsDictionary()); |
767 // Recursively referenced object is not cloned. | 805 // Recursively referenced object is not cloned. |
768 EXPECT_EQ(nullptr, cloned_dict->AsDictionary()->GetObjectBy("arr")); | 806 EXPECT_EQ(nullptr, cloned_dict->AsDictionary()->GetObjectBy("arr")); |
769 } | 807 } |
770 { | 808 { |
771 std::unique_ptr<CPDF_IndirectObjectHolder> m_ObjHolder( | 809 CPDF_IndirectObjectHolder objects_holder; |
772 new CPDF_IndirectObjectHolder()); | |
773 // Create an object with a reference loop. | 810 // Create an object with a reference loop. |
774 CPDF_Dictionary* dict_obj = new CPDF_Dictionary; | 811 CPDF_Dictionary* dict_obj = new CPDF_Dictionary; |
775 CPDF_Array* arr_obj = new CPDF_Array; | 812 CPDF_Array* arr_obj = new CPDF_Array; |
776 m_ObjHolder->AddIndirectObject(dict_obj); | 813 objects_holder.AddIndirectObject(dict_obj); |
777 EXPECT_EQ(1u, dict_obj->GetObjNum()); | 814 EXPECT_EQ(1u, dict_obj->GetObjNum()); |
778 dict_obj->SetAt("arr", arr_obj); | 815 dict_obj->SetAt("arr", arr_obj); |
779 arr_obj->InsertAt(0, dict_obj, m_ObjHolder.get()); | 816 arr_obj->InsertAt(0, dict_obj, &objects_holder); |
780 CPDF_Object* elem0 = arr_obj->GetObjectAt(0); | 817 CPDF_Object* elem0 = arr_obj->GetObjectAt(0); |
781 ASSERT_TRUE(elem0); | 818 ASSERT_TRUE(elem0); |
782 ASSERT_TRUE(elem0->IsReference()); | 819 ASSERT_TRUE(elem0->IsReference()); |
783 EXPECT_EQ(1u, elem0->AsReference()->GetRefObjNum()); | 820 EXPECT_EQ(1u, elem0->AsReference()->GetRefObjNum()); |
784 EXPECT_EQ(dict_obj, elem0->AsReference()->GetDirect()); | 821 EXPECT_EQ(dict_obj, elem0->AsReference()->GetDirect()); |
785 | 822 |
786 // Clone this object to see whether stack overflow will be triggered. | 823 // Clone this object to see whether stack overflow will be triggered. |
787 ScopedDict cloned_dict(ToDictionary(dict_obj->CloneDirectObject())); | 824 ScopedDict cloned_dict(ToDictionary(dict_obj->CloneDirectObject())); |
788 // Cloned object should be the same as the original. | 825 // Cloned object should be the same as the original. |
789 ASSERT_TRUE(cloned_dict); | 826 ASSERT_TRUE(cloned_dict); |
790 CPDF_Object* cloned_arr = cloned_dict->GetObjectBy("arr"); | 827 CPDF_Object* cloned_arr = cloned_dict->GetObjectBy("arr"); |
791 ASSERT_TRUE(cloned_arr); | 828 ASSERT_TRUE(cloned_arr); |
792 ASSERT_TRUE(cloned_arr->IsArray()); | 829 ASSERT_TRUE(cloned_arr->IsArray()); |
793 EXPECT_EQ(1u, cloned_arr->AsArray()->GetCount()); | 830 EXPECT_EQ(1u, cloned_arr->AsArray()->GetCount()); |
794 // Recursively referenced object is not cloned. | 831 // Recursively referenced object is not cloned. |
795 EXPECT_EQ(nullptr, cloned_arr->AsArray()->GetObjectAt(0)); | 832 EXPECT_EQ(nullptr, cloned_arr->AsArray()->GetObjectAt(0)); |
796 } | 833 } |
797 } | 834 } |
OLD | NEW |