blob: 1d8f6e1d64ebccec446059109a9e67346e3c900c [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
189TEST_F(SignalSenderVerificationTest, TestSignalRejected) {
190 // To make sure the histogram instance is created.
191 UMA_HISTOGRAM_COUNTS("DBus.RejectedSignalCount", 0);
kaiwang@chromium.org7881e422013-03-01 12:53:25 +0900192 base::HistogramBase* reject_signal_histogram =
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +0900193 base::StatisticsRecorder::FindHistogram("DBus.RejectedSignalCount");
194 scoped_ptr<base::HistogramSamples> samples1(
195 reject_signal_histogram->SnapshotSamples());
196
197 const char kNewMessage[] = "hello, new world";
198 test_service2_->SendTestSignal(kNewMessage);
199
200 // This test tests that our callback is NOT called by the ObjectProxy.
201 // Sleep to have message delivered to the client via the D-Bus service.
202 base::PlatformThread::Sleep(TestTimeouts::action_timeout());
203
204 scoped_ptr<base::HistogramSamples> samples2(
205 reject_signal_histogram->SnapshotSamples());
206
207 ASSERT_EQ("", test_signal_string_);
208 EXPECT_EQ(samples1->TotalCount() + 1, samples2->TotalCount());
209}
210
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +0900211TEST_F(SignalSenderVerificationTest, TestOwnerChanged) {
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +0900212 const char kMessage[] = "hello, world";
213
214 // Send the test signal from the exported object.
215 test_service_->SendTestSignal(kMessage);
216 // Receive the signal with the object proxy. The signal is handled in
217 // SignalSenderVerificationTest::OnTestSignal() in the main thread.
218 WaitForTestSignal();
219 ASSERT_EQ(kMessage, test_signal_string_);
220
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +0900221 // Release and acquire the name ownership.
222 // latest_name_owner_ should be non empty as |test_service_| owns the name.
223 ASSERT_FALSE(latest_name_owner_.empty());
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +0900224 test_service_->ShutdownAndBlock();
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +0900225 // OnNameOwnerChanged will PostTask to quit the message loop.
226 message_loop_.Run();
227 // latest_name_owner_ should be empty as the owner is gone.
228 ASSERT_TRUE(latest_name_owner_.empty());
229
230 // Reset the flag as NameOwnerChanged is already received in setup.
231 on_name_owner_changed_called_ = false;
cmasone@chromium.org989857e2013-07-31 15:34:59 +0900232 on_ownership_called_ = false;
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +0900233 test_service2_->RequestOwnership(
234 base::Bind(&SignalSenderVerificationTest::OnOwnership,
235 base::Unretained(this), true));
236 // Both of OnNameOwnerChanged() and OnOwnership() should quit the MessageLoop,
237 // but there's no expected order of those 2 event.
238 message_loop_.Run();
239 if (!on_name_owner_changed_called_ || !on_ownership_called_)
240 message_loop_.Run();
241 ASSERT_TRUE(on_name_owner_changed_called_);
242 ASSERT_TRUE(on_ownership_called_);
243
244 // latest_name_owner_ becomes non empty as the new owner appears.
245 ASSERT_FALSE(latest_name_owner_.empty());
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +0900246
247 // Now the second service owns the name.
248 const char kNewMessage[] = "hello, new world";
249
250 test_service2_->SendTestSignal(kNewMessage);
251 WaitForTestSignal();
252 ASSERT_EQ(kNewMessage, test_signal_string_);
253}
keybuk@chromium.org359c9b62012-11-27 09:23:25 +0900254
cmasone@chromium.org989857e2013-07-31 15:34:59 +0900255TEST_F(SignalSenderVerificationTest, TestOwnerStealing) {
256 // Release and acquire the name ownership.
257 // latest_name_owner_ should be non empty as |test_service_| owns the name.
258 ASSERT_FALSE(latest_name_owner_.empty());
259 test_service_->ShutdownAndBlock();
260 // OnNameOwnerChanged will PostTask to quit the message loop.
261 message_loop_.Run();
262 // latest_name_owner_ should be empty as the owner is gone.
263 ASSERT_TRUE(latest_name_owner_.empty());
264 // Reset the flag as NameOwnerChanged is already received in setup.
265 on_name_owner_changed_called_ = false;
266
267 // Start a test service that allows theft, using the D-Bus thread.
268 TestService::Options options;
269 options.dbus_task_runner = dbus_thread_->message_loop_proxy();
270 options.request_ownership_options = Bus::REQUIRE_PRIMARY_ALLOW_REPLACEMENT;
271 TestService stealable_test_service(options);
272 ASSERT_TRUE(stealable_test_service.StartService());
273 ASSERT_TRUE(stealable_test_service.WaitUntilServiceIsStarted());
274 ASSERT_TRUE(stealable_test_service.HasDBusThread());
275 ASSERT_TRUE(stealable_test_service.has_ownership());
276
277 // OnNameOwnerChanged will PostTask to quit the message loop.
278 message_loop_.Run();
279
280 // Send a signal to check that the service is correctly owned.
281 const char kMessage[] = "hello, world";
282
283 // Send the test signal from the exported object.
284 stealable_test_service.SendTestSignal(kMessage);
285 // Receive the signal with the object proxy. The signal is handled in
286 // SignalSenderVerificationTest::OnTestSignal() in the main thread.
287 WaitForTestSignal();
288 ASSERT_EQ(kMessage, test_signal_string_);
289
290 // Reset the flag as NameOwnerChanged was called above.
291 on_name_owner_changed_called_ = false;
292 test_service2_->RequestOwnership(
293 base::Bind(&SignalSenderVerificationTest::OnOwnership,
294 base::Unretained(this), true));
295 // Both of OnNameOwnerChanged() and OnOwnership() should quit the MessageLoop,
296 // but there's no expected order of those 2 event.
297 message_loop_.Run();
298 if (!on_name_owner_changed_called_ || !on_ownership_called_)
299 message_loop_.Run();
300 ASSERT_TRUE(on_name_owner_changed_called_);
301 ASSERT_TRUE(on_ownership_called_);
302
303 // Now the second service owns the name.
304 const char kNewMessage[] = "hello, new world";
305
306 test_service2_->SendTestSignal(kNewMessage);
307 WaitForTestSignal();
308 ASSERT_EQ(kNewMessage, test_signal_string_);
309
310 SafeServiceStop(&stealable_test_service);
311}
312
keybuk@chromium.org7adf6a32012-11-27 10:01:28 +0900313// Fails on Linux ChromiumOS Tests
314TEST_F(SignalSenderVerificationTest, DISABLED_TestMultipleObjects) {
keybuk@chromium.org359c9b62012-11-27 09:23:25 +0900315 const char kMessage[] = "hello, world";
316
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900317 ObjectProxy* object_proxy2 = bus_->GetObjectProxy(
keybuk@chromium.org359c9b62012-11-27 09:23:25 +0900318 "org.chromium.TestService",
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900319 ObjectPath("/org/chromium/DifferentObject"));
keybuk@chromium.org359c9b62012-11-27 09:23:25 +0900320
321 bool second_name_owner_changed_called = false;
322 object_proxy2->SetNameOwnerChangedCallback(
323 base::Bind(&SignalSenderVerificationTest::OnNameOwnerChanged,
324 base::Unretained(this),
325 &second_name_owner_changed_called));
326
327 // Connect to a signal on the additional remote object to trigger the
328 // name owner matching.
329 object_proxy2->ConnectToSignal(
330 "org.chromium.DifferentTestInterface",
331 "Test",
332 base::Bind(&SignalSenderVerificationTest::OnTestSignal,
333 base::Unretained(this)),
334 base::Bind(&SignalSenderVerificationTest::OnConnected,
335 base::Unretained(this)));
336 // Wait until the object proxy is connected to the signal.
337 message_loop_.Run();
338
339 // Send the test signal from the exported object.
340 test_service_->SendTestSignal(kMessage);
341 // Receive the signal with the object proxy. The signal is handled in
342 // SignalSenderVerificationTest::OnTestSignal() in the main thread.
343 WaitForTestSignal();
344 ASSERT_EQ(kMessage, test_signal_string_);
345
346 // Release and acquire the name ownership.
347 // latest_name_owner_ should be non empty as |test_service_| owns the name.
348 ASSERT_FALSE(latest_name_owner_.empty());
349 test_service_->ShutdownAndBlock();
350 // OnNameOwnerChanged will PostTask to quit the message loop.
351 message_loop_.Run();
352 // latest_name_owner_ should be empty as the owner is gone.
353 ASSERT_TRUE(latest_name_owner_.empty());
354
355 // Reset the flag as NameOwnerChanged is already received in setup.
356 on_name_owner_changed_called_ = false;
357 second_name_owner_changed_called = false;
358 test_service2_->RequestOwnership(
359 base::Bind(&SignalSenderVerificationTest::OnOwnership,
360 base::Unretained(this), true));
361 // Both of OnNameOwnerChanged() and OnOwnership() should quit the MessageLoop,
362 // but there's no expected order of those 2 event.
363 while (!on_name_owner_changed_called_ || !second_name_owner_changed_called ||
364 !on_ownership_called_)
365 message_loop_.Run();
366 ASSERT_TRUE(on_name_owner_changed_called_);
367 ASSERT_TRUE(second_name_owner_changed_called);
368 ASSERT_TRUE(on_ownership_called_);
369
370 // latest_name_owner_ becomes non empty as the new owner appears.
371 ASSERT_FALSE(latest_name_owner_.empty());
372
373 // Now the second service owns the name.
374 const char kNewMessage[] = "hello, new world";
375
376 test_service2_->SendTestSignal(kNewMessage);
377 WaitForTestSignal();
378 ASSERT_EQ(kNewMessage, test_signal_string_);
379}
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900380
381} // namespace dbus