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.
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop.h"
12 #include "base/stl_util.h"
13 #include "base/test/test_timeouts.h"
14 #include "base/threading/thread.h"
15 #include "base/threading/thread_restrictions.h"
17 #include "dbus/message.h"
18 #include "dbus/object_path.h"
19 #include "dbus/object_proxy.h"
20 #include "dbus/test_service.h"
21 #include "testing/gtest/include/gtest/gtest.h"
25 // See comments in ObjectProxy::RunResponseCallback() for why the number was
27 const int kHugePayloadSize
= 64 << 20; // 64 MB
31 // The end-to-end test exercises the asynchronous APIs in ObjectProxy and
33 class EndToEndAsyncTest
: public testing::Test
{
38 virtual void SetUp() {
39 // Make the main thread not to allow IO.
40 base::ThreadRestrictions::SetIOAllowed(false);
42 // Start the D-Bus thread.
43 dbus_thread_
.reset(new base::Thread("D-Bus Thread"));
44 base::Thread::Options thread_options
;
45 thread_options
.message_loop_type
= MessageLoop::TYPE_IO
;
46 ASSERT_TRUE(dbus_thread_
->StartWithOptions(thread_options
));
48 // Start the test service, using the D-Bus thread.
49 dbus::TestService::Options options
;
50 options
.dbus_thread_message_loop_proxy
= dbus_thread_
->message_loop_proxy();
51 test_service_
.reset(new dbus::TestService(options
));
52 ASSERT_TRUE(test_service_
->StartService());
53 ASSERT_TRUE(test_service_
->WaitUntilServiceIsStarted());
54 ASSERT_TRUE(test_service_
->HasDBusThread());
56 // Create the client, using the D-Bus thread.
57 dbus::Bus::Options bus_options
;
58 bus_options
.bus_type
= dbus::Bus::SESSION
;
59 bus_options
.connection_type
= dbus::Bus::PRIVATE
;
60 bus_options
.dbus_thread_message_loop_proxy
=
61 dbus_thread_
->message_loop_proxy();
62 bus_
= new dbus::Bus(bus_options
);
63 object_proxy_
= bus_
->GetObjectProxy(
64 "org.chromium.TestService",
65 dbus::ObjectPath("/org/chromium/TestObject"));
66 ASSERT_TRUE(bus_
->HasDBusThread());
68 // Connect to the "Test" signal of "org.chromium.TestInterface" from
70 object_proxy_
->ConnectToSignal(
71 "org.chromium.TestInterface",
73 base::Bind(&EndToEndAsyncTest::OnTestSignal
,
74 base::Unretained(this)),
75 base::Bind(&EndToEndAsyncTest::OnConnected
,
76 base::Unretained(this)));
77 // Wait until the object proxy is connected to the signal.
80 // Connect to the "Test2" signal of "org.chromium.TestInterface" from
81 // the remote object. There was a bug where we were emitting error
82 // messages like "Requested to remove an unknown match rule: ..." at
83 // the shutdown of Bus when an object proxy is connected to more than
84 // one signal of the same interface. See crosbug.com/23382 for details.
85 object_proxy_
->ConnectToSignal(
86 "org.chromium.TestInterface",
88 base::Bind(&EndToEndAsyncTest::OnTest2Signal
,
89 base::Unretained(this)),
90 base::Bind(&EndToEndAsyncTest::OnConnected
,
91 base::Unretained(this)));
92 // Wait until the object proxy is connected to the signal.
95 // Create a second object proxy for the root object.
96 root_object_proxy_
= bus_
->GetObjectProxy(
97 "org.chromium.TestService",
98 dbus::ObjectPath("/"));
99 ASSERT_TRUE(bus_
->HasDBusThread());
101 // Connect to the "Test" signal of "org.chromium.TestInterface" from
102 // the root remote object too.
103 root_object_proxy_
->ConnectToSignal(
104 "org.chromium.TestInterface",
106 base::Bind(&EndToEndAsyncTest::OnRootTestSignal
,
107 base::Unretained(this)),
108 base::Bind(&EndToEndAsyncTest::OnConnected
,
109 base::Unretained(this)));
110 // Wait until the root object proxy is connected to the signal.
114 virtual void TearDown() {
115 bus_
->ShutdownOnDBusThreadAndBlock();
117 // Shut down the service.
118 test_service_
->ShutdownAndBlock();
120 // Reset to the default.
121 base::ThreadRestrictions::SetIOAllowed(true);
123 // Stopping a thread is considered an IO operation, so do this after
125 test_service_
->Stop();
129 // Replaces the bus with a broken one.
130 void SetUpBrokenBus() {
131 // Shut down the existing bus.
132 bus_
->ShutdownOnDBusThreadAndBlock();
134 // Create new bus with invalid address.
135 const char kInvalidAddress
[] = "";
136 dbus::Bus::Options bus_options
;
137 bus_options
.bus_type
= dbus::Bus::CUSTOM_ADDRESS
;
138 bus_options
.address
= kInvalidAddress
;
139 bus_options
.connection_type
= dbus::Bus::PRIVATE
;
140 bus_options
.dbus_thread_message_loop_proxy
=
141 dbus_thread_
->message_loop_proxy();
142 bus_
= new dbus::Bus(bus_options
);
143 ASSERT_TRUE(bus_
->HasDBusThread());
145 // Create new object proxy.
146 object_proxy_
= bus_
->GetObjectProxy(
147 "org.chromium.TestService",
148 dbus::ObjectPath("/org/chromium/TestObject"));
151 // Calls the method asynchronously. OnResponse() will be called once the
152 // response is received.
153 void CallMethod(dbus::MethodCall
* method_call
,
155 object_proxy_
->CallMethod(method_call
,
157 base::Bind(&EndToEndAsyncTest::OnResponse
,
158 base::Unretained(this)));
161 // Calls the method asynchronously. OnResponse() will be called once the
162 // response is received without error, otherwise OnError() will be called.
163 void CallMethodWithErrorCallback(dbus::MethodCall
* method_call
,
165 object_proxy_
->CallMethodWithErrorCallback(
168 base::Bind(&EndToEndAsyncTest::OnResponse
, base::Unretained(this)),
169 base::Bind(&EndToEndAsyncTest::OnError
, base::Unretained(this)));
172 // Wait for the give number of responses.
173 void WaitForResponses(size_t num_responses
) {
174 while (response_strings_
.size() < num_responses
) {
179 // Called when the response is received.
180 void OnResponse(dbus::Response
* response
) {
181 // |response| will be deleted on exit of the function. Copy the
182 // payload to |response_strings_|.
184 dbus::MessageReader
reader(response
);
185 std::string response_string
;
186 ASSERT_TRUE(reader
.PopString(&response_string
));
187 response_strings_
.push_back(response_string
);
189 response_strings_
.push_back("");
191 message_loop_
.Quit();
194 // Wait for the given number of errors.
195 void WaitForErrors(size_t num_errors
) {
196 while (error_names_
.size() < num_errors
) {
201 // Called when an error is received.
202 void OnError(dbus::ErrorResponse
* error
) {
203 // |error| will be deleted on exit of the function. Copy the payload to
206 ASSERT_NE("", error
->GetErrorName());
207 error_names_
.push_back(error
->GetErrorName());
209 error_names_
.push_back("");
211 message_loop_
.Quit();
214 // Called when the "Test" signal is received, in the main thread.
215 // Copy the string payload to |test_signal_string_|.
216 void OnTestSignal(dbus::Signal
* signal
) {
217 dbus::MessageReader
reader(signal
);
218 ASSERT_TRUE(reader
.PopString(&test_signal_string_
));
219 message_loop_
.Quit();
222 // Called when the "Test" signal is received, in the main thread, by
223 // the root object proxy. Copy the string payload to
224 // |root_test_signal_string_|.
225 void OnRootTestSignal(dbus::Signal
* signal
) {
226 dbus::MessageReader
reader(signal
);
227 ASSERT_TRUE(reader
.PopString(&root_test_signal_string_
));
228 message_loop_
.Quit();
231 // Called when the "Test2" signal is received, in the main thread.
232 void OnTest2Signal(dbus::Signal
* signal
) {
233 dbus::MessageReader
reader(signal
);
234 message_loop_
.Quit();
237 // Called when connected to the signal.
238 void OnConnected(const std::string
& interface_name
,
239 const std::string
& signal_name
,
241 ASSERT_TRUE(success
);
242 message_loop_
.Quit();
245 // Wait for the hey signal to be received.
246 void WaitForTestSignal() {
247 // OnTestSignal() will quit the message loop.
251 MessageLoop message_loop_
;
252 std::vector
<std::string
> response_strings_
;
253 std::vector
<std::string
> error_names_
;
254 scoped_ptr
<base::Thread
> dbus_thread_
;
255 scoped_refptr
<dbus::Bus
> bus_
;
256 dbus::ObjectProxy
* object_proxy_
;
257 dbus::ObjectProxy
* root_object_proxy_
;
258 scoped_ptr
<dbus::TestService
> test_service_
;
259 // Text message from "Test" signal.
260 std::string test_signal_string_
;
261 // Text message from "Test" signal delivered to root.
262 std::string root_test_signal_string_
;
265 TEST_F(EndToEndAsyncTest
, Echo
) {
266 const char* kHello
= "hello";
268 // Create the method call.
269 dbus::MethodCall
method_call("org.chromium.TestInterface", "Echo");
270 dbus::MessageWriter
writer(&method_call
);
271 writer
.AppendString(kHello
);
274 const int timeout_ms
= dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
;
275 CallMethod(&method_call
, timeout_ms
);
277 // Check the response.
279 EXPECT_EQ(kHello
, response_strings_
[0]);
282 TEST_F(EndToEndAsyncTest
, EchoWithErrorCallback
) {
283 const char* kHello
= "hello";
285 // Create the method call.
286 dbus::MethodCall
method_call("org.chromium.TestInterface", "Echo");
287 dbus::MessageWriter
writer(&method_call
);
288 writer
.AppendString(kHello
);
291 const int timeout_ms
= dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
;
292 CallMethodWithErrorCallback(&method_call
, timeout_ms
);
294 // Check the response.
296 EXPECT_EQ(kHello
, response_strings_
[0]);
297 EXPECT_TRUE(error_names_
.empty());
300 // Call Echo method three times.
301 TEST_F(EndToEndAsyncTest
, EchoThreeTimes
) {
302 const char* kMessages
[] = { "foo", "bar", "baz" };
304 for (size_t i
= 0; i
< arraysize(kMessages
); ++i
) {
305 // Create the method call.
306 dbus::MethodCall
method_call("org.chromium.TestInterface", "Echo");
307 dbus::MessageWriter
writer(&method_call
);
308 writer
.AppendString(kMessages
[i
]);
311 const int timeout_ms
= dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
;
312 CallMethod(&method_call
, timeout_ms
);
315 // Check the responses.
317 // Sort as the order of the returned messages is not deterministic.
318 std::sort(response_strings_
.begin(), response_strings_
.end());
319 EXPECT_EQ("bar", response_strings_
[0]);
320 EXPECT_EQ("baz", response_strings_
[1]);
321 EXPECT_EQ("foo", response_strings_
[2]);
324 TEST_F(EndToEndAsyncTest
, Echo_HugePayload
) {
325 const std::string
kHugePayload(kHugePayloadSize
, 'o');
327 // Create the method call with a huge payload.
328 dbus::MethodCall
method_call("org.chromium.TestInterface", "Echo");
329 dbus::MessageWriter
writer(&method_call
);
330 writer
.AppendString(kHugePayload
);
333 const int timeout_ms
= dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
;
334 CallMethod(&method_call
, timeout_ms
);
336 // This caused a DCHECK failure before. Ensure that the issue is fixed.
338 EXPECT_EQ(kHugePayload
, response_strings_
[0]);
341 TEST_F(EndToEndAsyncTest
, BrokenBus
) {
342 const char* kHello
= "hello";
344 // Set up a broken bus.
347 // Create the method call.
348 dbus::MethodCall
method_call("org.chromium.TestInterface", "Echo");
349 dbus::MessageWriter
writer(&method_call
);
350 writer
.AppendString(kHello
);
353 const int timeout_ms
= dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
;
354 CallMethod(&method_call
, timeout_ms
);
357 // Should fail because of the broken bus.
358 ASSERT_EQ("", response_strings_
[0]);
361 TEST_F(EndToEndAsyncTest
, BrokenBusWithErrorCallback
) {
362 const char* kHello
= "hello";
364 // Set up a broken bus.
367 // Create the method call.
368 dbus::MethodCall
method_call("org.chromium.TestInterface", "Echo");
369 dbus::MessageWriter
writer(&method_call
);
370 writer
.AppendString(kHello
);
373 const int timeout_ms
= dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
;
374 CallMethodWithErrorCallback(&method_call
, timeout_ms
);
377 // Should fail because of the broken bus.
378 ASSERT_TRUE(response_strings_
.empty());
379 ASSERT_EQ("", error_names_
[0]);
382 TEST_F(EndToEndAsyncTest
, Timeout
) {
383 const char* kHello
= "hello";
385 // Create the method call.
386 dbus::MethodCall
method_call("org.chromium.TestInterface", "SlowEcho");
387 dbus::MessageWriter
writer(&method_call
);
388 writer
.AppendString(kHello
);
390 // Call the method with timeout of 0ms.
391 const int timeout_ms
= 0;
392 CallMethod(&method_call
, timeout_ms
);
395 // Should fail because of timeout.
396 ASSERT_EQ("", response_strings_
[0]);
399 TEST_F(EndToEndAsyncTest
, TimeoutWithErrorCallback
) {
400 const char* kHello
= "hello";
402 // Create the method call.
403 dbus::MethodCall
method_call("org.chromium.TestInterface", "SlowEcho");
404 dbus::MessageWriter
writer(&method_call
);
405 writer
.AppendString(kHello
);
407 // Call the method with timeout of 0ms.
408 const int timeout_ms
= 0;
409 CallMethodWithErrorCallback(&method_call
, timeout_ms
);
412 // Should fail because of timeout.
413 ASSERT_TRUE(response_strings_
.empty());
414 ASSERT_EQ(DBUS_ERROR_NO_REPLY
, error_names_
[0]);
417 // Tests calling a method that sends its reply asynchronously.
418 TEST_F(EndToEndAsyncTest
, AsyncEcho
) {
419 const char* kHello
= "hello";
421 // Create the method call.
422 dbus::MethodCall
method_call("org.chromium.TestInterface", "AsyncEcho");
423 dbus::MessageWriter
writer(&method_call
);
424 writer
.AppendString(kHello
);
427 const int timeout_ms
= dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
;
428 CallMethod(&method_call
, timeout_ms
);
430 // Check the response.
432 EXPECT_EQ(kHello
, response_strings_
[0]);
435 TEST_F(EndToEndAsyncTest
, NonexistentMethod
) {
436 dbus::MethodCall
method_call("org.chromium.TestInterface", "Nonexistent");
438 const int timeout_ms
= dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
;
439 CallMethod(&method_call
, timeout_ms
);
442 // Should fail because the method is nonexistent.
443 ASSERT_EQ("", response_strings_
[0]);
446 TEST_F(EndToEndAsyncTest
, NonexistentMethodWithErrorCallback
) {
447 dbus::MethodCall
method_call("org.chromium.TestInterface", "Nonexistent");
449 const int timeout_ms
= dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
;
450 CallMethodWithErrorCallback(&method_call
, timeout_ms
);
453 // Should fail because the method is nonexistent.
454 ASSERT_TRUE(response_strings_
.empty());
455 ASSERT_EQ(DBUS_ERROR_UNKNOWN_METHOD
, error_names_
[0]);
458 TEST_F(EndToEndAsyncTest
, BrokenMethod
) {
459 dbus::MethodCall
method_call("org.chromium.TestInterface", "BrokenMethod");
461 const int timeout_ms
= dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
;
462 CallMethod(&method_call
, timeout_ms
);
465 // Should fail because the method is broken.
466 ASSERT_EQ("", response_strings_
[0]);
469 TEST_F(EndToEndAsyncTest
, BrokenMethodWithErrorCallback
) {
470 dbus::MethodCall
method_call("org.chromium.TestInterface", "BrokenMethod");
472 const int timeout_ms
= dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
;
473 CallMethodWithErrorCallback(&method_call
, timeout_ms
);
476 // Should fail because the method is broken.
477 ASSERT_TRUE(response_strings_
.empty());
478 ASSERT_EQ(DBUS_ERROR_FAILED
, error_names_
[0]);
481 TEST_F(EndToEndAsyncTest
, InvalidObjectPath
) {
482 // Trailing '/' is only allowed for the root path.
483 const dbus::ObjectPath
invalid_object_path("/org/chromium/TestObject/");
485 // Replace object proxy with new one.
486 object_proxy_
= bus_
->GetObjectProxy("org.chromium.TestService",
487 invalid_object_path
);
489 dbus::MethodCall
method_call("org.chromium.TestInterface", "Echo");
491 const int timeout_ms
= dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
;
492 CallMethodWithErrorCallback(&method_call
, timeout_ms
);
495 // Should fail because of the invalid path.
496 ASSERT_TRUE(response_strings_
.empty());
497 ASSERT_EQ("", error_names_
[0]);
500 TEST_F(EndToEndAsyncTest
, InvalidServiceName
) {
501 // Bus name cannot contain '/'.
502 const std::string invalid_service_name
= ":1/2";
504 // Replace object proxy with new one.
505 object_proxy_
= bus_
->GetObjectProxy(
506 invalid_service_name
, dbus::ObjectPath("org.chromium.TestObject"));
508 dbus::MethodCall
method_call("org.chromium.TestInterface", "Echo");
510 const int timeout_ms
= dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
;
511 CallMethodWithErrorCallback(&method_call
, timeout_ms
);
514 // Should fail because of the invalid bus name.
515 ASSERT_TRUE(response_strings_
.empty());
516 ASSERT_EQ("", error_names_
[0]);
519 TEST_F(EndToEndAsyncTest
, EmptyResponseCallback
) {
520 const char* kHello
= "hello";
522 // Create the method call.
523 dbus::MethodCall
method_call("org.chromium.TestInterface", "Echo");
524 dbus::MessageWriter
writer(&method_call
);
525 writer
.AppendString(kHello
);
527 // Call the method with an empty callback.
528 const int timeout_ms
= dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
;
529 object_proxy_
->CallMethod(&method_call
,
531 dbus::ObjectProxy::EmptyResponseCallback());
532 // Post a delayed task to quit the message loop.
533 message_loop_
.PostDelayedTask(FROM_HERE
,
534 MessageLoop::QuitClosure(),
535 TestTimeouts::tiny_timeout());
537 // We cannot tell if the empty callback is called, but at least we can
538 // check if the test does not crash.
541 TEST_F(EndToEndAsyncTest
, TestSignal
) {
542 const char kMessage
[] = "hello, world";
543 // Send the test signal from the exported object.
544 test_service_
->SendTestSignal(kMessage
);
545 // Receive the signal with the object proxy. The signal is handled in
546 // EndToEndAsyncTest::OnTestSignal() in the main thread.
548 ASSERT_EQ(kMessage
, test_signal_string_
);
551 TEST_F(EndToEndAsyncTest
, TestSignalFromRoot
) {
552 const char kMessage
[] = "hello, world";
553 // Object proxies are tied to a particular object path, if a signal
554 // arrives from a different object path like "/" the first object proxy
555 // |object_proxy_| should not handle it, and should leave it for the root
556 // object proxy |root_object_proxy_|.
557 test_service_
->SendTestSignalFromRoot(kMessage
);
559 // Verify the signal was not received by the specific proxy.
560 ASSERT_TRUE(test_signal_string_
.empty());
561 // Verify the string WAS received by the root proxy.
562 ASSERT_EQ(kMessage
, root_test_signal_string_
);
565 TEST_F(EndToEndAsyncTest
, TestHugeSignal
) {
566 const std::string
kHugeMessage(kHugePayloadSize
, 'o');
568 // Send the huge signal from the exported object.
569 test_service_
->SendTestSignal(kHugeMessage
);
570 // This caused a DCHECK failure before. Ensure that the issue is fixed.
572 ASSERT_EQ(kHugeMessage
, test_signal_string_
);
575 class SignalReplacementTest
: public EndToEndAsyncTest
{
577 SignalReplacementTest() {
580 virtual void SetUp() {
581 // Set up base class.
582 EndToEndAsyncTest::SetUp();
584 // Reconnect the root object proxy's signal handler to a new handler
585 // so that we can verify that a second call to ConnectSignal() delivers
586 // to our new handler and not the old.
587 object_proxy_
->ConnectToSignal(
588 "org.chromium.TestInterface",
590 base::Bind(&SignalReplacementTest::OnReplacementTestSignal
,
591 base::Unretained(this)),
592 base::Bind(&SignalReplacementTest::OnReplacementConnected
,
593 base::Unretained(this)));
594 // Wait until the object proxy is connected to the signal.
599 // Called when the "Test" signal is received, in the main thread.
600 // Copy the string payload to |replacement_test_signal_string_|.
601 void OnReplacementTestSignal(dbus::Signal
* signal
) {
602 dbus::MessageReader
reader(signal
);
603 ASSERT_TRUE(reader
.PopString(&replacement_test_signal_string_
));
604 message_loop_
.Quit();
607 // Called when connected to the signal.
608 void OnReplacementConnected(const std::string
& interface_name
,
609 const std::string
& signal_name
,
611 ASSERT_TRUE(success
);
612 message_loop_
.Quit();
615 // Text message from "Test" signal delivered to replacement handler.
616 std::string replacement_test_signal_string_
;
619 TEST_F(SignalReplacementTest
, TestSignalReplacement
) {
620 const char kMessage
[] = "hello, world";
621 // Send the test signal from the exported object.
622 test_service_
->SendTestSignal(kMessage
);
623 // Receive the signal with the object proxy.
625 // Verify the string WAS NOT received by the original handler.
626 ASSERT_TRUE(test_signal_string_
.empty());
627 // Verify the signal WAS received by the replacement handler.
628 ASSERT_EQ(kMessage
, replacement_test_signal_string_
);