blob: 2de55c38f6e6f166974ba5daa3452aff52c6eb6a [file] [log] [blame]
[email protected]216ed0b2012-02-14 21:29:061// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]a51076112011-08-17 20:58:122// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
avi22437c692015-12-22 18:12:455#include <stddef.h>
6
[email protected]a51076112011-08-17 20:58:127#include <algorithm>
dcheng2a193282016-04-08 22:55:048#include <memory>
[email protected]a51076112011-08-17 20:58:129#include <string>
10#include <vector>
11
12#include "base/bind.h"
avi22437c692015-12-22 18:12:4513#include "base/macros.h"
[email protected]2a9ec0e2013-07-17 23:00:3014#include "base/message_loop/message_loop.h"
earthdok9562caf2014-09-03 10:32:3615#include "base/run_loop.h"
fdoray6ef45cf2016-08-25 15:36:3716#include "base/single_thread_task_runner.h"
[email protected]a51076112011-08-17 20:58:1217#include "base/stl_util.h"
[email protected]65e8e252011-11-11 08:36:2218#include "base/test/test_timeouts.h"
[email protected]a51076112011-08-17 20:58:1219#include "base/threading/thread.h"
20#include "base/threading/thread_restrictions.h"
21#include "dbus/bus.h"
22#include "dbus/message.h"
[email protected]216ed0b2012-02-14 21:29:0623#include "dbus/object_path.h"
[email protected]a51076112011-08-17 20:58:1224#include "dbus/object_proxy.h"
25#include "dbus/test_service.h"
26#include "testing/gtest/include/gtest/gtest.h"
27
[email protected]2a57ca642013-06-13 06:37:1928namespace dbus {
29
[email protected]3635d322012-06-02 02:53:2030namespace {
31
32// See comments in ObjectProxy::RunResponseCallback() for why the number was
33// chosen.
34const int kHugePayloadSize = 64 << 20; // 64 MB
35
36} // namespace
37
[email protected]829f0e4c2011-08-31 18:02:4338// The end-to-end test exercises the asynchronous APIs in ObjectProxy and
[email protected]a51076112011-08-17 20:58:1239// ExportedObject.
40class EndToEndAsyncTest : public testing::Test {
41 public:
dcheng3bb7119c2014-12-29 18:30:1742 void SetUp() override {
[email protected]a51076112011-08-17 20:58:1243 // Make the main thread not to allow IO.
44 base::ThreadRestrictions::SetIOAllowed(false);
45
[email protected]a51076112011-08-17 20:58:1246 // Start the D-Bus thread.
47 dbus_thread_.reset(new base::Thread("D-Bus Thread"));
48 base::Thread::Options thread_options;
[email protected]ff33b18e2013-05-01 16:10:3049 thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
[email protected]12f97662011-08-20 01:07:1750 ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options));
[email protected]a51076112011-08-17 20:58:1251
[email protected]12f97662011-08-20 01:07:1752 // Start the test service, using the D-Bus thread.
[email protected]2a57ca642013-06-13 06:37:1953 TestService::Options options;
skyostil8a033aa2015-06-17 15:46:0454 options.dbus_task_runner = dbus_thread_->task_runner();
[email protected]2a57ca642013-06-13 06:37:1955 test_service_.reset(new TestService(options));
[email protected]12f97662011-08-20 01:07:1756 ASSERT_TRUE(test_service_->StartService());
57 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted());
58 ASSERT_TRUE(test_service_->HasDBusThread());
59
60 // Create the client, using the D-Bus thread.
[email protected]2a57ca642013-06-13 06:37:1961 Bus::Options bus_options;
62 bus_options.bus_type = Bus::SESSION;
63 bus_options.connection_type = Bus::PRIVATE;
skyostil8a033aa2015-06-17 15:46:0464 bus_options.dbus_task_runner = dbus_thread_->task_runner();
[email protected]2a57ca642013-06-13 06:37:1965 bus_ = new Bus(bus_options);
[email protected]216ed0b2012-02-14 21:29:0666 object_proxy_ = bus_->GetObjectProxy(
hashimoto067d84f522016-01-05 08:48:0367 test_service_->service_name(),
[email protected]2a57ca642013-06-13 06:37:1968 ObjectPath("/org/chromium/TestObject"));
[email protected]12f97662011-08-20 01:07:1769 ASSERT_TRUE(bus_->HasDBusThread());
[email protected]3beaaa4e2011-08-23 07:29:2170
[email protected]0ad9ef82011-11-23 22:08:3871 // Connect to the "Test" signal of "org.chromium.TestInterface" from
72 // the remote object.
[email protected]3beaaa4e2011-08-23 07:29:2173 object_proxy_->ConnectToSignal(
74 "org.chromium.TestInterface",
75 "Test",
76 base::Bind(&EndToEndAsyncTest::OnTestSignal,
77 base::Unretained(this)),
78 base::Bind(&EndToEndAsyncTest::OnConnected,
79 base::Unretained(this)));
[email protected]4c985c62011-12-13 17:08:4280 // Wait until the object proxy is connected to the signal.
earthdok9562caf2014-09-03 10:32:3681 run_loop_.reset(new base::RunLoop());
82 run_loop_->Run();
[email protected]4c985c62011-12-13 17:08:4283
[email protected]0ad9ef82011-11-23 22:08:3884 // Connect to the "Test2" signal of "org.chromium.TestInterface" from
85 // the remote object. There was a bug where we were emitting error
86 // messages like "Requested to remove an unknown match rule: ..." at
87 // the shutdown of Bus when an object proxy is connected to more than
88 // one signal of the same interface. See crosbug.com/23382 for details.
89 object_proxy_->ConnectToSignal(
90 "org.chromium.TestInterface",
91 "Test2",
92 base::Bind(&EndToEndAsyncTest::OnTest2Signal,
93 base::Unretained(this)),
94 base::Bind(&EndToEndAsyncTest::OnConnected,
95 base::Unretained(this)));
[email protected]3beaaa4e2011-08-23 07:29:2196 // Wait until the object proxy is connected to the signal.
earthdok9562caf2014-09-03 10:32:3697 run_loop_.reset(new base::RunLoop());
98 run_loop_->Run();
[email protected]4d97f23372012-03-01 04:01:0599
100 // Create a second object proxy for the root object.
hashimoto067d84f522016-01-05 08:48:03101 root_object_proxy_ = bus_->GetObjectProxy(test_service_->service_name(),
[email protected]2a57ca642013-06-13 06:37:19102 ObjectPath("/"));
[email protected]4d97f23372012-03-01 04:01:05103 ASSERT_TRUE(bus_->HasDBusThread());
104
105 // Connect to the "Test" signal of "org.chromium.TestInterface" from
106 // the root remote object too.
107 root_object_proxy_->ConnectToSignal(
108 "org.chromium.TestInterface",
109 "Test",
110 base::Bind(&EndToEndAsyncTest::OnRootTestSignal,
111 base::Unretained(this)),
112 base::Bind(&EndToEndAsyncTest::OnConnected,
113 base::Unretained(this)));
114 // Wait until the root object proxy is connected to the signal.
earthdok9562caf2014-09-03 10:32:36115 run_loop_.reset(new base::RunLoop());
116 run_loop_->Run();
[email protected]a51076112011-08-17 20:58:12117 }
118
dcheng3bb7119c2014-12-29 18:30:17119 void TearDown() override {
[email protected]e20d39fe2011-09-02 06:56:23120 bus_->ShutdownOnDBusThreadAndBlock();
[email protected]a51076112011-08-17 20:58:12121
[email protected]12f97662011-08-20 01:07:17122 // Shut down the service.
[email protected]e20d39fe2011-09-02 06:56:23123 test_service_->ShutdownAndBlock();
[email protected]12f97662011-08-20 01:07:17124
[email protected]a51076112011-08-17 20:58:12125 // Reset to the default.
126 base::ThreadRestrictions::SetIOAllowed(true);
127
[email protected]829f0e4c2011-08-31 18:02:43128 // Stopping a thread is considered an IO operation, so do this after
[email protected]a51076112011-08-17 20:58:12129 // allowing IO.
130 test_service_->Stop();
131 }
132
133 protected:
[email protected]91fe7ea2012-04-20 03:18:27134 // Replaces the bus with a broken one.
135 void SetUpBrokenBus() {
136 // Shut down the existing bus.
137 bus_->ShutdownOnDBusThreadAndBlock();
138
139 // Create new bus with invalid address.
140 const char kInvalidAddress[] = "";
[email protected]2a57ca642013-06-13 06:37:19141 Bus::Options bus_options;
142 bus_options.bus_type = Bus::CUSTOM_ADDRESS;
[email protected]91fe7ea2012-04-20 03:18:27143 bus_options.address = kInvalidAddress;
[email protected]2a57ca642013-06-13 06:37:19144 bus_options.connection_type = Bus::PRIVATE;
skyostil8a033aa2015-06-17 15:46:04145 bus_options.dbus_task_runner = dbus_thread_->task_runner();
[email protected]2a57ca642013-06-13 06:37:19146 bus_ = new Bus(bus_options);
[email protected]91fe7ea2012-04-20 03:18:27147 ASSERT_TRUE(bus_->HasDBusThread());
148
149 // Create new object proxy.
150 object_proxy_ = bus_->GetObjectProxy(
hashimoto067d84f522016-01-05 08:48:03151 test_service_->service_name(),
[email protected]2a57ca642013-06-13 06:37:19152 ObjectPath("/org/chromium/TestObject"));
[email protected]91fe7ea2012-04-20 03:18:27153 }
154
[email protected]829f0e4c2011-08-31 18:02:43155 // Calls the method asynchronously. OnResponse() will be called once the
[email protected]a51076112011-08-17 20:58:12156 // response is received.
[email protected]2a57ca642013-06-13 06:37:19157 void CallMethod(MethodCall* method_call,
[email protected]a51076112011-08-17 20:58:12158 int timeout_ms) {
159 object_proxy_->CallMethod(method_call,
160 timeout_ms,
161 base::Bind(&EndToEndAsyncTest::OnResponse,
162 base::Unretained(this)));
163 }
164
[email protected]91fe7ea2012-04-20 03:18:27165 // Calls the method asynchronously. OnResponse() will be called once the
166 // response is received without error, otherwise OnError() will be called.
[email protected]2a57ca642013-06-13 06:37:19167 void CallMethodWithErrorCallback(MethodCall* method_call,
[email protected]91fe7ea2012-04-20 03:18:27168 int timeout_ms) {
169 object_proxy_->CallMethodWithErrorCallback(
170 method_call,
171 timeout_ms,
172 base::Bind(&EndToEndAsyncTest::OnResponse, base::Unretained(this)),
173 base::Bind(&EndToEndAsyncTest::OnError, base::Unretained(this)));
174 }
175
[email protected]a51076112011-08-17 20:58:12176 // Wait for the give number of responses.
177 void WaitForResponses(size_t num_responses) {
178 while (response_strings_.size() < num_responses) {
earthdok9562caf2014-09-03 10:32:36179 run_loop_.reset(new base::RunLoop);
180 run_loop_->Run();
[email protected]a51076112011-08-17 20:58:12181 }
182 }
183
184 // Called when the response is received.
[email protected]2a57ca642013-06-13 06:37:19185 void OnResponse(Response* response) {
[email protected]a51076112011-08-17 20:58:12186 // |response| will be deleted on exit of the function. Copy the
187 // payload to |response_strings_|.
188 if (response) {
[email protected]2a57ca642013-06-13 06:37:19189 MessageReader reader(response);
[email protected]a51076112011-08-17 20:58:12190 std::string response_string;
191 ASSERT_TRUE(reader.PopString(&response_string));
192 response_strings_.push_back(response_string);
193 } else {
[email protected]007b3f82013-04-09 08:46:45194 response_strings_.push_back(std::string());
[email protected]a51076112011-08-17 20:58:12195 }
earthdok9562caf2014-09-03 10:32:36196 run_loop_->Quit();
[email protected]a51076112011-08-17 20:58:12197 };
198
[email protected]91fe7ea2012-04-20 03:18:27199 // Wait for the given number of errors.
200 void WaitForErrors(size_t num_errors) {
201 while (error_names_.size() < num_errors) {
earthdok9562caf2014-09-03 10:32:36202 run_loop_.reset(new base::RunLoop);
203 run_loop_->Run();
[email protected]91fe7ea2012-04-20 03:18:27204 }
205 }
206
207 // Called when an error is received.
[email protected]2a57ca642013-06-13 06:37:19208 void OnError(ErrorResponse* error) {
[email protected]91fe7ea2012-04-20 03:18:27209 // |error| will be deleted on exit of the function. Copy the payload to
210 // |error_names_|.
211 if (error) {
212 ASSERT_NE("", error->GetErrorName());
213 error_names_.push_back(error->GetErrorName());
214 } else {
[email protected]007b3f82013-04-09 08:46:45215 error_names_.push_back(std::string());
[email protected]91fe7ea2012-04-20 03:18:27216 }
earthdok9562caf2014-09-03 10:32:36217 run_loop_->Quit();
[email protected]91fe7ea2012-04-20 03:18:27218 }
219
[email protected]3beaaa4e2011-08-23 07:29:21220 // Called when the "Test" signal is received, in the main thread.
221 // Copy the string payload to |test_signal_string_|.
[email protected]2a57ca642013-06-13 06:37:19222 void OnTestSignal(Signal* signal) {
223 MessageReader reader(signal);
[email protected]3beaaa4e2011-08-23 07:29:21224 ASSERT_TRUE(reader.PopString(&test_signal_string_));
earthdok9562caf2014-09-03 10:32:36225 run_loop_->Quit();
[email protected]3beaaa4e2011-08-23 07:29:21226 }
227
[email protected]4d97f23372012-03-01 04:01:05228 // Called when the "Test" signal is received, in the main thread, by
229 // the root object proxy. Copy the string payload to
230 // |root_test_signal_string_|.
[email protected]2a57ca642013-06-13 06:37:19231 void OnRootTestSignal(Signal* signal) {
232 MessageReader reader(signal);
[email protected]4d97f23372012-03-01 04:01:05233 ASSERT_TRUE(reader.PopString(&root_test_signal_string_));
earthdok9562caf2014-09-03 10:32:36234 run_loop_->Quit();
[email protected]4d97f23372012-03-01 04:01:05235 }
236
[email protected]0ad9ef82011-11-23 22:08:38237 // Called when the "Test2" signal is received, in the main thread.
[email protected]2a57ca642013-06-13 06:37:19238 void OnTest2Signal(Signal* signal) {
239 MessageReader reader(signal);
earthdok9562caf2014-09-03 10:32:36240 run_loop_->Quit();
[email protected]0ad9ef82011-11-23 22:08:38241 }
242
[email protected]3beaaa4e2011-08-23 07:29:21243 // Called when connected to the signal.
244 void OnConnected(const std::string& interface_name,
245 const std::string& signal_name,
246 bool success) {
247 ASSERT_TRUE(success);
earthdok9562caf2014-09-03 10:32:36248 run_loop_->Quit();
[email protected]3beaaa4e2011-08-23 07:29:21249 }
250
251 // Wait for the hey signal to be received.
252 void WaitForTestSignal() {
253 // OnTestSignal() will quit the message loop.
earthdok9562caf2014-09-03 10:32:36254 run_loop_.reset(new base::RunLoop);
255 run_loop_->Run();
[email protected]3beaaa4e2011-08-23 07:29:21256 }
257
[email protected]ff33b18e2013-05-01 16:10:30258 base::MessageLoop message_loop_;
dcheng2a193282016-04-08 22:55:04259 std::unique_ptr<base::RunLoop> run_loop_;
[email protected]a51076112011-08-17 20:58:12260 std::vector<std::string> response_strings_;
[email protected]91fe7ea2012-04-20 03:18:27261 std::vector<std::string> error_names_;
dcheng2a193282016-04-08 22:55:04262 std::unique_ptr<base::Thread> dbus_thread_;
[email protected]2a57ca642013-06-13 06:37:19263 scoped_refptr<Bus> bus_;
264 ObjectProxy* object_proxy_;
265 ObjectProxy* root_object_proxy_;
dcheng2a193282016-04-08 22:55:04266 std::unique_ptr<TestService> test_service_;
[email protected]3beaaa4e2011-08-23 07:29:21267 // Text message from "Test" signal.
268 std::string test_signal_string_;
[email protected]4d97f23372012-03-01 04:01:05269 // Text message from "Test" signal delivered to root.
270 std::string root_test_signal_string_;
[email protected]a51076112011-08-17 20:58:12271};
272
273TEST_F(EndToEndAsyncTest, Echo) {
274 const char* kHello = "hello";
275
276 // Create the method call.
[email protected]2a57ca642013-06-13 06:37:19277 MethodCall method_call("org.chromium.TestInterface", "Echo");
278 MessageWriter writer(&method_call);
[email protected]a51076112011-08-17 20:58:12279 writer.AppendString(kHello);
280
281 // Call the method.
[email protected]2a57ca642013-06-13 06:37:19282 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
[email protected]a51076112011-08-17 20:58:12283 CallMethod(&method_call, timeout_ms);
284
285 // Check the response.
286 WaitForResponses(1);
287 EXPECT_EQ(kHello, response_strings_[0]);
288}
289
[email protected]91fe7ea2012-04-20 03:18:27290TEST_F(EndToEndAsyncTest, EchoWithErrorCallback) {
291 const char* kHello = "hello";
292
293 // Create the method call.
[email protected]2a57ca642013-06-13 06:37:19294 MethodCall method_call("org.chromium.TestInterface", "Echo");
295 MessageWriter writer(&method_call);
[email protected]91fe7ea2012-04-20 03:18:27296 writer.AppendString(kHello);
297
298 // Call the method.
[email protected]2a57ca642013-06-13 06:37:19299 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
[email protected]91fe7ea2012-04-20 03:18:27300 CallMethodWithErrorCallback(&method_call, timeout_ms);
301
302 // Check the response.
303 WaitForResponses(1);
304 EXPECT_EQ(kHello, response_strings_[0]);
305 EXPECT_TRUE(error_names_.empty());
306}
307
[email protected]a51076112011-08-17 20:58:12308// Call Echo method three times.
309TEST_F(EndToEndAsyncTest, EchoThreeTimes) {
310 const char* kMessages[] = { "foo", "bar", "baz" };
311
312 for (size_t i = 0; i < arraysize(kMessages); ++i) {
313 // Create the method call.
[email protected]2a57ca642013-06-13 06:37:19314 MethodCall method_call("org.chromium.TestInterface", "Echo");
315 MessageWriter writer(&method_call);
[email protected]a51076112011-08-17 20:58:12316 writer.AppendString(kMessages[i]);
317
318 // Call the method.
[email protected]2a57ca642013-06-13 06:37:19319 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
[email protected]a51076112011-08-17 20:58:12320 CallMethod(&method_call, timeout_ms);
321 }
322
323 // Check the responses.
324 WaitForResponses(3);
325 // Sort as the order of the returned messages is not deterministic.
326 std::sort(response_strings_.begin(), response_strings_.end());
327 EXPECT_EQ("bar", response_strings_[0]);
328 EXPECT_EQ("baz", response_strings_[1]);
329 EXPECT_EQ("foo", response_strings_[2]);
330}
331
[email protected]3635d322012-06-02 02:53:20332TEST_F(EndToEndAsyncTest, Echo_HugePayload) {
333 const std::string kHugePayload(kHugePayloadSize, 'o');
334
335 // Create the method call with a huge payload.
[email protected]2a57ca642013-06-13 06:37:19336 MethodCall method_call("org.chromium.TestInterface", "Echo");
337 MessageWriter writer(&method_call);
[email protected]3635d322012-06-02 02:53:20338 writer.AppendString(kHugePayload);
339
340 // Call the method.
[email protected]2a57ca642013-06-13 06:37:19341 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
[email protected]3635d322012-06-02 02:53:20342 CallMethod(&method_call, timeout_ms);
343
344 // This caused a DCHECK failure before. Ensure that the issue is fixed.
345 WaitForResponses(1);
346 EXPECT_EQ(kHugePayload, response_strings_[0]);
347}
348
[email protected]91fe7ea2012-04-20 03:18:27349TEST_F(EndToEndAsyncTest, BrokenBus) {
350 const char* kHello = "hello";
351
352 // Set up a broken bus.
353 SetUpBrokenBus();
354
355 // Create the method call.
[email protected]2a57ca642013-06-13 06:37:19356 MethodCall method_call("org.chromium.TestInterface", "Echo");
357 MessageWriter writer(&method_call);
[email protected]91fe7ea2012-04-20 03:18:27358 writer.AppendString(kHello);
359
360 // Call the method.
[email protected]2a57ca642013-06-13 06:37:19361 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
[email protected]91fe7ea2012-04-20 03:18:27362 CallMethod(&method_call, timeout_ms);
363 WaitForResponses(1);
364
365 // Should fail because of the broken bus.
366 ASSERT_EQ("", response_strings_[0]);
367}
368
369TEST_F(EndToEndAsyncTest, BrokenBusWithErrorCallback) {
370 const char* kHello = "hello";
371
372 // Set up a broken bus.
373 SetUpBrokenBus();
374
375 // Create the method call.
[email protected]2a57ca642013-06-13 06:37:19376 MethodCall method_call("org.chromium.TestInterface", "Echo");
377 MessageWriter writer(&method_call);
[email protected]91fe7ea2012-04-20 03:18:27378 writer.AppendString(kHello);
379
380 // Call the method.
[email protected]2a57ca642013-06-13 06:37:19381 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
[email protected]91fe7ea2012-04-20 03:18:27382 CallMethodWithErrorCallback(&method_call, timeout_ms);
383 WaitForErrors(1);
384
385 // Should fail because of the broken bus.
386 ASSERT_TRUE(response_strings_.empty());
387 ASSERT_EQ("", error_names_[0]);
388}
389
[email protected]a51076112011-08-17 20:58:12390TEST_F(EndToEndAsyncTest, Timeout) {
391 const char* kHello = "hello";
392
393 // Create the method call.
[email protected]2a57ca642013-06-13 06:37:19394 MethodCall method_call("org.chromium.TestInterface", "SlowEcho");
395 MessageWriter writer(&method_call);
[email protected]a51076112011-08-17 20:58:12396 writer.AppendString(kHello);
397
[email protected]12f97662011-08-20 01:07:17398 // Call the method with timeout of 0ms.
399 const int timeout_ms = 0;
[email protected]a51076112011-08-17 20:58:12400 CallMethod(&method_call, timeout_ms);
401 WaitForResponses(1);
402
403 // Should fail because of timeout.
404 ASSERT_EQ("", response_strings_[0]);
405}
406
[email protected]91fe7ea2012-04-20 03:18:27407TEST_F(EndToEndAsyncTest, TimeoutWithErrorCallback) {
408 const char* kHello = "hello";
409
410 // Create the method call.
[email protected]2a57ca642013-06-13 06:37:19411 MethodCall method_call("org.chromium.TestInterface", "SlowEcho");
412 MessageWriter writer(&method_call);
[email protected]91fe7ea2012-04-20 03:18:27413 writer.AppendString(kHello);
414
415 // Call the method with timeout of 0ms.
416 const int timeout_ms = 0;
417 CallMethodWithErrorCallback(&method_call, timeout_ms);
418 WaitForErrors(1);
419
420 // Should fail because of timeout.
421 ASSERT_TRUE(response_strings_.empty());
422 ASSERT_EQ(DBUS_ERROR_NO_REPLY, error_names_[0]);
423}
424
hashimoto41ece522015-03-30 07:01:39425TEST_F(EndToEndAsyncTest, CancelPendingCalls) {
426 const char* kHello = "hello";
427
428 // Create the method call.
429 MethodCall method_call("org.chromium.TestInterface", "Echo");
430 MessageWriter writer(&method_call);
431 writer.AppendString(kHello);
432
433 // Call the method.
434 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
435 CallMethod(&method_call, timeout_ms);
436
437 // Remove the object proxy before receiving the result.
438 // This results in cancelling the pending method call.
hashimoto067d84f522016-01-05 08:48:03439 bus_->RemoveObjectProxy(test_service_->service_name(),
hashimoto41ece522015-03-30 07:01:39440 ObjectPath("/org/chromium/TestObject"),
441 base::Bind(&base::DoNothing));
442
443 // We shouldn't receive any responses. Wait for a while just to make sure.
444 run_loop_.reset(new base::RunLoop);
fdoray6ef45cf2016-08-25 15:36:37445 message_loop_.task_runner()->PostDelayedTask(
446 FROM_HERE, run_loop_->QuitClosure(), TestTimeouts::tiny_timeout());
hashimoto41ece522015-03-30 07:01:39447 run_loop_->Run();
448 EXPECT_TRUE(response_strings_.empty());
449}
450
[email protected]9aa74cc2011-11-30 04:57:42451// Tests calling a method that sends its reply asynchronously.
452TEST_F(EndToEndAsyncTest, AsyncEcho) {
453 const char* kHello = "hello";
454
455 // Create the method call.
[email protected]2a57ca642013-06-13 06:37:19456 MethodCall method_call("org.chromium.TestInterface", "AsyncEcho");
457 MessageWriter writer(&method_call);
[email protected]9aa74cc2011-11-30 04:57:42458 writer.AppendString(kHello);
459
460 // Call the method.
[email protected]2a57ca642013-06-13 06:37:19461 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
[email protected]9aa74cc2011-11-30 04:57:42462 CallMethod(&method_call, timeout_ms);
463
464 // Check the response.
465 WaitForResponses(1);
466 EXPECT_EQ(kHello, response_strings_[0]);
467}
468
[email protected]a51076112011-08-17 20:58:12469TEST_F(EndToEndAsyncTest, NonexistentMethod) {
[email protected]2a57ca642013-06-13 06:37:19470 MethodCall method_call("org.chromium.TestInterface", "Nonexistent");
[email protected]a51076112011-08-17 20:58:12471
[email protected]2a57ca642013-06-13 06:37:19472 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
[email protected]a51076112011-08-17 20:58:12473 CallMethod(&method_call, timeout_ms);
474 WaitForResponses(1);
475
476 // Should fail because the method is nonexistent.
477 ASSERT_EQ("", response_strings_[0]);
478}
479
[email protected]91fe7ea2012-04-20 03:18:27480TEST_F(EndToEndAsyncTest, NonexistentMethodWithErrorCallback) {
[email protected]2a57ca642013-06-13 06:37:19481 MethodCall method_call("org.chromium.TestInterface", "Nonexistent");
[email protected]91fe7ea2012-04-20 03:18:27482
[email protected]2a57ca642013-06-13 06:37:19483 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
[email protected]91fe7ea2012-04-20 03:18:27484 CallMethodWithErrorCallback(&method_call, timeout_ms);
485 WaitForErrors(1);
486
487 // Should fail because the method is nonexistent.
488 ASSERT_TRUE(response_strings_.empty());
489 ASSERT_EQ(DBUS_ERROR_UNKNOWN_METHOD, error_names_[0]);
490}
491
[email protected]a51076112011-08-17 20:58:12492TEST_F(EndToEndAsyncTest, BrokenMethod) {
[email protected]2a57ca642013-06-13 06:37:19493 MethodCall method_call("org.chromium.TestInterface", "BrokenMethod");
[email protected]a51076112011-08-17 20:58:12494
[email protected]2a57ca642013-06-13 06:37:19495 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
[email protected]a51076112011-08-17 20:58:12496 CallMethod(&method_call, timeout_ms);
497 WaitForResponses(1);
498
499 // Should fail because the method is broken.
500 ASSERT_EQ("", response_strings_[0]);
501}
[email protected]3beaaa4e2011-08-23 07:29:21502
[email protected]91fe7ea2012-04-20 03:18:27503TEST_F(EndToEndAsyncTest, BrokenMethodWithErrorCallback) {
[email protected]2a57ca642013-06-13 06:37:19504 MethodCall method_call("org.chromium.TestInterface", "BrokenMethod");
[email protected]91fe7ea2012-04-20 03:18:27505
[email protected]2a57ca642013-06-13 06:37:19506 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
[email protected]91fe7ea2012-04-20 03:18:27507 CallMethodWithErrorCallback(&method_call, timeout_ms);
508 WaitForErrors(1);
509
510 // Should fail because the method is broken.
511 ASSERT_TRUE(response_strings_.empty());
512 ASSERT_EQ(DBUS_ERROR_FAILED, error_names_[0]);
513}
514
[email protected]ca72ff22012-05-23 06:55:22515TEST_F(EndToEndAsyncTest, InvalidObjectPath) {
516 // Trailing '/' is only allowed for the root path.
[email protected]2a57ca642013-06-13 06:37:19517 const ObjectPath invalid_object_path("/org/chromium/TestObject/");
[email protected]ca72ff22012-05-23 06:55:22518
519 // Replace object proxy with new one.
hashimoto067d84f522016-01-05 08:48:03520 object_proxy_ = bus_->GetObjectProxy(test_service_->service_name(),
[email protected]ca72ff22012-05-23 06:55:22521 invalid_object_path);
522
[email protected]2a57ca642013-06-13 06:37:19523 MethodCall method_call("org.chromium.TestInterface", "Echo");
[email protected]ca72ff22012-05-23 06:55:22524
[email protected]2a57ca642013-06-13 06:37:19525 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
[email protected]ca72ff22012-05-23 06:55:22526 CallMethodWithErrorCallback(&method_call, timeout_ms);
527 WaitForErrors(1);
528
529 // Should fail because of the invalid path.
530 ASSERT_TRUE(response_strings_.empty());
531 ASSERT_EQ("", error_names_[0]);
532}
533
534TEST_F(EndToEndAsyncTest, InvalidServiceName) {
535 // Bus name cannot contain '/'.
536 const std::string invalid_service_name = ":1/2";
537
538 // Replace object proxy with new one.
539 object_proxy_ = bus_->GetObjectProxy(
[email protected]2a57ca642013-06-13 06:37:19540 invalid_service_name, ObjectPath("org.chromium.TestObject"));
[email protected]ca72ff22012-05-23 06:55:22541
[email protected]2a57ca642013-06-13 06:37:19542 MethodCall method_call("org.chromium.TestInterface", "Echo");
[email protected]ca72ff22012-05-23 06:55:22543
[email protected]2a57ca642013-06-13 06:37:19544 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
[email protected]ca72ff22012-05-23 06:55:22545 CallMethodWithErrorCallback(&method_call, timeout_ms);
546 WaitForErrors(1);
547
548 // Should fail because of the invalid bus name.
549 ASSERT_TRUE(response_strings_.empty());
550 ASSERT_EQ("", error_names_[0]);
551}
552
[email protected]65e8e252011-11-11 08:36:22553TEST_F(EndToEndAsyncTest, EmptyResponseCallback) {
554 const char* kHello = "hello";
555
556 // Create the method call.
[email protected]2a57ca642013-06-13 06:37:19557 MethodCall method_call("org.chromium.TestInterface", "Echo");
558 MessageWriter writer(&method_call);
[email protected]65e8e252011-11-11 08:36:22559 writer.AppendString(kHello);
560
561 // Call the method with an empty callback.
[email protected]2a57ca642013-06-13 06:37:19562 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
[email protected]65e8e252011-11-11 08:36:22563 object_proxy_->CallMethod(&method_call,
564 timeout_ms,
[email protected]2a57ca642013-06-13 06:37:19565 ObjectProxy::EmptyResponseCallback());
[email protected]65e8e252011-11-11 08:36:22566 // Post a delayed task to quit the message loop.
earthdok9562caf2014-09-03 10:32:36567 run_loop_.reset(new base::RunLoop);
fdoray6ef45cf2016-08-25 15:36:37568 message_loop_.task_runner()->PostDelayedTask(
569 FROM_HERE, run_loop_->QuitClosure(), TestTimeouts::tiny_timeout());
earthdok9562caf2014-09-03 10:32:36570 run_loop_->Run();
[email protected]65e8e252011-11-11 08:36:22571 // We cannot tell if the empty callback is called, but at least we can
572 // check if the test does not crash.
573}
574
[email protected]4c985c62011-12-13 17:08:42575TEST_F(EndToEndAsyncTest, TestSignal) {
[email protected]3beaaa4e2011-08-23 07:29:21576 const char kMessage[] = "hello, world";
577 // Send the test signal from the exported object.
578 test_service_->SendTestSignal(kMessage);
[email protected]829f0e4c2011-08-31 18:02:43579 // Receive the signal with the object proxy. The signal is handled in
[email protected]3beaaa4e2011-08-23 07:29:21580 // EndToEndAsyncTest::OnTestSignal() in the main thread.
581 WaitForTestSignal();
582 ASSERT_EQ(kMessage, test_signal_string_);
583}
[email protected]df159b22011-09-14 22:10:24584
[email protected]4c985c62011-12-13 17:08:42585TEST_F(EndToEndAsyncTest, TestSignalFromRoot) {
[email protected]df159b22011-09-14 22:10:24586 const char kMessage[] = "hello, world";
[email protected]4d97f23372012-03-01 04:01:05587 // Object proxies are tied to a particular object path, if a signal
588 // arrives from a different object path like "/" the first object proxy
589 // |object_proxy_| should not handle it, and should leave it for the root
590 // object proxy |root_object_proxy_|.
[email protected]df159b22011-09-14 22:10:24591 test_service_->SendTestSignalFromRoot(kMessage);
[email protected]df159b22011-09-14 22:10:24592 WaitForTestSignal();
[email protected]4d97f23372012-03-01 04:01:05593 // Verify the signal was not received by the specific proxy.
594 ASSERT_TRUE(test_signal_string_.empty());
595 // Verify the string WAS received by the root proxy.
596 ASSERT_EQ(kMessage, root_test_signal_string_);
[email protected]df159b22011-09-14 22:10:24597}
[email protected]3b6205c2012-03-21 23:39:17598
[email protected]3635d322012-06-02 02:53:20599TEST_F(EndToEndAsyncTest, TestHugeSignal) {
600 const std::string kHugeMessage(kHugePayloadSize, 'o');
601
602 // Send the huge signal from the exported object.
603 test_service_->SendTestSignal(kHugeMessage);
604 // This caused a DCHECK failure before. Ensure that the issue is fixed.
605 WaitForTestSignal();
606 ASSERT_EQ(kHugeMessage, test_signal_string_);
607}
608
[email protected]012e781dc2013-04-24 00:12:35609class SignalMultipleHandlerTest : public EndToEndAsyncTest {
[email protected]3b6205c2012-03-21 23:39:17610 public:
[email protected]012e781dc2013-04-24 00:12:35611 SignalMultipleHandlerTest() {
[email protected]3b6205c2012-03-21 23:39:17612 }
613
dcheng3bb7119c2014-12-29 18:30:17614 void SetUp() override {
[email protected]3b6205c2012-03-21 23:39:17615 // Set up base class.
616 EndToEndAsyncTest::SetUp();
617
[email protected]012e781dc2013-04-24 00:12:35618 // Connect the root object proxy's signal handler to a new handler
[email protected]3b6205c2012-03-21 23:39:17619 // so that we can verify that a second call to ConnectSignal() delivers
[email protected]012e781dc2013-04-24 00:12:35620 // to both our new handler and the old.
[email protected]3b6205c2012-03-21 23:39:17621 object_proxy_->ConnectToSignal(
622 "org.chromium.TestInterface",
623 "Test",
[email protected]012e781dc2013-04-24 00:12:35624 base::Bind(&SignalMultipleHandlerTest::OnAdditionalTestSignal,
[email protected]3b6205c2012-03-21 23:39:17625 base::Unretained(this)),
[email protected]012e781dc2013-04-24 00:12:35626 base::Bind(&SignalMultipleHandlerTest::OnAdditionalConnected,
[email protected]3b6205c2012-03-21 23:39:17627 base::Unretained(this)));
628 // Wait until the object proxy is connected to the signal.
earthdok9562caf2014-09-03 10:32:36629 run_loop_.reset(new base::RunLoop);
630 run_loop_->Run();
[email protected]3b6205c2012-03-21 23:39:17631 }
632
633 protected:
634 // Called when the "Test" signal is received, in the main thread.
[email protected]012e781dc2013-04-24 00:12:35635 // Copy the string payload to |additional_test_signal_string_|.
[email protected]2a57ca642013-06-13 06:37:19636 void OnAdditionalTestSignal(Signal* signal) {
637 MessageReader reader(signal);
[email protected]012e781dc2013-04-24 00:12:35638 ASSERT_TRUE(reader.PopString(&additional_test_signal_string_));
earthdok9562caf2014-09-03 10:32:36639 run_loop_->Quit();
[email protected]3b6205c2012-03-21 23:39:17640 }
641
642 // Called when connected to the signal.
[email protected]012e781dc2013-04-24 00:12:35643 void OnAdditionalConnected(const std::string& interface_name,
644 const std::string& signal_name,
645 bool success) {
[email protected]3b6205c2012-03-21 23:39:17646 ASSERT_TRUE(success);
earthdok9562caf2014-09-03 10:32:36647 run_loop_->Quit();
[email protected]3b6205c2012-03-21 23:39:17648 }
649
[email protected]012e781dc2013-04-24 00:12:35650 // Text message from "Test" signal delivered to additional handler.
651 std::string additional_test_signal_string_;
[email protected]3b6205c2012-03-21 23:39:17652};
653
[email protected]012e781dc2013-04-24 00:12:35654TEST_F(SignalMultipleHandlerTest, TestMultipleHandlers) {
[email protected]3b6205c2012-03-21 23:39:17655 const char kMessage[] = "hello, world";
656 // Send the test signal from the exported object.
657 test_service_->SendTestSignal(kMessage);
658 // Receive the signal with the object proxy.
659 WaitForTestSignal();
[email protected]012e781dc2013-04-24 00:12:35660 // Verify the string WAS received by the original handler.
661 ASSERT_EQ(kMessage, test_signal_string_);
662 // Verify the signal WAS ALSO received by the additional handler.
663 ASSERT_EQ(kMessage, additional_test_signal_string_);
[email protected]3b6205c2012-03-21 23:39:17664}
[email protected]2a57ca642013-06-13 06:37:19665
666} // namespace dbus