[Telemetry] Add interaction_record field to Value
[chromium-blink-merge.git] / dbus / end_to_end_async_unittest.cc
blob884683019e3ce4bcaf32c4ce59d142d02ee4a8fe
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/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"
17 #include "dbus/bus.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"
24 namespace dbus {
26 namespace {
28 // See comments in ObjectProxy::RunResponseCallback() for why the number was
29 // chosen.
30 const int kHugePayloadSize = 64 << 20; // 64 MB
32 } // namespace
34 // The end-to-end test exercises the asynchronous APIs in ObjectProxy and
35 // ExportedObject.
36 class EndToEndAsyncTest : public testing::Test {
37 public:
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_->message_loop_proxy();
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_->message_loop_proxy();
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
68 // the remote object.
69 object_proxy_->ConnectToSignal(
70 "org.chromium.TestInterface",
71 "Test",
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());
78 run_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 run_loop_.reset(new base::RunLoop());
94 run_loop_->Run();
96 // Create a second object proxy for the root object.
97 root_object_proxy_ = bus_->GetObjectProxy("org.chromium.TestService",
98 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 run_loop_.reset(new base::RunLoop());
112 run_loop_->Run();
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
125 // allowing IO.
126 test_service_->Stop();
129 protected:
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_->message_loop_proxy();
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,
154 int timeout_ms) {
155 object_proxy_->CallMethod(method_call,
156 timeout_ms,
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,
164 int timeout_ms) {
165 object_proxy_->CallMethodWithErrorCallback(
166 method_call,
167 timeout_ms,
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);
176 run_loop_->Run();
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_|.
184 if (response) {
185 MessageReader reader(response);
186 std::string response_string;
187 ASSERT_TRUE(reader.PopString(&response_string));
188 response_strings_.push_back(response_string);
189 } else {
190 response_strings_.push_back(std::string());
192 run_loop_->Quit();
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);
199 run_loop_->Run();
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
206 // |error_names_|.
207 if (error) {
208 ASSERT_NE("", error->GetErrorName());
209 error_names_.push_back(error->GetErrorName());
210 } else {
211 error_names_.push_back(std::string());
213 run_loop_->Quit();
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_));
221 run_loop_->Quit();
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_));
230 run_loop_->Quit();
233 // Called when the "Test2" signal is received, in the main thread.
234 void OnTest2Signal(Signal* signal) {
235 MessageReader reader(signal);
236 run_loop_->Quit();
239 // Called when connected to the signal.
240 void OnConnected(const std::string& interface_name,
241 const std::string& signal_name,
242 bool success) {
243 ASSERT_TRUE(success);
244 run_loop_->Quit();
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);
251 run_loop_->Run();
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);
277 // Call the method.
278 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
279 CallMethod(&method_call, timeout_ms);
281 // Check the response.
282 WaitForResponses(1);
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);
294 // Call the method.
295 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
296 CallMethodWithErrorCallback(&method_call, timeout_ms);
298 // Check the response.
299 WaitForResponses(1);
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]);
314 // Call the method.
315 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
316 CallMethod(&method_call, timeout_ms);
319 // Check the responses.
320 WaitForResponses(3);
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);
336 // Call the method.
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.
341 WaitForResponses(1);
342 EXPECT_EQ(kHugePayload, response_strings_[0]);
345 TEST_F(EndToEndAsyncTest, BrokenBus) {
346 const char* kHello = "hello";
348 // Set up a broken bus.
349 SetUpBrokenBus();
351 // Create the method call.
352 MethodCall method_call("org.chromium.TestInterface", "Echo");
353 MessageWriter writer(&method_call);
354 writer.AppendString(kHello);
356 // Call the method.
357 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
358 CallMethod(&method_call, timeout_ms);
359 WaitForResponses(1);
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.
369 SetUpBrokenBus();
371 // Create the method call.
372 MethodCall method_call("org.chromium.TestInterface", "Echo");
373 MessageWriter writer(&method_call);
374 writer.AppendString(kHello);
376 // Call the method.
377 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
378 CallMethodWithErrorCallback(&method_call, timeout_ms);
379 WaitForErrors(1);
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);
397 WaitForResponses(1);
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);
414 WaitForErrors(1);
416 // Should fail because of timeout.
417 ASSERT_TRUE(response_strings_.empty());
418 ASSERT_EQ(DBUS_ERROR_NO_REPLY, error_names_[0]);
421 // Tests calling a method that sends its reply asynchronously.
422 TEST_F(EndToEndAsyncTest, AsyncEcho) {
423 const char* kHello = "hello";
425 // Create the method call.
426 MethodCall method_call("org.chromium.TestInterface", "AsyncEcho");
427 MessageWriter writer(&method_call);
428 writer.AppendString(kHello);
430 // Call the method.
431 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
432 CallMethod(&method_call, timeout_ms);
434 // Check the response.
435 WaitForResponses(1);
436 EXPECT_EQ(kHello, response_strings_[0]);
439 TEST_F(EndToEndAsyncTest, NonexistentMethod) {
440 MethodCall method_call("org.chromium.TestInterface", "Nonexistent");
442 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
443 CallMethod(&method_call, timeout_ms);
444 WaitForResponses(1);
446 // Should fail because the method is nonexistent.
447 ASSERT_EQ("", response_strings_[0]);
450 TEST_F(EndToEndAsyncTest, NonexistentMethodWithErrorCallback) {
451 MethodCall method_call("org.chromium.TestInterface", "Nonexistent");
453 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
454 CallMethodWithErrorCallback(&method_call, timeout_ms);
455 WaitForErrors(1);
457 // Should fail because the method is nonexistent.
458 ASSERT_TRUE(response_strings_.empty());
459 ASSERT_EQ(DBUS_ERROR_UNKNOWN_METHOD, error_names_[0]);
462 TEST_F(EndToEndAsyncTest, BrokenMethod) {
463 MethodCall method_call("org.chromium.TestInterface", "BrokenMethod");
465 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
466 CallMethod(&method_call, timeout_ms);
467 WaitForResponses(1);
469 // Should fail because the method is broken.
470 ASSERT_EQ("", response_strings_[0]);
473 TEST_F(EndToEndAsyncTest, BrokenMethodWithErrorCallback) {
474 MethodCall method_call("org.chromium.TestInterface", "BrokenMethod");
476 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
477 CallMethodWithErrorCallback(&method_call, timeout_ms);
478 WaitForErrors(1);
480 // Should fail because the method is broken.
481 ASSERT_TRUE(response_strings_.empty());
482 ASSERT_EQ(DBUS_ERROR_FAILED, error_names_[0]);
485 TEST_F(EndToEndAsyncTest, InvalidObjectPath) {
486 // Trailing '/' is only allowed for the root path.
487 const ObjectPath invalid_object_path("/org/chromium/TestObject/");
489 // Replace object proxy with new one.
490 object_proxy_ = bus_->GetObjectProxy("org.chromium.TestService",
491 invalid_object_path);
493 MethodCall method_call("org.chromium.TestInterface", "Echo");
495 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
496 CallMethodWithErrorCallback(&method_call, timeout_ms);
497 WaitForErrors(1);
499 // Should fail because of the invalid path.
500 ASSERT_TRUE(response_strings_.empty());
501 ASSERT_EQ("", error_names_[0]);
504 TEST_F(EndToEndAsyncTest, InvalidServiceName) {
505 // Bus name cannot contain '/'.
506 const std::string invalid_service_name = ":1/2";
508 // Replace object proxy with new one.
509 object_proxy_ = bus_->GetObjectProxy(
510 invalid_service_name, ObjectPath("org.chromium.TestObject"));
512 MethodCall method_call("org.chromium.TestInterface", "Echo");
514 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
515 CallMethodWithErrorCallback(&method_call, timeout_ms);
516 WaitForErrors(1);
518 // Should fail because of the invalid bus name.
519 ASSERT_TRUE(response_strings_.empty());
520 ASSERT_EQ("", error_names_[0]);
523 TEST_F(EndToEndAsyncTest, EmptyResponseCallback) {
524 const char* kHello = "hello";
526 // Create the method call.
527 MethodCall method_call("org.chromium.TestInterface", "Echo");
528 MessageWriter writer(&method_call);
529 writer.AppendString(kHello);
531 // Call the method with an empty callback.
532 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
533 object_proxy_->CallMethod(&method_call,
534 timeout_ms,
535 ObjectProxy::EmptyResponseCallback());
536 // Post a delayed task to quit the message loop.
537 run_loop_.reset(new base::RunLoop);
538 message_loop_.PostDelayedTask(FROM_HERE,
539 run_loop_->QuitClosure(),
540 TestTimeouts::tiny_timeout());
541 run_loop_->Run();
542 // We cannot tell if the empty callback is called, but at least we can
543 // check if the test does not crash.
546 TEST_F(EndToEndAsyncTest, TestSignal) {
547 const char kMessage[] = "hello, world";
548 // Send the test signal from the exported object.
549 test_service_->SendTestSignal(kMessage);
550 // Receive the signal with the object proxy. The signal is handled in
551 // EndToEndAsyncTest::OnTestSignal() in the main thread.
552 WaitForTestSignal();
553 ASSERT_EQ(kMessage, test_signal_string_);
556 TEST_F(EndToEndAsyncTest, TestSignalFromRoot) {
557 const char kMessage[] = "hello, world";
558 // Object proxies are tied to a particular object path, if a signal
559 // arrives from a different object path like "/" the first object proxy
560 // |object_proxy_| should not handle it, and should leave it for the root
561 // object proxy |root_object_proxy_|.
562 test_service_->SendTestSignalFromRoot(kMessage);
563 WaitForTestSignal();
564 // Verify the signal was not received by the specific proxy.
565 ASSERT_TRUE(test_signal_string_.empty());
566 // Verify the string WAS received by the root proxy.
567 ASSERT_EQ(kMessage, root_test_signal_string_);
570 TEST_F(EndToEndAsyncTest, TestHugeSignal) {
571 const std::string kHugeMessage(kHugePayloadSize, 'o');
573 // Send the huge signal from the exported object.
574 test_service_->SendTestSignal(kHugeMessage);
575 // This caused a DCHECK failure before. Ensure that the issue is fixed.
576 WaitForTestSignal();
577 ASSERT_EQ(kHugeMessage, test_signal_string_);
580 class SignalMultipleHandlerTest : public EndToEndAsyncTest {
581 public:
582 SignalMultipleHandlerTest() {
585 void SetUp() override {
586 // Set up base class.
587 EndToEndAsyncTest::SetUp();
589 // Connect the root object proxy's signal handler to a new handler
590 // so that we can verify that a second call to ConnectSignal() delivers
591 // to both our new handler and the old.
592 object_proxy_->ConnectToSignal(
593 "org.chromium.TestInterface",
594 "Test",
595 base::Bind(&SignalMultipleHandlerTest::OnAdditionalTestSignal,
596 base::Unretained(this)),
597 base::Bind(&SignalMultipleHandlerTest::OnAdditionalConnected,
598 base::Unretained(this)));
599 // Wait until the object proxy is connected to the signal.
600 run_loop_.reset(new base::RunLoop);
601 run_loop_->Run();
604 protected:
605 // Called when the "Test" signal is received, in the main thread.
606 // Copy the string payload to |additional_test_signal_string_|.
607 void OnAdditionalTestSignal(Signal* signal) {
608 MessageReader reader(signal);
609 ASSERT_TRUE(reader.PopString(&additional_test_signal_string_));
610 run_loop_->Quit();
613 // Called when connected to the signal.
614 void OnAdditionalConnected(const std::string& interface_name,
615 const std::string& signal_name,
616 bool success) {
617 ASSERT_TRUE(success);
618 run_loop_->Quit();
621 // Text message from "Test" signal delivered to additional handler.
622 std::string additional_test_signal_string_;
625 TEST_F(SignalMultipleHandlerTest, TestMultipleHandlers) {
626 const char kMessage[] = "hello, world";
627 // Send the test signal from the exported object.
628 test_service_->SendTestSignal(kMessage);
629 // Receive the signal with the object proxy.
630 WaitForTestSignal();
631 // Verify the string WAS received by the original handler.
632 ASSERT_EQ(kMessage, test_signal_string_);
633 // Verify the signal WAS ALSO received by the additional handler.
634 ASSERT_EQ(kMessage, additional_test_signal_string_);
637 } // namespace dbus