1 // Copyright 2018 The PDFium Authors
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 "fxjs/cfx_globaldata.h"
6
7 #include <stdint.h>
8
9 #include <utility>
10 #include <vector>
11
12 #include "core/fxcrt/data_vector.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace {
16
17 class TestDelegate : public CFX_GlobalData::Delegate {
18 public:
19 TestDelegate() = default;
20 ~TestDelegate() override = default;
21
StoreBuffer(pdfium::span<const uint8_t> buffer)22 bool StoreBuffer(pdfium::span<const uint8_t> buffer) override {
23 last_buffer_ = DataVector<uint8_t>(buffer.begin(), buffer.end());
24 return true;
25 }
LoadBuffer()26 absl::optional<pdfium::span<uint8_t>> LoadBuffer() override {
27 return pdfium::span<uint8_t>(last_buffer_);
28 }
BufferDone()29 void BufferDone() override {
30 // Catch misuse after done.
31 last_buffer_ = DataVector<uint8_t>();
32 }
33
34 DataVector<uint8_t> last_buffer_;
35 };
36
37 } // namespace
38
TEST(CFXGlobalData,GetSafety)39 TEST(CFXGlobalData, GetSafety) {
40 CFX_GlobalData* pInstance = CFX_GlobalData::GetRetainedInstance(nullptr);
41 EXPECT_FALSE(pInstance->GetGlobalVariable("nonesuch"));
42 EXPECT_FALSE(pInstance->GetAt(-1));
43 EXPECT_FALSE(pInstance->GetAt(0));
44 EXPECT_FALSE(pInstance->GetAt(1));
45
46 pInstance->SetGlobalVariableNumber("double", 2.0);
47 pInstance->SetGlobalVariableString("string", "clams");
48
49 EXPECT_FALSE(pInstance->GetGlobalVariable("nonesuch"));
50 EXPECT_FALSE(pInstance->GetAt(-1));
51 EXPECT_EQ(pInstance->GetGlobalVariable("double"), pInstance->GetAt(0));
52 EXPECT_EQ(pInstance->GetGlobalVariable("string"), pInstance->GetAt(1));
53 EXPECT_FALSE(pInstance->GetAt(2));
54
55 ASSERT_TRUE(pInstance->Release());
56 }
57
TEST(CFXGlobalData,StoreReload)58 TEST(CFXGlobalData, StoreReload) {
59 TestDelegate delegate;
60 std::vector<std::unique_ptr<CFX_KeyValue>> array;
61 CFX_GlobalData* pInstance = CFX_GlobalData::GetRetainedInstance(&delegate);
62 pInstance->SetGlobalVariableNumber("double", 2.0);
63 pInstance->SetGlobalVariableString("string", "clams");
64 pInstance->SetGlobalVariableBoolean("boolean", true);
65 pInstance->SetGlobalVariableNull("null");
66 pInstance->SetGlobalVariableObject("array", std::move(array));
67 pInstance->SetGlobalVariablePersistent("double", true);
68 pInstance->SetGlobalVariablePersistent("string", true);
69 pInstance->SetGlobalVariablePersistent("boolean", true);
70 pInstance->SetGlobalVariablePersistent("null", true);
71 pInstance->SetGlobalVariablePersistent("array", true);
72 ASSERT_TRUE(pInstance->Release());
73
74 pInstance = CFX_GlobalData::GetRetainedInstance(&delegate);
75 auto* element = pInstance->GetAt(0);
76 ASSERT_TRUE(element);
77 EXPECT_EQ("double", element->data.sKey);
78 EXPECT_EQ(CFX_Value::DataType::kNumber, element->data.nType);
79 EXPECT_EQ(2.0, element->data.dData);
80
81 element = pInstance->GetAt(1);
82 ASSERT_TRUE(element);
83 EXPECT_EQ("string", element->data.sKey);
84 EXPECT_EQ(CFX_Value::DataType::kString, element->data.nType);
85 EXPECT_EQ("clams", element->data.sData);
86
87 element = pInstance->GetAt(2);
88 ASSERT_TRUE(element);
89 EXPECT_EQ("boolean", element->data.sKey);
90 EXPECT_EQ(CFX_Value::DataType::kBoolean, element->data.nType);
91 EXPECT_EQ(true, element->data.bData);
92
93 element = pInstance->GetAt(3);
94 ASSERT_TRUE(element);
95 EXPECT_EQ("null", element->data.sKey);
96 EXPECT_EQ(CFX_Value::DataType::kNull, element->data.nType);
97
98 // Arrays don't get persisted.
99 element = pInstance->GetAt(4);
100 ASSERT_FALSE(element);
101
102 ASSERT_TRUE(pInstance->Release());
103 }
104
TEST(CFXGlobalData,ResetValues)105 TEST(CFXGlobalData, ResetValues) {
106 CFX_GlobalData* pInstance = CFX_GlobalData::GetRetainedInstance(nullptr);
107 pInstance->SetGlobalVariableString("double", "bogus!!!");
108 pInstance->SetGlobalVariableString("string", "bogus!!!");
109 pInstance->SetGlobalVariableString("boolean", "bogus!!!");
110 pInstance->SetGlobalVariableString("null", "bogus!!!");
111
112 pInstance->SetGlobalVariableNumber("double", 2.0);
113 pInstance->SetGlobalVariableString("string", "clams");
114 pInstance->SetGlobalVariableBoolean("boolean", true);
115 pInstance->SetGlobalVariableNull("null");
116
117 auto* element = pInstance->GetAt(0);
118 ASSERT_TRUE(element);
119 EXPECT_EQ("double", element->data.sKey);
120 EXPECT_EQ(CFX_Value::DataType::kNumber, element->data.nType);
121 EXPECT_EQ(2.0, element->data.dData);
122
123 element = pInstance->GetAt(1);
124 ASSERT_TRUE(element);
125 EXPECT_EQ("string", element->data.sKey);
126 EXPECT_EQ(CFX_Value::DataType::kString, element->data.nType);
127 EXPECT_EQ("clams", element->data.sData);
128
129 element = pInstance->GetAt(2);
130 ASSERT_TRUE(element);
131 EXPECT_EQ("boolean", element->data.sKey);
132 EXPECT_EQ(CFX_Value::DataType::kBoolean, element->data.nType);
133 EXPECT_EQ(true, element->data.bData);
134
135 element = pInstance->GetAt(3);
136 ASSERT_TRUE(element);
137 EXPECT_EQ("null", element->data.sKey);
138 EXPECT_EQ(CFX_Value::DataType::kNull, element->data.nType);
139
140 ASSERT_TRUE(pInstance->Release());
141 }
142
TEST(CFXGlobalData,DeleteValues)143 TEST(CFXGlobalData, DeleteValues) {
144 CFX_GlobalData* pInstance = CFX_GlobalData::GetRetainedInstance(nullptr);
145 pInstance->SetGlobalVariableNumber("double", 2.0);
146 pInstance->SetGlobalVariableString("string", "clams");
147 pInstance->SetGlobalVariableBoolean("boolean", true);
148 pInstance->SetGlobalVariableNull("null");
149 EXPECT_EQ(4, pInstance->GetSize());
150
151 pInstance->DeleteGlobalVariable("nonesuch");
152 EXPECT_EQ(4, pInstance->GetSize());
153
154 pInstance->DeleteGlobalVariable("boolean");
155 EXPECT_EQ(3, pInstance->GetSize());
156
157 pInstance->DeleteGlobalVariable("string");
158 EXPECT_EQ(2, pInstance->GetSize());
159
160 pInstance->DeleteGlobalVariable("double");
161 EXPECT_EQ(1, pInstance->GetSize());
162
163 pInstance->DeleteGlobalVariable("null");
164 EXPECT_EQ(0, pInstance->GetSize());
165
166 ASSERT_TRUE(pInstance->Release());
167 }
168