1 // Copyright 2013 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/memory/scoped_ptr.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/values.h"
9 #include "chromeos/dbus/fake_dbus_thread_manager.h"
10 #include "chromeos/dbus/fake_shill_device_client.h"
11 #include "chromeos/dbus/fake_shill_manager_client.h"
12 #include "chromeos/network/network_device_handler_impl.h"
13 #include "chromeos/network/network_state_handler.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/cros_system_api/dbus/service_constants.h"
21 const char kDefaultCellularDevicePath
[] = "stub_cellular_device";
22 const char kUnknownCellularDevicePath
[] = "unknown_cellular_device";
23 const char kDefaultWifiDevicePath
[] = "stub_wifi_device";
24 const char kResultSuccess
[] = "success";
28 class NetworkDeviceHandlerTest
: public testing::Test
{
30 NetworkDeviceHandlerTest() : fake_device_client_(NULL
) {}
31 virtual ~NetworkDeviceHandlerTest() {}
33 virtual void SetUp() OVERRIDE
{
34 FakeDBusThreadManager
* dbus_manager
= new FakeDBusThreadManager
;
35 dbus_manager
->SetFakeShillClients();
37 fake_device_client_
= new FakeShillDeviceClient
;
38 dbus_manager
->SetShillDeviceClient(
39 scoped_ptr
<ShillDeviceClient
>(fake_device_client_
));
40 DBusThreadManager::InitializeForTesting(dbus_manager
);
42 success_callback_
= base::Bind(&NetworkDeviceHandlerTest::SuccessCallback
,
43 base::Unretained(this));
44 properties_success_callback_
=
45 base::Bind(&NetworkDeviceHandlerTest::PropertiesSuccessCallback
,
46 base::Unretained(this));
47 string_success_callback_
=
48 base::Bind(&NetworkDeviceHandlerTest::StringSuccessCallback
,
49 base::Unretained(this));
50 error_callback_
= base::Bind(&NetworkDeviceHandlerTest::ErrorCallback
,
51 base::Unretained(this));
53 network_state_handler_
.reset(NetworkStateHandler::InitializeForTest());
54 NetworkDeviceHandlerImpl
* device_handler
= new NetworkDeviceHandlerImpl
;
55 device_handler
->Init(network_state_handler_
.get());
56 network_device_handler_
.reset(device_handler
);
58 // Add devices after handlers have been initialized.
59 ShillDeviceClient::TestInterface
* device_test
=
60 fake_device_client_
->GetTestInterface();
61 device_test
->AddDevice(
62 kDefaultCellularDevicePath
, shill::kTypeCellular
, "cellular1");
63 device_test
->AddDevice(kDefaultWifiDevicePath
, shill::kTypeWifi
, "wifi1");
65 base::ListValue test_ip_configs
;
66 test_ip_configs
.AppendString("ip_config1");
67 device_test
->SetDeviceProperty(
68 kDefaultWifiDevicePath
, shill::kIPConfigsProperty
, test_ip_configs
);
70 message_loop_
.RunUntilIdle();
73 virtual void TearDown() OVERRIDE
{
74 network_device_handler_
.reset();
75 network_state_handler_
.reset();
76 DBusThreadManager::Shutdown();
79 void ErrorCallback(const std::string
& error_name
,
80 scoped_ptr
<base::DictionaryValue
> error_data
) {
81 LOG(ERROR
) << "ErrorCallback: " << error_name
;
85 void SuccessCallback() {
86 result_
= kResultSuccess
;
89 void PropertiesSuccessCallback(const std::string
& device_path
,
90 const base::DictionaryValue
& properties
) {
91 result_
= kResultSuccess
;
92 properties_
.reset(properties
.DeepCopy());
95 void StringSuccessCallback(const std::string
& result
) {
96 LOG(ERROR
) << "StringSuccessCallback: " << result
;
97 result_
= kResultSuccess
;
103 FakeShillDeviceClient
* fake_device_client_
;
104 scoped_ptr
<NetworkDeviceHandler
> network_device_handler_
;
105 scoped_ptr
<NetworkStateHandler
> network_state_handler_
;
106 base::MessageLoopForUI message_loop_
;
107 base::Closure success_callback_
;
108 network_handler::DictionaryResultCallback properties_success_callback_
;
109 network_handler::StringResultCallback string_success_callback_
;
110 network_handler::ErrorCallback error_callback_
;
111 scoped_ptr
<base::DictionaryValue
> properties_
;
114 DISALLOW_COPY_AND_ASSIGN(NetworkDeviceHandlerTest
);
117 TEST_F(NetworkDeviceHandlerTest
, GetDeviceProperties
) {
118 network_device_handler_
->GetDeviceProperties(
119 kDefaultWifiDevicePath
, properties_success_callback_
, error_callback_
);
120 message_loop_
.RunUntilIdle();
121 EXPECT_EQ(kResultSuccess
, result_
);
123 properties_
->GetString(shill::kTypeProperty
, &type
);
124 EXPECT_EQ(shill::kTypeWifi
, type
);
127 TEST_F(NetworkDeviceHandlerTest
, SetDeviceProperty
) {
128 // Set the shill::kScanIntervalProperty to true. The call
129 // should succeed and the value should be set.
130 network_device_handler_
->SetDeviceProperty(kDefaultCellularDevicePath
,
131 shill::kScanIntervalProperty
,
132 base::FundamentalValue(1),
135 message_loop_
.RunUntilIdle();
136 EXPECT_EQ(kResultSuccess
, result_
);
138 // GetDeviceProperties should return the value set by SetDeviceProperty.
139 network_device_handler_
->GetDeviceProperties(kDefaultCellularDevicePath
,
140 properties_success_callback_
,
142 message_loop_
.RunUntilIdle();
143 EXPECT_EQ(kResultSuccess
, result_
);
145 EXPECT_TRUE(properties_
->GetIntegerWithoutPathExpansion(
146 shill::kScanIntervalProperty
, &interval
));
147 EXPECT_EQ(1, interval
);
149 // Repeat the same with value false.
150 network_device_handler_
->SetDeviceProperty(kDefaultCellularDevicePath
,
151 shill::kScanIntervalProperty
,
152 base::FundamentalValue(2),
155 message_loop_
.RunUntilIdle();
156 EXPECT_EQ(kResultSuccess
, result_
);
158 network_device_handler_
->GetDeviceProperties(kDefaultCellularDevicePath
,
159 properties_success_callback_
,
161 message_loop_
.RunUntilIdle();
162 EXPECT_EQ(kResultSuccess
, result_
);
163 EXPECT_TRUE(properties_
->GetIntegerWithoutPathExpansion(
164 shill::kScanIntervalProperty
, &interval
));
165 EXPECT_EQ(2, interval
);
167 // Set property on an invalid path.
168 network_device_handler_
->SetDeviceProperty(kUnknownCellularDevicePath
,
169 shill::kScanIntervalProperty
,
170 base::FundamentalValue(1),
173 message_loop_
.RunUntilIdle();
174 EXPECT_EQ(NetworkDeviceHandler::kErrorDeviceMissing
, result_
);
176 // Setting a owner-protected device property through SetDeviceProperty must
178 network_device_handler_
->SetDeviceProperty(
179 kDefaultCellularDevicePath
,
180 shill::kCellularAllowRoamingProperty
,
181 base::FundamentalValue(true),
184 message_loop_
.RunUntilIdle();
185 EXPECT_NE(kResultSuccess
, result_
);
189 TEST_F(NetworkDeviceHandlerTest
, CellularAllowRoaming
) {
190 // Start with disabled data roaming.
191 ShillDeviceClient::TestInterface
* device_test
=
192 fake_device_client_
->GetTestInterface();
193 device_test
->SetDeviceProperty(kDefaultCellularDevicePath
,
194 shill::kCellularAllowRoamingProperty
,
195 base::FundamentalValue(false));
197 network_device_handler_
->SetCellularAllowRoaming(true);
198 message_loop_
.RunUntilIdle();
200 // Roaming should be enabled now.
201 network_device_handler_
->GetDeviceProperties(kDefaultCellularDevicePath
,
202 properties_success_callback_
,
204 message_loop_
.RunUntilIdle();
205 EXPECT_EQ(kResultSuccess
, result_
);
207 EXPECT_TRUE(properties_
->GetBooleanWithoutPathExpansion(
208 shill::kCellularAllowRoamingProperty
, &allow_roaming
));
209 EXPECT_TRUE(allow_roaming
);
211 network_device_handler_
->SetCellularAllowRoaming(false);
212 message_loop_
.RunUntilIdle();
214 // Roaming should be disable again.
215 network_device_handler_
->GetDeviceProperties(kDefaultCellularDevicePath
,
216 properties_success_callback_
,
218 message_loop_
.RunUntilIdle();
219 EXPECT_EQ(kResultSuccess
, result_
);
220 EXPECT_TRUE(properties_
->GetBooleanWithoutPathExpansion(
221 shill::kCellularAllowRoamingProperty
, &allow_roaming
));
222 EXPECT_FALSE(allow_roaming
);
225 TEST_F(NetworkDeviceHandlerTest
, SetWifiTDLSEnabled
) {
226 // We add a wifi device by default, initial call should succeed.
227 network_device_handler_
->SetWifiTDLSEnabled(
228 "fake_ip_address", true, string_success_callback_
, error_callback_
);
229 message_loop_
.RunUntilIdle();
230 EXPECT_EQ(kResultSuccess
, result_
);
233 TEST_F(NetworkDeviceHandlerTest
, SetWifiTDLSEnabledMissing
) {
234 // Remove the wifi device. Call should fail with "device missing" error.
235 fake_device_client_
->GetTestInterface()->RemoveDevice(kDefaultWifiDevicePath
);
236 message_loop_
.RunUntilIdle();
237 network_device_handler_
->SetWifiTDLSEnabled(
238 "fake_ip_address", true, string_success_callback_
, error_callback_
);
239 message_loop_
.RunUntilIdle();
240 EXPECT_EQ(NetworkDeviceHandler::kErrorDeviceMissing
, result_
);
243 TEST_F(NetworkDeviceHandlerTest
, SetWifiTDLSEnabledBusy
) {
244 // Set the busy count, call should succeed after repeat attempt.
245 fake_device_client_
->set_tdls_busy_count(1);
246 network_device_handler_
->SetWifiTDLSEnabled(
247 "fake_ip_address", true, string_success_callback_
, error_callback_
);
248 message_loop_
.RunUntilIdle();
249 EXPECT_EQ(kResultSuccess
, result_
);
251 // Set the busy count to a large number, call should fail after max number
252 // of repeat attempt.
253 fake_device_client_
->set_tdls_busy_count(100000);
254 network_device_handler_
->SetWifiTDLSEnabled(
255 "fake_ip_address", true, string_success_callback_
, error_callback_
);
256 message_loop_
.RunUntilIdle();
257 EXPECT_EQ(NetworkDeviceHandler::kErrorTimeout
, result_
);
260 TEST_F(NetworkDeviceHandlerTest
, GetWifiTDLSStatus
) {
261 // We add a wifi device by default, initial call should succeed.
262 network_device_handler_
->GetWifiTDLSStatus(
263 "fake_ip_address", string_success_callback_
, error_callback_
);
264 message_loop_
.RunUntilIdle();
265 EXPECT_EQ(kResultSuccess
, result_
);
267 // Remove the wifi device. Call should fail with "device missing" error.
268 fake_device_client_
->GetTestInterface()->RemoveDevice(kDefaultWifiDevicePath
);
269 message_loop_
.RunUntilIdle();
270 network_device_handler_
->GetWifiTDLSStatus(
271 "fake_ip_address", string_success_callback_
, error_callback_
);
272 message_loop_
.RunUntilIdle();
273 EXPECT_EQ(NetworkDeviceHandler::kErrorDeviceMissing
, result_
);
276 TEST_F(NetworkDeviceHandlerTest
, RequestRefreshIPConfigs
) {
277 network_device_handler_
->RequestRefreshIPConfigs(
278 kDefaultWifiDevicePath
, success_callback_
, error_callback_
);
279 message_loop_
.RunUntilIdle();
280 EXPECT_EQ(kResultSuccess
, result_
);
281 // TODO(stevenjb): Add test interface to ShillIPConfigClient and test
285 TEST_F(NetworkDeviceHandlerTest
, SetCarrier
) {
286 const char kCarrier
[] = "carrier";
288 // Test that the success callback gets called.
289 network_device_handler_
->SetCarrier(
290 kDefaultCellularDevicePath
, kCarrier
, success_callback_
, error_callback_
);
291 message_loop_
.RunUntilIdle();
292 EXPECT_EQ(kResultSuccess
, result_
);
294 // Test that the shill error propagates to the error callback.
295 network_device_handler_
->SetCarrier(
296 kUnknownCellularDevicePath
, kCarrier
, success_callback_
, error_callback_
);
297 message_loop_
.RunUntilIdle();
298 EXPECT_EQ(NetworkDeviceHandler::kErrorDeviceMissing
, result_
);
301 TEST_F(NetworkDeviceHandlerTest
, RequirePin
) {
302 const char kPin
[] = "1234";
304 // Test that the success callback gets called.
305 network_device_handler_
->RequirePin(kDefaultCellularDevicePath
,
310 message_loop_
.RunUntilIdle();
311 EXPECT_EQ(kResultSuccess
, result_
);
313 // Test that the shill error propagates to the error callback.
314 network_device_handler_
->RequirePin(kUnknownCellularDevicePath
,
319 message_loop_
.RunUntilIdle();
320 EXPECT_EQ(NetworkDeviceHandler::kErrorDeviceMissing
, result_
);
323 TEST_F(NetworkDeviceHandlerTest
, EnterPin
) {
324 const char kPin
[] = "1234";
326 // Test that the success callback gets called.
327 network_device_handler_
->EnterPin(
328 kDefaultCellularDevicePath
, kPin
, success_callback_
, error_callback_
);
329 message_loop_
.RunUntilIdle();
330 EXPECT_EQ(kResultSuccess
, result_
);
332 // Test that the shill error propagates to the error callback.
333 network_device_handler_
->EnterPin(
334 kUnknownCellularDevicePath
, kPin
, success_callback_
, error_callback_
);
335 message_loop_
.RunUntilIdle();
336 EXPECT_EQ(NetworkDeviceHandler::kErrorDeviceMissing
, result_
);
339 TEST_F(NetworkDeviceHandlerTest
, UnblockPin
) {
340 const char kPuk
[] = "12345678";
341 const char kPin
[] = "1234";
343 // Test that the success callback gets called.
344 network_device_handler_
->UnblockPin(kDefaultCellularDevicePath
,
349 message_loop_
.RunUntilIdle();
350 EXPECT_EQ(kResultSuccess
, result_
);
352 // Test that the shill error propagates to the error callback.
353 network_device_handler_
->UnblockPin(kUnknownCellularDevicePath
,
358 message_loop_
.RunUntilIdle();
359 EXPECT_EQ(NetworkDeviceHandler::kErrorDeviceMissing
, result_
);
362 TEST_F(NetworkDeviceHandlerTest
, ChangePin
) {
363 const char kOldPin
[] = "4321";
364 const char kNewPin
[] = "1234";
366 // Test that the success callback gets called.
367 network_device_handler_
->ChangePin(kDefaultCellularDevicePath
,
372 message_loop_
.RunUntilIdle();
373 EXPECT_EQ(kResultSuccess
, result_
);
375 // Test that the shill error propagates to the error callback.
376 network_device_handler_
->ChangePin(kUnknownCellularDevicePath
,
381 message_loop_
.RunUntilIdle();
382 EXPECT_EQ(NetworkDeviceHandler::kErrorDeviceMissing
, result_
);
385 } // namespace chromeos