Roll WebKit 141933:141963
[chromium-blink-merge.git] / dbus / signal_sender_verification_unittest.cc
blobd2be464b905d764b610a58733197bf1438df4a6d
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.
5 #include "base/bind.h"
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"
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"
20 // The test for sender verification in ObjectProxy.
21 class SignalSenderVerificationTest : public testing::Test {
22 public:
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_thread_message_loop_proxy =
45 dbus_thread_->message_loop_proxy();
46 bus_ = new dbus::Bus(bus_options);
47 object_proxy_ = bus_->GetObjectProxy(
48 "org.chromium.TestService",
49 dbus::ObjectPath("/org/chromium/TestObject"));
50 ASSERT_TRUE(bus_->HasDBusThread());
52 object_proxy_->SetNameOwnerChangedCallback(
53 base::Bind(&SignalSenderVerificationTest::OnNameOwnerChanged,
54 base::Unretained(this),
55 &on_name_owner_changed_called_));
57 // Connect to the "Test" signal of "org.chromium.TestInterface" from
58 // the remote object.
59 object_proxy_->ConnectToSignal(
60 "org.chromium.TestInterface",
61 "Test",
62 base::Bind(&SignalSenderVerificationTest::OnTestSignal,
63 base::Unretained(this)),
64 base::Bind(&SignalSenderVerificationTest::OnConnected,
65 base::Unretained(this)));
66 // Wait until the object proxy is connected to the signal.
67 message_loop_.Run();
69 // Start the test service, using the D-Bus thread.
70 dbus::TestService::Options options;
71 options.dbus_thread_message_loop_proxy = dbus_thread_->message_loop_proxy();
72 test_service_.reset(new dbus::TestService(options));
73 ASSERT_TRUE(test_service_->StartService());
74 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted());
75 ASSERT_TRUE(test_service_->HasDBusThread());
76 ASSERT_TRUE(test_service_->has_ownership());
78 // Same setup for the second TestService. This service should not have the
79 // ownership of the name at this point.
80 test_service2_.reset(new dbus::TestService(options));
81 ASSERT_TRUE(test_service2_->StartService());
82 ASSERT_TRUE(test_service2_->WaitUntilServiceIsStarted());
83 ASSERT_TRUE(test_service2_->HasDBusThread());
84 ASSERT_FALSE(test_service2_->has_ownership());
86 // The name should be owned and known at this point.
87 if (!on_name_owner_changed_called_)
88 message_loop_.Run();
89 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
104 // allowing IO.
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(
113 FROM_HERE,
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, dbus::Signal* signal) {
124 dbus::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;
130 *called_flag = true;
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(dbus::Signal* signal) {
137 dbus::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,
145 bool success) {
146 ASSERT_TRUE(success);
147 message_loop_.Quit();
150 protected:
152 // Wait for the hey signal to be received.
153 void WaitForTestSignal() {
154 // OnTestSignal() will quit the message loop.
155 message_loop_.Run();
158 MessageLoop message_loop_;
159 scoped_ptr<base::Thread> dbus_thread_;
160 scoped_refptr<dbus::Bus> bus_;
161 dbus::ObjectProxy* object_proxy_;
162 scoped_ptr<dbus::TestService> test_service_;
163 scoped_ptr<dbus::TestService> test_service2_;
164 // Text message from "Test" signal.
165 std::string test_signal_string_;
167 // The known latest name owner of TestService. Updated in OnNameOwnerChanged.
168 std::string latest_name_owner_;
170 // Boolean flags to record callback calls.
171 bool on_name_owner_changed_called_;
172 bool on_ownership_called_;
175 TEST_F(SignalSenderVerificationTest, TestSignalAccepted) {
176 const char kMessage[] = "hello, world";
177 // Send the test signal from the exported object.
178 test_service_->SendTestSignal(kMessage);
179 // Receive the signal with the object proxy. The signal is handled in
180 // SignalSenderVerificationTest::OnTestSignal() in the main thread.
181 WaitForTestSignal();
182 ASSERT_EQ(kMessage, test_signal_string_);
185 TEST_F(SignalSenderVerificationTest, TestSignalRejected) {
186 // To make sure the histogram instance is created.
187 UMA_HISTOGRAM_COUNTS("DBus.RejectedSignalCount", 0);
188 base::Histogram* reject_signal_histogram =
189 base::StatisticsRecorder::FindHistogram("DBus.RejectedSignalCount");
190 scoped_ptr<base::HistogramSamples> samples1(
191 reject_signal_histogram->SnapshotSamples());
193 const char kNewMessage[] = "hello, new world";
194 test_service2_->SendTestSignal(kNewMessage);
196 // This test tests that our callback is NOT called by the ObjectProxy.
197 // Sleep to have message delivered to the client via the D-Bus service.
198 base::PlatformThread::Sleep(TestTimeouts::action_timeout());
200 scoped_ptr<base::HistogramSamples> samples2(
201 reject_signal_histogram->SnapshotSamples());
203 ASSERT_EQ("", test_signal_string_);
204 EXPECT_EQ(samples1->TotalCount() + 1, samples2->TotalCount());
207 TEST_F(SignalSenderVerificationTest, TestOwnerChanged) {
208 const char kMessage[] = "hello, world";
210 // Send the test signal from the exported object.
211 test_service_->SendTestSignal(kMessage);
212 // Receive the signal with the object proxy. The signal is handled in
213 // SignalSenderVerificationTest::OnTestSignal() in the main thread.
214 WaitForTestSignal();
215 ASSERT_EQ(kMessage, test_signal_string_);
217 // Release and acquire the name ownership.
218 // latest_name_owner_ should be non empty as |test_service_| owns the name.
219 ASSERT_FALSE(latest_name_owner_.empty());
220 test_service_->ShutdownAndBlock();
221 // OnNameOwnerChanged will PostTask to quit the message loop.
222 message_loop_.Run();
223 // latest_name_owner_ should be empty as the owner is gone.
224 ASSERT_TRUE(latest_name_owner_.empty());
226 // Reset the flag as NameOwnerChanged is already received in setup.
227 on_name_owner_changed_called_ = false;
228 test_service2_->RequestOwnership(
229 base::Bind(&SignalSenderVerificationTest::OnOwnership,
230 base::Unretained(this), true));
231 // Both of OnNameOwnerChanged() and OnOwnership() should quit the MessageLoop,
232 // but there's no expected order of those 2 event.
233 message_loop_.Run();
234 if (!on_name_owner_changed_called_ || !on_ownership_called_)
235 message_loop_.Run();
236 ASSERT_TRUE(on_name_owner_changed_called_);
237 ASSERT_TRUE(on_ownership_called_);
239 // latest_name_owner_ becomes non empty as the new owner appears.
240 ASSERT_FALSE(latest_name_owner_.empty());
242 // Now the second service owns the name.
243 const char kNewMessage[] = "hello, new world";
245 test_service2_->SendTestSignal(kNewMessage);
246 WaitForTestSignal();
247 ASSERT_EQ(kNewMessage, test_signal_string_);
250 // Fails on Linux ChromiumOS Tests
251 TEST_F(SignalSenderVerificationTest, DISABLED_TestMultipleObjects) {
252 const char kMessage[] = "hello, world";
254 dbus::ObjectProxy* object_proxy2 = bus_->GetObjectProxy(
255 "org.chromium.TestService",
256 dbus::ObjectPath("/org/chromium/DifferentObject"));
258 bool second_name_owner_changed_called = false;
259 object_proxy2->SetNameOwnerChangedCallback(
260 base::Bind(&SignalSenderVerificationTest::OnNameOwnerChanged,
261 base::Unretained(this),
262 &second_name_owner_changed_called));
264 // Connect to a signal on the additional remote object to trigger the
265 // name owner matching.
266 object_proxy2->ConnectToSignal(
267 "org.chromium.DifferentTestInterface",
268 "Test",
269 base::Bind(&SignalSenderVerificationTest::OnTestSignal,
270 base::Unretained(this)),
271 base::Bind(&SignalSenderVerificationTest::OnConnected,
272 base::Unretained(this)));
273 // Wait until the object proxy is connected to the signal.
274 message_loop_.Run();
276 // Send the test signal from the exported object.
277 test_service_->SendTestSignal(kMessage);
278 // Receive the signal with the object proxy. The signal is handled in
279 // SignalSenderVerificationTest::OnTestSignal() in the main thread.
280 WaitForTestSignal();
281 ASSERT_EQ(kMessage, test_signal_string_);
283 // Release and acquire the name ownership.
284 // latest_name_owner_ should be non empty as |test_service_| owns the name.
285 ASSERT_FALSE(latest_name_owner_.empty());
286 test_service_->ShutdownAndBlock();
287 // OnNameOwnerChanged will PostTask to quit the message loop.
288 message_loop_.Run();
289 // latest_name_owner_ should be empty as the owner is gone.
290 ASSERT_TRUE(latest_name_owner_.empty());
292 // Reset the flag as NameOwnerChanged is already received in setup.
293 on_name_owner_changed_called_ = false;
294 second_name_owner_changed_called = false;
295 test_service2_->RequestOwnership(
296 base::Bind(&SignalSenderVerificationTest::OnOwnership,
297 base::Unretained(this), true));
298 // Both of OnNameOwnerChanged() and OnOwnership() should quit the MessageLoop,
299 // but there's no expected order of those 2 event.
300 while (!on_name_owner_changed_called_ || !second_name_owner_changed_called ||
301 !on_ownership_called_)
302 message_loop_.Run();
303 ASSERT_TRUE(on_name_owner_changed_called_);
304 ASSERT_TRUE(second_name_owner_changed_called);
305 ASSERT_TRUE(on_ownership_called_);
307 // latest_name_owner_ becomes non empty as the new owner appears.
308 ASSERT_FALSE(latest_name_owner_.empty());
310 // Now the second service owns the name.
311 const char kNewMessage[] = "hello, new world";
313 test_service2_->SendTestSignal(kNewMessage);
314 WaitForTestSignal();
315 ASSERT_EQ(kNewMessage, test_signal_string_);