OLD | NEW |
(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 <vector> |
| 9 |
| 10 #include "core/include/fxcrt/fx_basic.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 class PDFObjectsTest : public testing::Test { |
| 14 public: |
| 15 void SetUp() override { |
| 16 // Initialize different kinds of objects. |
| 17 // Boolean objects. |
| 18 CPDF_Boolean* boolean_false_obj = new CPDF_Boolean(false); |
| 19 CPDF_Boolean* boolean_true_obj = new CPDF_Boolean(true); |
| 20 // Number objects. |
| 21 CPDF_Number* number_int_obj = new CPDF_Number(1245); |
| 22 CPDF_Number* number_float_obj = new CPDF_Number(9.00345f); |
| 23 // String objects. |
| 24 CPDF_String* str_reg_obj = new CPDF_String(L"A simple test"); |
| 25 CPDF_String* str_spec_obj = new CPDF_String(L"\t\n"); |
| 26 // Name object. |
| 27 CPDF_Name* name_obj = new CPDF_Name("space"); |
| 28 // Array object. |
| 29 CPDF_Array* array_obj = new CPDF_Array; |
| 30 array_obj->InsertAt(0, new CPDF_Number(8902)); |
| 31 array_obj->InsertAt(1, new CPDF_Name("address")); |
| 32 // Dictionary object. |
| 33 m_DictObj = new CPDF_Dictionary; |
| 34 m_DictObj->SetAt("bool", new CPDF_Boolean(false)); |
| 35 m_DictObj->SetAt("num", new CPDF_Number(0.23f)); |
| 36 // Stream object. |
| 37 const char content[] = "abcdefghijklmnopqrstuvwxyz"; |
| 38 size_t buf_len = FX_ArraySize(content); |
| 39 uint8_t* buf = reinterpret_cast<uint8_t*>(malloc(buf_len)); |
| 40 memcpy(buf, content, buf_len); |
| 41 m_StreamDictObj = new CPDF_Dictionary; |
| 42 m_StreamDictObj->SetAt("key1", new CPDF_String(L" test dict")); |
| 43 m_StreamDictObj->SetAt("key2", new CPDF_Number(-1)); |
| 44 CPDF_Stream* stream_obj = new CPDF_Stream(buf, buf_len, m_StreamDictObj); |
| 45 // Null Object. |
| 46 CPDF_Null* null_obj = new CPDF_Null; |
| 47 // All direct objects. |
| 48 CPDF_Object* objs[] = {boolean_false_obj, boolean_true_obj, number_int_obj, |
| 49 number_float_obj, str_reg_obj, str_spec_obj, |
| 50 name_obj, array_obj, m_DictObj, |
| 51 stream_obj, null_obj}; |
| 52 for (int i = 0; i < FX_ArraySize(objs); ++i) |
| 53 m_DirectObjs.emplace_back(objs[i]); |
| 54 |
| 55 // Indirect references to indirect objects. |
| 56 m_ObjHolder.reset(new CPDF_IndirectObjectHolder(nullptr)); |
| 57 CPDF_Object* referred_objs[] = { |
| 58 boolean_true_obj, number_int_obj, str_spec_obj, name_obj, |
| 59 array_obj, m_DictObj, stream_obj}; |
| 60 for (int i = 0; i < FX_ArraySize(referred_objs); ++i) { |
| 61 m_ObjHolder->AddIndirectObject(referred_objs[i]); |
| 62 m_RefObjs.emplace_back( |
| 63 new CPDF_Reference(m_ObjHolder.get(), referred_objs[i]->GetObjNum())); |
| 64 } |
| 65 } |
| 66 |
| 67 protected: |
| 68 using ScopedObj = std::unique_ptr<CPDF_Object, ReleaseDeleter<CPDF_Object>>; |
| 69 |
| 70 // m_ObjHolder needs to be declared first and destructed last since it also |
| 71 // refers to some objects in m_DirectObjs. |
| 72 std::unique_ptr<CPDF_IndirectObjectHolder> m_ObjHolder; |
| 73 std::vector<ScopedObj> m_DirectObjs; |
| 74 std::vector<ScopedObj> m_RefObjs; |
| 75 CPDF_Dictionary* m_DictObj; |
| 76 CPDF_Dictionary* m_StreamDictObj; |
| 77 }; |
| 78 |
| 79 TEST_F(PDFObjectsTest, GetString) { |
| 80 const char* direct_obj_results[] = { |
| 81 "false", "true", "1245", "9.00345", "A simple test", "\t\n", "space", |
| 82 "", "", "", ""}; |
| 83 // Check for direct objects. |
| 84 for (int i = 0; i < m_DirectObjs.size(); ++i) |
| 85 EXPECT_STREQ(m_DirectObjs[i]->GetString().c_str(), direct_obj_results[i]); |
| 86 |
| 87 // Check indirect references. |
| 88 const char* indirect_obj_results[] = {"true", "1245", "\t\n", "space", |
| 89 "", "", ""}; |
| 90 for (int i = 0; i < m_RefObjs.size(); ++i) { |
| 91 EXPECT_STREQ(m_RefObjs[i]->GetString().c_str(), indirect_obj_results[i]); |
| 92 } |
| 93 } |
| 94 |
| 95 TEST_F(PDFObjectsTest, GetConstString) { |
| 96 const char* direct_obj_results[] = { |
| 97 nullptr, nullptr, nullptr, nullptr, "A simple test", "\t\n", |
| 98 "space", nullptr, nullptr, nullptr, nullptr}; |
| 99 // Check for direct objects. |
| 100 for (int i = 0; i < m_DirectObjs.size(); ++i) { |
| 101 if (!direct_obj_results[i]) { |
| 102 EXPECT_EQ(m_DirectObjs[i]->GetConstString().GetCStr(), |
| 103 direct_obj_results[i]); |
| 104 } else { |
| 105 EXPECT_STREQ(m_DirectObjs[i]->GetConstString().GetCStr(), |
| 106 direct_obj_results[i]); |
| 107 } |
| 108 } |
| 109 // Check indirect references. |
| 110 const char* indirect_obj_results[] = {nullptr, nullptr, "\t\n", "space", |
| 111 nullptr, nullptr, nullptr}; |
| 112 for (int i = 0; i < m_RefObjs.size(); ++i) { |
| 113 if (!indirect_obj_results[i]) |
| 114 EXPECT_EQ(m_RefObjs[i]->GetConstString().GetCStr(), nullptr); |
| 115 else { |
| 116 EXPECT_STREQ(m_RefObjs[i]->GetConstString().GetCStr(), |
| 117 indirect_obj_results[i]); |
| 118 } |
| 119 } |
| 120 } |
| 121 |
| 122 TEST_F(PDFObjectsTest, GetNumber) { |
| 123 const FX_FLOAT direct_obj_results[] = {0, 0, 1245, 9.00345f, 0, 0, |
| 124 0, 0, 0, 0, 0}; |
| 125 // Check for direct objects. |
| 126 for (int i = 0; i < m_DirectObjs.size(); ++i) |
| 127 EXPECT_EQ(m_DirectObjs[i]->GetNumber(), direct_obj_results[i]); |
| 128 |
| 129 // Check indirect references. |
| 130 const FX_FLOAT indirect_obj_results[] = {0, 1245, 0, 0, 0, 0, 0}; |
| 131 for (int i = 0; i < m_RefObjs.size(); ++i) |
| 132 EXPECT_EQ(m_RefObjs[i]->GetNumber(), indirect_obj_results[i]); |
| 133 } |
| 134 |
| 135 TEST_F(PDFObjectsTest, GetInteger) { |
| 136 const int direct_obj_results[] = {0, 1, 1245, 9, 0, 0, 0, 0, 0, 0, 0}; |
| 137 // Check for direct objects. |
| 138 for (int i = 0; i < m_DirectObjs.size(); ++i) |
| 139 EXPECT_EQ(m_DirectObjs[i]->GetInteger(), direct_obj_results[i]); |
| 140 |
| 141 // Check indirect references. |
| 142 const int indirect_obj_results[] = {1, 1245, 0, 0, 0, 0, 0}; |
| 143 for (int i = 0; i < m_RefObjs.size(); ++i) |
| 144 EXPECT_EQ(m_RefObjs[i]->GetInteger(), indirect_obj_results[i]); |
| 145 } |
| 146 |
| 147 TEST_F(PDFObjectsTest, GetDict) { |
| 148 const CPDF_Dictionary* direct_obj_results[] = { |
| 149 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, |
| 150 nullptr, nullptr, m_DictObj, m_StreamDictObj, nullptr}; |
| 151 // Check for direct objects. |
| 152 for (int i = 0; i < m_DirectObjs.size(); ++i) |
| 153 EXPECT_EQ(m_DirectObjs[i]->GetDict(), direct_obj_results[i]); |
| 154 |
| 155 // Check indirect references. |
| 156 const CPDF_Dictionary* indirect_obj_results[] = { |
| 157 nullptr, nullptr, nullptr, nullptr, nullptr, m_DictObj, m_StreamDictObj}; |
| 158 for (int i = 0; i < m_RefObjs.size(); ++i) |
| 159 EXPECT_EQ(m_RefObjs[i]->GetDict(), indirect_obj_results[i]); |
| 160 } |
OLD | NEW |