Allow trusted brokers to restrict connections for spawned applications to whitelisted...
[chromium-blink-merge.git] / chromeos / dbus / shill_service_client_unittest.cc
blob0e03578d473976fa0c4ddc5bc47f7cc039461360
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/values.h"
7 #include "chromeos/dbus/shill_client_unittest_base.h"
8 #include "chromeos/dbus/shill_service_client.h"
9 #include "dbus/message.h"
10 #include "dbus/object_path.h"
11 #include "dbus/values_util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/cros_system_api/dbus/service_constants.h"
15 using testing::_;
16 using testing::ByRef;
18 namespace chromeos {
20 namespace {
22 const char kExampleServicePath[] = "/foo/bar";
24 } // namespace
26 class ShillServiceClientTest : public ShillClientUnittestBase {
27 public:
28 ShillServiceClientTest()
29 : ShillClientUnittestBase(shill::kFlimflamServiceInterface,
30 dbus::ObjectPath(kExampleServicePath)) {
33 void SetUp() override {
34 ShillClientUnittestBase::SetUp();
35 // Create a client with the mock bus.
36 client_.reset(ShillServiceClient::Create());
37 client_->Init(mock_bus_.get());
38 // Run the message loop to run the signal connection result callback.
39 message_loop_.RunUntilIdle();
42 void TearDown() override { ShillClientUnittestBase::TearDown(); }
44 protected:
45 scoped_ptr<ShillServiceClient> client_;
48 TEST_F(ShillServiceClientTest, PropertyChanged) {
49 const int kValue = 42;
50 // Create a signal.
51 dbus::Signal signal(shill::kFlimflamServiceInterface,
52 shill::kMonitorPropertyChanged);
53 dbus::MessageWriter writer(&signal);
54 writer.AppendString(shill::kSignalStrengthProperty);
55 writer.AppendVariantOfByte(kValue);
57 // Set expectations.
58 const base::FundamentalValue value(kValue);
59 MockPropertyChangeObserver observer;
60 EXPECT_CALL(observer,
61 OnPropertyChanged(
62 shill::kSignalStrengthProperty,
63 ValueEq(ByRef(value)))).Times(1);
65 // Add the observer
66 client_->AddPropertyChangedObserver(
67 dbus::ObjectPath(kExampleServicePath),
68 &observer);
70 // Run the signal callback.
71 SendPropertyChangedSignal(&signal);
73 // Remove the observer.
74 client_->RemovePropertyChangedObserver(
75 dbus::ObjectPath(kExampleServicePath),
76 &observer);
78 EXPECT_CALL(observer, OnPropertyChanged(_, _)).Times(0);
80 // Run the signal callback again and make sure the observer isn't called.
81 SendPropertyChangedSignal(&signal);
84 TEST_F(ShillServiceClientTest, GetProperties) {
85 const int kValue = 42;
86 // Create response.
87 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
88 dbus::MessageWriter writer(response.get());
89 dbus::MessageWriter array_writer(NULL);
90 writer.OpenArray("{sv}", &array_writer);
91 dbus::MessageWriter entry_writer(NULL);
92 array_writer.OpenDictEntry(&entry_writer);
93 entry_writer.AppendString(shill::kSignalStrengthProperty);
94 entry_writer.AppendVariantOfByte(kValue);
95 array_writer.CloseContainer(&entry_writer);
96 writer.CloseContainer(&array_writer);
98 // Set expectations.
99 base::DictionaryValue value;
100 value.SetWithoutPathExpansion(shill::kSignalStrengthProperty,
101 new base::FundamentalValue(kValue));
102 PrepareForMethodCall(shill::kGetPropertiesFunction,
103 base::Bind(&ExpectNoArgument),
104 response.get());
105 // Call method.
106 client_->GetProperties(dbus::ObjectPath(kExampleServicePath),
107 base::Bind(&ExpectDictionaryValueResult, &value));
108 // Run the message loop.
109 message_loop_.RunUntilIdle();
112 TEST_F(ShillServiceClientTest, SetProperty) {
113 const char kValue[] = "passphrase";
114 // Create response.
115 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
117 // Set expectations.
118 const base::StringValue value(kValue);
119 PrepareForMethodCall(shill::kSetPropertyFunction,
120 base::Bind(&ExpectStringAndValueArguments,
121 shill::kPassphraseProperty,
122 &value),
123 response.get());
124 // Call method.
125 MockClosure mock_closure;
126 MockErrorCallback mock_error_callback;
127 client_->SetProperty(dbus::ObjectPath(kExampleServicePath),
128 shill::kPassphraseProperty,
129 value,
130 mock_closure.GetCallback(),
131 mock_error_callback.GetCallback());
132 EXPECT_CALL(mock_closure, Run()).Times(1);
133 EXPECT_CALL(mock_error_callback, Run(_, _)).Times(0);
135 // Run the message loop.
136 message_loop_.RunUntilIdle();
139 TEST_F(ShillServiceClientTest, SetProperties) {
140 // Create response.
141 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
143 // Set expectations.
144 scoped_ptr<base::DictionaryValue> arg(CreateExampleServiceProperties());
145 // Use a variant valued dictionary rather than a string valued one.
146 const bool string_valued = false;
147 PrepareForMethodCall(
148 shill::kSetPropertiesFunction,
149 base::Bind(&ExpectDictionaryValueArgument, arg.get(), string_valued),
150 response.get());
152 // Call method.
153 MockClosure mock_closure;
154 MockErrorCallback mock_error_callback;
155 client_->SetProperties(dbus::ObjectPath(kExampleServicePath),
156 *arg,
157 mock_closure.GetCallback(),
158 mock_error_callback.GetCallback());
159 EXPECT_CALL(mock_closure, Run()).Times(1);
160 EXPECT_CALL(mock_error_callback, Run(_, _)).Times(0);
162 // Run the message loop.
163 message_loop_.RunUntilIdle();
166 TEST_F(ShillServiceClientTest, ClearProperty) {
167 // Create response.
168 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
170 // Set expectations.
171 PrepareForMethodCall(shill::kClearPropertyFunction,
172 base::Bind(&ExpectStringArgument,
173 shill::kPassphraseProperty),
174 response.get());
175 // Call method.
176 MockClosure mock_closure;
177 MockErrorCallback mock_error_callback;
178 client_->ClearProperty(dbus::ObjectPath(kExampleServicePath),
179 shill::kPassphraseProperty,
180 mock_closure.GetCallback(),
181 mock_error_callback.GetCallback());
182 EXPECT_CALL(mock_closure, Run()).Times(1);
183 EXPECT_CALL(mock_error_callback, Run(_, _)).Times(0);
185 // Run the message loop.
186 message_loop_.RunUntilIdle();
189 TEST_F(ShillServiceClientTest, ClearProperties) {
190 // Create response.
191 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
192 dbus::MessageWriter writer(response.get());
193 dbus::MessageWriter array_writer(NULL);
194 writer.OpenArray("b", &array_writer);
195 array_writer.AppendBool(true);
196 array_writer.AppendBool(true);
197 writer.CloseContainer(&array_writer);
199 // Set expectations.
200 std::vector<std::string> keys;
201 keys.push_back(shill::kPassphraseProperty);
202 keys.push_back(shill::kSignalStrengthProperty);
203 PrepareForMethodCall(shill::kClearPropertiesFunction,
204 base::Bind(&ExpectArrayOfStringsArgument, keys),
205 response.get());
206 // Call method.
207 MockListValueCallback mock_list_value_callback;
208 MockErrorCallback mock_error_callback;
209 client_->ClearProperties(dbus::ObjectPath(kExampleServicePath),
210 keys,
211 mock_list_value_callback.GetCallback(),
212 mock_error_callback.GetCallback());
213 EXPECT_CALL(mock_list_value_callback, Run(_)).Times(1);
214 EXPECT_CALL(mock_error_callback, Run(_, _)).Times(0);
216 // Run the message loop.
217 message_loop_.RunUntilIdle();
220 TEST_F(ShillServiceClientTest, Connect) {
221 // Create response.
222 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
224 // Set expectations.
225 MockClosure mock_closure;
226 MockErrorCallback mock_error_callback;
227 PrepareForMethodCall(shill::kConnectFunction,
228 base::Bind(&ExpectNoArgument),
229 response.get());
230 EXPECT_CALL(mock_closure, Run()).Times(1);
231 // Call method.
232 client_->Connect(dbus::ObjectPath(kExampleServicePath),
233 mock_closure.GetCallback(),
234 mock_error_callback.GetCallback());
236 // Run the message loop.
237 message_loop_.RunUntilIdle();
240 TEST_F(ShillServiceClientTest, Disconnect) {
241 // Create response.
242 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
244 // Set expectations.
245 PrepareForMethodCall(shill::kDisconnectFunction,
246 base::Bind(&ExpectNoArgument),
247 response.get());
248 // Call method.
249 MockClosure mock_closure;
250 MockErrorCallback mock_error_callback;
251 client_->Disconnect(dbus::ObjectPath(kExampleServicePath),
252 mock_closure.GetCallback(),
253 mock_error_callback.GetCallback());
254 EXPECT_CALL(mock_closure, Run()).Times(1);
255 EXPECT_CALL(mock_error_callback, Run(_, _)).Times(0);
257 // Run the message loop.
258 message_loop_.RunUntilIdle();
261 TEST_F(ShillServiceClientTest, Remove) {
262 // Create response.
263 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
265 // Set expectations.
266 PrepareForMethodCall(shill::kRemoveServiceFunction,
267 base::Bind(&ExpectNoArgument),
268 response.get());
269 // Call method.
270 MockClosure mock_closure;
271 MockErrorCallback mock_error_callback;
272 client_->Remove(dbus::ObjectPath(kExampleServicePath),
273 mock_closure.GetCallback(),
274 mock_error_callback.GetCallback());
275 EXPECT_CALL(mock_closure, Run()).Times(1);
276 EXPECT_CALL(mock_error_callback, Run(_, _)).Times(0);
278 // Run the message loop.
279 message_loop_.RunUntilIdle();
282 TEST_F(ShillServiceClientTest, ActivateCellularModem) {
283 const char kCarrier[] = "carrier";
284 // Create response.
285 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
287 // Set expectations.
288 PrepareForMethodCall(shill::kActivateCellularModemFunction,
289 base::Bind(&ExpectStringArgument, kCarrier),
290 response.get());
291 // Call method.
292 MockClosure mock_closure;
293 MockErrorCallback mock_error_callback;
294 client_->ActivateCellularModem(dbus::ObjectPath(kExampleServicePath),
295 kCarrier,
296 mock_closure.GetCallback(),
297 mock_error_callback.GetCallback());
298 EXPECT_CALL(mock_closure, Run()).Times(1);
299 EXPECT_CALL(mock_error_callback, Run(_, _)).Times(0);
301 // Run the message loop.
302 message_loop_.RunUntilIdle();
305 } // namespace chromeos