Roll src/third_party/WebKit f0fc3a1:b92951d (svn 201688:201689)
[chromium-blink-merge.git] / chromeos / dbus / shill_manager_client_unittest.cc
blobc9eb1a8ca3cdc1290bc3c7079631350d8ce5e51d
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_manager_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 void ExpectStringArguments(const std::vector<std::string>& arguments,
23 dbus::MessageReader* reader) {
24 for (std::vector<std::string>::const_iterator iter = arguments.begin();
25 iter != arguments.end(); ++iter) {
26 std::string arg_string;
27 ASSERT_TRUE(reader->PopString(&arg_string));
28 EXPECT_EQ(*iter, arg_string);
30 EXPECT_FALSE(reader->HasMoreData());
33 void ExpectStringArgumentsFollowedByObjectPath(
34 const std::vector<std::string>& arguments,
35 const dbus::ObjectPath& object_path,
36 dbus::MessageReader* reader) {
37 for (std::vector<std::string>::const_iterator iter = arguments.begin();
38 iter != arguments.end(); ++iter) {
39 std::string arg_string;
40 ASSERT_TRUE(reader->PopString(&arg_string));
41 EXPECT_EQ(*iter, arg_string);
43 dbus::ObjectPath path;
44 ASSERT_TRUE(reader->PopObjectPath(&path));
45 EXPECT_EQ(object_path, path);
46 EXPECT_FALSE(reader->HasMoreData());
50 } // namespace
52 class ShillManagerClientTest : public ShillClientUnittestBase {
53 public:
54 ShillManagerClientTest()
55 : ShillClientUnittestBase(shill::kFlimflamManagerInterface,
56 dbus::ObjectPath(shill::kFlimflamServicePath)) {
59 void SetUp() override {
60 ShillClientUnittestBase::SetUp();
61 // Create a client with the mock bus.
62 client_.reset(ShillManagerClient::Create());
63 client_->Init(mock_bus_.get());
64 // Run the message loop to run the signal connection result callback.
65 message_loop_.RunUntilIdle();
68 void TearDown() override { ShillClientUnittestBase::TearDown(); }
70 protected:
71 scoped_ptr<ShillManagerClient> client_;
74 TEST_F(ShillManagerClientTest, PropertyChanged) {
75 // Create a signal.
76 base::FundamentalValue kOfflineMode(true);
77 dbus::Signal signal(shill::kFlimflamManagerInterface,
78 shill::kMonitorPropertyChanged);
79 dbus::MessageWriter writer(&signal);
80 writer.AppendString(shill::kOfflineModeProperty);
81 dbus::AppendBasicTypeValueData(&writer, kOfflineMode);
83 // Set expectations.
84 MockPropertyChangeObserver observer;
85 EXPECT_CALL(observer,
86 OnPropertyChanged(shill::kOfflineModeProperty,
87 ValueEq(ByRef(kOfflineMode)))).Times(1);
89 // Add the observer
90 client_->AddPropertyChangedObserver(&observer);
92 // Run the signal callback.
93 SendPropertyChangedSignal(&signal);
95 // Remove the observer.
96 client_->RemovePropertyChangedObserver(&observer);
98 // Make sure it's not called anymore.
99 EXPECT_CALL(observer, OnPropertyChanged(_, _)).Times(0);
101 // Run the signal callback again and make sure the observer isn't called.
102 SendPropertyChangedSignal(&signal);
105 TEST_F(ShillManagerClientTest, GetProperties) {
106 // Create response.
107 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
108 dbus::MessageWriter writer(response.get());
109 dbus::MessageWriter array_writer(NULL);
110 writer.OpenArray("{sv}", &array_writer);
111 dbus::MessageWriter entry_writer(NULL);
112 array_writer.OpenDictEntry(&entry_writer);
113 entry_writer.AppendString(shill::kOfflineModeProperty);
114 entry_writer.AppendVariantOfBool(true);
115 array_writer.CloseContainer(&entry_writer);
116 writer.CloseContainer(&array_writer);
118 // Create the expected value.
119 base::DictionaryValue value;
120 value.SetWithoutPathExpansion(shill::kOfflineModeProperty,
121 new base::FundamentalValue(true));
122 // Set expectations.
123 PrepareForMethodCall(shill::kGetPropertiesFunction,
124 base::Bind(&ExpectNoArgument),
125 response.get());
126 // Call method.
127 client_->GetProperties(base::Bind(&ExpectDictionaryValueResult, &value));
128 // Run the message loop.
129 message_loop_.RunUntilIdle();
132 TEST_F(ShillManagerClientTest, GetNetworksForGeolocation) {
133 // Create response.
134 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
136 dbus::MessageWriter writer(response.get());
137 dbus::MessageWriter type_dict_writer(NULL);
138 writer.OpenArray("{sv}", &type_dict_writer);
139 dbus::MessageWriter type_entry_writer(NULL);
140 type_dict_writer.OpenDictEntry(&type_entry_writer);
141 type_entry_writer.AppendString(shill::kTypeWifi);
142 dbus::MessageWriter variant_writer(NULL);
143 type_entry_writer.OpenVariant("aa{ss}", &variant_writer);
144 dbus::MessageWriter wap_list_writer(NULL);
145 variant_writer.OpenArray("a{ss}", &wap_list_writer);
146 dbus::MessageWriter property_dict_writer(NULL);
147 wap_list_writer.OpenArray("{ss}", &property_dict_writer);
148 dbus::MessageWriter property_entry_writer(NULL);
149 property_dict_writer.OpenDictEntry(&property_entry_writer);
150 property_entry_writer.AppendString(shill::kGeoMacAddressProperty);
151 property_entry_writer.AppendString("01:23:45:67:89:AB");
152 property_dict_writer.CloseContainer(&property_entry_writer);
153 wap_list_writer.CloseContainer(&property_dict_writer);
154 variant_writer.CloseContainer(&wap_list_writer);
155 type_entry_writer.CloseContainer(&wap_list_writer);
156 type_dict_writer.CloseContainer(&type_entry_writer);
157 writer.CloseContainer(&type_dict_writer);
160 // Create the expected value.
161 base::DictionaryValue type_dict_value;
162 base::ListValue* type_entry_value = new base::ListValue;
163 base::DictionaryValue* property_dict_value = new base::DictionaryValue;
164 property_dict_value->SetWithoutPathExpansion(
165 shill::kGeoMacAddressProperty,
166 new base::StringValue("01:23:45:67:89:AB"));
167 type_entry_value->Append(property_dict_value);
168 type_dict_value.SetWithoutPathExpansion("wifi", type_entry_value);
170 // Set expectations.
171 PrepareForMethodCall(shill::kGetNetworksForGeolocation,
172 base::Bind(&ExpectNoArgument),
173 response.get());
174 // Call method.
175 client_->GetNetworksForGeolocation(base::Bind(&ExpectDictionaryValueResult,
176 &type_dict_value));
178 // Run the message loop.
179 message_loop_.RunUntilIdle();
182 TEST_F(ShillManagerClientTest, SetProperty) {
183 // Create response.
184 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
185 // Set expectations.
186 base::StringValue value("portal list");
187 PrepareForMethodCall(shill::kSetPropertyFunction,
188 base::Bind(ExpectStringAndValueArguments,
189 shill::kCheckPortalListProperty,
190 &value),
191 response.get());
192 // Call method.
193 MockClosure mock_closure;
194 MockErrorCallback mock_error_callback;
195 client_->SetProperty(shill::kCheckPortalListProperty,
196 value,
197 mock_closure.GetCallback(),
198 mock_error_callback.GetCallback());
199 EXPECT_CALL(mock_closure, Run()).Times(1);
200 EXPECT_CALL(mock_error_callback, Run(_, _)).Times(0);
202 // Run the message loop.
203 message_loop_.RunUntilIdle();
206 TEST_F(ShillManagerClientTest, RequestScan) {
207 // Create response.
208 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
209 // Set expectations.
210 PrepareForMethodCall(shill::kRequestScanFunction,
211 base::Bind(&ExpectStringArgument, shill::kTypeWifi),
212 response.get());
213 // Call method.
214 MockClosure mock_closure;
215 MockErrorCallback mock_error_callback;
216 client_->RequestScan(shill::kTypeWifi,
217 mock_closure.GetCallback(),
218 mock_error_callback.GetCallback());
219 EXPECT_CALL(mock_closure, Run()).Times(1);
220 EXPECT_CALL(mock_error_callback, Run(_, _)).Times(0);
222 // Run the message loop.
223 message_loop_.RunUntilIdle();
226 TEST_F(ShillManagerClientTest, EnableTechnology) {
227 // Create response.
228 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
229 // Set expectations.
230 PrepareForMethodCall(shill::kEnableTechnologyFunction,
231 base::Bind(&ExpectStringArgument, shill::kTypeWifi),
232 response.get());
233 // Call method.
234 MockClosure mock_closure;
235 MockErrorCallback mock_error_callback;
236 client_->EnableTechnology(shill::kTypeWifi,
237 mock_closure.GetCallback(),
238 mock_error_callback.GetCallback());
239 EXPECT_CALL(mock_closure, Run()).Times(1);
240 EXPECT_CALL(mock_error_callback, Run(_, _)).Times(0);
242 // Run the message loop.
243 message_loop_.RunUntilIdle();
246 TEST_F(ShillManagerClientTest, DisableTechnology) {
247 // Create response.
248 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
249 // Set expectations.
250 PrepareForMethodCall(shill::kDisableTechnologyFunction,
251 base::Bind(&ExpectStringArgument, shill::kTypeWifi),
252 response.get());
253 // Call method.
254 MockClosure mock_closure;
255 MockErrorCallback mock_error_callback;
256 client_->DisableTechnology(shill::kTypeWifi,
257 mock_closure.GetCallback(),
258 mock_error_callback.GetCallback());
259 EXPECT_CALL(mock_closure, Run()).Times(1);
260 EXPECT_CALL(mock_error_callback, Run(_, _)).Times(0);
262 // Run the message loop.
263 message_loop_.RunUntilIdle();
266 TEST_F(ShillManagerClientTest, ConfigureService) {
267 // Create response.
268 const dbus::ObjectPath object_path("/");
269 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
270 dbus::MessageWriter writer(response.get());
271 writer.AppendObjectPath(object_path);
272 // Create the argument dictionary.
273 scoped_ptr<base::DictionaryValue> arg(CreateExampleServiceProperties());
274 // Use a variant valued dictionary rather than a string valued one.
275 const bool string_valued = false;
276 // Set expectations.
277 PrepareForMethodCall(
278 shill::kConfigureServiceFunction,
279 base::Bind(&ExpectDictionaryValueArgument, arg.get(), string_valued),
280 response.get());
281 // Call method.
282 MockErrorCallback mock_error_callback;
283 client_->ConfigureService(*arg,
284 base::Bind(&ExpectObjectPathResultWithoutStatus,
285 object_path),
286 mock_error_callback.GetCallback());
287 EXPECT_CALL(mock_error_callback, Run(_, _)).Times(0);
289 // Run the message loop.
290 message_loop_.RunUntilIdle();
293 TEST_F(ShillManagerClientTest, GetService) {
294 // Create response.
295 const dbus::ObjectPath object_path("/");
296 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
297 dbus::MessageWriter writer(response.get());
298 writer.AppendObjectPath(object_path);
299 // Create the argument dictionary.
300 scoped_ptr<base::DictionaryValue> arg(CreateExampleServiceProperties());
301 // Use a variant valued dictionary rather than a string valued one.
302 const bool string_valued = false;
303 // Set expectations.
304 PrepareForMethodCall(
305 shill::kGetServiceFunction,
306 base::Bind(&ExpectDictionaryValueArgument, arg.get(), string_valued),
307 response.get());
308 // Call method.
309 MockErrorCallback mock_error_callback;
310 client_->GetService(*arg,
311 base::Bind(&ExpectObjectPathResultWithoutStatus,
312 object_path),
313 mock_error_callback.GetCallback());
314 EXPECT_CALL(mock_error_callback, Run(_, _)).Times(0);
316 // Run the message loop.
317 message_loop_.RunUntilIdle();
320 TEST_F(ShillManagerClientTest, VerifyDestination) {
321 // Create response.
322 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
323 dbus::MessageWriter writer(response.get());
324 bool expected = true;
325 writer.AppendBool(expected);
326 // Set expectations.
327 std::vector<std::string> arguments;
328 arguments.push_back("certificate");
329 arguments.push_back("public_key");
330 arguments.push_back("nonce");
331 arguments.push_back("signed_data");
332 arguments.push_back("device_serial");
333 arguments.push_back("device_ssid");
334 arguments.push_back("device_bssid");
335 PrepareForMethodCall(shill::kVerifyDestinationFunction,
336 base::Bind(&ExpectStringArguments, arguments),
337 response.get());
339 // Call method.
340 MockErrorCallback mock_error_callback;
341 ShillManagerClient::VerificationProperties properties;
342 properties.certificate = arguments[0];
343 properties.public_key = arguments[1];
344 properties.nonce = arguments[2];
345 properties.signed_data = arguments[3];
346 properties.device_serial = arguments[4];
347 properties.device_ssid = arguments[5];
348 properties.device_bssid = arguments[6];
349 client_->VerifyDestination(
350 properties,
351 base::Bind(&ExpectBoolResultWithoutStatus, expected),
352 mock_error_callback.GetCallback());
353 EXPECT_CALL(mock_error_callback, Run(_, _)).Times(0);
355 // Run the message loop.
356 message_loop_.RunUntilIdle();
359 TEST_F(ShillManagerClientTest, VerifyAndEncryptCredentials) {
360 // Create response.
361 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
362 dbus::MessageWriter writer(response.get());
363 std::string expected = "encrypted_credentials";
364 writer.AppendString(expected);
365 // Set expectations.
366 std::vector<std::string> arguments;
367 arguments.push_back("certificate");
368 arguments.push_back("public_key");
369 arguments.push_back("nonce");
370 arguments.push_back("signed_data");
371 arguments.push_back("device_serial");
372 arguments.push_back("device_ssid");
373 arguments.push_back("device_bssid");
374 std::string service_path = "/";
375 dbus::ObjectPath service_path_obj(service_path);
376 PrepareForMethodCall(shill::kVerifyAndEncryptCredentialsFunction,
377 base::Bind(&ExpectStringArgumentsFollowedByObjectPath,
378 arguments,
379 service_path_obj),
380 response.get());
382 // Call method.
383 MockErrorCallback mock_error_callback;
384 ShillManagerClient::VerificationProperties properties;
385 properties.certificate = arguments[0];
386 properties.public_key = arguments[1];
387 properties.nonce = arguments[2];
388 properties.signed_data = arguments[3];
389 properties.device_serial = arguments[4];
390 properties.device_ssid = arguments[5];
391 properties.device_bssid = arguments[6];
392 client_->VerifyAndEncryptCredentials(
393 properties,
394 service_path,
395 base::Bind(&ExpectStringResultWithoutStatus, expected),
396 mock_error_callback.GetCallback());
397 EXPECT_CALL(mock_error_callback, Run(_, _)).Times(0);
399 // Run the message loop.
400 message_loop_.RunUntilIdle();
403 TEST_F(ShillManagerClientTest, VerifyAndEncryptData) {
404 // Create response.
405 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
406 dbus::MessageWriter writer(response.get());
407 std::string expected = "encrypted_data";
408 writer.AppendString(expected);
409 // Set expectations.
410 std::vector<std::string> arguments;
411 arguments.push_back("certificate");
412 arguments.push_back("public_key");
413 arguments.push_back("nonce");
414 arguments.push_back("signed_data");
415 arguments.push_back("device_serial");
416 arguments.push_back("device_ssid");
417 arguments.push_back("device_bssid");
418 arguments.push_back("data");
419 PrepareForMethodCall(shill::kVerifyAndEncryptDataFunction,
420 base::Bind(&ExpectStringArguments, arguments),
421 response.get());
423 // Call method.
424 MockErrorCallback mock_error_callback;
425 ShillManagerClient::VerificationProperties properties;
426 properties.certificate = arguments[0];
427 properties.public_key = arguments[1];
428 properties.nonce = arguments[2];
429 properties.signed_data = arguments[3];
430 properties.device_serial = arguments[4];
431 properties.device_ssid = arguments[5];
432 properties.device_bssid = arguments[6];
433 client_->VerifyAndEncryptData(
434 properties,
435 arguments[7],
436 base::Bind(&ExpectStringResultWithoutStatus, expected),
437 mock_error_callback.GetCallback());
438 EXPECT_CALL(mock_error_callback, Run(_, _)).Times(0);
440 // Run the message loop.
441 message_loop_.RunUntilIdle();
444 } // namespace chromeos