property_unittest.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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/property.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/bind.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/message_loop/message_pump_type.h"
  13. #include "base/run_loop.h"
  14. #include "base/strings/string_number_conversions.h"
  15. #include "base/test/task_environment.h"
  16. #include "base/threading/thread.h"
  17. #include "base/threading/thread_restrictions.h"
  18. #include "dbus/bus.h"
  19. #include "dbus/object_path.h"
  20. #include "dbus/object_proxy.h"
  21. #include "dbus/test_service.h"
  22. #include "testing/gtest/include/gtest/gtest.h"
  23. namespace dbus {
  24. // The property test exerises the asynchronous APIs in PropertySet and
  25. // Property<>.
  26. class PropertyTest : public testing::Test {
  27. public:
  28. PropertyTest() = default;
  29. struct Properties : public PropertySet {
  30. Property<std::string> name;
  31. Property<int16_t> version;
  32. Property<std::vector<std::string>> methods;
  33. Property<std::vector<ObjectPath>> objects;
  34. Property<std::vector<uint8_t>> bytes;
  35. Properties(ObjectProxy* object_proxy,
  36. PropertyChangedCallback property_changed_callback)
  37. : PropertySet(object_proxy,
  38. "org.chromium.TestInterface",
  39. property_changed_callback) {
  40. RegisterProperty("Name", &name);
  41. RegisterProperty("Version", &version);
  42. RegisterProperty("Methods", &methods);
  43. RegisterProperty("Objects", &objects);
  44. RegisterProperty("Bytes", &bytes);
  45. }
  46. };
  47. void SetUp() override {
  48. // Make the main thread not to allow IO.
  49. disallow_blocking_.emplace();
  50. // Start the D-Bus thread.
  51. dbus_thread_ = std::make_unique<base::Thread>("D-Bus Thread");
  52. base::Thread::Options thread_options;
  53. thread_options.message_pump_type = base::MessagePumpType::IO;
  54. ASSERT_TRUE(dbus_thread_->StartWithOptions(std::move(thread_options)));
  55. // Start the test service, using the D-Bus thread.
  56. TestService::Options options;
  57. options.dbus_task_runner = dbus_thread_->task_runner();
  58. test_service_ = std::make_unique<TestService>(options);
  59. ASSERT_TRUE(test_service_->StartService());
  60. test_service_->WaitUntilServiceIsStarted();
  61. ASSERT_TRUE(test_service_->HasDBusThread());
  62. // Create the client, using the D-Bus thread.
  63. Bus::Options bus_options;
  64. bus_options.bus_type = Bus::SESSION;
  65. bus_options.connection_type = Bus::PRIVATE;
  66. bus_options.dbus_task_runner = dbus_thread_->task_runner();
  67. bus_ = new Bus(bus_options);
  68. object_proxy_ = bus_->GetObjectProxy(
  69. test_service_->service_name(),
  70. ObjectPath("/org/chromium/TestObject"));
  71. ASSERT_TRUE(bus_->HasDBusThread());
  72. // Create the properties structure
  73. properties_ = std::make_unique<Properties>(
  74. object_proxy_, base::BindRepeating(&PropertyTest::OnPropertyChanged,
  75. base::Unretained(this)));
  76. properties_->ConnectSignals();
  77. properties_->GetAll();
  78. }
  79. void TearDown() override {
  80. bus_->ShutdownOnDBusThreadAndBlock();
  81. // Shut down the service.
  82. test_service_->ShutdownAndBlock();
  83. // Stopping a thread is considered an IO operation, so do this after
  84. // allowing IO.
  85. disallow_blocking_.reset();
  86. test_service_->Stop();
  87. }
  88. // Generic callback, bind with a string |id| for passing to
  89. // WaitForCallback() to ensure the callback for the right method is
  90. // waited for.
  91. void PropertyCallback(const std::string& id, bool success) {
  92. last_callback_ = id;
  93. run_loop_->Quit();
  94. }
  95. // Generic method callback, that might be used together with
  96. // WaitForMethodCallback to test wether method was succesfully called.
  97. void MethodCallback(Response* response) { run_loop_->Quit(); }
  98. protected:
  99. // Called when a property value is updated.
  100. void OnPropertyChanged(const std::string& name) {
  101. updated_properties_.push_back(name);
  102. run_loop_->Quit();
  103. }
  104. // Waits for the given number of updates.
  105. void WaitForUpdates(size_t num_updates) {
  106. while (updated_properties_.size() < num_updates) {
  107. run_loop_ = std::make_unique<base::RunLoop>();
  108. run_loop_->Run();
  109. }
  110. for (size_t i = 0; i < num_updates; ++i)
  111. updated_properties_.erase(updated_properties_.begin());
  112. }
  113. // Name, Version, Methods, Objects
  114. static const int kExpectedSignalUpdates = 5;
  115. // Waits for initial values to be set.
  116. void WaitForGetAll() {
  117. WaitForUpdates(kExpectedSignalUpdates);
  118. }
  119. // Waits until MethodCallback is called.
  120. void WaitForMethodCallback() {
  121. run_loop_ = std::make_unique<base::RunLoop>();
  122. run_loop_->Run();
  123. }
  124. // Waits for the callback. |id| is the string bound to the callback when
  125. // the method call is made that identifies it and distinguishes from any
  126. // other; you can set this to whatever you wish.
  127. void WaitForCallback(const std::string& id) {
  128. while (last_callback_ != id) {
  129. run_loop_ = std::make_unique<base::RunLoop>();
  130. run_loop_->Run();
  131. }
  132. }
  133. base::test::SingleThreadTaskEnvironment task_environment_;
  134. absl::optional<base::ScopedDisallowBlocking> disallow_blocking_;
  135. std::unique_ptr<base::RunLoop> run_loop_;
  136. std::unique_ptr<base::Thread> dbus_thread_;
  137. scoped_refptr<Bus> bus_;
  138. raw_ptr<ObjectProxy> object_proxy_;
  139. std::unique_ptr<Properties> properties_;
  140. std::unique_ptr<TestService> test_service_;
  141. // Properties updated.
  142. std::vector<std::string> updated_properties_;
  143. // Last callback received.
  144. std::string last_callback_;
  145. };
  146. TEST_F(PropertyTest, InitialValues) {
  147. EXPECT_FALSE(properties_->name.is_valid());
  148. EXPECT_FALSE(properties_->version.is_valid());
  149. WaitForGetAll();
  150. EXPECT_TRUE(properties_->name.is_valid());
  151. EXPECT_EQ("TestService", properties_->name.value());
  152. EXPECT_TRUE(properties_->version.is_valid());
  153. EXPECT_EQ(10, properties_->version.value());
  154. std::vector<std::string> methods = properties_->methods.value();
  155. ASSERT_EQ(4U, methods.size());
  156. EXPECT_EQ("Echo", methods[0]);
  157. EXPECT_EQ("SlowEcho", methods[1]);
  158. EXPECT_EQ("AsyncEcho", methods[2]);
  159. EXPECT_EQ("BrokenMethod", methods[3]);
  160. std::vector<ObjectPath> objects = properties_->objects.value();
  161. ASSERT_EQ(1U, objects.size());
  162. EXPECT_EQ(ObjectPath("/TestObjectPath"), objects[0]);
  163. std::vector<uint8_t> bytes = properties_->bytes.value();
  164. ASSERT_EQ(4U, bytes.size());
  165. EXPECT_EQ('T', bytes[0]);
  166. EXPECT_EQ('e', bytes[1]);
  167. EXPECT_EQ('s', bytes[2]);
  168. EXPECT_EQ('t', bytes[3]);
  169. }
  170. TEST_F(PropertyTest, UpdatedValues) {
  171. WaitForGetAll();
  172. // Update the value of the "Name" property, this value should not change.
  173. properties_->name.Get(base::BindOnce(&PropertyTest::PropertyCallback,
  174. base::Unretained(this), "Name"));
  175. WaitForCallback("Name");
  176. WaitForUpdates(1);
  177. EXPECT_EQ("TestService", properties_->name.value());
  178. // Update the value of the "Version" property, this value should be changed.
  179. properties_->version.Get(base::BindOnce(&PropertyTest::PropertyCallback,
  180. base::Unretained(this), "Version"));
  181. WaitForCallback("Version");
  182. WaitForUpdates(1);
  183. EXPECT_EQ(20, properties_->version.value());
  184. // Update the value of the "Methods" property, this value should not change
  185. // and should not grow to contain duplicate entries.
  186. properties_->methods.Get(base::BindOnce(&PropertyTest::PropertyCallback,
  187. base::Unretained(this), "Methods"));
  188. WaitForCallback("Methods");
  189. WaitForUpdates(1);
  190. std::vector<std::string> methods = properties_->methods.value();
  191. ASSERT_EQ(4U, methods.size());
  192. EXPECT_EQ("Echo", methods[0]);
  193. EXPECT_EQ("SlowEcho", methods[1]);
  194. EXPECT_EQ("AsyncEcho", methods[2]);
  195. EXPECT_EQ("BrokenMethod", methods[3]);
  196. // Update the value of the "Objects" property, this value should not change
  197. // and should not grow to contain duplicate entries.
  198. properties_->objects.Get(base::BindOnce(&PropertyTest::PropertyCallback,
  199. base::Unretained(this), "Objects"));
  200. WaitForCallback("Objects");
  201. WaitForUpdates(1);
  202. std::vector<ObjectPath> objects = properties_->objects.value();
  203. ASSERT_EQ(1U, objects.size());
  204. EXPECT_EQ(ObjectPath("/TestObjectPath"), objects[0]);
  205. // Update the value of the "Bytes" property, this value should not change
  206. // and should not grow to contain duplicate entries.
  207. properties_->bytes.Get(base::BindOnce(&PropertyTest::PropertyCallback,
  208. base::Unretained(this), "Bytes"));
  209. WaitForCallback("Bytes");
  210. WaitForUpdates(1);
  211. std::vector<uint8_t> bytes = properties_->bytes.value();
  212. ASSERT_EQ(4U, bytes.size());
  213. EXPECT_EQ('T', bytes[0]);
  214. EXPECT_EQ('e', bytes[1]);
  215. EXPECT_EQ('s', bytes[2]);
  216. EXPECT_EQ('t', bytes[3]);
  217. }
  218. TEST_F(PropertyTest, Get) {
  219. WaitForGetAll();
  220. // Ask for the new Version property.
  221. properties_->version.Get(base::BindOnce(&PropertyTest::PropertyCallback,
  222. base::Unretained(this), "Get"));
  223. WaitForCallback("Get");
  224. // Make sure we got a property update too.
  225. WaitForUpdates(1);
  226. EXPECT_EQ(20, properties_->version.value());
  227. }
  228. TEST_F(PropertyTest, Set) {
  229. WaitForGetAll();
  230. // Set a new name.
  231. properties_->name.Set("NewService",
  232. base::BindOnce(&PropertyTest::PropertyCallback,
  233. base::Unretained(this), "Set"));
  234. WaitForCallback("Set");
  235. // TestService sends a property update.
  236. WaitForUpdates(1);
  237. EXPECT_EQ("NewService", properties_->name.value());
  238. }
  239. TEST_F(PropertyTest, Invalidate) {
  240. WaitForGetAll();
  241. EXPECT_TRUE(properties_->name.is_valid());
  242. // Invalidate name.
  243. MethodCall method_call("org.chromium.TestInterface", "PerformAction");
  244. MessageWriter writer(&method_call);
  245. writer.AppendString("InvalidateProperty");
  246. writer.AppendObjectPath(ObjectPath("/org/chromium/TestService"));
  247. object_proxy_->CallMethod(
  248. &method_call, ObjectProxy::TIMEOUT_USE_DEFAULT,
  249. base::BindOnce(&PropertyTest::MethodCallback, base::Unretained(this)));
  250. WaitForMethodCallback();
  251. // TestService sends a property update.
  252. WaitForUpdates(1);
  253. EXPECT_FALSE(properties_->name.is_valid());
  254. // Set name to something valid.
  255. properties_->name.Set("NewService",
  256. base::BindOnce(&PropertyTest::PropertyCallback,
  257. base::Unretained(this), "Set"));
  258. WaitForCallback("Set");
  259. // TestService sends a property update.
  260. WaitForUpdates(1);
  261. EXPECT_TRUE(properties_->name.is_valid());
  262. }
  263. TEST(PropertyTestStatic, ReadWriteStringMap) {
  264. std::unique_ptr<Response> message(Response::CreateEmpty());
  265. MessageWriter writer(message.get());
  266. MessageWriter variant_writer(nullptr);
  267. MessageWriter variant_array_writer(nullptr);
  268. MessageWriter struct_entry_writer(nullptr);
  269. writer.OpenVariant("a{ss}", &variant_writer);
  270. variant_writer.OpenArray("{ss}", &variant_array_writer);
  271. const char* items[] = {"One", "Two", "Three", "Four"};
  272. for (unsigned i = 0; i < std::size(items); ++i) {
  273. variant_array_writer.OpenDictEntry(&struct_entry_writer);
  274. struct_entry_writer.AppendString(items[i]);
  275. struct_entry_writer.AppendString(base::NumberToString(i + 1));
  276. variant_array_writer.CloseContainer(&struct_entry_writer);
  277. }
  278. variant_writer.CloseContainer(&variant_array_writer);
  279. writer.CloseContainer(&variant_writer);
  280. MessageReader reader(message.get());
  281. Property<std::map<std::string, std::string>> string_map;
  282. EXPECT_TRUE(string_map.PopValueFromReader(&reader));
  283. ASSERT_EQ(4U, string_map.value().size());
  284. EXPECT_EQ("1", string_map.value().at("One"));
  285. EXPECT_EQ("2", string_map.value().at("Two"));
  286. EXPECT_EQ("3", string_map.value().at("Three"));
  287. EXPECT_EQ("4", string_map.value().at("Four"));
  288. }
  289. TEST(PropertyTestStatic, SerializeStringMap) {
  290. std::map<std::string, std::string> test_map;
  291. test_map["Hi"] = "There";
  292. test_map["Map"] = "Test";
  293. test_map["Random"] = "Text";
  294. std::unique_ptr<Response> message(Response::CreateEmpty());
  295. MessageWriter writer(message.get());
  296. Property<std::map<std::string, std::string>> string_map;
  297. string_map.ReplaceSetValueForTesting(test_map);
  298. string_map.AppendSetValueToWriter(&writer);
  299. MessageReader reader(message.get());
  300. EXPECT_TRUE(string_map.PopValueFromReader(&reader));
  301. EXPECT_EQ(test_map, string_map.value());
  302. }
  303. TEST(PropertyTestStatic, ReadWriteNetAddressArray) {
  304. std::unique_ptr<Response> message(Response::CreateEmpty());
  305. MessageWriter writer(message.get());
  306. MessageWriter variant_writer(nullptr);
  307. MessageWriter variant_array_writer(nullptr);
  308. MessageWriter struct_entry_writer(nullptr);
  309. writer.OpenVariant("a(ayq)", &variant_writer);
  310. variant_writer.OpenArray("(ayq)", &variant_array_writer);
  311. uint8_t ip_bytes[] = {0x54, 0x65, 0x73, 0x74, 0x30};
  312. for (uint16_t i = 0; i < 5; ++i) {
  313. variant_array_writer.OpenStruct(&struct_entry_writer);
  314. ip_bytes[4] = 0x30 + i;
  315. struct_entry_writer.AppendArrayOfBytes(ip_bytes, std::size(ip_bytes));
  316. struct_entry_writer.AppendUint16(i);
  317. variant_array_writer.CloseContainer(&struct_entry_writer);
  318. }
  319. variant_writer.CloseContainer(&variant_array_writer);
  320. writer.CloseContainer(&variant_writer);
  321. MessageReader reader(message.get());
  322. Property<std::vector<std::pair<std::vector<uint8_t>, uint16_t>>> ip_list;
  323. EXPECT_TRUE(ip_list.PopValueFromReader(&reader));
  324. ASSERT_EQ(5U, ip_list.value().size());
  325. size_t item_index = 0;
  326. for (auto& item : ip_list.value()) {
  327. ASSERT_EQ(5U, item.first.size());
  328. ip_bytes[4] = 0x30 + item_index;
  329. EXPECT_EQ(0, memcmp(ip_bytes, item.first.data(), 5U));
  330. EXPECT_EQ(item_index, item.second);
  331. ++item_index;
  332. }
  333. }
  334. TEST(PropertyTestStatic, SerializeNetAddressArray) {
  335. std::vector<std::pair<std::vector<uint8_t>, uint16_t>> test_list;
  336. uint8_t ip_bytes[] = {0x54, 0x65, 0x73, 0x74, 0x30};
  337. for (uint16_t i = 0; i < 5; ++i) {
  338. ip_bytes[4] = 0x30 + i;
  339. std::vector<uint8_t> bytes(ip_bytes, ip_bytes + std::size(ip_bytes));
  340. test_list.push_back(make_pair(bytes, 16));
  341. }
  342. std::unique_ptr<Response> message(Response::CreateEmpty());
  343. MessageWriter writer(message.get());
  344. Property<std::vector<std::pair<std::vector<uint8_t>, uint16_t>>> ip_list;
  345. ip_list.ReplaceSetValueForTesting(test_list);
  346. ip_list.AppendSetValueToWriter(&writer);
  347. MessageReader reader(message.get());
  348. EXPECT_TRUE(ip_list.PopValueFromReader(&reader));
  349. EXPECT_EQ(test_list, ip_list.value());
  350. }
  351. TEST(PropertyTestStatic, ReadWriteStringToByteVectorMapVariantWrapped) {
  352. std::unique_ptr<Response> message(Response::CreateEmpty());
  353. MessageWriter writer(message.get());
  354. MessageWriter variant_writer(nullptr);
  355. MessageWriter dict_writer(nullptr);
  356. writer.OpenVariant("a{sv}", &variant_writer);
  357. variant_writer.OpenArray("{sv}", &dict_writer);
  358. const char* keys[] = {"One", "Two", "Three", "Four"};
  359. const std::vector<uint8_t> values[] = {{1}, {1, 2}, {1, 2, 3}, {1, 2, 3, 4}};
  360. for (unsigned i = 0; i < std::size(keys); ++i) {
  361. MessageWriter entry_writer(nullptr);
  362. dict_writer.OpenDictEntry(&entry_writer);
  363. entry_writer.AppendString(keys[i]);
  364. MessageWriter value_varient_writer(nullptr);
  365. entry_writer.OpenVariant("ay", &value_varient_writer);
  366. value_varient_writer.AppendArrayOfBytes(values[i].data(), values[i].size());
  367. entry_writer.CloseContainer(&value_varient_writer);
  368. dict_writer.CloseContainer(&entry_writer);
  369. }
  370. variant_writer.CloseContainer(&dict_writer);
  371. writer.CloseContainer(&variant_writer);
  372. MessageReader reader(message.get());
  373. Property<std::map<std::string, std::vector<uint8_t>>> test_property;
  374. EXPECT_TRUE(test_property.PopValueFromReader(&reader));
  375. ASSERT_EQ(std::size(keys), test_property.value().size());
  376. for (unsigned i = 0; i < std::size(keys); ++i)
  377. EXPECT_EQ(values[i], test_property.value().at(keys[i]));
  378. }
  379. TEST(PropertyTestStatic, ReadWriteStringToByteVectorMap) {
  380. std::unique_ptr<Response> message(Response::CreateEmpty());
  381. MessageWriter writer(message.get());
  382. MessageWriter variant_writer(nullptr);
  383. MessageWriter dict_writer(nullptr);
  384. writer.OpenVariant("a{say}", &variant_writer);
  385. variant_writer.OpenArray("{say}", &dict_writer);
  386. const char* keys[] = {"One", "Two", "Three", "Four"};
  387. const std::vector<uint8_t> values[] = {{1}, {1, 2}, {1, 2, 3}, {1, 2, 3, 4}};
  388. for (unsigned i = 0; i < std::size(keys); ++i) {
  389. MessageWriter entry_writer(nullptr);
  390. dict_writer.OpenDictEntry(&entry_writer);
  391. entry_writer.AppendString(keys[i]);
  392. entry_writer.AppendArrayOfBytes(values[i].data(), values[i].size());
  393. dict_writer.CloseContainer(&entry_writer);
  394. }
  395. variant_writer.CloseContainer(&dict_writer);
  396. writer.CloseContainer(&variant_writer);
  397. MessageReader reader(message.get());
  398. Property<std::map<std::string, std::vector<uint8_t>>> test_property;
  399. EXPECT_TRUE(test_property.PopValueFromReader(&reader));
  400. ASSERT_EQ(std::size(keys), test_property.value().size());
  401. for (unsigned i = 0; i < std::size(keys); ++i)
  402. EXPECT_EQ(values[i], test_property.value().at(keys[i]));
  403. }
  404. TEST(PropertyTestStatic, SerializeStringToByteVectorMap) {
  405. std::map<std::string, std::vector<uint8_t>> test_map;
  406. test_map["Hi"] = {1, 2, 3};
  407. test_map["Map"] = {0xab, 0xcd};
  408. test_map["Random"] = {0x0};
  409. std::unique_ptr<Response> message(Response::CreateEmpty());
  410. MessageWriter writer(message.get());
  411. Property<std::map<std::string, std::vector<uint8_t>>> test_property;
  412. test_property.ReplaceSetValueForTesting(test_map);
  413. test_property.AppendSetValueToWriter(&writer);
  414. MessageReader reader(message.get());
  415. EXPECT_TRUE(test_property.PopValueFromReader(&reader));
  416. EXPECT_EQ(test_map, test_property.value());
  417. }
  418. TEST(PropertyTestStatic, ReadWriteUInt16ToByteVectorMapVariantWrapped) {
  419. std::unique_ptr<Response> message(Response::CreateEmpty());
  420. MessageWriter writer(message.get());
  421. MessageWriter variant_writer(nullptr);
  422. MessageWriter dict_writer(nullptr);
  423. writer.OpenVariant("a{qv}", &variant_writer);
  424. variant_writer.OpenArray("{qv}", &dict_writer);
  425. const uint16_t keys[] = {11, 12, 13, 14};
  426. const std::vector<uint8_t> values[] = {{1}, {1, 2}, {1, 2, 3}, {1, 2, 3, 4}};
  427. for (unsigned i = 0; i < std::size(keys); ++i) {
  428. MessageWriter entry_writer(nullptr);
  429. dict_writer.OpenDictEntry(&entry_writer);
  430. entry_writer.AppendUint16(keys[i]);
  431. MessageWriter value_varient_writer(nullptr);
  432. entry_writer.OpenVariant("ay", &value_varient_writer);
  433. value_varient_writer.AppendArrayOfBytes(values[i].data(), values[i].size());
  434. entry_writer.CloseContainer(&value_varient_writer);
  435. dict_writer.CloseContainer(&entry_writer);
  436. }
  437. variant_writer.CloseContainer(&dict_writer);
  438. writer.CloseContainer(&variant_writer);
  439. MessageReader reader(message.get());
  440. Property<std::map<uint16_t, std::vector<uint8_t>>> test_property;
  441. EXPECT_TRUE(test_property.PopValueFromReader(&reader));
  442. ASSERT_EQ(std::size(keys), test_property.value().size());
  443. for (unsigned i = 0; i < std::size(keys); ++i)
  444. EXPECT_EQ(values[i], test_property.value().at(keys[i]));
  445. }
  446. TEST(PropertyTestStatic, ReadWriteUInt16ToByteVectorMap) {
  447. std::unique_ptr<Response> message(Response::CreateEmpty());
  448. MessageWriter writer(message.get());
  449. MessageWriter variant_writer(nullptr);
  450. MessageWriter dict_writer(nullptr);
  451. writer.OpenVariant("a{qay}", &variant_writer);
  452. variant_writer.OpenArray("{qay}", &dict_writer);
  453. const uint16_t keys[] = {11, 12, 13, 14};
  454. const std::vector<uint8_t> values[] = {{1}, {1, 2}, {1, 2, 3}, {1, 2, 3, 4}};
  455. for (unsigned i = 0; i < std::size(keys); ++i) {
  456. MessageWriter entry_writer(nullptr);
  457. dict_writer.OpenDictEntry(&entry_writer);
  458. entry_writer.AppendUint16(keys[i]);
  459. entry_writer.AppendArrayOfBytes(values[i].data(), values[i].size());
  460. dict_writer.CloseContainer(&entry_writer);
  461. }
  462. variant_writer.CloseContainer(&dict_writer);
  463. writer.CloseContainer(&variant_writer);
  464. MessageReader reader(message.get());
  465. Property<std::map<uint16_t, std::vector<uint8_t>>> test_property;
  466. EXPECT_TRUE(test_property.PopValueFromReader(&reader));
  467. ASSERT_EQ(std::size(keys), test_property.value().size());
  468. for (unsigned i = 0; i < std::size(keys); ++i)
  469. EXPECT_EQ(values[i], test_property.value().at(keys[i]));
  470. }
  471. TEST(PropertyTestStatic, SerializeUInt16ToByteVectorMap) {
  472. std::map<uint16_t, std::vector<uint8_t>> test_map;
  473. test_map[11] = {1, 2, 3};
  474. test_map[12] = {0xab, 0xcd};
  475. test_map[13] = {0x0};
  476. std::unique_ptr<Response> message(Response::CreateEmpty());
  477. MessageWriter writer(message.get());
  478. Property<std::map<uint16_t, std::vector<uint8_t>>> test_property;
  479. test_property.ReplaceSetValueForTesting(test_map);
  480. test_property.AppendSetValueToWriter(&writer);
  481. MessageReader reader(message.get());
  482. EXPECT_TRUE(test_property.PopValueFromReader(&reader));
  483. EXPECT_EQ(test_map, test_property.value());
  484. }
  485. } // namespace dbus