values_util_unittest.cc 22 KB

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