1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "dbus/property.h"
14 #include "base/functional/bind.h"
15 #include "base/memory/raw_ptr.h"
16 #include "base/message_loop/message_pump_type.h"
17 #include "base/run_loop.h"
18 #include "base/strings/string_number_conversions.h"
19 #include "base/test/task_environment.h"
20 #include "base/threading/thread.h"
21 #include "base/threading/thread_restrictions.h"
23 #include "dbus/object_path.h"
24 #include "dbus/object_proxy.h"
25 #include "dbus/test_service.h"
26 #include "testing/gtest/include/gtest/gtest.h"
30 // The property test exerises the asynchronous APIs in PropertySet and
32 class PropertyTest : public testing::Test {
34 PropertyTest() = default;
36 struct Properties : public PropertySet {
37 Property<std::string> name;
38 Property<int16_t> version;
39 Property<std::vector<std::string>> methods;
40 Property<std::vector<ObjectPath>> objects;
41 Property<std::vector<uint8_t>> bytes;
43 Properties(ObjectProxy* object_proxy,
44 PropertyChangedCallback property_changed_callback)
45 : PropertySet(object_proxy,
46 "org.chromium.TestInterface",
47 property_changed_callback) {
48 RegisterProperty("Name", &name);
49 RegisterProperty("Version", &version);
50 RegisterProperty("Methods", &methods);
51 RegisterProperty("Objects", &objects);
52 RegisterProperty("Bytes", &bytes);
56 void SetUp() override {
57 // Make the main thread not to allow IO.
58 disallow_blocking_.emplace();
60 // Start the D-Bus thread.
61 dbus_thread_ = std::make_unique<base::Thread>("D-Bus Thread");
62 base::Thread::Options thread_options;
63 thread_options.message_pump_type = base::MessagePumpType::IO;
64 ASSERT_TRUE(dbus_thread_->StartWithOptions(std::move(thread_options)));
66 // Start the test service, using the D-Bus thread.
67 TestService::Options options;
68 options.dbus_task_runner = dbus_thread_->task_runner();
69 test_service_ = std::make_unique<TestService>(options);
70 ASSERT_TRUE(test_service_->StartService());
71 test_service_->WaitUntilServiceIsStarted();
72 ASSERT_TRUE(test_service_->HasDBusThread());
74 // Create the client, using the D-Bus thread.
75 Bus::Options bus_options;
76 bus_options.bus_type = Bus::SESSION;
77 bus_options.connection_type = Bus::PRIVATE;
78 bus_options.dbus_task_runner = dbus_thread_->task_runner();
79 bus_ = new Bus(bus_options);
80 object_proxy_ = bus_->GetObjectProxy(
81 test_service_->service_name(),
82 ObjectPath("/org/chromium/TestObject"));
83 ASSERT_TRUE(bus_->HasDBusThread());
85 // Create the properties structure
86 properties_ = std::make_unique<Properties>(
87 object_proxy_, base::BindRepeating(&PropertyTest::OnPropertyChanged,
88 base::Unretained(this)));
89 properties_->ConnectSignals();
90 properties_->GetAll();
93 void TearDown() override {
94 bus_->ShutdownOnDBusThreadAndBlock();
96 // Shut down the service.
97 test_service_->ShutdownAndBlock();
99 // Stopping a thread is considered an IO operation, so do this after
101 disallow_blocking_.reset();
102 test_service_->Stop();
105 // Generic callback, bind with a string |id| for passing to
106 // WaitForCallback() to ensure the callback for the right method is
108 void PropertyCallback(const std::string& id, bool success) {
113 // Generic method callback, that might be used together with
114 // WaitForMethodCallback to test wether method was succesfully called.
115 void MethodCallback(Response* response) { run_loop_->Quit(); }
118 // Called when a property value is updated.
119 void OnPropertyChanged(const std::string& name) {
120 updated_properties_.push_back(name);
124 // Waits for the given number of updates.
125 void WaitForUpdates(size_t num_updates) {
126 while (updated_properties_.size() < num_updates) {
127 run_loop_ = std::make_unique<base::RunLoop>();
130 for (size_t i = 0; i < num_updates; ++i)
131 updated_properties_.erase(updated_properties_.begin());
134 // Name, Version, Methods, Objects
135 static const int kExpectedSignalUpdates = 5;
137 // Waits for initial values to be set.
138 void WaitForGetAll() {
139 WaitForUpdates(kExpectedSignalUpdates);
142 // Waits until MethodCallback is called.
143 void WaitForMethodCallback() {
144 run_loop_ = std::make_unique<base::RunLoop>();
148 // Waits for the callback. |id| is the string bound to the callback when
149 // the method call is made that identifies it and distinguishes from any
150 // other; you can set this to whatever you wish.
151 void WaitForCallback(const std::string& id) {
152 while (last_callback_ != id) {
153 run_loop_ = std::make_unique<base::RunLoop>();
158 base::test::SingleThreadTaskEnvironment task_environment_;
159 absl::optional<base::ScopedDisallowBlocking> disallow_blocking_;
160 std::unique_ptr<base::RunLoop> run_loop_;
161 std::unique_ptr<base::Thread> dbus_thread_;
162 scoped_refptr<Bus> bus_;
163 raw_ptr<ObjectProxy, AcrossTasksDanglingUntriaged> object_proxy_;
164 std::unique_ptr<Properties> properties_;
165 std::unique_ptr<TestService> test_service_;
166 // Properties updated.
167 std::vector<std::string> updated_properties_;
168 // Last callback received.
169 std::string last_callback_;
172 TEST_F(PropertyTest, InitialValues) {
173 EXPECT_FALSE(properties_->name.is_valid());
174 EXPECT_FALSE(properties_->version.is_valid());
178 EXPECT_TRUE(properties_->name.is_valid());
179 EXPECT_EQ("TestService", properties_->name.value());
180 EXPECT_TRUE(properties_->version.is_valid());
181 EXPECT_EQ(10, properties_->version.value());
183 std::vector<std::string> methods = properties_->methods.value();
184 ASSERT_EQ(4U, methods.size());
185 EXPECT_EQ("Echo", methods[0]);
186 EXPECT_EQ("SlowEcho", methods[1]);
187 EXPECT_EQ("AsyncEcho", methods[2]);
188 EXPECT_EQ("BrokenMethod", methods[3]);
190 std::vector<ObjectPath> objects = properties_->objects.value();
191 ASSERT_EQ(1U, objects.size());
192 EXPECT_EQ(ObjectPath("/TestObjectPath"), objects[0]);
194 std::vector<uint8_t> bytes = properties_->bytes.value();
195 ASSERT_EQ(4U, bytes.size());
196 EXPECT_EQ('T', bytes[0]);
197 EXPECT_EQ('e', bytes[1]);
198 EXPECT_EQ('s', bytes[2]);
199 EXPECT_EQ('t', bytes[3]);
202 TEST_F(PropertyTest, UpdatedValues) {
205 // Update the value of the "Name" property, this value should not change.
206 properties_->name.Get(base::BindOnce(&PropertyTest::PropertyCallback,
207 base::Unretained(this), "Name"));
208 WaitForCallback("Name");
211 EXPECT_EQ("TestService", properties_->name.value());
213 // Update the value of the "Version" property, this value should be changed.
214 properties_->version.Get(base::BindOnce(&PropertyTest::PropertyCallback,
215 base::Unretained(this), "Version"));
216 WaitForCallback("Version");
219 EXPECT_EQ(20, properties_->version.value());
221 // Update the value of the "Methods" property, this value should not change
222 // and should not grow to contain duplicate entries.
223 properties_->methods.Get(base::BindOnce(&PropertyTest::PropertyCallback,
224 base::Unretained(this), "Methods"));
225 WaitForCallback("Methods");
228 std::vector<std::string> methods = properties_->methods.value();
229 ASSERT_EQ(4U, methods.size());
230 EXPECT_EQ("Echo", methods[0]);
231 EXPECT_EQ("SlowEcho", methods[1]);
232 EXPECT_EQ("AsyncEcho", methods[2]);
233 EXPECT_EQ("BrokenMethod", methods[3]);
235 // Update the value of the "Objects" property, this value should not change
236 // and should not grow to contain duplicate entries.
237 properties_->objects.Get(base::BindOnce(&PropertyTest::PropertyCallback,
238 base::Unretained(this), "Objects"));
239 WaitForCallback("Objects");
242 std::vector<ObjectPath> objects = properties_->objects.value();
243 ASSERT_EQ(1U, objects.size());
244 EXPECT_EQ(ObjectPath("/TestObjectPath"), objects[0]);
246 // Update the value of the "Bytes" property, this value should not change
247 // and should not grow to contain duplicate entries.
248 properties_->bytes.Get(base::BindOnce(&PropertyTest::PropertyCallback,
249 base::Unretained(this), "Bytes"));
250 WaitForCallback("Bytes");
253 std::vector<uint8_t> bytes = properties_->bytes.value();
254 ASSERT_EQ(4U, bytes.size());
255 EXPECT_EQ('T', bytes[0]);
256 EXPECT_EQ('e', bytes[1]);
257 EXPECT_EQ('s', bytes[2]);
258 EXPECT_EQ('t', bytes[3]);
261 TEST_F(PropertyTest, Get) {
264 // Ask for the new Version property.
265 properties_->version.Get(base::BindOnce(&PropertyTest::PropertyCallback,
266 base::Unretained(this), "Get"));
267 WaitForCallback("Get");
269 // Make sure we got a property update too.
272 EXPECT_EQ(20, properties_->version.value());
275 TEST_F(PropertyTest, Set) {
279 properties_->name.Set("NewService",
280 base::BindOnce(&PropertyTest::PropertyCallback,
281 base::Unretained(this), "Set"));
282 WaitForCallback("Set");
284 // TestService sends a property update.
287 EXPECT_EQ("NewService", properties_->name.value());
290 TEST_F(PropertyTest, Invalidate) {
293 EXPECT_TRUE(properties_->name.is_valid());
296 MethodCall method_call("org.chromium.TestInterface", "PerformAction");
297 MessageWriter writer(&method_call);
298 writer.AppendString("InvalidateProperty");
299 writer.AppendObjectPath(ObjectPath("/org/chromium/TestService"));
300 object_proxy_->CallMethod(
301 &method_call, ObjectProxy::TIMEOUT_USE_DEFAULT,
302 base::BindOnce(&PropertyTest::MethodCallback, base::Unretained(this)));
303 WaitForMethodCallback();
305 // TestService sends a property update.
308 EXPECT_FALSE(properties_->name.is_valid());
310 // Set name to something valid.
311 properties_->name.Set("NewService",
312 base::BindOnce(&PropertyTest::PropertyCallback,
313 base::Unretained(this), "Set"));
314 WaitForCallback("Set");
316 // TestService sends a property update.
319 EXPECT_TRUE(properties_->name.is_valid());
322 TEST(PropertyTestStatic, ReadWriteStringMap) {
323 std::unique_ptr<Response> message(Response::CreateEmpty());
324 MessageWriter writer(message.get());
325 MessageWriter variant_writer(nullptr);
326 MessageWriter variant_array_writer(nullptr);
327 MessageWriter struct_entry_writer(nullptr);
329 writer.OpenVariant("a{ss}", &variant_writer);
330 variant_writer.OpenArray("{ss}", &variant_array_writer);
331 const char* items[] = {"One", "Two", "Three", "Four"};
332 for (unsigned i = 0; i < std::size(items); ++i) {
333 variant_array_writer.OpenDictEntry(&struct_entry_writer);
334 struct_entry_writer.AppendString(items[i]);
335 struct_entry_writer.AppendString(base::NumberToString(i + 1));
336 variant_array_writer.CloseContainer(&struct_entry_writer);
338 variant_writer.CloseContainer(&variant_array_writer);
339 writer.CloseContainer(&variant_writer);
341 MessageReader reader(message.get());
342 Property<std::map<std::string, std::string>> string_map;
343 EXPECT_TRUE(string_map.PopValueFromReader(&reader));
344 ASSERT_EQ(4U, string_map.value().size());
345 EXPECT_EQ("1", string_map.value().at("One"));
346 EXPECT_EQ("2", string_map.value().at("Two"));
347 EXPECT_EQ("3", string_map.value().at("Three"));
348 EXPECT_EQ("4", string_map.value().at("Four"));
351 TEST(PropertyTestStatic, SerializeStringMap) {
352 std::map<std::string, std::string> test_map;
353 test_map["Hi"] = "There";
354 test_map["Map"] = "Test";
355 test_map["Random"] = "Text";
357 std::unique_ptr<Response> message(Response::CreateEmpty());
358 MessageWriter writer(message.get());
360 Property<std::map<std::string, std::string>> string_map;
361 string_map.ReplaceSetValueForTesting(test_map);
362 string_map.AppendSetValueToWriter(&writer);
364 MessageReader reader(message.get());
365 EXPECT_TRUE(string_map.PopValueFromReader(&reader));
366 EXPECT_EQ(test_map, string_map.value());
369 TEST(PropertyTestStatic, ReadWriteNetAddressArray) {
370 std::unique_ptr<Response> message(Response::CreateEmpty());
371 MessageWriter writer(message.get());
372 MessageWriter variant_writer(nullptr);
373 MessageWriter variant_array_writer(nullptr);
374 MessageWriter struct_entry_writer(nullptr);
376 writer.OpenVariant("a(ayq)", &variant_writer);
377 variant_writer.OpenArray("(ayq)", &variant_array_writer);
378 uint8_t ip_bytes[] = {0x54, 0x65, 0x73, 0x74, 0x30};
379 for (uint16_t i = 0; i < 5; ++i) {
380 variant_array_writer.OpenStruct(&struct_entry_writer);
381 ip_bytes[4] = 0x30 + i;
382 struct_entry_writer.AppendArrayOfBytes(ip_bytes, std::size(ip_bytes));
383 struct_entry_writer.AppendUint16(i);
384 variant_array_writer.CloseContainer(&struct_entry_writer);
386 variant_writer.CloseContainer(&variant_array_writer);
387 writer.CloseContainer(&variant_writer);
389 MessageReader reader(message.get());
390 Property<std::vector<std::pair<std::vector<uint8_t>, uint16_t>>> ip_list;
391 EXPECT_TRUE(ip_list.PopValueFromReader(&reader));
393 ASSERT_EQ(5U, ip_list.value().size());
394 size_t item_index = 0;
395 for (auto& item : ip_list.value()) {
396 ASSERT_EQ(5U, item.first.size());
397 ip_bytes[4] = 0x30 + item_index;
398 EXPECT_EQ(0, memcmp(ip_bytes, item.first.data(), 5U));
399 EXPECT_EQ(item_index, item.second);
404 TEST(PropertyTestStatic, SerializeNetAddressArray) {
405 std::vector<std::pair<std::vector<uint8_t>, uint16_t>> test_list;
407 uint8_t ip_bytes[] = {0x54, 0x65, 0x73, 0x74, 0x30};
408 for (uint16_t i = 0; i < 5; ++i) {
409 ip_bytes[4] = 0x30 + i;
410 std::vector<uint8_t> bytes(ip_bytes, ip_bytes + std::size(ip_bytes));
411 test_list.push_back(make_pair(bytes, 16));
414 std::unique_ptr<Response> message(Response::CreateEmpty());
415 MessageWriter writer(message.get());
417 Property<std::vector<std::pair<std::vector<uint8_t>, uint16_t>>> ip_list;
418 ip_list.ReplaceSetValueForTesting(test_list);
419 ip_list.AppendSetValueToWriter(&writer);
421 MessageReader reader(message.get());
422 EXPECT_TRUE(ip_list.PopValueFromReader(&reader));
423 EXPECT_EQ(test_list, ip_list.value());
426 TEST(PropertyTestStatic, ReadWriteStringToByteVectorMapVariantWrapped) {
427 std::unique_ptr<Response> message(Response::CreateEmpty());
428 MessageWriter writer(message.get());
429 MessageWriter variant_writer(nullptr);
430 MessageWriter dict_writer(nullptr);
432 writer.OpenVariant("a{sv}", &variant_writer);
433 variant_writer.OpenArray("{sv}", &dict_writer);
435 const char* keys[] = {"One", "Two", "Three", "Four"};
436 const std::vector<uint8_t> values[] = {{1}, {1, 2}, {1, 2, 3}, {1, 2, 3, 4}};
437 for (unsigned i = 0; i < std::size(keys); ++i) {
438 MessageWriter entry_writer(nullptr);
439 dict_writer.OpenDictEntry(&entry_writer);
441 entry_writer.AppendString(keys[i]);
443 MessageWriter value_varient_writer(nullptr);
444 entry_writer.OpenVariant("ay", &value_varient_writer);
445 value_varient_writer.AppendArrayOfBytes(values[i].data(), values[i].size());
446 entry_writer.CloseContainer(&value_varient_writer);
448 dict_writer.CloseContainer(&entry_writer);
451 variant_writer.CloseContainer(&dict_writer);
452 writer.CloseContainer(&variant_writer);
454 MessageReader reader(message.get());
455 Property<std::map<std::string, std::vector<uint8_t>>> test_property;
456 EXPECT_TRUE(test_property.PopValueFromReader(&reader));
458 ASSERT_EQ(std::size(keys), test_property.value().size());
459 for (unsigned i = 0; i < std::size(keys); ++i)
460 EXPECT_EQ(values[i], test_property.value().at(keys[i]));
463 TEST(PropertyTestStatic, ReadWriteStringToByteVectorMap) {
464 std::unique_ptr<Response> message(Response::CreateEmpty());
465 MessageWriter writer(message.get());
466 MessageWriter variant_writer(nullptr);
467 MessageWriter dict_writer(nullptr);
469 writer.OpenVariant("a{say}", &variant_writer);
470 variant_writer.OpenArray("{say}", &dict_writer);
472 const char* keys[] = {"One", "Two", "Three", "Four"};
473 const std::vector<uint8_t> values[] = {{1}, {1, 2}, {1, 2, 3}, {1, 2, 3, 4}};
474 for (unsigned i = 0; i < std::size(keys); ++i) {
475 MessageWriter entry_writer(nullptr);
476 dict_writer.OpenDictEntry(&entry_writer);
478 entry_writer.AppendString(keys[i]);
479 entry_writer.AppendArrayOfBytes(values[i].data(), values[i].size());
481 dict_writer.CloseContainer(&entry_writer);
484 variant_writer.CloseContainer(&dict_writer);
485 writer.CloseContainer(&variant_writer);
487 MessageReader reader(message.get());
488 Property<std::map<std::string, std::vector<uint8_t>>> test_property;
489 EXPECT_TRUE(test_property.PopValueFromReader(&reader));
491 ASSERT_EQ(std::size(keys), test_property.value().size());
492 for (unsigned i = 0; i < std::size(keys); ++i)
493 EXPECT_EQ(values[i], test_property.value().at(keys[i]));
496 TEST(PropertyTestStatic, SerializeStringToByteVectorMap) {
497 std::map<std::string, std::vector<uint8_t>> test_map;
498 test_map["Hi"] = {1, 2, 3};
499 test_map["Map"] = {0xab, 0xcd};
500 test_map["Random"] = {0x0};
502 std::unique_ptr<Response> message(Response::CreateEmpty());
503 MessageWriter writer(message.get());
505 Property<std::map<std::string, std::vector<uint8_t>>> test_property;
506 test_property.ReplaceSetValueForTesting(test_map);
507 test_property.AppendSetValueToWriter(&writer);
509 MessageReader reader(message.get());
510 EXPECT_TRUE(test_property.PopValueFromReader(&reader));
511 EXPECT_EQ(test_map, test_property.value());
514 TEST(PropertyTestStatic, ReadWriteUInt16ToByteVectorMapVariantWrapped) {
515 std::unique_ptr<Response> message(Response::CreateEmpty());
516 MessageWriter writer(message.get());
517 MessageWriter variant_writer(nullptr);
518 MessageWriter dict_writer(nullptr);
520 writer.OpenVariant("a{qv}", &variant_writer);
521 variant_writer.OpenArray("{qv}", &dict_writer);
523 const uint16_t keys[] = {11, 12, 13, 14};
524 const std::vector<uint8_t> values[] = {{1}, {1, 2}, {1, 2, 3}, {1, 2, 3, 4}};
525 for (unsigned i = 0; i < std::size(keys); ++i) {
526 MessageWriter entry_writer(nullptr);
527 dict_writer.OpenDictEntry(&entry_writer);
529 entry_writer.AppendUint16(keys[i]);
531 MessageWriter value_varient_writer(nullptr);
532 entry_writer.OpenVariant("ay", &value_varient_writer);
533 value_varient_writer.AppendArrayOfBytes(values[i].data(), values[i].size());
534 entry_writer.CloseContainer(&value_varient_writer);
536 dict_writer.CloseContainer(&entry_writer);
539 variant_writer.CloseContainer(&dict_writer);
540 writer.CloseContainer(&variant_writer);
542 MessageReader reader(message.get());
543 Property<std::map<uint16_t, std::vector<uint8_t>>> test_property;
544 EXPECT_TRUE(test_property.PopValueFromReader(&reader));
546 ASSERT_EQ(std::size(keys), test_property.value().size());
547 for (unsigned i = 0; i < std::size(keys); ++i)
548 EXPECT_EQ(values[i], test_property.value().at(keys[i]));
551 TEST(PropertyTestStatic, ReadWriteUInt16ToByteVectorMap) {
552 std::unique_ptr<Response> message(Response::CreateEmpty());
553 MessageWriter writer(message.get());
554 MessageWriter variant_writer(nullptr);
555 MessageWriter dict_writer(nullptr);
557 writer.OpenVariant("a{qay}", &variant_writer);
558 variant_writer.OpenArray("{qay}", &dict_writer);
560 const uint16_t keys[] = {11, 12, 13, 14};
561 const std::vector<uint8_t> values[] = {{1}, {1, 2}, {1, 2, 3}, {1, 2, 3, 4}};
562 for (unsigned i = 0; i < std::size(keys); ++i) {
563 MessageWriter entry_writer(nullptr);
564 dict_writer.OpenDictEntry(&entry_writer);
566 entry_writer.AppendUint16(keys[i]);
567 entry_writer.AppendArrayOfBytes(values[i].data(), values[i].size());
569 dict_writer.CloseContainer(&entry_writer);
572 variant_writer.CloseContainer(&dict_writer);
573 writer.CloseContainer(&variant_writer);
575 MessageReader reader(message.get());
576 Property<std::map<uint16_t, std::vector<uint8_t>>> test_property;
577 EXPECT_TRUE(test_property.PopValueFromReader(&reader));
579 ASSERT_EQ(std::size(keys), test_property.value().size());
580 for (unsigned i = 0; i < std::size(keys); ++i)
581 EXPECT_EQ(values[i], test_property.value().at(keys[i]));
584 TEST(PropertyTestStatic, SerializeUInt16ToByteVectorMap) {
585 std::map<uint16_t, std::vector<uint8_t>> test_map;
586 test_map[11] = {1, 2, 3};
587 test_map[12] = {0xab, 0xcd};
588 test_map[13] = {0x0};
590 std::unique_ptr<Response> message(Response::CreateEmpty());
591 MessageWriter writer(message.get());
593 Property<std::map<uint16_t, std::vector<uint8_t>>> test_property;
594 test_property.ReplaceSetValueForTesting(test_map);
595 test_property.AppendSetValueToWriter(&writer);
597 MessageReader reader(message.get());
598 EXPECT_TRUE(test_property.PopValueFromReader(&reader));
599 EXPECT_EQ(test_map, test_property.value());