blob: 59faa22e1a8e3679d3c047f239f345e940fae0b7 [file] [log] [blame]
[email protected]cf910da22012-02-15 04:21:081// 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
7#include <string>
8#include <vector>
9
10#include "base/basictypes.h"
11#include "base/bind.h"
12#include "base/logging.h"
[email protected]2a9ec0e2013-07-17 23:00:3013#include "base/message_loop/message_loop.h"
earthdok9562caf2014-09-03 10:32:3614#include "base/run_loop.h"
dtapuska32d25452015-02-09 16:02:5515#include "base/strings/string_number_conversions.h"
[email protected]cf910da22012-02-15 04:21:0816#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"
[email protected]cf910da22012-02-15 04:21:0821#include "dbus/test_service.h"
22#include "testing/gtest/include/gtest/gtest.h"
23
[email protected]2a57ca642013-06-13 06:37:1924namespace dbus {
25
[email protected]cf910da22012-02-15 04:21:0826// The property test exerises the asynchronous APIs in PropertySet and
27// Property<>.
28class PropertyTest : public testing::Test {
29 public:
jpawlowskied276542015-05-11 11:07:0430 PropertyTest() {}
[email protected]cf910da22012-02-15 04:21:0831
[email protected]2a57ca642013-06-13 06:37:1932 struct Properties : public PropertySet {
33 Property<std::string> name;
34 Property<int16> version;
35 Property<std::vector<std::string> > methods;
36 Property<std::vector<ObjectPath> > objects;
[email protected]ebbfffa22014-03-15 07:40:4937 Property<std::vector<uint8> > bytes;
[email protected]cf910da22012-02-15 04:21:0838
[email protected]2a57ca642013-06-13 06:37:1939 Properties(ObjectProxy* object_proxy,
[email protected]cf910da22012-02-15 04:21:0840 PropertyChangedCallback property_changed_callback)
[email protected]2a57ca642013-06-13 06:37:1941 : PropertySet(object_proxy,
42 "org.chromium.TestInterface",
43 property_changed_callback) {
[email protected]cf910da22012-02-15 04:21:0844 RegisterProperty("Name", &name);
45 RegisterProperty("Version", &version);
46 RegisterProperty("Methods", &methods);
47 RegisterProperty("Objects", &objects);
[email protected]ebbfffa22014-03-15 07:40:4948 RegisterProperty("Bytes", &bytes);
[email protected]cf910da22012-02-15 04:21:0849 }
50 };
51
dcheng3bb7119c2014-12-29 18:30:1752 void SetUp() override {
[email protected]cf910da22012-02-15 04:21:0853 // Make the main thread not to allow IO.
54 base::ThreadRestrictions::SetIOAllowed(false);
55
56 // Start the D-Bus thread.
57 dbus_thread_.reset(new base::Thread("D-Bus Thread"));
58 base::Thread::Options thread_options;
[email protected]ff33b18e2013-05-01 16:10:3059 thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
[email protected]cf910da22012-02-15 04:21:0860 ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options));
61
62 // Start the test service, using the D-Bus thread.
[email protected]2a57ca642013-06-13 06:37:1963 TestService::Options options;
skyostil8a033aa2015-06-17 15:46:0464 options.dbus_task_runner = dbus_thread_->task_runner();
[email protected]2a57ca642013-06-13 06:37:1965 test_service_.reset(new TestService(options));
[email protected]cf910da22012-02-15 04:21:0866 ASSERT_TRUE(test_service_->StartService());
67 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted());
68 ASSERT_TRUE(test_service_->HasDBusThread());
69
70 // Create the client, using the D-Bus thread.
[email protected]2a57ca642013-06-13 06:37:1971 Bus::Options bus_options;
72 bus_options.bus_type = Bus::SESSION;
73 bus_options.connection_type = Bus::PRIVATE;
skyostil8a033aa2015-06-17 15:46:0474 bus_options.dbus_task_runner = dbus_thread_->task_runner();
[email protected]2a57ca642013-06-13 06:37:1975 bus_ = new Bus(bus_options);
[email protected]cf910da22012-02-15 04:21:0876 object_proxy_ = bus_->GetObjectProxy(
77 "org.chromium.TestService",
[email protected]2a57ca642013-06-13 06:37:1978 ObjectPath("/org/chromium/TestObject"));
[email protected]cf910da22012-02-15 04:21:0879 ASSERT_TRUE(bus_->HasDBusThread());
80
81 // Create the properties structure
[email protected]ba4ce1c2012-05-31 06:55:5382 properties_.reset(new Properties(
83 object_proxy_,
84 base::Bind(&PropertyTest::OnPropertyChanged,
85 base::Unretained(this))));
[email protected]cf910da22012-02-15 04:21:0886 properties_->ConnectSignals();
87 properties_->GetAll();
88 }
89
dcheng3bb7119c2014-12-29 18:30:1790 void TearDown() override {
[email protected]cf910da22012-02-15 04:21:0891 bus_->ShutdownOnDBusThreadAndBlock();
92
93 // Shut down the service.
94 test_service_->ShutdownAndBlock();
95
96 // Reset to the default.
97 base::ThreadRestrictions::SetIOAllowed(true);
98
99 // Stopping a thread is considered an IO operation, so do this after
100 // allowing IO.
101 test_service_->Stop();
102 }
103
104 // Generic callback, bind with a string |id| for passing to
105 // WaitForCallback() to ensure the callback for the right method is
106 // waited for.
107 void PropertyCallback(const std::string& id, bool success) {
108 last_callback_ = id;
earthdok9562caf2014-09-03 10:32:36109 run_loop_->Quit();
[email protected]cf910da22012-02-15 04:21:08110 }
111
jpawlowskied276542015-05-11 11:07:04112 // Generic method callback, that might be used together with
113 // WaitForMethodCallback to test wether method was succesfully called.
114 void MethodCallback(Response* response) { run_loop_->Quit(); }
115
[email protected]cf910da22012-02-15 04:21:08116 protected:
117 // Called when a property value is updated.
118 void OnPropertyChanged(const std::string& name) {
119 updated_properties_.push_back(name);
earthdok9562caf2014-09-03 10:32:36120 run_loop_->Quit();
[email protected]cf910da22012-02-15 04:21:08121 }
122
[email protected]8a3eaffa2012-03-22 20:34:05123 // Waits for the given number of updates.
[email protected]cf910da22012-02-15 04:21:08124 void WaitForUpdates(size_t num_updates) {
earthdok9562caf2014-09-03 10:32:36125 while (updated_properties_.size() < num_updates) {
126 run_loop_.reset(new base::RunLoop);
127 run_loop_->Run();
128 }
[email protected]cf910da22012-02-15 04:21:08129 for (size_t i = 0; i < num_updates; ++i)
130 updated_properties_.erase(updated_properties_.begin());
131 }
132
133 // Name, Version, Methods, Objects
[email protected]ebbfffa22014-03-15 07:40:49134 static const int kExpectedSignalUpdates = 5;
[email protected]cf910da22012-02-15 04:21:08135
[email protected]8a3eaffa2012-03-22 20:34:05136 // Waits for initial values to be set.
[email protected]cf910da22012-02-15 04:21:08137 void WaitForGetAll() {
138 WaitForUpdates(kExpectedSignalUpdates);
139 }
140
jpawlowskied276542015-05-11 11:07:04141 // Waits until MethodCallback is called.
142 void WaitForMethodCallback() {
143 run_loop_.reset(new base::RunLoop);
144 run_loop_->Run();
145 }
146
[email protected]8a3eaffa2012-03-22 20:34:05147 // Waits for the callback. |id| is the string bound to the callback when
[email protected]cf910da22012-02-15 04:21:08148 // the method call is made that identifies it and distinguishes from any
149 // other; you can set this to whatever you wish.
150 void WaitForCallback(const std::string& id) {
151 while (last_callback_ != id) {
earthdok9562caf2014-09-03 10:32:36152 run_loop_.reset(new base::RunLoop);
153 run_loop_->Run();
[email protected]cf910da22012-02-15 04:21:08154 }
155 }
156
[email protected]ff33b18e2013-05-01 16:10:30157 base::MessageLoop message_loop_;
earthdok9562caf2014-09-03 10:32:36158 scoped_ptr<base::RunLoop> run_loop_;
[email protected]cf910da22012-02-15 04:21:08159 scoped_ptr<base::Thread> dbus_thread_;
[email protected]2a57ca642013-06-13 06:37:19160 scoped_refptr<Bus> bus_;
161 ObjectProxy* object_proxy_;
[email protected]ba4ce1c2012-05-31 06:55:53162 scoped_ptr<Properties> properties_;
[email protected]2a57ca642013-06-13 06:37:19163 scoped_ptr<TestService> test_service_;
[email protected]cf910da22012-02-15 04:21:08164 // Properties updated.
165 std::vector<std::string> updated_properties_;
166 // Last callback received.
167 std::string last_callback_;
168};
169
170TEST_F(PropertyTest, InitialValues) {
jpawlowskied276542015-05-11 11:07:04171 EXPECT_FALSE(properties_->name.is_valid());
172 EXPECT_FALSE(properties_->version.is_valid());
173
[email protected]cf910da22012-02-15 04:21:08174 WaitForGetAll();
175
jpawlowskied276542015-05-11 11:07:04176 EXPECT_TRUE(properties_->name.is_valid());
[email protected]8a3eaffa2012-03-22 20:34:05177 EXPECT_EQ("TestService", properties_->name.value());
jpawlowskied276542015-05-11 11:07:04178 EXPECT_TRUE(properties_->version.is_valid());
[email protected]8a3eaffa2012-03-22 20:34:05179 EXPECT_EQ(10, properties_->version.value());
[email protected]cf910da22012-02-15 04:21:08180
181 std::vector<std::string> methods = properties_->methods.value();
182 ASSERT_EQ(4U, methods.size());
183 EXPECT_EQ("Echo", methods[0]);
184 EXPECT_EQ("SlowEcho", methods[1]);
185 EXPECT_EQ("AsyncEcho", methods[2]);
186 EXPECT_EQ("BrokenMethod", methods[3]);
187
[email protected]2a57ca642013-06-13 06:37:19188 std::vector<ObjectPath> objects = properties_->objects.value();
[email protected]cf910da22012-02-15 04:21:08189 ASSERT_EQ(1U, objects.size());
[email protected]2a57ca642013-06-13 06:37:19190 EXPECT_EQ(ObjectPath("/TestObjectPath"), objects[0]);
[email protected]ebbfffa22014-03-15 07:40:49191
192 std::vector<uint8> bytes = properties_->bytes.value();
193 ASSERT_EQ(4U, bytes.size());
194 EXPECT_EQ('T', bytes[0]);
195 EXPECT_EQ('e', bytes[1]);
196 EXPECT_EQ('s', bytes[2]);
197 EXPECT_EQ('t', bytes[3]);
[email protected]cf910da22012-02-15 04:21:08198}
199
[email protected]72bbacc2012-03-21 23:43:45200TEST_F(PropertyTest, UpdatedValues) {
201 WaitForGetAll();
202
203 // Update the value of the "Name" property, this value should not change.
204 properties_->name.Get(base::Bind(&PropertyTest::PropertyCallback,
205 base::Unretained(this),
206 "Name"));
207 WaitForCallback("Name");
208 WaitForUpdates(1);
209
[email protected]8a3eaffa2012-03-22 20:34:05210 EXPECT_EQ("TestService", properties_->name.value());
[email protected]72bbacc2012-03-21 23:43:45211
212 // Update the value of the "Version" property, this value should be changed.
213 properties_->version.Get(base::Bind(&PropertyTest::PropertyCallback,
214 base::Unretained(this),
215 "Version"));
216 WaitForCallback("Version");
217 WaitForUpdates(1);
218
[email protected]8a3eaffa2012-03-22 20:34:05219 EXPECT_EQ(20, properties_->version.value());
[email protected]72bbacc2012-03-21 23:43:45220
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::Bind(&PropertyTest::PropertyCallback,
224 base::Unretained(this),
225 "Methods"));
226 WaitForCallback("Methods");
227 WaitForUpdates(1);
228
229 std::vector<std::string> methods = properties_->methods.value();
230 ASSERT_EQ(4U, methods.size());
231 EXPECT_EQ("Echo", methods[0]);
232 EXPECT_EQ("SlowEcho", methods[1]);
233 EXPECT_EQ("AsyncEcho", methods[2]);
234 EXPECT_EQ("BrokenMethod", methods[3]);
235
236 // Update the value of the "Objects" property, this value should not change
237 // and should not grow to contain duplicate entries.
238 properties_->objects.Get(base::Bind(&PropertyTest::PropertyCallback,
239 base::Unretained(this),
240 "Objects"));
241 WaitForCallback("Objects");
242 WaitForUpdates(1);
243
[email protected]2a57ca642013-06-13 06:37:19244 std::vector<ObjectPath> objects = properties_->objects.value();
[email protected]72bbacc2012-03-21 23:43:45245 ASSERT_EQ(1U, objects.size());
[email protected]2a57ca642013-06-13 06:37:19246 EXPECT_EQ(ObjectPath("/TestObjectPath"), objects[0]);
[email protected]ebbfffa22014-03-15 07:40:49247
248 // Update the value of the "Bytes" property, this value should not change
249 // and should not grow to contain duplicate entries.
250 properties_->bytes.Get(base::Bind(&PropertyTest::PropertyCallback,
251 base::Unretained(this),
252 "Bytes"));
253 WaitForCallback("Bytes");
254 WaitForUpdates(1);
255
256 std::vector<uint8> bytes = properties_->bytes.value();
257 ASSERT_EQ(4U, bytes.size());
258 EXPECT_EQ('T', bytes[0]);
259 EXPECT_EQ('e', bytes[1]);
260 EXPECT_EQ('s', bytes[2]);
261 EXPECT_EQ('t', bytes[3]);
[email protected]72bbacc2012-03-21 23:43:45262}
263
[email protected]cf910da22012-02-15 04:21:08264TEST_F(PropertyTest, Get) {
265 WaitForGetAll();
266
267 // Ask for the new Version property.
268 properties_->version.Get(base::Bind(&PropertyTest::PropertyCallback,
269 base::Unretained(this),
270 "Get"));
271 WaitForCallback("Get");
272
273 // Make sure we got a property update too.
274 WaitForUpdates(1);
275
[email protected]8a3eaffa2012-03-22 20:34:05276 EXPECT_EQ(20, properties_->version.value());
[email protected]cf910da22012-02-15 04:21:08277}
278
279TEST_F(PropertyTest, Set) {
280 WaitForGetAll();
281
282 // Set a new name.
283 properties_->name.Set("NewService",
284 base::Bind(&PropertyTest::PropertyCallback,
285 base::Unretained(this),
286 "Set"));
287 WaitForCallback("Set");
288
289 // TestService sends a property update.
290 WaitForUpdates(1);
291
[email protected]8a3eaffa2012-03-22 20:34:05292 EXPECT_EQ("NewService", properties_->name.value());
[email protected]cf910da22012-02-15 04:21:08293}
[email protected]2a57ca642013-06-13 06:37:19294
jpawlowskied276542015-05-11 11:07:04295TEST_F(PropertyTest, Invalidate) {
296 WaitForGetAll();
297
298 EXPECT_TRUE(properties_->name.is_valid());
299
300 // Invalidate name.
301 MethodCall method_call("org.chromium.TestInterface", "PerformAction");
302 MessageWriter writer(&method_call);
303 writer.AppendString("InvalidateProperty");
304 writer.AppendObjectPath(ObjectPath("/org/chromium/TestService"));
305 object_proxy_->CallMethod(
306 &method_call, ObjectProxy::TIMEOUT_USE_DEFAULT,
307 base::Bind(&PropertyTest::MethodCallback, base::Unretained(this)));
308 WaitForMethodCallback();
309
310 // TestService sends a property update.
311 WaitForUpdates(1);
312
313 EXPECT_FALSE(properties_->name.is_valid());
314
315 // Set name to something valid.
316 properties_->name.Set("NewService",
317 base::Bind(&PropertyTest::PropertyCallback,
318 base::Unretained(this), "Set"));
319 WaitForCallback("Set");
320
321 // TestService sends a property update.
322 WaitForUpdates(1);
323
324 EXPECT_TRUE(properties_->name.is_valid());
325}
326
dtapuska32d25452015-02-09 16:02:55327TEST(PropertyTestStatic, ReadWriteStringMap) {
328 scoped_ptr<Response> message(Response::CreateEmpty());
329 MessageWriter writer(message.get());
330 MessageWriter variant_writer(NULL);
331 MessageWriter variant_array_writer(NULL);
332 MessageWriter struct_entry_writer(NULL);
333
334 writer.OpenVariant("a{ss}", &variant_writer);
335 variant_writer.OpenArray("{ss}", &variant_array_writer);
336 const char* items[] = {"One", "Two", "Three", "Four"};
337 for (unsigned i = 0; i < arraysize(items); ++i) {
338 variant_array_writer.OpenDictEntry(&struct_entry_writer);
339 struct_entry_writer.AppendString(items[i]);
340 struct_entry_writer.AppendString(base::UintToString(i + 1));
341 variant_array_writer.CloseContainer(&struct_entry_writer);
342 }
343 variant_writer.CloseContainer(&variant_array_writer);
344 writer.CloseContainer(&variant_writer);
345
346 MessageReader reader(message.get());
347 Property<std::map<std::string, std::string>> string_map;
348 EXPECT_TRUE(string_map.PopValueFromReader(&reader));
349 ASSERT_EQ(4U, string_map.value().size());
350 EXPECT_EQ("1", string_map.value().at("One"));
351 EXPECT_EQ("2", string_map.value().at("Two"));
352 EXPECT_EQ("3", string_map.value().at("Three"));
353 EXPECT_EQ("4", string_map.value().at("Four"));
354}
355
356TEST(PropertyTestStatic, SerializeStringMap) {
357 std::map<std::string, std::string> test_map;
358 test_map["Hi"] = "There";
359 test_map["Map"] = "Test";
360 test_map["Random"] = "Text";
361
362 scoped_ptr<Response> message(Response::CreateEmpty());
363 MessageWriter writer(message.get());
364
365 Property<std::map<std::string, std::string>> string_map;
366 string_map.ReplaceSetValueForTesting(test_map);
367 string_map.AppendSetValueToWriter(&writer);
368
369 MessageReader reader(message.get());
370 EXPECT_TRUE(string_map.PopValueFromReader(&reader));
371 EXPECT_EQ(test_map, string_map.value());
372}
373
374TEST(PropertyTestStatic, ReadWriteNetAddressArray) {
375 scoped_ptr<Response> message(Response::CreateEmpty());
376 MessageWriter writer(message.get());
377 MessageWriter variant_writer(NULL);
378 MessageWriter variant_array_writer(NULL);
379 MessageWriter struct_entry_writer(NULL);
380
381 writer.OpenVariant("a(ayq)", &variant_writer);
382 variant_writer.OpenArray("(ayq)", &variant_array_writer);
383 uint8 ip_bytes[] = {0x54, 0x65, 0x73, 0x74, 0x30};
384 for (uint16 i = 0; i < 5; ++i) {
385 variant_array_writer.OpenStruct(&struct_entry_writer);
386 ip_bytes[4] = 0x30 + i;
387 struct_entry_writer.AppendArrayOfBytes(ip_bytes, arraysize(ip_bytes));
388 struct_entry_writer.AppendUint16(i);
389 variant_array_writer.CloseContainer(&struct_entry_writer);
390 }
391 variant_writer.CloseContainer(&variant_array_writer);
392 writer.CloseContainer(&variant_writer);
393
394 MessageReader reader(message.get());
395 Property<std::vector<std::pair<std::vector<uint8>, uint16>>> ip_list;
396 EXPECT_TRUE(ip_list.PopValueFromReader(&reader));
397
398 ASSERT_EQ(5U, ip_list.value().size());
399 size_t item_index = 0;
400 for (auto& item : ip_list.value()) {
401 ASSERT_EQ(5U, item.first.size());
402 ip_bytes[4] = 0x30 + item_index;
403 EXPECT_EQ(0, memcmp(ip_bytes, item.first.data(), 5U));
404 EXPECT_EQ(item_index, item.second);
405 ++item_index;
406 }
407}
408
409TEST(PropertyTestStatic, SerializeNetAddressArray) {
410 std::vector<std::pair<std::vector<uint8>, uint16>> test_list;
411
412 uint8 ip_bytes[] = {0x54, 0x65, 0x73, 0x74, 0x30};
413 for (uint16 i = 0; i < 5; ++i) {
414 ip_bytes[4] = 0x30 + i;
415 std::vector<uint8> bytes(ip_bytes, ip_bytes + arraysize(ip_bytes));
416 test_list.push_back(make_pair(bytes, 16));
417 }
418
419 scoped_ptr<Response> message(Response::CreateEmpty());
420 MessageWriter writer(message.get());
421
422 Property<std::vector<std::pair<std::vector<uint8>, uint16>>> ip_list;
423 ip_list.ReplaceSetValueForTesting(test_list);
424 ip_list.AppendSetValueToWriter(&writer);
425
426 MessageReader reader(message.get());
427 EXPECT_TRUE(ip_list.PopValueFromReader(&reader));
428 EXPECT_EQ(test_list, ip_list.value());
429}
430
[email protected]2a57ca642013-06-13 06:37:19431} // namespace dbus