blob: 8c6ca57819a376b5cb12aa71983ab15344781a07 [file] [log] [blame]
[email protected]8bbe31e2012-10-29 06:27:331// 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
dcheng2a193282016-04-08 22:55:045#include <memory>
6
[email protected]8bbe31e2012-10-29 06:27:337#include "base/bind.h"
Alexander Timin4f9c35c2018-11-01 20:15:208#include "base/message_loop/message_loop.h"
asvitkine12d9cda2017-01-19 15:17:389#include "base/metrics/histogram_macros.h"
[email protected]8bbe31e2012-10-29 06:27:3310#include "base/metrics/histogram_samples.h"
earthdok9562caf2014-09-03 10:32:3611#include "base/run_loop.h"
fdoray6ef45cf2016-08-25 15:36:3712#include "base/single_thread_task_runner.h"
[email protected]8bbe31e2012-10-29 06:27:3313#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
[email protected]2a57ca642013-06-13 06:37:1922namespace dbus {
23
[email protected]8bbe31e2012-10-29 06:27:3324// The test for sender verification in ObjectProxy.
25class SignalSenderVerificationTest : public testing::Test {
26 public:
[email protected]6d36c0c2012-11-14 11:02:5927 SignalSenderVerificationTest()
28 : on_name_owner_changed_called_(false),
29 on_ownership_called_(false) {
[email protected]8bbe31e2012-10-29 06:27:3330 }
31
dcheng3bb7119c2014-12-29 18:30:1732 void SetUp() override {
[email protected]8bbe31e2012-10-29 06:27:3333 // 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;
[email protected]ff33b18e2013-05-01 16:10:3039 thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
[email protected]8bbe31e2012-10-29 06:27:3340 ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options));
41
hashimoto067d84f522016-01-05 08:48:0342 // 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
[email protected]8bbe31e2012-10-29 06:27:3347 // Create the client, using the D-Bus thread.
[email protected]2a57ca642013-06-13 06:37:1948 Bus::Options bus_options;
49 bus_options.bus_type = Bus::SESSION;
50 bus_options.connection_type = Bus::PRIVATE;
skyostil8a033aa2015-06-17 15:46:0451 bus_options.dbus_task_runner = dbus_thread_->task_runner();
[email protected]2a57ca642013-06-13 06:37:1952 bus_ = new Bus(bus_options);
[email protected]8bbe31e2012-10-29 06:27:3353 object_proxy_ = bus_->GetObjectProxy(
hashimoto067d84f522016-01-05 08:48:0354 test_service_->service_name(),
[email protected]2a57ca642013-06-13 06:37:1955 ObjectPath("/org/chromium/TestObject"));
[email protected]8bbe31e2012-10-29 06:27:3356 ASSERT_TRUE(bus_->HasDBusThread());
57
[email protected]6d36c0c2012-11-14 11:02:5958 object_proxy_->SetNameOwnerChangedCallback(
59 base::Bind(&SignalSenderVerificationTest::OnNameOwnerChanged,
[email protected]d7134d42012-11-27 00:23:2560 base::Unretained(this),
61 &on_name_owner_changed_called_));
[email protected]6d36c0c2012-11-14 11:02:5962
[email protected]8bbe31e2012-10-29 06:27:3363 // 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.
earthdok9562caf2014-09-03 10:32:3673 run_loop_.reset(new base::RunLoop);
74 run_loop_->Run();
[email protected]6d36c0c2012-11-14 11:02:5975
hashimoto067d84f522016-01-05 08:48:0376 // Start the test service.
[email protected]6d36c0c2012-11-14 11:02:5977 ASSERT_TRUE(test_service_->StartService());
Ryo Hashimoto3bbeb5c2018-08-13 04:58:3978 test_service_->WaitUntilServiceIsStarted();
[email protected]6d36c0c2012-11-14 11:02:5979 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.
hashimoto067d84f522016-01-05 08:48:0384 options.service_name = test_service_->service_name();
[email protected]2a57ca642013-06-13 06:37:1985 test_service2_.reset(new TestService(options));
[email protected]6d36c0c2012-11-14 11:02:5986 ASSERT_TRUE(test_service2_->StartService());
Ryo Hashimoto3bbeb5c2018-08-13 04:58:3987 test_service2_->WaitUntilServiceIsStarted();
[email protected]6d36c0c2012-11-14 11:02:5988 ASSERT_TRUE(test_service2_->HasDBusThread());
89 ASSERT_FALSE(test_service2_->has_ownership());
90
91 // The name should be owned and known at this point.
earthdok9562caf2014-09-03 10:32:3692 if (!on_name_owner_changed_called_) {
93 run_loop_.reset(new base::RunLoop);
94 run_loop_->Run();
95 }
[email protected]6d36c0c2012-11-14 11:02:5996 ASSERT_FALSE(latest_name_owner_.empty());
[email protected]8bbe31e2012-10-29 06:27:3397 }
98
dcheng3bb7119c2014-12-29 18:30:1799 void TearDown() override {
[email protected]8bbe31e2012-10-29 06:27:33100 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
[email protected]6d36c0c2012-11-14 11:02:59115 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.
fdoray6ef45cf2016-08-25 15:36:37118 message_loop_.task_runner()->PostTask(
[email protected]6d36c0c2012-11-14 11:02:59119 FROM_HERE,
kylechar39c39282019-02-19 19:04:04120 base::BindOnce(&SignalSenderVerificationTest::OnOwnershipInternal,
121 base::Unretained(this)));
[email protected]6d36c0c2012-11-14 11:02:59122 }
123
124 void OnOwnershipInternal() {
125 on_ownership_called_ = true;
earthdok9562caf2014-09-03 10:32:36126 run_loop_->Quit();
[email protected]6d36c0c2012-11-14 11:02:59127 }
128
[email protected]db817802e2013-09-27 07:12:03129 void OnNameOwnerChanged(bool* called_flag,
130 const std::string& old_owner,
131 const std::string& new_owner) {
[email protected]6d36c0c2012-11-14 11:02:59132 latest_name_owner_ = new_owner;
[email protected]d7134d42012-11-27 00:23:25133 *called_flag = true;
earthdok9562caf2014-09-03 10:32:36134 run_loop_->Quit();
[email protected]6d36c0c2012-11-14 11:02:59135 }
136
[email protected]8bbe31e2012-10-29 06:27:33137 // Called when the "Test" signal is received, in the main thread.
138 // Copy the string payload to |test_signal_string_|.
[email protected]2a57ca642013-06-13 06:37:19139 void OnTestSignal(Signal* signal) {
140 MessageReader reader(signal);
[email protected]8bbe31e2012-10-29 06:27:33141 ASSERT_TRUE(reader.PopString(&test_signal_string_));
earthdok9562caf2014-09-03 10:32:36142 run_loop_->Quit();
[email protected]8bbe31e2012-10-29 06:27:33143 }
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);
earthdok9562caf2014-09-03 10:32:36150 run_loop_->Quit();
[email protected]8bbe31e2012-10-29 06:27:33151 }
152
[email protected]d7134d42012-11-27 00:23:25153 protected:
[email protected]8bbe31e2012-10-29 06:27:33154 // Wait for the hey signal to be received.
155 void WaitForTestSignal() {
156 // OnTestSignal() will quit the message loop.
earthdok9562caf2014-09-03 10:32:36157 run_loop_.reset(new base::RunLoop);
158 run_loop_->Run();
[email protected]8bbe31e2012-10-29 06:27:33159 }
160
[email protected]e2824902013-07-31 06:34:59161 // 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
[email protected]ff33b18e2013-05-01 16:10:30169 base::MessageLoop message_loop_;
dcheng2a193282016-04-08 22:55:04170 std::unique_ptr<base::RunLoop> run_loop_;
171 std::unique_ptr<base::Thread> dbus_thread_;
[email protected]2a57ca642013-06-13 06:37:19172 scoped_refptr<Bus> bus_;
173 ObjectProxy* object_proxy_;
dcheng2a193282016-04-08 22:55:04174 std::unique_ptr<TestService> test_service_;
175 std::unique_ptr<TestService> test_service2_;
[email protected]8bbe31e2012-10-29 06:27:33176 // Text message from "Test" signal.
177 std::string test_signal_string_;
[email protected]6d36c0c2012-11-14 11:02:59178
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_;
[email protected]8bbe31e2012-10-29 06:27:33185};
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
hashimoto72254d512017-04-25 07:13:31197TEST_F(SignalSenderVerificationTest, TestSignalRejected) {
[email protected]8bbe31e2012-10-29 06:27:33198 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.
hashimoto72254d512017-04-25 07:13:31203 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout());
[email protected]8bbe31e2012-10-29 06:27:33204
205 ASSERT_EQ("", test_signal_string_);
[email protected]8bbe31e2012-10-29 06:27:33206}
207
Lei Zhang53986532018-04-24 20:51:22208// Flaky. https://crbug.com/785555
209TEST_F(SignalSenderVerificationTest, DISABLED_TestOwnerChanged) {
[email protected]8bbe31e2012-10-29 06:27:33210 const char kMessage[] = "hello, world";
211
212 // Send the test signal from the exported object.
213 test_service_->SendTestSignal(kMessage);
214 // Receive the signal with the object proxy. The signal is handled in
215 // SignalSenderVerificationTest::OnTestSignal() in the main thread.
216 WaitForTestSignal();
217 ASSERT_EQ(kMessage, test_signal_string_);
218
[email protected]6d36c0c2012-11-14 11:02:59219 // Release and acquire the name ownership.
220 // latest_name_owner_ should be non empty as |test_service_| owns the name.
221 ASSERT_FALSE(latest_name_owner_.empty());
[email protected]8bbe31e2012-10-29 06:27:33222 test_service_->ShutdownAndBlock();
[email protected]6d36c0c2012-11-14 11:02:59223 // OnNameOwnerChanged will PostTask to quit the message loop.
earthdok9562caf2014-09-03 10:32:36224 run_loop_.reset(new base::RunLoop);
225 run_loop_->Run();
[email protected]6d36c0c2012-11-14 11:02:59226 // latest_name_owner_ should be empty as the owner is gone.
227 ASSERT_TRUE(latest_name_owner_.empty());
228
229 // Reset the flag as NameOwnerChanged is already received in setup.
230 on_name_owner_changed_called_ = false;
[email protected]e2824902013-07-31 06:34:59231 on_ownership_called_ = false;
[email protected]6d36c0c2012-11-14 11:02:59232 test_service2_->RequestOwnership(
233 base::Bind(&SignalSenderVerificationTest::OnOwnership,
234 base::Unretained(this), true));
235 // Both of OnNameOwnerChanged() and OnOwnership() should quit the MessageLoop,
236 // but there's no expected order of those 2 event.
earthdok9562caf2014-09-03 10:32:36237 run_loop_.reset(new base::RunLoop);
238 run_loop_->Run();
239 if (!on_name_owner_changed_called_ || !on_ownership_called_) {
240 run_loop_.reset(new base::RunLoop);
241 run_loop_->Run();
242 }
[email protected]6d36c0c2012-11-14 11:02:59243 ASSERT_TRUE(on_name_owner_changed_called_);
244 ASSERT_TRUE(on_ownership_called_);
245
246 // latest_name_owner_ becomes non empty as the new owner appears.
247 ASSERT_FALSE(latest_name_owner_.empty());
[email protected]8bbe31e2012-10-29 06:27:33248
249 // Now the second service owns the name.
250 const char kNewMessage[] = "hello, new world";
251
252 test_service2_->SendTestSignal(kNewMessage);
253 WaitForTestSignal();
254 ASSERT_EQ(kNewMessage, test_signal_string_);
255}
[email protected]d7134d42012-11-27 00:23:25256
Lei Zhang53986532018-04-24 20:51:22257// Flaky. https://crbug.com/785555
258TEST_F(SignalSenderVerificationTest, DISABLED_TestOwnerStealing) {
[email protected]e2824902013-07-31 06:34:59259 // Release and acquire the name ownership.
260 // latest_name_owner_ should be non empty as |test_service_| owns the name.
261 ASSERT_FALSE(latest_name_owner_.empty());
262 test_service_->ShutdownAndBlock();
263 // OnNameOwnerChanged will PostTask to quit the message loop.
earthdok9562caf2014-09-03 10:32:36264 run_loop_.reset(new base::RunLoop);
265 run_loop_->Run();
[email protected]e2824902013-07-31 06:34:59266 // latest_name_owner_ should be empty as the owner is gone.
267 ASSERT_TRUE(latest_name_owner_.empty());
268 // Reset the flag as NameOwnerChanged is already received in setup.
269 on_name_owner_changed_called_ = false;
270
271 // Start a test service that allows theft, using the D-Bus thread.
272 TestService::Options options;
skyostil8a033aa2015-06-17 15:46:04273 options.dbus_task_runner = dbus_thread_->task_runner();
[email protected]e2824902013-07-31 06:34:59274 options.request_ownership_options = Bus::REQUIRE_PRIMARY_ALLOW_REPLACEMENT;
hashimoto067d84f522016-01-05 08:48:03275 options.service_name = test_service_->service_name();
[email protected]e2824902013-07-31 06:34:59276 TestService stealable_test_service(options);
277 ASSERT_TRUE(stealable_test_service.StartService());
Ryo Hashimoto3bbeb5c2018-08-13 04:58:39278 stealable_test_service.WaitUntilServiceIsStarted();
[email protected]e2824902013-07-31 06:34:59279 ASSERT_TRUE(stealable_test_service.HasDBusThread());
280 ASSERT_TRUE(stealable_test_service.has_ownership());
281
282 // OnNameOwnerChanged will PostTask to quit the message loop.
earthdok9562caf2014-09-03 10:32:36283 run_loop_.reset(new base::RunLoop);
284 run_loop_->Run();
[email protected]e2824902013-07-31 06:34:59285
286 // Send a signal to check that the service is correctly owned.
287 const char kMessage[] = "hello, world";
288
289 // Send the test signal from the exported object.
290 stealable_test_service.SendTestSignal(kMessage);
291 // Receive the signal with the object proxy. The signal is handled in
292 // SignalSenderVerificationTest::OnTestSignal() in the main thread.
293 WaitForTestSignal();
294 ASSERT_EQ(kMessage, test_signal_string_);
295
296 // Reset the flag as NameOwnerChanged was called above.
297 on_name_owner_changed_called_ = false;
298 test_service2_->RequestOwnership(
299 base::Bind(&SignalSenderVerificationTest::OnOwnership,
300 base::Unretained(this), true));
301 // Both of OnNameOwnerChanged() and OnOwnership() should quit the MessageLoop,
302 // but there's no expected order of those 2 event.
earthdok9562caf2014-09-03 10:32:36303 run_loop_.reset(new base::RunLoop);
304 run_loop_->Run();
305 if (!on_name_owner_changed_called_ || !on_ownership_called_) {
306 run_loop_.reset(new base::RunLoop);
307 run_loop_->Run();
308 }
[email protected]e2824902013-07-31 06:34:59309 ASSERT_TRUE(on_name_owner_changed_called_);
310 ASSERT_TRUE(on_ownership_called_);
311
312 // Now the second service owns the name.
313 const char kNewMessage[] = "hello, new world";
314
315 test_service2_->SendTestSignal(kNewMessage);
316 WaitForTestSignal();
317 ASSERT_EQ(kNewMessage, test_signal_string_);
318
319 SafeServiceStop(&stealable_test_service);
320}
321
[email protected]b27cb742012-11-27 01:01:28322// Fails on Linux ChromiumOS Tests
323TEST_F(SignalSenderVerificationTest, DISABLED_TestMultipleObjects) {
[email protected]d7134d42012-11-27 00:23:25324 const char kMessage[] = "hello, world";
325
[email protected]2a57ca642013-06-13 06:37:19326 ObjectProxy* object_proxy2 = bus_->GetObjectProxy(
hashimoto067d84f522016-01-05 08:48:03327 test_service_->service_name(),
[email protected]2a57ca642013-06-13 06:37:19328 ObjectPath("/org/chromium/DifferentObject"));
[email protected]d7134d42012-11-27 00:23:25329
330 bool second_name_owner_changed_called = false;
331 object_proxy2->SetNameOwnerChangedCallback(
332 base::Bind(&SignalSenderVerificationTest::OnNameOwnerChanged,
333 base::Unretained(this),
334 &second_name_owner_changed_called));
335
336 // Connect to a signal on the additional remote object to trigger the
337 // name owner matching.
338 object_proxy2->ConnectToSignal(
339 "org.chromium.DifferentTestInterface",
340 "Test",
341 base::Bind(&SignalSenderVerificationTest::OnTestSignal,
342 base::Unretained(this)),
343 base::Bind(&SignalSenderVerificationTest::OnConnected,
344 base::Unretained(this)));
345 // Wait until the object proxy is connected to the signal.
earthdok9562caf2014-09-03 10:32:36346 run_loop_.reset(new base::RunLoop);
347 run_loop_->Run();
[email protected]d7134d42012-11-27 00:23:25348
349 // Send the test signal from the exported object.
350 test_service_->SendTestSignal(kMessage);
351 // Receive the signal with the object proxy. The signal is handled in
352 // SignalSenderVerificationTest::OnTestSignal() in the main thread.
353 WaitForTestSignal();
354 ASSERT_EQ(kMessage, test_signal_string_);
355
356 // Release and acquire the name ownership.
357 // latest_name_owner_ should be non empty as |test_service_| owns the name.
358 ASSERT_FALSE(latest_name_owner_.empty());
359 test_service_->ShutdownAndBlock();
360 // OnNameOwnerChanged will PostTask to quit the message loop.
earthdok9562caf2014-09-03 10:32:36361 run_loop_.reset(new base::RunLoop);
362 run_loop_->Run();
[email protected]d7134d42012-11-27 00:23:25363 // latest_name_owner_ should be empty as the owner is gone.
364 ASSERT_TRUE(latest_name_owner_.empty());
365
366 // Reset the flag as NameOwnerChanged is already received in setup.
367 on_name_owner_changed_called_ = false;
368 second_name_owner_changed_called = false;
369 test_service2_->RequestOwnership(
370 base::Bind(&SignalSenderVerificationTest::OnOwnership,
371 base::Unretained(this), true));
372 // Both of OnNameOwnerChanged() and OnOwnership() should quit the MessageLoop,
373 // but there's no expected order of those 2 event.
374 while (!on_name_owner_changed_called_ || !second_name_owner_changed_called ||
earthdok9562caf2014-09-03 10:32:36375 !on_ownership_called_) {
376 run_loop_.reset(new base::RunLoop);
377 run_loop_->Run();
378 }
[email protected]d7134d42012-11-27 00:23:25379 ASSERT_TRUE(on_name_owner_changed_called_);
380 ASSERT_TRUE(second_name_owner_changed_called);
381 ASSERT_TRUE(on_ownership_called_);
382
383 // latest_name_owner_ becomes non empty as the new owner appears.
384 ASSERT_FALSE(latest_name_owner_.empty());
385
386 // Now the second service owns the name.
387 const char kNewMessage[] = "hello, new world";
388
389 test_service2_->SendTestSignal(kNewMessage);
390 WaitForTestSignal();
391 ASSERT_EQ(kNewMessage, test_signal_string_);
392}
[email protected]2a57ca642013-06-13 06:37:19393
394} // namespace dbus