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/message_loop.h"
12 #include "base/run_loop.h"
13 #include "base/stl_util.h"
14 #include "base/test/test_timeouts.h"
15 #include "base/threading/thread.h"
16 #include "base/threading/thread_restrictions.h"
18 #include "dbus/message.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"
28 // See comments in ObjectProxy::RunResponseCallback() for why the number was
30 const int kHugePayloadSize
= 64 << 20; // 64 MB
34 // The end-to-end test exercises the asynchronous APIs in ObjectProxy and
36 class EndToEndAsyncTest
: public testing::Test
{
38 void SetUp() override
{
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
= base::MessageLoop::TYPE_IO
;
46 ASSERT_TRUE(dbus_thread_
->StartWithOptions(thread_options
));
48 // Start the test service, using the D-Bus thread.
49 TestService::Options options
;
50 options
.dbus_task_runner
= dbus_thread_
->task_runner();
51 test_service_
.reset(new 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 Bus::Options bus_options
;
58 bus_options
.bus_type
= Bus::SESSION
;
59 bus_options
.connection_type
= Bus::PRIVATE
;
60 bus_options
.dbus_task_runner
= dbus_thread_
->task_runner();
61 bus_
= new Bus(bus_options
);
62 object_proxy_
= bus_
->GetObjectProxy(
63 "org.chromium.TestService",
64 ObjectPath("/org/chromium/TestObject"));
65 ASSERT_TRUE(bus_
->HasDBusThread());
67 // Connect to the "Test" signal of "org.chromium.TestInterface" from
69 object_proxy_
->ConnectToSignal(
70 "org.chromium.TestInterface",
72 base::Bind(&EndToEndAsyncTest::OnTestSignal
,
73 base::Unretained(this)),
74 base::Bind(&EndToEndAsyncTest::OnConnected
,
75 base::Unretained(this)));
76 // Wait until the object proxy is connected to the signal.
77 run_loop_
.reset(new base::RunLoop());
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.
93 run_loop_
.reset(new base::RunLoop());
96 // Create a second object proxy for the root object.
97 root_object_proxy_
= bus_
->GetObjectProxy("org.chromium.TestService",
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.
111 run_loop_
.reset(new base::RunLoop());
115 void TearDown() override
{
116 bus_
->ShutdownOnDBusThreadAndBlock();
118 // Shut down the service.
119 test_service_
->ShutdownAndBlock();
121 // Reset to the default.
122 base::ThreadRestrictions::SetIOAllowed(true);
124 // Stopping a thread is considered an IO operation, so do this after
126 test_service_
->Stop();
130 // Replaces the bus with a broken one.
131 void SetUpBrokenBus() {
132 // Shut down the existing bus.
133 bus_
->ShutdownOnDBusThreadAndBlock();
135 // Create new bus with invalid address.
136 const char kInvalidAddress
[] = "";
137 Bus::Options bus_options
;
138 bus_options
.bus_type
= Bus::CUSTOM_ADDRESS
;
139 bus_options
.address
= kInvalidAddress
;
140 bus_options
.connection_type
= Bus::PRIVATE
;
141 bus_options
.dbus_task_runner
= dbus_thread_
->task_runner();
142 bus_
= new Bus(bus_options
);
143 ASSERT_TRUE(bus_
->HasDBusThread());
145 // Create new object proxy.
146 object_proxy_
= bus_
->GetObjectProxy(
147 "org.chromium.TestService",
148 ObjectPath("/org/chromium/TestObject"));
151 // Calls the method asynchronously. OnResponse() will be called once the
152 // response is received.
153 void CallMethod(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(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
) {
175 run_loop_
.reset(new base::RunLoop
);
180 // Called when the response is received.
181 void OnResponse(Response
* response
) {
182 // |response| will be deleted on exit of the function. Copy the
183 // payload to |response_strings_|.
185 MessageReader
reader(response
);
186 std::string response_string
;
187 ASSERT_TRUE(reader
.PopString(&response_string
));
188 response_strings_
.push_back(response_string
);
190 response_strings_
.push_back(std::string());
195 // Wait for the given number of errors.
196 void WaitForErrors(size_t num_errors
) {
197 while (error_names_
.size() < num_errors
) {
198 run_loop_
.reset(new base::RunLoop
);
203 // Called when an error is received.
204 void OnError(ErrorResponse
* error
) {
205 // |error| will be deleted on exit of the function. Copy the payload to
208 ASSERT_NE("", error
->GetErrorName());
209 error_names_
.push_back(error
->GetErrorName());
211 error_names_
.push_back(std::string());
216 // Called when the "Test" signal is received, in the main thread.
217 // Copy the string payload to |test_signal_string_|.
218 void OnTestSignal(Signal
* signal
) {
219 MessageReader
reader(signal
);
220 ASSERT_TRUE(reader
.PopString(&test_signal_string_
));
224 // Called when the "Test" signal is received, in the main thread, by
225 // the root object proxy. Copy the string payload to
226 // |root_test_signal_string_|.
227 void OnRootTestSignal(Signal
* signal
) {
228 MessageReader
reader(signal
);
229 ASSERT_TRUE(reader
.PopString(&root_test_signal_string_
));
233 // Called when the "Test2" signal is received, in the main thread.
234 void OnTest2Signal(Signal
* signal
) {
235 MessageReader
reader(signal
);
239 // Called when connected to the signal.
240 void OnConnected(const std::string
& interface_name
,
241 const std::string
& signal_name
,
243 ASSERT_TRUE(success
);
247 // Wait for the hey signal to be received.
248 void WaitForTestSignal() {
249 // OnTestSignal() will quit the message loop.
250 run_loop_
.reset(new base::RunLoop
);
254 base::MessageLoop message_loop_
;
255 scoped_ptr
<base::RunLoop
> run_loop_
;
256 std::vector
<std::string
> response_strings_
;
257 std::vector
<std::string
> error_names_
;
258 scoped_ptr
<base::Thread
> dbus_thread_
;
259 scoped_refptr
<Bus
> bus_
;
260 ObjectProxy
* object_proxy_
;
261 ObjectProxy
* root_object_proxy_
;
262 scoped_ptr
<TestService
> test_service_
;
263 // Text message from "Test" signal.
264 std::string test_signal_string_
;
265 // Text message from "Test" signal delivered to root.
266 std::string root_test_signal_string_
;
269 TEST_F(EndToEndAsyncTest
, Echo
) {
270 const char* kHello
= "hello";
272 // Create the method call.
273 MethodCall
method_call("org.chromium.TestInterface", "Echo");
274 MessageWriter
writer(&method_call
);
275 writer
.AppendString(kHello
);
278 const int timeout_ms
= ObjectProxy::TIMEOUT_USE_DEFAULT
;
279 CallMethod(&method_call
, timeout_ms
);
281 // Check the response.
283 EXPECT_EQ(kHello
, response_strings_
[0]);
286 TEST_F(EndToEndAsyncTest
, EchoWithErrorCallback
) {
287 const char* kHello
= "hello";
289 // Create the method call.
290 MethodCall
method_call("org.chromium.TestInterface", "Echo");
291 MessageWriter
writer(&method_call
);
292 writer
.AppendString(kHello
);
295 const int timeout_ms
= ObjectProxy::TIMEOUT_USE_DEFAULT
;
296 CallMethodWithErrorCallback(&method_call
, timeout_ms
);
298 // Check the response.
300 EXPECT_EQ(kHello
, response_strings_
[0]);
301 EXPECT_TRUE(error_names_
.empty());
304 // Call Echo method three times.
305 TEST_F(EndToEndAsyncTest
, EchoThreeTimes
) {
306 const char* kMessages
[] = { "foo", "bar", "baz" };
308 for (size_t i
= 0; i
< arraysize(kMessages
); ++i
) {
309 // Create the method call.
310 MethodCall
method_call("org.chromium.TestInterface", "Echo");
311 MessageWriter
writer(&method_call
);
312 writer
.AppendString(kMessages
[i
]);
315 const int timeout_ms
= ObjectProxy::TIMEOUT_USE_DEFAULT
;
316 CallMethod(&method_call
, timeout_ms
);
319 // Check the responses.
321 // Sort as the order of the returned messages is not deterministic.
322 std::sort(response_strings_
.begin(), response_strings_
.end());
323 EXPECT_EQ("bar", response_strings_
[0]);
324 EXPECT_EQ("baz", response_strings_
[1]);
325 EXPECT_EQ("foo", response_strings_
[2]);
328 TEST_F(EndToEndAsyncTest
, Echo_HugePayload
) {
329 const std::string
kHugePayload(kHugePayloadSize
, 'o');
331 // Create the method call with a huge payload.
332 MethodCall
method_call("org.chromium.TestInterface", "Echo");
333 MessageWriter
writer(&method_call
);
334 writer
.AppendString(kHugePayload
);
337 const int timeout_ms
= ObjectProxy::TIMEOUT_USE_DEFAULT
;
338 CallMethod(&method_call
, timeout_ms
);
340 // This caused a DCHECK failure before. Ensure that the issue is fixed.
342 EXPECT_EQ(kHugePayload
, response_strings_
[0]);
345 TEST_F(EndToEndAsyncTest
, BrokenBus
) {
346 const char* kHello
= "hello";
348 // Set up a broken bus.
351 // Create the method call.
352 MethodCall
method_call("org.chromium.TestInterface", "Echo");
353 MessageWriter
writer(&method_call
);
354 writer
.AppendString(kHello
);
357 const int timeout_ms
= ObjectProxy::TIMEOUT_USE_DEFAULT
;
358 CallMethod(&method_call
, timeout_ms
);
361 // Should fail because of the broken bus.
362 ASSERT_EQ("", response_strings_
[0]);
365 TEST_F(EndToEndAsyncTest
, BrokenBusWithErrorCallback
) {
366 const char* kHello
= "hello";
368 // Set up a broken bus.
371 // Create the method call.
372 MethodCall
method_call("org.chromium.TestInterface", "Echo");
373 MessageWriter
writer(&method_call
);
374 writer
.AppendString(kHello
);
377 const int timeout_ms
= ObjectProxy::TIMEOUT_USE_DEFAULT
;
378 CallMethodWithErrorCallback(&method_call
, timeout_ms
);
381 // Should fail because of the broken bus.
382 ASSERT_TRUE(response_strings_
.empty());
383 ASSERT_EQ("", error_names_
[0]);
386 TEST_F(EndToEndAsyncTest
, Timeout
) {
387 const char* kHello
= "hello";
389 // Create the method call.
390 MethodCall
method_call("org.chromium.TestInterface", "SlowEcho");
391 MessageWriter
writer(&method_call
);
392 writer
.AppendString(kHello
);
394 // Call the method with timeout of 0ms.
395 const int timeout_ms
= 0;
396 CallMethod(&method_call
, timeout_ms
);
399 // Should fail because of timeout.
400 ASSERT_EQ("", response_strings_
[0]);
403 TEST_F(EndToEndAsyncTest
, TimeoutWithErrorCallback
) {
404 const char* kHello
= "hello";
406 // Create the method call.
407 MethodCall
method_call("org.chromium.TestInterface", "SlowEcho");
408 MessageWriter
writer(&method_call
);
409 writer
.AppendString(kHello
);
411 // Call the method with timeout of 0ms.
412 const int timeout_ms
= 0;
413 CallMethodWithErrorCallback(&method_call
, timeout_ms
);
416 // Should fail because of timeout.
417 ASSERT_TRUE(response_strings_
.empty());
418 ASSERT_EQ(DBUS_ERROR_NO_REPLY
, error_names_
[0]);
421 TEST_F(EndToEndAsyncTest
, CancelPendingCalls
) {
422 const char* kHello
= "hello";
424 // Create the method call.
425 MethodCall
method_call("org.chromium.TestInterface", "Echo");
426 MessageWriter
writer(&method_call
);
427 writer
.AppendString(kHello
);
430 const int timeout_ms
= ObjectProxy::TIMEOUT_USE_DEFAULT
;
431 CallMethod(&method_call
, timeout_ms
);
433 // Remove the object proxy before receiving the result.
434 // This results in cancelling the pending method call.
435 bus_
->RemoveObjectProxy("org.chromium.TestService",
436 ObjectPath("/org/chromium/TestObject"),
437 base::Bind(&base::DoNothing
));
439 // We shouldn't receive any responses. Wait for a while just to make sure.
440 run_loop_
.reset(new base::RunLoop
);
441 message_loop_
.PostDelayedTask(FROM_HERE
,
442 run_loop_
->QuitClosure(),
443 TestTimeouts::tiny_timeout());
445 EXPECT_TRUE(response_strings_
.empty());
448 // Tests calling a method that sends its reply asynchronously.
449 TEST_F(EndToEndAsyncTest
, AsyncEcho
) {
450 const char* kHello
= "hello";
452 // Create the method call.
453 MethodCall
method_call("org.chromium.TestInterface", "AsyncEcho");
454 MessageWriter
writer(&method_call
);
455 writer
.AppendString(kHello
);
458 const int timeout_ms
= ObjectProxy::TIMEOUT_USE_DEFAULT
;
459 CallMethod(&method_call
, timeout_ms
);
461 // Check the response.
463 EXPECT_EQ(kHello
, response_strings_
[0]);
466 TEST_F(EndToEndAsyncTest
, NonexistentMethod
) {
467 MethodCall
method_call("org.chromium.TestInterface", "Nonexistent");
469 const int timeout_ms
= ObjectProxy::TIMEOUT_USE_DEFAULT
;
470 CallMethod(&method_call
, timeout_ms
);
473 // Should fail because the method is nonexistent.
474 ASSERT_EQ("", response_strings_
[0]);
477 TEST_F(EndToEndAsyncTest
, NonexistentMethodWithErrorCallback
) {
478 MethodCall
method_call("org.chromium.TestInterface", "Nonexistent");
480 const int timeout_ms
= ObjectProxy::TIMEOUT_USE_DEFAULT
;
481 CallMethodWithErrorCallback(&method_call
, timeout_ms
);
484 // Should fail because the method is nonexistent.
485 ASSERT_TRUE(response_strings_
.empty());
486 ASSERT_EQ(DBUS_ERROR_UNKNOWN_METHOD
, error_names_
[0]);
489 TEST_F(EndToEndAsyncTest
, BrokenMethod
) {
490 MethodCall
method_call("org.chromium.TestInterface", "BrokenMethod");
492 const int timeout_ms
= ObjectProxy::TIMEOUT_USE_DEFAULT
;
493 CallMethod(&method_call
, timeout_ms
);
496 // Should fail because the method is broken.
497 ASSERT_EQ("", response_strings_
[0]);
500 TEST_F(EndToEndAsyncTest
, BrokenMethodWithErrorCallback
) {
501 MethodCall
method_call("org.chromium.TestInterface", "BrokenMethod");
503 const int timeout_ms
= ObjectProxy::TIMEOUT_USE_DEFAULT
;
504 CallMethodWithErrorCallback(&method_call
, timeout_ms
);
507 // Should fail because the method is broken.
508 ASSERT_TRUE(response_strings_
.empty());
509 ASSERT_EQ(DBUS_ERROR_FAILED
, error_names_
[0]);
512 TEST_F(EndToEndAsyncTest
, InvalidObjectPath
) {
513 // Trailing '/' is only allowed for the root path.
514 const ObjectPath
invalid_object_path("/org/chromium/TestObject/");
516 // Replace object proxy with new one.
517 object_proxy_
= bus_
->GetObjectProxy("org.chromium.TestService",
518 invalid_object_path
);
520 MethodCall
method_call("org.chromium.TestInterface", "Echo");
522 const int timeout_ms
= ObjectProxy::TIMEOUT_USE_DEFAULT
;
523 CallMethodWithErrorCallback(&method_call
, timeout_ms
);
526 // Should fail because of the invalid path.
527 ASSERT_TRUE(response_strings_
.empty());
528 ASSERT_EQ("", error_names_
[0]);
531 TEST_F(EndToEndAsyncTest
, InvalidServiceName
) {
532 // Bus name cannot contain '/'.
533 const std::string invalid_service_name
= ":1/2";
535 // Replace object proxy with new one.
536 object_proxy_
= bus_
->GetObjectProxy(
537 invalid_service_name
, ObjectPath("org.chromium.TestObject"));
539 MethodCall
method_call("org.chromium.TestInterface", "Echo");
541 const int timeout_ms
= ObjectProxy::TIMEOUT_USE_DEFAULT
;
542 CallMethodWithErrorCallback(&method_call
, timeout_ms
);
545 // Should fail because of the invalid bus name.
546 ASSERT_TRUE(response_strings_
.empty());
547 ASSERT_EQ("", error_names_
[0]);
550 TEST_F(EndToEndAsyncTest
, EmptyResponseCallback
) {
551 const char* kHello
= "hello";
553 // Create the method call.
554 MethodCall
method_call("org.chromium.TestInterface", "Echo");
555 MessageWriter
writer(&method_call
);
556 writer
.AppendString(kHello
);
558 // Call the method with an empty callback.
559 const int timeout_ms
= ObjectProxy::TIMEOUT_USE_DEFAULT
;
560 object_proxy_
->CallMethod(&method_call
,
562 ObjectProxy::EmptyResponseCallback());
563 // Post a delayed task to quit the message loop.
564 run_loop_
.reset(new base::RunLoop
);
565 message_loop_
.PostDelayedTask(FROM_HERE
,
566 run_loop_
->QuitClosure(),
567 TestTimeouts::tiny_timeout());
569 // We cannot tell if the empty callback is called, but at least we can
570 // check if the test does not crash.
573 TEST_F(EndToEndAsyncTest
, TestSignal
) {
574 const char kMessage
[] = "hello, world";
575 // Send the test signal from the exported object.
576 test_service_
->SendTestSignal(kMessage
);
577 // Receive the signal with the object proxy. The signal is handled in
578 // EndToEndAsyncTest::OnTestSignal() in the main thread.
580 ASSERT_EQ(kMessage
, test_signal_string_
);
583 TEST_F(EndToEndAsyncTest
, TestSignalFromRoot
) {
584 const char kMessage
[] = "hello, world";
585 // Object proxies are tied to a particular object path, if a signal
586 // arrives from a different object path like "/" the first object proxy
587 // |object_proxy_| should not handle it, and should leave it for the root
588 // object proxy |root_object_proxy_|.
589 test_service_
->SendTestSignalFromRoot(kMessage
);
591 // Verify the signal was not received by the specific proxy.
592 ASSERT_TRUE(test_signal_string_
.empty());
593 // Verify the string WAS received by the root proxy.
594 ASSERT_EQ(kMessage
, root_test_signal_string_
);
597 TEST_F(EndToEndAsyncTest
, TestHugeSignal
) {
598 const std::string
kHugeMessage(kHugePayloadSize
, 'o');
600 // Send the huge signal from the exported object.
601 test_service_
->SendTestSignal(kHugeMessage
);
602 // This caused a DCHECK failure before. Ensure that the issue is fixed.
604 ASSERT_EQ(kHugeMessage
, test_signal_string_
);
607 class SignalMultipleHandlerTest
: public EndToEndAsyncTest
{
609 SignalMultipleHandlerTest() {
612 void SetUp() override
{
613 // Set up base class.
614 EndToEndAsyncTest::SetUp();
616 // Connect the root object proxy's signal handler to a new handler
617 // so that we can verify that a second call to ConnectSignal() delivers
618 // to both our new handler and the old.
619 object_proxy_
->ConnectToSignal(
620 "org.chromium.TestInterface",
622 base::Bind(&SignalMultipleHandlerTest::OnAdditionalTestSignal
,
623 base::Unretained(this)),
624 base::Bind(&SignalMultipleHandlerTest::OnAdditionalConnected
,
625 base::Unretained(this)));
626 // Wait until the object proxy is connected to the signal.
627 run_loop_
.reset(new base::RunLoop
);
632 // Called when the "Test" signal is received, in the main thread.
633 // Copy the string payload to |additional_test_signal_string_|.
634 void OnAdditionalTestSignal(Signal
* signal
) {
635 MessageReader
reader(signal
);
636 ASSERT_TRUE(reader
.PopString(&additional_test_signal_string_
));
640 // Called when connected to the signal.
641 void OnAdditionalConnected(const std::string
& interface_name
,
642 const std::string
& signal_name
,
644 ASSERT_TRUE(success
);
648 // Text message from "Test" signal delivered to additional handler.
649 std::string additional_test_signal_string_
;
652 TEST_F(SignalMultipleHandlerTest
, TestMultipleHandlers
) {
653 const char kMessage
[] = "hello, world";
654 // Send the test signal from the exported object.
655 test_service_
->SendTestSignal(kMessage
);
656 // Receive the signal with the object proxy.
658 // Verify the string WAS received by the original handler.
659 ASSERT_EQ(kMessage
, test_signal_string_
);
660 // Verify the signal WAS ALSO received by the additional handler.
661 ASSERT_EQ(kMessage
, additional_test_signal_string_
);