Chromium Code Reviews| 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" |
| 11 #include "core/fpdfapi/fpdf_parser/include/cpdf_reference.h" | 11 #include "core/fpdfapi/fpdf_parser/include/cpdf_reference.h" |
| 12 #include "core/fpdfapi/fpdf_parser/include/cpdf_stream.h" | 12 #include "core/fpdfapi/fpdf_parser/include/cpdf_stream.h" |
| 13 #include "core/fpdfapi/fpdf_parser/include/cpdf_string.h" | 13 #include "core/fpdfapi/fpdf_parser/include/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/include/cpdf_indirect_object_holder.h" | 19 #include "core/fpdfapi/fpdf_parser/include/cpdf_indirect_object_holder.h" |
| 20 #include "core/fxcrt/include/fx_basic.h" | 20 #include "core/fxcrt/include/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 = | |
| 27 std::unique_ptr<CPDF_Dictionary, ReleaseDeleter<CPDF_Dictionary>>; | |
| 26 | 28 |
| 27 void TestArrayAccessors(const CPDF_Array* arr, | 29 void TestArrayAccessors(const CPDF_Array* arr, |
| 28 size_t index, | 30 size_t index, |
| 29 const char* str_val, | 31 const char* str_val, |
| 30 const char* const_str_val, | 32 const char* const_str_val, |
| 31 int int_val, | 33 int int_val, |
| 32 float float_val, | 34 float float_val, |
| 33 CPDF_Array* arr_val, | 35 CPDF_Array* arr_val, |
| 34 CPDF_Dictionary* dict_val, | 36 CPDF_Dictionary* dict_val, |
| 35 CPDF_Stream* stream_val) { | 37 CPDF_Stream* stream_val) { |
| (...skipping 701 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 737 EXPECT_EQ(arr->GetCount(), arr1->GetCount()); | 739 EXPECT_EQ(arr->GetCount(), arr1->GetCount()); |
| 738 for (size_t i = 0; i < arr->GetCount(); ++i) { | 740 for (size_t i = 0; i < arr->GetCount(); ++i) { |
| 739 EXPECT_EQ(CPDF_Object::REFERENCE, arr->GetObjectAt(i)->GetType()); | 741 EXPECT_EQ(CPDF_Object::REFERENCE, arr->GetObjectAt(i)->GetType()); |
| 740 EXPECT_EQ(indirect_objs[i], arr->GetObjectAt(i)->GetDirect()); | 742 EXPECT_EQ(indirect_objs[i], arr->GetObjectAt(i)->GetDirect()); |
| 741 EXPECT_EQ(indirect_objs[i], arr->GetDirectObjectAt(i)); | 743 EXPECT_EQ(indirect_objs[i], arr->GetDirectObjectAt(i)); |
| 742 EXPECT_EQ(CPDF_Object::REFERENCE, arr1->GetObjectAt(i)->GetType()); | 744 EXPECT_EQ(CPDF_Object::REFERENCE, arr1->GetObjectAt(i)->GetType()); |
| 743 EXPECT_EQ(indirect_objs[i], arr1->GetObjectAt(i)->GetDirect()); | 745 EXPECT_EQ(indirect_objs[i], arr1->GetObjectAt(i)->GetDirect()); |
| 744 EXPECT_EQ(indirect_objs[i], arr1->GetDirectObjectAt(i)); | 746 EXPECT_EQ(indirect_objs[i], arr1->GetDirectObjectAt(i)); |
| 745 } | 747 } |
| 746 } | 748 } |
| 749 | |
| 750 TEST_F(PDFObjectsTest, CloneCheckLoop) { | |
| 751 { | |
| 752 // Create an object with a reference loop. | |
| 753 ScopedArray arr_obj(new CPDF_Array); | |
| 754 // Dictionary object. | |
| 755 CPDF_Dictionary* dict_obj = new CPDF_Dictionary; | |
| 756 dict_obj->SetAt("arr", arr_obj.get()); | |
| 757 arr_obj->InsertAt(0, dict_obj); | |
| 758 | |
| 759 // Clone this object to see whether stack overflow will be triggered. | |
| 760 ScopedArray cloned_array(arr_obj->Clone()->AsArray()); | |
| 761 // Cloned object should be the same as the original. | |
| 762 EXPECT_EQ(1u, cloned_array->GetCount()); | |
|
Lei Zhang
2016/08/18 15:16:44
ASSERT_TRUE(cloned_array); ditto below with |clone
Wei Li
2016/08/18 22:02:30
Done.
| |
| 763 CPDF_Object* cloned_dict = cloned_array->GetObjectAt(0); | |
| 764 ASSERT_TRUE(cloned_dict->IsDictionary()); | |
| 765 // Recursively referenced object is not cloned. | |
| 766 EXPECT_EQ(nullptr, cloned_dict->AsDictionary()->GetObjectBy("arr")); | |
|
Lei Zhang
2016/08/18 15:16:44
Or just EXPECT_FALSE(...);
Wei Li
2016/08/18 22:02:30
Yes, that would work too. Will keep "EXPECT_EQ(nul
| |
| 767 } | |
| 768 { | |
| 769 m_ObjHolder.reset(new CPDF_IndirectObjectHolder()); | |
| 770 // Create an object with a reference loop. | |
| 771 CPDF_Dictionary* dict_obj = new CPDF_Dictionary; | |
| 772 CPDF_Array* arr_obj = new CPDF_Array; | |
| 773 m_ObjHolder->AddIndirectObject(dict_obj); | |
| 774 EXPECT_EQ(1u, dict_obj->GetObjNum()); | |
| 775 m_ObjHolder->AddIndirectObject(arr_obj); | |
| 776 dict_obj->SetAt("arr", arr_obj); | |
| 777 arr_obj->InsertAt(0, dict_obj, m_ObjHolder.get()); | |
| 778 EXPECT_TRUE(arr_obj->GetObjectAt(0)->IsReference()); | |
| 779 EXPECT_EQ(1u, arr_obj->GetObjectAt(0)->AsReference()->GetRefObjNum()); | |
| 780 EXPECT_EQ(dict_obj, arr_obj->GetObjectAt(0)->AsReference()->GetDirect()); | |
| 781 | |
| 782 // Clone this object to see whether stack overflow will be triggered. | |
| 783 ScopedDict cloned_dict(dict_obj->CloneDeRef(true)->AsDictionary()); | |
| 784 // Cloned object should be the same as the original. | |
| 785 CPDF_Object* cloned_arr = cloned_dict->GetObjectBy("arr"); | |
| 786 ASSERT_TRUE(cloned_arr->IsArray()); | |
| 787 EXPECT_EQ(1u, cloned_arr->AsArray()->GetCount()); | |
| 788 // Recursively referenced object is not cloned. | |
| 789 EXPECT_EQ(nullptr, cloned_arr->AsArray()->GetObjectAt(0)); | |
| 790 } | |
| 791 } | |
| OLD | NEW |