blob: 00a1138d257b4810488b8b5c8fecf28976eb295d [file] [log] [blame]
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +09001// 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.
4
dcheng30c5a172016-04-09 07:55:04 +09005#include <memory>
6
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +09007#include "base/bind.h"
avi@chromium.orga29af562013-07-18 08:00:30 +09008#include "base/message_loop/message_loop.h"
asvitkine9481ed82017-01-20 00:17:38 +09009#include "base/metrics/histogram_macros.h"
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +090010#include "base/metrics/histogram_samples.h"
earthdok64401d72014-09-03 19:32:36 +090011#include "base/run_loop.h"
fdoray851719c2016-08-26 00:36:37 +090012#include "base/single_thread_task_runner.h"
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +090013#include "base/test/test_timeouts.h"
14#include "base/threading/platform_thread.h"
15#include "base/threading/thread_restrictions.h"
16#include "dbus/bus.h"
17#include "dbus/message.h"
18#include "dbus/object_proxy.h"
19#include "dbus/test_service.h"
20#include "testing/gtest/include/gtest/gtest.h"
21
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090022namespace dbus {
23
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +090024// The test for sender verification in ObjectProxy.
25class SignalSenderVerificationTest : public testing::Test {
26 public:
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +090027 SignalSenderVerificationTest()
28 : on_name_owner_changed_called_(false),
29 on_ownership_called_(false) {
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +090030 }
31
dcheng7f5750d2014-12-30 03:30:17 +090032 void SetUp() override {
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +090033 // Make the main thread not to allow IO.
34 base::ThreadRestrictions::SetIOAllowed(false);
35
36 // Start the D-Bus thread.
37 dbus_thread_.reset(new base::Thread("D-Bus Thread"));
38 base::Thread::Options thread_options;
xhwang@chromium.orgdff6b132013-05-02 01:10:30 +090039 thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +090040 ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options));
41
hashimotof4a4c0d2016-01-05 17:48:03 +090042 // Create the test service, using the D-Bus thread.
43 TestService::Options options;
44 options.dbus_task_runner = dbus_thread_->task_runner();
45 test_service_.reset(new TestService(options));
46
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +090047 // Create the client, using the D-Bus thread.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090048 Bus::Options bus_options;
49 bus_options.bus_type = Bus::SESSION;
50 bus_options.connection_type = Bus::PRIVATE;
skyostile5a8dc42015-06-18 00:46:04 +090051 bus_options.dbus_task_runner = dbus_thread_->task_runner();
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090052 bus_ = new Bus(bus_options);
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +090053 object_proxy_ = bus_->GetObjectProxy(
hashimotof4a4c0d2016-01-05 17:48:03 +090054 test_service_->service_name(),
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090055 ObjectPath("/org/chromium/TestObject"));
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +090056 ASSERT_TRUE(bus_->HasDBusThread());
57
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +090058 object_proxy_->SetNameOwnerChangedCallback(
59 base::Bind(&SignalSenderVerificationTest::OnNameOwnerChanged,
keybuk@chromium.org359c9b62012-11-27 09:23:25 +090060 base::Unretained(this),
61 &on_name_owner_changed_called_));
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +090062
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +090063 // Connect to the "Test" signal of "org.chromium.TestInterface" from
64 // the remote object.
65 object_proxy_->ConnectToSignal(
66 "org.chromium.TestInterface",
67 "Test",
68 base::Bind(&SignalSenderVerificationTest::OnTestSignal,
69 base::Unretained(this)),
70 base::Bind(&SignalSenderVerificationTest::OnConnected,
71 base::Unretained(this)));
72 // Wait until the object proxy is connected to the signal.
earthdok64401d72014-09-03 19:32:36 +090073 run_loop_.reset(new base::RunLoop);
74 run_loop_->Run();
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +090075
hashimotof4a4c0d2016-01-05 17:48:03 +090076 // Start the test service.
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +090077 ASSERT_TRUE(test_service_->StartService());
78 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted());
79 ASSERT_TRUE(test_service_->HasDBusThread());
80 ASSERT_TRUE(test_service_->has_ownership());
81
82 // Same setup for the second TestService. This service should not have the
83 // ownership of the name at this point.
hashimotof4a4c0d2016-01-05 17:48:03 +090084 options.service_name = test_service_->service_name();
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090085 test_service2_.reset(new TestService(options));
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +090086 ASSERT_TRUE(test_service2_->StartService());
87 ASSERT_TRUE(test_service2_->WaitUntilServiceIsStarted());
88 ASSERT_TRUE(test_service2_->HasDBusThread());
89 ASSERT_FALSE(test_service2_->has_ownership());
90
91 // The name should be owned and known at this point.
earthdok64401d72014-09-03 19:32:36 +090092 if (!on_name_owner_changed_called_) {
93 run_loop_.reset(new base::RunLoop);
94 run_loop_->Run();
95 }
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +090096 ASSERT_FALSE(latest_name_owner_.empty());
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +090097 }
98
dcheng7f5750d2014-12-30 03:30:17 +090099 void TearDown() override {
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +0900100 bus_->ShutdownOnDBusThreadAndBlock();
101
102 // Shut down the service.
103 test_service_->ShutdownAndBlock();
104 test_service2_->ShutdownAndBlock();
105
106 // Reset to the default.
107 base::ThreadRestrictions::SetIOAllowed(true);
108
109 // Stopping a thread is considered an IO operation, so do this after
110 // allowing IO.
111 test_service_->Stop();
112 test_service2_->Stop();
113 }
114
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +0900115 void OnOwnership(bool expected, bool success) {
116 ASSERT_EQ(expected, success);
117 // PostTask to quit the MessageLoop as this is called from D-Bus thread.
fdoray851719c2016-08-26 00:36:37 +0900118 message_loop_.task_runner()->PostTask(
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +0900119 FROM_HERE,
120 base::Bind(&SignalSenderVerificationTest::OnOwnershipInternal,
121 base::Unretained(this)));
122 }
123
124 void OnOwnershipInternal() {
125 on_ownership_called_ = true;
earthdok64401d72014-09-03 19:32:36 +0900126 run_loop_->Quit();
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +0900127 }
128
hashimoto@chromium.org4f3851c2013-09-27 16:12:03 +0900129 void OnNameOwnerChanged(bool* called_flag,
130 const std::string& old_owner,
131 const std::string& new_owner) {
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +0900132 latest_name_owner_ = new_owner;
keybuk@chromium.org359c9b62012-11-27 09:23:25 +0900133 *called_flag = true;
earthdok64401d72014-09-03 19:32:36 +0900134 run_loop_->Quit();
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +0900135 }
136
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +0900137 // Called when the "Test" signal is received, in the main thread.
138 // Copy the string payload to |test_signal_string_|.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900139 void OnTestSignal(Signal* signal) {
140 MessageReader reader(signal);
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +0900141 ASSERT_TRUE(reader.PopString(&test_signal_string_));
earthdok64401d72014-09-03 19:32:36 +0900142 run_loop_->Quit();
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +0900143 }
144
145 // Called when connected to the signal.
146 void OnConnected(const std::string& interface_name,
147 const std::string& signal_name,
148 bool success) {
149 ASSERT_TRUE(success);
earthdok64401d72014-09-03 19:32:36 +0900150 run_loop_->Quit();
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +0900151 }
152
keybuk@chromium.org359c9b62012-11-27 09:23:25 +0900153 protected:
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +0900154 // Wait for the hey signal to be received.
155 void WaitForTestSignal() {
156 // OnTestSignal() will quit the message loop.
earthdok64401d72014-09-03 19:32:36 +0900157 run_loop_.reset(new base::RunLoop);
158 run_loop_->Run();
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +0900159 }
160
cmasone@chromium.org989857e2013-07-31 15:34:59 +0900161 // Stopping a thread is considered an IO operation, so we need to fiddle with
162 // thread restrictions before and after calling Stop() on a TestService.
163 void SafeServiceStop(TestService* test_service) {
164 base::ThreadRestrictions::SetIOAllowed(true);
165 test_service->Stop();
166 base::ThreadRestrictions::SetIOAllowed(false);
167 }
168
xhwang@chromium.orgdff6b132013-05-02 01:10:30 +0900169 base::MessageLoop message_loop_;
dcheng30c5a172016-04-09 07:55:04 +0900170 std::unique_ptr<base::RunLoop> run_loop_;
171 std::unique_ptr<base::Thread> dbus_thread_;
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900172 scoped_refptr<Bus> bus_;
173 ObjectProxy* object_proxy_;
dcheng30c5a172016-04-09 07:55:04 +0900174 std::unique_ptr<TestService> test_service_;
175 std::unique_ptr<TestService> test_service2_;
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +0900176 // Text message from "Test" signal.
177 std::string test_signal_string_;
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +0900178
179 // The known latest name owner of TestService. Updated in OnNameOwnerChanged.
180 std::string latest_name_owner_;
181
182 // Boolean flags to record callback calls.
183 bool on_name_owner_changed_called_;
184 bool on_ownership_called_;
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +0900185};
186
187TEST_F(SignalSenderVerificationTest, TestSignalAccepted) {
188 const char kMessage[] = "hello, world";
189 // Send the test signal from the exported object.
190 test_service_->SendTestSignal(kMessage);
191 // Receive the signal with the object proxy. The signal is handled in
192 // SignalSenderVerificationTest::OnTestSignal() in the main thread.
193 WaitForTestSignal();
194 ASSERT_EQ(kMessage, test_signal_string_);
195}
196
hashimoto13ef3372017-04-25 16:13:31 +0900197TEST_F(SignalSenderVerificationTest, TestSignalRejected) {
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +0900198 const char kNewMessage[] = "hello, new world";
199 test_service2_->SendTestSignal(kNewMessage);
200
201 // This test tests that our callback is NOT called by the ObjectProxy.
202 // Sleep to have message delivered to the client via the D-Bus service.
hashimoto13ef3372017-04-25 16:13:31 +0900203 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout());
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +0900204
205 ASSERT_EQ("", test_signal_string_);
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +0900206}
207
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +0900208TEST_F(SignalSenderVerificationTest, TestOwnerChanged) {
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +0900209 const char kMessage[] = "hello, world";
210
211 // Send the test signal from the exported object.
212 test_service_->SendTestSignal(kMessage);
213 // Receive the signal with the object proxy. The signal is handled in
214 // SignalSenderVerificationTest::OnTestSignal() in the main thread.
215 WaitForTestSignal();
216 ASSERT_EQ(kMessage, test_signal_string_);
217
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +0900218 // Release and acquire the name ownership.
219 // latest_name_owner_ should be non empty as |test_service_| owns the name.
220 ASSERT_FALSE(latest_name_owner_.empty());
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +0900221 test_service_->ShutdownAndBlock();
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +0900222 // OnNameOwnerChanged will PostTask to quit the message loop.
earthdok64401d72014-09-03 19:32:36 +0900223 run_loop_.reset(new base::RunLoop);
224 run_loop_->Run();
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +0900225 // latest_name_owner_ should be empty as the owner is gone.
226 ASSERT_TRUE(latest_name_owner_.empty());
227
228 // Reset the flag as NameOwnerChanged is already received in setup.
229 on_name_owner_changed_called_ = false;
cmasone@chromium.org989857e2013-07-31 15:34:59 +0900230 on_ownership_called_ = false;
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +0900231 test_service2_->RequestOwnership(
232 base::Bind(&SignalSenderVerificationTest::OnOwnership,
233 base::Unretained(this), true));
234 // Both of OnNameOwnerChanged() and OnOwnership() should quit the MessageLoop,
235 // but there's no expected order of those 2 event.
earthdok64401d72014-09-03 19:32:36 +0900236 run_loop_.reset(new base::RunLoop);
237 run_loop_->Run();
238 if (!on_name_owner_changed_called_ || !on_ownership_called_) {
239 run_loop_.reset(new base::RunLoop);
240 run_loop_->Run();
241 }
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +0900242 ASSERT_TRUE(on_name_owner_changed_called_);
243 ASSERT_TRUE(on_ownership_called_);
244
245 // latest_name_owner_ becomes non empty as the new owner appears.
246 ASSERT_FALSE(latest_name_owner_.empty());
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +0900247
248 // Now the second service owns the name.
249 const char kNewMessage[] = "hello, new world";
250
251 test_service2_->SendTestSignal(kNewMessage);
252 WaitForTestSignal();
253 ASSERT_EQ(kNewMessage, test_signal_string_);
254}
keybuk@chromium.org359c9b62012-11-27 09:23:25 +0900255
cmasone@chromium.org989857e2013-07-31 15:34:59 +0900256TEST_F(SignalSenderVerificationTest, TestOwnerStealing) {
257 // Release and acquire the name ownership.
258 // latest_name_owner_ should be non empty as |test_service_| owns the name.
259 ASSERT_FALSE(latest_name_owner_.empty());
260 test_service_->ShutdownAndBlock();
261 // OnNameOwnerChanged will PostTask to quit the message loop.
earthdok64401d72014-09-03 19:32:36 +0900262 run_loop_.reset(new base::RunLoop);
263 run_loop_->Run();
cmasone@chromium.org989857e2013-07-31 15:34:59 +0900264 // latest_name_owner_ should be empty as the owner is gone.
265 ASSERT_TRUE(latest_name_owner_.empty());
266 // Reset the flag as NameOwnerChanged is already received in setup.
267 on_name_owner_changed_called_ = false;
268
269 // Start a test service that allows theft, using the D-Bus thread.
270 TestService::Options options;
skyostile5a8dc42015-06-18 00:46:04 +0900271 options.dbus_task_runner = dbus_thread_->task_runner();
cmasone@chromium.org989857e2013-07-31 15:34:59 +0900272 options.request_ownership_options = Bus::REQUIRE_PRIMARY_ALLOW_REPLACEMENT;
hashimotof4a4c0d2016-01-05 17:48:03 +0900273 options.service_name = test_service_->service_name();
cmasone@chromium.org989857e2013-07-31 15:34:59 +0900274 TestService stealable_test_service(options);
275 ASSERT_TRUE(stealable_test_service.StartService());
276 ASSERT_TRUE(stealable_test_service.WaitUntilServiceIsStarted());
277 ASSERT_TRUE(stealable_test_service.HasDBusThread());
278 ASSERT_TRUE(stealable_test_service.has_ownership());
279
280 // OnNameOwnerChanged will PostTask to quit the message loop.
earthdok64401d72014-09-03 19:32:36 +0900281 run_loop_.reset(new base::RunLoop);
282 run_loop_->Run();
cmasone@chromium.org989857e2013-07-31 15:34:59 +0900283
284 // Send a signal to check that the service is correctly owned.
285 const char kMessage[] = "hello, world";
286
287 // Send the test signal from the exported object.
288 stealable_test_service.SendTestSignal(kMessage);
289 // Receive the signal with the object proxy. The signal is handled in
290 // SignalSenderVerificationTest::OnTestSignal() in the main thread.
291 WaitForTestSignal();
292 ASSERT_EQ(kMessage, test_signal_string_);
293
294 // Reset the flag as NameOwnerChanged was called above.
295 on_name_owner_changed_called_ = false;
296 test_service2_->RequestOwnership(
297 base::Bind(&SignalSenderVerificationTest::OnOwnership,
298 base::Unretained(this), true));
299 // Both of OnNameOwnerChanged() and OnOwnership() should quit the MessageLoop,
300 // but there's no expected order of those 2 event.
earthdok64401d72014-09-03 19:32:36 +0900301 run_loop_.reset(new base::RunLoop);
302 run_loop_->Run();
303 if (!on_name_owner_changed_called_ || !on_ownership_called_) {
304 run_loop_.reset(new base::RunLoop);
305 run_loop_->Run();
306 }
cmasone@chromium.org989857e2013-07-31 15:34:59 +0900307 ASSERT_TRUE(on_name_owner_changed_called_);
308 ASSERT_TRUE(on_ownership_called_);
309
310 // Now the second service owns the name.
311 const char kNewMessage[] = "hello, new world";
312
313 test_service2_->SendTestSignal(kNewMessage);
314 WaitForTestSignal();
315 ASSERT_EQ(kNewMessage, test_signal_string_);
316
317 SafeServiceStop(&stealable_test_service);
318}
319
keybuk@chromium.org7adf6a32012-11-27 10:01:28 +0900320// Fails on Linux ChromiumOS Tests
321TEST_F(SignalSenderVerificationTest, DISABLED_TestMultipleObjects) {
keybuk@chromium.org359c9b62012-11-27 09:23:25 +0900322 const char kMessage[] = "hello, world";
323
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900324 ObjectProxy* object_proxy2 = bus_->GetObjectProxy(
hashimotof4a4c0d2016-01-05 17:48:03 +0900325 test_service_->service_name(),
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900326 ObjectPath("/org/chromium/DifferentObject"));
keybuk@chromium.org359c9b62012-11-27 09:23:25 +0900327
328 bool second_name_owner_changed_called = false;
329 object_proxy2->SetNameOwnerChangedCallback(
330 base::Bind(&SignalSenderVerificationTest::OnNameOwnerChanged,
331 base::Unretained(this),
332 &second_name_owner_changed_called));
333
334 // Connect to a signal on the additional remote object to trigger the
335 // name owner matching.
336 object_proxy2->ConnectToSignal(
337 "org.chromium.DifferentTestInterface",
338 "Test",
339 base::Bind(&SignalSenderVerificationTest::OnTestSignal,
340 base::Unretained(this)),
341 base::Bind(&SignalSenderVerificationTest::OnConnected,
342 base::Unretained(this)));
343 // Wait until the object proxy is connected to the signal.
earthdok64401d72014-09-03 19:32:36 +0900344 run_loop_.reset(new base::RunLoop);
345 run_loop_->Run();
keybuk@chromium.org359c9b62012-11-27 09:23:25 +0900346
347 // Send the test signal from the exported object.
348 test_service_->SendTestSignal(kMessage);
349 // Receive the signal with the object proxy. The signal is handled in
350 // SignalSenderVerificationTest::OnTestSignal() in the main thread.
351 WaitForTestSignal();
352 ASSERT_EQ(kMessage, test_signal_string_);
353
354 // Release and acquire the name ownership.
355 // latest_name_owner_ should be non empty as |test_service_| owns the name.
356 ASSERT_FALSE(latest_name_owner_.empty());
357 test_service_->ShutdownAndBlock();
358 // OnNameOwnerChanged will PostTask to quit the message loop.
earthdok64401d72014-09-03 19:32:36 +0900359 run_loop_.reset(new base::RunLoop);
360 run_loop_->Run();
keybuk@chromium.org359c9b62012-11-27 09:23:25 +0900361 // latest_name_owner_ should be empty as the owner is gone.
362 ASSERT_TRUE(latest_name_owner_.empty());
363
364 // Reset the flag as NameOwnerChanged is already received in setup.
365 on_name_owner_changed_called_ = false;
366 second_name_owner_changed_called = false;
367 test_service2_->RequestOwnership(
368 base::Bind(&SignalSenderVerificationTest::OnOwnership,
369 base::Unretained(this), true));
370 // Both of OnNameOwnerChanged() and OnOwnership() should quit the MessageLoop,
371 // but there's no expected order of those 2 event.
372 while (!on_name_owner_changed_called_ || !second_name_owner_changed_called ||
earthdok64401d72014-09-03 19:32:36 +0900373 !on_ownership_called_) {
374 run_loop_.reset(new base::RunLoop);
375 run_loop_->Run();
376 }
keybuk@chromium.org359c9b62012-11-27 09:23:25 +0900377 ASSERT_TRUE(on_name_owner_changed_called_);
378 ASSERT_TRUE(second_name_owner_changed_called);
379 ASSERT_TRUE(on_ownership_called_);
380
381 // latest_name_owner_ becomes non empty as the new owner appears.
382 ASSERT_FALSE(latest_name_owner_.empty());
383
384 // Now the second service owns the name.
385 const char kNewMessage[] = "hello, new world";
386
387 test_service2_->SendTestSignal(kNewMessage);
388 WaitForTestSignal();
389 ASSERT_EQ(kNewMessage, test_signal_string_);
390}
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900391
392} // namespace dbus