1 // 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.
6 #include "base/memory/scoped_ptr.h"
7 #include "base/message_loop/message_loop.h"
8 #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"
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"
22 // The test for sender verification in ObjectProxy.
23 class SignalSenderVerificationTest
: public testing::Test
{
25 SignalSenderVerificationTest()
26 : on_name_owner_changed_called_(false),
27 on_ownership_called_(false) {
30 virtual void SetUp() {
31 base::StatisticsRecorder::Initialize();
33 // Make the main thread not to allow IO.
34 base::ThreadRestrictions::SetIOAllowed(false);
36 // Start the D-Bus thread.
37 dbus_thread_
.reset(new base::Thread("D-Bus Thread"));
38 base::Thread::Options thread_options
;
39 thread_options
.message_loop_type
= base::MessageLoop::TYPE_IO
;
40 ASSERT_TRUE(dbus_thread_
->StartWithOptions(thread_options
));
42 // Create the client, using the D-Bus thread.
43 Bus::Options bus_options
;
44 bus_options
.bus_type
= Bus::SESSION
;
45 bus_options
.connection_type
= Bus::PRIVATE
;
46 bus_options
.dbus_task_runner
= dbus_thread_
->message_loop_proxy();
47 bus_
= new Bus(bus_options
);
48 object_proxy_
= bus_
->GetObjectProxy(
49 "org.chromium.TestService",
50 ObjectPath("/org/chromium/TestObject"));
51 ASSERT_TRUE(bus_
->HasDBusThread());
53 object_proxy_
->SetNameOwnerChangedCallback(
54 base::Bind(&SignalSenderVerificationTest::OnNameOwnerChanged
,
55 base::Unretained(this),
56 &on_name_owner_changed_called_
));
58 // Connect to the "Test" signal of "org.chromium.TestInterface" from
60 object_proxy_
->ConnectToSignal(
61 "org.chromium.TestInterface",
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.
70 // Start the test service, using the D-Bus thread.
71 TestService::Options options
;
72 options
.dbus_task_runner
= dbus_thread_
->message_loop_proxy();
73 test_service_
.reset(new TestService(options
));
74 ASSERT_TRUE(test_service_
->StartService());
75 ASSERT_TRUE(test_service_
->WaitUntilServiceIsStarted());
76 ASSERT_TRUE(test_service_
->HasDBusThread());
77 ASSERT_TRUE(test_service_
->has_ownership());
79 // Same setup for the second TestService. This service should not have the
80 // ownership of the name at this point.
81 test_service2_
.reset(new TestService(options
));
82 ASSERT_TRUE(test_service2_
->StartService());
83 ASSERT_TRUE(test_service2_
->WaitUntilServiceIsStarted());
84 ASSERT_TRUE(test_service2_
->HasDBusThread());
85 ASSERT_FALSE(test_service2_
->has_ownership());
87 // The name should be owned and known at this point.
88 if (!on_name_owner_changed_called_
)
90 ASSERT_FALSE(latest_name_owner_
.empty());
93 virtual void TearDown() {
94 bus_
->ShutdownOnDBusThreadAndBlock();
96 // Shut down the service.
97 test_service_
->ShutdownAndBlock();
98 test_service2_
->ShutdownAndBlock();
100 // Reset to the default.
101 base::ThreadRestrictions::SetIOAllowed(true);
103 // Stopping a thread is considered an IO operation, so do this after
105 test_service_
->Stop();
106 test_service2_
->Stop();
109 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(
114 base::Bind(&SignalSenderVerificationTest::OnOwnershipInternal
,
115 base::Unretained(this)));
118 void OnOwnershipInternal() {
119 on_ownership_called_
= true;
120 message_loop_
.Quit();
123 void OnNameOwnerChanged(bool* called_flag
,
124 const std::string
& old_owner
,
125 const std::string
& new_owner
) {
126 latest_name_owner_
= new_owner
;
128 message_loop_
.Quit();
131 // Called when the "Test" signal is received, in the main thread.
132 // Copy the string payload to |test_signal_string_|.
133 void OnTestSignal(Signal
* signal
) {
134 MessageReader
reader(signal
);
135 ASSERT_TRUE(reader
.PopString(&test_signal_string_
));
136 message_loop_
.Quit();
139 // Called when connected to the signal.
140 void OnConnected(const std::string
& interface_name
,
141 const std::string
& signal_name
,
143 ASSERT_TRUE(success
);
144 message_loop_
.Quit();
148 // Wait for the hey signal to be received.
149 void WaitForTestSignal() {
150 // OnTestSignal() will quit the message loop.
154 // 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);
162 base::MessageLoop message_loop_
;
163 scoped_ptr
<base::Thread
> dbus_thread_
;
164 scoped_refptr
<Bus
> bus_
;
165 ObjectProxy
* object_proxy_
;
166 scoped_ptr
<TestService
> test_service_
;
167 scoped_ptr
<TestService
> test_service2_
;
168 // Text message from "Test" signal.
169 std::string test_signal_string_
;
171 // The known latest name owner of TestService. Updated in OnNameOwnerChanged.
172 std::string latest_name_owner_
;
174 // Boolean flags to record callback calls.
175 bool on_name_owner_changed_called_
;
176 bool on_ownership_called_
;
179 TEST_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.
186 ASSERT_EQ(kMessage
, test_signal_string_
);
189 // Disabled, http://crbug.com/407063 .
190 TEST_F(SignalSenderVerificationTest
, DISABLED_TestSignalRejected
) {
191 // To make sure the histogram instance is created.
192 UMA_HISTOGRAM_COUNTS("DBus.RejectedSignalCount", 0);
193 base::HistogramBase
* reject_signal_histogram
=
194 base::StatisticsRecorder::FindHistogram("DBus.RejectedSignalCount");
195 scoped_ptr
<base::HistogramSamples
> samples1(
196 reject_signal_histogram
->SnapshotSamples());
198 const char kNewMessage
[] = "hello, new world";
199 test_service2_
->SendTestSignal(kNewMessage
);
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());
205 scoped_ptr
<base::HistogramSamples
> samples2(
206 reject_signal_histogram
->SnapshotSamples());
208 ASSERT_EQ("", test_signal_string_
);
209 EXPECT_EQ(samples1
->TotalCount() + 1, samples2
->TotalCount());
212 TEST_F(SignalSenderVerificationTest
, TestOwnerChanged
) {
213 const char kMessage
[] = "hello, world";
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.
220 ASSERT_EQ(kMessage
, test_signal_string_
);
222 // 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());
225 test_service_
->ShutdownAndBlock();
226 // OnNameOwnerChanged will PostTask to quit the message loop.
228 // latest_name_owner_ should be empty as the owner is gone.
229 ASSERT_TRUE(latest_name_owner_
.empty());
231 // Reset the flag as NameOwnerChanged is already received in setup.
232 on_name_owner_changed_called_
= false;
233 on_ownership_called_
= false;
234 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.
240 if (!on_name_owner_changed_called_
|| !on_ownership_called_
)
242 ASSERT_TRUE(on_name_owner_changed_called_
);
243 ASSERT_TRUE(on_ownership_called_
);
245 // latest_name_owner_ becomes non empty as the new owner appears.
246 ASSERT_FALSE(latest_name_owner_
.empty());
248 // Now the second service owns the name.
249 const char kNewMessage
[] = "hello, new world";
251 test_service2_
->SendTestSignal(kNewMessage
);
253 ASSERT_EQ(kNewMessage
, test_signal_string_
);
256 TEST_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.
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;
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());
278 // OnNameOwnerChanged will PostTask to quit the message loop.
281 // Send a signal to check that the service is correctly owned.
282 const char kMessage
[] = "hello, world";
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.
289 ASSERT_EQ(kMessage
, test_signal_string_
);
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.
299 if (!on_name_owner_changed_called_
|| !on_ownership_called_
)
301 ASSERT_TRUE(on_name_owner_changed_called_
);
302 ASSERT_TRUE(on_ownership_called_
);
304 // Now the second service owns the name.
305 const char kNewMessage
[] = "hello, new world";
307 test_service2_
->SendTestSignal(kNewMessage
);
309 ASSERT_EQ(kNewMessage
, test_signal_string_
);
311 SafeServiceStop(&stealable_test_service
);
314 // Fails on Linux ChromiumOS Tests
315 TEST_F(SignalSenderVerificationTest
, DISABLED_TestMultipleObjects
) {
316 const char kMessage
[] = "hello, world";
318 ObjectProxy
* object_proxy2
= bus_
->GetObjectProxy(
319 "org.chromium.TestService",
320 ObjectPath("/org/chromium/DifferentObject"));
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
));
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",
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.
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.
345 ASSERT_EQ(kMessage
, test_signal_string_
);
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.
353 // latest_name_owner_ should be empty as the owner is gone.
354 ASSERT_TRUE(latest_name_owner_
.empty());
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_
)
367 ASSERT_TRUE(on_name_owner_changed_called_
);
368 ASSERT_TRUE(second_name_owner_changed_called
);
369 ASSERT_TRUE(on_ownership_called_
);
371 // latest_name_owner_ becomes non empty as the new owner appears.
372 ASSERT_FALSE(latest_name_owner_
.empty());
374 // Now the second service owns the name.
375 const char kNewMessage
[] = "hello, new world";
377 test_service2_
->SendTestSignal(kNewMessage
);
379 ASSERT_EQ(kNewMessage
, test_signal_string_
);