network_device_handler_unittest.cc revision 68043e1e95eeb07d5cae7aca370b26518b0867d6
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. 4 5#include "chromeos/network/network_device_handler.h" 6 7#include "base/bind.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" 15 16namespace chromeos { 17 18namespace { 19 20const char kDefaultCellularDevicePath[] = "stub_cellular_device"; 21const char kDefaultWifiDevicePath[] = "stub_wifi_device"; 22const char kResultSuccess[] = "success"; 23 24} // namespace 25 26class NetworkDeviceHandlerTest : public testing::Test { 27 public: 28 NetworkDeviceHandlerTest() {} 29 virtual ~NetworkDeviceHandlerTest() {} 30 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()); 42 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"); 49 50 base::FundamentalValue allow_roaming(false); 51 device_test->SetDeviceProperty( 52 kDefaultCellularDevicePath, 53 shill::kCellularAllowRoamingProperty, 54 allow_roaming); 55 56 base::ListValue test_ip_configs; 57 test_ip_configs.AppendString("ip_config1"); 58 device_test->SetDeviceProperty( 59 kDefaultWifiDevicePath, shill::kIPConfigsProperty, test_ip_configs); 60 } 61 62 virtual void TearDown() OVERRIDE { 63 network_device_handler_.reset(); 64 DBusThreadManager::Shutdown(); 65 } 66 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), 72 device_path, 73 base::Bind(&NetworkDeviceHandlerTest::ErrorCallback, 74 base::Unretained(this)), 75 error_name); 76 } 77 78 void ErrorCallback(const std::string& error_name, 79 scoped_ptr<base::DictionaryValue> error_data) { 80 result_ = error_name; 81 } 82 83 void SuccessCallback() { 84 result_ = kResultSuccess; 85 } 86 87 void PropertiesSuccessCallback(const std::string& device_path, 88 const base::DictionaryValue& properties) { 89 result_ = kResultSuccess; 90 properties_.reset(properties.DeepCopy()); 91 } 92 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."); 99 } 100 101 protected: 102 std::string result_; 103 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_; 110 111 private: 112 DISALLOW_COPY_AND_ASSIGN(NetworkDeviceHandlerTest); 113}; 114 115TEST_F(NetworkDeviceHandlerTest, ErrorTranslation) { 116 EXPECT_TRUE(result_.empty()); 117 network_handler::ErrorCallback callback = 118 base::Bind(&NetworkDeviceHandlerTest::ErrorCallback, 119 base::Unretained(this)); 120 121 network_device_handler_->HandleShillCallFailureForTest( 122 kDefaultCellularDevicePath, 123 callback, 124 "org.chromium.flimflam.Error.Failure", 125 "Error happened."); 126 EXPECT_EQ(NetworkDeviceHandler::kErrorFailure, result_); 127 128 network_device_handler_->HandleShillCallFailureForTest( 129 kDefaultCellularDevicePath, 130 callback, 131 "org.chromium.flimflam.Error.IncorrectPin", 132 "Incorrect pin."); 133 EXPECT_EQ(NetworkDeviceHandler::kErrorIncorrectPin, result_); 134 135 network_device_handler_->HandleShillCallFailureForTest( 136 kDefaultCellularDevicePath, 137 callback, 138 "org.chromium.flimflam.Error.NotSupported", 139 "Operation not supported."); 140 EXPECT_EQ(NetworkDeviceHandler::kErrorNotSupported, result_); 141 142 network_device_handler_->HandleShillCallFailureForTest( 143 kDefaultCellularDevicePath, 144 callback, 145 "org.chromium.flimflam.Error.PinBlocked", 146 "PIN is blocked."); 147 EXPECT_EQ(NetworkDeviceHandler::kErrorPinBlocked, result_); 148 149 network_device_handler_->HandleShillCallFailureForTest( 150 kDefaultCellularDevicePath, 151 callback, 152 "org.chromium.flimflam.Error.PinRequired", 153 "A PIN error has occurred."); 154 EXPECT_EQ(NetworkDeviceHandler::kErrorPinRequired, result_); 155 156 network_device_handler_->HandleShillCallFailureForTest( 157 kDefaultCellularDevicePath, 158 callback, 159 "org.chromium.flimflam.Error.WorldExploded", 160 "The earth is no more."); 161 EXPECT_EQ(NetworkDeviceHandler::kErrorUnknown, result_); 162} 163 164TEST_F(NetworkDeviceHandlerTest, GetDeviceProperties) { 165 network_device_handler_->GetDeviceProperties( 166 kDefaultWifiDevicePath, 167 properties_success_callback_, 168 error_callback_); 169 message_loop_.RunUntilIdle(); 170 EXPECT_EQ(kResultSuccess, result_); 171 std::string type; 172 properties_->GetString(shill::kTypeProperty, &type); 173 EXPECT_EQ(shill::kTypeWifi, type); 174} 175 176TEST_F(NetworkDeviceHandlerTest, SetDeviceProperty) { 177 // Check that GetDeviceProperties returns the expected initial values. 178 network_device_handler_->GetDeviceProperties( 179 kDefaultCellularDevicePath, 180 properties_success_callback_, 181 error_callback_); 182 message_loop_.RunUntilIdle(); 183 EXPECT_EQ(kResultSuccess, result_); 184 bool allow_roaming; 185 EXPECT_TRUE(properties_->GetBooleanWithoutPathExpansion( 186 shill::kCellularAllowRoamingProperty, &allow_roaming)); 187 EXPECT_FALSE(allow_roaming); 188 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, 195 allow_roaming_value, 196 success_callback_, 197 error_callback_); 198 message_loop_.RunUntilIdle(); 199 EXPECT_EQ(kResultSuccess, result_); 200 201 // GetDeviceProperties should return the value set by SetDeviceProperty. 202 network_device_handler_->GetDeviceProperties( 203 kDefaultCellularDevicePath, 204 properties_success_callback_, 205 error_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); 211 212 // Set property on an invalid path. 213 network_device_handler_->SetDeviceProperty( 214 "/device/invalid_path", 215 shill::kCellularAllowRoamingProperty, 216 allow_roaming_value, 217 success_callback_, 218 error_callback_); 219 message_loop_.RunUntilIdle(); 220 EXPECT_EQ(NetworkDeviceHandler::kErrorFailure, result_); 221} 222 223TEST_F(NetworkDeviceHandlerTest, RequestRefreshIPConfigs) { 224 network_device_handler_->RequestRefreshIPConfigs( 225 kDefaultWifiDevicePath, 226 success_callback_, 227 error_callback_); 228 message_loop_.RunUntilIdle(); 229 EXPECT_EQ(kResultSuccess, result_); 230 // TODO(stevenjb): Add test interface to ShillIPConfigClient and test 231 // refresh calls. 232} 233 234TEST_F(NetworkDeviceHandlerTest, SetCarrier) { 235 const char kCarrier[] = "carrier"; 236 237 // Test that the success callback gets called. 238 network_device_handler_->SetCarrier( 239 kDefaultCellularDevicePath, 240 kCarrier, 241 success_callback_, 242 error_callback_); 243 message_loop_.RunUntilIdle(); 244 EXPECT_EQ(kResultSuccess, result_); 245 246 // Test that the shill error gets properly translated and propagates to the 247 // error callback. 248 network_device_handler_->SetCarrier( 249 kDefaultCellularDevicePath, 250 kCarrier, 251 GetErrorInvokingCallback(kDefaultCellularDevicePath, 252 "org.chromium.flimflam.Error.NotSupported"), 253 error_callback_); 254 message_loop_.RunUntilIdle(); 255 EXPECT_EQ(NetworkDeviceHandler::kErrorNotSupported, result_); 256} 257 258TEST_F(NetworkDeviceHandlerTest, RequirePin) { 259 const char kPin[] = "1234"; 260 261 // Test that the success callback gets called. 262 network_device_handler_->RequirePin( 263 kDefaultCellularDevicePath, 264 true, 265 kPin, 266 success_callback_, 267 error_callback_); 268 message_loop_.RunUntilIdle(); 269 EXPECT_EQ(kResultSuccess, result_); 270 271 // Test that the shill error gets properly translated and propagates to the 272 // error callback. 273 network_device_handler_->RequirePin( 274 kDefaultCellularDevicePath, 275 true, 276 kPin, 277 GetErrorInvokingCallback(kDefaultCellularDevicePath, 278 "org.chromium.flimflam.Error.IncorrectPin"), 279 error_callback_); 280 message_loop_.RunUntilIdle(); 281 EXPECT_EQ(NetworkDeviceHandler::kErrorIncorrectPin, result_); 282} 283 284TEST_F(NetworkDeviceHandlerTest, EnterPin) { 285 const char kPin[] = "1234"; 286 287 // Test that the success callback gets called. 288 network_device_handler_->EnterPin( 289 kDefaultCellularDevicePath, 290 kPin, 291 success_callback_, 292 error_callback_); 293 message_loop_.RunUntilIdle(); 294 EXPECT_EQ(kResultSuccess, result_); 295 296 // Test that the shill error gets properly translated and propagates to the 297 // error callback. 298 network_device_handler_->EnterPin( 299 kDefaultCellularDevicePath, 300 kPin, 301 GetErrorInvokingCallback(kDefaultCellularDevicePath, 302 "org.chromium.flimflam.Error.IncorrectPin"), 303 error_callback_); 304 message_loop_.RunUntilIdle(); 305 EXPECT_EQ(NetworkDeviceHandler::kErrorIncorrectPin, result_); 306} 307 308TEST_F(NetworkDeviceHandlerTest, UnblockPin) { 309 const char kPuk[] = "12345678"; 310 const char kPin[] = "1234"; 311 312 // Test that the success callback gets called. 313 network_device_handler_->UnblockPin( 314 kDefaultCellularDevicePath, 315 kPin, 316 kPuk, 317 success_callback_, 318 error_callback_); 319 message_loop_.RunUntilIdle(); 320 EXPECT_EQ(kResultSuccess, result_); 321 322 // Test that the shill error gets properly translated and propagates to the 323 // error callback. 324 network_device_handler_->UnblockPin( 325 kDefaultCellularDevicePath, 326 kPin, 327 kPuk, 328 GetErrorInvokingCallback(kDefaultCellularDevicePath, 329 "org.chromium.flimflam.Error.PinRequired"), 330 error_callback_); 331 message_loop_.RunUntilIdle(); 332 EXPECT_EQ(NetworkDeviceHandler::kErrorPinRequired, result_); 333} 334 335TEST_F(NetworkDeviceHandlerTest, ChangePin) { 336 const char kOldPin[] = "4321"; 337 const char kNewPin[] = "1234"; 338 339 // Test that the success callback gets called. 340 network_device_handler_->ChangePin( 341 kDefaultCellularDevicePath, 342 kOldPin, 343 kNewPin, 344 success_callback_, 345 error_callback_); 346 message_loop_.RunUntilIdle(); 347 EXPECT_EQ(kResultSuccess, result_); 348 349 // Test that the shill error gets properly translated and propagates to the 350 // error callback. 351 network_device_handler_->ChangePin( 352 kDefaultCellularDevicePath, 353 kOldPin, 354 kNewPin, 355 GetErrorInvokingCallback(kDefaultCellularDevicePath, 356 "org.chromium.flimflam.Error.PinBlocked"), 357 error_callback_); 358 message_loop_.RunUntilIdle(); 359 EXPECT_EQ(NetworkDeviceHandler::kErrorPinBlocked, result_); 360} 361 362} // namespace chromeos 363