1 // 2 // Copyright (C) 2012 The Android Open Source Project 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 #include "shill/cellular/cellular_capability_cdma.h" 18 19 #include <base/bind.h> 20 #if defined(__ANDROID__) 21 #include <dbus/service_constants.h> 22 #else 23 #include <chromeos/dbus/service_constants.h> 24 #endif // __ANDROID__ 25 #include <gtest/gtest.h> 26 #include <mm/mm-modem.h> 27 28 #include "shill/cellular/cellular.h" 29 #include "shill/cellular/cellular_service.h" 30 #include "shill/cellular/mock_cellular.h" 31 #include "shill/cellular/mock_modem_cdma_proxy.h" 32 #include "shill/cellular/mock_modem_info.h" 33 #include "shill/cellular/mock_modem_proxy.h" 34 #include "shill/error.h" 35 #include "shill/mock_adaptors.h" 36 #include "shill/test_event_dispatcher.h" 37 38 using base::Bind; 39 using base::Unretained; 40 using std::string; 41 using testing::_; 42 using testing::InSequence; 43 using testing::Invoke; 44 using testing::Return; 45 using testing::StrEq; 46 47 namespace shill { 48 49 class CellularCapabilityCDMATest : public testing::Test { 50 public: 51 CellularCapabilityCDMATest() 52 : modem_info_(nullptr, &dispatcher_, nullptr, nullptr), 53 cellular_(new MockCellular(&modem_info_, 54 "", 55 "", 56 0, 57 Cellular::kTypeCDMA, 58 "", 59 "")), 60 classic_proxy_(new MockModemProxy()), 61 proxy_(new MockModemCDMAProxy()), 62 capability_(nullptr) { 63 modem_info_.metrics()->RegisterDevice(cellular_->interface_index(), 64 Technology::kCellular); 65 } 66 67 virtual ~CellularCapabilityCDMATest() { 68 cellular_->service_ = nullptr; 69 capability_ = nullptr; 70 } 71 72 virtual void SetUp() { 73 capability_ = 74 static_cast<CellularCapabilityCDMA*>(cellular_->capability_.get()); 75 } 76 77 void InvokeActivate(const string& carrier, Error* error, 78 const ActivationResultCallback& callback, 79 int timeout) { 80 callback.Run(MM_MODEM_CDMA_ACTIVATION_ERROR_NO_ERROR, Error()); 81 } 82 void InvokeActivateError(const string& carrier, Error* error, 83 const ActivationResultCallback& callback, 84 int timeout) { 85 callback.Run(MM_MODEM_CDMA_ACTIVATION_ERROR_NO_SIGNAL, Error()); 86 } 87 void InvokeDisconnect(Error* error, 88 const ResultCallback& callback, 89 int timeout) { 90 callback.Run(Error()); 91 } 92 void InvokeDisconnectError(Error* error, 93 const ResultCallback& callback, 94 int timeout) { 95 Error err(Error::kOperationFailed); 96 callback.Run(err); 97 } 98 void InvokeGetSignalQuality(Error* error, 99 const SignalQualityCallback& callback, 100 int timeout) { 101 callback.Run(kStrength, Error()); 102 } 103 void InvokeGetRegistrationState(Error* error, 104 const RegistrationStateCallback& callback, 105 int timeout) { 106 callback.Run(MM_MODEM_CDMA_REGISTRATION_STATE_REGISTERED, 107 MM_MODEM_CDMA_REGISTRATION_STATE_HOME, 108 Error()); 109 } 110 111 MOCK_METHOD1(TestCallback, void(const Error& error)); 112 113 protected: 114 static const char kMEID[]; 115 static const char kTestCarrier[]; 116 static const unsigned int kStrength; 117 118 bool IsActivationStarting() const { 119 return capability_->activation_starting_; 120 } 121 122 void SetRegistrationStateEVDO(uint32_t state) { 123 capability_->registration_state_evdo_ = state; 124 } 125 126 void SetRegistrationState1x(uint32_t state) { 127 capability_->registration_state_1x_ = state; 128 } 129 130 void SetProxy() { 131 capability_->proxy_.reset(proxy_.release()); 132 capability_->CellularCapabilityClassic::proxy_.reset( 133 classic_proxy_.release()); 134 } 135 136 void SetService() { 137 cellular_->service_ = new CellularService(&modem_info_, cellular_); 138 } 139 140 void SetDeviceState(Cellular::State state) { 141 cellular_->state_ = state; 142 } 143 144 EventDispatcherForTest dispatcher_; 145 MockModemInfo modem_info_; 146 scoped_refptr<MockCellular> cellular_; 147 std::unique_ptr<MockModemProxy> classic_proxy_; 148 std::unique_ptr<MockModemCDMAProxy> proxy_; 149 CellularCapabilityCDMA* capability_; // Owned by |cellular_|. 150 }; 151 152 const char CellularCapabilityCDMATest::kMEID[] = "D1234567EF8901"; 153 const char CellularCapabilityCDMATest::kTestCarrier[] = "The Cellular Carrier"; 154 const unsigned int CellularCapabilityCDMATest::kStrength = 90; 155 156 TEST_F(CellularCapabilityCDMATest, PropertyStore) { 157 EXPECT_TRUE(cellular_->store().Contains(kPRLVersionProperty)); 158 } 159 160 TEST_F(CellularCapabilityCDMATest, Activate) { 161 SetDeviceState(Cellular::kStateEnabled); 162 EXPECT_CALL(*proxy_, Activate(kTestCarrier, _, _, 163 CellularCapability::kTimeoutActivate)) 164 .WillOnce(Invoke(this, 165 &CellularCapabilityCDMATest::InvokeActivate)); 166 EXPECT_CALL(*this, TestCallback(_)); 167 SetProxy(); 168 SetService(); 169 capability_->Activate(kTestCarrier, nullptr, 170 Bind(&CellularCapabilityCDMATest::TestCallback, Unretained(this))); 171 EXPECT_EQ(MM_MODEM_CDMA_ACTIVATION_STATE_ACTIVATING, 172 capability_->activation_state()); 173 EXPECT_EQ(kActivationStateActivating, 174 cellular_->service()->activation_state()); 175 EXPECT_EQ("", cellular_->service()->error()); 176 } 177 178 TEST_F(CellularCapabilityCDMATest, ActivateWhileConnected) { 179 SetDeviceState(Cellular::kStateConnected); 180 { 181 InSequence dummy; 182 183 EXPECT_CALL(*cellular_, Disconnect(_, StrEq("Activate"))); 184 EXPECT_CALL(*proxy_, Activate(kTestCarrier, _, _, 185 CellularCapability::kTimeoutActivate)) 186 .WillOnce(Invoke(this, 187 &CellularCapabilityCDMATest::InvokeActivate)); 188 EXPECT_CALL(*this, TestCallback(_)); 189 } 190 SetProxy(); 191 SetService(); 192 Error error; 193 capability_->Activate(kTestCarrier, &error, 194 Bind(&CellularCapabilityCDMATest::TestCallback, Unretained(this))); 195 // So now we should be "activating" while we wait for a disconnect. 196 EXPECT_TRUE(IsActivationStarting()); 197 EXPECT_TRUE(capability_->IsActivating()); 198 EXPECT_EQ(MM_MODEM_CDMA_ACTIVATION_STATE_NOT_ACTIVATED, 199 capability_->activation_state()); 200 // Simulate a disconnect. 201 SetDeviceState(Cellular::kStateRegistered); 202 capability_->DisconnectCleanup(); 203 // Now the modem is actually activating. 204 EXPECT_EQ(MM_MODEM_CDMA_ACTIVATION_STATE_ACTIVATING, 205 capability_->activation_state()); 206 EXPECT_EQ(kActivationStateActivating, 207 cellular_->service()->activation_state()); 208 EXPECT_EQ("", cellular_->service()->error()); 209 EXPECT_FALSE(IsActivationStarting()); 210 EXPECT_TRUE(capability_->IsActivating()); 211 } 212 213 TEST_F(CellularCapabilityCDMATest, ActivateWhileConnectedButFail) { 214 SetDeviceState(Cellular::kStateConnected); 215 { 216 InSequence dummy; 217 218 EXPECT_CALL(*cellular_, Disconnect(_, StrEq("Activate"))); 219 EXPECT_CALL(*proxy_, Activate(kTestCarrier, _, _, 220 CellularCapability::kTimeoutActivate)) 221 .Times(0); 222 } 223 SetProxy(); 224 SetService(); 225 Error error; 226 capability_->Activate(kTestCarrier, &error, 227 Bind(&CellularCapabilityCDMATest::TestCallback, Unretained(this))); 228 // So now we should be "activating" while we wait for a disconnect. 229 EXPECT_TRUE(IsActivationStarting()); 230 EXPECT_TRUE(capability_->IsActivating()); 231 EXPECT_EQ(MM_MODEM_CDMA_ACTIVATION_STATE_NOT_ACTIVATED, 232 capability_->activation_state()); 233 // Similulate a failed disconnect (the modem is still connected!). 234 capability_->DisconnectCleanup(); 235 EXPECT_EQ(MM_MODEM_CDMA_ACTIVATION_STATE_NOT_ACTIVATED, 236 capability_->activation_state()); 237 EXPECT_EQ(kActivationStateNotActivated, 238 cellular_->service()->activation_state()); 239 EXPECT_EQ(kErrorActivationFailed, cellular_->service()->error()); 240 EXPECT_FALSE(IsActivationStarting()); 241 EXPECT_FALSE(capability_->IsActivating()); 242 } 243 244 TEST_F(CellularCapabilityCDMATest, ActivateError) { 245 SetDeviceState(Cellular::kStateEnabled); 246 EXPECT_CALL(*proxy_, Activate(kTestCarrier, _, _, 247 CellularCapability::kTimeoutActivate)) 248 .WillOnce(Invoke(this, 249 &CellularCapabilityCDMATest::InvokeActivateError)); 250 EXPECT_CALL(*this, TestCallback(_)); 251 SetProxy(); 252 SetService(); 253 capability_->Activate(kTestCarrier, nullptr, 254 Bind(&CellularCapabilityCDMATest::TestCallback, Unretained(this))); 255 EXPECT_EQ(MM_MODEM_CDMA_ACTIVATION_STATE_NOT_ACTIVATED, 256 capability_->activation_state()); 257 EXPECT_EQ(kActivationStateNotActivated, 258 cellular_->service()->activation_state()); 259 EXPECT_EQ(kErrorActivationFailed, 260 cellular_->service()->error()); 261 } 262 263 TEST_F(CellularCapabilityCDMATest, GetActivationStateString) { 264 EXPECT_EQ(kActivationStateActivated, 265 CellularCapabilityCDMA::GetActivationStateString( 266 MM_MODEM_CDMA_ACTIVATION_STATE_ACTIVATED)); 267 EXPECT_EQ(kActivationStateActivating, 268 CellularCapabilityCDMA::GetActivationStateString( 269 MM_MODEM_CDMA_ACTIVATION_STATE_ACTIVATING)); 270 EXPECT_EQ(kActivationStateNotActivated, 271 CellularCapabilityCDMA::GetActivationStateString( 272 MM_MODEM_CDMA_ACTIVATION_STATE_NOT_ACTIVATED)); 273 EXPECT_EQ(kActivationStatePartiallyActivated, 274 CellularCapabilityCDMA::GetActivationStateString( 275 MM_MODEM_CDMA_ACTIVATION_STATE_PARTIALLY_ACTIVATED)); 276 EXPECT_EQ(kActivationStateUnknown, 277 CellularCapabilityCDMA::GetActivationStateString(123)); 278 } 279 280 TEST_F(CellularCapabilityCDMATest, GetActivationErrorString) { 281 EXPECT_EQ(kErrorNeedEvdo, 282 CellularCapabilityCDMA::GetActivationErrorString( 283 MM_MODEM_CDMA_ACTIVATION_ERROR_WRONG_RADIO_INTERFACE)); 284 EXPECT_EQ(kErrorNeedHomeNetwork, 285 CellularCapabilityCDMA::GetActivationErrorString( 286 MM_MODEM_CDMA_ACTIVATION_ERROR_ROAMING)); 287 EXPECT_EQ(kErrorOtaspFailed, 288 CellularCapabilityCDMA::GetActivationErrorString( 289 MM_MODEM_CDMA_ACTIVATION_ERROR_COULD_NOT_CONNECT)); 290 EXPECT_EQ(kErrorOtaspFailed, 291 CellularCapabilityCDMA::GetActivationErrorString( 292 MM_MODEM_CDMA_ACTIVATION_ERROR_SECURITY_AUTHENTICATION_FAILED)); 293 EXPECT_EQ(kErrorOtaspFailed, 294 CellularCapabilityCDMA::GetActivationErrorString( 295 MM_MODEM_CDMA_ACTIVATION_ERROR_PROVISIONING_FAILED)); 296 EXPECT_EQ("", 297 CellularCapabilityCDMA::GetActivationErrorString( 298 MM_MODEM_CDMA_ACTIVATION_ERROR_NO_ERROR)); 299 EXPECT_EQ(kErrorActivationFailed, 300 CellularCapabilityCDMA::GetActivationErrorString( 301 MM_MODEM_CDMA_ACTIVATION_ERROR_NO_SIGNAL)); 302 EXPECT_EQ(kErrorActivationFailed, 303 CellularCapabilityCDMA::GetActivationErrorString(1234)); 304 } 305 306 TEST_F(CellularCapabilityCDMATest, IsRegisteredEVDO) { 307 EXPECT_FALSE(capability_->IsRegistered()); 308 SetRegistrationStateEVDO(MM_MODEM_CDMA_REGISTRATION_STATE_UNKNOWN); 309 EXPECT_FALSE(capability_->IsRegistered()); 310 SetRegistrationStateEVDO(MM_MODEM_CDMA_REGISTRATION_STATE_REGISTERED); 311 EXPECT_TRUE(capability_->IsRegistered()); 312 SetRegistrationStateEVDO(MM_MODEM_CDMA_REGISTRATION_STATE_HOME); 313 EXPECT_TRUE(capability_->IsRegistered()); 314 SetRegistrationStateEVDO(MM_MODEM_CDMA_REGISTRATION_STATE_ROAMING); 315 EXPECT_TRUE(capability_->IsRegistered()); 316 } 317 318 TEST_F(CellularCapabilityCDMATest, IsRegistered1x) { 319 EXPECT_FALSE(capability_->IsRegistered()); 320 SetRegistrationState1x(MM_MODEM_CDMA_REGISTRATION_STATE_UNKNOWN); 321 EXPECT_FALSE(capability_->IsRegistered()); 322 SetRegistrationState1x(MM_MODEM_CDMA_REGISTRATION_STATE_REGISTERED); 323 EXPECT_TRUE(capability_->IsRegistered()); 324 SetRegistrationState1x(MM_MODEM_CDMA_REGISTRATION_STATE_HOME); 325 EXPECT_TRUE(capability_->IsRegistered()); 326 SetRegistrationState1x(MM_MODEM_CDMA_REGISTRATION_STATE_ROAMING); 327 EXPECT_TRUE(capability_->IsRegistered()); 328 } 329 330 TEST_F(CellularCapabilityCDMATest, GetNetworkTechnologyString) { 331 EXPECT_EQ("", capability_->GetNetworkTechnologyString()); 332 SetRegistrationStateEVDO(MM_MODEM_CDMA_REGISTRATION_STATE_HOME); 333 EXPECT_EQ(kNetworkTechnologyEvdo, 334 capability_->GetNetworkTechnologyString()); 335 SetRegistrationStateEVDO(MM_MODEM_CDMA_REGISTRATION_STATE_UNKNOWN); 336 SetRegistrationState1x(MM_MODEM_CDMA_REGISTRATION_STATE_HOME); 337 EXPECT_EQ(kNetworkTechnology1Xrtt, 338 capability_->GetNetworkTechnologyString()); 339 } 340 341 TEST_F(CellularCapabilityCDMATest, GetRoamingStateString) { 342 EXPECT_EQ(kRoamingStateUnknown, 343 capability_->GetRoamingStateString()); 344 SetRegistrationStateEVDO(MM_MODEM_CDMA_REGISTRATION_STATE_REGISTERED); 345 EXPECT_EQ(kRoamingStateUnknown, 346 capability_->GetRoamingStateString()); 347 SetRegistrationStateEVDO(MM_MODEM_CDMA_REGISTRATION_STATE_HOME); 348 EXPECT_EQ(kRoamingStateHome, capability_->GetRoamingStateString()); 349 SetRegistrationStateEVDO(MM_MODEM_CDMA_REGISTRATION_STATE_ROAMING); 350 EXPECT_EQ(kRoamingStateRoaming, 351 capability_->GetRoamingStateString()); 352 SetRegistrationStateEVDO(MM_MODEM_CDMA_REGISTRATION_STATE_UNKNOWN); 353 SetRegistrationState1x(MM_MODEM_CDMA_REGISTRATION_STATE_REGISTERED); 354 EXPECT_EQ(kRoamingStateUnknown, 355 capability_->GetRoamingStateString()); 356 SetRegistrationState1x(MM_MODEM_CDMA_REGISTRATION_STATE_HOME); 357 EXPECT_EQ(kRoamingStateHome, capability_->GetRoamingStateString()); 358 SetRegistrationState1x(MM_MODEM_CDMA_REGISTRATION_STATE_ROAMING); 359 EXPECT_EQ(kRoamingStateRoaming, 360 capability_->GetRoamingStateString()); 361 } 362 363 TEST_F(CellularCapabilityCDMATest, GetSignalQuality) { 364 EXPECT_CALL(*proxy_, 365 GetSignalQuality(nullptr, _, CellularCapability::kTimeoutDefault)) 366 .WillOnce(Invoke(this, 367 &CellularCapabilityCDMATest::InvokeGetSignalQuality)); 368 SetProxy(); 369 SetService(); 370 EXPECT_EQ(0, cellular_->service()->strength()); 371 capability_->GetSignalQuality(); 372 EXPECT_EQ(kStrength, cellular_->service()->strength()); 373 } 374 375 TEST_F(CellularCapabilityCDMATest, GetRegistrationState) { 376 EXPECT_FALSE(cellular_->service().get()); 377 EXPECT_EQ(MM_MODEM_CDMA_REGISTRATION_STATE_UNKNOWN, 378 capability_->registration_state_1x()); 379 EXPECT_EQ(MM_MODEM_CDMA_REGISTRATION_STATE_UNKNOWN, 380 capability_->registration_state_evdo()); 381 EXPECT_CALL(*proxy_, 382 GetRegistrationState(nullptr, _, 383 CellularCapability::kTimeoutDefault)) 384 .WillOnce(Invoke( 385 this, 386 &CellularCapabilityCDMATest::InvokeGetRegistrationState)); 387 SetProxy(); 388 cellular_->state_ = Cellular::kStateEnabled; 389 EXPECT_CALL(*modem_info_.mock_manager(), RegisterService(_)); 390 capability_->GetRegistrationState(); 391 dispatcher_.DispatchPendingEvents(); 392 EXPECT_EQ(MM_MODEM_CDMA_REGISTRATION_STATE_REGISTERED, 393 capability_->registration_state_1x()); 394 EXPECT_EQ(MM_MODEM_CDMA_REGISTRATION_STATE_HOME, 395 capability_->registration_state_evdo()); 396 EXPECT_TRUE(cellular_->service().get()); 397 } 398 399 } // namespace shill 400