Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(775)

Side by Side Diff: dbus/signal_sender_verification_unittest.cc

Issue 11199007: Add sender verification of D-Bus signals. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: remove unnecessary code. Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
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"
19
20 namespace {
21
22 // See comments in ObjectProxy::RunResponseCallback() and
23 // ObjectProxy::UpdateNameOwnerAsyncfor why the number was chosen.
24 const int kHugePayloadSize = 64 << 20; // 64 MB
25
26 } // namespace
27
28 // The test for sender verification in ObjectProxy.
29 class SignalSenderVerificationTest : public testing::Test {
30 public:
31 SignalSenderVerificationTest() {
32 }
33
34 virtual void SetUp() {
35 base::StatisticsRecorder::Initialize();
36
37 // Make the main thread not to allow IO.
38 base::ThreadRestrictions::SetIOAllowed(false);
39
40 // Start the D-Bus thread.
41 dbus_thread_.reset(new base::Thread("D-Bus Thread"));
42 base::Thread::Options thread_options;
43 thread_options.message_loop_type = MessageLoop::TYPE_IO;
44 ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options));
45
46 // Start the test service, using the D-Bus thread.
47 dbus::TestService::Options options;
48 options.dbus_thread_message_loop_proxy = dbus_thread_->message_loop_proxy();
49 test_service_.reset(new dbus::TestService(options));
50 ASSERT_TRUE(test_service_->StartService());
51 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted());
52 ASSERT_TRUE(test_service_->HasDBusThread());
53
54 // Same setup for the second TestService. This service should not have the
55 // ownership of the name at this point.
56 test_service2_.reset(new dbus::TestService(options));
57 ASSERT_TRUE(test_service2_->StartService());
58 ASSERT_TRUE(test_service2_->WaitUntilServiceIsStarted());
59 ASSERT_TRUE(test_service2_->HasDBusThread());
60
61 // Create the client, using the D-Bus thread.
62 dbus::Bus::Options bus_options;
63 bus_options.bus_type = dbus::Bus::SESSION;
64 bus_options.connection_type = dbus::Bus::PRIVATE;
65 bus_options.dbus_thread_message_loop_proxy =
66 dbus_thread_->message_loop_proxy();
67 bus_ = new dbus::Bus(bus_options);
68 object_proxy_ = bus_->GetObjectProxy(
69 "org.chromium.TestService",
70 dbus::ObjectPath("/org/chromium/TestObject"));
71 ASSERT_TRUE(bus_->HasDBusThread());
72
73 // Connect to the "Test" signal of "org.chromium.TestInterface" from
74 // the remote object.
75 object_proxy_->ConnectToSignal(
76 "org.chromium.TestInterface",
77 "Test",
78 base::Bind(&SignalSenderVerificationTest::OnTestSignal,
79 base::Unretained(this)),
80 base::Bind(&SignalSenderVerificationTest::OnConnected,
81 base::Unretained(this)));
82 // Wait until the object proxy is connected to the signal.
83 message_loop_.Run();
84 }
85
86 virtual void TearDown() {
87 bus_->ShutdownOnDBusThreadAndBlock();
88
89 // Shut down the service.
90 test_service_->ShutdownAndBlock();
91 test_service2_->ShutdownAndBlock();
92
93 // Reset to the default.
94 base::ThreadRestrictions::SetIOAllowed(true);
95
96 // Stopping a thread is considered an IO operation, so do this after
97 // allowing IO.
98 test_service_->Stop();
99 test_service2_->Stop();
100 }
101
102 protected:
103
104 // Called when the "Test" signal is received, in the main thread.
105 // Copy the string payload to |test_signal_string_|.
106 void OnTestSignal(dbus::Signal* signal) {
107 dbus::MessageReader reader(signal);
108 ASSERT_TRUE(reader.PopString(&test_signal_string_));
109 message_loop_.Quit();
110 }
111
112 // Called when connected to the signal.
113 void OnConnected(const std::string& interface_name,
114 const std::string& signal_name,
115 bool success) {
116 ASSERT_TRUE(success);
117 message_loop_.Quit();
118 }
119
120 // Wait for the hey signal to be received.
121 void WaitForTestSignal() {
122 // OnTestSignal() will quit the message loop.
123 message_loop_.Run();
124 }
125
126 MessageLoop message_loop_;
127 scoped_ptr<base::Thread> dbus_thread_;
128 scoped_refptr<dbus::Bus> bus_;
129 dbus::ObjectProxy* object_proxy_;
130 scoped_ptr<dbus::TestService> test_service_;
131 scoped_ptr<dbus::TestService> test_service2_;
132 // Text message from "Test" signal.
133 std::string test_signal_string_;
134 };
135
136 TEST_F(SignalSenderVerificationTest, TestSignalAccepted) {
137 const char kMessage[] = "hello, world";
138 // Send the test signal from the exported object.
139 test_service_->SendTestSignal(kMessage);
140 // Receive the signal with the object proxy. The signal is handled in
141 // SignalSenderVerificationTest::OnTestSignal() in the main thread.
142 WaitForTestSignal();
143 ASSERT_EQ(kMessage, test_signal_string_);
144 }
145
146 TEST_F(SignalSenderVerificationTest, TestSignalRejected) {
147 // To make sure the histogram instance is created.
148 UMA_HISTOGRAM_COUNTS("DBus.RejectedSignalCount", 0);
149 base::Histogram* r_histogram =
150 base::StatisticsRecorder::FindHistogram("DBus.RejectedSignalCount");
151 scoped_ptr<base::HistogramSamples> samples1(
152 r_histogram->SnapshotSamples());
153
154 const char kNewMessage[] = "hello, new world";
155 test_service2_->SendTestSignal(kNewMessage);
156
157 // This test tests that our callback is NOT called by the ObjectProxy.
158 // We are giving some D-Bus service and thread and
159 // Sleep to have message delivered to the client via the D-Bus service.
160 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout());
161 // Let the callbacks handle responses (the test message and GetNameOwner
162 // result). This may call GetNameOwner to retrieve the name owner.
163 message_loop_.RunUntilIdle();
164 // Sleep again to wait for GetNameOwner response and handle the pending test
165 // message.
166 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout());
167
168 scoped_ptr<base::HistogramSamples> samples2(
169 r_histogram->SnapshotSamples());
170
171 ASSERT_EQ("", test_signal_string_);
172 EXPECT_EQ(samples1->TotalCount() + 1, samples2->TotalCount());
173 }
174
175 TEST_F(SignalSenderVerificationTest, TestOwnerChanged) {
176 const char kMessage[] = "hello, world";
177
178 // Send the test signal from the exported object.
179 test_service_->SendTestSignal(kMessage);
180 // Receive the signal with the object proxy. The signal is handled in
181 // SignalSenderVerificationTest::OnTestSignal() in the main thread.
182 WaitForTestSignal();
183 ASSERT_EQ(kMessage, test_signal_string_);
184
185 // Release and aquire the name ownership.
186 test_service_->ShutdownAndBlock();
187 test_service2_->RequestOwnership();
188
189 // Now the second service owns the name.
190 const char kNewMessage[] = "hello, new world";
191
192 test_service2_->SendTestSignal(kNewMessage);
193 WaitForTestSignal();
194 ASSERT_EQ(kNewMessage, test_signal_string_);
195 }
196
197 TEST_F(SignalSenderVerificationTest, TestHugeSignal) {
satorux1 2012/10/25 01:55:16 I think we have the same test elsewhere. then plea
Haruki Sato 2012/10/25 07:29:52 Done.
198 const std::string kHugeMessage(kHugePayloadSize, 'o');
199
200 // Send the huge signal from the exported object.
201 test_service_->SendTestSignal(kHugeMessage);
202 // This caused a DCHECK failure before. Ensure that the issue is fixed.
203 WaitForTestSignal();
204 ASSERT_EQ(kHugeMessage, test_signal_string_);
205 }
OLDNEW
« dbus/object_proxy.cc ('K') | « dbus/object_proxy.cc ('k') | dbus/test_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698