I've added a warning to the top of Desktop Notifications which explains the changes
[chromium-blink-merge.git] / dbus / end_to_end_async_unittest.cc
blob6943494546fd27f309be5ad5d6f6cc013b93f464
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.
5 #include <algorithm>
6 #include <string>
7 #include <vector>
9 #include "base/bind.h"
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"
16 #include "dbus/bus.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"
23 namespace {
25 // See comments in ObjectProxy::RunResponseCallback() for why the number was
26 // chosen.
27 const int kHugePayloadSize = 64 << 20; // 64 MB
29 } // namespace
31 // The end-to-end test exercises the asynchronous APIs in ObjectProxy and
32 // ExportedObject.
33 class EndToEndAsyncTest : public testing::Test {
34 public:
35 EndToEndAsyncTest() : on_disconnected_call_count_(0) {}
37 virtual void SetUp() {
38 // Make the main thread not to allow IO.
39 base::ThreadRestrictions::SetIOAllowed(false);
41 // Start the D-Bus thread.
42 dbus_thread_.reset(new base::Thread("D-Bus Thread"));
43 base::Thread::Options thread_options;
44 thread_options.message_loop_type = MessageLoop::TYPE_IO;
45 ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options));
47 // Start the test service, using the D-Bus thread.
48 dbus::TestService::Options options;
49 options.dbus_task_runner = dbus_thread_->message_loop_proxy();
50 test_service_.reset(new dbus::TestService(options));
51 ASSERT_TRUE(test_service_->StartService());
52 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted());
53 ASSERT_TRUE(test_service_->HasDBusThread());
55 // Create the client, using the D-Bus thread.
56 dbus::Bus::Options bus_options;
57 bus_options.bus_type = dbus::Bus::SESSION;
58 bus_options.connection_type = dbus::Bus::PRIVATE;
59 bus_options.dbus_task_runner = dbus_thread_->message_loop_proxy();
60 bus_options.disconnected_callback =
61 base::Bind(&EndToEndAsyncTest::OnDisconnected, base::Unretained(this));
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
69 // the remote object.
70 object_proxy_->ConnectToSignal(
71 "org.chromium.TestInterface",
72 "Test",
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.
78 message_loop_.Run();
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",
87 "Test2",
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 message_loop_.Run();
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",
105 "Test",
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 message_loop_.Run();
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
124 // allowing IO.
125 test_service_->Stop();
128 protected:
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_task_runner = dbus_thread_->message_loop_proxy();
141 bus_ = new dbus::Bus(bus_options);
142 ASSERT_TRUE(bus_->HasDBusThread());
144 // Create new object proxy.
145 object_proxy_ = bus_->GetObjectProxy(
146 "org.chromium.TestService",
147 dbus::ObjectPath("/org/chromium/TestObject"));
150 // Calls the method asynchronously. OnResponse() will be called once the
151 // response is received.
152 void CallMethod(dbus::MethodCall* method_call,
153 int timeout_ms) {
154 object_proxy_->CallMethod(method_call,
155 timeout_ms,
156 base::Bind(&EndToEndAsyncTest::OnResponse,
157 base::Unretained(this)));
160 // Calls the method asynchronously. OnResponse() will be called once the
161 // response is received without error, otherwise OnError() will be called.
162 void CallMethodWithErrorCallback(dbus::MethodCall* method_call,
163 int timeout_ms) {
164 object_proxy_->CallMethodWithErrorCallback(
165 method_call,
166 timeout_ms,
167 base::Bind(&EndToEndAsyncTest::OnResponse, base::Unretained(this)),
168 base::Bind(&EndToEndAsyncTest::OnError, base::Unretained(this)));
171 // Wait for the give number of responses.
172 void WaitForResponses(size_t num_responses) {
173 while (response_strings_.size() < num_responses) {
174 message_loop_.Run();
178 // Called when the response is received.
179 void OnResponse(dbus::Response* response) {
180 // |response| will be deleted on exit of the function. Copy the
181 // payload to |response_strings_|.
182 if (response) {
183 dbus::MessageReader reader(response);
184 std::string response_string;
185 ASSERT_TRUE(reader.PopString(&response_string));
186 response_strings_.push_back(response_string);
187 } else {
188 response_strings_.push_back(std::string());
190 message_loop_.Quit();
193 // Wait for the given number of errors.
194 void WaitForErrors(size_t num_errors) {
195 while (error_names_.size() < num_errors) {
196 message_loop_.Run();
200 // Called when an error is received.
201 void OnError(dbus::ErrorResponse* error) {
202 // |error| will be deleted on exit of the function. Copy the payload to
203 // |error_names_|.
204 if (error) {
205 ASSERT_NE("", error->GetErrorName());
206 error_names_.push_back(error->GetErrorName());
207 } else {
208 error_names_.push_back(std::string());
210 message_loop_.Quit();
213 // Called when the "Test" signal is received, in the main thread.
214 // Copy the string payload to |test_signal_string_|.
215 void OnTestSignal(dbus::Signal* signal) {
216 dbus::MessageReader reader(signal);
217 ASSERT_TRUE(reader.PopString(&test_signal_string_));
218 message_loop_.Quit();
221 // Called when the "Test" signal is received, in the main thread, by
222 // the root object proxy. Copy the string payload to
223 // |root_test_signal_string_|.
224 void OnRootTestSignal(dbus::Signal* signal) {
225 dbus::MessageReader reader(signal);
226 ASSERT_TRUE(reader.PopString(&root_test_signal_string_));
227 message_loop_.Quit();
230 // Called when the "Test2" signal is received, in the main thread.
231 void OnTest2Signal(dbus::Signal* signal) {
232 dbus::MessageReader reader(signal);
233 message_loop_.Quit();
236 // Called when connected to the signal.
237 void OnConnected(const std::string& interface_name,
238 const std::string& signal_name,
239 bool success) {
240 ASSERT_TRUE(success);
241 message_loop_.Quit();
244 // Called when the connection with dbus-daemon is disconnected.
245 void OnDisconnected() {
246 message_loop_.Quit();
247 ++on_disconnected_call_count_;
250 // Wait for the hey signal to be received.
251 void WaitForTestSignal() {
252 // OnTestSignal() will quit the message loop.
253 message_loop_.Run();
256 MessageLoop message_loop_;
257 std::vector<std::string> response_strings_;
258 std::vector<std::string> error_names_;
259 scoped_ptr<base::Thread> dbus_thread_;
260 scoped_refptr<dbus::Bus> bus_;
261 dbus::ObjectProxy* object_proxy_;
262 dbus::ObjectProxy* root_object_proxy_;
263 scoped_ptr<dbus::TestService> test_service_;
264 // Text message from "Test" signal.
265 std::string test_signal_string_;
266 // Text message from "Test" signal delivered to root.
267 std::string root_test_signal_string_;
268 int on_disconnected_call_count_;
271 TEST_F(EndToEndAsyncTest, Echo) {
272 const char* kHello = "hello";
274 // Create the method call.
275 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
276 dbus::MessageWriter writer(&method_call);
277 writer.AppendString(kHello);
279 // Call the method.
280 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
281 CallMethod(&method_call, timeout_ms);
283 // Check the response.
284 WaitForResponses(1);
285 EXPECT_EQ(kHello, response_strings_[0]);
288 TEST_F(EndToEndAsyncTest, EchoWithErrorCallback) {
289 const char* kHello = "hello";
291 // Create the method call.
292 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
293 dbus::MessageWriter writer(&method_call);
294 writer.AppendString(kHello);
296 // Call the method.
297 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
298 CallMethodWithErrorCallback(&method_call, timeout_ms);
300 // Check the response.
301 WaitForResponses(1);
302 EXPECT_EQ(kHello, response_strings_[0]);
303 EXPECT_TRUE(error_names_.empty());
306 // Call Echo method three times.
307 TEST_F(EndToEndAsyncTest, EchoThreeTimes) {
308 const char* kMessages[] = { "foo", "bar", "baz" };
310 for (size_t i = 0; i < arraysize(kMessages); ++i) {
311 // Create the method call.
312 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
313 dbus::MessageWriter writer(&method_call);
314 writer.AppendString(kMessages[i]);
316 // Call the method.
317 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
318 CallMethod(&method_call, timeout_ms);
321 // Check the responses.
322 WaitForResponses(3);
323 // Sort as the order of the returned messages is not deterministic.
324 std::sort(response_strings_.begin(), response_strings_.end());
325 EXPECT_EQ("bar", response_strings_[0]);
326 EXPECT_EQ("baz", response_strings_[1]);
327 EXPECT_EQ("foo", response_strings_[2]);
330 TEST_F(EndToEndAsyncTest, Echo_HugePayload) {
331 const std::string kHugePayload(kHugePayloadSize, 'o');
333 // Create the method call with a huge payload.
334 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
335 dbus::MessageWriter writer(&method_call);
336 writer.AppendString(kHugePayload);
338 // Call the method.
339 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
340 CallMethod(&method_call, timeout_ms);
342 // This caused a DCHECK failure before. Ensure that the issue is fixed.
343 WaitForResponses(1);
344 EXPECT_EQ(kHugePayload, response_strings_[0]);
347 TEST_F(EndToEndAsyncTest, BrokenBus) {
348 const char* kHello = "hello";
350 // Set up a broken bus.
351 SetUpBrokenBus();
353 // Create the method call.
354 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
355 dbus::MessageWriter writer(&method_call);
356 writer.AppendString(kHello);
358 // Call the method.
359 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
360 CallMethod(&method_call, timeout_ms);
361 WaitForResponses(1);
363 // Should fail because of the broken bus.
364 ASSERT_EQ("", response_strings_[0]);
367 TEST_F(EndToEndAsyncTest, BrokenBusWithErrorCallback) {
368 const char* kHello = "hello";
370 // Set up a broken bus.
371 SetUpBrokenBus();
373 // Create the method call.
374 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
375 dbus::MessageWriter writer(&method_call);
376 writer.AppendString(kHello);
378 // Call the method.
379 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
380 CallMethodWithErrorCallback(&method_call, timeout_ms);
381 WaitForErrors(1);
383 // Should fail because of the broken bus.
384 ASSERT_TRUE(response_strings_.empty());
385 ASSERT_EQ("", error_names_[0]);
388 TEST_F(EndToEndAsyncTest, Timeout) {
389 const char* kHello = "hello";
391 // Create the method call.
392 dbus::MethodCall method_call("org.chromium.TestInterface", "SlowEcho");
393 dbus::MessageWriter writer(&method_call);
394 writer.AppendString(kHello);
396 // Call the method with timeout of 0ms.
397 const int timeout_ms = 0;
398 CallMethod(&method_call, timeout_ms);
399 WaitForResponses(1);
401 // Should fail because of timeout.
402 ASSERT_EQ("", response_strings_[0]);
405 TEST_F(EndToEndAsyncTest, TimeoutWithErrorCallback) {
406 const char* kHello = "hello";
408 // Create the method call.
409 dbus::MethodCall method_call("org.chromium.TestInterface", "SlowEcho");
410 dbus::MessageWriter writer(&method_call);
411 writer.AppendString(kHello);
413 // Call the method with timeout of 0ms.
414 const int timeout_ms = 0;
415 CallMethodWithErrorCallback(&method_call, timeout_ms);
416 WaitForErrors(1);
418 // Should fail because of timeout.
419 ASSERT_TRUE(response_strings_.empty());
420 ASSERT_EQ(DBUS_ERROR_NO_REPLY, error_names_[0]);
423 // Tests calling a method that sends its reply asynchronously.
424 TEST_F(EndToEndAsyncTest, AsyncEcho) {
425 const char* kHello = "hello";
427 // Create the method call.
428 dbus::MethodCall method_call("org.chromium.TestInterface", "AsyncEcho");
429 dbus::MessageWriter writer(&method_call);
430 writer.AppendString(kHello);
432 // Call the method.
433 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
434 CallMethod(&method_call, timeout_ms);
436 // Check the response.
437 WaitForResponses(1);
438 EXPECT_EQ(kHello, response_strings_[0]);
441 TEST_F(EndToEndAsyncTest, NonexistentMethod) {
442 dbus::MethodCall method_call("org.chromium.TestInterface", "Nonexistent");
444 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
445 CallMethod(&method_call, timeout_ms);
446 WaitForResponses(1);
448 // Should fail because the method is nonexistent.
449 ASSERT_EQ("", response_strings_[0]);
452 TEST_F(EndToEndAsyncTest, NonexistentMethodWithErrorCallback) {
453 dbus::MethodCall method_call("org.chromium.TestInterface", "Nonexistent");
455 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
456 CallMethodWithErrorCallback(&method_call, timeout_ms);
457 WaitForErrors(1);
459 // Should fail because the method is nonexistent.
460 ASSERT_TRUE(response_strings_.empty());
461 ASSERT_EQ(DBUS_ERROR_UNKNOWN_METHOD, error_names_[0]);
464 TEST_F(EndToEndAsyncTest, BrokenMethod) {
465 dbus::MethodCall method_call("org.chromium.TestInterface", "BrokenMethod");
467 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
468 CallMethod(&method_call, timeout_ms);
469 WaitForResponses(1);
471 // Should fail because the method is broken.
472 ASSERT_EQ("", response_strings_[0]);
475 TEST_F(EndToEndAsyncTest, BrokenMethodWithErrorCallback) {
476 dbus::MethodCall method_call("org.chromium.TestInterface", "BrokenMethod");
478 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
479 CallMethodWithErrorCallback(&method_call, timeout_ms);
480 WaitForErrors(1);
482 // Should fail because the method is broken.
483 ASSERT_TRUE(response_strings_.empty());
484 ASSERT_EQ(DBUS_ERROR_FAILED, error_names_[0]);
487 TEST_F(EndToEndAsyncTest, InvalidObjectPath) {
488 // Trailing '/' is only allowed for the root path.
489 const dbus::ObjectPath invalid_object_path("/org/chromium/TestObject/");
491 // Replace object proxy with new one.
492 object_proxy_ = bus_->GetObjectProxy("org.chromium.TestService",
493 invalid_object_path);
495 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
497 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
498 CallMethodWithErrorCallback(&method_call, timeout_ms);
499 WaitForErrors(1);
501 // Should fail because of the invalid path.
502 ASSERT_TRUE(response_strings_.empty());
503 ASSERT_EQ("", error_names_[0]);
506 TEST_F(EndToEndAsyncTest, InvalidServiceName) {
507 // Bus name cannot contain '/'.
508 const std::string invalid_service_name = ":1/2";
510 // Replace object proxy with new one.
511 object_proxy_ = bus_->GetObjectProxy(
512 invalid_service_name, dbus::ObjectPath("org.chromium.TestObject"));
514 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
516 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
517 CallMethodWithErrorCallback(&method_call, timeout_ms);
518 WaitForErrors(1);
520 // Should fail because of the invalid bus name.
521 ASSERT_TRUE(response_strings_.empty());
522 ASSERT_EQ("", error_names_[0]);
525 TEST_F(EndToEndAsyncTest, EmptyResponseCallback) {
526 const char* kHello = "hello";
528 // Create the method call.
529 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
530 dbus::MessageWriter writer(&method_call);
531 writer.AppendString(kHello);
533 // Call the method with an empty callback.
534 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
535 object_proxy_->CallMethod(&method_call,
536 timeout_ms,
537 dbus::ObjectProxy::EmptyResponseCallback());
538 // Post a delayed task to quit the message loop.
539 message_loop_.PostDelayedTask(FROM_HERE,
540 MessageLoop::QuitClosure(),
541 TestTimeouts::tiny_timeout());
542 message_loop_.Run();
543 // We cannot tell if the empty callback is called, but at least we can
544 // check if the test does not crash.
547 TEST_F(EndToEndAsyncTest, TestSignal) {
548 const char kMessage[] = "hello, world";
549 // Send the test signal from the exported object.
550 test_service_->SendTestSignal(kMessage);
551 // Receive the signal with the object proxy. The signal is handled in
552 // EndToEndAsyncTest::OnTestSignal() in the main thread.
553 WaitForTestSignal();
554 ASSERT_EQ(kMessage, test_signal_string_);
557 TEST_F(EndToEndAsyncTest, TestSignalFromRoot) {
558 const char kMessage[] = "hello, world";
559 // Object proxies are tied to a particular object path, if a signal
560 // arrives from a different object path like "/" the first object proxy
561 // |object_proxy_| should not handle it, and should leave it for the root
562 // object proxy |root_object_proxy_|.
563 test_service_->SendTestSignalFromRoot(kMessage);
564 WaitForTestSignal();
565 // Verify the signal was not received by the specific proxy.
566 ASSERT_TRUE(test_signal_string_.empty());
567 // Verify the string WAS received by the root proxy.
568 ASSERT_EQ(kMessage, root_test_signal_string_);
571 TEST_F(EndToEndAsyncTest, TestHugeSignal) {
572 const std::string kHugeMessage(kHugePayloadSize, 'o');
574 // Send the huge signal from the exported object.
575 test_service_->SendTestSignal(kHugeMessage);
576 // This caused a DCHECK failure before. Ensure that the issue is fixed.
577 WaitForTestSignal();
578 ASSERT_EQ(kHugeMessage, test_signal_string_);
581 TEST_F(EndToEndAsyncTest, DisconnectedSignal) {
582 bus_->PostTaskToDBusThread(FROM_HERE,
583 base::Bind(&dbus::Bus::ClosePrivateConnection,
584 base::Unretained(bus_.get())));
585 // OnDisconnected callback quits message loop.
586 message_loop_.Run();
587 EXPECT_EQ(1, on_disconnected_call_count_);
590 class SignalReplacementTest : public EndToEndAsyncTest {
591 public:
592 SignalReplacementTest() {
595 virtual void SetUp() {
596 // Set up base class.
597 EndToEndAsyncTest::SetUp();
599 // Reconnect the root object proxy's signal handler to a new handler
600 // so that we can verify that a second call to ConnectSignal() delivers
601 // to our new handler and not the old.
602 object_proxy_->ConnectToSignal(
603 "org.chromium.TestInterface",
604 "Test",
605 base::Bind(&SignalReplacementTest::OnReplacementTestSignal,
606 base::Unretained(this)),
607 base::Bind(&SignalReplacementTest::OnReplacementConnected,
608 base::Unretained(this)));
609 // Wait until the object proxy is connected to the signal.
610 message_loop_.Run();
613 protected:
614 // Called when the "Test" signal is received, in the main thread.
615 // Copy the string payload to |replacement_test_signal_string_|.
616 void OnReplacementTestSignal(dbus::Signal* signal) {
617 dbus::MessageReader reader(signal);
618 ASSERT_TRUE(reader.PopString(&replacement_test_signal_string_));
619 message_loop_.Quit();
622 // Called when connected to the signal.
623 void OnReplacementConnected(const std::string& interface_name,
624 const std::string& signal_name,
625 bool success) {
626 ASSERT_TRUE(success);
627 message_loop_.Quit();
630 // Text message from "Test" signal delivered to replacement handler.
631 std::string replacement_test_signal_string_;
634 TEST_F(SignalReplacementTest, TestSignalReplacement) {
635 const char kMessage[] = "hello, world";
636 // Send the test signal from the exported object.
637 test_service_->SendTestSignal(kMessage);
638 // Receive the signal with the object proxy.
639 WaitForTestSignal();
640 // Verify the string WAS NOT received by the original handler.
641 ASSERT_TRUE(test_signal_string_.empty());
642 // Verify the signal WAS received by the replacement handler.
643 ASSERT_EQ(kMessage, replacement_test_signal_string_);