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

Side by Side Diff: dbus/end_to_end_async_unittest.cc

Issue 9508005: dbus: verify object path of incoming signals (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: comment fixes Created 8 years, 9 months 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | dbus/object_proxy.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <algorithm> 5 #include <algorithm>
6 #include <string> 6 #include <string>
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 // one signal of the same interface. See crosbug.com/23382 for details. 76 // one signal of the same interface. See crosbug.com/23382 for details.
77 object_proxy_->ConnectToSignal( 77 object_proxy_->ConnectToSignal(
78 "org.chromium.TestInterface", 78 "org.chromium.TestInterface",
79 "Test2", 79 "Test2",
80 base::Bind(&EndToEndAsyncTest::OnTest2Signal, 80 base::Bind(&EndToEndAsyncTest::OnTest2Signal,
81 base::Unretained(this)), 81 base::Unretained(this)),
82 base::Bind(&EndToEndAsyncTest::OnConnected, 82 base::Bind(&EndToEndAsyncTest::OnConnected,
83 base::Unretained(this))); 83 base::Unretained(this)));
84 // Wait until the object proxy is connected to the signal. 84 // Wait until the object proxy is connected to the signal.
85 message_loop_.Run(); 85 message_loop_.Run();
86
87 // Create a second object proxy for the root object.
88 root_object_proxy_ = bus_->GetObjectProxy(
89 "org.chromium.TestService",
90 dbus::ObjectPath("/"));
91 ASSERT_TRUE(bus_->HasDBusThread());
92
93 // Connect to the "Test" signal of "org.chromium.TestInterface" from
94 // the root remote object too.
95 root_object_proxy_->ConnectToSignal(
96 "org.chromium.TestInterface",
97 "Test",
98 base::Bind(&EndToEndAsyncTest::OnRootTestSignal,
99 base::Unretained(this)),
100 base::Bind(&EndToEndAsyncTest::OnConnected,
101 base::Unretained(this)));
102 // Wait until the root object proxy is connected to the signal.
103 message_loop_.Run();
86 } 104 }
87 105
88 virtual void TearDown() { 106 virtual void TearDown() {
89 bus_->ShutdownOnDBusThreadAndBlock(); 107 bus_->ShutdownOnDBusThreadAndBlock();
90 108
91 // Shut down the service. 109 // Shut down the service.
92 test_service_->ShutdownAndBlock(); 110 test_service_->ShutdownAndBlock();
93 111
94 // Reset to the default. 112 // Reset to the default.
95 base::ThreadRestrictions::SetIOAllowed(true); 113 base::ThreadRestrictions::SetIOAllowed(true);
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 }; 151 };
134 152
135 // Called when the "Test" signal is received, in the main thread. 153 // Called when the "Test" signal is received, in the main thread.
136 // Copy the string payload to |test_signal_string_|. 154 // Copy the string payload to |test_signal_string_|.
137 void OnTestSignal(dbus::Signal* signal) { 155 void OnTestSignal(dbus::Signal* signal) {
138 dbus::MessageReader reader(signal); 156 dbus::MessageReader reader(signal);
139 ASSERT_TRUE(reader.PopString(&test_signal_string_)); 157 ASSERT_TRUE(reader.PopString(&test_signal_string_));
140 message_loop_.Quit(); 158 message_loop_.Quit();
141 } 159 }
142 160
161 // Called when the "Test" signal is received, in the main thread, by
162 // the root object proxy. Copy the string payload to
163 // |root_test_signal_string_|.
164 void OnRootTestSignal(dbus::Signal* signal) {
165 dbus::MessageReader reader(signal);
166 ASSERT_TRUE(reader.PopString(&root_test_signal_string_));
167 message_loop_.Quit();
168 }
169
143 // Called when the "Test2" signal is received, in the main thread. 170 // Called when the "Test2" signal is received, in the main thread.
144 void OnTest2Signal(dbus::Signal* signal) { 171 void OnTest2Signal(dbus::Signal* signal) {
145 dbus::MessageReader reader(signal); 172 dbus::MessageReader reader(signal);
146 message_loop_.Quit(); 173 message_loop_.Quit();
147 } 174 }
148 175
149 // Called when connected to the signal. 176 // Called when connected to the signal.
150 void OnConnected(const std::string& interface_name, 177 void OnConnected(const std::string& interface_name,
151 const std::string& signal_name, 178 const std::string& signal_name,
152 bool success) { 179 bool success) {
153 ASSERT_TRUE(success); 180 ASSERT_TRUE(success);
154 message_loop_.Quit(); 181 message_loop_.Quit();
155 } 182 }
156 183
157 // Wait for the hey signal to be received. 184 // Wait for the hey signal to be received.
158 void WaitForTestSignal() { 185 void WaitForTestSignal() {
159 // OnTestSignal() will quit the message loop. 186 // OnTestSignal() will quit the message loop.
160 message_loop_.Run(); 187 message_loop_.Run();
161 } 188 }
162 189
163 MessageLoop message_loop_; 190 MessageLoop message_loop_;
164 std::vector<std::string> response_strings_; 191 std::vector<std::string> response_strings_;
165 scoped_ptr<base::Thread> dbus_thread_; 192 scoped_ptr<base::Thread> dbus_thread_;
166 scoped_refptr<dbus::Bus> bus_; 193 scoped_refptr<dbus::Bus> bus_;
167 dbus::ObjectProxy* object_proxy_; 194 dbus::ObjectProxy* object_proxy_;
195 dbus::ObjectProxy* root_object_proxy_;
168 scoped_ptr<dbus::TestService> test_service_; 196 scoped_ptr<dbus::TestService> test_service_;
169 // Text message from "Test" signal. 197 // Text message from "Test" signal.
170 std::string test_signal_string_; 198 std::string test_signal_string_;
199 // Text message from "Test" signal delivered to root.
200 std::string root_test_signal_string_;
171 }; 201 };
172 202
173 TEST_F(EndToEndAsyncTest, Echo) { 203 TEST_F(EndToEndAsyncTest, Echo) {
174 const char* kHello = "hello"; 204 const char* kHello = "hello";
175 205
176 // Create the method call. 206 // Create the method call.
177 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo"); 207 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
178 dbus::MessageWriter writer(&method_call); 208 dbus::MessageWriter writer(&method_call);
179 writer.AppendString(kHello); 209 writer.AppendString(kHello);
180 210
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 // Send the test signal from the exported object. 325 // Send the test signal from the exported object.
296 test_service_->SendTestSignal(kMessage); 326 test_service_->SendTestSignal(kMessage);
297 // Receive the signal with the object proxy. The signal is handled in 327 // Receive the signal with the object proxy. The signal is handled in
298 // EndToEndAsyncTest::OnTestSignal() in the main thread. 328 // EndToEndAsyncTest::OnTestSignal() in the main thread.
299 WaitForTestSignal(); 329 WaitForTestSignal();
300 ASSERT_EQ(kMessage, test_signal_string_); 330 ASSERT_EQ(kMessage, test_signal_string_);
301 } 331 }
302 332
303 TEST_F(EndToEndAsyncTest, TestSignalFromRoot) { 333 TEST_F(EndToEndAsyncTest, TestSignalFromRoot) {
304 const char kMessage[] = "hello, world"; 334 const char kMessage[] = "hello, world";
305 // Send the test signal from the root object path, to see if we can 335 // Object proxies are tied to a particular object path, if a signal
306 // handle signals sent from "/", like dbus-send does. 336 // arrives from a different object path like "/" the first object proxy
337 // |object_proxy_| should not handle it, and should leave it for the root
338 // object proxy |root_object_proxy_|.
307 test_service_->SendTestSignalFromRoot(kMessage); 339 test_service_->SendTestSignalFromRoot(kMessage);
308 // Receive the signal with the object proxy. The signal is handled in
309 // EndToEndAsyncTest::OnTestSignal() in the main thread.
310 WaitForTestSignal(); 340 WaitForTestSignal();
311 ASSERT_EQ(kMessage, test_signal_string_); 341 // Verify the signal was not received by the specific proxy.
342 ASSERT_TRUE(test_signal_string_.empty());
343 // Verify the string WAS received by the root proxy.
344 ASSERT_EQ(kMessage, root_test_signal_string_);
312 } 345 }
OLDNEW
« no previous file with comments | « no previous file | dbus/object_proxy.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698