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

Side by Side Diff: core/fpdfapi/parser/cpdf_object_unittest.cpp

Issue 2520493002: Make CPDF_Stream() take unique_ptr's to its dictionary. (Closed)
Patch Set: rebase, lint, fix new test. Created 4 years 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
« no previous file with comments | « core/fpdfapi/page/cpdf_streamparser.cpp ('k') | core/fpdfapi/parser/cpdf_stream.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/parser/cpdf_array.h" 5 #include "core/fpdfapi/parser/cpdf_array.h"
6 #include "core/fpdfapi/parser/cpdf_boolean.h" 6 #include "core/fpdfapi/parser/cpdf_boolean.h"
7 #include "core/fpdfapi/parser/cpdf_dictionary.h" 7 #include "core/fpdfapi/parser/cpdf_dictionary.h"
8 #include "core/fpdfapi/parser/cpdf_name.h" 8 #include "core/fpdfapi/parser/cpdf_name.h"
9 #include "core/fpdfapi/parser/cpdf_null.h" 9 #include "core/fpdfapi/parser/cpdf_null.h"
10 #include "core/fpdfapi/parser/cpdf_number.h" 10 #include "core/fpdfapi/parser/cpdf_number.h"
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 m_ArrayObj->InsertNewAt<CPDF_Name>(1, "address"); 64 m_ArrayObj->InsertNewAt<CPDF_Name>(1, "address");
65 // Dictionary object. 65 // Dictionary object.
66 m_DictObj = new CPDF_Dictionary(); 66 m_DictObj = new CPDF_Dictionary();
67 m_DictObj->SetNewFor<CPDF_Boolean>("bool", false); 67 m_DictObj->SetNewFor<CPDF_Boolean>("bool", false);
68 m_DictObj->SetNewFor<CPDF_Number>("num", 0.23f); 68 m_DictObj->SetNewFor<CPDF_Number>("num", 0.23f);
69 // Stream object. 69 // Stream object.
70 const char content[] = "abcdefghijklmnopqrstuvwxyz"; 70 const char content[] = "abcdefghijklmnopqrstuvwxyz";
71 size_t buf_len = FX_ArraySize(content); 71 size_t buf_len = FX_ArraySize(content);
72 uint8_t* buf = reinterpret_cast<uint8_t*>(malloc(buf_len)); 72 uint8_t* buf = reinterpret_cast<uint8_t*>(malloc(buf_len));
73 memcpy(buf, content, buf_len); 73 memcpy(buf, content, buf_len);
74 m_StreamDictObj = new CPDF_Dictionary(); 74 auto pNewDict = pdfium::MakeUnique<CPDF_Dictionary>();
75 m_StreamDictObj = pNewDict.get();
75 m_StreamDictObj->SetNewFor<CPDF_String>("key1", L" test dict"); 76 m_StreamDictObj->SetNewFor<CPDF_String>("key1", L" test dict");
76 m_StreamDictObj->SetNewFor<CPDF_Number>("key2", -1); 77 m_StreamDictObj->SetNewFor<CPDF_Number>("key2", -1);
77 CPDF_Stream* stream_obj = new CPDF_Stream(buf, buf_len, m_StreamDictObj); 78 CPDF_Stream* stream_obj =
79 new CPDF_Stream(buf, buf_len, std::move(pNewDict));
78 // Null Object. 80 // Null Object.
79 CPDF_Null* null_obj = new CPDF_Null; 81 CPDF_Null* null_obj = new CPDF_Null;
80 // All direct objects. 82 // All direct objects.
81 CPDF_Object* objs[] = {boolean_false_obj, boolean_true_obj, number_int_obj, 83 CPDF_Object* objs[] = {boolean_false_obj, boolean_true_obj, number_int_obj,
82 number_float_obj, str_reg_obj, str_spec_obj, 84 number_float_obj, str_reg_obj, str_spec_obj,
83 name_obj, m_ArrayObj, m_DictObj, 85 name_obj, m_ArrayObj, m_DictObj,
84 stream_obj, null_obj}; 86 stream_obj, null_obj};
85 m_DirectObjTypes = { 87 m_DirectObjTypes = {
86 CPDF_Object::BOOLEAN, CPDF_Object::BOOLEAN, CPDF_Object::NUMBER, 88 CPDF_Object::BOOLEAN, CPDF_Object::BOOLEAN, CPDF_Object::NUMBER,
87 CPDF_Object::NUMBER, CPDF_Object::STRING, CPDF_Object::STRING, 89 CPDF_Object::NUMBER, CPDF_Object::STRING, CPDF_Object::STRING,
(...skipping 493 matching lines...) Expand 10 before | Expand all | Expand 10 after
581 std::string key("key"); 583 std::string key("key");
582 char buf[33]; 584 char buf[33];
583 key.append(FXSYS_itoa(j, buf, 10)); 585 key.append(FXSYS_itoa(j, buf, 10));
584 int value = j + 200; 586 int value = j + 200;
585 vals[i]->SetNewFor<CPDF_Number>(key.c_str(), value); 587 vals[i]->SetNewFor<CPDF_Number>(key.c_str(), value);
586 } 588 }
587 uint8_t content[] = "content: this is a stream"; 589 uint8_t content[] = "content: this is a stream";
588 size_t data_size = FX_ArraySize(content); 590 size_t data_size = FX_ArraySize(content);
589 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(data_size)); 591 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(data_size));
590 memcpy(data, content, data_size); 592 memcpy(data, content, data_size);
591 stream_vals[i] = arr->AddNew<CPDF_Stream>(data, data_size, vals[i]); 593 stream_vals[i] = arr->AddNew<CPDF_Stream>(data, data_size,
594 pdfium::WrapUnique(vals[i]));
592 } 595 }
593 for (size_t i = 0; i < 3; ++i) { 596 for (size_t i = 0; i < 3; ++i) {
594 TestArrayAccessors(arr.get(), i, // Array and index. 597 TestArrayAccessors(arr.get(), i, // Array and index.
595 "", // String value. 598 "", // String value.
596 nullptr, // Const string value. 599 nullptr, // Const string value.
597 0, // Integer value. 600 0, // Integer value.
598 0, // Float value. 601 0, // Float value.
599 nullptr, // Array value. 602 nullptr, // Array value.
600 vals[i], // Dictionary value. 603 vals[i], // Dictionary value.
601 stream_vals[i]); // Stream value. 604 stream_vals[i]); // Stream value.
(...skipping 24 matching lines...) Expand all
626 629
627 CPDF_Dictionary* stream_dict = new CPDF_Dictionary(); 630 CPDF_Dictionary* stream_dict = new CPDF_Dictionary();
628 stream_dict->SetNewFor<CPDF_String>("key1", "John", false); 631 stream_dict->SetNewFor<CPDF_String>("key1", "John", false);
629 stream_dict->SetNewFor<CPDF_String>("key2", "King", false); 632 stream_dict->SetNewFor<CPDF_String>("key2", "King", false);
630 uint8_t data[] = "A stream for test"; 633 uint8_t data[] = "A stream for test";
631 // The data buffer will be owned by stream object, so it needs to be 634 // The data buffer will be owned by stream object, so it needs to be
632 // dynamically allocated. 635 // dynamically allocated.
633 size_t buf_size = sizeof(data); 636 size_t buf_size = sizeof(data);
634 uint8_t* buf = reinterpret_cast<uint8_t*>(malloc(buf_size)); 637 uint8_t* buf = reinterpret_cast<uint8_t*>(malloc(buf_size));
635 memcpy(buf, data, buf_size); 638 memcpy(buf, data, buf_size);
636 CPDF_Stream* stream_val = 639 CPDF_Stream* stream_val = arr->InsertNewAt<CPDF_Stream>(
637 arr->InsertNewAt<CPDF_Stream>(13, buf, buf_size, stream_dict); 640 13, buf, buf_size, pdfium::WrapUnique(stream_dict));
638 const char* const expected_str[] = { 641 const char* const expected_str[] = {
639 "true", "false", "0", "-1234", "2345", "0.05", "", 642 "true", "false", "0", "-1234", "2345", "0.05", "",
640 "It is a test!", "NAME", "test", "", "", "", ""}; 643 "It is a test!", "NAME", "test", "", "", "", ""};
641 const int expected_int[] = {1, 0, 0, -1234, 2345, 0, 0, 644 const int expected_int[] = {1, 0, 0, -1234, 2345, 0, 0,
642 0, 0, 0, 0, 0, 0, 0}; 645 0, 0, 0, 0, 0, 0, 0};
643 const float expected_float[] = {0, 0, 0, -1234, 2345, 0.05f, 0, 646 const float expected_float[] = {0, 0, 0, -1234, 2345, 0.05f, 0,
644 0, 0, 0, 0, 0, 0, 0}; 647 0, 0, 0, 0, 0, 0, 0};
645 for (size_t i = 0; i < arr->GetCount(); ++i) { 648 for (size_t i = 0; i < arr->GetCount(); ++i) {
646 EXPECT_STREQ(expected_str[i], arr->GetStringAt(i).c_str()); 649 EXPECT_STREQ(expected_str[i], arr->GetStringAt(i).c_str());
647 EXPECT_EQ(expected_int[i], arr->GetIntegerAt(i)); 650 EXPECT_EQ(expected_int[i], arr->GetIntegerAt(i));
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
809 // Cloned object should be the same as the original. 812 // Cloned object should be the same as the original.
810 ASSERT_TRUE(cloned_array); 813 ASSERT_TRUE(cloned_array);
811 EXPECT_EQ(1u, cloned_array->GetCount()); 814 EXPECT_EQ(1u, cloned_array->GetCount());
812 CPDF_Object* cloned_dict = cloned_array->GetObjectAt(0); 815 CPDF_Object* cloned_dict = cloned_array->GetObjectAt(0);
813 ASSERT_TRUE(cloned_dict); 816 ASSERT_TRUE(cloned_dict);
814 ASSERT_TRUE(cloned_dict->IsDictionary()); 817 ASSERT_TRUE(cloned_dict->IsDictionary());
815 // Recursively referenced object is not cloned. 818 // Recursively referenced object is not cloned.
816 EXPECT_EQ(nullptr, cloned_dict->AsDictionary()->GetObjectFor("arr")); 819 EXPECT_EQ(nullptr, cloned_dict->AsDictionary()->GetObjectFor("arr"));
817 } 820 }
818 { 821 {
819 // Create a dictionary/stream pair with a reference loop. 822 // Create a dictionary/stream pair with a reference loop. It takes
823 // some work to do this nowadays, in particular we need the
824 // anti-pattern pdfium::WrapUnique(dict.get()).
820 auto dict_obj = pdfium::MakeUnique<CPDF_Dictionary>(); 825 auto dict_obj = pdfium::MakeUnique<CPDF_Dictionary>();
821 CPDF_Stream* stream_obj = 826 CPDF_Stream* stream_obj = dict_obj->SetNewFor<CPDF_Stream>(
822 dict_obj->SetNewFor<CPDF_Stream>("stream", nullptr, 0, dict_obj.get()); 827 "stream", nullptr, 0, pdfium::WrapUnique(dict_obj.get()));
823 // Clone this object to see whether stack overflow will be triggered. 828 // Clone this object to see whether stack overflow will be triggered.
824 std::unique_ptr<CPDF_Stream> cloned_stream = ToStream(stream_obj->Clone()); 829 std::unique_ptr<CPDF_Stream> cloned_stream = ToStream(stream_obj->Clone());
825 // Cloned object should be the same as the original. 830 // Cloned object should be the same as the original.
826 ASSERT_TRUE(cloned_stream); 831 ASSERT_TRUE(cloned_stream);
827 CPDF_Object* cloned_dict = cloned_stream->GetDict(); 832 CPDF_Object* cloned_dict = cloned_stream->GetDict();
828 ASSERT_TRUE(cloned_dict); 833 ASSERT_TRUE(cloned_dict);
829 ASSERT_TRUE(cloned_dict->IsDictionary()); 834 ASSERT_TRUE(cloned_dict->IsDictionary());
830 // Recursively referenced object is not cloned. 835 // Recursively referenced object is not cloned.
831 EXPECT_EQ(nullptr, cloned_dict->AsDictionary()->GetObjectFor("stream")); 836 EXPECT_EQ(nullptr, cloned_dict->AsDictionary()->GetObjectFor("stream"));
832 } 837 }
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
865 CPDF_Object* pObj = dict->SetNewFor<CPDF_Number>("clams", 42); 870 CPDF_Object* pObj = dict->SetNewFor<CPDF_Number>("clams", 42);
866 dict->ConvertToIndirectObjectFor("clams", &objects_holder); 871 dict->ConvertToIndirectObjectFor("clams", &objects_holder);
867 CPDF_Object* pRef = dict->GetObjectFor("clams"); 872 CPDF_Object* pRef = dict->GetObjectFor("clams");
868 CPDF_Object* pNum = dict->GetDirectObjectFor("clams"); 873 CPDF_Object* pNum = dict->GetDirectObjectFor("clams");
869 EXPECT_TRUE(pRef->IsReference()); 874 EXPECT_TRUE(pRef->IsReference());
870 EXPECT_TRUE(pNum->IsNumber()); 875 EXPECT_TRUE(pNum->IsNumber());
871 EXPECT_NE(pObj, pRef); 876 EXPECT_NE(pObj, pRef);
872 EXPECT_EQ(pObj, pNum); 877 EXPECT_EQ(pObj, pNum);
873 EXPECT_EQ(42, dict->GetIntegerFor("clams")); 878 EXPECT_EQ(42, dict->GetIntegerFor("clams"));
874 } 879 }
OLDNEW
« no previous file with comments | « core/fpdfapi/page/cpdf_streamparser.cpp ('k') | core/fpdfapi/parser/cpdf_stream.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698