blob: 9eae3a5638cbe5e0ac266ae2adb7d003fe06b36d [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
5#include "base/bind.h"
6#include "base/memory/scoped_ptr.h"
avi@chromium.orga29af562013-07-18 08:00:30 +09007#include "base/message_loop/message_loop.h"
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +09008#include "base/metrics/histogram.h"
9#include "base/metrics/histogram_samples.h"
10#include "base/metrics/statistics_recorder.h"
11#include "base/test/test_timeouts.h"
12#include "base/threading/platform_thread.h"
13#include "base/threading/thread_restrictions.h"
14#include "dbus/bus.h"
15#include "dbus/message.h"
16#include "dbus/object_proxy.h"
17#include "dbus/test_service.h"
18#include "testing/gtest/include/gtest/gtest.h"
19
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090020namespace dbus {
21
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +090022// The test for sender verification in ObjectProxy.
23class SignalSenderVerificationTest : public testing::Test {
24 public:
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +090025 SignalSenderVerificationTest()
26 : on_name_owner_changed_called_(false),
27 on_ownership_called_(false) {
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +090028 }
29
30 virtual void SetUp() {
31 base::StatisticsRecorder::Initialize();
32
33 // 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
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +090042 // Create the client, using the D-Bus thread.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090043 Bus::Options bus_options;
44 bus_options.bus_type = Bus::SESSION;
45 bus_options.connection_type = Bus::PRIVATE;
thestig@chromium.org074b1db2013-02-20 10:36:53 +090046 bus_options.dbus_task_runner = dbus_thread_->message_loop_proxy();
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090047 bus_ = new Bus(bus_options);
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +090048 object_proxy_ = bus_->GetObjectProxy(
49 "org.chromium.TestService",
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090050 ObjectPath("/org/chromium/TestObject"));
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +090051 ASSERT_TRUE(bus_->HasDBusThread());
52
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +090053 object_proxy_->SetNameOwnerChangedCallback(
54 base::Bind(&SignalSenderVerificationTest::OnNameOwnerChanged,
keybuk@chromium.org359c9b62012-11-27 09:23:25 +090055 base::Unretained(this),
56 &on_name_owner_changed_called_));
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +090057
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +090058 // Connect to the "Test" signal of "org.chromium.TestInterface" from
59 // the remote object.
60 object_proxy_->ConnectToSignal(
61 "org.chromium.TestInterface",
62 "Test",
63 base::Bind(&SignalSenderVerificationTest::OnTestSignal,
64 base::Unretained(this)),
65 base::Bind(&SignalSenderVerificationTest::OnConnected,
66 base::Unretained(this)));
67 // Wait until the object proxy is connected to the signal.
68 message_loop_.Run();
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +090069
70 // Start the test service, using the D-Bus thread.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090071 TestService::Options options;
thestig@chromium.org074b1db2013-02-20 10:36:53 +090072 options.dbus_task_runner = dbus_thread_->message_loop_proxy();
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090073 test_service_.reset(new TestService(options));
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +090074 ASSERT_TRUE(test_service_->StartService());
75 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted());
76 ASSERT_TRUE(test_service_->HasDBusThread());
77 ASSERT_TRUE(test_service_->has_ownership());
78
79 // Same setup for the second TestService. This service should not have the
80 // ownership of the name at this point.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090081 test_service2_.reset(new TestService(options));
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +090082 ASSERT_TRUE(test_service2_->StartService());
83 ASSERT_TRUE(test_service2_->WaitUntilServiceIsStarted());
84 ASSERT_TRUE(test_service2_->HasDBusThread());
85 ASSERT_FALSE(test_service2_->has_ownership());
86
87 // The name should be owned and known at this point.
88 if (!on_name_owner_changed_called_)
89 message_loop_.Run();
90 ASSERT_FALSE(latest_name_owner_.empty());
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +090091 }
92
93 virtual void TearDown() {
94 bus_->ShutdownOnDBusThreadAndBlock();
95
96 // Shut down the service.
97 test_service_->ShutdownAndBlock();
98 test_service2_->ShutdownAndBlock();
99
100 // Reset to the default.
101 base::ThreadRestrictions::SetIOAllowed(true);
102
103 // Stopping a thread is considered an IO operation, so do this after
104 // allowing IO.
105 test_service_->Stop();
106 test_service2_->Stop();
107 }
108
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +0900109 void OnOwnership(bool expected, bool success) {
110 ASSERT_EQ(expected, success);
111 // PostTask to quit the MessageLoop as this is called from D-Bus thread.
112 message_loop_.PostTask(
113 FROM_HERE,
114 base::Bind(&SignalSenderVerificationTest::OnOwnershipInternal,
115 base::Unretained(this)));
116 }
117
118 void OnOwnershipInternal() {
119 on_ownership_called_ = true;
120 message_loop_.Quit();
121 }
122
hashimoto@chromium.org4f3851c2013-09-27 16:12:03 +0900123 void OnNameOwnerChanged(bool* called_flag,
124 const std::string& old_owner,
125 const std::string& new_owner) {
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +0900126 latest_name_owner_ = new_owner;
keybuk@chromium.org359c9b62012-11-27 09:23:25 +0900127 *called_flag = true;
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +0900128 message_loop_.Quit();
129 }
130
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +0900131 // Called when the "Test" signal is received, in the main thread.
132 // Copy the string payload to |test_signal_string_|.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900133 void OnTestSignal(Signal* signal) {
134 MessageReader reader(signal);
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +0900135 ASSERT_TRUE(reader.PopString(&test_signal_string_));
136 message_loop_.Quit();
137 }
138
139 // Called when connected to the signal.
140 void OnConnected(const std::string& interface_name,
141 const std::string& signal_name,
142 bool success) {
143 ASSERT_TRUE(success);
144 message_loop_.Quit();
145 }
146
keybuk@chromium.org359c9b62012-11-27 09:23:25 +0900147 protected:
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +0900148 // Wait for the hey signal to be received.
149 void WaitForTestSignal() {
150 // OnTestSignal() will quit the message loop.
151 message_loop_.Run();
152 }
153
cmasone@chromium.org989857e2013-07-31 15:34:59 +0900154 // Stopping a thread is considered an IO operation, so we need to fiddle with
155 // thread restrictions before and after calling Stop() on a TestService.
156 void SafeServiceStop(TestService* test_service) {
157 base::ThreadRestrictions::SetIOAllowed(true);
158 test_service->Stop();
159 base::ThreadRestrictions::SetIOAllowed(false);
160 }
161
xhwang@chromium.orgdff6b132013-05-02 01:10:30 +0900162 base::MessageLoop message_loop_;
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +0900163 scoped_ptr<base::Thread> dbus_thread_;
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900164 scoped_refptr<Bus> bus_;
165 ObjectProxy* object_proxy_;
166 scoped_ptr<TestService> test_service_;
167 scoped_ptr<TestService> test_service2_;
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +0900168 // Text message from "Test" signal.
169 std::string test_signal_string_;
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +0900170
171 // The known latest name owner of TestService. Updated in OnNameOwnerChanged.
172 std::string latest_name_owner_;
173
174 // Boolean flags to record callback calls.
175 bool on_name_owner_changed_called_;
176 bool on_ownership_called_;
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +0900177};
178
179TEST_F(SignalSenderVerificationTest, TestSignalAccepted) {
180 const char kMessage[] = "hello, world";
181 // Send the test signal from the exported object.
182 test_service_->SendTestSignal(kMessage);
183 // Receive the signal with the object proxy. The signal is handled in
184 // SignalSenderVerificationTest::OnTestSignal() in the main thread.
185 WaitForTestSignal();
186 ASSERT_EQ(kMessage, test_signal_string_);
187}
188
Paweł Hajdan, Jr9f305522014-08-25 20:18:30 +0900189// Disabled, http://crbug.com/407063 .
190TEST_F(SignalSenderVerificationTest, DISABLED_TestSignalRejected) {
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +0900191 // To make sure the histogram instance is created.
192 UMA_HISTOGRAM_COUNTS("DBus.RejectedSignalCount", 0);
kaiwang@chromium.org7881e422013-03-01 12:53:25 +0900193 base::HistogramBase* reject_signal_histogram =
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +0900194 base::StatisticsRecorder::FindHistogram("DBus.RejectedSignalCount");
195 scoped_ptr<base::HistogramSamples> samples1(
196 reject_signal_histogram->SnapshotSamples());
197
198 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.
203 base::PlatformThread::Sleep(TestTimeouts::action_timeout());
204
205 scoped_ptr<base::HistogramSamples> samples2(
206 reject_signal_histogram->SnapshotSamples());
207
208 ASSERT_EQ("", test_signal_string_);
209 EXPECT_EQ(samples1->TotalCount() + 1, samples2->TotalCount());
210}
211
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +0900212TEST_F(SignalSenderVerificationTest, TestOwnerChanged) {
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +0900213 const char kMessage[] = "hello, world";
214
215 // Send the test signal from the exported object.
216 test_service_->SendTestSignal(kMessage);
217 // Receive the signal with the object proxy. The signal is handled in
218 // SignalSenderVerificationTest::OnTestSignal() in the main thread.
219 WaitForTestSignal();
220 ASSERT_EQ(kMessage, test_signal_string_);
221
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +0900222 // Release and acquire the name ownership.
223 // latest_name_owner_ should be non empty as |test_service_| owns the name.
224 ASSERT_FALSE(latest_name_owner_.empty());
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +0900225 test_service_->ShutdownAndBlock();
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +0900226 // OnNameOwnerChanged will PostTask to quit the message loop.
227 message_loop_.Run();
228 // latest_name_owner_ should be empty as the owner is gone.
229 ASSERT_TRUE(latest_name_owner_.empty());
230
231 // Reset the flag as NameOwnerChanged is already received in setup.
232 on_name_owner_changed_called_ = false;
cmasone@chromium.org989857e2013-07-31 15:34:59 +0900233 on_ownership_called_ = false;
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +0900234 test_service2_->RequestOwnership(
235 base::Bind(&SignalSenderVerificationTest::OnOwnership,
236 base::Unretained(this), true));
237 // Both of OnNameOwnerChanged() and OnOwnership() should quit the MessageLoop,
238 // but there's no expected order of those 2 event.
239 message_loop_.Run();
240 if (!on_name_owner_changed_called_ || !on_ownership_called_)
241 message_loop_.Run();
242 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.
262 message_loop_.Run();
263 // latest_name_owner_ should be empty as the owner is gone.
264 ASSERT_TRUE(latest_name_owner_.empty());
265 // Reset the flag as NameOwnerChanged is already received in setup.
266 on_name_owner_changed_called_ = false;
267
268 // Start a test service that allows theft, using the D-Bus thread.
269 TestService::Options options;
270 options.dbus_task_runner = dbus_thread_->message_loop_proxy();
271 options.request_ownership_options = Bus::REQUIRE_PRIMARY_ALLOW_REPLACEMENT;
272 TestService stealable_test_service(options);
273 ASSERT_TRUE(stealable_test_service.StartService());
274 ASSERT_TRUE(stealable_test_service.WaitUntilServiceIsStarted());
275 ASSERT_TRUE(stealable_test_service.HasDBusThread());
276 ASSERT_TRUE(stealable_test_service.has_ownership());
277
278 // OnNameOwnerChanged will PostTask to quit the message loop.
279 message_loop_.Run();
280
281 // Send a signal to check that the service is correctly owned.
282 const char kMessage[] = "hello, world";
283
284 // Send the test signal from the exported object.
285 stealable_test_service.SendTestSignal(kMessage);
286 // Receive the signal with the object proxy. The signal is handled in
287 // SignalSenderVerificationTest::OnTestSignal() in the main thread.
288 WaitForTestSignal();
289 ASSERT_EQ(kMessage, test_signal_string_);
290
291 // Reset the flag as NameOwnerChanged was called above.
292 on_name_owner_changed_called_ = false;
293 test_service2_->RequestOwnership(
294 base::Bind(&SignalSenderVerificationTest::OnOwnership,
295 base::Unretained(this), true));
296 // Both of OnNameOwnerChanged() and OnOwnership() should quit the MessageLoop,
297 // but there's no expected order of those 2 event.
298 message_loop_.Run();
299 if (!on_name_owner_changed_called_ || !on_ownership_called_)
300 message_loop_.Run();
301 ASSERT_TRUE(on_name_owner_changed_called_);
302 ASSERT_TRUE(on_ownership_called_);
303
304 // Now the second service owns the name.
305 const char kNewMessage[] = "hello, new world";
306
307 test_service2_->SendTestSignal(kNewMessage);
308 WaitForTestSignal();
309 ASSERT_EQ(kNewMessage, test_signal_string_);
310
311 SafeServiceStop(&stealable_test_service);
312}
313
keybuk@chromium.org7adf6a32012-11-27 10:01:28 +0900314// Fails on Linux ChromiumOS Tests
315TEST_F(SignalSenderVerificationTest, DISABLED_TestMultipleObjects) {
keybuk@chromium.org359c9b62012-11-27 09:23:25 +0900316 const char kMessage[] = "hello, world";
317
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900318 ObjectProxy* object_proxy2 = bus_->GetObjectProxy(
keybuk@chromium.org359c9b62012-11-27 09:23:25 +0900319 "org.chromium.TestService",
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900320 ObjectPath("/org/chromium/DifferentObject"));
keybuk@chromium.org359c9b62012-11-27 09:23:25 +0900321
322 bool second_name_owner_changed_called = false;
323 object_proxy2->SetNameOwnerChangedCallback(
324 base::Bind(&SignalSenderVerificationTest::OnNameOwnerChanged,
325 base::Unretained(this),
326 &second_name_owner_changed_called));
327
328 // Connect to a signal on the additional remote object to trigger the
329 // name owner matching.
330 object_proxy2->ConnectToSignal(
331 "org.chromium.DifferentTestInterface",
332 "Test",
333 base::Bind(&SignalSenderVerificationTest::OnTestSignal,
334 base::Unretained(this)),
335 base::Bind(&SignalSenderVerificationTest::OnConnected,
336 base::Unretained(this)));
337 // Wait until the object proxy is connected to the signal.
338 message_loop_.Run();
339
340 // Send the test signal from the exported object.
341 test_service_->SendTestSignal(kMessage);
342 // Receive the signal with the object proxy. The signal is handled in
343 // SignalSenderVerificationTest::OnTestSignal() in the main thread.
344 WaitForTestSignal();
345 ASSERT_EQ(kMessage, test_signal_string_);
346
347 // Release and acquire the name ownership.
348 // latest_name_owner_ should be non empty as |test_service_| owns the name.
349 ASSERT_FALSE(latest_name_owner_.empty());
350 test_service_->ShutdownAndBlock();
351 // OnNameOwnerChanged will PostTask to quit the message loop.
352 message_loop_.Run();
353 // latest_name_owner_ should be empty as the owner is gone.
354 ASSERT_TRUE(latest_name_owner_.empty());
355
356 // Reset the flag as NameOwnerChanged is already received in setup.
357 on_name_owner_changed_called_ = false;
358 second_name_owner_changed_called = false;
359 test_service2_->RequestOwnership(
360 base::Bind(&SignalSenderVerificationTest::OnOwnership,
361 base::Unretained(this), true));
362 // Both of OnNameOwnerChanged() and OnOwnership() should quit the MessageLoop,
363 // but there's no expected order of those 2 event.
364 while (!on_name_owner_changed_called_ || !second_name_owner_changed_called ||
365 !on_ownership_called_)
366 message_loop_.Run();
367 ASSERT_TRUE(on_name_owner_changed_called_);
368 ASSERT_TRUE(second_name_owner_changed_called);
369 ASSERT_TRUE(on_ownership_called_);
370
371 // latest_name_owner_ becomes non empty as the new owner appears.
372 ASSERT_FALSE(latest_name_owner_.empty());
373
374 // Now the second service owns the name.
375 const char kNewMessage[] = "hello, new world";
376
377 test_service2_->SendTestSignal(kNewMessage);
378 WaitForTestSignal();
379 ASSERT_EQ(kNewMessage, test_signal_string_);
380}
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900381
382} // namespace dbus