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