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.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"
20 // The test for sender verification in ObjectProxy.
21 class SignalSenderVerificationTest
: public testing::Test
{
23 SignalSenderVerificationTest()
24 : on_name_owner_changed_called_(false),
25 on_ownership_called_(false) {
28 virtual void SetUp() {
29 base::StatisticsRecorder::Initialize();
31 // Make the main thread not to allow IO.
32 base::ThreadRestrictions::SetIOAllowed(false);
34 // Start the D-Bus thread.
35 dbus_thread_
.reset(new base::Thread("D-Bus Thread"));
36 base::Thread::Options thread_options
;
37 thread_options
.message_loop_type
= MessageLoop::TYPE_IO
;
38 ASSERT_TRUE(dbus_thread_
->StartWithOptions(thread_options
));
40 // Create the client, using the D-Bus thread.
41 dbus::Bus::Options bus_options
;
42 bus_options
.bus_type
= dbus::Bus::SESSION
;
43 bus_options
.connection_type
= dbus::Bus::PRIVATE
;
44 bus_options
.dbus_task_runner
= dbus_thread_
->message_loop_proxy();
45 bus_
= new dbus::Bus(bus_options
);
46 object_proxy_
= bus_
->GetObjectProxy(
47 "org.chromium.TestService",
48 dbus::ObjectPath("/org/chromium/TestObject"));
49 ASSERT_TRUE(bus_
->HasDBusThread());
51 object_proxy_
->SetNameOwnerChangedCallback(
52 base::Bind(&SignalSenderVerificationTest::OnNameOwnerChanged
,
53 base::Unretained(this),
54 &on_name_owner_changed_called_
));
56 // Connect to the "Test" signal of "org.chromium.TestInterface" from
58 object_proxy_
->ConnectToSignal(
59 "org.chromium.TestInterface",
61 base::Bind(&SignalSenderVerificationTest::OnTestSignal
,
62 base::Unretained(this)),
63 base::Bind(&SignalSenderVerificationTest::OnConnected
,
64 base::Unretained(this)));
65 // Wait until the object proxy is connected to the signal.
68 // Start the test service, using the D-Bus thread.
69 dbus::TestService::Options options
;
70 options
.dbus_task_runner
= dbus_thread_
->message_loop_proxy();
71 test_service_
.reset(new dbus::TestService(options
));
72 ASSERT_TRUE(test_service_
->StartService());
73 ASSERT_TRUE(test_service_
->WaitUntilServiceIsStarted());
74 ASSERT_TRUE(test_service_
->HasDBusThread());
75 ASSERT_TRUE(test_service_
->has_ownership());
77 // Same setup for the second TestService. This service should not have the
78 // ownership of the name at this point.
79 test_service2_
.reset(new dbus::TestService(options
));
80 ASSERT_TRUE(test_service2_
->StartService());
81 ASSERT_TRUE(test_service2_
->WaitUntilServiceIsStarted());
82 ASSERT_TRUE(test_service2_
->HasDBusThread());
83 ASSERT_FALSE(test_service2_
->has_ownership());
85 // The name should be owned and known at this point.
86 if (!on_name_owner_changed_called_
)
88 ASSERT_FALSE(latest_name_owner_
.empty());
91 virtual void TearDown() {
92 bus_
->ShutdownOnDBusThreadAndBlock();
94 // Shut down the service.
95 test_service_
->ShutdownAndBlock();
96 test_service2_
->ShutdownAndBlock();
98 // Reset to the default.
99 base::ThreadRestrictions::SetIOAllowed(true);
101 // Stopping a thread is considered an IO operation, so do this after
103 test_service_
->Stop();
104 test_service2_
->Stop();
107 void OnOwnership(bool expected
, bool success
) {
108 ASSERT_EQ(expected
, success
);
109 // PostTask to quit the MessageLoop as this is called from D-Bus thread.
110 message_loop_
.PostTask(
112 base::Bind(&SignalSenderVerificationTest::OnOwnershipInternal
,
113 base::Unretained(this)));
116 void OnOwnershipInternal() {
117 on_ownership_called_
= true;
118 message_loop_
.Quit();
121 void OnNameOwnerChanged(bool* called_flag
, dbus::Signal
* signal
) {
122 dbus::MessageReader
reader(signal
);
123 std::string name
, old_owner
, new_owner
;
124 ASSERT_TRUE(reader
.PopString(&name
));
125 ASSERT_TRUE(reader
.PopString(&old_owner
));
126 ASSERT_TRUE(reader
.PopString(&new_owner
));
127 latest_name_owner_
= new_owner
;
129 message_loop_
.Quit();
132 // Called when the "Test" signal is received, in the main thread.
133 // Copy the string payload to |test_signal_string_|.
134 void OnTestSignal(dbus::Signal
* signal
) {
135 dbus::MessageReader
reader(signal
);
136 ASSERT_TRUE(reader
.PopString(&test_signal_string_
));
137 message_loop_
.Quit();
140 // Called when connected to the signal.
141 void OnConnected(const std::string
& interface_name
,
142 const std::string
& signal_name
,
144 ASSERT_TRUE(success
);
145 message_loop_
.Quit();
149 // Wait for the hey signal to be received.
150 void WaitForTestSignal() {
151 // OnTestSignal() will quit the message loop.
155 MessageLoop message_loop_
;
156 scoped_ptr
<base::Thread
> dbus_thread_
;
157 scoped_refptr
<dbus::Bus
> bus_
;
158 dbus::ObjectProxy
* object_proxy_
;
159 scoped_ptr
<dbus::TestService
> test_service_
;
160 scoped_ptr
<dbus::TestService
> test_service2_
;
161 // Text message from "Test" signal.
162 std::string test_signal_string_
;
164 // The known latest name owner of TestService. Updated in OnNameOwnerChanged.
165 std::string latest_name_owner_
;
167 // Boolean flags to record callback calls.
168 bool on_name_owner_changed_called_
;
169 bool on_ownership_called_
;
172 TEST_F(SignalSenderVerificationTest
, TestSignalAccepted
) {
173 const char kMessage
[] = "hello, world";
174 // Send the test signal from the exported object.
175 test_service_
->SendTestSignal(kMessage
);
176 // Receive the signal with the object proxy. The signal is handled in
177 // SignalSenderVerificationTest::OnTestSignal() in the main thread.
179 ASSERT_EQ(kMessage
, test_signal_string_
);
182 TEST_F(SignalSenderVerificationTest
, TestSignalRejected
) {
183 // To make sure the histogram instance is created.
184 UMA_HISTOGRAM_COUNTS("DBus.RejectedSignalCount", 0);
185 base::HistogramBase
* reject_signal_histogram
=
186 base::StatisticsRecorder::FindHistogram("DBus.RejectedSignalCount");
187 scoped_ptr
<base::HistogramSamples
> samples1(
188 reject_signal_histogram
->SnapshotSamples());
190 const char kNewMessage
[] = "hello, new world";
191 test_service2_
->SendTestSignal(kNewMessage
);
193 // This test tests that our callback is NOT called by the ObjectProxy.
194 // Sleep to have message delivered to the client via the D-Bus service.
195 base::PlatformThread::Sleep(TestTimeouts::action_timeout());
197 scoped_ptr
<base::HistogramSamples
> samples2(
198 reject_signal_histogram
->SnapshotSamples());
200 ASSERT_EQ("", test_signal_string_
);
201 EXPECT_EQ(samples1
->TotalCount() + 1, samples2
->TotalCount());
204 TEST_F(SignalSenderVerificationTest
, TestOwnerChanged
) {
205 const char kMessage
[] = "hello, world";
207 // Send the test signal from the exported object.
208 test_service_
->SendTestSignal(kMessage
);
209 // Receive the signal with the object proxy. The signal is handled in
210 // SignalSenderVerificationTest::OnTestSignal() in the main thread.
212 ASSERT_EQ(kMessage
, test_signal_string_
);
214 // Release and acquire the name ownership.
215 // latest_name_owner_ should be non empty as |test_service_| owns the name.
216 ASSERT_FALSE(latest_name_owner_
.empty());
217 test_service_
->ShutdownAndBlock();
218 // OnNameOwnerChanged will PostTask to quit the message loop.
220 // latest_name_owner_ should be empty as the owner is gone.
221 ASSERT_TRUE(latest_name_owner_
.empty());
223 // Reset the flag as NameOwnerChanged is already received in setup.
224 on_name_owner_changed_called_
= false;
225 test_service2_
->RequestOwnership(
226 base::Bind(&SignalSenderVerificationTest::OnOwnership
,
227 base::Unretained(this), true));
228 // Both of OnNameOwnerChanged() and OnOwnership() should quit the MessageLoop,
229 // but there's no expected order of those 2 event.
231 if (!on_name_owner_changed_called_
|| !on_ownership_called_
)
233 ASSERT_TRUE(on_name_owner_changed_called_
);
234 ASSERT_TRUE(on_ownership_called_
);
236 // latest_name_owner_ becomes non empty as the new owner appears.
237 ASSERT_FALSE(latest_name_owner_
.empty());
239 // Now the second service owns the name.
240 const char kNewMessage
[] = "hello, new world";
242 test_service2_
->SendTestSignal(kNewMessage
);
244 ASSERT_EQ(kNewMessage
, test_signal_string_
);
247 // Fails on Linux ChromiumOS Tests
248 TEST_F(SignalSenderVerificationTest
, DISABLED_TestMultipleObjects
) {
249 const char kMessage
[] = "hello, world";
251 dbus::ObjectProxy
* object_proxy2
= bus_
->GetObjectProxy(
252 "org.chromium.TestService",
253 dbus::ObjectPath("/org/chromium/DifferentObject"));
255 bool second_name_owner_changed_called
= false;
256 object_proxy2
->SetNameOwnerChangedCallback(
257 base::Bind(&SignalSenderVerificationTest::OnNameOwnerChanged
,
258 base::Unretained(this),
259 &second_name_owner_changed_called
));
261 // Connect to a signal on the additional remote object to trigger the
262 // name owner matching.
263 object_proxy2
->ConnectToSignal(
264 "org.chromium.DifferentTestInterface",
266 base::Bind(&SignalSenderVerificationTest::OnTestSignal
,
267 base::Unretained(this)),
268 base::Bind(&SignalSenderVerificationTest::OnConnected
,
269 base::Unretained(this)));
270 // Wait until the object proxy is connected to the signal.
273 // Send the test signal from the exported object.
274 test_service_
->SendTestSignal(kMessage
);
275 // Receive the signal with the object proxy. The signal is handled in
276 // SignalSenderVerificationTest::OnTestSignal() in the main thread.
278 ASSERT_EQ(kMessage
, test_signal_string_
);
280 // Release and acquire the name ownership.
281 // latest_name_owner_ should be non empty as |test_service_| owns the name.
282 ASSERT_FALSE(latest_name_owner_
.empty());
283 test_service_
->ShutdownAndBlock();
284 // OnNameOwnerChanged will PostTask to quit the message loop.
286 // latest_name_owner_ should be empty as the owner is gone.
287 ASSERT_TRUE(latest_name_owner_
.empty());
289 // Reset the flag as NameOwnerChanged is already received in setup.
290 on_name_owner_changed_called_
= false;
291 second_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 while (!on_name_owner_changed_called_
|| !second_name_owner_changed_called
||
298 !on_ownership_called_
)
300 ASSERT_TRUE(on_name_owner_changed_called_
);
301 ASSERT_TRUE(second_name_owner_changed_called
);
302 ASSERT_TRUE(on_ownership_called_
);
304 // latest_name_owner_ becomes non empty as the new owner appears.
305 ASSERT_FALSE(latest_name_owner_
.empty());
307 // Now the second service owns the name.
308 const char kNewMessage
[] = "hello, new world";
310 test_service2_
->SendTestSignal(kNewMessage
);
312 ASSERT_EQ(kNewMessage
, test_signal_string_
);