blob: 2287c22a45696d73eb31d410de3da76bf9ad4cf8 [file] [log] [blame]
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +09001// 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/property.h"
6
avi0ad0ce02015-12-23 03:12:45 +09007#include <stddef.h>
8#include <stdint.h>
9
puthik90fd20c2016-10-06 14:03:03 +090010#include <memory>
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090011#include <string>
12#include <vector>
13
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090014#include "base/bind.h"
15#include "base/logging.h"
avi0ad0ce02015-12-23 03:12:45 +090016#include "base/macros.h"
avi@chromium.orga29af562013-07-18 08:00:30 +090017#include "base/message_loop/message_loop.h"
earthdok64401d72014-09-03 19:32:36 +090018#include "base/run_loop.h"
dtapuska95f71722015-02-10 01:02:55 +090019#include "base/strings/string_number_conversions.h"
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090020#include "base/threading/thread.h"
21#include "base/threading/thread_restrictions.h"
22#include "dbus/bus.h"
23#include "dbus/object_path.h"
24#include "dbus/object_proxy.h"
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090025#include "dbus/test_service.h"
26#include "testing/gtest/include/gtest/gtest.h"
27
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090028namespace dbus {
29
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090030// The property test exerises the asynchronous APIs in PropertySet and
31// Property<>.
32class PropertyTest : public testing::Test {
33 public:
jpawlowski77e73fc2015-05-11 20:07:04 +090034 PropertyTest() {}
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090035
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090036 struct Properties : public PropertySet {
37 Property<std::string> name;
avi0ad0ce02015-12-23 03:12:45 +090038 Property<int16_t> version;
Ben Chandf19e682017-11-08 10:50:21 +090039 Property<std::vector<std::string>> methods;
40 Property<std::vector<ObjectPath>> objects;
avi0ad0ce02015-12-23 03:12:45 +090041 Property<std::vector<uint8_t>> bytes;
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090042
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090043 Properties(ObjectProxy* object_proxy,
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090044 PropertyChangedCallback property_changed_callback)
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090045 : PropertySet(object_proxy,
46 "org.chromium.TestInterface",
47 property_changed_callback) {
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090048 RegisterProperty("Name", &name);
49 RegisterProperty("Version", &version);
50 RegisterProperty("Methods", &methods);
51 RegisterProperty("Objects", &objects);
armansito@chromium.org11e12742014-03-15 16:40:49 +090052 RegisterProperty("Bytes", &bytes);
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090053 }
54 };
55
dcheng7f5750d2014-12-30 03:30:17 +090056 void SetUp() override {
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090057 // Make the main thread not to allow IO.
58 base::ThreadRestrictions::SetIOAllowed(false);
59
60 // Start the D-Bus thread.
61 dbus_thread_.reset(new base::Thread("D-Bus Thread"));
62 base::Thread::Options thread_options;
xhwang@chromium.orgdff6b132013-05-02 01:10:30 +090063 thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090064 ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options));
65
66 // Start the test service, using the D-Bus thread.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090067 TestService::Options options;
skyostile5a8dc42015-06-18 00:46:04 +090068 options.dbus_task_runner = dbus_thread_->task_runner();
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090069 test_service_.reset(new TestService(options));
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090070 ASSERT_TRUE(test_service_->StartService());
71 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted());
72 ASSERT_TRUE(test_service_->HasDBusThread());
73
74 // Create the client, using the D-Bus thread.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090075 Bus::Options bus_options;
76 bus_options.bus_type = Bus::SESSION;
77 bus_options.connection_type = Bus::PRIVATE;
skyostile5a8dc42015-06-18 00:46:04 +090078 bus_options.dbus_task_runner = dbus_thread_->task_runner();
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090079 bus_ = new Bus(bus_options);
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090080 object_proxy_ = bus_->GetObjectProxy(
hashimotof4a4c0d2016-01-05 17:48:03 +090081 test_service_->service_name(),
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090082 ObjectPath("/org/chromium/TestObject"));
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090083 ASSERT_TRUE(bus_->HasDBusThread());
84
85 // Create the properties structure
satorux@chromium.org5e612da2012-05-31 15:55:53 +090086 properties_.reset(new Properties(
87 object_proxy_,
88 base::Bind(&PropertyTest::OnPropertyChanged,
89 base::Unretained(this))));
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090090 properties_->ConnectSignals();
91 properties_->GetAll();
92 }
93
dcheng7f5750d2014-12-30 03:30:17 +090094 void TearDown() override {
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090095 bus_->ShutdownOnDBusThreadAndBlock();
96
97 // Shut down the service.
98 test_service_->ShutdownAndBlock();
99
100 // Reset to the default.
101 base::ThreadRestrictions::SetIOAllowed(true);
102
103 // Stopping a thread is considered an IO operation, so do this after
104 // allowing IO.
105 test_service_->Stop();
106 }
107
108 // Generic callback, bind with a string |id| for passing to
109 // WaitForCallback() to ensure the callback for the right method is
110 // waited for.
111 void PropertyCallback(const std::string& id, bool success) {
112 last_callback_ = id;
earthdok64401d72014-09-03 19:32:36 +0900113 run_loop_->Quit();
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900114 }
115
jpawlowski77e73fc2015-05-11 20:07:04 +0900116 // Generic method callback, that might be used together with
117 // WaitForMethodCallback to test wether method was succesfully called.
118 void MethodCallback(Response* response) { run_loop_->Quit(); }
119
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900120 protected:
121 // Called when a property value is updated.
122 void OnPropertyChanged(const std::string& name) {
123 updated_properties_.push_back(name);
earthdok64401d72014-09-03 19:32:36 +0900124 run_loop_->Quit();
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900125 }
126
keybuk@chromium.org683dd8c2012-03-23 05:34:05 +0900127 // Waits for the given number of updates.
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900128 void WaitForUpdates(size_t num_updates) {
earthdok64401d72014-09-03 19:32:36 +0900129 while (updated_properties_.size() < num_updates) {
130 run_loop_.reset(new base::RunLoop);
131 run_loop_->Run();
132 }
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900133 for (size_t i = 0; i < num_updates; ++i)
134 updated_properties_.erase(updated_properties_.begin());
135 }
136
137 // Name, Version, Methods, Objects
armansito@chromium.org11e12742014-03-15 16:40:49 +0900138 static const int kExpectedSignalUpdates = 5;
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900139
keybuk@chromium.org683dd8c2012-03-23 05:34:05 +0900140 // Waits for initial values to be set.
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900141 void WaitForGetAll() {
142 WaitForUpdates(kExpectedSignalUpdates);
143 }
144
jpawlowski77e73fc2015-05-11 20:07:04 +0900145 // Waits until MethodCallback is called.
146 void WaitForMethodCallback() {
147 run_loop_.reset(new base::RunLoop);
148 run_loop_->Run();
149 }
150
keybuk@chromium.org683dd8c2012-03-23 05:34:05 +0900151 // Waits for the callback. |id| is the string bound to the callback when
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900152 // the method call is made that identifies it and distinguishes from any
153 // other; you can set this to whatever you wish.
154 void WaitForCallback(const std::string& id) {
155 while (last_callback_ != id) {
earthdok64401d72014-09-03 19:32:36 +0900156 run_loop_.reset(new base::RunLoop);
157 run_loop_->Run();
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900158 }
159 }
160
xhwang@chromium.orgdff6b132013-05-02 01:10:30 +0900161 base::MessageLoop message_loop_;
dcheng30c5a172016-04-09 07:55:04 +0900162 std::unique_ptr<base::RunLoop> run_loop_;
163 std::unique_ptr<base::Thread> dbus_thread_;
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900164 scoped_refptr<Bus> bus_;
165 ObjectProxy* object_proxy_;
dcheng30c5a172016-04-09 07:55:04 +0900166 std::unique_ptr<Properties> properties_;
167 std::unique_ptr<TestService> test_service_;
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900168 // Properties updated.
169 std::vector<std::string> updated_properties_;
170 // Last callback received.
171 std::string last_callback_;
172};
173
174TEST_F(PropertyTest, InitialValues) {
jpawlowski77e73fc2015-05-11 20:07:04 +0900175 EXPECT_FALSE(properties_->name.is_valid());
176 EXPECT_FALSE(properties_->version.is_valid());
177
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900178 WaitForGetAll();
179
jpawlowski77e73fc2015-05-11 20:07:04 +0900180 EXPECT_TRUE(properties_->name.is_valid());
keybuk@chromium.org683dd8c2012-03-23 05:34:05 +0900181 EXPECT_EQ("TestService", properties_->name.value());
jpawlowski77e73fc2015-05-11 20:07:04 +0900182 EXPECT_TRUE(properties_->version.is_valid());
keybuk@chromium.org683dd8c2012-03-23 05:34:05 +0900183 EXPECT_EQ(10, properties_->version.value());
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900184
185 std::vector<std::string> methods = properties_->methods.value();
186 ASSERT_EQ(4U, methods.size());
187 EXPECT_EQ("Echo", methods[0]);
188 EXPECT_EQ("SlowEcho", methods[1]);
189 EXPECT_EQ("AsyncEcho", methods[2]);
190 EXPECT_EQ("BrokenMethod", methods[3]);
191
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900192 std::vector<ObjectPath> objects = properties_->objects.value();
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900193 ASSERT_EQ(1U, objects.size());
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900194 EXPECT_EQ(ObjectPath("/TestObjectPath"), objects[0]);
armansito@chromium.org11e12742014-03-15 16:40:49 +0900195
avi0ad0ce02015-12-23 03:12:45 +0900196 std::vector<uint8_t> bytes = properties_->bytes.value();
armansito@chromium.org11e12742014-03-15 16:40:49 +0900197 ASSERT_EQ(4U, bytes.size());
198 EXPECT_EQ('T', bytes[0]);
199 EXPECT_EQ('e', bytes[1]);
200 EXPECT_EQ('s', bytes[2]);
201 EXPECT_EQ('t', bytes[3]);
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900202}
203
keybuk@google.comf056b962012-03-22 08:43:45 +0900204TEST_F(PropertyTest, UpdatedValues) {
205 WaitForGetAll();
206
207 // Update the value of the "Name" property, this value should not change.
208 properties_->name.Get(base::Bind(&PropertyTest::PropertyCallback,
209 base::Unretained(this),
210 "Name"));
211 WaitForCallback("Name");
212 WaitForUpdates(1);
213
keybuk@chromium.org683dd8c2012-03-23 05:34:05 +0900214 EXPECT_EQ("TestService", properties_->name.value());
keybuk@google.comf056b962012-03-22 08:43:45 +0900215
216 // Update the value of the "Version" property, this value should be changed.
217 properties_->version.Get(base::Bind(&PropertyTest::PropertyCallback,
218 base::Unretained(this),
219 "Version"));
220 WaitForCallback("Version");
221 WaitForUpdates(1);
222
keybuk@chromium.org683dd8c2012-03-23 05:34:05 +0900223 EXPECT_EQ(20, properties_->version.value());
keybuk@google.comf056b962012-03-22 08:43:45 +0900224
225 // Update the value of the "Methods" property, this value should not change
226 // and should not grow to contain duplicate entries.
227 properties_->methods.Get(base::Bind(&PropertyTest::PropertyCallback,
228 base::Unretained(this),
229 "Methods"));
230 WaitForCallback("Methods");
231 WaitForUpdates(1);
232
233 std::vector<std::string> methods = properties_->methods.value();
234 ASSERT_EQ(4U, methods.size());
235 EXPECT_EQ("Echo", methods[0]);
236 EXPECT_EQ("SlowEcho", methods[1]);
237 EXPECT_EQ("AsyncEcho", methods[2]);
238 EXPECT_EQ("BrokenMethod", methods[3]);
239
240 // Update the value of the "Objects" property, this value should not change
241 // and should not grow to contain duplicate entries.
242 properties_->objects.Get(base::Bind(&PropertyTest::PropertyCallback,
243 base::Unretained(this),
244 "Objects"));
245 WaitForCallback("Objects");
246 WaitForUpdates(1);
247
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900248 std::vector<ObjectPath> objects = properties_->objects.value();
keybuk@google.comf056b962012-03-22 08:43:45 +0900249 ASSERT_EQ(1U, objects.size());
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900250 EXPECT_EQ(ObjectPath("/TestObjectPath"), objects[0]);
armansito@chromium.org11e12742014-03-15 16:40:49 +0900251
252 // Update the value of the "Bytes" property, this value should not change
253 // and should not grow to contain duplicate entries.
254 properties_->bytes.Get(base::Bind(&PropertyTest::PropertyCallback,
255 base::Unretained(this),
256 "Bytes"));
257 WaitForCallback("Bytes");
258 WaitForUpdates(1);
259
avi0ad0ce02015-12-23 03:12:45 +0900260 std::vector<uint8_t> bytes = properties_->bytes.value();
armansito@chromium.org11e12742014-03-15 16:40:49 +0900261 ASSERT_EQ(4U, bytes.size());
262 EXPECT_EQ('T', bytes[0]);
263 EXPECT_EQ('e', bytes[1]);
264 EXPECT_EQ('s', bytes[2]);
265 EXPECT_EQ('t', bytes[3]);
keybuk@google.comf056b962012-03-22 08:43:45 +0900266}
267
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900268TEST_F(PropertyTest, Get) {
269 WaitForGetAll();
270
271 // Ask for the new Version property.
272 properties_->version.Get(base::Bind(&PropertyTest::PropertyCallback,
273 base::Unretained(this),
274 "Get"));
275 WaitForCallback("Get");
276
277 // Make sure we got a property update too.
278 WaitForUpdates(1);
279
keybuk@chromium.org683dd8c2012-03-23 05:34:05 +0900280 EXPECT_EQ(20, properties_->version.value());
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900281}
282
283TEST_F(PropertyTest, Set) {
284 WaitForGetAll();
285
286 // Set a new name.
287 properties_->name.Set("NewService",
288 base::Bind(&PropertyTest::PropertyCallback,
289 base::Unretained(this),
290 "Set"));
291 WaitForCallback("Set");
292
293 // TestService sends a property update.
294 WaitForUpdates(1);
295
keybuk@chromium.org683dd8c2012-03-23 05:34:05 +0900296 EXPECT_EQ("NewService", properties_->name.value());
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900297}
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900298
jpawlowski77e73fc2015-05-11 20:07:04 +0900299TEST_F(PropertyTest, Invalidate) {
300 WaitForGetAll();
301
302 EXPECT_TRUE(properties_->name.is_valid());
303
304 // Invalidate name.
305 MethodCall method_call("org.chromium.TestInterface", "PerformAction");
306 MessageWriter writer(&method_call);
307 writer.AppendString("InvalidateProperty");
308 writer.AppendObjectPath(ObjectPath("/org/chromium/TestService"));
309 object_proxy_->CallMethod(
310 &method_call, ObjectProxy::TIMEOUT_USE_DEFAULT,
311 base::Bind(&PropertyTest::MethodCallback, base::Unretained(this)));
312 WaitForMethodCallback();
313
314 // TestService sends a property update.
315 WaitForUpdates(1);
316
317 EXPECT_FALSE(properties_->name.is_valid());
318
319 // Set name to something valid.
320 properties_->name.Set("NewService",
321 base::Bind(&PropertyTest::PropertyCallback,
322 base::Unretained(this), "Set"));
323 WaitForCallback("Set");
324
325 // TestService sends a property update.
326 WaitForUpdates(1);
327
328 EXPECT_TRUE(properties_->name.is_valid());
329}
330
dtapuska95f71722015-02-10 01:02:55 +0900331TEST(PropertyTestStatic, ReadWriteStringMap) {
dcheng30c5a172016-04-09 07:55:04 +0900332 std::unique_ptr<Response> message(Response::CreateEmpty());
dtapuska95f71722015-02-10 01:02:55 +0900333 MessageWriter writer(message.get());
334 MessageWriter variant_writer(NULL);
335 MessageWriter variant_array_writer(NULL);
336 MessageWriter struct_entry_writer(NULL);
337
338 writer.OpenVariant("a{ss}", &variant_writer);
339 variant_writer.OpenArray("{ss}", &variant_array_writer);
340 const char* items[] = {"One", "Two", "Three", "Four"};
341 for (unsigned i = 0; i < arraysize(items); ++i) {
342 variant_array_writer.OpenDictEntry(&struct_entry_writer);
343 struct_entry_writer.AppendString(items[i]);
344 struct_entry_writer.AppendString(base::UintToString(i + 1));
345 variant_array_writer.CloseContainer(&struct_entry_writer);
346 }
347 variant_writer.CloseContainer(&variant_array_writer);
348 writer.CloseContainer(&variant_writer);
349
350 MessageReader reader(message.get());
351 Property<std::map<std::string, std::string>> string_map;
352 EXPECT_TRUE(string_map.PopValueFromReader(&reader));
353 ASSERT_EQ(4U, string_map.value().size());
354 EXPECT_EQ("1", string_map.value().at("One"));
355 EXPECT_EQ("2", string_map.value().at("Two"));
356 EXPECT_EQ("3", string_map.value().at("Three"));
357 EXPECT_EQ("4", string_map.value().at("Four"));
358}
359
360TEST(PropertyTestStatic, SerializeStringMap) {
361 std::map<std::string, std::string> test_map;
362 test_map["Hi"] = "There";
363 test_map["Map"] = "Test";
364 test_map["Random"] = "Text";
365
dcheng30c5a172016-04-09 07:55:04 +0900366 std::unique_ptr<Response> message(Response::CreateEmpty());
dtapuska95f71722015-02-10 01:02:55 +0900367 MessageWriter writer(message.get());
368
369 Property<std::map<std::string, std::string>> string_map;
370 string_map.ReplaceSetValueForTesting(test_map);
371 string_map.AppendSetValueToWriter(&writer);
372
373 MessageReader reader(message.get());
374 EXPECT_TRUE(string_map.PopValueFromReader(&reader));
375 EXPECT_EQ(test_map, string_map.value());
376}
377
378TEST(PropertyTestStatic, ReadWriteNetAddressArray) {
dcheng30c5a172016-04-09 07:55:04 +0900379 std::unique_ptr<Response> message(Response::CreateEmpty());
dtapuska95f71722015-02-10 01:02:55 +0900380 MessageWriter writer(message.get());
381 MessageWriter variant_writer(NULL);
382 MessageWriter variant_array_writer(NULL);
383 MessageWriter struct_entry_writer(NULL);
384
385 writer.OpenVariant("a(ayq)", &variant_writer);
386 variant_writer.OpenArray("(ayq)", &variant_array_writer);
avi0ad0ce02015-12-23 03:12:45 +0900387 uint8_t ip_bytes[] = {0x54, 0x65, 0x73, 0x74, 0x30};
388 for (uint16_t i = 0; i < 5; ++i) {
dtapuska95f71722015-02-10 01:02:55 +0900389 variant_array_writer.OpenStruct(&struct_entry_writer);
390 ip_bytes[4] = 0x30 + i;
391 struct_entry_writer.AppendArrayOfBytes(ip_bytes, arraysize(ip_bytes));
392 struct_entry_writer.AppendUint16(i);
393 variant_array_writer.CloseContainer(&struct_entry_writer);
394 }
395 variant_writer.CloseContainer(&variant_array_writer);
396 writer.CloseContainer(&variant_writer);
397
398 MessageReader reader(message.get());
avi0ad0ce02015-12-23 03:12:45 +0900399 Property<std::vector<std::pair<std::vector<uint8_t>, uint16_t>>> ip_list;
dtapuska95f71722015-02-10 01:02:55 +0900400 EXPECT_TRUE(ip_list.PopValueFromReader(&reader));
401
402 ASSERT_EQ(5U, ip_list.value().size());
403 size_t item_index = 0;
404 for (auto& item : ip_list.value()) {
405 ASSERT_EQ(5U, item.first.size());
406 ip_bytes[4] = 0x30 + item_index;
407 EXPECT_EQ(0, memcmp(ip_bytes, item.first.data(), 5U));
408 EXPECT_EQ(item_index, item.second);
409 ++item_index;
410 }
411}
412
413TEST(PropertyTestStatic, SerializeNetAddressArray) {
avi0ad0ce02015-12-23 03:12:45 +0900414 std::vector<std::pair<std::vector<uint8_t>, uint16_t>> test_list;
dtapuska95f71722015-02-10 01:02:55 +0900415
avi0ad0ce02015-12-23 03:12:45 +0900416 uint8_t ip_bytes[] = {0x54, 0x65, 0x73, 0x74, 0x30};
417 for (uint16_t i = 0; i < 5; ++i) {
dtapuska95f71722015-02-10 01:02:55 +0900418 ip_bytes[4] = 0x30 + i;
avi0ad0ce02015-12-23 03:12:45 +0900419 std::vector<uint8_t> bytes(ip_bytes, ip_bytes + arraysize(ip_bytes));
dtapuska95f71722015-02-10 01:02:55 +0900420 test_list.push_back(make_pair(bytes, 16));
421 }
422
dcheng30c5a172016-04-09 07:55:04 +0900423 std::unique_ptr<Response> message(Response::CreateEmpty());
dtapuska95f71722015-02-10 01:02:55 +0900424 MessageWriter writer(message.get());
425
avi0ad0ce02015-12-23 03:12:45 +0900426 Property<std::vector<std::pair<std::vector<uint8_t>, uint16_t>>> ip_list;
dtapuska95f71722015-02-10 01:02:55 +0900427 ip_list.ReplaceSetValueForTesting(test_list);
428 ip_list.AppendSetValueToWriter(&writer);
429
430 MessageReader reader(message.get());
431 EXPECT_TRUE(ip_list.PopValueFromReader(&reader));
432 EXPECT_EQ(test_list, ip_list.value());
433}
434
puthik90fd20c2016-10-06 14:03:03 +0900435TEST(PropertyTestStatic, ReadWriteStringToByteVectorMap) {
436 std::unique_ptr<Response> message(Response::CreateEmpty());
437 MessageWriter writer(message.get());
438 MessageWriter variant_writer(nullptr);
439 MessageWriter dict_writer(nullptr);
440
441 writer.OpenVariant("a{sv}", &variant_writer);
442 variant_writer.OpenArray("{sv}", &dict_writer);
443
444 const char* keys[] = {"One", "Two", "Three", "Four"};
445 const std::vector<uint8_t> values[] = {{1}, {1, 2}, {1, 2, 3}, {1, 2, 3, 4}};
446 for (unsigned i = 0; i < arraysize(keys); ++i) {
447 MessageWriter entry_writer(nullptr);
448 dict_writer.OpenDictEntry(&entry_writer);
449
450 entry_writer.AppendString(keys[i]);
451
452 MessageWriter value_varient_writer(nullptr);
453 entry_writer.OpenVariant("ay", &value_varient_writer);
454 value_varient_writer.AppendArrayOfBytes(values[i].data(), values[i].size());
455 entry_writer.CloseContainer(&value_varient_writer);
456
457 dict_writer.CloseContainer(&entry_writer);
458 }
459
460 variant_writer.CloseContainer(&dict_writer);
461 writer.CloseContainer(&variant_writer);
462
463 MessageReader reader(message.get());
464 Property<std::unordered_map<std::string, std::vector<uint8_t>>> test_property;
465 EXPECT_TRUE(test_property.PopValueFromReader(&reader));
466
467 ASSERT_EQ(arraysize(keys), test_property.value().size());
468 for (unsigned i = 0; i < arraysize(keys); ++i)
469 EXPECT_EQ(values[i], test_property.value().at(keys[i]));
470}
471
472TEST(PropertyTestStatic, SerializeStringToByteVectorMap) {
473 std::unordered_map<std::string, std::vector<uint8_t>> test_map;
474 test_map["Hi"] = {1, 2, 3};
475 test_map["Map"] = {0xab, 0xcd};
476 test_map["Random"] = {0x0};
477
478 std::unique_ptr<Response> message(Response::CreateEmpty());
479 MessageWriter writer(message.get());
480
481 Property<std::unordered_map<std::string, std::vector<uint8_t>>> test_property;
482 test_property.ReplaceSetValueForTesting(test_map);
483 test_property.AppendSetValueToWriter(&writer);
484
485 MessageReader reader(message.get());
486 EXPECT_TRUE(test_property.PopValueFromReader(&reader));
487 EXPECT_EQ(test_map, test_property.value());
488}
489
puthik6558aab2016-11-05 04:38:17 +0900490TEST(PropertyTestStatic, ReadWriteUInt16ToByteVectorMap) {
491 std::unique_ptr<Response> message(Response::CreateEmpty());
492 MessageWriter writer(message.get());
493 MessageWriter variant_writer(nullptr);
494 MessageWriter dict_writer(nullptr);
495
496 writer.OpenVariant("a{qv}", &variant_writer);
497 variant_writer.OpenArray("{qv}", &dict_writer);
498
499 const uint16_t keys[] = {11, 12, 13, 14};
500 const std::vector<uint8_t> values[] = {{1}, {1, 2}, {1, 2, 3}, {1, 2, 3, 4}};
501 for (unsigned i = 0; i < arraysize(keys); ++i) {
502 MessageWriter entry_writer(nullptr);
503 dict_writer.OpenDictEntry(&entry_writer);
504
505 entry_writer.AppendUint16(keys[i]);
506
507 MessageWriter value_varient_writer(nullptr);
508 entry_writer.OpenVariant("ay", &value_varient_writer);
509 value_varient_writer.AppendArrayOfBytes(values[i].data(), values[i].size());
510 entry_writer.CloseContainer(&value_varient_writer);
511
512 dict_writer.CloseContainer(&entry_writer);
513 }
514
515 variant_writer.CloseContainer(&dict_writer);
516 writer.CloseContainer(&variant_writer);
517
518 MessageReader reader(message.get());
519 Property<std::unordered_map<uint16_t, std::vector<uint8_t>>> test_property;
520 EXPECT_TRUE(test_property.PopValueFromReader(&reader));
521
522 ASSERT_EQ(arraysize(keys), test_property.value().size());
523 for (unsigned i = 0; i < arraysize(keys); ++i)
524 EXPECT_EQ(values[i], test_property.value().at(keys[i]));
525}
526
527TEST(PropertyTestStatic, SerializeUInt16ToByteVectorMap) {
528 std::unordered_map<uint16_t, std::vector<uint8_t>> test_map;
529 test_map[11] = {1, 2, 3};
530 test_map[12] = {0xab, 0xcd};
531 test_map[13] = {0x0};
532
533 std::unique_ptr<Response> message(Response::CreateEmpty());
534 MessageWriter writer(message.get());
535
536 Property<std::unordered_map<uint16_t, std::vector<uint8_t>>> test_property;
537 test_property.ReplaceSetValueForTesting(test_map);
538 test_property.AppendSetValueToWriter(&writer);
539
540 MessageReader reader(message.get());
541 EXPECT_TRUE(test_property.PopValueFromReader(&reader));
542 EXPECT_EQ(test_map, test_property.value());
543}
544
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900545} // namespace dbus