blob: 0e2f4a7c3b0e2bede7264505de14d5e1bf518260 [file] [log] [blame]
keybuk@google.combf4649a2012-02-15 06:29:06 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
satorux@chromium.org163f1cb2011-08-18 05:58:12 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
avi0ad0ce02015-12-23 03:12:45 +09005#include <stddef.h>
6
satorux@chromium.org163f1cb2011-08-18 05:58:12 +09007#include <algorithm>
dcheng30c5a172016-04-09 07:55:04 +09008#include <memory>
satorux@chromium.org163f1cb2011-08-18 05:58:12 +09009#include <string>
10#include <vector>
11
12#include "base/bind.h"
avi0ad0ce02015-12-23 03:12:45 +090013#include "base/macros.h"
avi@chromium.orga29af562013-07-18 08:00:30 +090014#include "base/message_loop/message_loop.h"
earthdok64401d72014-09-03 19:32:36 +090015#include "base/run_loop.h"
fdoray851719c2016-08-26 00:36:37 +090016#include "base/single_thread_task_runner.h"
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090017#include "base/stl_util.h"
satorux@chromium.orgb1753152011-11-11 17:36:22 +090018#include "base/test/test_timeouts.h"
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090019#include "base/threading/thread.h"
20#include "base/threading/thread_restrictions.h"
21#include "dbus/bus.h"
22#include "dbus/message.h"
keybuk@google.combf4649a2012-02-15 06:29:06 +090023#include "dbus/object_path.h"
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090024#include "dbus/object_proxy.h"
25#include "dbus/test_service.h"
26#include "testing/gtest/include/gtest/gtest.h"
27
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090028namespace dbus {
29
satorux@chromium.org9f10a6e2012-06-02 11:53:20 +090030namespace {
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
satorux@chromium.orgc6ac7572011-09-01 03:02:43 +090038// The end-to-end test exercises the asynchronous APIs in ObjectProxy and
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090039// ExportedObject.
40class EndToEndAsyncTest : public testing::Test {
41 public:
dcheng7f5750d2014-12-30 03:30:17 +090042 void SetUp() override {
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090043 // Make the main thread not to allow IO.
44 base::ThreadRestrictions::SetIOAllowed(false);
45
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090046 // Start the D-Bus thread.
47 dbus_thread_.reset(new base::Thread("D-Bus Thread"));
48 base::Thread::Options thread_options;
xhwang@chromium.orgdff6b132013-05-02 01:10:30 +090049 thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
satorux@chromium.org01fbb892011-08-20 10:07:17 +090050 ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options));
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090051
satorux@chromium.org01fbb892011-08-20 10:07:17 +090052 // Start the test service, using the D-Bus thread.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090053 TestService::Options options;
skyostile5a8dc42015-06-18 00:46:04 +090054 options.dbus_task_runner = dbus_thread_->task_runner();
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090055 test_service_.reset(new TestService(options));
satorux@chromium.org01fbb892011-08-20 10:07:17 +090056 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.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090061 Bus::Options bus_options;
62 bus_options.bus_type = Bus::SESSION;
63 bus_options.connection_type = Bus::PRIVATE;
skyostile5a8dc42015-06-18 00:46:04 +090064 bus_options.dbus_task_runner = dbus_thread_->task_runner();
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090065 bus_ = new Bus(bus_options);
keybuk@google.combf4649a2012-02-15 06:29:06 +090066 object_proxy_ = bus_->GetObjectProxy(
hashimotof4a4c0d2016-01-05 17:48:03 +090067 test_service_->service_name(),
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090068 ObjectPath("/org/chromium/TestObject"));
satorux@chromium.org01fbb892011-08-20 10:07:17 +090069 ASSERT_TRUE(bus_->HasDBusThread());
satorux@chromium.org7f0c4512011-08-23 16:29:21 +090070
satorux@chromium.org5b3e4962011-11-24 07:08:38 +090071 // Connect to the "Test" signal of "org.chromium.TestInterface" from
72 // the remote object.
satorux@chromium.org7f0c4512011-08-23 16:29:21 +090073 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)));
satorux@chromium.org2b613f22011-12-14 02:08:42 +090080 // Wait until the object proxy is connected to the signal.
earthdok64401d72014-09-03 19:32:36 +090081 run_loop_.reset(new base::RunLoop());
82 run_loop_->Run();
satorux@chromium.org2b613f22011-12-14 02:08:42 +090083
satorux@chromium.org5b3e4962011-11-24 07:08:38 +090084 // 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)));
satorux@chromium.org7f0c4512011-08-23 16:29:21 +090096 // Wait until the object proxy is connected to the signal.
earthdok64401d72014-09-03 19:32:36 +090097 run_loop_.reset(new base::RunLoop());
98 run_loop_->Run();
keybuk@chromium.orgb929d752012-03-01 13:01:05 +090099
100 // Create a second object proxy for the root object.
hashimotof4a4c0d2016-01-05 17:48:03 +0900101 root_object_proxy_ = bus_->GetObjectProxy(test_service_->service_name(),
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900102 ObjectPath("/"));
keybuk@chromium.orgb929d752012-03-01 13:01:05 +0900103 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.
earthdok64401d72014-09-03 19:32:36 +0900115 run_loop_.reset(new base::RunLoop());
116 run_loop_->Run();
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900117 }
118
dcheng7f5750d2014-12-30 03:30:17 +0900119 void TearDown() override {
satorux@chromium.orgd336d452011-09-02 15:56:23 +0900120 bus_->ShutdownOnDBusThreadAndBlock();
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900121
satorux@chromium.org01fbb892011-08-20 10:07:17 +0900122 // Shut down the service.
satorux@chromium.orgd336d452011-09-02 15:56:23 +0900123 test_service_->ShutdownAndBlock();
satorux@chromium.org01fbb892011-08-20 10:07:17 +0900124
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900125 // Reset to the default.
126 base::ThreadRestrictions::SetIOAllowed(true);
127
satorux@chromium.orgc6ac7572011-09-01 03:02:43 +0900128 // Stopping a thread is considered an IO operation, so do this after
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900129 // allowing IO.
130 test_service_->Stop();
131 }
132
133 protected:
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900134 // 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[] = "";
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900141 Bus::Options bus_options;
142 bus_options.bus_type = Bus::CUSTOM_ADDRESS;
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900143 bus_options.address = kInvalidAddress;
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900144 bus_options.connection_type = Bus::PRIVATE;
skyostile5a8dc42015-06-18 00:46:04 +0900145 bus_options.dbus_task_runner = dbus_thread_->task_runner();
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900146 bus_ = new Bus(bus_options);
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900147 ASSERT_TRUE(bus_->HasDBusThread());
148
149 // Create new object proxy.
150 object_proxy_ = bus_->GetObjectProxy(
hashimotof4a4c0d2016-01-05 17:48:03 +0900151 test_service_->service_name(),
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900152 ObjectPath("/org/chromium/TestObject"));
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900153 }
154
satorux@chromium.orgc6ac7572011-09-01 03:02:43 +0900155 // Calls the method asynchronously. OnResponse() will be called once the
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900156 // response is received.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900157 void CallMethod(MethodCall* method_call,
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900158 int timeout_ms) {
159 object_proxy_->CallMethod(method_call,
160 timeout_ms,
161 base::Bind(&EndToEndAsyncTest::OnResponse,
162 base::Unretained(this)));
163 }
164
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900165 // Calls the method asynchronously. OnResponse() will be called once the
166 // response is received without error, otherwise OnError() will be called.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900167 void CallMethodWithErrorCallback(MethodCall* method_call,
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900168 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
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900176 // Wait for the give number of responses.
177 void WaitForResponses(size_t num_responses) {
178 while (response_strings_.size() < num_responses) {
earthdok64401d72014-09-03 19:32:36 +0900179 run_loop_.reset(new base::RunLoop);
180 run_loop_->Run();
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900181 }
182 }
183
184 // Called when the response is received.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900185 void OnResponse(Response* response) {
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900186 // |response| will be deleted on exit of the function. Copy the
187 // payload to |response_strings_|.
188 if (response) {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900189 MessageReader reader(response);
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900190 std::string response_string;
191 ASSERT_TRUE(reader.PopString(&response_string));
192 response_strings_.push_back(response_string);
193 } else {
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +0900194 response_strings_.push_back(std::string());
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900195 }
earthdok64401d72014-09-03 19:32:36 +0900196 run_loop_->Quit();
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900197 };
198
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900199 // Wait for the given number of errors.
200 void WaitForErrors(size_t num_errors) {
201 while (error_names_.size() < num_errors) {
earthdok64401d72014-09-03 19:32:36 +0900202 run_loop_.reset(new base::RunLoop);
203 run_loop_->Run();
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900204 }
205 }
206
207 // Called when an error is received.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900208 void OnError(ErrorResponse* error) {
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900209 // |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 {
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +0900215 error_names_.push_back(std::string());
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900216 }
earthdok64401d72014-09-03 19:32:36 +0900217 run_loop_->Quit();
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900218 }
219
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900220 // Called when the "Test" signal is received, in the main thread.
221 // Copy the string payload to |test_signal_string_|.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900222 void OnTestSignal(Signal* signal) {
223 MessageReader reader(signal);
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900224 ASSERT_TRUE(reader.PopString(&test_signal_string_));
earthdok64401d72014-09-03 19:32:36 +0900225 run_loop_->Quit();
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900226 }
227
keybuk@chromium.orgb929d752012-03-01 13:01:05 +0900228 // 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_|.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900231 void OnRootTestSignal(Signal* signal) {
232 MessageReader reader(signal);
keybuk@chromium.orgb929d752012-03-01 13:01:05 +0900233 ASSERT_TRUE(reader.PopString(&root_test_signal_string_));
earthdok64401d72014-09-03 19:32:36 +0900234 run_loop_->Quit();
keybuk@chromium.orgb929d752012-03-01 13:01:05 +0900235 }
236
satorux@chromium.org5b3e4962011-11-24 07:08:38 +0900237 // Called when the "Test2" signal is received, in the main thread.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900238 void OnTest2Signal(Signal* signal) {
239 MessageReader reader(signal);
earthdok64401d72014-09-03 19:32:36 +0900240 run_loop_->Quit();
satorux@chromium.org5b3e4962011-11-24 07:08:38 +0900241 }
242
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900243 // 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);
earthdok64401d72014-09-03 19:32:36 +0900248 run_loop_->Quit();
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900249 }
250
251 // Wait for the hey signal to be received.
252 void WaitForTestSignal() {
253 // OnTestSignal() will quit the message loop.
earthdok64401d72014-09-03 19:32:36 +0900254 run_loop_.reset(new base::RunLoop);
255 run_loop_->Run();
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900256 }
257
xhwang@chromium.orgdff6b132013-05-02 01:10:30 +0900258 base::MessageLoop message_loop_;
dcheng30c5a172016-04-09 07:55:04 +0900259 std::unique_ptr<base::RunLoop> run_loop_;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900260 std::vector<std::string> response_strings_;
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900261 std::vector<std::string> error_names_;
dcheng30c5a172016-04-09 07:55:04 +0900262 std::unique_ptr<base::Thread> dbus_thread_;
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900263 scoped_refptr<Bus> bus_;
264 ObjectProxy* object_proxy_;
265 ObjectProxy* root_object_proxy_;
dcheng30c5a172016-04-09 07:55:04 +0900266 std::unique_ptr<TestService> test_service_;
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900267 // Text message from "Test" signal.
268 std::string test_signal_string_;
keybuk@chromium.orgb929d752012-03-01 13:01:05 +0900269 // Text message from "Test" signal delivered to root.
270 std::string root_test_signal_string_;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900271};
272
273TEST_F(EndToEndAsyncTest, Echo) {
274 const char* kHello = "hello";
275
276 // Create the method call.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900277 MethodCall method_call("org.chromium.TestInterface", "Echo");
278 MessageWriter writer(&method_call);
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900279 writer.AppendString(kHello);
280
281 // Call the method.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900282 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900283 CallMethod(&method_call, timeout_ms);
284
285 // Check the response.
286 WaitForResponses(1);
287 EXPECT_EQ(kHello, response_strings_[0]);
288}
289
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900290TEST_F(EndToEndAsyncTest, EchoWithErrorCallback) {
291 const char* kHello = "hello";
292
293 // Create the method call.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900294 MethodCall method_call("org.chromium.TestInterface", "Echo");
295 MessageWriter writer(&method_call);
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900296 writer.AppendString(kHello);
297
298 // Call the method.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900299 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900300 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
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900308// 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.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900314 MethodCall method_call("org.chromium.TestInterface", "Echo");
315 MessageWriter writer(&method_call);
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900316 writer.AppendString(kMessages[i]);
317
318 // Call the method.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900319 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900320 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
satorux@chromium.org9f10a6e2012-06-02 11:53:20 +0900332TEST_F(EndToEndAsyncTest, Echo_HugePayload) {
333 const std::string kHugePayload(kHugePayloadSize, 'o');
334
335 // Create the method call with a huge payload.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900336 MethodCall method_call("org.chromium.TestInterface", "Echo");
337 MessageWriter writer(&method_call);
satorux@chromium.org9f10a6e2012-06-02 11:53:20 +0900338 writer.AppendString(kHugePayload);
339
340 // Call the method.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900341 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
satorux@chromium.org9f10a6e2012-06-02 11:53:20 +0900342 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
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900349TEST_F(EndToEndAsyncTest, BrokenBus) {
350 const char* kHello = "hello";
351
352 // Set up a broken bus.
353 SetUpBrokenBus();
354
355 // Create the method call.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900356 MethodCall method_call("org.chromium.TestInterface", "Echo");
357 MessageWriter writer(&method_call);
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900358 writer.AppendString(kHello);
359
360 // Call the method.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900361 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900362 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.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900376 MethodCall method_call("org.chromium.TestInterface", "Echo");
377 MessageWriter writer(&method_call);
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900378 writer.AppendString(kHello);
379
380 // Call the method.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900381 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900382 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
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900390TEST_F(EndToEndAsyncTest, Timeout) {
391 const char* kHello = "hello";
392
393 // Create the method call.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900394 MethodCall method_call("org.chromium.TestInterface", "SlowEcho");
395 MessageWriter writer(&method_call);
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900396 writer.AppendString(kHello);
397
satorux@chromium.org01fbb892011-08-20 10:07:17 +0900398 // Call the method with timeout of 0ms.
399 const int timeout_ms = 0;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900400 CallMethod(&method_call, timeout_ms);
401 WaitForResponses(1);
402
403 // Should fail because of timeout.
404 ASSERT_EQ("", response_strings_[0]);
405}
406
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900407TEST_F(EndToEndAsyncTest, TimeoutWithErrorCallback) {
408 const char* kHello = "hello";
409
410 // Create the method call.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900411 MethodCall method_call("org.chromium.TestInterface", "SlowEcho");
412 MessageWriter writer(&method_call);
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900413 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
hashimoto14eb1502015-03-30 16:01:39 +0900425TEST_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.
hashimotof4a4c0d2016-01-05 17:48:03 +0900439 bus_->RemoveObjectProxy(test_service_->service_name(),
hashimoto14eb1502015-03-30 16:01:39 +0900440 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);
fdoray851719c2016-08-26 00:36:37 +0900445 message_loop_.task_runner()->PostDelayedTask(
446 FROM_HERE, run_loop_->QuitClosure(), TestTimeouts::tiny_timeout());
hashimoto14eb1502015-03-30 16:01:39 +0900447 run_loop_->Run();
448 EXPECT_TRUE(response_strings_.empty());
449}
450
vlaviano@chromium.org241215d2011-11-30 13:57:42 +0900451// Tests calling a method that sends its reply asynchronously.
452TEST_F(EndToEndAsyncTest, AsyncEcho) {
453 const char* kHello = "hello";
454
455 // Create the method call.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900456 MethodCall method_call("org.chromium.TestInterface", "AsyncEcho");
457 MessageWriter writer(&method_call);
vlaviano@chromium.org241215d2011-11-30 13:57:42 +0900458 writer.AppendString(kHello);
459
460 // Call the method.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900461 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
vlaviano@chromium.org241215d2011-11-30 13:57:42 +0900462 CallMethod(&method_call, timeout_ms);
463
464 // Check the response.
465 WaitForResponses(1);
466 EXPECT_EQ(kHello, response_strings_[0]);
467}
468
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900469TEST_F(EndToEndAsyncTest, NonexistentMethod) {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900470 MethodCall method_call("org.chromium.TestInterface", "Nonexistent");
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900471
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900472 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900473 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
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900480TEST_F(EndToEndAsyncTest, NonexistentMethodWithErrorCallback) {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900481 MethodCall method_call("org.chromium.TestInterface", "Nonexistent");
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900482
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900483 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900484 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
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900492TEST_F(EndToEndAsyncTest, BrokenMethod) {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900493 MethodCall method_call("org.chromium.TestInterface", "BrokenMethod");
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900494
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900495 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900496 CallMethod(&method_call, timeout_ms);
497 WaitForResponses(1);
498
499 // Should fail because the method is broken.
500 ASSERT_EQ("", response_strings_[0]);
501}
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900502
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900503TEST_F(EndToEndAsyncTest, BrokenMethodWithErrorCallback) {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900504 MethodCall method_call("org.chromium.TestInterface", "BrokenMethod");
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900505
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900506 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900507 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
hashimoto@chromium.orgb0305512012-05-23 15:55:22 +0900515TEST_F(EndToEndAsyncTest, InvalidServiceName) {
516 // Bus name cannot contain '/'.
517 const std::string invalid_service_name = ":1/2";
518
519 // Replace object proxy with new one.
Ryo Hashimoto61ab06d2017-09-25 16:13:50 +0900520 object_proxy_ = bus_->GetObjectProxy(invalid_service_name,
521 ObjectPath("/org/chromium/TestObject"));
hashimoto@chromium.orgb0305512012-05-23 15:55:22 +0900522
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900523 MethodCall method_call("org.chromium.TestInterface", "Echo");
hashimoto@chromium.orgb0305512012-05-23 15:55:22 +0900524
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900525 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
hashimoto@chromium.orgb0305512012-05-23 15:55:22 +0900526 CallMethodWithErrorCallback(&method_call, timeout_ms);
527 WaitForErrors(1);
528
529 // Should fail because of the invalid bus name.
530 ASSERT_TRUE(response_strings_.empty());
531 ASSERT_EQ("", error_names_[0]);
532}
533
satorux@chromium.orgb1753152011-11-11 17:36:22 +0900534TEST_F(EndToEndAsyncTest, EmptyResponseCallback) {
535 const char* kHello = "hello";
536
537 // Create the method call.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900538 MethodCall method_call("org.chromium.TestInterface", "Echo");
539 MessageWriter writer(&method_call);
satorux@chromium.orgb1753152011-11-11 17:36:22 +0900540 writer.AppendString(kHello);
541
542 // Call the method with an empty callback.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900543 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
satorux@chromium.orgb1753152011-11-11 17:36:22 +0900544 object_proxy_->CallMethod(&method_call,
545 timeout_ms,
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900546 ObjectProxy::EmptyResponseCallback());
satorux@chromium.orgb1753152011-11-11 17:36:22 +0900547 // Post a delayed task to quit the message loop.
earthdok64401d72014-09-03 19:32:36 +0900548 run_loop_.reset(new base::RunLoop);
fdoray851719c2016-08-26 00:36:37 +0900549 message_loop_.task_runner()->PostDelayedTask(
550 FROM_HERE, run_loop_->QuitClosure(), TestTimeouts::tiny_timeout());
earthdok64401d72014-09-03 19:32:36 +0900551 run_loop_->Run();
satorux@chromium.orgb1753152011-11-11 17:36:22 +0900552 // We cannot tell if the empty callback is called, but at least we can
553 // check if the test does not crash.
554}
555
satorux@chromium.org2b613f22011-12-14 02:08:42 +0900556TEST_F(EndToEndAsyncTest, TestSignal) {
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900557 const char kMessage[] = "hello, world";
558 // Send the test signal from the exported object.
559 test_service_->SendTestSignal(kMessage);
satorux@chromium.orgc6ac7572011-09-01 03:02:43 +0900560 // Receive the signal with the object proxy. The signal is handled in
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900561 // EndToEndAsyncTest::OnTestSignal() in the main thread.
562 WaitForTestSignal();
563 ASSERT_EQ(kMessage, test_signal_string_);
564}
satorux@chromium.org7225b7a2011-09-15 07:10:24 +0900565
satorux@chromium.org2b613f22011-12-14 02:08:42 +0900566TEST_F(EndToEndAsyncTest, TestSignalFromRoot) {
satorux@chromium.org7225b7a2011-09-15 07:10:24 +0900567 const char kMessage[] = "hello, world";
keybuk@chromium.orgb929d752012-03-01 13:01:05 +0900568 // Object proxies are tied to a particular object path, if a signal
569 // arrives from a different object path like "/" the first object proxy
570 // |object_proxy_| should not handle it, and should leave it for the root
571 // object proxy |root_object_proxy_|.
satorux@chromium.org7225b7a2011-09-15 07:10:24 +0900572 test_service_->SendTestSignalFromRoot(kMessage);
satorux@chromium.org7225b7a2011-09-15 07:10:24 +0900573 WaitForTestSignal();
keybuk@chromium.orgb929d752012-03-01 13:01:05 +0900574 // Verify the signal was not received by the specific proxy.
575 ASSERT_TRUE(test_signal_string_.empty());
576 // Verify the string WAS received by the root proxy.
577 ASSERT_EQ(kMessage, root_test_signal_string_);
satorux@chromium.org7225b7a2011-09-15 07:10:24 +0900578}
keybuk@chromium.orgde7b4a92012-03-22 08:39:17 +0900579
satorux@chromium.org9f10a6e2012-06-02 11:53:20 +0900580TEST_F(EndToEndAsyncTest, TestHugeSignal) {
581 const std::string kHugeMessage(kHugePayloadSize, 'o');
582
583 // Send the huge signal from the exported object.
584 test_service_->SendTestSignal(kHugeMessage);
585 // This caused a DCHECK failure before. Ensure that the issue is fixed.
586 WaitForTestSignal();
587 ASSERT_EQ(kHugeMessage, test_signal_string_);
588}
589
keybuk@chromium.org2594a712013-04-24 09:12:35 +0900590class SignalMultipleHandlerTest : public EndToEndAsyncTest {
keybuk@chromium.orgde7b4a92012-03-22 08:39:17 +0900591 public:
Chris Watkins635e8902017-11-29 16:44:11 +0900592 SignalMultipleHandlerTest() = default;
keybuk@chromium.orgde7b4a92012-03-22 08:39:17 +0900593
dcheng7f5750d2014-12-30 03:30:17 +0900594 void SetUp() override {
keybuk@chromium.orgde7b4a92012-03-22 08:39:17 +0900595 // Set up base class.
596 EndToEndAsyncTest::SetUp();
597
keybuk@chromium.org2594a712013-04-24 09:12:35 +0900598 // Connect the root object proxy's signal handler to a new handler
keybuk@chromium.orgde7b4a92012-03-22 08:39:17 +0900599 // so that we can verify that a second call to ConnectSignal() delivers
keybuk@chromium.org2594a712013-04-24 09:12:35 +0900600 // to both our new handler and the old.
keybuk@chromium.orgde7b4a92012-03-22 08:39:17 +0900601 object_proxy_->ConnectToSignal(
602 "org.chromium.TestInterface",
603 "Test",
keybuk@chromium.org2594a712013-04-24 09:12:35 +0900604 base::Bind(&SignalMultipleHandlerTest::OnAdditionalTestSignal,
keybuk@chromium.orgde7b4a92012-03-22 08:39:17 +0900605 base::Unretained(this)),
keybuk@chromium.org2594a712013-04-24 09:12:35 +0900606 base::Bind(&SignalMultipleHandlerTest::OnAdditionalConnected,
keybuk@chromium.orgde7b4a92012-03-22 08:39:17 +0900607 base::Unretained(this)));
608 // Wait until the object proxy is connected to the signal.
earthdok64401d72014-09-03 19:32:36 +0900609 run_loop_.reset(new base::RunLoop);
610 run_loop_->Run();
keybuk@chromium.orgde7b4a92012-03-22 08:39:17 +0900611 }
612
613 protected:
614 // Called when the "Test" signal is received, in the main thread.
keybuk@chromium.org2594a712013-04-24 09:12:35 +0900615 // Copy the string payload to |additional_test_signal_string_|.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900616 void OnAdditionalTestSignal(Signal* signal) {
617 MessageReader reader(signal);
keybuk@chromium.org2594a712013-04-24 09:12:35 +0900618 ASSERT_TRUE(reader.PopString(&additional_test_signal_string_));
earthdok64401d72014-09-03 19:32:36 +0900619 run_loop_->Quit();
keybuk@chromium.orgde7b4a92012-03-22 08:39:17 +0900620 }
621
622 // Called when connected to the signal.
keybuk@chromium.org2594a712013-04-24 09:12:35 +0900623 void OnAdditionalConnected(const std::string& interface_name,
624 const std::string& signal_name,
625 bool success) {
keybuk@chromium.orgde7b4a92012-03-22 08:39:17 +0900626 ASSERT_TRUE(success);
earthdok64401d72014-09-03 19:32:36 +0900627 run_loop_->Quit();
keybuk@chromium.orgde7b4a92012-03-22 08:39:17 +0900628 }
629
keybuk@chromium.org2594a712013-04-24 09:12:35 +0900630 // Text message from "Test" signal delivered to additional handler.
631 std::string additional_test_signal_string_;
keybuk@chromium.orgde7b4a92012-03-22 08:39:17 +0900632};
633
keybuk@chromium.org2594a712013-04-24 09:12:35 +0900634TEST_F(SignalMultipleHandlerTest, TestMultipleHandlers) {
keybuk@chromium.orgde7b4a92012-03-22 08:39:17 +0900635 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();
keybuk@chromium.org2594a712013-04-24 09:12:35 +0900640 // Verify the string WAS received by the original handler.
641 ASSERT_EQ(kMessage, test_signal_string_);
642 // Verify the signal WAS ALSO received by the additional handler.
643 ASSERT_EQ(kMessage, additional_test_signal_string_);
keybuk@chromium.orgde7b4a92012-03-22 08:39:17 +0900644}
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900645
646} // namespace dbus