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.
5 #include "chromeos/network/network_device_handler.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/values.h"
11 #include "chromeos/dbus/dbus_thread_manager.h"
12 #include "chromeos/dbus/shill_device_client.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/cros_system_api/dbus/service_constants.h"
20 const char kDefaultCellularDevicePath[] = "stub_cellular_device";
21 const char kDefaultWifiDevicePath[] = "stub_wifi_device";
22 const char kResultSuccess[] = "success";
26 class NetworkDeviceHandlerTest : public testing::Test {
28 NetworkDeviceHandlerTest() {}
29 virtual ~NetworkDeviceHandlerTest() {}
31 virtual void SetUp() OVERRIDE {
32 DBusThreadManager::InitializeWithStub();
33 message_loop_.RunUntilIdle();
34 success_callback_ = base::Bind(&NetworkDeviceHandlerTest::SuccessCallback,
35 base::Unretained(this));
36 properties_success_callback_ =
37 base::Bind(&NetworkDeviceHandlerTest::PropertiesSuccessCallback,
38 base::Unretained(this));
39 error_callback_ = base::Bind(&NetworkDeviceHandlerTest::ErrorCallback,
40 base::Unretained(this));
41 network_device_handler_.reset(new NetworkDeviceHandler());
43 ShillDeviceClient::TestInterface* device_test =
44 DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface();
45 device_test->ClearDevices();
46 device_test->AddDevice(
47 kDefaultCellularDevicePath, shill::kTypeCellular, "cellular1");
48 device_test->AddDevice(kDefaultWifiDevicePath, shill::kTypeWifi, "wifi1");
50 base::FundamentalValue allow_roaming(false);
51 device_test->SetDeviceProperty(
52 kDefaultCellularDevicePath,
53 shill::kCellularAllowRoamingProperty,
56 base::ListValue test_ip_configs;
57 test_ip_configs.AppendString("ip_config1");
58 device_test->SetDeviceProperty(
59 kDefaultWifiDevicePath, shill::kIPConfigsProperty, test_ip_configs);
62 virtual void TearDown() OVERRIDE {
63 network_device_handler_.reset();
64 DBusThreadManager::Shutdown();
67 base::Closure GetErrorInvokingCallback(
68 const std::string& device_path,
69 const std::string& error_name) {
70 return base::Bind(&NetworkDeviceHandlerTest::InvokeDBusErrorCallback,
71 base::Unretained(this),
73 base::Bind(&NetworkDeviceHandlerTest::ErrorCallback,
74 base::Unretained(this)),
78 void ErrorCallback(const std::string& error_name,
79 scoped_ptr<base::DictionaryValue> error_data) {
83 void SuccessCallback() {
84 result_ = kResultSuccess;
87 void PropertiesSuccessCallback(const std::string& device_path,
88 const base::DictionaryValue& properties) {
89 result_ = kResultSuccess;
90 properties_.reset(properties.DeepCopy());
93 void InvokeDBusErrorCallback(
94 const std::string& device_path,
95 const network_handler::ErrorCallback& callback,
96 const std::string& error_name) {
97 network_device_handler_->HandleShillCallFailureForTest(
98 device_path, callback, error_name, "Error message.");
104 scoped_ptr<NetworkDeviceHandler> network_device_handler_;
105 base::MessageLoopForUI message_loop_;
106 base::Closure success_callback_;
107 network_handler::DictionaryResultCallback properties_success_callback_;
108 network_handler::ErrorCallback error_callback_;
109 scoped_ptr<base::DictionaryValue> properties_;
112 DISALLOW_COPY_AND_ASSIGN(NetworkDeviceHandlerTest);
115 TEST_F(NetworkDeviceHandlerTest, ErrorTranslation) {
116 EXPECT_TRUE(result_.empty());
117 network_handler::ErrorCallback callback =
118 base::Bind(&NetworkDeviceHandlerTest::ErrorCallback,
119 base::Unretained(this));
121 network_device_handler_->HandleShillCallFailureForTest(
122 kDefaultCellularDevicePath,
124 "org.chromium.flimflam.Error.Failure",
126 EXPECT_EQ(NetworkDeviceHandler::kErrorFailure, result_);
128 network_device_handler_->HandleShillCallFailureForTest(
129 kDefaultCellularDevicePath,
131 "org.chromium.flimflam.Error.IncorrectPin",
133 EXPECT_EQ(NetworkDeviceHandler::kErrorIncorrectPin, result_);
135 network_device_handler_->HandleShillCallFailureForTest(
136 kDefaultCellularDevicePath,
138 "org.chromium.flimflam.Error.NotSupported",
139 "Operation not supported.");
140 EXPECT_EQ(NetworkDeviceHandler::kErrorNotSupported, result_);
142 network_device_handler_->HandleShillCallFailureForTest(
143 kDefaultCellularDevicePath,
145 "org.chromium.flimflam.Error.PinBlocked",
147 EXPECT_EQ(NetworkDeviceHandler::kErrorPinBlocked, result_);
149 network_device_handler_->HandleShillCallFailureForTest(
150 kDefaultCellularDevicePath,
152 "org.chromium.flimflam.Error.PinRequired",
153 "A PIN error has occurred.");
154 EXPECT_EQ(NetworkDeviceHandler::kErrorPinRequired, result_);
156 network_device_handler_->HandleShillCallFailureForTest(
157 kDefaultCellularDevicePath,
159 "org.chromium.flimflam.Error.WorldExploded",
160 "The earth is no more.");
161 EXPECT_EQ(NetworkDeviceHandler::kErrorUnknown, result_);
164 TEST_F(NetworkDeviceHandlerTest, GetDeviceProperties) {
165 network_device_handler_->GetDeviceProperties(
166 kDefaultWifiDevicePath,
167 properties_success_callback_,
169 message_loop_.RunUntilIdle();
170 EXPECT_EQ(kResultSuccess, result_);
172 properties_->GetString(shill::kTypeProperty, &type);
173 EXPECT_EQ(shill::kTypeWifi, type);
176 TEST_F(NetworkDeviceHandlerTest, SetDeviceProperty) {
177 // Check that GetDeviceProperties returns the expected initial values.
178 network_device_handler_->GetDeviceProperties(
179 kDefaultCellularDevicePath,
180 properties_success_callback_,
182 message_loop_.RunUntilIdle();
183 EXPECT_EQ(kResultSuccess, result_);
185 EXPECT_TRUE(properties_->GetBooleanWithoutPathExpansion(
186 shill::kCellularAllowRoamingProperty, &allow_roaming));
187 EXPECT_FALSE(allow_roaming);
189 // Set the shill::kCellularAllowRoamingProperty to true. The call
190 // should succeed and the value should be set.
191 base::FundamentalValue allow_roaming_value(true);
192 network_device_handler_->SetDeviceProperty(
193 kDefaultCellularDevicePath,
194 shill::kCellularAllowRoamingProperty,
198 message_loop_.RunUntilIdle();
199 EXPECT_EQ(kResultSuccess, result_);
201 // GetDeviceProperties should return the value set by SetDeviceProperty.
202 network_device_handler_->GetDeviceProperties(
203 kDefaultCellularDevicePath,
204 properties_success_callback_,
206 message_loop_.RunUntilIdle();
207 EXPECT_EQ(kResultSuccess, result_);
208 EXPECT_TRUE(properties_->GetBooleanWithoutPathExpansion(
209 shill::kCellularAllowRoamingProperty, &allow_roaming));
210 EXPECT_TRUE(allow_roaming);
212 // Set property on an invalid path.
213 network_device_handler_->SetDeviceProperty(
214 "/device/invalid_path",
215 shill::kCellularAllowRoamingProperty,
219 message_loop_.RunUntilIdle();
220 EXPECT_EQ(NetworkDeviceHandler::kErrorFailure, result_);
223 TEST_F(NetworkDeviceHandlerTest, RequestRefreshIPConfigs) {
224 network_device_handler_->RequestRefreshIPConfigs(
225 kDefaultWifiDevicePath,
228 message_loop_.RunUntilIdle();
229 EXPECT_EQ(kResultSuccess, result_);
230 // TODO(stevenjb): Add test interface to ShillIPConfigClient and test
234 TEST_F(NetworkDeviceHandlerTest, SetCarrier) {
235 const char kCarrier[] = "carrier";
237 // Test that the success callback gets called.
238 network_device_handler_->SetCarrier(
239 kDefaultCellularDevicePath,
243 message_loop_.RunUntilIdle();
244 EXPECT_EQ(kResultSuccess, result_);
246 // Test that the shill error gets properly translated and propagates to the
248 network_device_handler_->SetCarrier(
249 kDefaultCellularDevicePath,
251 GetErrorInvokingCallback(kDefaultCellularDevicePath,
252 "org.chromium.flimflam.Error.NotSupported"),
254 message_loop_.RunUntilIdle();
255 EXPECT_EQ(NetworkDeviceHandler::kErrorNotSupported, result_);
258 TEST_F(NetworkDeviceHandlerTest, RequirePin) {
259 const char kPin[] = "1234";
261 // Test that the success callback gets called.
262 network_device_handler_->RequirePin(
263 kDefaultCellularDevicePath,
268 message_loop_.RunUntilIdle();
269 EXPECT_EQ(kResultSuccess, result_);
271 // Test that the shill error gets properly translated and propagates to the
273 network_device_handler_->RequirePin(
274 kDefaultCellularDevicePath,
277 GetErrorInvokingCallback(kDefaultCellularDevicePath,
278 "org.chromium.flimflam.Error.IncorrectPin"),
280 message_loop_.RunUntilIdle();
281 EXPECT_EQ(NetworkDeviceHandler::kErrorIncorrectPin, result_);
284 TEST_F(NetworkDeviceHandlerTest, EnterPin) {
285 const char kPin[] = "1234";
287 // Test that the success callback gets called.
288 network_device_handler_->EnterPin(
289 kDefaultCellularDevicePath,
293 message_loop_.RunUntilIdle();
294 EXPECT_EQ(kResultSuccess, result_);
296 // Test that the shill error gets properly translated and propagates to the
298 network_device_handler_->EnterPin(
299 kDefaultCellularDevicePath,
301 GetErrorInvokingCallback(kDefaultCellularDevicePath,
302 "org.chromium.flimflam.Error.IncorrectPin"),
304 message_loop_.RunUntilIdle();
305 EXPECT_EQ(NetworkDeviceHandler::kErrorIncorrectPin, result_);
308 TEST_F(NetworkDeviceHandlerTest, UnblockPin) {
309 const char kPuk[] = "12345678";
310 const char kPin[] = "1234";
312 // Test that the success callback gets called.
313 network_device_handler_->UnblockPin(
314 kDefaultCellularDevicePath,
319 message_loop_.RunUntilIdle();
320 EXPECT_EQ(kResultSuccess, result_);
322 // Test that the shill error gets properly translated and propagates to the
324 network_device_handler_->UnblockPin(
325 kDefaultCellularDevicePath,
328 GetErrorInvokingCallback(kDefaultCellularDevicePath,
329 "org.chromium.flimflam.Error.PinRequired"),
331 message_loop_.RunUntilIdle();
332 EXPECT_EQ(NetworkDeviceHandler::kErrorPinRequired, result_);
335 TEST_F(NetworkDeviceHandlerTest, ChangePin) {
336 const char kOldPin[] = "4321";
337 const char kNewPin[] = "1234";
339 // Test that the success callback gets called.
340 network_device_handler_->ChangePin(
341 kDefaultCellularDevicePath,
346 message_loop_.RunUntilIdle();
347 EXPECT_EQ(kResultSuccess, result_);
349 // Test that the shill error gets properly translated and propagates to the
351 network_device_handler_->ChangePin(
352 kDefaultCellularDevicePath,
355 GetErrorInvokingCallback(kDefaultCellularDevicePath,
356 "org.chromium.flimflam.Error.PinBlocked"),
358 message_loop_.RunUntilIdle();
359 EXPECT_EQ(NetworkDeviceHandler::kErrorPinBlocked, result_);
362 } // namespace chromeos