1 // Copyright (c) 2012 The Chromium 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 "dbus/values_util.h"
6
7 #include <stddef.h>
8 #include <stdint.h>
9
10 #include <cmath>
11 #include <memory>
12 #include <utility>
13 #include <vector>
14
15 #include "base/json/json_writer.h"
16 #include "base/macros.h"
17 #include "base/values.h"
18 #include "dbus/message.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20
21 namespace dbus {
22
TEST(ValuesUtilTest,PopBasicTypes)23 TEST(ValuesUtilTest, PopBasicTypes) {
24 std::unique_ptr<Response> response(Response::CreateEmpty());
25 // Append basic type values.
26 MessageWriter writer(response.get());
27 const uint8_t kByteValue = 42;
28 writer.AppendByte(kByteValue);
29 const bool kBoolValue = true;
30 writer.AppendBool(kBoolValue);
31 const int16_t kInt16Value = -43;
32 writer.AppendInt16(kInt16Value);
33 const uint16_t kUint16Value = 44;
34 writer.AppendUint16(kUint16Value);
35 const int32_t kInt32Value = -45;
36 writer.AppendInt32(kInt32Value);
37 const uint32_t kUint32Value = 46;
38 writer.AppendUint32(kUint32Value);
39 const int64_t kInt64Value = -47;
40 writer.AppendInt64(kInt64Value);
41 const uint64_t kUint64Value = 48;
42 writer.AppendUint64(kUint64Value);
43 const double kDoubleValue = 4.9;
44 writer.AppendDouble(kDoubleValue);
45 const std::string kStringValue = "fifty";
46 writer.AppendString(kStringValue);
47 const std::string kEmptyStringValue;
48 writer.AppendString(kEmptyStringValue);
49 const ObjectPath kObjectPathValue("/ObjectPath");
50 writer.AppendObjectPath(kObjectPathValue);
51
52 MessageReader reader(response.get());
53 std::unique_ptr<base::Value> value;
54 std::unique_ptr<base::Value> expected_value;
55 // Pop a byte.
56 value = PopDataAsValue(&reader);
57 ASSERT_NE(nullptr, value);
58 expected_value.reset(new base::Value(kByteValue));
59 EXPECT_TRUE(value->Equals(expected_value.get()));
60 // Pop a bool.
61 value = PopDataAsValue(&reader);
62 ASSERT_NE(nullptr, value);
63 expected_value.reset(new base::Value(kBoolValue));
64 EXPECT_TRUE(value->Equals(expected_value.get()));
65 // Pop an int16_t.
66 value = PopDataAsValue(&reader);
67 ASSERT_NE(nullptr, value);
68 expected_value.reset(new base::Value(kInt16Value));
69 EXPECT_TRUE(value->Equals(expected_value.get()));
70 // Pop a uint16_t.
71 value = PopDataAsValue(&reader);
72 ASSERT_NE(nullptr, value);
73 expected_value.reset(new base::Value(kUint16Value));
74 EXPECT_TRUE(value->Equals(expected_value.get()));
75 // Pop an int32_t.
76 value = PopDataAsValue(&reader);
77 ASSERT_NE(nullptr, value);
78 expected_value.reset(new base::Value(kInt32Value));
79 EXPECT_TRUE(value->Equals(expected_value.get()));
80 // Pop a uint32_t.
81 value = PopDataAsValue(&reader);
82 ASSERT_NE(nullptr, value);
83 expected_value.reset(new base::Value(static_cast<double>(kUint32Value)));
84 EXPECT_TRUE(value->Equals(expected_value.get()));
85 // Pop an int64_t.
86 value = PopDataAsValue(&reader);
87 ASSERT_NE(nullptr, value);
88 expected_value.reset(new base::Value(static_cast<double>(kInt64Value)));
89 EXPECT_TRUE(value->Equals(expected_value.get()));
90 // Pop a uint64_t.
91 value = PopDataAsValue(&reader);
92 ASSERT_NE(nullptr, value);
93 expected_value.reset(new base::Value(static_cast<double>(kUint64Value)));
94 EXPECT_TRUE(value->Equals(expected_value.get()));
95 // Pop a double.
96 value = PopDataAsValue(&reader);
97 ASSERT_NE(nullptr, value);
98 expected_value.reset(new base::Value(kDoubleValue));
99 EXPECT_TRUE(value->Equals(expected_value.get()));
100 // Pop a string.
101 value = PopDataAsValue(&reader);
102 ASSERT_NE(nullptr, value);
103 expected_value.reset(new base::Value(kStringValue));
104 EXPECT_TRUE(value->Equals(expected_value.get()));
105 // Pop an empty string.
106 value = PopDataAsValue(&reader);
107 ASSERT_NE(nullptr, value);
108 expected_value.reset(new base::Value(kEmptyStringValue));
109 EXPECT_TRUE(value->Equals(expected_value.get()));
110 // Pop an object path.
111 value = PopDataAsValue(&reader);
112 ASSERT_NE(nullptr, value);
113 expected_value.reset(new base::Value(kObjectPathValue.value()));
114 EXPECT_TRUE(value->Equals(expected_value.get()));
115 }
116
TEST(ValuesUtilTest,PopVariant)117 TEST(ValuesUtilTest, PopVariant) {
118 std::unique_ptr<Response> response(Response::CreateEmpty());
119 // Append variant values.
120 MessageWriter writer(response.get());
121 const bool kBoolValue = true;
122 writer.AppendVariantOfBool(kBoolValue);
123 const int32_t kInt32Value = -45;
124 writer.AppendVariantOfInt32(kInt32Value);
125 const double kDoubleValue = 4.9;
126 writer.AppendVariantOfDouble(kDoubleValue);
127 const std::string kStringValue = "fifty";
128 writer.AppendVariantOfString(kStringValue);
129
130 MessageReader reader(response.get());
131 std::unique_ptr<base::Value> value;
132 std::unique_ptr<base::Value> expected_value;
133 // Pop a bool.
134 value = PopDataAsValue(&reader);
135 ASSERT_NE(nullptr, value);
136 expected_value.reset(new base::Value(kBoolValue));
137 EXPECT_TRUE(value->Equals(expected_value.get()));
138 // Pop an int32_t.
139 value = PopDataAsValue(&reader);
140 ASSERT_NE(nullptr, value);
141 expected_value.reset(new base::Value(kInt32Value));
142 EXPECT_TRUE(value->Equals(expected_value.get()));
143 // Pop a double.
144 value = PopDataAsValue(&reader);
145 ASSERT_NE(nullptr, value);
146 expected_value.reset(new base::Value(kDoubleValue));
147 EXPECT_TRUE(value->Equals(expected_value.get()));
148 // Pop a string.
149 value = PopDataAsValue(&reader);
150 ASSERT_NE(nullptr, value);
151 expected_value.reset(new base::Value(kStringValue));
152 EXPECT_TRUE(value->Equals(expected_value.get()));
153 }
154
155 // Pop extremely large integers which cannot be precisely represented in
156 // double.
TEST(ValuesUtilTest,PopExtremelyLargeIntegers)157 TEST(ValuesUtilTest, PopExtremelyLargeIntegers) {
158 std::unique_ptr<Response> response(Response::CreateEmpty());
159 // Append large integers.
160 MessageWriter writer(response.get());
161 const int64_t kInt64Value = -123456789012345689LL;
162 writer.AppendInt64(kInt64Value);
163 const uint64_t kUint64Value = 9876543210987654321ULL;
164 writer.AppendUint64(kUint64Value);
165
166 MessageReader reader(response.get());
167 std::unique_ptr<base::Value> value;
168 std::unique_ptr<base::Value> expected_value;
169 double double_value = 0;
170 // Pop an int64_t.
171 value = PopDataAsValue(&reader);
172 ASSERT_NE(nullptr, value);
173 expected_value.reset(new base::Value(static_cast<double>(kInt64Value)));
174 EXPECT_TRUE(value->Equals(expected_value.get()));
175 ASSERT_TRUE(value->GetAsDouble(&double_value));
176 EXPECT_NE(kInt64Value, static_cast<int64_t>(double_value));
177 // Pop a uint64_t.
178 value = PopDataAsValue(&reader);
179 ASSERT_NE(nullptr, value);
180 expected_value.reset(new base::Value(static_cast<double>(kUint64Value)));
181 EXPECT_TRUE(value->Equals(expected_value.get()));
182 ASSERT_TRUE(value->GetAsDouble(&double_value));
183 EXPECT_NE(kUint64Value, static_cast<uint64_t>(double_value));
184 }
185
TEST(ValuesUtilTest,PopIntArray)186 TEST(ValuesUtilTest, PopIntArray) {
187 std::unique_ptr<Response> response(Response::CreateEmpty());
188 // Append an int32_t array.
189 MessageWriter writer(response.get());
190 MessageWriter sub_writer(nullptr);
191 std::vector<int32_t> data;
192 data.push_back(0);
193 data.push_back(1);
194 data.push_back(2);
195 writer.OpenArray("i", &sub_writer);
196 for (size_t i = 0; i != data.size(); ++i)
197 sub_writer.AppendInt32(data[i]);
198 writer.CloseContainer(&sub_writer);
199
200 // Create the expected value.
201 std::unique_ptr<base::ListValue> list_value(new base::ListValue);
202 for (size_t i = 0; i != data.size(); ++i)
203 list_value->AppendInteger(data[i]);
204
205 // Pop an int32_t array.
206 MessageReader reader(response.get());
207 std::unique_ptr<base::Value> value(PopDataAsValue(&reader));
208 ASSERT_NE(nullptr, value);
209 EXPECT_TRUE(value->Equals(list_value.get()));
210 }
211
TEST(ValuesUtilTest,PopStringArray)212 TEST(ValuesUtilTest, PopStringArray) {
213 std::unique_ptr<Response> response(Response::CreateEmpty());
214 // Append a string array.
215 MessageWriter writer(response.get());
216 MessageWriter sub_writer(nullptr);
217 std::vector<std::string> data;
218 data.push_back("Dreamlifter");
219 data.push_back("Beluga");
220 data.push_back("Mriya");
221 writer.AppendArrayOfStrings(data);
222
223 // Create the expected value.
224 std::unique_ptr<base::ListValue> list_value(new base::ListValue);
225 for (size_t i = 0; i != data.size(); ++i)
226 list_value->AppendString(data[i]);
227
228 // Pop a string array.
229 MessageReader reader(response.get());
230 std::unique_ptr<base::Value> value(PopDataAsValue(&reader));
231 ASSERT_NE(nullptr, value);
232 EXPECT_TRUE(value->Equals(list_value.get()));
233 }
234
TEST(ValuesUtilTest,PopStruct)235 TEST(ValuesUtilTest, PopStruct) {
236 std::unique_ptr<Response> response(Response::CreateEmpty());
237 // Append a struct.
238 MessageWriter writer(response.get());
239 MessageWriter sub_writer(nullptr);
240 writer.OpenStruct(&sub_writer);
241 const bool kBoolValue = true;
242 sub_writer.AppendBool(kBoolValue);
243 const int32_t kInt32Value = -123;
244 sub_writer.AppendInt32(kInt32Value);
245 const double kDoubleValue = 1.23;
246 sub_writer.AppendDouble(kDoubleValue);
247 const std::string kStringValue = "one two three";
248 sub_writer.AppendString(kStringValue);
249 writer.CloseContainer(&sub_writer);
250
251 // Create the expected value.
252 base::ListValue list_value;
253 list_value.AppendBoolean(kBoolValue);
254 list_value.AppendInteger(kInt32Value);
255 list_value.AppendDouble(kDoubleValue);
256 list_value.AppendString(kStringValue);
257
258 // Pop a struct.
259 MessageReader reader(response.get());
260 std::unique_ptr<base::Value> value(PopDataAsValue(&reader));
261 ASSERT_NE(nullptr, value);
262 EXPECT_TRUE(value->Equals(&list_value));
263 }
264
TEST(ValuesUtilTest,PopStringToVariantDictionary)265 TEST(ValuesUtilTest, PopStringToVariantDictionary) {
266 std::unique_ptr<Response> response(Response::CreateEmpty());
267 // Append a dictionary.
268 MessageWriter writer(response.get());
269 MessageWriter sub_writer(nullptr);
270 MessageWriter entry_writer(nullptr);
271 writer.OpenArray("{sv}", &sub_writer);
272 sub_writer.OpenDictEntry(&entry_writer);
273 const std::string kKey1 = "one";
274 entry_writer.AppendString(kKey1);
275 const bool kBoolValue = true;
276 entry_writer.AppendVariantOfBool(kBoolValue);
277 sub_writer.CloseContainer(&entry_writer);
278 sub_writer.OpenDictEntry(&entry_writer);
279 const std::string kKey2 = "two";
280 entry_writer.AppendString(kKey2);
281 const int32_t kInt32Value = -45;
282 entry_writer.AppendVariantOfInt32(kInt32Value);
283 sub_writer.CloseContainer(&entry_writer);
284 sub_writer.OpenDictEntry(&entry_writer);
285 const std::string kKey3 = "three";
286 entry_writer.AppendString(kKey3);
287 const double kDoubleValue = 4.9;
288 entry_writer.AppendVariantOfDouble(kDoubleValue);
289 sub_writer.CloseContainer(&entry_writer);
290 sub_writer.OpenDictEntry(&entry_writer);
291 const std::string kKey4 = "four";
292 entry_writer.AppendString(kKey4);
293 const std::string kStringValue = "fifty";
294 entry_writer.AppendVariantOfString(kStringValue);
295 sub_writer.CloseContainer(&entry_writer);
296 writer.CloseContainer(&sub_writer);
297
298 // Create the expected value.
299 base::DictionaryValue dictionary_value;
300 dictionary_value.SetBoolean(kKey1, kBoolValue);
301 dictionary_value.SetInteger(kKey2, kInt32Value);
302 dictionary_value.SetDouble(kKey3, kDoubleValue);
303 dictionary_value.SetString(kKey4, kStringValue);
304
305 // Pop a dictinoary.
306 MessageReader reader(response.get());
307 std::unique_ptr<base::Value> value(PopDataAsValue(&reader));
308 ASSERT_NE(nullptr, value);
309 EXPECT_TRUE(value->Equals(&dictionary_value));
310 }
311
TEST(ValuesUtilTest,PopDictionaryWithDottedStringKey)312 TEST(ValuesUtilTest, PopDictionaryWithDottedStringKey) {
313 std::unique_ptr<Response> response(Response::CreateEmpty());
314 // Append a dictionary.
315 MessageWriter writer(response.get());
316 MessageWriter sub_writer(nullptr);
317 MessageWriter entry_writer(nullptr);
318 writer.OpenArray("{sv}", &sub_writer);
319 sub_writer.OpenDictEntry(&entry_writer);
320 const std::string kKey1 = "www.example.com"; // String including dots.
321 entry_writer.AppendString(kKey1);
322 const bool kBoolValue = true;
323 entry_writer.AppendVariantOfBool(kBoolValue);
324 sub_writer.CloseContainer(&entry_writer);
325 sub_writer.OpenDictEntry(&entry_writer);
326 const std::string kKey2 = ".example"; // String starting with a dot.
327 entry_writer.AppendString(kKey2);
328 const int32_t kInt32Value = -45;
329 entry_writer.AppendVariantOfInt32(kInt32Value);
330 sub_writer.CloseContainer(&entry_writer);
331 sub_writer.OpenDictEntry(&entry_writer);
332 const std::string kKey3 = "example."; // String ending with a dot.
333 entry_writer.AppendString(kKey3);
334 const double kDoubleValue = 4.9;
335 entry_writer.AppendVariantOfDouble(kDoubleValue);
336 sub_writer.CloseContainer(&entry_writer);
337 writer.CloseContainer(&sub_writer);
338
339 // Create the expected value.
340 base::DictionaryValue dictionary_value;
341 dictionary_value.SetKey(kKey1, base::Value(kBoolValue));
342 dictionary_value.SetKey(kKey2, base::Value(kInt32Value));
343 dictionary_value.SetKey(kKey3, base::Value(kDoubleValue));
344
345 // Pop a dictinoary.
346 MessageReader reader(response.get());
347 std::unique_ptr<base::Value> value(PopDataAsValue(&reader));
348 ASSERT_NE(nullptr, value);
349 EXPECT_TRUE(value->Equals(&dictionary_value));
350 }
351
TEST(ValuesUtilTest,PopDoubleToIntDictionary)352 TEST(ValuesUtilTest, PopDoubleToIntDictionary) {
353 // Create test data.
354 const int32_t kValues[] = {0, 1, 1, 2, 3, 5, 8, 13, 21};
355 const std::vector<int32_t> values(kValues, kValues + arraysize(kValues));
356 std::vector<double> keys(values.size());
357 for (size_t i = 0; i != values.size(); ++i)
358 keys[i] = std::sqrt(values[i]);
359
360 // Append a dictionary.
361 std::unique_ptr<Response> response(Response::CreateEmpty());
362 MessageWriter writer(response.get());
363 MessageWriter sub_writer(nullptr);
364 writer.OpenArray("{di}", &sub_writer);
365 for (size_t i = 0; i != values.size(); ++i) {
366 MessageWriter entry_writer(nullptr);
367 sub_writer.OpenDictEntry(&entry_writer);
368 entry_writer.AppendDouble(keys[i]);
369 entry_writer.AppendInt32(values[i]);
370 sub_writer.CloseContainer(&entry_writer);
371 }
372 writer.CloseContainer(&sub_writer);
373
374 // Create the expected value.
375 base::DictionaryValue dictionary_value;
376 for (size_t i = 0; i != values.size(); ++i) {
377 std::string key_string;
378 base::JSONWriter::Write(base::Value(keys[i]), &key_string);
379 dictionary_value.SetKey(key_string, base::Value(values[i]));
380 }
381
382 // Pop a dictionary.
383 MessageReader reader(response.get());
384 std::unique_ptr<base::Value> value(PopDataAsValue(&reader));
385 ASSERT_NE(nullptr, value);
386 EXPECT_TRUE(value->Equals(&dictionary_value));
387 }
388
TEST(ValuesUtilTest,AppendBasicTypes)389 TEST(ValuesUtilTest, AppendBasicTypes) {
390 const base::Value kBoolValue(false);
391 const base::Value kIntegerValue(42);
392 const base::Value kDoubleValue(4.2);
393 const base::Value kStringValue("string");
394
395 std::unique_ptr<Response> response(Response::CreateEmpty());
396 MessageWriter writer(response.get());
397 AppendBasicTypeValueData(&writer, kBoolValue);
398 AppendBasicTypeValueData(&writer, kIntegerValue);
399 AppendBasicTypeValueData(&writer, kDoubleValue);
400 AppendBasicTypeValueData(&writer, kStringValue);
401
402 MessageReader reader(response.get());
403 std::unique_ptr<base::Value> value;
404 value = PopDataAsValue(&reader);
405 ASSERT_NE(nullptr, value);
406 EXPECT_TRUE(value->Equals(&kBoolValue));
407 value = PopDataAsValue(&reader);
408 ASSERT_NE(nullptr, value);
409 EXPECT_TRUE(value->Equals(&kIntegerValue));
410 value = PopDataAsValue(&reader);
411 ASSERT_NE(nullptr, value);
412 EXPECT_TRUE(value->Equals(&kDoubleValue));
413 value = PopDataAsValue(&reader);
414 ASSERT_NE(nullptr, value);
415 EXPECT_TRUE(value->Equals(&kStringValue));
416 }
417
TEST(ValuesUtilTest,AppendBasicTypesAsVariant)418 TEST(ValuesUtilTest, AppendBasicTypesAsVariant) {
419 const base::Value kBoolValue(false);
420 const base::Value kIntegerValue(42);
421 const base::Value kDoubleValue(4.2);
422 const base::Value kStringValue("string");
423
424 std::unique_ptr<Response> response(Response::CreateEmpty());
425 MessageWriter writer(response.get());
426 AppendBasicTypeValueDataAsVariant(&writer, kBoolValue);
427 AppendBasicTypeValueDataAsVariant(&writer, kIntegerValue);
428 AppendBasicTypeValueDataAsVariant(&writer, kDoubleValue);
429 AppendBasicTypeValueDataAsVariant(&writer, kStringValue);
430
431 MessageReader reader(response.get());
432 std::unique_ptr<base::Value> value;
433 value = PopDataAsValue(&reader);
434 ASSERT_NE(nullptr, value);
435 EXPECT_TRUE(value->Equals(&kBoolValue));
436 value = PopDataAsValue(&reader);
437 ASSERT_NE(nullptr, value);
438 EXPECT_TRUE(value->Equals(&kIntegerValue));
439 value = PopDataAsValue(&reader);
440 ASSERT_NE(nullptr, value);
441 EXPECT_TRUE(value->Equals(&kDoubleValue));
442 value = PopDataAsValue(&reader);
443 ASSERT_NE(nullptr, value);
444 EXPECT_TRUE(value->Equals(&kStringValue));
445 }
446
TEST(ValuesUtilTest,AppendValueDataBasicTypes)447 TEST(ValuesUtilTest, AppendValueDataBasicTypes) {
448 const base::Value kBoolValue(false);
449 const base::Value kIntegerValue(42);
450 const base::Value kDoubleValue(4.2);
451 const base::Value kStringValue("string");
452
453 std::unique_ptr<Response> response(Response::CreateEmpty());
454 MessageWriter writer(response.get());
455 AppendValueData(&writer, kBoolValue);
456 AppendValueData(&writer, kIntegerValue);
457 AppendValueData(&writer, kDoubleValue);
458 AppendValueData(&writer, kStringValue);
459
460 MessageReader reader(response.get());
461 std::unique_ptr<base::Value> value;
462 value = PopDataAsValue(&reader);
463 ASSERT_NE(nullptr, value);
464 EXPECT_TRUE(value->Equals(&kBoolValue));
465 value = PopDataAsValue(&reader);
466 ASSERT_NE(nullptr, value);
467 EXPECT_TRUE(value->Equals(&kIntegerValue));
468 value = PopDataAsValue(&reader);
469 ASSERT_NE(nullptr, value);
470 EXPECT_TRUE(value->Equals(&kDoubleValue));
471 value = PopDataAsValue(&reader);
472 ASSERT_NE(nullptr, value);
473 EXPECT_TRUE(value->Equals(&kStringValue));
474 }
475
TEST(ValuesUtilTest,AppendValueDataAsVariantBasicTypes)476 TEST(ValuesUtilTest, AppendValueDataAsVariantBasicTypes) {
477 const base::Value kBoolValue(false);
478 const base::Value kIntegerValue(42);
479 const base::Value kDoubleValue(4.2);
480 const base::Value kStringValue("string");
481
482 std::unique_ptr<Response> response(Response::CreateEmpty());
483 MessageWriter writer(response.get());
484 AppendValueDataAsVariant(&writer, kBoolValue);
485 AppendValueDataAsVariant(&writer, kIntegerValue);
486 AppendValueDataAsVariant(&writer, kDoubleValue);
487 AppendValueDataAsVariant(&writer, kStringValue);
488
489 MessageReader reader(response.get());
490 std::unique_ptr<base::Value> value;
491 value = PopDataAsValue(&reader);
492 ASSERT_NE(nullptr, value);
493 EXPECT_TRUE(value->Equals(&kBoolValue));
494 value = PopDataAsValue(&reader);
495 ASSERT_NE(nullptr, value);
496 EXPECT_TRUE(value->Equals(&kIntegerValue));
497 value = PopDataAsValue(&reader);
498 ASSERT_NE(nullptr, value);
499 EXPECT_TRUE(value->Equals(&kDoubleValue));
500 value = PopDataAsValue(&reader);
501 ASSERT_NE(nullptr, value);
502 EXPECT_TRUE(value->Equals(&kStringValue));
503 }
504
TEST(ValuesUtilTest,AppendDictionary)505 TEST(ValuesUtilTest, AppendDictionary) {
506 // Set up the input dictionary.
507 const std::string kKey1 = "one";
508 const std::string kKey2 = "two";
509 const std::string kKey3 = "three";
510 const std::string kKey4 = "four";
511 const std::string kKey5 = "five";
512 const std::string kKey6 = "six";
513
514 const bool kBoolValue = true;
515 const int32_t kInt32Value = -45;
516 const double kDoubleValue = 4.9;
517 const std::string kStringValue = "fifty";
518
519 auto list_value = std::make_unique<base::ListValue>();
520 list_value->AppendBoolean(kBoolValue);
521 list_value->AppendInteger(kInt32Value);
522
523 auto dictionary_value = std::make_unique<base::DictionaryValue>();
524 dictionary_value->SetBoolean(kKey1, kBoolValue);
525 dictionary_value->SetInteger(kKey2, kDoubleValue);
526
527 base::DictionaryValue test_dictionary;
528 test_dictionary.SetBoolean(kKey1, kBoolValue);
529 test_dictionary.SetInteger(kKey2, kInt32Value);
530 test_dictionary.SetDouble(kKey3, kDoubleValue);
531 test_dictionary.SetString(kKey4, kStringValue);
532 test_dictionary.Set(kKey5, std::move(list_value));
533 test_dictionary.Set(kKey6, std::move(dictionary_value));
534
535 std::unique_ptr<Response> response(Response::CreateEmpty());
536 MessageWriter writer(response.get());
537 AppendValueData(&writer, test_dictionary);
538 base::Value int_value(kInt32Value);
539 AppendValueData(&writer, int_value);
540
541 // Read the data.
542 MessageReader reader(response.get());
543 std::unique_ptr<base::Value> value;
544 value = PopDataAsValue(&reader);
545 ASSERT_NE(nullptr, value);
546 EXPECT_TRUE(value->Equals(&test_dictionary));
547 value = PopDataAsValue(&reader);
548 ASSERT_NE(nullptr, value);
549 EXPECT_TRUE(value->Equals(&int_value));
550 }
551
TEST(ValuesUtilTest,AppendDictionaryAsVariant)552 TEST(ValuesUtilTest, AppendDictionaryAsVariant) {
553 // Set up the input dictionary.
554 const std::string kKey1 = "one";
555 const std::string kKey2 = "two";
556 const std::string kKey3 = "three";
557 const std::string kKey4 = "four";
558 const std::string kKey5 = "five";
559 const std::string kKey6 = "six";
560
561 const bool kBoolValue = true;
562 const int32_t kInt32Value = -45;
563 const double kDoubleValue = 4.9;
564 const std::string kStringValue = "fifty";
565
566 auto list_value = std::make_unique<base::ListValue>();
567 list_value->AppendBoolean(kBoolValue);
568 list_value->AppendInteger(kInt32Value);
569
570 auto dictionary_value = std::make_unique<base::DictionaryValue>();
571 dictionary_value->SetBoolean(kKey1, kBoolValue);
572 dictionary_value->SetInteger(kKey2, kDoubleValue);
573
574 base::DictionaryValue test_dictionary;
575 test_dictionary.SetBoolean(kKey1, kBoolValue);
576 test_dictionary.SetInteger(kKey2, kInt32Value);
577 test_dictionary.SetDouble(kKey3, kDoubleValue);
578 test_dictionary.SetString(kKey4, kStringValue);
579 test_dictionary.Set(kKey5, std::move(list_value));
580 test_dictionary.Set(kKey6, std::move(dictionary_value));
581
582 std::unique_ptr<Response> response(Response::CreateEmpty());
583 MessageWriter writer(response.get());
584 AppendValueDataAsVariant(&writer, test_dictionary);
585 base::Value int_value(kInt32Value);
586 AppendValueData(&writer, int_value);
587
588 // Read the data.
589 MessageReader reader(response.get());
590 std::unique_ptr<base::Value> value;
591 value = PopDataAsValue(&reader);
592 ASSERT_NE(nullptr, value);
593 EXPECT_TRUE(value->Equals(&test_dictionary));
594 value = PopDataAsValue(&reader);
595 ASSERT_NE(nullptr, value);
596 EXPECT_TRUE(value->Equals(&int_value));
597 }
598
TEST(ValuesUtilTest,AppendList)599 TEST(ValuesUtilTest, AppendList) {
600 // Set up the input list.
601 const std::string kKey1 = "one";
602 const std::string kKey2 = "two";
603
604 const bool kBoolValue = true;
605 const int32_t kInt32Value = -45;
606 const double kDoubleValue = 4.9;
607 const std::string kStringValue = "fifty";
608
609 std::unique_ptr<base::ListValue> list_value(new base::ListValue());
610 list_value->AppendBoolean(kBoolValue);
611 list_value->AppendInteger(kInt32Value);
612
613 std::unique_ptr<base::DictionaryValue> dictionary_value(
614 new base::DictionaryValue());
615 dictionary_value->SetBoolean(kKey1, kBoolValue);
616 dictionary_value->SetInteger(kKey2, kDoubleValue);
617
618 base::ListValue test_list;
619 test_list.AppendBoolean(kBoolValue);
620 test_list.AppendInteger(kInt32Value);
621 test_list.AppendDouble(kDoubleValue);
622 test_list.AppendString(kStringValue);
623 test_list.Append(std::move(list_value));
624 test_list.Append(std::move(dictionary_value));
625
626 std::unique_ptr<Response> response(Response::CreateEmpty());
627 MessageWriter writer(response.get());
628 AppendValueData(&writer, test_list);
629 base::Value int_value(kInt32Value);
630 AppendValueData(&writer, int_value);
631
632 // Read the data.
633 MessageReader reader(response.get());
634 std::unique_ptr<base::Value> value;
635 value = PopDataAsValue(&reader);
636 ASSERT_NE(nullptr, value);
637 EXPECT_TRUE(value->Equals(&test_list));
638 value = PopDataAsValue(&reader);
639 ASSERT_NE(nullptr, value);
640 EXPECT_TRUE(value->Equals(&int_value));
641 }
642
TEST(ValuesUtilTest,AppendListAsVariant)643 TEST(ValuesUtilTest, AppendListAsVariant) {
644 // Set up the input list.
645 const std::string kKey1 = "one";
646 const std::string kKey2 = "two";
647
648 const bool kBoolValue = true;
649 const int32_t kInt32Value = -45;
650 const double kDoubleValue = 4.9;
651 const std::string kStringValue = "fifty";
652
653 std::unique_ptr<base::ListValue> list_value(new base::ListValue());
654 list_value->AppendBoolean(kBoolValue);
655 list_value->AppendInteger(kInt32Value);
656
657 std::unique_ptr<base::DictionaryValue> dictionary_value(
658 new base::DictionaryValue());
659 dictionary_value->SetBoolean(kKey1, kBoolValue);
660 dictionary_value->SetInteger(kKey2, kDoubleValue);
661
662 base::ListValue test_list;
663 test_list.AppendBoolean(kBoolValue);
664 test_list.AppendInteger(kInt32Value);
665 test_list.AppendDouble(kDoubleValue);
666 test_list.AppendString(kStringValue);
667 test_list.Append(std::move(list_value));
668 test_list.Append(std::move(dictionary_value));
669
670 std::unique_ptr<Response> response(Response::CreateEmpty());
671 MessageWriter writer(response.get());
672 AppendValueDataAsVariant(&writer, test_list);
673 base::Value int_value(kInt32Value);
674 AppendValueData(&writer, int_value);
675
676 // Read the data.
677 MessageReader reader(response.get());
678 std::unique_ptr<base::Value> value;
679 value = PopDataAsValue(&reader);
680 ASSERT_NE(nullptr, value);
681 EXPECT_TRUE(value->Equals(&test_list));
682 value = PopDataAsValue(&reader);
683 ASSERT_NE(nullptr, value);
684 EXPECT_TRUE(value->Equals(&int_value));
685 }
686
687 } // namespace dbus
688