cellular_unittest.cc revision bad1c10ffd2d4ac14f7bd9f4ef6a8982e711f566
1// 2// Copyright (C) 2013 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.h" 18 19#include <sys/socket.h> 20#include <linux/if.h> // NOLINT - Needs typedefs from sys/socket.h. 21#include <linux/netlink.h> 22 23#include <base/bind.h> 24#include <chromeos/dbus/service_constants.h> 25 26#include "shill/cellular/cellular_bearer.h" 27#include "shill/cellular/cellular_capability_cdma.h" 28#include "shill/cellular/cellular_capability_classic.h" 29#include "shill/cellular/cellular_capability_gsm.h" 30#include "shill/cellular/cellular_capability_universal.h" 31#include "shill/cellular/cellular_service.h" 32#include "shill/cellular/mock_cellular_service.h" 33#include "shill/cellular/mock_mm1_modem_modem3gpp_proxy.h" 34#include "shill/cellular/mock_mm1_modem_proxy.h" 35#include "shill/cellular/mock_mm1_modem_simple_proxy.h" 36#include "shill/cellular/mock_mobile_operator_info.h" 37#include "shill/cellular/mock_modem_cdma_proxy.h" 38#include "shill/cellular/mock_modem_gsm_card_proxy.h" 39#include "shill/cellular/mock_modem_gsm_network_proxy.h" 40#include "shill/cellular/mock_modem_info.h" 41#include "shill/cellular/mock_modem_proxy.h" 42#include "shill/cellular/mock_modem_simple_proxy.h" 43#include "shill/dhcp/mock_dhcp_config.h" 44#include "shill/dhcp/mock_dhcp_provider.h" 45#include "shill/error.h" 46#include "shill/mock_adaptors.h" 47#include "shill/mock_control.h" 48#include "shill/mock_dbus_properties_proxy.h" 49#include "shill/mock_device_info.h" 50#include "shill/mock_external_task.h" 51#include "shill/mock_ppp_device.h" 52#include "shill/mock_ppp_device_factory.h" 53#include "shill/mock_process_manager.h" 54#include "shill/net/mock_rtnl_handler.h" 55#include "shill/property_store_unittest.h" 56#include "shill/rpc_task.h" // for RpcTaskDelegate 57#include "shill/test_event_dispatcher.h" 58#include "shill/testing.h" 59 60// mm/mm-modem.h must be included after cellular_capability_universal.h 61// in order to allow MM_MODEM_CDMA_* to be defined properly. 62#include <mm/mm-modem.h> 63 64using base::Bind; 65using base::Unretained; 66using std::map; 67using std::string; 68using std::unique_ptr; 69using std::vector; 70using testing::_; 71using testing::AnyNumber; 72using testing::DoAll; 73using testing::Invoke; 74using testing::Mock; 75using testing::NiceMock; 76using testing::Return; 77using testing::ReturnRef; 78using testing::SaveArg; 79using testing::SetArgumentPointee; 80using testing::Unused; 81 82namespace shill { 83 84class CellularPropertyTest : public PropertyStoreTest { 85 public: 86 CellularPropertyTest() 87 : modem_info_(control_interface(), 88 dispatcher(), 89 metrics(), 90 manager()), 91 device_(new Cellular(&modem_info_, 92 "usb0", 93 "00:01:02:03:04:05", 94 3, 95 Cellular::kTypeCDMA, 96 "", 97 "")) {} 98 virtual ~CellularPropertyTest() {} 99 100 protected: 101 MockModemInfo modem_info_; 102 DeviceRefPtr device_; 103}; 104 105TEST_F(CellularPropertyTest, Contains) { 106 EXPECT_TRUE(device_->store().Contains(kNameProperty)); 107 EXPECT_FALSE(device_->store().Contains("")); 108} 109 110TEST_F(CellularPropertyTest, SetProperty) { 111 { 112 Error error; 113 const bool allow_roaming = true; 114 EXPECT_TRUE(device_->mutable_store()->SetAnyProperty( 115 kCellularAllowRoamingProperty, allow_roaming, &error)); 116 } 117 // Ensure that attempting to write a R/O property returns InvalidArgs error. 118 { 119 Error error; 120 EXPECT_FALSE(device_->mutable_store()->SetAnyProperty( 121 kAddressProperty, PropertyStoreTest::kStringV, &error)); 122 ASSERT_TRUE(error.IsFailure()); // name() may be invalid otherwise 123 EXPECT_EQ(Error::kInvalidArguments, error.type()); 124 } 125 { 126 Error error; 127 EXPECT_FALSE(device_->mutable_store()->SetAnyProperty( 128 kCarrierProperty, PropertyStoreTest::kStringV, &error)); 129 ASSERT_TRUE(error.IsFailure()); // name() may be invalid otherwise 130 EXPECT_EQ(Error::kInvalidArguments, error.type()); 131 } 132} 133 134class CellularTest : public testing::Test { 135 public: 136 CellularTest() 137 : kHomeProviderCode("10001"), 138 kHomeProviderCountry("us"), 139 kHomeProviderName("HomeProviderName"), 140 kServingOperatorCode("10002"), 141 kServingOperatorCountry("ca"), 142 kServingOperatorName("ServingOperatorName"), 143 control_interface_(this), 144 modem_info_(&control_interface_, &dispatcher_, nullptr, nullptr), 145 device_info_(modem_info_.control_interface(), &dispatcher_, 146 modem_info_.metrics(), modem_info_.manager()), 147 dhcp_config_(new MockDHCPConfig(modem_info_.control_interface(), 148 kTestDeviceName)), 149 create_gsm_card_proxy_from_factory_(false), 150 mock_home_provider_info_(nullptr), 151 mock_serving_operator_info_(nullptr), 152 device_(new Cellular(&modem_info_, 153 kTestDeviceName, 154 kTestDeviceAddress, 155 3, 156 Cellular::kTypeGSM, 157 kDBusService, 158 kDBusPath)) { 159 PopulateProxies(); 160 modem_info_.metrics()->RegisterDevice(device_->interface_index(), 161 Technology::kCellular); 162 } 163 164 virtual void SetUp() { 165 static_cast<Device*>(device_.get())->rtnl_handler_ = &rtnl_handler_; 166 device_->set_dhcp_provider(&dhcp_provider_); 167 device_->process_manager_ = &process_manager_; 168 EXPECT_CALL(*modem_info_.mock_manager(), device_info()) 169 .WillRepeatedly(Return(&device_info_)); 170 EXPECT_CALL(*modem_info_.mock_manager(), DeregisterService(_)) 171 .Times(AnyNumber()); 172 } 173 174 virtual void TearDown() { 175 device_->DestroyIPConfig(); 176 device_->state_ = Cellular::kStateDisabled; 177 device_->capability_->ReleaseProxies(); 178 device_->set_dhcp_provider(nullptr); 179 // Break cycle between Cellular and CellularService. 180 device_->service_ = nullptr; 181 device_->SelectService(nullptr); 182 } 183 184 void PopulateProxies() { 185 dbus_properties_proxy_.reset(new MockDBusPropertiesProxy()); 186 proxy_.reset(new MockModemProxy()); 187 simple_proxy_.reset(new MockModemSimpleProxy()); 188 cdma_proxy_.reset(new MockModemCDMAProxy()); 189 gsm_card_proxy_.reset(new MockModemGSMCardProxy()); 190 gsm_network_proxy_.reset(new MockModemGSMNetworkProxy()); 191 mm1_modem_3gpp_proxy_.reset(new mm1::MockModemModem3gppProxy()); 192 mm1_proxy_.reset(new mm1::MockModemProxy()); 193 mm1_simple_proxy_.reset(new mm1::MockModemSimpleProxy()); 194 } 195 196 void SetMockMobileOperatorInfoObjects() { 197 mock_home_provider_info_ = 198 new MockMobileOperatorInfo(&dispatcher_, "HomeProvider"); 199 // Takes ownership. 200 device_->set_home_provider_info(mock_home_provider_info_); 201 202 mock_serving_operator_info_ = 203 new MockMobileOperatorInfo(&dispatcher_, "ServingOperator"); 204 // Takes ownership. 205 device_->set_serving_operator_info(mock_serving_operator_info_); 206 } 207 208 void InvokeEnable(bool enable, Error* error, 209 const ResultCallback& callback, int timeout) { 210 callback.Run(Error()); 211 } 212 void InvokeEnableReturningWrongState( 213 bool enable, Error* error, const ResultCallback& callback, int timeout) { 214 callback.Run(Error(Error::kWrongState)); 215 } 216 void InvokeGetSignalQuality(Error* error, 217 const SignalQualityCallback& callback, 218 int timeout) { 219 callback.Run(kStrength, Error()); 220 } 221 void InvokeGetModemStatus(Error* error, 222 const KeyValueStoreCallback& callback, 223 int timeout) { 224 KeyValueStore props; 225 props.SetString("carrier", kTestCarrier); 226 props.SetString("unknown-property", "irrelevant-value"); 227 callback.Run(props, Error()); 228 } 229 void InvokeGetModemInfo(Error* error, const ModemInfoCallback& callback, 230 int timeout) { 231 static const char kManufacturer[] = "Company"; 232 static const char kModelID[] = "Gobi 2000"; 233 static const char kHWRev[] = "A00B1234"; 234 callback.Run(kManufacturer, kModelID, kHWRev, Error()); 235 } 236 void InvokeGetRegistrationState1X(Error* error, 237 const RegistrationStateCallback& callback, 238 int timeout) { 239 callback.Run(MM_MODEM_CDMA_REGISTRATION_STATE_HOME, 240 MM_MODEM_CDMA_REGISTRATION_STATE_UNKNOWN, 241 Error()); 242 } 243 void InvokeGetIMEI(Error* error, const GSMIdentifierCallback& callback, 244 int timeout) { 245 callback.Run(kIMEI, Error()); 246 } 247 void InvokeGetIMSI(Error* error, const GSMIdentifierCallback& callback, 248 int timeout) { 249 callback.Run(kIMSI, Error()); 250 } 251 void InvokeGetMSISDN(Error* error, const GSMIdentifierCallback& callback, 252 int timeout) { 253 callback.Run(kMSISDN, Error()); 254 } 255 void InvokeGetSPN(Error* error, const GSMIdentifierCallback& callback, 256 int timeout) { 257 callback.Run(kTestCarrierSPN, Error()); 258 } 259 void InvokeGetRegistrationInfo(Error* error, 260 const RegistrationInfoCallback& callback, 261 int timeout) { 262 static const char kNetworkID[] = "22803"; 263 callback.Run(MM_MODEM_GSM_NETWORK_REG_STATUS_ROAMING, 264 kNetworkID, kTestCarrier, Error()); 265 } 266 void InvokeRegister(const string& network_id, 267 Error* error, 268 const ResultCallback& callback, 269 int timeout) { 270 callback.Run(Error()); 271 } 272 void InvokeGetRegistrationState(Error* error, 273 const RegistrationStateCallback& callback, 274 int timeout) { 275 callback.Run(MM_MODEM_CDMA_REGISTRATION_STATE_REGISTERED, 276 MM_MODEM_CDMA_REGISTRATION_STATE_HOME, 277 Error()); 278 } 279 void InvokeGetRegistrationStateUnregistered( 280 Error* error, 281 const RegistrationStateCallback& callback, 282 int timeout) { 283 callback.Run(MM_MODEM_CDMA_REGISTRATION_STATE_UNKNOWN, 284 MM_MODEM_CDMA_REGISTRATION_STATE_UNKNOWN, 285 Error()); 286 } 287 void InvokeConnect(KeyValueStore props, Error* error, 288 const ResultCallback& callback, int timeout) { 289 EXPECT_EQ(Service::kStateAssociating, device_->service_->state()); 290 callback.Run(Error()); 291 } 292 void InvokeConnectFail(KeyValueStore props, Error* error, 293 const ResultCallback& callback, int timeout) { 294 EXPECT_EQ(Service::kStateAssociating, device_->service_->state()); 295 callback.Run(Error(Error::kNotOnHomeNetwork)); 296 } 297 void InvokeConnectFailNoService(KeyValueStore props, Error* error, 298 const ResultCallback& callback, int timeout) { 299 device_->service_ = nullptr; 300 callback.Run(Error(Error::kNotOnHomeNetwork)); 301 } 302 void InvokeConnectSuccessNoService(KeyValueStore props, Error* error, 303 const ResultCallback& callback, 304 int timeout) { 305 device_->service_ = nullptr; 306 callback.Run(Error()); 307 } 308 void InvokeDisconnect(Error* error, const ResultCallback& callback, 309 int timeout) { 310 if (!callback.is_null()) 311 callback.Run(Error()); 312 } 313 void InvokeDisconnectFail(Error* error, const ResultCallback& callback, 314 int timeout) { 315 error->Populate(Error::kOperationFailed); 316 if (!callback.is_null()) 317 callback.Run(*error); 318 } 319 void InvokeDisconnectMM1(const string& bearer, Error* error, 320 const ResultCallback& callback, int timeout) { 321 if (!callback.is_null()) 322 callback.Run(Error()); 323 } 324 void InvokeSetPowerState(const uint32_t& power_state, 325 Error* error, 326 const ResultCallback& callback, 327 int timeout) { 328 callback.Run(Error()); 329 } 330 void ExpectCdmaStartModem(string network_technology) { 331 if (!device_->IsUnderlyingDeviceEnabled()) 332 EXPECT_CALL(*proxy_, 333 Enable(true, _, _, CellularCapability::kTimeoutEnable)) 334 .WillOnce(Invoke(this, &CellularTest::InvokeEnable)); 335 EXPECT_CALL(*simple_proxy_, 336 GetModemStatus(_, _, CellularCapability::kTimeoutDefault)) 337 .WillOnce(Invoke(this, &CellularTest::InvokeGetModemStatus)); 338 EXPECT_CALL(*proxy_, 339 GetModemInfo(_, _, CellularCapability::kTimeoutDefault)) 340 .WillOnce(Invoke(this, &CellularTest::InvokeGetModemInfo)); 341 if (network_technology == kNetworkTechnology1Xrtt) 342 EXPECT_CALL(*cdma_proxy_, GetRegistrationState(nullptr, _, _)) 343 .WillOnce(Invoke(this, &CellularTest::InvokeGetRegistrationState1X)); 344 else 345 EXPECT_CALL(*cdma_proxy_, GetRegistrationState(nullptr, _, _)) 346 .WillOnce(Invoke(this, &CellularTest::InvokeGetRegistrationState)); 347 EXPECT_CALL(*cdma_proxy_, GetSignalQuality(nullptr, _, _)) 348 .Times(2) 349 .WillRepeatedly(Invoke(this, &CellularTest::InvokeGetSignalQuality)); 350 EXPECT_CALL(*this, TestCallback(IsSuccess())); 351 EXPECT_CALL(*modem_info_.mock_manager(), RegisterService(_)); 352 } 353 354 void ExpectDisconnectCapabilityUniversal() { 355 SetCellularType(Cellular::kTypeUniversal); 356 device_->state_ = Cellular::kStateConnected; 357 EXPECT_CALL(*mm1_simple_proxy_, Disconnect(_, _, _, _)) 358 .WillOnce(Invoke(this, &CellularTest::InvokeDisconnectMM1)); 359 GetCapabilityUniversal()->modem_simple_proxy_.reset( 360 mm1_simple_proxy_.release()); 361 } 362 363 void VerifyDisconnect() { 364 EXPECT_EQ(Cellular::kStateRegistered, device_->state_); 365 } 366 367 void StartPPP(int pid) { 368 EXPECT_CALL(process_manager_, StartProcess(_, _, _, _, _, _)) 369 .WillOnce(Return(pid)); 370 device_->StartPPP("fake_serial_device"); 371 EXPECT_FALSE(device_->ipconfig()); // No DHCP client. 372 EXPECT_FALSE(device_->selected_service()); 373 EXPECT_FALSE(device_->is_ppp_authenticating_); 374 EXPECT_NE(nullptr, device_->ppp_task_); 375 Mock::VerifyAndClearExpectations(&process_manager_); 376 } 377 378 void FakeUpConnectedPPP() { 379 const char kInterfaceName[] = "fake-ppp-device"; 380 const int kInterfaceIndex = -1; 381 auto mock_ppp_device = make_scoped_refptr( 382 new MockPPPDevice(modem_info_.control_interface(), nullptr, nullptr, 383 nullptr, kInterfaceName, kInterfaceIndex)); 384 device_->ppp_device_ = mock_ppp_device; 385 device_->state_ = Cellular::kStateConnected; 386 } 387 388 void ExpectPPPStopped() { 389 auto mock_ppp_device = 390 dynamic_cast<MockPPPDevice*>(device_->ppp_device_.get()); 391 EXPECT_CALL(*mock_ppp_device, DropConnection()); 392 } 393 394 void VerifyPPPStopped() { 395 EXPECT_EQ(nullptr, device_->ppp_task_); 396 EXPECT_FALSE(device_->ppp_device_); 397 } 398 399 void SetCommonOnAfterResumeExpectations() { 400 EXPECT_CALL(*dbus_properties_proxy_, GetAll(_)) 401 .WillRepeatedly(Return(KeyValueStore())); 402 EXPECT_CALL(*mm1_proxy_, set_state_changed_callback(_)).Times(AnyNumber()); 403 EXPECT_CALL(*modem_info_.mock_metrics(), NotifyDeviceScanStarted(_)) 404 .Times(AnyNumber()); 405 EXPECT_CALL(*modem_info_.mock_manager(), UpdateEnabledTechnologies()) 406 .Times(AnyNumber()); 407 EXPECT_CALL(*dynamic_cast<DeviceMockAdaptor*>(device_->adaptor()), 408 EmitBoolChanged(_, _)).Times(AnyNumber()); 409 } 410 411 mm1::MockModemProxy* SetupOnAfterResume() { 412 SetCellularType(Cellular::kTypeUniversal); 413 SetCommonOnAfterResumeExpectations(); 414 return mm1_proxy_.get(); // Before the capability snags it. 415 } 416 417 void VerifyOperatorMap(const Stringmap& operator_map, 418 const string& code, 419 const string& name, 420 const string& country) { 421 Stringmap::const_iterator it; 422 Stringmap::const_iterator endit = operator_map.end(); 423 424 it = operator_map.find(kOperatorCodeKey); 425 if (code == "") { 426 EXPECT_EQ(endit, it); 427 } else { 428 ASSERT_NE(endit, it); 429 EXPECT_EQ(code, it->second); 430 } 431 it = operator_map.find(kOperatorNameKey); 432 if (name == "") { 433 EXPECT_EQ(endit, it); 434 } else { 435 ASSERT_NE(endit, it); 436 EXPECT_EQ(name, it->second); 437 } 438 it = operator_map.find(kOperatorCountryKey); 439 if (country == "") { 440 EXPECT_EQ(endit, it); 441 } else { 442 ASSERT_NE(endit, it); 443 EXPECT_EQ(country, it->second); 444 } 445 } 446 447 MOCK_METHOD1(TestCallback, void(const Error& error)); 448 449 protected: 450 static const char kTestDeviceName[]; 451 static const char kTestDeviceAddress[]; 452 static const char kDBusService[]; 453 static const char kDBusPath[]; 454 static const char kTestCarrier[]; 455 static const char kTestCarrierSPN[]; 456 static const char kMEID[]; 457 static const char kIMEI[]; 458 static const char kIMSI[]; 459 static const char kMSISDN[]; 460 static const char kTestMobileProviderDBPath[]; 461 static const Stringmaps kTestNetworksGSM; 462 static const Stringmaps kTestNetworksCellular; 463 static const int kStrength; 464 465 // Must be std::string so that we can safely ReturnRef. 466 const string kHomeProviderCode; 467 const string kHomeProviderCountry; 468 const string kHomeProviderName; 469 const string kServingOperatorCode; 470 const string kServingOperatorCountry; 471 const string kServingOperatorName; 472 473 class TestControl : public MockControl { 474 public: 475 explicit TestControl(CellularTest* test) : test_(test) {} 476 477 virtual DBusPropertiesProxyInterface* CreateDBusPropertiesProxy( 478 const std::string& path, 479 const std::string& service) { 480 CHECK(test_->dbus_properties_proxy_); 481 return test_->dbus_properties_proxy_.release(); 482 } 483 484 virtual ModemProxyInterface* CreateModemProxy( 485 const string& /*path*/, 486 const string& /*service*/) { 487 CHECK(test_->proxy_); 488 return test_->proxy_.release(); 489 } 490 491 virtual ModemSimpleProxyInterface* CreateModemSimpleProxy( 492 const string& /*path*/, 493 const string& /*service*/) { 494 CHECK(test_->simple_proxy_); 495 return test_->simple_proxy_.release(); 496 } 497 498 virtual ModemCDMAProxyInterface* CreateModemCDMAProxy( 499 const string& /*path*/, 500 const string& /*service*/) { 501 CHECK(test_->cdma_proxy_); 502 return test_->cdma_proxy_.release(); 503 } 504 505 virtual ModemGSMCardProxyInterface* CreateModemGSMCardProxy( 506 const string& /*path*/, 507 const string& /*service*/) { 508 // TODO(benchan): This code conditionally returns a nullptr to avoid 509 // CellularCapabilityGSM::InitProperties (and thus 510 // CellularCapabilityGSM::GetIMSI) from being called during the 511 // construction. Remove this workaround after refactoring the tests. 512 CHECK(!test_->create_gsm_card_proxy_from_factory_ || 513 test_->gsm_card_proxy_); 514 return test_->create_gsm_card_proxy_from_factory_ ? 515 test_->gsm_card_proxy_.release() : nullptr; 516 } 517 518 virtual ModemGSMNetworkProxyInterface* CreateModemGSMNetworkProxy( 519 const string& /*path*/, 520 const string& /*service*/) { 521 CHECK(test_->gsm_network_proxy_); 522 return test_->gsm_network_proxy_.release(); 523 } 524 525 virtual mm1::ModemModem3gppProxyInterface* CreateMM1ModemModem3gppProxy( 526 const std::string& path, 527 const std::string& service) { 528 CHECK(test_->mm1_modem_3gpp_proxy_); 529 return test_->mm1_modem_3gpp_proxy_.release(); 530 } 531 532 virtual mm1::ModemProxyInterface* CreateMM1ModemProxy( 533 const std::string& path, 534 const std::string& service) { 535 CHECK(test_->mm1_proxy_); 536 return test_->mm1_proxy_.release(); 537 } 538 539 virtual mm1::ModemSimpleProxyInterface* CreateMM1ModemSimpleProxy( 540 const string& /*path*/, 541 const string& /*service*/) { 542 CHECK(test_->mm1_simple_proxy_); 543 return test_->mm1_simple_proxy_.release(); 544 } 545 546 private: 547 CellularTest* test_; 548 }; 549 void StartRTNLHandler(); 550 void StopRTNLHandler(); 551 552 void AllowCreateGSMCardProxyFromFactory() { 553 create_gsm_card_proxy_from_factory_ = true; 554 } 555 556 void SetCellularType(Cellular::Type type) { 557 device_->InitCapability(type); 558 } 559 560 CellularCapabilityClassic* GetCapabilityClassic() { 561 return dynamic_cast<CellularCapabilityClassic*>( 562 device_->capability_.get()); 563 } 564 565 CellularCapabilityCDMA* GetCapabilityCDMA() { 566 return dynamic_cast<CellularCapabilityCDMA*>(device_->capability_.get()); 567 } 568 569 CellularCapabilityGSM* GetCapabilityGSM() { 570 return dynamic_cast<CellularCapabilityGSM*>(device_->capability_.get()); 571 } 572 573 CellularCapabilityUniversal* GetCapabilityUniversal() { 574 return dynamic_cast<CellularCapabilityUniversal*>( 575 device_->capability_.get()); 576 } 577 578 // Different tests simulate a cellular service being set using a real /mock 579 // service. 580 CellularService* SetService() { 581 device_->service_ = new CellularService(&modem_info_, device_); 582 return device_->service_.get(); 583 } 584 MockCellularService* SetMockService() { 585 device_->service_ = new MockCellularService(&modem_info_, device_); 586 return static_cast<MockCellularService*>(device_->service_.get()); 587 } 588 589 void set_enabled_persistent(bool new_value) { 590 device_->enabled_persistent_ = new_value; 591 } 592 593 void SetCapabilityUniversalActiveBearer(unique_ptr<CellularBearer> bearer) { 594 SetCellularType(Cellular::kTypeUniversal); 595 CellularCapabilityUniversal* capability = GetCapabilityUniversal(); 596 capability->active_bearer_ = std::move(bearer); 597 } 598 599 EventDispatcherForTest dispatcher_; 600 TestControl control_interface_; 601 MockModemInfo modem_info_; 602 MockDeviceInfo device_info_; 603 MockProcessManager process_manager_; 604 NiceMock<MockRTNLHandler> rtnl_handler_; 605 606 MockDHCPProvider dhcp_provider_; 607 scoped_refptr<MockDHCPConfig> dhcp_config_; 608 609 bool create_gsm_card_proxy_from_factory_; 610 unique_ptr<MockDBusPropertiesProxy> dbus_properties_proxy_; 611 unique_ptr<MockModemProxy> proxy_; 612 unique_ptr<MockModemSimpleProxy> simple_proxy_; 613 unique_ptr<MockModemCDMAProxy> cdma_proxy_; 614 unique_ptr<MockModemGSMCardProxy> gsm_card_proxy_; 615 unique_ptr<MockModemGSMNetworkProxy> gsm_network_proxy_; 616 unique_ptr<mm1::MockModemModem3gppProxy> mm1_modem_3gpp_proxy_; 617 unique_ptr<mm1::MockModemProxy> mm1_proxy_; 618 unique_ptr<mm1::MockModemSimpleProxy> mm1_simple_proxy_; 619 MockMobileOperatorInfo* mock_home_provider_info_; 620 MockMobileOperatorInfo* mock_serving_operator_info_; 621 CellularRefPtr device_; 622}; 623 624const char CellularTest::kTestDeviceName[] = "usb0"; 625const char CellularTest::kTestDeviceAddress[] = "00:01:02:03:04:05"; 626const char CellularTest::kDBusService[] = "org.chromium.ModemManager"; 627const char CellularTest::kDBusPath[] = "/org/chromium/ModemManager/Gobi/0"; 628const char CellularTest::kTestCarrier[] = "The Cellular Carrier"; 629const char CellularTest::kTestCarrierSPN[] = "Home Provider"; 630const char CellularTest::kMEID[] = "01234567EF8901"; 631const char CellularTest::kIMEI[] = "987654321098765"; 632const char CellularTest::kIMSI[] = "123456789012345"; 633const char CellularTest::kMSISDN[] = "12345678901"; 634const char CellularTest::kTestMobileProviderDBPath[] = 635 "provider_db_unittest.bfd"; 636const Stringmaps CellularTest::kTestNetworksGSM = 637 {{{CellularCapabilityGSM::kNetworkPropertyStatus, "1"}, 638 {CellularCapabilityGSM::kNetworkPropertyID, "0000"}, 639 {CellularCapabilityGSM::kNetworkPropertyLongName, "some_long_name"}, 640 {CellularCapabilityGSM::kNetworkPropertyShortName, "short"}}}; 641const Stringmaps CellularTest::kTestNetworksCellular = 642 {{{kStatusProperty, "available"}, 643 {kNetworkIdProperty, "0000"}, 644 {kLongNameProperty, "some_long_name"}, 645 {kShortNameProperty, "short"}}}; 646const int CellularTest::kStrength = 90; 647 648TEST_F(CellularTest, GetStateString) { 649 EXPECT_EQ("CellularStateDisabled", 650 Cellular::GetStateString(Cellular::kStateDisabled)); 651 EXPECT_EQ("CellularStateEnabled", 652 Cellular::GetStateString(Cellular::kStateEnabled)); 653 EXPECT_EQ("CellularStateRegistered", 654 Cellular::GetStateString(Cellular::kStateRegistered)); 655 EXPECT_EQ("CellularStateConnected", 656 Cellular::GetStateString(Cellular::kStateConnected)); 657 EXPECT_EQ("CellularStateLinked", 658 Cellular::GetStateString(Cellular::kStateLinked)); 659} 660 661TEST_F(CellularTest, GetModemStateString) { 662 EXPECT_EQ("CellularModemStateFailed", 663 Cellular::GetModemStateString(Cellular::kModemStateFailed)); 664 EXPECT_EQ("CellularModemStateUnknown", 665 Cellular::GetModemStateString(Cellular::kModemStateUnknown)); 666 EXPECT_EQ("CellularModemStateInitializing", 667 Cellular::GetModemStateString(Cellular::kModemStateInitializing)); 668 EXPECT_EQ("CellularModemStateLocked", 669 Cellular::GetModemStateString(Cellular::kModemStateLocked)); 670 EXPECT_EQ("CellularModemStateDisabled", 671 Cellular::GetModemStateString(Cellular::kModemStateDisabled)); 672 EXPECT_EQ("CellularModemStateDisabling", 673 Cellular::GetModemStateString(Cellular::kModemStateDisabling)); 674 EXPECT_EQ("CellularModemStateEnabling", 675 Cellular::GetModemStateString(Cellular::kModemStateEnabling)); 676 EXPECT_EQ("CellularModemStateEnabled", 677 Cellular::GetModemStateString(Cellular::kModemStateEnabled)); 678 EXPECT_EQ("CellularModemStateSearching", 679 Cellular::GetModemStateString(Cellular::kModemStateSearching)); 680 EXPECT_EQ("CellularModemStateRegistered", 681 Cellular::GetModemStateString(Cellular::kModemStateRegistered)); 682 EXPECT_EQ("CellularModemStateDisconnecting", 683 Cellular::GetModemStateString(Cellular::kModemStateDisconnecting)); 684 EXPECT_EQ("CellularModemStateConnecting", 685 Cellular::GetModemStateString(Cellular::kModemStateConnecting)); 686 EXPECT_EQ("CellularModemStateConnected", 687 Cellular::GetModemStateString(Cellular::kModemStateConnected)); 688} 689 690TEST_F(CellularTest, StartCDMARegister) { 691 SetCellularType(Cellular::kTypeCDMA); 692 ExpectCdmaStartModem(kNetworkTechnology1Xrtt); 693 EXPECT_CALL(*cdma_proxy_, MEID()).WillOnce(Return(kMEID)); 694 Error error; 695 device_->Start(&error, Bind(&CellularTest::TestCallback, Unretained(this))); 696 dispatcher_.DispatchPendingEvents(); 697 EXPECT_EQ(kMEID, device_->meid()); 698 EXPECT_EQ(kTestCarrier, device_->carrier()); 699 EXPECT_EQ(Cellular::kStateRegistered, device_->state_); 700 ASSERT_TRUE(device_->service_.get()); 701 EXPECT_EQ(kNetworkTechnology1Xrtt, device_->service_->network_technology()); 702 EXPECT_EQ(kStrength, device_->service_->strength()); 703 EXPECT_EQ(kRoamingStateHome, device_->service_->roaming_state()); 704} 705 706TEST_F(CellularTest, StartGSMRegister) { 707 SetMockMobileOperatorInfoObjects(); 708 EXPECT_CALL(*proxy_, Enable(true, _, _, CellularCapability::kTimeoutEnable)) 709 .WillOnce(Invoke(this, &CellularTest::InvokeEnable)); 710 EXPECT_CALL(*gsm_card_proxy_, 711 GetIMEI(_, _, CellularCapability::kTimeoutDefault)) 712 .WillOnce(Invoke(this, &CellularTest::InvokeGetIMEI)); 713 EXPECT_CALL(*gsm_card_proxy_, 714 GetIMSI(_, _, CellularCapability::kTimeoutDefault)) 715 .WillOnce(Invoke(this, &CellularTest::InvokeGetIMSI)); 716 EXPECT_CALL(*gsm_card_proxy_, 717 GetSPN(_, _, CellularCapability::kTimeoutDefault)) 718 .WillOnce(Invoke(this, &CellularTest::InvokeGetSPN)); 719 EXPECT_CALL(*gsm_card_proxy_, 720 GetMSISDN(_, _, CellularCapability::kTimeoutDefault)) 721 .WillOnce(Invoke(this, &CellularTest::InvokeGetMSISDN)); 722 EXPECT_CALL(*gsm_network_proxy_, AccessTechnology()) 723 .WillOnce(Return(MM_MODEM_GSM_ACCESS_TECH_EDGE)); 724 EXPECT_CALL(*gsm_card_proxy_, EnabledFacilityLocks()) 725 .WillOnce(Return(MM_MODEM_GSM_FACILITY_SIM)); 726 EXPECT_CALL(*proxy_, GetModemInfo(_, _, CellularCapability::kTimeoutDefault)) 727 .WillOnce(Invoke(this, &CellularTest::InvokeGetModemInfo)); 728 EXPECT_CALL(*gsm_network_proxy_, 729 GetRegistrationInfo(_, _, CellularCapability::kTimeoutDefault)) 730 .WillOnce(Invoke(this, &CellularTest::InvokeGetRegistrationInfo)); 731 EXPECT_CALL(*gsm_network_proxy_, GetSignalQuality(nullptr, _, _)) 732 .Times(2) 733 .WillRepeatedly(Invoke(this, 734 &CellularTest::InvokeGetSignalQuality)); 735 EXPECT_CALL(*mock_serving_operator_info_, UpdateMCCMNC(_)); 736 EXPECT_CALL(*mock_serving_operator_info_, UpdateOperatorName(_)); 737 EXPECT_CALL(*this, TestCallback(IsSuccess())); 738 EXPECT_CALL(*modem_info_.mock_manager(), RegisterService(_)); 739 AllowCreateGSMCardProxyFromFactory(); 740 741 Error error; 742 device_->Start(&error, Bind(&CellularTest::TestCallback, Unretained(this))); 743 EXPECT_TRUE(error.IsSuccess()); 744 dispatcher_.DispatchPendingEvents(); 745 EXPECT_EQ(kIMEI, device_->imei()); 746 EXPECT_EQ(kIMSI, device_->imsi()); 747 EXPECT_EQ(kTestCarrierSPN, GetCapabilityGSM()->spn_); 748 EXPECT_EQ(kMSISDN, device_->mdn()); 749 EXPECT_EQ(Cellular::kStateRegistered, device_->state_); 750 ASSERT_TRUE(device_->service_.get()); 751 EXPECT_EQ(kNetworkTechnologyEdge, device_->service_->network_technology()); 752 EXPECT_TRUE(GetCapabilityGSM()->sim_lock_status_.enabled); 753 EXPECT_EQ(kStrength, device_->service_->strength()); 754 EXPECT_EQ(kRoamingStateRoaming, device_->service_->roaming_state()); 755} 756 757TEST_F(CellularTest, StartConnected) { 758 EXPECT_CALL(device_info_, GetFlags(device_->interface_index(), _)) 759 .WillOnce(Return(true)); 760 SetCellularType(Cellular::kTypeCDMA); 761 device_->set_modem_state(Cellular::kModemStateConnected); 762 device_->set_meid(kMEID); 763 ExpectCdmaStartModem(kNetworkTechnologyEvdo); 764 Error error; 765 device_->Start(&error, Bind(&CellularTest::TestCallback, Unretained(this))); 766 EXPECT_TRUE(error.IsSuccess()); 767 dispatcher_.DispatchPendingEvents(); 768 EXPECT_EQ(Cellular::kStateConnected, device_->state_); 769} 770 771TEST_F(CellularTest, StartLinked) { 772 EXPECT_CALL(device_info_, GetFlags(device_->interface_index(), _)) 773 .WillOnce(DoAll(SetArgumentPointee<1>(IFF_UP), Return(true))); 774 SetCellularType(Cellular::kTypeCDMA); 775 device_->set_modem_state(Cellular::kModemStateConnected); 776 device_->set_meid(kMEID); 777 ExpectCdmaStartModem(kNetworkTechnologyEvdo); 778 EXPECT_CALL(dhcp_provider_, CreateIPv4Config(kTestDeviceName, _, _, _)) 779 .WillOnce(Return(dhcp_config_)); 780 EXPECT_CALL(*dhcp_config_, RequestIP()).WillOnce(Return(true)); 781 EXPECT_CALL(*modem_info_.mock_manager(), UpdateService(_)).Times(3); 782 Error error; 783 device_->Start(&error, Bind(&CellularTest::TestCallback, Unretained(this))); 784 EXPECT_TRUE(error.IsSuccess()); 785 dispatcher_.DispatchPendingEvents(); 786 EXPECT_EQ(Cellular::kStateLinked, device_->state_); 787 EXPECT_EQ(Service::kStateConfiguring, device_->service_->state()); 788 device_->SelectService(nullptr); 789} 790 791TEST_F(CellularTest, FriendlyServiceName) { 792 // Test that the name created for the service is sensible under different 793 // scenarios w.r.t. information about the mobile network operator. 794 SetMockMobileOperatorInfoObjects(); 795 CHECK(mock_home_provider_info_); 796 CHECK(mock_serving_operator_info_); 797 798 SetCellularType(Cellular::kTypeCDMA); 799 // We are not testing the behaviour of capabilities here. 800 device_->mobile_operator_info_observer_->set_capability(nullptr); 801 802 // (1) Service created, MNO not known => Default name. 803 EXPECT_CALL(*mock_home_provider_info_, IsMobileNetworkOperatorKnown()) 804 .WillRepeatedly(Return(false)); 805 EXPECT_CALL(*mock_serving_operator_info_, IsMobileNetworkOperatorKnown()) 806 .WillRepeatedly(Return(false)); 807 device_->CreateService(); 808 // Compare substrings explicitly using EXPECT_EQ for better error message. 809 size_t prefix_len = strlen(Cellular::kGenericServiceNamePrefix); 810 EXPECT_EQ(Cellular::kGenericServiceNamePrefix, 811 device_->service_->friendly_name().substr(0, prefix_len)); 812 Mock::VerifyAndClearExpectations(mock_home_provider_info_); 813 Mock::VerifyAndClearExpectations(mock_serving_operator_info_); 814 device_->DestroyService(); 815 816 // (2) Service created, then home provider determined => Name provided by 817 // home provider. 818 EXPECT_CALL(*mock_serving_operator_info_, IsMobileNetworkOperatorKnown()) 819 .WillRepeatedly(Return(false)); 820 EXPECT_CALL(*mock_home_provider_info_, IsMobileNetworkOperatorKnown()) 821 .WillRepeatedly(Return(false)); 822 device_->CreateService(); 823 // Now emulate an event for updated home provider information. 824 Mock::VerifyAndClearExpectations(mock_home_provider_info_); 825 mock_home_provider_info_->SetEmptyDefaultsForProperties(); 826 EXPECT_CALL(*mock_home_provider_info_, IsMobileNetworkOperatorKnown()) 827 .WillRepeatedly(Return(true)); 828 EXPECT_CALL(*mock_home_provider_info_, operator_name()) 829 .WillRepeatedly(ReturnRef(kHomeProviderName)); 830 device_->mobile_operator_info_observer_->OnOperatorChanged(); 831 EXPECT_EQ(kHomeProviderName, device_->service_->friendly_name()); 832 Mock::VerifyAndClearExpectations(mock_home_provider_info_); 833 Mock::VerifyAndClearExpectations(mock_serving_operator_info_); 834 device_->DestroyService(); 835 836 // (3) Service created, then serving operator determined => Name provided by 837 // serving operator. 838 EXPECT_CALL(*mock_home_provider_info_, IsMobileNetworkOperatorKnown()) 839 .WillRepeatedly(Return(false)); 840 EXPECT_CALL(*mock_serving_operator_info_, IsMobileNetworkOperatorKnown()) 841 .WillRepeatedly(Return(false)); 842 device_->CreateService(); 843 // Now emulate an event for updated serving operator information. 844 Mock::VerifyAndClearExpectations(mock_serving_operator_info_); 845 mock_serving_operator_info_->SetEmptyDefaultsForProperties(); 846 EXPECT_CALL(*mock_serving_operator_info_, IsMobileNetworkOperatorKnown()) 847 .WillRepeatedly(Return(true)); 848 EXPECT_CALL(*mock_serving_operator_info_, operator_name()) 849 .WillRepeatedly(ReturnRef(kServingOperatorName)); 850 device_->mobile_operator_info_observer_->OnOperatorChanged(); 851 EXPECT_EQ(kServingOperatorName, device_->service_->friendly_name()); 852 Mock::VerifyAndClearExpectations(mock_home_provider_info_); 853 Mock::VerifyAndClearExpectations(mock_serving_operator_info_); 854 device_->DestroyService(); 855 856 // (4) Service created, then home provider determined, then serving operator 857 // determined => final name is serving operator. 858 EXPECT_CALL(*mock_home_provider_info_, IsMobileNetworkOperatorKnown()) 859 .WillRepeatedly(Return(false)); 860 EXPECT_CALL(*mock_serving_operator_info_, IsMobileNetworkOperatorKnown()) 861 .WillRepeatedly(Return(false)); 862 device_->CreateService(); 863 // Now emulate an event for updated home provider information. 864 Mock::VerifyAndClearExpectations(mock_home_provider_info_); 865 mock_home_provider_info_->SetEmptyDefaultsForProperties(); 866 EXPECT_CALL(*mock_home_provider_info_, IsMobileNetworkOperatorKnown()) 867 .WillRepeatedly(Return(true)); 868 EXPECT_CALL(*mock_home_provider_info_, operator_name()) 869 .WillRepeatedly(ReturnRef(kHomeProviderName)); 870 device_->mobile_operator_info_observer_->OnOperatorChanged(); 871 // Now emulate an event for updated serving operator information. 872 Mock::VerifyAndClearExpectations(mock_serving_operator_info_); 873 mock_serving_operator_info_->SetEmptyDefaultsForProperties(); 874 EXPECT_CALL(*mock_serving_operator_info_, IsMobileNetworkOperatorKnown()) 875 .WillRepeatedly(Return(true)); 876 EXPECT_CALL(*mock_serving_operator_info_, operator_name()) 877 .WillRepeatedly(ReturnRef(kServingOperatorName)); 878 device_->mobile_operator_info_observer_->OnOperatorChanged(); 879 EXPECT_EQ(kServingOperatorName, device_->service_->friendly_name()); 880 Mock::VerifyAndClearExpectations(mock_home_provider_info_); 881 Mock::VerifyAndClearExpectations(mock_serving_operator_info_); 882 device_->DestroyService(); 883 884 // (5) Service created, then serving operator determined, then home provider 885 // determined => final name is serving operator. 886 EXPECT_CALL(*mock_home_provider_info_, IsMobileNetworkOperatorKnown()) 887 .WillRepeatedly(Return(false)); 888 EXPECT_CALL(*mock_serving_operator_info_, IsMobileNetworkOperatorKnown()) 889 .WillRepeatedly(Return(false)); 890 device_->CreateService(); 891 // Now emulate an event for updated serving operator information. 892 Mock::VerifyAndClearExpectations(mock_serving_operator_info_); 893 mock_serving_operator_info_->SetEmptyDefaultsForProperties(); 894 EXPECT_CALL(*mock_serving_operator_info_, IsMobileNetworkOperatorKnown()) 895 .WillRepeatedly(Return(true)); 896 EXPECT_CALL(*mock_serving_operator_info_, operator_name()) 897 .WillRepeatedly(ReturnRef(kServingOperatorName)); 898 device_->mobile_operator_info_observer_->OnOperatorChanged(); 899 // Now emulate an event for updated home provider information. 900 Mock::VerifyAndClearExpectations(mock_home_provider_info_); 901 mock_home_provider_info_->SetEmptyDefaultsForProperties(); 902 EXPECT_CALL(*mock_home_provider_info_, IsMobileNetworkOperatorKnown()) 903 .WillRepeatedly(Return(true)); 904 EXPECT_CALL(*mock_home_provider_info_, operator_name()) 905 .WillRepeatedly(ReturnRef(kHomeProviderName)); 906 device_->mobile_operator_info_observer_->OnOperatorChanged(); 907 EXPECT_EQ(kServingOperatorName, device_->service_->friendly_name()); 908 Mock::VerifyAndClearExpectations(mock_home_provider_info_); 909 Mock::VerifyAndClearExpectations(mock_serving_operator_info_); 910 device_->DestroyService(); 911 912 // (6) Serving operator known, home provider known, and then service created 913 // => Name is serving operator. 914 mock_home_provider_info_->SetEmptyDefaultsForProperties(); 915 mock_serving_operator_info_->SetEmptyDefaultsForProperties(); 916 EXPECT_CALL(*mock_serving_operator_info_, IsMobileNetworkOperatorKnown()) 917 .WillRepeatedly(Return(true)); 918 EXPECT_CALL(*mock_home_provider_info_, IsMobileNetworkOperatorKnown()) 919 .WillRepeatedly(Return(true)); 920 EXPECT_CALL(*mock_home_provider_info_, operator_name()) 921 .WillRepeatedly(ReturnRef(kHomeProviderName)); 922 EXPECT_CALL(*mock_serving_operator_info_, operator_name()) 923 .WillRepeatedly(ReturnRef(kServingOperatorName)); 924 device_->CreateService(); 925 EXPECT_EQ(kServingOperatorName, device_->service_->friendly_name()); 926} 927 928TEST_F(CellularTest, HomeProviderServingOperator) { 929 // Test that the the home provider information is correctly updated under 930 // different scenarios w.r.t. information about the mobile network operators. 931 SetMockMobileOperatorInfoObjects(); 932 CHECK(mock_home_provider_info_); 933 CHECK(mock_serving_operator_info_); 934 Stringmap home_provider; 935 Stringmap serving_operator; 936 937 938 // (1) Neither home provider nor serving operator known. 939 EXPECT_CALL(*mock_home_provider_info_, IsMobileNetworkOperatorKnown()) 940 .WillRepeatedly(Return(false)); 941 EXPECT_CALL(*mock_serving_operator_info_, IsMobileNetworkOperatorKnown()) 942 .WillRepeatedly(Return(false)); 943 944 device_->CreateService(); 945 946 home_provider = device_->home_provider(); 947 VerifyOperatorMap(home_provider, "", "", ""); 948 serving_operator = device_->service_->serving_operator(); 949 VerifyOperatorMap(serving_operator, "", "", ""); 950 Mock::VerifyAndClearExpectations(mock_home_provider_info_); 951 Mock::VerifyAndClearExpectations(mock_serving_operator_info_); 952 device_->DestroyService(); 953 954 // (2) serving operator known. 955 // When home provider is not known, serving operator proxies in. 956 EXPECT_CALL(*mock_serving_operator_info_, IsMobileNetworkOperatorKnown()) 957 .WillRepeatedly(Return(false)); 958 mock_serving_operator_info_->SetEmptyDefaultsForProperties(); 959 EXPECT_CALL(*mock_serving_operator_info_, IsMobileNetworkOperatorKnown()) 960 .WillRepeatedly(Return(true)); 961 EXPECT_CALL(*mock_serving_operator_info_, mccmnc()) 962 .WillRepeatedly(ReturnRef(kServingOperatorCode)); 963 EXPECT_CALL(*mock_serving_operator_info_, operator_name()) 964 .WillRepeatedly(ReturnRef(kServingOperatorName)); 965 EXPECT_CALL(*mock_serving_operator_info_, country()) 966 .WillRepeatedly(ReturnRef(kServingOperatorCountry)); 967 968 device_->CreateService(); 969 970 home_provider = device_->home_provider(); 971 VerifyOperatorMap(home_provider, 972 kServingOperatorCode, 973 kServingOperatorName, 974 kServingOperatorCountry); 975 serving_operator = device_->service_->serving_operator(); 976 VerifyOperatorMap(serving_operator, 977 kServingOperatorCode, 978 kServingOperatorName, 979 kServingOperatorCountry); 980 Mock::VerifyAndClearExpectations(mock_home_provider_info_); 981 Mock::VerifyAndClearExpectations(mock_serving_operator_info_); 982 device_->DestroyService(); 983 984 // (3) home provider known. 985 // When serving operator is not known, home provider proxies in. 986 EXPECT_CALL(*mock_serving_operator_info_, IsMobileNetworkOperatorKnown()) 987 .WillRepeatedly(Return(false)); 988 mock_home_provider_info_->SetEmptyDefaultsForProperties(); 989 EXPECT_CALL(*mock_home_provider_info_, IsMobileNetworkOperatorKnown()) 990 .WillRepeatedly(Return(true)); 991 EXPECT_CALL(*mock_home_provider_info_, mccmnc()) 992 .WillRepeatedly(ReturnRef(kHomeProviderCode)); 993 EXPECT_CALL(*mock_home_provider_info_, operator_name()) 994 .WillRepeatedly(ReturnRef(kHomeProviderName)); 995 EXPECT_CALL(*mock_home_provider_info_, country()) 996 .WillRepeatedly(ReturnRef(kHomeProviderCountry)); 997 998 device_->CreateService(); 999 1000 home_provider = device_->home_provider(); 1001 VerifyOperatorMap(home_provider, 1002 kHomeProviderCode, 1003 kHomeProviderName, 1004 kHomeProviderCountry); 1005 serving_operator = device_->service_->serving_operator(); 1006 VerifyOperatorMap(serving_operator, 1007 kHomeProviderCode, 1008 kHomeProviderName, 1009 kHomeProviderCountry); 1010 Mock::VerifyAndClearExpectations(mock_home_provider_info_); 1011 Mock::VerifyAndClearExpectations(mock_serving_operator_info_); 1012 device_->DestroyService(); 1013 1014 // (4) Serving operator known, home provider known. 1015 mock_home_provider_info_->SetEmptyDefaultsForProperties(); 1016 EXPECT_CALL(*mock_home_provider_info_, IsMobileNetworkOperatorKnown()) 1017 .WillRepeatedly(Return(true)); 1018 EXPECT_CALL(*mock_home_provider_info_, mccmnc()) 1019 .WillRepeatedly(ReturnRef(kHomeProviderCode)); 1020 EXPECT_CALL(*mock_home_provider_info_, operator_name()) 1021 .WillRepeatedly(ReturnRef(kHomeProviderName)); 1022 EXPECT_CALL(*mock_home_provider_info_, country()) 1023 .WillRepeatedly(ReturnRef(kHomeProviderCountry)); 1024 mock_serving_operator_info_->SetEmptyDefaultsForProperties(); 1025 EXPECT_CALL(*mock_serving_operator_info_, IsMobileNetworkOperatorKnown()) 1026 .WillRepeatedly(Return(true)); 1027 EXPECT_CALL(*mock_serving_operator_info_, mccmnc()) 1028 .WillRepeatedly(ReturnRef(kServingOperatorCode)); 1029 EXPECT_CALL(*mock_serving_operator_info_, operator_name()) 1030 .WillRepeatedly(ReturnRef(kServingOperatorName)); 1031 EXPECT_CALL(*mock_serving_operator_info_, country()) 1032 .WillRepeatedly(ReturnRef(kServingOperatorCountry)); 1033 1034 device_->CreateService(); 1035 1036 home_provider = device_->home_provider(); 1037 VerifyOperatorMap(home_provider, 1038 kHomeProviderCode, 1039 kHomeProviderName, 1040 kHomeProviderCountry); 1041 serving_operator = device_->service_->serving_operator(); 1042 VerifyOperatorMap(serving_operator, 1043 kServingOperatorCode, 1044 kServingOperatorName, 1045 kServingOperatorCountry); 1046} 1047 1048static bool IllegalChar(char a) { 1049 return !(isalnum(a) || a == '_'); 1050} 1051 1052TEST_F(CellularTest, StorageIdentifier) { 1053 // Test that the storage identifier name used by the service is sensible under 1054 // different scenarios w.r.t. information about the mobile network operator. 1055 SetMockMobileOperatorInfoObjects(); 1056 mock_home_provider_info_->SetEmptyDefaultsForProperties(); 1057 mock_serving_operator_info_->SetEmptyDefaultsForProperties(); 1058 CHECK(mock_home_provider_info_); 1059 CHECK(mock_serving_operator_info_); 1060 1061 // See cellular_service.cc 1062 string prefix = string(kTypeCellular) + "_" + 1063 string(kTestDeviceAddress) + "_"; 1064 // Service replaces ':' with '_' 1065 std::replace_if(prefix.begin(), prefix.end(), &IllegalChar, '_'); 1066 const string kUuidHomeProvider = "uuidHomeProvider"; 1067 const string kUuidServingOperator = "uuidServingOperator"; 1068 const string kSimIdentifier = "12345123451234512345"; 1069 1070 SetCellularType(Cellular::kTypeCDMA); 1071 // We are not testing the behaviour of capabilities here. 1072 device_->mobile_operator_info_observer_->set_capability(nullptr); 1073 ON_CALL(*mock_home_provider_info_, IsMobileNetworkOperatorKnown()) 1074 .WillByDefault(Return(false)); 1075 1076 // (1) Service created, both home provider and serving operator known => 1077 // home provider used. 1078 mock_home_provider_info_->SetEmptyDefaultsForProperties(); 1079 mock_serving_operator_info_->SetEmptyDefaultsForProperties(); 1080 EXPECT_CALL(*mock_home_provider_info_, IsMobileNetworkOperatorKnown()) 1081 .WillRepeatedly(Return(true)); 1082 EXPECT_CALL(*mock_home_provider_info_, uuid()) 1083 .WillRepeatedly(ReturnRef(kUuidHomeProvider)); 1084 EXPECT_CALL(*mock_serving_operator_info_, IsMobileNetworkOperatorKnown()) 1085 .WillRepeatedly(Return(true)); 1086 EXPECT_CALL(*mock_serving_operator_info_, uuid()) 1087 .WillRepeatedly(ReturnRef(kUuidServingOperator)); 1088 device_->CreateService(); 1089 EXPECT_EQ(prefix + kUuidHomeProvider, 1090 device_->service()->GetStorageIdentifier()); 1091 Mock::VerifyAndClearExpectations(mock_home_provider_info_); 1092 Mock::VerifyAndClearExpectations(mock_serving_operator_info_); 1093 device_->DestroyService(); 1094 1095 // Common expectation for following tests: 1096 EXPECT_CALL(*mock_home_provider_info_, IsMobileNetworkOperatorKnown()) 1097 .WillRepeatedly(Return(false)); 1098 1099 // (2) Service created, no extra information => Default storage_id; 1100 EXPECT_CALL(*mock_serving_operator_info_, IsMobileNetworkOperatorKnown()) 1101 .WillRepeatedly(Return(false)); 1102 device_->CreateService(); 1103 EXPECT_EQ(prefix + device_->service()->friendly_name(), 1104 device_->service()->GetStorageIdentifier()); 1105 Mock::VerifyAndClearExpectations(mock_serving_operator_info_); 1106 device_->DestroyService(); 1107 1108 // (3) Service created, serving operator known, uuid known. 1109 mock_serving_operator_info_->SetEmptyDefaultsForProperties(); 1110 EXPECT_CALL(*mock_serving_operator_info_, IsMobileNetworkOperatorKnown()) 1111 .WillRepeatedly(Return(true)); 1112 EXPECT_CALL(*mock_serving_operator_info_, uuid()) 1113 .WillRepeatedly(ReturnRef(kUuidServingOperator)); 1114 device_->CreateService(); 1115 EXPECT_EQ(prefix + kUuidServingOperator, 1116 device_->service()->GetStorageIdentifier()); 1117 Mock::VerifyAndClearExpectations(mock_serving_operator_info_); 1118 device_->DestroyService(); 1119 1120 // (4) Service created, serving operator known, uuid not known, iccid known. 1121 mock_serving_operator_info_->SetEmptyDefaultsForProperties(); 1122 EXPECT_CALL(*mock_serving_operator_info_, IsMobileNetworkOperatorKnown()) 1123 .WillRepeatedly(Return(true)); 1124 device_->set_sim_identifier(kSimIdentifier); 1125 device_->CreateService(); 1126 EXPECT_EQ(prefix + kSimIdentifier, 1127 device_->service()->GetStorageIdentifier()); 1128 Mock::VerifyAndClearExpectations(mock_serving_operator_info_); 1129 device_->DestroyService(); 1130} 1131 1132namespace { 1133 1134MATCHER(ContainsPhoneNumber, "") { 1135 return arg.ContainsString( 1136 CellularCapabilityClassic::kConnectPropertyPhoneNumber); 1137} 1138 1139} // namespace 1140 1141TEST_F(CellularTest, Connect) { 1142 Error error; 1143 EXPECT_CALL(device_info_, GetFlags(device_->interface_index(), _)) 1144 .Times(2) 1145 .WillRepeatedly(Return(true)); 1146 device_->state_ = Cellular::kStateConnected; 1147 device_->Connect(&error); 1148 EXPECT_EQ(Error::kAlreadyConnected, error.type()); 1149 error.Populate(Error::kSuccess); 1150 1151 device_->state_ = Cellular::kStateLinked; 1152 device_->Connect(&error); 1153 EXPECT_EQ(Error::kAlreadyConnected, error.type()); 1154 1155 device_->state_ = Cellular::kStateEnabled; 1156 device_->Connect(&error); 1157 EXPECT_EQ(Error::kNotRegistered, error.type()); 1158 1159 error.Reset(); 1160 device_->state_ = Cellular::kStateDisabled; 1161 device_->Connect(&error); 1162 EXPECT_EQ(Error::kNotRegistered, error.type()); 1163 1164 device_->state_ = Cellular::kStateRegistered; 1165 SetService(); 1166 1167 device_->allow_roaming_ = false; 1168 device_->service_->roaming_state_ = kRoamingStateRoaming; 1169 device_->Connect(&error); 1170 EXPECT_EQ(Error::kNotOnHomeNetwork, error.type()); 1171 1172 error.Populate(Error::kSuccess); 1173 EXPECT_CALL(*simple_proxy_, 1174 Connect(ContainsPhoneNumber(), _, _, 1175 CellularCapability::kTimeoutConnect)) 1176 .Times(2) 1177 .WillRepeatedly(Invoke(this, &CellularTest::InvokeConnect)); 1178 GetCapabilityClassic()->simple_proxy_.reset(simple_proxy_.release()); 1179 device_->service_->roaming_state_ = kRoamingStateHome; 1180 device_->state_ = Cellular::kStateRegistered; 1181 device_->Connect(&error); 1182 EXPECT_TRUE(error.IsSuccess()); 1183 dispatcher_.DispatchPendingEvents(); 1184 EXPECT_EQ(Cellular::kStateConnected, device_->state_); 1185 1186 device_->allow_roaming_ = true; 1187 device_->service_->roaming_state_ = kRoamingStateRoaming; 1188 device_->state_ = Cellular::kStateRegistered; 1189 device_->Connect(&error); 1190 EXPECT_TRUE(error.IsSuccess()); 1191 dispatcher_.DispatchPendingEvents(); 1192 EXPECT_EQ(Cellular::kStateConnected, device_->state_); 1193} 1194 1195TEST_F(CellularTest, Disconnect) { 1196 Error error; 1197 device_->state_ = Cellular::kStateRegistered; 1198 device_->Disconnect(&error, "in test"); 1199 EXPECT_EQ(Error::kNotConnected, error.type()); 1200 error.Reset(); 1201 1202 device_->state_ = Cellular::kStateConnected; 1203 EXPECT_CALL(*proxy_, 1204 Disconnect(_, _, CellularCapability::kTimeoutDisconnect)) 1205 .WillOnce(Invoke(this, &CellularTest::InvokeDisconnect)); 1206 GetCapabilityClassic()->proxy_.reset(proxy_.release()); 1207 device_->Disconnect(&error, "in test"); 1208 EXPECT_TRUE(error.IsSuccess()); 1209 EXPECT_EQ(Cellular::kStateRegistered, device_->state_); 1210} 1211 1212TEST_F(CellularTest, DisconnectFailure) { 1213 // Test the case where the underlying modem state is set 1214 // to disconnecting, but shill thinks it's still connected 1215 Error error; 1216 device_->state_ = Cellular::kStateConnected; 1217 EXPECT_CALL(*proxy_, 1218 Disconnect(_, _, CellularCapability::kTimeoutDisconnect)) 1219 .Times(2) 1220 .WillRepeatedly(Invoke(this, &CellularTest::InvokeDisconnectFail)); 1221 GetCapabilityClassic()->proxy_.reset(proxy_.release()); 1222 device_->modem_state_ = Cellular::kModemStateDisconnecting; 1223 device_->Disconnect(&error, "in test"); 1224 EXPECT_TRUE(error.IsFailure()); 1225 EXPECT_EQ(Cellular::kStateConnected, device_->state_); 1226 1227 device_->modem_state_ = Cellular::kModemStateConnected; 1228 device_->Disconnect(&error, "in test"); 1229 EXPECT_TRUE(error.IsFailure()); 1230 EXPECT_EQ(Cellular::kStateRegistered, device_->state_); 1231} 1232 1233TEST_F(CellularTest, ConnectFailure) { 1234 SetCellularType(Cellular::kTypeCDMA); 1235 device_->state_ = Cellular::kStateRegistered; 1236 SetService(); 1237 ASSERT_EQ(Service::kStateIdle, device_->service_->state()); 1238 EXPECT_CALL(*simple_proxy_, 1239 Connect(_, _, _, CellularCapability::kTimeoutConnect)) 1240 .WillOnce(Invoke(this, &CellularTest::InvokeConnectFail)); 1241 GetCapabilityClassic()->simple_proxy_.reset(simple_proxy_.release()); 1242 Error error; 1243 device_->Connect(&error); 1244 EXPECT_EQ(Service::kStateFailure, device_->service_->state()); 1245} 1246 1247TEST_F(CellularTest, ConnectFailureNoService) { 1248 // Make sure we don't crash if the connect failed and there is no 1249 // CellularService object. This can happen if the modem is enabled and 1250 // then quick disabled. 1251 SetCellularType(Cellular::kTypeCDMA); 1252 device_->state_ = Cellular::kStateRegistered; 1253 SetService(); 1254 EXPECT_CALL( 1255 *simple_proxy_, 1256 Connect(_, _, _, CellularCapability::kTimeoutConnect)) 1257 .WillOnce(Invoke(this, &CellularTest::InvokeConnectFailNoService)); 1258 EXPECT_CALL(*modem_info_.mock_manager(), UpdateService(_)); 1259 GetCapabilityClassic()->simple_proxy_.reset(simple_proxy_.release()); 1260 Error error; 1261 device_->Connect(&error); 1262} 1263 1264TEST_F(CellularTest, ConnectSuccessNoService) { 1265 // Make sure we don't crash if the connect succeeds but the service was 1266 // destroyed before the connect request completes. 1267 SetCellularType(Cellular::kTypeCDMA); 1268 device_->state_ = Cellular::kStateRegistered; 1269 SetService(); 1270 EXPECT_CALL( 1271 *simple_proxy_, 1272 Connect(_, _, _, CellularCapability::kTimeoutConnect)) 1273 .WillOnce(Invoke(this, &CellularTest::InvokeConnectSuccessNoService)); 1274 EXPECT_CALL(*modem_info_.mock_manager(), UpdateService(_)); 1275 GetCapabilityClassic()->simple_proxy_.reset(simple_proxy_.release()); 1276 Error error; 1277 device_->Connect(&error); 1278} 1279 1280TEST_F(CellularTest, LinkEventWontDestroyService) { 1281 // If the network interface goes down, Cellular::LinkEvent should 1282 // drop the connection but the service object should persist. 1283 device_->state_ = Cellular::kStateLinked; 1284 CellularService* service = SetService(); 1285 device_->LinkEvent(0, 0); // flags doesn't contain IFF_UP 1286 EXPECT_EQ(device_->state_, Cellular::kStateConnected); 1287 EXPECT_EQ(device_->service_, service); 1288} 1289 1290TEST_F(CellularTest, UseNoArpGateway) { 1291 EXPECT_CALL(dhcp_provider_, CreateIPv4Config(kTestDeviceName, _, _, false)) 1292 .WillOnce(Return(dhcp_config_)); 1293 device_->AcquireIPConfig(); 1294} 1295 1296TEST_F(CellularTest, ModemStateChangeEnable) { 1297 EXPECT_CALL(*simple_proxy_, 1298 GetModemStatus(_, _, CellularCapability::kTimeoutDefault)) 1299 .WillOnce(Invoke(this, &CellularTest::InvokeGetModemStatus)); 1300 EXPECT_CALL(*cdma_proxy_, MEID()).WillOnce(Return(kMEID)); 1301 EXPECT_CALL(*proxy_, 1302 GetModemInfo(_, _, CellularCapability::kTimeoutDefault)) 1303 .WillOnce(Invoke(this, &CellularTest::InvokeGetModemInfo)); 1304 EXPECT_CALL(*cdma_proxy_, GetRegistrationState(nullptr, _, _)) 1305 .WillOnce(Invoke(this, 1306 &CellularTest::InvokeGetRegistrationStateUnregistered)); 1307 EXPECT_CALL(*cdma_proxy_, GetSignalQuality(nullptr, _, _)) 1308 .WillOnce(Invoke(this, &CellularTest::InvokeGetSignalQuality)); 1309 EXPECT_CALL(*modem_info_.mock_manager(), UpdateEnabledTechnologies()); 1310 device_->state_ = Cellular::kStateDisabled; 1311 device_->set_modem_state(Cellular::kModemStateDisabled); 1312 SetCellularType(Cellular::kTypeCDMA); 1313 1314 KeyValueStore props; 1315 props.SetBool(CellularCapabilityClassic::kModemPropertyEnabled, true); 1316 device_->OnPropertiesChanged(MM_MODEM_INTERFACE, props, vector<string>()); 1317 dispatcher_.DispatchPendingEvents(); 1318 1319 EXPECT_EQ(Cellular::kModemStateEnabled, device_->modem_state()); 1320 EXPECT_EQ(Cellular::kStateEnabled, device_->state()); 1321 EXPECT_TRUE(device_->enabled()); 1322} 1323 1324TEST_F(CellularTest, ModemStateChangeDisable) { 1325 EXPECT_CALL(*proxy_, 1326 Disconnect(_, _, CellularCapability::kTimeoutDisconnect)) 1327 .WillOnce(Invoke(this, &CellularTest::InvokeDisconnect)); 1328 EXPECT_CALL(*proxy_, 1329 Enable(false, _, _, CellularCapability::kTimeoutEnable)) 1330 .WillOnce(Invoke(this, &CellularTest::InvokeEnable)); 1331 EXPECT_CALL(*modem_info_.mock_manager(), UpdateEnabledTechnologies()); 1332 device_->enabled_ = true; 1333 device_->enabled_pending_ = true; 1334 device_->state_ = Cellular::kStateEnabled; 1335 device_->set_modem_state(Cellular::kModemStateEnabled); 1336 SetCellularType(Cellular::kTypeCDMA); 1337 GetCapabilityClassic()->InitProxies(); 1338 1339 GetCapabilityClassic()->OnModemStateChangedSignal(kModemClassicStateEnabled, 1340 kModemClassicStateDisabled, 1341 0); 1342 dispatcher_.DispatchPendingEvents(); 1343 1344 EXPECT_EQ(Cellular::kModemStateDisabled, device_->modem_state()); 1345 EXPECT_EQ(Cellular::kStateDisabled, device_->state()); 1346 EXPECT_FALSE(device_->enabled()); 1347} 1348 1349TEST_F(CellularTest, ModemStateChangeStaleConnected) { 1350 // Test to make sure that we ignore stale modem Connected state transitions. 1351 // When a modem is asked to connect and before the connect completes, the 1352 // modem is disabled, it may send a stale Connected state transition after 1353 // it has been disabled. 1354 AllowCreateGSMCardProxyFromFactory(); 1355 device_->state_ = Cellular::kStateDisabled; 1356 device_->modem_state_ = Cellular::kModemStateEnabling; 1357 device_->OnModemStateChanged(Cellular::kModemStateConnected); 1358 dispatcher_.DispatchPendingEvents(); 1359 EXPECT_EQ(Cellular::kStateDisabled, device_->state()); 1360} 1361 1362TEST_F(CellularTest, ModemStateChangeValidConnected) { 1363 device_->state_ = Cellular::kStateEnabled; 1364 device_->modem_state_ = Cellular::kModemStateConnecting; 1365 SetService(); 1366 device_->OnModemStateChanged(Cellular::kModemStateConnected); 1367 EXPECT_EQ(Cellular::kStateConnected, device_->state()); 1368} 1369 1370TEST_F(CellularTest, ModemStateChangeLostRegistration) { 1371 SetCellularType(Cellular::kTypeUniversal); 1372 CellularCapabilityUniversal* capability = GetCapabilityUniversal(); 1373 capability->registration_state_ = MM_MODEM_3GPP_REGISTRATION_STATE_HOME; 1374 EXPECT_TRUE(capability->IsRegistered()); 1375 device_->set_modem_state(Cellular::kModemStateRegistered); 1376 device_->OnModemStateChanged(Cellular::kModemStateEnabled); 1377 EXPECT_FALSE(capability->IsRegistered()); 1378} 1379 1380TEST_F(CellularTest, StartModemCallback) { 1381 EXPECT_CALL(*this, TestCallback(IsSuccess())); 1382 EXPECT_EQ(device_->state_, Cellular::kStateDisabled); 1383 device_->StartModemCallback(Bind(&CellularTest::TestCallback, 1384 Unretained(this)), 1385 Error(Error::kSuccess)); 1386 EXPECT_EQ(device_->state_, Cellular::kStateEnabled); 1387} 1388 1389TEST_F(CellularTest, StartModemCallbackFail) { 1390 EXPECT_CALL(*this, TestCallback(IsFailure())); 1391 EXPECT_EQ(device_->state_, Cellular::kStateDisabled); 1392 device_->StartModemCallback(Bind(&CellularTest::TestCallback, 1393 Unretained(this)), 1394 Error(Error::kOperationFailed)); 1395 EXPECT_EQ(device_->state_, Cellular::kStateDisabled); 1396} 1397 1398TEST_F(CellularTest, StopModemCallback) { 1399 EXPECT_CALL(*this, TestCallback(IsSuccess())); 1400 SetMockService(); 1401 device_->StopModemCallback(Bind(&CellularTest::TestCallback, 1402 Unretained(this)), 1403 Error(Error::kSuccess)); 1404 EXPECT_EQ(device_->state_, Cellular::kStateDisabled); 1405 EXPECT_FALSE(device_->service_.get()); 1406} 1407 1408TEST_F(CellularTest, StopModemCallbackFail) { 1409 EXPECT_CALL(*this, TestCallback(IsFailure())); 1410 SetMockService(); 1411 device_->StopModemCallback(Bind(&CellularTest::TestCallback, 1412 Unretained(this)), 1413 Error(Error::kOperationFailed)); 1414 EXPECT_EQ(device_->state_, Cellular::kStateDisabled); 1415 EXPECT_FALSE(device_->service_.get()); 1416} 1417 1418TEST_F(CellularTest, SetAllowRoaming) { 1419 EXPECT_FALSE(device_->allow_roaming_); 1420 EXPECT_CALL(*modem_info_.mock_manager(), UpdateDevice(_)); 1421 Error error; 1422 device_->SetAllowRoaming(true, &error); 1423 EXPECT_TRUE(error.IsSuccess()); 1424 EXPECT_TRUE(device_->allow_roaming_); 1425} 1426 1427class TestRPCTaskDelegate : 1428 public RPCTaskDelegate, 1429 public base::SupportsWeakPtr<TestRPCTaskDelegate> { 1430 public: 1431 virtual void GetLogin(std::string* user, std::string* password) {} 1432 virtual void Notify(const std::string& reason, 1433 const std::map<std::string, std::string>& dict) {} 1434}; 1435 1436TEST_F(CellularTest, LinkEventUpWithPPP) { 1437 // If PPP is running, don't run DHCP as well. 1438 TestRPCTaskDelegate task_delegate; 1439 base::Callback<void(pid_t, int)> death_callback; 1440 unique_ptr<NiceMock<MockExternalTask>> mock_task( 1441 new NiceMock<MockExternalTask>(modem_info_.control_interface(), 1442 &process_manager_, 1443 task_delegate.AsWeakPtr(), 1444 death_callback)); 1445 EXPECT_CALL(*mock_task, OnDelete()).Times(AnyNumber()); 1446 device_->ppp_task_ = std::move(mock_task); 1447 device_->state_ = Cellular::kStateConnected; 1448 EXPECT_CALL(dhcp_provider_, CreateIPv4Config(kTestDeviceName, _, _, _)) 1449 .Times(0); 1450 EXPECT_CALL(*dhcp_config_, RequestIP()).Times(0); 1451 device_->LinkEvent(IFF_UP, 0); 1452} 1453 1454TEST_F(CellularTest, LinkEventUpWithoutPPP) { 1455 // If PPP is not running, fire up DHCP. 1456 device_->state_ = Cellular::kStateConnected; 1457 EXPECT_CALL(dhcp_provider_, CreateIPv4Config(kTestDeviceName, _, _, _)) 1458 .WillOnce(Return(dhcp_config_)); 1459 EXPECT_CALL(*dhcp_config_, RequestIP()); 1460 EXPECT_CALL(*dhcp_config_, ReleaseIP(_)).Times(AnyNumber()); 1461 device_->LinkEvent(IFF_UP, 0); 1462} 1463 1464TEST_F(CellularTest, StartPPP) { 1465 const int kPID = 234; 1466 EXPECT_EQ(nullptr, device_->ppp_task_); 1467 StartPPP(kPID); 1468} 1469 1470TEST_F(CellularTest, StartPPPAlreadyStarted) { 1471 const int kPID = 234; 1472 StartPPP(kPID); 1473 1474 const int kPID2 = 235; 1475 StartPPP(kPID2); 1476} 1477 1478TEST_F(CellularTest, StartPPPAfterEthernetUp) { 1479 CellularService* service(SetService()); 1480 device_->state_ = Cellular::kStateLinked; 1481 device_->set_ipconfig(dhcp_config_); 1482 device_->SelectService(service); 1483 EXPECT_CALL(*dhcp_config_, ReleaseIP(_)) 1484 .Times(AnyNumber()) 1485 .WillRepeatedly(Return(true)); 1486 const int kPID = 234; 1487 EXPECT_EQ(nullptr, device_->ppp_task_); 1488 StartPPP(kPID); 1489 EXPECT_EQ(Cellular::kStateLinked, device_->state()); 1490} 1491 1492TEST_F(CellularTest, GetLogin) { 1493 // Doesn't crash when there is no service. 1494 string username_to_pppd; 1495 string password_to_pppd; 1496 EXPECT_FALSE(device_->service()); 1497 device_->GetLogin(&username_to_pppd, &password_to_pppd); 1498 1499 // Provides expected username and password in normal case. 1500 const char kFakeUsername[] = "fake-user"; 1501 const char kFakePassword[] = "fake-password"; 1502 CellularService& service(*SetService()); 1503 service.ppp_username_ = kFakeUsername; 1504 service.ppp_password_ = kFakePassword; 1505 device_->GetLogin(&username_to_pppd, &password_to_pppd); 1506} 1507 1508TEST_F(CellularTest, Notify) { 1509 // Common setup. 1510 MockPPPDeviceFactory* ppp_device_factory = 1511 MockPPPDeviceFactory::GetInstance(); 1512 const int kPID = 91; 1513 device_->ppp_device_factory_ = ppp_device_factory; 1514 SetMockService(); 1515 StartPPP(kPID); 1516 1517 const map<string, string> kEmptyArgs; 1518 device_->Notify(kPPPReasonAuthenticating, kEmptyArgs); 1519 EXPECT_TRUE(device_->is_ppp_authenticating_); 1520 device_->Notify(kPPPReasonAuthenticated, kEmptyArgs); 1521 EXPECT_FALSE(device_->is_ppp_authenticating_); 1522 1523 // Normal connect. 1524 const string kInterfaceName("fake-device"); 1525 const int kInterfaceIndex = 1; 1526 scoped_refptr<MockPPPDevice> ppp_device; 1527 map<string, string> ppp_config; 1528 ppp_device = 1529 new MockPPPDevice(modem_info_.control_interface(), nullptr, nullptr, 1530 nullptr, kInterfaceName, kInterfaceIndex); 1531 ppp_config[kPPPInterfaceName] = kInterfaceName; 1532 EXPECT_CALL(device_info_, GetIndex(kInterfaceName)) 1533 .WillOnce(Return(kInterfaceIndex)); 1534 EXPECT_CALL(device_info_, RegisterDevice(_)); 1535 EXPECT_CALL(*ppp_device_factory, 1536 CreatePPPDevice(_, _, _, _, kInterfaceName, kInterfaceIndex)) 1537 .WillOnce(Return(ppp_device.get())); 1538 EXPECT_CALL(*ppp_device, SetEnabled(true)); 1539 EXPECT_CALL(*ppp_device, SelectService(_)); 1540 EXPECT_CALL(*ppp_device, UpdateIPConfigFromPPP(ppp_config, false)); 1541 device_->Notify(kPPPReasonConnect, ppp_config); 1542 Mock::VerifyAndClearExpectations(&device_info_); 1543 Mock::VerifyAndClearExpectations(ppp_device.get()); 1544 1545 // Re-connect on same network device: if pppd sends us multiple connect 1546 // events, we behave sanely. 1547 EXPECT_CALL(device_info_, GetIndex(kInterfaceName)) 1548 .WillOnce(Return(kInterfaceIndex)); 1549 EXPECT_CALL(*ppp_device, SetEnabled(true)); 1550 EXPECT_CALL(*ppp_device, SelectService(_)); 1551 EXPECT_CALL(*ppp_device, UpdateIPConfigFromPPP(ppp_config, false)); 1552 device_->Notify(kPPPReasonConnect, ppp_config); 1553 Mock::VerifyAndClearExpectations(&device_info_); 1554 Mock::VerifyAndClearExpectations(ppp_device.get()); 1555 1556 // Re-connect on new network device: if we still have the PPPDevice 1557 // from a prior connect, this new connect should DTRT. This is 1558 // probably an unlikely case. 1559 const string kInterfaceName2("fake-device2"); 1560 const int kInterfaceIndex2 = 2; 1561 scoped_refptr<MockPPPDevice> ppp_device2; 1562 map<string, string> ppp_config2; 1563 ppp_device2 = 1564 new MockPPPDevice(modem_info_.control_interface(), nullptr, nullptr, 1565 nullptr, kInterfaceName2, kInterfaceIndex2); 1566 ppp_config2[kPPPInterfaceName] = kInterfaceName2; 1567 EXPECT_CALL(device_info_, GetIndex(kInterfaceName2)) 1568 .WillOnce(Return(kInterfaceIndex2)); 1569 EXPECT_CALL(device_info_, 1570 RegisterDevice(static_cast<DeviceRefPtr>(ppp_device2))); 1571 EXPECT_CALL(*ppp_device_factory, 1572 CreatePPPDevice(_, _, _, _, kInterfaceName2, kInterfaceIndex2)) 1573 .WillOnce(Return(ppp_device2.get())); 1574 EXPECT_CALL(*ppp_device, SelectService(ServiceRefPtr(nullptr))); 1575 EXPECT_CALL(*ppp_device2, SetEnabled(true)); 1576 EXPECT_CALL(*ppp_device2, SelectService(_)); 1577 EXPECT_CALL(*ppp_device2, UpdateIPConfigFromPPP(ppp_config2, false)); 1578 device_->Notify(kPPPReasonConnect, ppp_config2); 1579 Mock::VerifyAndClearExpectations(&device_info_); 1580 Mock::VerifyAndClearExpectations(ppp_device.get()); 1581 Mock::VerifyAndClearExpectations(ppp_device2.get()); 1582 1583 // Disconnect should report unknown failure, since we had a 1584 // Notify(kPPPReasonAuthenticated, ...). 1585 EXPECT_CALL(*ppp_device2, SetServiceFailure(Service::kFailureUnknown)); 1586 device_->Notify(kPPPReasonDisconnect, kEmptyArgs); 1587 EXPECT_EQ(nullptr, device_->ppp_task_); 1588 1589 // |Cellular::ppp_task_| is destroyed on the task loop. Must dispatch once to 1590 // cleanup. 1591 dispatcher_.DispatchPendingEvents(); 1592} 1593 1594TEST_F(CellularTest, PPPConnectionFailedBeforeAuth) { 1595 // Test that we properly set Service state in the case where pppd 1596 // disconnects before authenticating (as opposed to the Notify test, 1597 // where pppd disconnects after connecting). 1598 const int kPID = 52; 1599 const map<string, string> kEmptyArgs; 1600 MockCellularService* service = SetMockService(); 1601 StartPPP(kPID); 1602 1603 ExpectDisconnectCapabilityUniversal(); 1604 EXPECT_CALL(*service, SetFailure(Service::kFailureUnknown)); 1605 device_->Notify(kPPPReasonDisconnect, kEmptyArgs); 1606 EXPECT_EQ(nullptr, device_->ppp_task_); 1607 VerifyDisconnect(); 1608 1609 // |Cellular::ppp_task_| is destroyed on the task loop. Must dispatch once to 1610 // cleanup. 1611 dispatcher_.DispatchPendingEvents(); 1612} 1613 1614TEST_F(CellularTest, PPPConnectionFailedDuringAuth) { 1615 // Test that we properly set Service state in the case where pppd 1616 // disconnects during authentication (as opposed to the Notify test, 1617 // where pppd disconnects after connecting). 1618 const int kPID = 52; 1619 const map<string, string> kEmptyArgs; 1620 MockCellularService* service = SetMockService(); 1621 StartPPP(kPID); 1622 1623 ExpectDisconnectCapabilityUniversal(); 1624 EXPECT_CALL(*service, SetFailure(Service::kFailurePPPAuth)); 1625 device_->Notify(kPPPReasonAuthenticating, kEmptyArgs); 1626 device_->Notify(kPPPReasonDisconnect, kEmptyArgs); 1627 EXPECT_EQ(nullptr, device_->ppp_task_); 1628 VerifyDisconnect(); 1629 1630 // |Cellular::ppp_task_| is destroyed on the task loop. Must dispatch once to 1631 // cleanup. 1632 dispatcher_.DispatchPendingEvents(); 1633} 1634 1635TEST_F(CellularTest, PPPConnectionFailedAfterAuth) { 1636 // Test that we properly set Service state in the case where pppd 1637 // disconnects after authenticating, but before connecting (as 1638 // opposed to the Notify test, where pppd disconnects after 1639 // connecting). 1640 const int kPID = 52; 1641 const map<string, string> kEmptyArgs; 1642 MockCellularService* service = SetMockService(); 1643 StartPPP(kPID); 1644 1645 EXPECT_CALL(*service, SetFailure(Service::kFailureUnknown)); 1646 ExpectDisconnectCapabilityUniversal(); 1647 device_->Notify(kPPPReasonAuthenticating, kEmptyArgs); 1648 device_->Notify(kPPPReasonAuthenticated, kEmptyArgs); 1649 device_->Notify(kPPPReasonDisconnect, kEmptyArgs); 1650 EXPECT_EQ(nullptr, device_->ppp_task_); 1651 VerifyDisconnect(); 1652 1653 // |Cellular::ppp_task_| is destroyed on the task loop. Must dispatch once to 1654 // cleanup. 1655 dispatcher_.DispatchPendingEvents(); 1656} 1657 1658TEST_F(CellularTest, OnPPPDied) { 1659 const int kPID = 1234; 1660 const int kExitStatus = 5; 1661 ExpectDisconnectCapabilityUniversal(); 1662 device_->OnPPPDied(kPID, kExitStatus); 1663 VerifyDisconnect(); 1664} 1665 1666TEST_F(CellularTest, OnPPPDiedCleanupDevice) { 1667 // Test that OnPPPDied causes the ppp_device_ reference to be dropped. 1668 const int kPID = 123; 1669 const int kExitStatus = 5; 1670 StartPPP(kPID); 1671 FakeUpConnectedPPP(); 1672 ExpectDisconnectCapabilityUniversal(); 1673 device_->OnPPPDied(kPID, kExitStatus); 1674 VerifyPPPStopped(); 1675 1676 // |Cellular::ppp_task_| is destroyed on the task loop. Must dispatch once to 1677 // cleanup. 1678 dispatcher_.DispatchPendingEvents(); 1679} 1680 1681TEST_F(CellularTest, DropConnection) { 1682 device_->set_ipconfig(dhcp_config_); 1683 EXPECT_CALL(*dhcp_config_, ReleaseIP(_)); 1684 device_->DropConnection(); 1685 Mock::VerifyAndClearExpectations(dhcp_config_.get()); // verify before dtor 1686 EXPECT_FALSE(device_->ipconfig()); 1687} 1688 1689TEST_F(CellularTest, DropConnectionPPP) { 1690 scoped_refptr<MockPPPDevice> ppp_device( 1691 new MockPPPDevice(modem_info_.control_interface(), 1692 nullptr, nullptr, nullptr, "fake_ppp0", -1)); 1693 EXPECT_CALL(*ppp_device, DropConnection()); 1694 device_->ppp_device_ = ppp_device; 1695 device_->DropConnection(); 1696} 1697 1698TEST_F(CellularTest, ChangeServiceState) { 1699 MockCellularService* service(SetMockService()); 1700 EXPECT_CALL(*service, SetState(_)); 1701 EXPECT_CALL(*service, SetFailure(_)); 1702 EXPECT_CALL(*service, SetFailureSilent(_)); 1703 ON_CALL(*service, state()).WillByDefault(Return(Service::kStateUnknown)); 1704 1705 // Without PPP, these should be handled by our selected_service(). 1706 device_->SelectService(service); 1707 device_->SetServiceState(Service::kStateConfiguring); 1708 device_->SetServiceFailure(Service::kFailurePPPAuth); 1709 device_->SetServiceFailureSilent(Service::kFailureUnknown); 1710 Mock::VerifyAndClearExpectations(service); // before Cellular dtor 1711} 1712 1713TEST_F(CellularTest, ChangeServiceStatePPP) { 1714 MockCellularService* service(SetMockService()); 1715 scoped_refptr<MockPPPDevice> ppp_device( 1716 new MockPPPDevice(modem_info_.control_interface(), 1717 nullptr, nullptr, nullptr, "fake_ppp0", -1)); 1718 EXPECT_CALL(*ppp_device, SetServiceState(_)); 1719 EXPECT_CALL(*ppp_device, SetServiceFailure(_)); 1720 EXPECT_CALL(*ppp_device, SetServiceFailureSilent(_)); 1721 EXPECT_CALL(*service, SetState(_)).Times(0); 1722 EXPECT_CALL(*service, SetFailure(_)).Times(0); 1723 EXPECT_CALL(*service, SetFailureSilent(_)).Times(0); 1724 device_->ppp_device_ = ppp_device; 1725 1726 // With PPP, these should all be punted over to the |ppp_device|. 1727 // Note in particular that Cellular does not manipulate |service| in 1728 // this case. 1729 device_->SetServiceState(Service::kStateConfiguring); 1730 device_->SetServiceFailure(Service::kFailurePPPAuth); 1731 device_->SetServiceFailureSilent(Service::kFailureUnknown); 1732} 1733 1734TEST_F(CellularTest, StopPPPOnDisconnect) { 1735 const int kPID = 123; 1736 Error error; 1737 StartPPP(kPID); 1738 FakeUpConnectedPPP(); 1739 ExpectPPPStopped(); 1740 device_->Disconnect(&error, "in test"); 1741 VerifyPPPStopped(); 1742} 1743 1744TEST_F(CellularTest, StopPPPOnSuspend) { 1745 const int kPID = 123; 1746 StartPPP(kPID); 1747 FakeUpConnectedPPP(); 1748 ExpectPPPStopped(); 1749 device_->OnBeforeSuspend(ResultCallback()); 1750 VerifyPPPStopped(); 1751} 1752 1753TEST_F(CellularTest, OnAfterResumeDisabledWantDisabled) { 1754 // The Device was disabled prior to resume, and the profile settings 1755 // indicate that the device should be disabled. We should leave 1756 // things alone. 1757 1758 // Initial state. 1759 mm1::MockModemProxy* mm1_proxy = SetupOnAfterResume(); 1760 set_enabled_persistent(false); 1761 EXPECT_FALSE(device_->running()); 1762 EXPECT_FALSE(device_->enabled_persistent()); 1763 EXPECT_EQ(Cellular::kStateDisabled, device_->state_); 1764 1765 // Resume, while device is disabled. 1766 EXPECT_CALL(*mm1_proxy, Enable(_, _, _, _)).Times(0); 1767 device_->OnAfterResume(); 1768 EXPECT_FALSE(device_->running()); 1769 EXPECT_FALSE(device_->enabled_persistent()); 1770 EXPECT_EQ(Cellular::kStateDisabled, device_->state_); 1771} 1772 1773TEST_F(CellularTest, OnAfterResumeDisableInProgressWantDisabled) { 1774 // The Device was not disabled prior to resume, but the profile 1775 // settings indicate that the device _should be_ disabled. Most 1776 // likely, we started disabling the device, but that did not 1777 // complete before we suspended. We should leave things alone. 1778 1779 // Initial state. 1780 mm1::MockModemProxy* mm1_proxy = SetupOnAfterResume(); 1781 Error error; 1782 EXPECT_CALL(*mm1_proxy, Enable(true, _, _, _)) 1783 .WillOnce(Invoke(this, &CellularTest::InvokeEnable)); 1784 device_->SetEnabled(true); 1785 EXPECT_TRUE(device_->running()); 1786 EXPECT_EQ(Cellular::kStateEnabled, device_->state_); 1787 1788 // Start disable. 1789 EXPECT_CALL(*modem_info_.mock_manager(), UpdateDevice(_)); 1790 device_->SetEnabledPersistent(false, &error, ResultCallback()); 1791 EXPECT_FALSE(device_->running()); // changes immediately 1792 EXPECT_FALSE(device_->enabled_persistent()); // changes immediately 1793 EXPECT_EQ(Cellular::kStateEnabled, device_->state_); // changes on completion 1794 1795 // Resume, with disable still in progress. 1796 device_->OnAfterResume(); 1797 EXPECT_FALSE(device_->running()); 1798 EXPECT_FALSE(device_->enabled_persistent()); 1799 EXPECT_EQ(Cellular::kStateEnabled, device_->state_); 1800 1801 // Finish the disable operation. 1802 EXPECT_CALL(*mm1_proxy, Enable(false, _, _, _)) 1803 .WillOnce(Invoke(this, &CellularTest::InvokeEnable)); 1804 EXPECT_CALL(*mm1_proxy, SetPowerState(_, _, _, _)) 1805 .WillOnce(Invoke(this, &CellularTest::InvokeSetPowerState)); 1806 dispatcher_.DispatchPendingEvents(); 1807 EXPECT_FALSE(device_->running()); 1808 EXPECT_FALSE(device_->enabled_persistent()); 1809 EXPECT_EQ(Cellular::kStateDisabled, device_->state_); 1810} 1811 1812TEST_F(CellularTest, OnAfterResumeDisableQueuedWantEnabled) { 1813 // The Device was not disabled prior to resume, and the profile 1814 // settings indicate that the device should be enabled. In 1815 // particular, we went into suspend before we actually processed the 1816 // task queued by CellularCapabilityUniversal::StopModem. 1817 // 1818 // This is unlikely, and a case where we fail to do the right thing. 1819 // The tests exists to document this corner case, which we get wrong. 1820 1821 // Initial state. 1822 mm1::MockModemProxy* mm1_proxy = SetupOnAfterResume(); 1823 EXPECT_CALL(*mm1_proxy, Enable(true, _, _, _)) 1824 .WillOnce(Invoke(this, &CellularTest::InvokeEnable)); 1825 device_->SetEnabled(true); 1826 EXPECT_TRUE(device_->running()); 1827 EXPECT_TRUE(device_->enabled_persistent()); 1828 EXPECT_EQ(Cellular::kStateEnabled, device_->state_); 1829 1830 // Start disable. 1831 device_->SetEnabled(false); 1832 EXPECT_FALSE(device_->running()); // changes immediately 1833 EXPECT_TRUE(device_->enabled_persistent()); // no change 1834 EXPECT_EQ(Cellular::kStateEnabled, device_->state_); // changes on completion 1835 1836 // Refresh proxies, since CellularCapabilityUniversal::StartModem wants 1837 // new proxies. Also, stash away references for later. 1838 PopulateProxies(); 1839 SetCommonOnAfterResumeExpectations(); 1840 mm1_proxy = mm1_proxy_.get(); 1841 auto dbus_properties_proxy = dbus_properties_proxy_.get(); 1842 1843 // Resume, with disable still in progress. 1844 EXPECT_CALL(*mm1_proxy, Enable(true, _, _, _)) 1845 .WillOnce(Invoke(this, &CellularTest::InvokeEnableReturningWrongState)); 1846 EXPECT_EQ(Cellular::kStateEnabled, device_->state_); // disable still pending 1847 device_->OnAfterResume(); 1848 EXPECT_TRUE(device_->running()); // changes immediately 1849 EXPECT_TRUE(device_->enabled_persistent()); // no change 1850 EXPECT_EQ(Cellular::kStateDisabled, device_->state_); // by OnAfterResume 1851 1852 // Set up state that we need. 1853 KeyValueStore modem_properties; 1854 modem_properties.SetInt(MM_MODEM_PROPERTY_STATE, 1855 Cellular::kModemStateDisabled); 1856 1857 // Let the disable complete. 1858 EXPECT_CALL(*mm1_proxy, Enable(false, _, _, _)) 1859 .WillOnce(Invoke(this, &CellularTest::InvokeEnable)); 1860 EXPECT_CALL(*mm1_proxy, SetPowerState(_, _, _, _)) 1861 .WillOnce(Invoke(this, &CellularTest::InvokeSetPowerState)); 1862 EXPECT_CALL(*dbus_properties_proxy, GetAll(_)) 1863 .WillRepeatedly(Return(modem_properties)); 1864 dispatcher_.DispatchPendingEvents(); 1865 EXPECT_TRUE(device_->running()); // last changed by OnAfterResume 1866 EXPECT_TRUE(device_->enabled_persistent()); // last changed by OnAfterResume 1867 EXPECT_EQ(Cellular::kStateDisabled, device_->state_); 1868 1869 // There's nothing queued up to restart the modem. Even though we 1870 // want to be running, we're stuck in the disabled state. 1871 dispatcher_.DispatchPendingEvents(); 1872 EXPECT_TRUE(device_->running()); 1873 EXPECT_TRUE(device_->enabled_persistent()); 1874 EXPECT_EQ(Cellular::kStateDisabled, device_->state_); 1875} 1876 1877TEST_F(CellularTest, OnAfterResumePowerDownInProgressWantEnabled) { 1878 // The Device was not fully disabled prior to resume, and the 1879 // profile settings indicate that the device should be enabled. In 1880 // this case, we have disabled the device, but are waiting for the 1881 // power-down (switch to low power) to complete. 1882 // 1883 // This test emulates the behavior of the Huawei E303 dongle, when 1884 // Manager::kTerminationActionsTimeoutMilliseconds is 9500 1885 // msec. (The dongle takes 10-11 seconds to go through the whole 1886 // disable, power-down sequence). 1887 // 1888 // Eventually, the power-down would complete, and the device would 1889 // be stuck in the disabled state. To counter-act that, 1890 // OnAfterResume tries to enable the device now, even though the 1891 // device is currently enabled. 1892 1893 // Initial state. 1894 mm1::MockModemProxy* mm1_proxy = SetupOnAfterResume(); 1895 EXPECT_CALL(*mm1_proxy, Enable(true, _, _, _)) 1896 .WillOnce(Invoke(this, &CellularTest::InvokeEnable)); 1897 device_->SetEnabled(true); 1898 EXPECT_TRUE(device_->running()); 1899 EXPECT_TRUE(device_->enabled_persistent()); 1900 EXPECT_EQ(Cellular::kStateEnabled, device_->state_); 1901 1902 // Start disable. 1903 ResultCallback modem_proxy_enable_callback; 1904 EXPECT_CALL(*mm1_proxy, Enable(false, _, _, _)) 1905 .WillOnce(SaveArg<2>(&modem_proxy_enable_callback)); 1906 device_->SetEnabled(false); 1907 dispatcher_.DispatchPendingEvents(); // SetEnabled yields a deferred task 1908 EXPECT_FALSE(device_->running()); // changes immediately 1909 EXPECT_TRUE(device_->enabled_persistent()); // no change 1910 EXPECT_EQ(Cellular::kStateEnabled, device_->state_); // changes on completion 1911 1912 // Let the disable complete. That will trigger power-down. 1913 // 1914 // Note that, unlike for mm1_proxy->Enable, we don't save the 1915 // callback for mm1_proxy->SetPowerState. We expect the callback not 1916 // to be executed, as explained in the comment about having a fresh 1917 // proxy OnAfterResume, below. 1918 Error error; 1919 ASSERT_TRUE(error.IsSuccess()); 1920 EXPECT_CALL(*mm1_proxy, SetPowerState(MM_MODEM_POWER_STATE_LOW, _, _, _)) 1921 .WillOnce(SetErrorTypeInArgument<1>(Error::kOperationInitiated)); 1922 modem_proxy_enable_callback.Run(error); 1923 1924 // No response to power-down yet. It probably completed while the host 1925 // was asleep, and so the reply from the modem was lost. 1926 1927 // Refresh proxies, since CellularCapabilityUniversal::StartModem wants 1928 // new proxies. Also, stash away references for later. 1929 PopulateProxies(); 1930 SetCommonOnAfterResumeExpectations(); 1931 auto new_mm1_proxy = mm1_proxy_.get(); 1932 auto dbus_properties_proxy = dbus_properties_proxy_.get(); 1933 1934 // Resume. 1935 ResultCallback new_callback; 1936 EXPECT_EQ(Cellular::kStateEnabled, device_->state_); // disable still pending 1937 EXPECT_CALL(*new_mm1_proxy, Enable(true, _, _, _)) 1938 .WillOnce(SaveArg<2>(&modem_proxy_enable_callback)); 1939 device_->OnAfterResume(); 1940 EXPECT_TRUE(device_->running()); // changes immediately 1941 EXPECT_TRUE(device_->enabled_persistent()); // no change 1942 EXPECT_EQ(Cellular::kStateDisabled, device_->state_); // by OnAfterResume 1943 1944 // We should have a fresh proxy OnAfterResume. Otherwise, we may get 1945 // confused when the SetPowerState call completes (either naturally, 1946 // or via a time-out from dbus-c++). 1947 // 1948 // The pointers must differ, because the new proxy is constructed 1949 // before the old one is destructed. 1950 EXPECT_FALSE(new_mm1_proxy == mm1_proxy); 1951 1952 // Set up state that we need. 1953 KeyValueStore modem_properties; 1954 modem_properties.SetInt(MM_MODEM_PROPERTY_STATE, 1955 Cellular::kModemStateEnabled); 1956 1957 // Let the enable complete. 1958 ASSERT_TRUE(error.IsSuccess()); 1959 EXPECT_CALL(*dbus_properties_proxy, GetAll(_)) 1960 .WillRepeatedly(Return(modem_properties)); 1961 ASSERT_TRUE(!modem_proxy_enable_callback.is_null()); 1962 modem_proxy_enable_callback.Run(error); 1963 EXPECT_TRUE(device_->running()); 1964 EXPECT_TRUE(device_->enabled_persistent()); 1965 EXPECT_EQ(Cellular::kStateEnabled, device_->state_); 1966} 1967 1968TEST_F(CellularTest, OnAfterResumeDisabledWantEnabled) { 1969 // This is the ideal case. The disable process completed before 1970 // going into suspend. 1971 mm1::MockModemProxy* mm1_proxy = SetupOnAfterResume(); 1972 EXPECT_FALSE(device_->running()); 1973 EXPECT_TRUE(device_->enabled_persistent()); 1974 EXPECT_EQ(Cellular::kStateDisabled, device_->state_); 1975 1976 // Resume. 1977 ResultCallback modem_proxy_enable_callback; 1978 EXPECT_CALL(*mm1_proxy, Enable(true, _, _, _)) 1979 .WillOnce(SaveArg<2>(&modem_proxy_enable_callback)); 1980 device_->OnAfterResume(); 1981 1982 // Complete enable. 1983 Error error; 1984 ASSERT_TRUE(error.IsSuccess()); 1985 modem_proxy_enable_callback.Run(error); 1986 EXPECT_TRUE(device_->running()); 1987 EXPECT_TRUE(device_->enabled_persistent()); 1988 EXPECT_EQ(Cellular::kStateEnabled, device_->state_); 1989} 1990 1991// Custom property setters should return false, and make no changes, if 1992// the new value is the same as the old value. 1993TEST_F(CellularTest, CustomSetterNoopChange) { 1994 Error error; 1995 EXPECT_FALSE(device_->allow_roaming_); 1996 EXPECT_FALSE(device_->SetAllowRoaming(false, &error)); 1997 EXPECT_TRUE(error.IsSuccess()); 1998} 1999 2000TEST_F(CellularTest, ScanImmediateFailure) { 2001 Error error; 2002 2003 device_->set_found_networks(kTestNetworksCellular); 2004 EXPECT_FALSE(device_->scanning_); 2005 // |InitProxies| must be called before calling any functions on the 2006 // Capability*, to set up the modem proxies. 2007 // Warning: The test loses all references to the proxies when |InitProxies| is 2008 // called. 2009 GetCapabilityGSM()->InitProxies(); 2010 device_->Scan(Device::kFullScan, &error, ""); 2011 EXPECT_TRUE(error.IsFailure()); 2012 EXPECT_FALSE(device_->scanning_); 2013 EXPECT_EQ(kTestNetworksCellular, device_->found_networks()); 2014} 2015 2016TEST_F(CellularTest, ScanAsynchronousFailure) { 2017 Error error; 2018 ScanResultsCallback results_callback; 2019 2020 device_->set_found_networks(kTestNetworksCellular); 2021 EXPECT_CALL(*gsm_network_proxy_, Scan(&error, _, _)) 2022 .WillOnce(DoAll(SetErrorTypeInArgument<0>(Error::kOperationInitiated), 2023 SaveArg<1>(&results_callback))); 2024 EXPECT_FALSE(device_->scanning_); 2025 // |InitProxies| must be called before calling any functions on the 2026 // Capability*, to set up the modem proxies. 2027 // Warning: The test loses all references to the proxies when |InitProxies| is 2028 // called. 2029 GetCapabilityGSM()->InitProxies(); 2030 device_->Scan(Device::kFullScan, &error, ""); 2031 EXPECT_TRUE(error.IsOngoing()); 2032 EXPECT_TRUE(device_->scanning_); 2033 2034 // Asynchronously fail the scan. 2035 error.Populate(Error::kOperationFailed); 2036 results_callback.Run(kTestNetworksGSM, error); 2037 EXPECT_FALSE(device_->scanning_); 2038 EXPECT_TRUE(device_->found_networks().empty()); 2039} 2040 2041TEST_F(CellularTest, ScanSuccess) { 2042 Error error; 2043 ScanResultsCallback results_callback; 2044 2045 device_->clear_found_networks(); 2046 EXPECT_CALL(*gsm_network_proxy_, Scan(&error, _, _)) 2047 .WillOnce(DoAll(SetErrorTypeInArgument<0>(Error::kOperationInitiated), 2048 SaveArg<1>(&results_callback))); 2049 EXPECT_FALSE(device_->scanning_); 2050 // |InitProxies| must be called before calling any functions on the 2051 // Capability*, to set up the modem proxies. 2052 // Warning: The test loses all references to the proxies when |InitProxies| is 2053 // called. 2054 GetCapabilityGSM()->InitProxies(); 2055 device_->Scan(Device::kFullScan, &error, ""); 2056 EXPECT_TRUE(error.IsOngoing()); 2057 EXPECT_TRUE(device_->scanning_); 2058 2059 // Successfully complete the scan. 2060 const GSMScanResults gsm_results{}; 2061 error.Populate(Error::kSuccess); 2062 results_callback.Run(kTestNetworksGSM, error); 2063 EXPECT_FALSE(device_->scanning_); 2064 EXPECT_EQ(kTestNetworksCellular, device_->found_networks()); 2065} 2066 2067TEST_F(CellularTest, EstablishLinkDHCP) { 2068 unique_ptr<CellularBearer> bearer( 2069 new CellularBearer(&control_interface_, "", "")); 2070 bearer->set_ipv4_config_method(IPConfig::kMethodDHCP); 2071 SetCapabilityUniversalActiveBearer(std::move(bearer)); 2072 device_->state_ = Cellular::kStateConnected; 2073 2074 MockCellularService* service = SetMockService(); 2075 ON_CALL(*service, state()).WillByDefault(Return(Service::kStateUnknown)); 2076 2077 EXPECT_CALL(device_info_, GetFlags(device_->interface_index(), _)) 2078 .WillOnce(DoAll(SetArgumentPointee<1>(IFF_UP), Return(true))); 2079 EXPECT_CALL(dhcp_provider_, CreateIPv4Config(kTestDeviceName, _, _, _)) 2080 .WillOnce(Return(dhcp_config_)); 2081 EXPECT_CALL(*dhcp_config_, RequestIP()).WillOnce(Return(true)); 2082 EXPECT_CALL(*service, SetState(Service::kStateConfiguring)); 2083 device_->EstablishLink(); 2084 EXPECT_EQ(service, device_->selected_service()); 2085 Mock::VerifyAndClearExpectations(service); // before Cellular dtor 2086} 2087 2088TEST_F(CellularTest, EstablishLinkPPP) { 2089 unique_ptr<CellularBearer> bearer( 2090 new CellularBearer(&control_interface_, "", "")); 2091 bearer->set_ipv4_config_method(IPConfig::kMethodPPP); 2092 SetCapabilityUniversalActiveBearer(std::move(bearer)); 2093 device_->state_ = Cellular::kStateConnected; 2094 2095 const int kPID = 123; 2096 EXPECT_CALL(process_manager_, StartProcess(_, _, _, _, _, _)) 2097 .WillOnce(Return(kPID)); 2098 device_->EstablishLink(); 2099 EXPECT_FALSE(device_->ipconfig()); // No DHCP client. 2100 EXPECT_FALSE(device_->selected_service()); 2101 EXPECT_FALSE(device_->is_ppp_authenticating_); 2102 EXPECT_NE(nullptr, device_->ppp_task_); 2103} 2104 2105TEST_F(CellularTest, EstablishLinkStatic) { 2106 IPAddress::Family kAddressFamily = IPAddress::kFamilyIPv4; 2107 const char kAddress[] = "10.0.0.1"; 2108 const char kGateway[] = "10.0.0.254"; 2109 const int32_t kSubnetPrefix = 16; 2110 const char* const kDNS[] = {"10.0.0.2", "8.8.4.4", "8.8.8.8"}; 2111 2112 unique_ptr<IPConfig::Properties> ipconfig_properties( 2113 new IPConfig::Properties); 2114 ipconfig_properties->address_family = kAddressFamily; 2115 ipconfig_properties->address = kAddress; 2116 ipconfig_properties->gateway = kGateway; 2117 ipconfig_properties->subnet_prefix = kSubnetPrefix; 2118 ipconfig_properties->dns_servers = vector<string>{kDNS[0], kDNS[1], kDNS[2]}; 2119 2120 unique_ptr<CellularBearer> bearer( 2121 new CellularBearer(&control_interface_, "", "")); 2122 bearer->set_ipv4_config_method(IPConfig::kMethodStatic); 2123 bearer->set_ipv4_config_properties(std::move(ipconfig_properties)); 2124 SetCapabilityUniversalActiveBearer(std::move(bearer)); 2125 device_->state_ = Cellular::kStateConnected; 2126 2127 MockCellularService* service = SetMockService(); 2128 ON_CALL(*service, state()).WillByDefault(Return(Service::kStateUnknown)); 2129 2130 EXPECT_CALL(device_info_, GetFlags(device_->interface_index(), _)) 2131 .WillOnce(DoAll(SetArgumentPointee<1>(IFF_UP), Return(true))); 2132 EXPECT_CALL(*service, SetState(Service::kStateConfiguring)); 2133 device_->EstablishLink(); 2134 EXPECT_EQ(service, device_->selected_service()); 2135 ASSERT_TRUE(device_->ipconfig()); 2136 EXPECT_EQ(kAddressFamily, device_->ipconfig()->properties().address_family); 2137 EXPECT_EQ(kAddress, device_->ipconfig()->properties().address); 2138 EXPECT_EQ(kGateway, device_->ipconfig()->properties().gateway); 2139 EXPECT_EQ(kSubnetPrefix, device_->ipconfig()->properties().subnet_prefix); 2140 ASSERT_EQ(3, device_->ipconfig()->properties().dns_servers.size()); 2141 EXPECT_EQ(kDNS[0], device_->ipconfig()->properties().dns_servers[0]); 2142 EXPECT_EQ(kDNS[1], device_->ipconfig()->properties().dns_servers[1]); 2143 EXPECT_EQ(kDNS[2], device_->ipconfig()->properties().dns_servers[2]); 2144 Mock::VerifyAndClearExpectations(service); // before Cellular dtor 2145} 2146 2147} // namespace shill 2148