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.
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"
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());
52 class ShillManagerClientTest
: public ShillClientUnittestBase
{
54 ShillManagerClientTest()
55 : ShillClientUnittestBase(shill::kFlimflamManagerInterface
,
56 dbus::ObjectPath(shill::kFlimflamServicePath
)) {
59 virtual void SetUp() {
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 virtual void TearDown() {
69 ShillClientUnittestBase::TearDown();
73 scoped_ptr
<ShillManagerClient
> client_
;
76 TEST_F(ShillManagerClientTest
, PropertyChanged
) {
78 base::FundamentalValue
kOfflineMode(true);
79 dbus::Signal
signal(shill::kFlimflamManagerInterface
,
80 shill::kMonitorPropertyChanged
);
81 dbus::MessageWriter
writer(&signal
);
82 writer
.AppendString(shill::kOfflineModeProperty
);
83 dbus::AppendBasicTypeValueData(&writer
, kOfflineMode
);
86 MockPropertyChangeObserver observer
;
88 OnPropertyChanged(shill::kOfflineModeProperty
,
89 ValueEq(ByRef(kOfflineMode
)))).Times(1);
92 client_
->AddPropertyChangedObserver(&observer
);
94 // Run the signal callback.
95 SendPropertyChangedSignal(&signal
);
97 // Remove the observer.
98 client_
->RemovePropertyChangedObserver(&observer
);
100 // Make sure it's not called anymore.
101 EXPECT_CALL(observer
, OnPropertyChanged(_
, _
)).Times(0);
103 // Run the signal callback again and make sure the observer isn't called.
104 SendPropertyChangedSignal(&signal
);
107 TEST_F(ShillManagerClientTest
, GetProperties
) {
109 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
110 dbus::MessageWriter
writer(response
.get());
111 dbus::MessageWriter
array_writer(NULL
);
112 writer
.OpenArray("{sv}", &array_writer
);
113 dbus::MessageWriter
entry_writer(NULL
);
114 array_writer
.OpenDictEntry(&entry_writer
);
115 entry_writer
.AppendString(shill::kOfflineModeProperty
);
116 entry_writer
.AppendVariantOfBool(true);
117 array_writer
.CloseContainer(&entry_writer
);
118 writer
.CloseContainer(&array_writer
);
120 // Create the expected value.
121 base::DictionaryValue value
;
122 value
.SetWithoutPathExpansion(shill::kOfflineModeProperty
,
123 new base::FundamentalValue(true));
125 PrepareForMethodCall(shill::kGetPropertiesFunction
,
126 base::Bind(&ExpectNoArgument
),
129 client_
->GetProperties(base::Bind(&ExpectDictionaryValueResult
, &value
));
130 // Run the message loop.
131 message_loop_
.RunUntilIdle();
134 TEST_F(ShillManagerClientTest
, GetNetworksForGeolocation
) {
136 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
138 dbus::MessageWriter
writer(response
.get());
139 dbus::MessageWriter
type_dict_writer(NULL
);
140 writer
.OpenArray("{sv}", &type_dict_writer
);
141 dbus::MessageWriter
type_entry_writer(NULL
);
142 type_dict_writer
.OpenDictEntry(&type_entry_writer
);
143 type_entry_writer
.AppendString(shill::kTypeWifi
);
144 dbus::MessageWriter
variant_writer(NULL
);
145 type_entry_writer
.OpenVariant("aa{ss}", &variant_writer
);
146 dbus::MessageWriter
wap_list_writer(NULL
);
147 variant_writer
.OpenArray("a{ss}", &wap_list_writer
);
148 dbus::MessageWriter
property_dict_writer(NULL
);
149 wap_list_writer
.OpenArray("{ss}", &property_dict_writer
);
150 dbus::MessageWriter
property_entry_writer(NULL
);
151 property_dict_writer
.OpenDictEntry(&property_entry_writer
);
152 property_entry_writer
.AppendString(shill::kGeoMacAddressProperty
);
153 property_entry_writer
.AppendString("01:23:45:67:89:AB");
154 property_dict_writer
.CloseContainer(&property_entry_writer
);
155 wap_list_writer
.CloseContainer(&property_dict_writer
);
156 variant_writer
.CloseContainer(&wap_list_writer
);
157 type_entry_writer
.CloseContainer(&wap_list_writer
);
158 type_dict_writer
.CloseContainer(&type_entry_writer
);
159 writer
.CloseContainer(&type_dict_writer
);
162 // Create the expected value.
163 base::DictionaryValue type_dict_value
;
164 base::ListValue
* type_entry_value
= new base::ListValue
;
165 base::DictionaryValue
* property_dict_value
= new base::DictionaryValue
;
166 property_dict_value
->SetWithoutPathExpansion(
167 shill::kGeoMacAddressProperty
,
168 new base::StringValue("01:23:45:67:89:AB"));
169 type_entry_value
->Append(property_dict_value
);
170 type_dict_value
.SetWithoutPathExpansion("wifi", type_entry_value
);
173 PrepareForMethodCall(shill::kGetNetworksForGeolocation
,
174 base::Bind(&ExpectNoArgument
),
177 client_
->GetNetworksForGeolocation(base::Bind(&ExpectDictionaryValueResult
,
180 // Run the message loop.
181 message_loop_
.RunUntilIdle();
184 TEST_F(ShillManagerClientTest
, SetProperty
) {
186 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
188 base::StringValue
value("portal list");
189 PrepareForMethodCall(shill::kSetPropertyFunction
,
190 base::Bind(ExpectStringAndValueArguments
,
191 shill::kCheckPortalListProperty
,
195 MockClosure mock_closure
;
196 MockErrorCallback mock_error_callback
;
197 client_
->SetProperty(shill::kCheckPortalListProperty
,
199 mock_closure
.GetCallback(),
200 mock_error_callback
.GetCallback());
201 EXPECT_CALL(mock_closure
, Run()).Times(1);
202 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
204 // Run the message loop.
205 message_loop_
.RunUntilIdle();
208 TEST_F(ShillManagerClientTest
, RequestScan
) {
210 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
212 PrepareForMethodCall(shill::kRequestScanFunction
,
213 base::Bind(&ExpectStringArgument
, shill::kTypeWifi
),
216 MockClosure mock_closure
;
217 MockErrorCallback mock_error_callback
;
218 client_
->RequestScan(shill::kTypeWifi
,
219 mock_closure
.GetCallback(),
220 mock_error_callback
.GetCallback());
221 EXPECT_CALL(mock_closure
, Run()).Times(1);
222 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
224 // Run the message loop.
225 message_loop_
.RunUntilIdle();
228 TEST_F(ShillManagerClientTest
, EnableTechnology
) {
230 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
232 PrepareForMethodCall(shill::kEnableTechnologyFunction
,
233 base::Bind(&ExpectStringArgument
, shill::kTypeWifi
),
236 MockClosure mock_closure
;
237 MockErrorCallback mock_error_callback
;
238 client_
->EnableTechnology(shill::kTypeWifi
,
239 mock_closure
.GetCallback(),
240 mock_error_callback
.GetCallback());
241 EXPECT_CALL(mock_closure
, Run()).Times(1);
242 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
244 // Run the message loop.
245 message_loop_
.RunUntilIdle();
248 TEST_F(ShillManagerClientTest
, DisableTechnology
) {
250 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
252 PrepareForMethodCall(shill::kDisableTechnologyFunction
,
253 base::Bind(&ExpectStringArgument
, shill::kTypeWifi
),
256 MockClosure mock_closure
;
257 MockErrorCallback mock_error_callback
;
258 client_
->DisableTechnology(shill::kTypeWifi
,
259 mock_closure
.GetCallback(),
260 mock_error_callback
.GetCallback());
261 EXPECT_CALL(mock_closure
, Run()).Times(1);
262 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
264 // Run the message loop.
265 message_loop_
.RunUntilIdle();
268 TEST_F(ShillManagerClientTest
, ConfigureService
) {
270 const dbus::ObjectPath
object_path("/");
271 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
272 dbus::MessageWriter
writer(response
.get());
273 writer
.AppendObjectPath(object_path
);
274 // Create the argument dictionary.
275 scoped_ptr
<base::DictionaryValue
> arg(CreateExampleServiceProperties());
276 // Use a variant valued dictionary rather than a string valued one.
277 const bool string_valued
= false;
279 PrepareForMethodCall(
280 shill::kConfigureServiceFunction
,
281 base::Bind(&ExpectDictionaryValueArgument
, arg
.get(), string_valued
),
284 MockErrorCallback mock_error_callback
;
285 client_
->ConfigureService(*arg
,
286 base::Bind(&ExpectObjectPathResultWithoutStatus
,
288 mock_error_callback
.GetCallback());
289 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
291 // Run the message loop.
292 message_loop_
.RunUntilIdle();
295 TEST_F(ShillManagerClientTest
, GetService
) {
297 const dbus::ObjectPath
object_path("/");
298 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
299 dbus::MessageWriter
writer(response
.get());
300 writer
.AppendObjectPath(object_path
);
301 // Create the argument dictionary.
302 scoped_ptr
<base::DictionaryValue
> arg(CreateExampleServiceProperties());
303 // Use a variant valued dictionary rather than a string valued one.
304 const bool string_valued
= false;
306 PrepareForMethodCall(
307 shill::kGetServiceFunction
,
308 base::Bind(&ExpectDictionaryValueArgument
, arg
.get(), string_valued
),
311 MockErrorCallback mock_error_callback
;
312 client_
->GetService(*arg
,
313 base::Bind(&ExpectObjectPathResultWithoutStatus
,
315 mock_error_callback
.GetCallback());
316 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
318 // Run the message loop.
319 message_loop_
.RunUntilIdle();
322 TEST_F(ShillManagerClientTest
, VerifyDestination
) {
324 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
325 dbus::MessageWriter
writer(response
.get());
326 bool expected
= true;
327 writer
.AppendBool(expected
);
329 std::vector
<std::string
> arguments
;
330 arguments
.push_back("certificate");
331 arguments
.push_back("public_key");
332 arguments
.push_back("nonce");
333 arguments
.push_back("signed_data");
334 arguments
.push_back("device_serial");
335 arguments
.push_back("device_ssid");
336 arguments
.push_back("device_bssid");
337 PrepareForMethodCall(shill::kVerifyDestinationFunction
,
338 base::Bind(&ExpectStringArguments
, arguments
),
342 MockErrorCallback mock_error_callback
;
343 ShillManagerClient::VerificationProperties properties
;
344 properties
.certificate
= arguments
[0];
345 properties
.public_key
= arguments
[1];
346 properties
.nonce
= arguments
[2];
347 properties
.signed_data
= arguments
[3];
348 properties
.device_serial
= arguments
[4];
349 properties
.device_ssid
= arguments
[5];
350 properties
.device_bssid
= arguments
[6];
351 client_
->VerifyDestination(
353 base::Bind(&ExpectBoolResultWithoutStatus
, expected
),
354 mock_error_callback
.GetCallback());
355 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
357 // Run the message loop.
358 message_loop_
.RunUntilIdle();
361 TEST_F(ShillManagerClientTest
, VerifyAndEncryptCredentials
) {
363 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
364 dbus::MessageWriter
writer(response
.get());
365 std::string expected
= "encrypted_credentials";
366 writer
.AppendString(expected
);
368 std::vector
<std::string
> arguments
;
369 arguments
.push_back("certificate");
370 arguments
.push_back("public_key");
371 arguments
.push_back("nonce");
372 arguments
.push_back("signed_data");
373 arguments
.push_back("device_serial");
374 arguments
.push_back("device_ssid");
375 arguments
.push_back("device_bssid");
376 std::string service_path
= "/";
377 dbus::ObjectPath
service_path_obj(service_path
);
378 PrepareForMethodCall(shill::kVerifyAndEncryptCredentialsFunction
,
379 base::Bind(&ExpectStringArgumentsFollowedByObjectPath
,
385 MockErrorCallback mock_error_callback
;
386 ShillManagerClient::VerificationProperties properties
;
387 properties
.certificate
= arguments
[0];
388 properties
.public_key
= arguments
[1];
389 properties
.nonce
= arguments
[2];
390 properties
.signed_data
= arguments
[3];
391 properties
.device_serial
= arguments
[4];
392 properties
.device_ssid
= arguments
[5];
393 properties
.device_bssid
= arguments
[6];
394 client_
->VerifyAndEncryptCredentials(
397 base::Bind(&ExpectStringResultWithoutStatus
, expected
),
398 mock_error_callback
.GetCallback());
399 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
401 // Run the message loop.
402 message_loop_
.RunUntilIdle();
405 TEST_F(ShillManagerClientTest
, VerifyAndEncryptData
) {
407 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
408 dbus::MessageWriter
writer(response
.get());
409 std::string expected
= "encrypted_data";
410 writer
.AppendString(expected
);
412 std::vector
<std::string
> arguments
;
413 arguments
.push_back("certificate");
414 arguments
.push_back("public_key");
415 arguments
.push_back("nonce");
416 arguments
.push_back("signed_data");
417 arguments
.push_back("device_serial");
418 arguments
.push_back("device_ssid");
419 arguments
.push_back("device_bssid");
420 arguments
.push_back("data");
421 PrepareForMethodCall(shill::kVerifyAndEncryptDataFunction
,
422 base::Bind(&ExpectStringArguments
, arguments
),
426 MockErrorCallback mock_error_callback
;
427 ShillManagerClient::VerificationProperties properties
;
428 properties
.certificate
= arguments
[0];
429 properties
.public_key
= arguments
[1];
430 properties
.nonce
= arguments
[2];
431 properties
.signed_data
= arguments
[3];
432 properties
.device_serial
= arguments
[4];
433 properties
.device_ssid
= arguments
[5];
434 properties
.device_bssid
= arguments
[6];
435 client_
->VerifyAndEncryptData(
438 base::Bind(&ExpectStringResultWithoutStatus
, expected
),
439 mock_error_callback
.GetCallback());
440 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
442 // Run the message loop.
443 message_loop_
.RunUntilIdle();
446 } // namespace chromeos