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 "base/bind.h" 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" 16 17 namespace chromeos { 18 19 namespace { 20 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"; 25 26 } // namespace 27 28 class NetworkDeviceHandlerTest : public testing::Test { 29 public: 30 NetworkDeviceHandlerTest() : fake_device_client_(NULL) {} 31 virtual ~NetworkDeviceHandlerTest() {} 32 33 virtual void SetUp() OVERRIDE { 34 FakeDBusThreadManager* dbus_manager = new FakeDBusThreadManager; 35 dbus_manager->SetFakeShillClients(); 36 37 fake_device_client_ = new FakeShillDeviceClient; 38 dbus_manager->SetShillDeviceClient( 39 scoped_ptr<ShillDeviceClient>(fake_device_client_)); 40 DBusThreadManager::InitializeForTesting(dbus_manager); 41 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)); 52 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); 57 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"); 64 65 base::ListValue test_ip_configs; 66 test_ip_configs.AppendString("ip_config1"); 67 device_test->SetDeviceProperty( 68 kDefaultWifiDevicePath, shill::kIPConfigsProperty, test_ip_configs); 69 70 message_loop_.RunUntilIdle(); 71 } 72 73 virtual void TearDown() OVERRIDE { 74 network_device_handler_.reset(); 75 network_state_handler_.reset(); 76 DBusThreadManager::Shutdown(); 77 } 78 79 void ErrorCallback(const std::string& error_name, 80 scoped_ptr<base::DictionaryValue> error_data) { 81 LOG(ERROR) << "ErrorCallback: " << error_name; 82 result_ = error_name; 83 } 84 85 void SuccessCallback() { 86 result_ = kResultSuccess; 87 } 88 89 void PropertiesSuccessCallback(const std::string& device_path, 90 const base::DictionaryValue& properties) { 91 result_ = kResultSuccess; 92 properties_.reset(properties.DeepCopy()); 93 } 94 95 void StringSuccessCallback(const std::string& result) { 96 LOG(ERROR) << "StringSuccessCallback: " << result; 97 result_ = kResultSuccess; 98 } 99 100 protected: 101 std::string result_; 102 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_; 112 113 private: 114 DISALLOW_COPY_AND_ASSIGN(NetworkDeviceHandlerTest); 115 }; 116 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_); 122 std::string type; 123 properties_->GetString(shill::kTypeProperty, &type); 124 EXPECT_EQ(shill::kTypeWifi, type); 125 } 126 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), 133 success_callback_, 134 error_callback_); 135 message_loop_.RunUntilIdle(); 136 EXPECT_EQ(kResultSuccess, result_); 137 138 // GetDeviceProperties should return the value set by SetDeviceProperty. 139 network_device_handler_->GetDeviceProperties(kDefaultCellularDevicePath, 140 properties_success_callback_, 141 error_callback_); 142 message_loop_.RunUntilIdle(); 143 EXPECT_EQ(kResultSuccess, result_); 144 int interval = 0; 145 EXPECT_TRUE(properties_->GetIntegerWithoutPathExpansion( 146 shill::kScanIntervalProperty, &interval)); 147 EXPECT_EQ(1, interval); 148 149 // Repeat the same with value false. 150 network_device_handler_->SetDeviceProperty(kDefaultCellularDevicePath, 151 shill::kScanIntervalProperty, 152 base::FundamentalValue(2), 153 success_callback_, 154 error_callback_); 155 message_loop_.RunUntilIdle(); 156 EXPECT_EQ(kResultSuccess, result_); 157 158 network_device_handler_->GetDeviceProperties(kDefaultCellularDevicePath, 159 properties_success_callback_, 160 error_callback_); 161 message_loop_.RunUntilIdle(); 162 EXPECT_EQ(kResultSuccess, result_); 163 EXPECT_TRUE(properties_->GetIntegerWithoutPathExpansion( 164 shill::kScanIntervalProperty, &interval)); 165 EXPECT_EQ(2, interval); 166 167 // Set property on an invalid path. 168 network_device_handler_->SetDeviceProperty(kUnknownCellularDevicePath, 169 shill::kScanIntervalProperty, 170 base::FundamentalValue(1), 171 success_callback_, 172 error_callback_); 173 message_loop_.RunUntilIdle(); 174 EXPECT_EQ(NetworkDeviceHandler::kErrorDeviceMissing, result_); 175 176 // Setting a owner-protected device property through SetDeviceProperty must 177 // fail. 178 network_device_handler_->SetDeviceProperty( 179 kDefaultCellularDevicePath, 180 shill::kCellularAllowRoamingProperty, 181 base::FundamentalValue(true), 182 success_callback_, 183 error_callback_); 184 message_loop_.RunUntilIdle(); 185 EXPECT_NE(kResultSuccess, result_); 186 187 } 188 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)); 196 197 network_device_handler_->SetCellularAllowRoaming(true); 198 message_loop_.RunUntilIdle(); 199 200 // Roaming should be enabled now. 201 network_device_handler_->GetDeviceProperties(kDefaultCellularDevicePath, 202 properties_success_callback_, 203 error_callback_); 204 message_loop_.RunUntilIdle(); 205 EXPECT_EQ(kResultSuccess, result_); 206 bool allow_roaming; 207 EXPECT_TRUE(properties_->GetBooleanWithoutPathExpansion( 208 shill::kCellularAllowRoamingProperty, &allow_roaming)); 209 EXPECT_TRUE(allow_roaming); 210 211 network_device_handler_->SetCellularAllowRoaming(false); 212 message_loop_.RunUntilIdle(); 213 214 // Roaming should be disable again. 215 network_device_handler_->GetDeviceProperties(kDefaultCellularDevicePath, 216 properties_success_callback_, 217 error_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); 223 } 224 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_); 231 } 232 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_); 241 } 242 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_); 250 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_); 258 } 259 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_); 266 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_); 274 } 275 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 282 // refresh calls. 283 } 284 285 TEST_F(NetworkDeviceHandlerTest, SetCarrier) { 286 const char kCarrier[] = "carrier"; 287 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_); 293 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_); 299 } 300 301 TEST_F(NetworkDeviceHandlerTest, RequirePin) { 302 const char kPin[] = "1234"; 303 304 // Test that the success callback gets called. 305 network_device_handler_->RequirePin(kDefaultCellularDevicePath, 306 true, 307 kPin, 308 success_callback_, 309 error_callback_); 310 message_loop_.RunUntilIdle(); 311 EXPECT_EQ(kResultSuccess, result_); 312 313 // Test that the shill error propagates to the error callback. 314 network_device_handler_->RequirePin(kUnknownCellularDevicePath, 315 true, 316 kPin, 317 success_callback_, 318 error_callback_); 319 message_loop_.RunUntilIdle(); 320 EXPECT_EQ(NetworkDeviceHandler::kErrorDeviceMissing, result_); 321 } 322 323 TEST_F(NetworkDeviceHandlerTest, EnterPin) { 324 const char kPin[] = "1234"; 325 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_); 331 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_); 337 } 338 339 TEST_F(NetworkDeviceHandlerTest, UnblockPin) { 340 const char kPuk[] = "12345678"; 341 const char kPin[] = "1234"; 342 343 // Test that the success callback gets called. 344 network_device_handler_->UnblockPin(kDefaultCellularDevicePath, 345 kPin, 346 kPuk, 347 success_callback_, 348 error_callback_); 349 message_loop_.RunUntilIdle(); 350 EXPECT_EQ(kResultSuccess, result_); 351 352 // Test that the shill error propagates to the error callback. 353 network_device_handler_->UnblockPin(kUnknownCellularDevicePath, 354 kPin, 355 kPuk, 356 success_callback_, 357 error_callback_); 358 message_loop_.RunUntilIdle(); 359 EXPECT_EQ(NetworkDeviceHandler::kErrorDeviceMissing, result_); 360 } 361 362 TEST_F(NetworkDeviceHandlerTest, ChangePin) { 363 const char kOldPin[] = "4321"; 364 const char kNewPin[] = "1234"; 365 366 // Test that the success callback gets called. 367 network_device_handler_->ChangePin(kDefaultCellularDevicePath, 368 kOldPin, 369 kNewPin, 370 success_callback_, 371 error_callback_); 372 message_loop_.RunUntilIdle(); 373 EXPECT_EQ(kResultSuccess, result_); 374 375 // Test that the shill error propagates to the error callback. 376 network_device_handler_->ChangePin(kUnknownCellularDevicePath, 377 kOldPin, 378 kNewPin, 379 success_callback_, 380 error_callback_); 381 message_loop_.RunUntilIdle(); 382 EXPECT_EQ(NetworkDeviceHandler::kErrorDeviceMissing, result_); 383 } 384 385 } // namespace chromeos 386