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