1 // Copyright 2015 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <brillo/value_conversion.h>
16
17 #include <algorithm>
18 #include <limits>
19 #include <memory>
20 #include <string>
21 #include <vector>
22
23 #include <base/json/json_reader.h>
24 #include <base/json/json_writer.h>
25 #include <gtest/gtest.h>
26
27 namespace brillo {
28
29 namespace {
30
ParseValue(std::string json)31 std::unique_ptr<base::Value> ParseValue(std::string json) {
32 std::replace(json.begin(), json.end(), '\'', '"');
33 std::string message;
34 auto value = base::JSONReader::ReadAndReturnError(json, base::JSON_PARSE_RFC,
35 nullptr, &message);
36 CHECK(value) << "Failed to load JSON: " << message << ", " << json;
37 return value;
38 }
39
IsEqualValue(const base::Value & val1,const base::Value & val2)40 inline bool IsEqualValue(const base::Value& val1, const base::Value& val2) {
41 return val1.Equals(&val2);
42 }
43
44 #define EXPECT_JSON_EQ(expected, actual) \
45 EXPECT_PRED2(IsEqualValue, *ParseValue(expected), actual)
46
47 } // namespace
48
TEST(ValueConversionTest,FromValueInt)49 TEST(ValueConversionTest, FromValueInt) {
50 int actual;
51 EXPECT_TRUE(FromValue(*ParseValue("123"), &actual));
52 EXPECT_EQ(123, actual);
53
54 EXPECT_TRUE(FromValue(*ParseValue("-123"), &actual));
55 EXPECT_EQ(-123, actual);
56
57 EXPECT_FALSE(FromValue(*ParseValue("true"), &actual));
58 }
59
TEST(ValueConversionTest,FromValueBool)60 TEST(ValueConversionTest, FromValueBool) {
61 bool actual;
62 EXPECT_TRUE(FromValue(*ParseValue("false"), &actual));
63 EXPECT_FALSE(actual);
64
65 EXPECT_TRUE(FromValue(*ParseValue("true"), &actual));
66 EXPECT_TRUE(actual);
67
68 EXPECT_FALSE(FromValue(*ParseValue("0"), &actual));
69 EXPECT_FALSE(FromValue(*ParseValue("1"), &actual));
70 }
71
TEST(ValueConversionTest,FromValueDouble)72 TEST(ValueConversionTest, FromValueDouble) {
73 double actual;
74 EXPECT_TRUE(FromValue(*ParseValue("12.5"), &actual));
75 EXPECT_DOUBLE_EQ(12.5, actual);
76
77 EXPECT_TRUE(FromValue(*ParseValue("-0.1"), &actual));
78 EXPECT_DOUBLE_EQ(-0.1, actual);
79
80 EXPECT_TRUE(FromValue(*ParseValue("17"), &actual));
81 EXPECT_DOUBLE_EQ(17.0, actual);
82
83 EXPECT_FALSE(FromValue(*ParseValue("'1.0'"), &actual));
84 }
85
TEST(ValueConversionTest,FromValueString)86 TEST(ValueConversionTest, FromValueString) {
87 std::string actual;
88 EXPECT_TRUE(FromValue(*ParseValue("'foo'"), &actual));
89 EXPECT_EQ("foo", actual);
90
91 EXPECT_TRUE(FromValue(*ParseValue("'bar'"), &actual));
92 EXPECT_EQ("bar", actual);
93
94 EXPECT_TRUE(FromValue(*ParseValue("''"), &actual));
95 EXPECT_TRUE(actual.empty());
96
97 EXPECT_FALSE(FromValue(*ParseValue("1"), &actual));
98 }
99
TEST(ValueConversionTest,FromValueListValue)100 TEST(ValueConversionTest, FromValueListValue) {
101 const base::ListValue* list = nullptr;
102 auto in_value = ParseValue("[1, 2, 'foo']");
103 EXPECT_TRUE(FromValue(*in_value, &list));
104 EXPECT_JSON_EQ("[1, 2, 'foo']", *list);
105 }
106
TEST(ValueConversionTest,FromValueDictValue)107 TEST(ValueConversionTest, FromValueDictValue) {
108 const base::DictionaryValue* dict = nullptr;
109 auto in_value = ParseValue("{'foo':'bar','baz': 1}");
110 EXPECT_TRUE(FromValue(*in_value, &dict));
111 EXPECT_JSON_EQ("{'foo':'bar','baz': 1}", *dict);
112 }
113
TEST(ValueConversionTest,FromValueListValueUniquePtr)114 TEST(ValueConversionTest, FromValueListValueUniquePtr) {
115 std::unique_ptr<base::ListValue> list;
116 EXPECT_TRUE(FromValue(*ParseValue("[1, 2, 'bar']"), &list));
117 EXPECT_JSON_EQ("[1, 2, 'bar']", *list);
118 }
119
TEST(ValueConversionTest,FromValueDictValueUniquePtr)120 TEST(ValueConversionTest, FromValueDictValueUniquePtr) {
121 std::unique_ptr<base::DictionaryValue> dict;
122 EXPECT_TRUE(FromValue(*ParseValue("{'foo':'bar','baz': 1}"), &dict));
123 EXPECT_JSON_EQ("{'foo':'bar','baz': 1}", *dict);
124 }
125
TEST(ValueConversionTest,FromValueVectorOfInt)126 TEST(ValueConversionTest, FromValueVectorOfInt) {
127 std::vector<int> actual;
128 EXPECT_TRUE(FromValue(*ParseValue("[1, 2, 3, 4]"), &actual));
129 EXPECT_EQ((std::vector<int>{1, 2, 3, 4}), actual);
130
131 EXPECT_TRUE(FromValue(*ParseValue("[]"), &actual));
132 EXPECT_TRUE(actual.empty());
133
134 EXPECT_FALSE(FromValue(*ParseValue("[1, 2, 3, '4']"), &actual));
135 }
136
TEST(ValueConversionTest,FromValueVectorOfBool)137 TEST(ValueConversionTest, FromValueVectorOfBool) {
138 std::vector<bool> actual;
139 EXPECT_TRUE(FromValue(*ParseValue("[true, true, false]"), &actual));
140 EXPECT_EQ((std::vector<bool>{true, true, false}), actual);
141
142 EXPECT_TRUE(FromValue(*ParseValue("[]"), &actual));
143 EXPECT_TRUE(actual.empty());
144
145 EXPECT_FALSE(FromValue(*ParseValue("[true, 0]"), &actual));
146 }
147
TEST(ValueConversionTest,FromValueVectorOfDouble)148 TEST(ValueConversionTest, FromValueVectorOfDouble) {
149 std::vector<double> actual;
150 EXPECT_TRUE(FromValue(*ParseValue("[1, 2.0, 6.5, -11.2]"), &actual));
151 EXPECT_EQ((std::vector<double>{1.0, 2.0, 6.5, -11.2}), actual);
152
153 EXPECT_TRUE(FromValue(*ParseValue("[]"), &actual));
154 EXPECT_TRUE(actual.empty());
155
156 EXPECT_FALSE(FromValue(*ParseValue("['s']"), &actual));
157 }
158
TEST(ValueConversionTest,FromValueVectorOfString)159 TEST(ValueConversionTest, FromValueVectorOfString) {
160 std::vector<std::string> actual;
161 EXPECT_TRUE(FromValue(*ParseValue("['', 'foo', 'bar']"), &actual));
162 EXPECT_EQ((std::vector<std::string>{"", "foo", "bar"}), actual);
163
164 EXPECT_TRUE(FromValue(*ParseValue("[]"), &actual));
165 EXPECT_TRUE(actual.empty());
166
167 EXPECT_FALSE(FromValue(*ParseValue("[100]"), &actual));
168 }
169
TEST(ValueConversionTest,FromValueVectorOfVectors)170 TEST(ValueConversionTest, FromValueVectorOfVectors) {
171 std::vector<std::vector<int>> actual;
172 EXPECT_TRUE(FromValue(*ParseValue("[[1,2], [], [3]]"), &actual));
173 EXPECT_EQ((std::vector<std::vector<int>>{{1,2}, {}, {3}}), actual);
174
175 EXPECT_TRUE(FromValue(*ParseValue("[]"), &actual));
176 EXPECT_TRUE(actual.empty());
177
178 EXPECT_FALSE(FromValue(*ParseValue("[100]"), &actual));
179 }
180
TEST(ValueConversionTest,FromValueMap)181 TEST(ValueConversionTest, FromValueMap) {
182 std::map<std::string, int> actual;
183 EXPECT_TRUE(FromValue(*ParseValue("{'foo':1, 'bar':2, 'baz':3}"), &actual));
184 EXPECT_EQ((std::map<std::string, int>{{"foo", 1}, {"bar", 2}, {"baz", 3}}),
185 actual);
186
187 EXPECT_TRUE(FromValue(*ParseValue("{}"), &actual));
188 EXPECT_TRUE(actual.empty());
189
190 EXPECT_FALSE(FromValue(*ParseValue("{'foo':1, 'bar':'2'}"), &actual));
191 }
192
TEST(ValueConversionTest,FromValueMapOfVectors)193 TEST(ValueConversionTest, FromValueMapOfVectors) {
194 std::map<std::string, std::vector<int>> actual;
195 EXPECT_TRUE(FromValue(*ParseValue("{'foo':[1,2], 'bar':[]}"), &actual));
196 std::map<std::string, std::vector<int>> expected{
197 {"foo", {1, 2}}, {"bar", {}}};
198 EXPECT_EQ(expected, actual);
199
200 EXPECT_TRUE(FromValue(*ParseValue("{}"), &actual));
201 EXPECT_TRUE(actual.empty());
202
203 EXPECT_FALSE(FromValue(*ParseValue("{'foo':[1], 'bar':[2,'3']}"), &actual));
204 }
205
TEST(ValueConversionTest,FromValueVectorOfMaps)206 TEST(ValueConversionTest, FromValueVectorOfMaps) {
207 std::vector<std::map<std::string, int>> actual;
208 EXPECT_TRUE(FromValue(*ParseValue("[{'foo':1,'bar':2},{'baz':3}]"), &actual));
209 std::vector<std::map<std::string, int>> expected{
210 {{"foo", 1}, {"bar", 2}}, {{"baz", 3}}};
211 EXPECT_EQ(expected, actual);
212
213 EXPECT_TRUE(FromValue(*ParseValue("[]"), &actual));
214 EXPECT_TRUE(actual.empty());
215
216 EXPECT_FALSE(FromValue(*ParseValue("[{'foo':1}, 'bar']"), &actual));
217 }
218
TEST(ValueConversionTest,FromValueVectorOfLists)219 TEST(ValueConversionTest, FromValueVectorOfLists) {
220 std::vector<std::unique_ptr<base::ListValue>> actual;
221 EXPECT_TRUE(FromValue(*ParseValue("[['foo',1],['bar',2],[true]]"), &actual));
222 ASSERT_EQ(3, actual.size());
223 EXPECT_JSON_EQ("['foo', 1]", *actual[0]);
224 EXPECT_JSON_EQ("['bar', 2]", *actual[1]);
225 EXPECT_JSON_EQ("[true]", *actual[2]);
226 }
227
TEST(ValueConversionTest,FromValueVectorOfDicts)228 TEST(ValueConversionTest, FromValueVectorOfDicts) {
229 std::vector<std::unique_ptr<base::DictionaryValue>> actual;
230 EXPECT_TRUE(FromValue(*ParseValue("[{'foo': 1}, {'bar': 2}]"), &actual));
231 ASSERT_EQ(2, actual.size());
232 EXPECT_JSON_EQ("{'foo': 1}", *actual[0]);
233 EXPECT_JSON_EQ("{'bar': 2}", *actual[1]);
234 }
235
TEST(ValueConversionTest,ToValueScalar)236 TEST(ValueConversionTest, ToValueScalar) {
237 EXPECT_JSON_EQ("1234", *ToValue(1234));
238 EXPECT_JSON_EQ("true", *ToValue(true));
239 EXPECT_JSON_EQ("false", *ToValue(false));
240 EXPECT_JSON_EQ("12.5", *ToValue(12.5));
241 EXPECT_JSON_EQ("'foobar'", *ToValue("foobar"));
242 }
243
TEST(ValueConversionTest,ToValueVector)244 TEST(ValueConversionTest, ToValueVector) {
245 EXPECT_JSON_EQ("[1, 2, 3]", *ToValue(std::vector<int>{1, 2, 3}));
246 EXPECT_JSON_EQ("[]", *ToValue(std::vector<int>{}));
247 EXPECT_JSON_EQ("[true, false]", *ToValue(std::vector<bool>{true, false}));
248 EXPECT_JSON_EQ("['foo', 'bar']",
249 *ToValue(std::vector<std::string>{"foo", "bar"}));
250 EXPECT_JSON_EQ("[[1,2],[3]]",
251 *ToValue(std::vector<std::vector<int>>{{1, 2}, {3}}));
252 }
253
TEST(ValueConversionTest,ToValueMap)254 TEST(ValueConversionTest, ToValueMap) {
255 EXPECT_JSON_EQ("{'foo': 1, 'bar': 2}",
256 *ToValue(std::map<std::string, int>{{"foo", 1}, {"bar", 2}}));
257 EXPECT_JSON_EQ("{}", *ToValue(std::map<std::string, int>{}));
258 EXPECT_JSON_EQ("{'foo': true}",
259 *ToValue(std::map<std::string, bool>{{"foo", true}}));
260 EXPECT_JSON_EQ("{'foo': 1.1, 'bar': 2.2}",
261 *ToValue(std::map<std::string, double>{{"foo", 1.1},
262 {"bar", 2.2}}));
263 }
264
265 } // namespace brillo
266