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
, Signal
* signal
) {
124 MessageReader
reader(signal
);
125 std::string name
, old_owner
, new_owner
;
126 ASSERT_TRUE(reader
.PopString(&name
));
127 ASSERT_TRUE(reader
.PopString(&old_owner
));
128 ASSERT_TRUE(reader
.PopString(&new_owner
));
129 latest_name_owner_
= new_owner
;
131 message_loop_
.Quit();
134 // Called when the "Test" signal is received, in the main thread.
135 // Copy the string payload to |test_signal_string_|.
136 void OnTestSignal(Signal
* signal
) {
137 MessageReader
reader(signal
);
138 ASSERT_TRUE(reader
.PopString(&test_signal_string_
));
139 message_loop_
.Quit();
142 // Called when connected to the signal.
143 void OnConnected(const std::string
& interface_name
,
144 const std::string
& signal_name
,
146 ASSERT_TRUE(success
);
147 message_loop_
.Quit();
151 // Wait for the hey signal to be received.
152 void WaitForTestSignal() {
153 // OnTestSignal() will quit the message loop.
157 base::MessageLoop message_loop_
;
158 scoped_ptr
<base::Thread
> dbus_thread_
;
159 scoped_refptr
<Bus
> bus_
;
160 ObjectProxy
* object_proxy_
;
161 scoped_ptr
<TestService
> test_service_
;
162 scoped_ptr
<TestService
> test_service2_
;
163 // Text message from "Test" signal.
164 std::string test_signal_string_
;
166 // The known latest name owner of TestService. Updated in OnNameOwnerChanged.
167 std::string latest_name_owner_
;
169 // Boolean flags to record callback calls.
170 bool on_name_owner_changed_called_
;
171 bool on_ownership_called_
;
174 TEST_F(SignalSenderVerificationTest
, TestSignalAccepted
) {
175 const char kMessage
[] = "hello, world";
176 // Send the test signal from the exported object.
177 test_service_
->SendTestSignal(kMessage
);
178 // Receive the signal with the object proxy. The signal is handled in
179 // SignalSenderVerificationTest::OnTestSignal() in the main thread.
181 ASSERT_EQ(kMessage
, test_signal_string_
);
184 TEST_F(SignalSenderVerificationTest
, TestSignalRejected
) {
185 // To make sure the histogram instance is created.
186 UMA_HISTOGRAM_COUNTS("DBus.RejectedSignalCount", 0);
187 base::HistogramBase
* reject_signal_histogram
=
188 base::StatisticsRecorder::FindHistogram("DBus.RejectedSignalCount");
189 scoped_ptr
<base::HistogramSamples
> samples1(
190 reject_signal_histogram
->SnapshotSamples());
192 const char kNewMessage
[] = "hello, new world";
193 test_service2_
->SendTestSignal(kNewMessage
);
195 // This test tests that our callback is NOT called by the ObjectProxy.
196 // Sleep to have message delivered to the client via the D-Bus service.
197 base::PlatformThread::Sleep(TestTimeouts::action_timeout());
199 scoped_ptr
<base::HistogramSamples
> samples2(
200 reject_signal_histogram
->SnapshotSamples());
202 ASSERT_EQ("", test_signal_string_
);
203 EXPECT_EQ(samples1
->TotalCount() + 1, samples2
->TotalCount());
206 TEST_F(SignalSenderVerificationTest
, TestOwnerChanged
) {
207 const char kMessage
[] = "hello, world";
209 // Send the test signal from the exported object.
210 test_service_
->SendTestSignal(kMessage
);
211 // Receive the signal with the object proxy. The signal is handled in
212 // SignalSenderVerificationTest::OnTestSignal() in the main thread.
214 ASSERT_EQ(kMessage
, test_signal_string_
);
216 // Release and acquire the name ownership.
217 // latest_name_owner_ should be non empty as |test_service_| owns the name.
218 ASSERT_FALSE(latest_name_owner_
.empty());
219 test_service_
->ShutdownAndBlock();
220 // OnNameOwnerChanged will PostTask to quit the message loop.
222 // latest_name_owner_ should be empty as the owner is gone.
223 ASSERT_TRUE(latest_name_owner_
.empty());
225 // Reset the flag as NameOwnerChanged is already received in setup.
226 on_name_owner_changed_called_
= false;
227 test_service2_
->RequestOwnership(
228 base::Bind(&SignalSenderVerificationTest::OnOwnership
,
229 base::Unretained(this), true));
230 // Both of OnNameOwnerChanged() and OnOwnership() should quit the MessageLoop,
231 // but there's no expected order of those 2 event.
233 if (!on_name_owner_changed_called_
|| !on_ownership_called_
)
235 ASSERT_TRUE(on_name_owner_changed_called_
);
236 ASSERT_TRUE(on_ownership_called_
);
238 // latest_name_owner_ becomes non empty as the new owner appears.
239 ASSERT_FALSE(latest_name_owner_
.empty());
241 // Now the second service owns the name.
242 const char kNewMessage
[] = "hello, new world";
244 test_service2_
->SendTestSignal(kNewMessage
);
246 ASSERT_EQ(kNewMessage
, test_signal_string_
);
249 // Fails on Linux ChromiumOS Tests
250 TEST_F(SignalSenderVerificationTest
, DISABLED_TestMultipleObjects
) {
251 const char kMessage
[] = "hello, world";
253 ObjectProxy
* object_proxy2
= bus_
->GetObjectProxy(
254 "org.chromium.TestService",
255 ObjectPath("/org/chromium/DifferentObject"));
257 bool second_name_owner_changed_called
= false;
258 object_proxy2
->SetNameOwnerChangedCallback(
259 base::Bind(&SignalSenderVerificationTest::OnNameOwnerChanged
,
260 base::Unretained(this),
261 &second_name_owner_changed_called
));
263 // Connect to a signal on the additional remote object to trigger the
264 // name owner matching.
265 object_proxy2
->ConnectToSignal(
266 "org.chromium.DifferentTestInterface",
268 base::Bind(&SignalSenderVerificationTest::OnTestSignal
,
269 base::Unretained(this)),
270 base::Bind(&SignalSenderVerificationTest::OnConnected
,
271 base::Unretained(this)));
272 // Wait until the object proxy is connected to the signal.
275 // Send the test signal from the exported object.
276 test_service_
->SendTestSignal(kMessage
);
277 // Receive the signal with the object proxy. The signal is handled in
278 // SignalSenderVerificationTest::OnTestSignal() in the main thread.
280 ASSERT_EQ(kMessage
, test_signal_string_
);
282 // Release and acquire the name ownership.
283 // latest_name_owner_ should be non empty as |test_service_| owns the name.
284 ASSERT_FALSE(latest_name_owner_
.empty());
285 test_service_
->ShutdownAndBlock();
286 // OnNameOwnerChanged will PostTask to quit the message loop.
288 // latest_name_owner_ should be empty as the owner is gone.
289 ASSERT_TRUE(latest_name_owner_
.empty());
291 // Reset the flag as NameOwnerChanged is already received in setup.
292 on_name_owner_changed_called_
= false;
293 second_name_owner_changed_called
= false;
294 test_service2_
->RequestOwnership(
295 base::Bind(&SignalSenderVerificationTest::OnOwnership
,
296 base::Unretained(this), true));
297 // Both of OnNameOwnerChanged() and OnOwnership() should quit the MessageLoop,
298 // but there's no expected order of those 2 event.
299 while (!on_name_owner_changed_called_
|| !second_name_owner_changed_called
||
300 !on_ownership_called_
)
302 ASSERT_TRUE(on_name_owner_changed_called_
);
303 ASSERT_TRUE(second_name_owner_changed_called
);
304 ASSERT_TRUE(on_ownership_called_
);
306 // latest_name_owner_ becomes non empty as the new owner appears.
307 ASSERT_FALSE(latest_name_owner_
.empty());
309 // Now the second service owns the name.
310 const char kNewMessage
[] = "hello, new world";
312 test_service2_
->SendTestSignal(kNewMessage
);
314 ASSERT_EQ(kNewMessage
, test_signal_string_
);