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