network_device_handler_unittest.cc revision 9ab5563a3196760eb381d102cbb2bc0f7abc6a50
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, flimflam::kTypeCellular, "cellular1"); 48 device_test->AddDevice( 49 kDefaultWifiDevicePath, flimflam::kTypeWifi, "wifi1"); 50 base::ListValue test_ip_configs; 51 test_ip_configs.AppendString("ip_config1"); 52 device_test->SetDeviceProperty( 53 kDefaultWifiDevicePath, flimflam::kIPConfigsProperty, test_ip_configs); 54 } 55 56 virtual void TearDown() OVERRIDE { 57 network_device_handler_.reset(); 58 DBusThreadManager::Shutdown(); 59 } 60 61 base::Closure GetErrorInvokingCallback( 62 const std::string& device_path, 63 const std::string& error_name) { 64 return base::Bind(&NetworkDeviceHandlerTest::InvokeDBusErrorCallback, 65 base::Unretained(this), 66 device_path, 67 base::Bind(&NetworkDeviceHandlerTest::ErrorCallback, 68 base::Unretained(this)), 69 error_name); 70 } 71 72 void ErrorCallback(const std::string& error_name, 73 scoped_ptr<base::DictionaryValue> error_data) { 74 result_ = error_name; 75 } 76 77 void SuccessCallback() { 78 result_ = kResultSuccess; 79 } 80 81 void PropertiesSuccessCallback(const std::string& device_path, 82 const base::DictionaryValue& properties) { 83 result_ = kResultSuccess; 84 properties_.reset(properties.DeepCopy()); 85 } 86 87 void InvokeDBusErrorCallback( 88 const std::string& device_path, 89 const network_handler::ErrorCallback& callback, 90 const std::string& error_name) { 91 network_device_handler_->HandleShillCallFailureForTest( 92 device_path, callback, error_name, "Error message."); 93 } 94 95 protected: 96 std::string result_; 97 98 scoped_ptr<NetworkDeviceHandler> network_device_handler_; 99 base::MessageLoopForUI message_loop_; 100 base::Closure success_callback_; 101 network_handler::DictionaryResultCallback properties_success_callback_; 102 network_handler::ErrorCallback error_callback_; 103 scoped_ptr<base::DictionaryValue> properties_; 104 105 private: 106 DISALLOW_COPY_AND_ASSIGN(NetworkDeviceHandlerTest); 107}; 108 109TEST_F(NetworkDeviceHandlerTest, ErrorTranslation) { 110 EXPECT_TRUE(result_.empty()); 111 network_handler::ErrorCallback callback = 112 base::Bind(&NetworkDeviceHandlerTest::ErrorCallback, 113 base::Unretained(this)); 114 115 network_device_handler_->HandleShillCallFailureForTest( 116 kDefaultCellularDevicePath, 117 callback, 118 "org.chromium.flimflam.Error.Failure", 119 "Error happened."); 120 EXPECT_EQ(NetworkDeviceHandler::kErrorFailure, result_); 121 122 network_device_handler_->HandleShillCallFailureForTest( 123 kDefaultCellularDevicePath, 124 callback, 125 "org.chromium.flimflam.Error.IncorrectPin", 126 "Incorrect pin."); 127 EXPECT_EQ(NetworkDeviceHandler::kErrorIncorrectPin, result_); 128 129 network_device_handler_->HandleShillCallFailureForTest( 130 kDefaultCellularDevicePath, 131 callback, 132 "org.chromium.flimflam.Error.NotSupported", 133 "Operation not supported."); 134 EXPECT_EQ(NetworkDeviceHandler::kErrorNotSupported, result_); 135 136 network_device_handler_->HandleShillCallFailureForTest( 137 kDefaultCellularDevicePath, 138 callback, 139 "org.chromium.flimflam.Error.PinBlocked", 140 "PIN is blocked."); 141 EXPECT_EQ(NetworkDeviceHandler::kErrorPinBlocked, result_); 142 143 network_device_handler_->HandleShillCallFailureForTest( 144 kDefaultCellularDevicePath, 145 callback, 146 "org.chromium.flimflam.Error.PinRequired", 147 "A PIN error has occurred."); 148 EXPECT_EQ(NetworkDeviceHandler::kErrorPinRequired, result_); 149 150 network_device_handler_->HandleShillCallFailureForTest( 151 kDefaultCellularDevicePath, 152 callback, 153 "org.chromium.flimflam.Error.WorldExploded", 154 "The earth is no more."); 155 EXPECT_EQ(NetworkDeviceHandler::kErrorUnknown, result_); 156} 157 158TEST_F(NetworkDeviceHandlerTest, GetDeviceProperties) { 159 network_device_handler_->GetDeviceProperties( 160 kDefaultWifiDevicePath, 161 properties_success_callback_, 162 error_callback_); 163 message_loop_.RunUntilIdle(); 164 EXPECT_EQ(kResultSuccess, result_); 165 std::string type; 166 properties_->GetString(flimflam::kTypeProperty, &type); 167 EXPECT_EQ(flimflam::kTypeWifi, type); 168} 169 170TEST_F(NetworkDeviceHandlerTest, RequestRefreshIPConfigs) { 171 network_device_handler_->RequestRefreshIPConfigs( 172 kDefaultWifiDevicePath, 173 success_callback_, 174 error_callback_); 175 message_loop_.RunUntilIdle(); 176 EXPECT_EQ(kResultSuccess, result_); 177 // TODO(stevenjb): Add test interface to ShillIPConfigClient and test 178 // refresh calls. 179} 180 181TEST_F(NetworkDeviceHandlerTest, SetCarrier) { 182 const char kCarrier[] = "carrier"; 183 184 // Test that the success callback gets called. 185 network_device_handler_->SetCarrier( 186 kDefaultCellularDevicePath, 187 kCarrier, 188 success_callback_, 189 error_callback_); 190 message_loop_.RunUntilIdle(); 191 EXPECT_EQ(kResultSuccess, result_); 192 193 // Test that the shill error gets properly translated and propagates to the 194 // error callback. 195 network_device_handler_->SetCarrier( 196 kDefaultCellularDevicePath, 197 kCarrier, 198 GetErrorInvokingCallback(kDefaultCellularDevicePath, 199 "org.chromium.flimflam.Error.NotSupported"), 200 error_callback_); 201 message_loop_.RunUntilIdle(); 202 EXPECT_EQ(NetworkDeviceHandler::kErrorNotSupported, result_); 203} 204 205TEST_F(NetworkDeviceHandlerTest, RequirePin) { 206 const char kPin[] = "1234"; 207 208 // Test that the success callback gets called. 209 network_device_handler_->RequirePin( 210 kDefaultCellularDevicePath, 211 true, 212 kPin, 213 success_callback_, 214 error_callback_); 215 message_loop_.RunUntilIdle(); 216 EXPECT_EQ(kResultSuccess, result_); 217 218 // Test that the shill error gets properly translated and propagates to the 219 // error callback. 220 network_device_handler_->RequirePin( 221 kDefaultCellularDevicePath, 222 true, 223 kPin, 224 GetErrorInvokingCallback(kDefaultCellularDevicePath, 225 "org.chromium.flimflam.Error.IncorrectPin"), 226 error_callback_); 227 message_loop_.RunUntilIdle(); 228 EXPECT_EQ(NetworkDeviceHandler::kErrorIncorrectPin, result_); 229} 230 231TEST_F(NetworkDeviceHandlerTest, EnterPin) { 232 const char kPin[] = "1234"; 233 234 // Test that the success callback gets called. 235 network_device_handler_->EnterPin( 236 kDefaultCellularDevicePath, 237 kPin, 238 success_callback_, 239 error_callback_); 240 message_loop_.RunUntilIdle(); 241 EXPECT_EQ(kResultSuccess, result_); 242 243 // Test that the shill error gets properly translated and propagates to the 244 // error callback. 245 network_device_handler_->EnterPin( 246 kDefaultCellularDevicePath, 247 kPin, 248 GetErrorInvokingCallback(kDefaultCellularDevicePath, 249 "org.chromium.flimflam.Error.IncorrectPin"), 250 error_callback_); 251 message_loop_.RunUntilIdle(); 252 EXPECT_EQ(NetworkDeviceHandler::kErrorIncorrectPin, result_); 253} 254 255TEST_F(NetworkDeviceHandlerTest, UnblockPin) { 256 const char kPuk[] = "12345678"; 257 const char kPin[] = "1234"; 258 259 // Test that the success callback gets called. 260 network_device_handler_->UnblockPin( 261 kDefaultCellularDevicePath, 262 kPin, 263 kPuk, 264 success_callback_, 265 error_callback_); 266 message_loop_.RunUntilIdle(); 267 EXPECT_EQ(kResultSuccess, result_); 268 269 // Test that the shill error gets properly translated and propagates to the 270 // error callback. 271 network_device_handler_->UnblockPin( 272 kDefaultCellularDevicePath, 273 kPin, 274 kPuk, 275 GetErrorInvokingCallback(kDefaultCellularDevicePath, 276 "org.chromium.flimflam.Error.PinRequired"), 277 error_callback_); 278 message_loop_.RunUntilIdle(); 279 EXPECT_EQ(NetworkDeviceHandler::kErrorPinRequired, result_); 280} 281 282TEST_F(NetworkDeviceHandlerTest, ChangePin) { 283 const char kOldPin[] = "4321"; 284 const char kNewPin[] = "1234"; 285 286 // Test that the success callback gets called. 287 network_device_handler_->ChangePin( 288 kDefaultCellularDevicePath, 289 kOldPin, 290 kNewPin, 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_->ChangePin( 299 kDefaultCellularDevicePath, 300 kOldPin, 301 kNewPin, 302 GetErrorInvokingCallback(kDefaultCellularDevicePath, 303 "org.chromium.flimflam.Error.PinBlocked"), 304 error_callback_); 305 message_loop_.RunUntilIdle(); 306 EXPECT_EQ(NetworkDeviceHandler::kErrorPinBlocked, result_); 307} 308 309} // namespace chromeos 310