blob: 19c88dd8d81f4b1b62a4cde46012af16a0dd7924 [file] [log] [blame]
Arman Ugurayf4c61812013-01-10 18:58:39 -08001// Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
Chris Masoneb925cc82011-06-22 15:39:57 -07002// 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.h"
6
Darin Petkov0828f5f2011-08-11 10:18:52 -07007#include <sys/socket.h>
8#include <linux/if.h>
9#include <linux/netlink.h> // Needs typedefs from sys/socket.h.
10
Eric Shienbrood9a245532012-03-07 14:20:39 -050011#include <base/bind.h>
Chris Masoneb925cc82011-06-22 15:39:57 -070012#include <chromeos/dbus/service_constants.h>
Chris Masoneb925cc82011-06-22 15:39:57 -070013
Ben Chan539ab022014-02-03 16:34:57 -080014#include "shill/cellular_bearer.h"
Darin Petkovae0c64e2011-11-15 15:50:27 +010015#include "shill/cellular_capability_cdma.h"
Jason Glasgow82f9ab32012-04-04 14:27:19 -040016#include "shill/cellular_capability_classic.h"
Darin Petkov184c54e2011-11-15 12:44:39 +010017#include "shill/cellular_capability_gsm.h"
Ben Chan09fa2a02012-11-07 22:09:09 -080018#include "shill/cellular_capability_universal.h"
Darin Petkovd9661952011-08-03 16:25:42 -070019#include "shill/cellular_service.h"
Darin Petkov4d6d9412011-08-24 13:19:54 -070020#include "shill/error.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070021#include "shill/event_dispatcher.h"
mukesh agrawal28185512013-10-18 16:57:09 -070022#include "shill/mock_adaptors.h"
Thieu Le37b90032012-05-15 15:18:41 -070023#include "shill/mock_cellular_service.h"
mukesh agrawal28185512013-10-18 16:57:09 -070024#include "shill/mock_dbus_properties_proxy.h"
Chris Masone2ae797d2011-08-23 20:41:00 -070025#include "shill/mock_device_info.h"
Darin Petkov77cb6812011-08-15 16:19:41 -070026#include "shill/mock_dhcp_config.h"
27#include "shill/mock_dhcp_provider.h"
mukesh agrawalfbc40d22013-06-28 00:25:13 -070028#include "shill/mock_external_task.h"
mukesh agrawal28185512013-10-18 16:57:09 -070029#include "shill/mock_mm1_modem_modem3gpp_proxy.h"
30#include "shill/mock_mm1_modem_proxy.h"
mukesh agrawalf407d592013-07-31 11:37:57 -070031#include "shill/mock_mm1_modem_simple_proxy.h"
Prathmesh Prabhu95afcbb2014-04-08 16:05:42 -070032#include "shill/mock_mobile_operator_info.h"
Darin Petkovbec79a22011-08-01 14:47:17 -070033#include "shill/mock_modem_cdma_proxy.h"
Darin Petkov975b5e72011-08-30 11:48:08 -070034#include "shill/mock_modem_gsm_card_proxy.h"
Darin Petkova1e0a1c2011-08-25 15:08:33 -070035#include "shill/mock_modem_gsm_network_proxy.h"
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070036#include "shill/mock_modem_info.h"
Darin Petkove9d12e02011-07-27 15:09:37 -070037#include "shill/mock_modem_proxy.h"
Darin Petkove604f702011-07-28 15:51:17 -070038#include "shill/mock_modem_simple_proxy.h"
mukesh agrawal5d851b12013-07-11 14:09:41 -070039#include "shill/mock_ppp_device.h"
mukesh agrawalf407d592013-07-31 11:37:57 -070040#include "shill/mock_ppp_device_factory.h"
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070041#include "shill/mock_rtnl_handler.h"
Chris Masoneb925cc82011-06-22 15:39:57 -070042#include "shill/property_store_unittest.h"
Darin Petkove9d12e02011-07-27 15:09:37 -070043#include "shill/proxy_factory.h"
mukesh agrawalfbc40d22013-06-28 00:25:13 -070044#include "shill/rpc_task.h" // for RpcTaskDelegate
Ben Chanbe277dd2014-02-05 17:26:47 -080045#include "shill/testing.h"
Chris Masoneb925cc82011-06-22 15:39:57 -070046
Ben Chan09fa2a02012-11-07 22:09:09 -080047// 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
Eric Shienbrood9a245532012-03-07 14:20:39 -050051using base::Bind;
52using base::Unretained;
Darin Petkovc0865312011-09-16 15:31:20 -070053using std::map;
Chris Masoneb925cc82011-06-22 15:39:57 -070054using std::string;
Eric Shienbrood7fce52c2012-04-13 19:11:02 -040055using std::vector;
Darin Petkovbec79a22011-08-01 14:47:17 -070056using testing::_;
Chris Masoneb9c00592011-10-06 13:10:39 -070057using testing::AnyNumber;
mukesh agrawalf407d592013-07-31 11:37:57 -070058using testing::DoAll;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050059using testing::Invoke;
Arman Ugurayf84a4242013-04-09 20:01:07 -070060using testing::Mock;
Darin Petkov0828f5f2011-08-11 10:18:52 -070061using testing::NiceMock;
Darin Petkove604f702011-07-28 15:51:17 -070062using testing::Return;
Prathmesh Prabhu95afcbb2014-04-08 16:05:42 -070063using testing::ReturnRef;
mukesh agrawal28185512013-10-18 16:57:09 -070064using testing::SaveArg;
Darin Petkovbec79a22011-08-01 14:47:17 -070065using testing::SetArgumentPointee;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050066using testing::Unused;
Chris Masoneb925cc82011-06-22 15:39:57 -070067
68namespace shill {
69
Darin Petkov0828f5f2011-08-11 10:18:52 -070070class CellularPropertyTest : public PropertyStoreTest {
71 public:
72 CellularPropertyTest()
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070073 : modem_info_(control_interface(),
74 dispatcher(),
75 metrics(),
76 manager(),
77 glib()),
78 device_(new Cellular(&modem_info_,
Darin Petkove9d12e02011-07-27 15:09:37 -070079 "usb0",
Chris Masone626719f2011-08-18 16:58:48 -070080 "00:01:02:03:04:05",
Darin Petkove9d12e02011-07-27 15:09:37 -070081 3,
Ben Chan3ecdf822012-08-06 12:29:23 -070082 Cellular::kTypeCDMA,
Darin Petkov0828f5f2011-08-11 10:18:52 -070083 "",
Darin Petkov137884a2011-10-26 18:52:47 +020084 "",
Jason Glasgowa585fc32012-06-06 11:04:09 -040085 "",
Ben Chan3ecdf822012-08-06 12:29:23 -070086 ProxyFactory::GetInstance())) {}
Darin Petkov0828f5f2011-08-11 10:18:52 -070087 virtual ~CellularPropertyTest() {}
Darin Petkove9d12e02011-07-27 15:09:37 -070088
Chris Masoneb925cc82011-06-22 15:39:57 -070089 protected:
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070090 MockModemInfo modem_info_;
Darin Petkov0828f5f2011-08-11 10:18:52 -070091 DeviceRefPtr device_;
Chris Masoneb925cc82011-06-22 15:39:57 -070092};
93
Darin Petkov0828f5f2011-08-11 10:18:52 -070094TEST_F(CellularPropertyTest, Contains) {
Ben Chan7ea768e2013-09-20 15:08:40 -070095 EXPECT_TRUE(device_->store().Contains(kNameProperty));
mukesh agrawalde29fa82011-09-16 16:16:36 -070096 EXPECT_FALSE(device_->store().Contains(""));
Chris Masoneb925cc82011-06-22 15:39:57 -070097}
98
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -080099TEST_F(CellularPropertyTest, SetProperty) {
Chris Masonea8a2c252011-06-27 22:16:30 -0700100 {
101 ::DBus::Error error;
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700102 ::DBus::Variant allow_roaming;
103 allow_roaming.writer().append_bool(true);
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800104 EXPECT_TRUE(DBusAdaptor::SetProperty(
mukesh agrawalde29fa82011-09-16 16:16:36 -0700105 device_->mutable_store(),
Ben Chan7ea768e2013-09-20 15:08:40 -0700106 kCellularAllowRoamingProperty,
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700107 allow_roaming,
Chris Masonea8a2c252011-06-27 22:16:30 -0700108 &error));
109 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700110 // Ensure that attempting to write a R/O property returns InvalidArgs error.
111 {
112 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800113 EXPECT_FALSE(DBusAdaptor::SetProperty(device_->mutable_store(),
Ben Chan7ea768e2013-09-20 15:08:40 -0700114 kAddressProperty,
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800115 PropertyStoreTest::kStringV,
116 &error));
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700117 ASSERT_TRUE(error.is_set()); // name() may be invalid otherwise
Chris Masone9d779932011-08-25 16:33:41 -0700118 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -0700119 }
120 {
121 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800122 EXPECT_FALSE(DBusAdaptor::SetProperty(device_->mutable_store(),
Ben Chan7ea768e2013-09-20 15:08:40 -0700123 kCarrierProperty,
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800124 PropertyStoreTest::kStringV,
125 &error));
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700126 ASSERT_TRUE(error.is_set()); // name() may be invalid otherwise
Chris Masone9d779932011-08-25 16:33:41 -0700127 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -0700128 }
Chris Masoneb925cc82011-06-22 15:39:57 -0700129}
130
Darin Petkov0828f5f2011-08-11 10:18:52 -0700131class CellularTest : public testing::Test {
132 public:
133 CellularTest()
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700134 : modem_info_(NULL, &dispatcher_, NULL, NULL, NULL),
135 device_info_(modem_info_.control_interface(), &dispatcher_,
136 modem_info_.metrics(), modem_info_.manager()),
137 dhcp_config_(new MockDHCPConfig(modem_info_.control_interface(),
Paul Stewartd408fdf2012-05-07 17:15:57 -0700138 kTestDeviceName)),
Ben Chan3ecdf822012-08-06 12:29:23 -0700139 create_gsm_card_proxy_from_factory_(false),
Ben Chan3ecdf822012-08-06 12:29:23 -0700140 proxy_factory_(this),
Prathmesh Prabhu95afcbb2014-04-08 16:05:42 -0700141 mock_home_provider_info_(NULL),
142 mock_serving_operator_info_(NULL),
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700143 device_(new Cellular(&modem_info_,
Darin Petkov77cb6812011-08-15 16:19:41 -0700144 kTestDeviceName,
Chris Masone626719f2011-08-18 16:58:48 -0700145 kTestDeviceAddress,
Darin Petkov0828f5f2011-08-11 10:18:52 -0700146 3,
147 Cellular::kTypeGSM,
148 kDBusOwner,
Jason Glasgowa585fc32012-06-06 11:04:09 -0400149 kDBusService,
Darin Petkov137884a2011-10-26 18:52:47 +0200150 kDBusPath,
Thieu Lece4483e2013-01-23 15:12:03 -0800151 &proxy_factory_)) {
mukesh agrawal28185512013-10-18 16:57:09 -0700152 PopulateProxies();
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700153 modem_info_.metrics()->RegisterDevice(device_->interface_index(),
154 Technology::kCellular);
Darin Petkov137884a2011-10-26 18:52:47 +0200155 }
Darin Petkov0828f5f2011-08-11 10:18:52 -0700156
157 virtual void SetUp() {
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700158 static_cast<Device *>(device_)->rtnl_handler_ = &rtnl_handler_;
Darin Petkov77cb6812011-08-15 16:19:41 -0700159 device_->set_dhcp_provider(&dhcp_provider_);
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700160 EXPECT_CALL(*modem_info_.mock_manager(), device_info())
161 .WillRepeatedly(Return(&device_info_));
162 EXPECT_CALL(*modem_info_.mock_manager(), DeregisterService(_))
163 .Times(AnyNumber());
Darin Petkov0828f5f2011-08-11 10:18:52 -0700164 }
165
166 virtual void TearDown() {
Eric Shienbrood9a245532012-03-07 14:20:39 -0500167 device_->DestroyIPConfig();
Darin Petkovfb0625e2012-01-16 13:05:56 +0100168 device_->state_ = Cellular::kStateDisabled;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500169 device_->capability_->ReleaseProxies();
Darin Petkov77cb6812011-08-15 16:19:41 -0700170 device_->set_dhcp_provider(NULL);
mukesh agrawal0381f9a2013-07-11 16:41:52 -0700171 // Break cycle between Cellular and CellularService.
172 device_->service_ = NULL;
173 device_->SelectService(NULL);
Darin Petkov0828f5f2011-08-11 10:18:52 -0700174 }
175
mukesh agrawal28185512013-10-18 16:57:09 -0700176 void PopulateProxies() {
177 dbus_properties_proxy_.reset(new MockDBusPropertiesProxy());
178 proxy_.reset(new MockModemProxy());
179 simple_proxy_.reset(new MockModemSimpleProxy());
180 cdma_proxy_.reset(new MockModemCDMAProxy());
181 gsm_card_proxy_.reset(new MockModemGSMCardProxy());
182 gsm_network_proxy_.reset(new MockModemGSMNetworkProxy());
183 mm1_modem_3gpp_proxy_.reset(new mm1::MockModemModem3gppProxy());
184 mm1_proxy_.reset(new mm1::MockModemProxy());
185 mm1_simple_proxy_.reset(new mm1::MockModemSimpleProxy());
186 }
187
Prathmesh Prabhu95afcbb2014-04-08 16:05:42 -0700188 void SetMockMobileOperatorInfoObjects() {
189 mock_home_provider_info_ = new MockMobileOperatorInfo(&dispatcher_);
190 // Takes ownership.
191 device_->set_home_provider_info(mock_home_provider_info_);
192
193 mock_serving_operator_info_ = new MockMobileOperatorInfo(&dispatcher_);
194 // Takes ownership.
195 device_->set_serving_operator_info(mock_serving_operator_info_);
196 }
197
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700198 void InitProviderDB() {
199 modem_info_.SetProviderDB(kTestMobileProviderDBPath);
200 }
201
Eric Shienbrood9a245532012-03-07 14:20:39 -0500202 void InvokeEnable(bool enable, Error *error,
203 const ResultCallback &callback, int timeout) {
204 callback.Run(Error());
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500205 }
mukesh agrawal28185512013-10-18 16:57:09 -0700206 void InvokeEnableReturningWrongState(
207 bool enable, Error *error, const ResultCallback &callback, int timeout) {
208 callback.Run(Error(Error::kWrongState));
209 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500210 void InvokeGetSignalQuality(Error *error,
211 const SignalQualityCallback &callback,
212 int timeout) {
213 callback.Run(kStrength, Error());
214 }
215 void InvokeGetModemStatus(Error *error,
216 const DBusPropertyMapCallback &callback,
217 int timeout) {
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500218 DBusPropertiesMap props;
219 props["carrier"].writer().append_string(kTestCarrier);
220 props["unknown-property"].writer().append_string("irrelevant-value");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500221 callback.Run(props, Error());
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500222 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500223 void InvokeGetModemInfo(Error *error, const ModemInfoCallback &callback,
224 int timeout) {
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500225 static const char kManufacturer[] = "Company";
226 static const char kModelID[] = "Gobi 2000";
227 static const char kHWRev[] = "A00B1234";
228 ModemHardwareInfo info;
229 info._1 = kManufacturer;
230 info._2 = kModelID;
231 info._3 = kHWRev;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500232 callback.Run(info, Error());
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500233 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500234 void InvokeGetRegistrationState1X(Error *error,
235 const RegistrationStateCallback &callback,
236 int timeout) {
237 callback.Run(MM_MODEM_CDMA_REGISTRATION_STATE_HOME,
238 MM_MODEM_CDMA_REGISTRATION_STATE_UNKNOWN,
239 Error());
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500240 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500241 void InvokeGetIMEI(Error *error, const GSMIdentifierCallback &callback,
242 int timeout) {
243 callback.Run(kIMEI, Error());
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500244 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500245 void InvokeGetIMSI(Error *error, const GSMIdentifierCallback &callback,
246 int timeout) {
247 callback.Run(kIMSI, Error());
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500248 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500249 void InvokeGetMSISDN(Error *error, const GSMIdentifierCallback &callback,
250 int timeout) {
251 callback.Run(kMSISDN, Error());
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500252 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500253 void InvokeGetSPN(Error *error, const GSMIdentifierCallback &callback,
254 int timeout) {
Darin Petkova4ca3c32012-08-17 16:05:24 +0200255 callback.Run(kTestCarrierSPN, Error());
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500256 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500257 void InvokeGetRegistrationInfo(Error *error,
258 const RegistrationInfoCallback &callback,
259 int timeout) {
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500260 static const char kNetworkID[] = "22803";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500261 callback.Run(MM_MODEM_GSM_NETWORK_REG_STATUS_ROAMING,
262 kNetworkID, kTestCarrier, Error());
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500263 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500264 void InvokeRegister(const string &network_id,
265 Error *error,
266 const ResultCallback &callback,
267 int timeout) {
268 callback.Run(Error());
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500269 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500270 void InvokeGetRegistrationState(Error *error,
271 const RegistrationStateCallback &callback,
272 int timeout) {
273 callback.Run(MM_MODEM_CDMA_REGISTRATION_STATE_REGISTERED,
274 MM_MODEM_CDMA_REGISTRATION_STATE_HOME,
275 Error());
276 }
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400277 void InvokeGetRegistrationStateUnregistered(
278 Error *error,
279 const RegistrationStateCallback &callback,
280 int timeout) {
281 callback.Run(MM_MODEM_CDMA_REGISTRATION_STATE_UNKNOWN,
282 MM_MODEM_CDMA_REGISTRATION_STATE_UNKNOWN,
283 Error());
Arman Uguray539c4232012-09-11 10:00:22 -0700284 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500285 void InvokeConnect(DBusPropertiesMap props, Error *error,
286 const ResultCallback &callback, int timeout) {
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400287 EXPECT_EQ(Service::kStateAssociating, device_->service_->state());
Eric Shienbrood9a245532012-03-07 14:20:39 -0500288 callback.Run(Error());
289 }
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400290 void InvokeConnectFail(DBusPropertiesMap props, Error *error,
Thieu Leb5954a22012-05-18 10:37:34 -0700291 const ResultCallback &callback, int timeout) {
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400292 EXPECT_EQ(Service::kStateAssociating, device_->service_->state());
293 callback.Run(Error(Error::kNotOnHomeNetwork));
294 }
Thieu Leb5954a22012-05-18 10:37:34 -0700295 void InvokeConnectFailNoService(DBusPropertiesMap props, Error *error,
296 const ResultCallback &callback, int timeout) {
297 device_->service_ = NULL;
298 callback.Run(Error(Error::kNotOnHomeNetwork));
299 }
Thieu Led4974cd2013-05-23 10:39:28 -0700300 void InvokeConnectSuccessNoService(DBusPropertiesMap props, Error *error,
301 const ResultCallback &callback,
302 int timeout) {
303 device_->service_ = NULL;
304 callback.Run(Error());
305 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500306 void InvokeDisconnect(Error *error, const ResultCallback &callback,
307 int timeout) {
308 if (!callback.is_null())
309 callback.Run(Error());
310 }
Arman Uguray539c4232012-09-11 10:00:22 -0700311 void InvokeDisconnectFail(Error *error, const ResultCallback &callback,
312 int timeout) {
313 error->Populate(Error::kOperationFailed);
314 if (!callback.is_null())
315 callback.Run(*error);
316 }
mukesh agrawalf407d592013-07-31 11:37:57 -0700317 void InvokeDisconnectMM1(const ::DBus::Path &bearer, Error *error,
318 const ResultCallback &callback, int timeout) {
319 if (!callback.is_null())
320 callback.Run(Error());
321 }
Prathmesh Prabhu49ffffd2014-01-09 18:28:55 -0800322 void InvokeScanFailed(Error *error, Unused, Unused) {
323 error->Populate(Error::kOperationFailed);
324 }
325 void InvokeScanInitiated(Error *error, Unused, Unused) {
326 error->Populate(Error::kOperationInitiated);
327 }
mukesh agrawal28185512013-10-18 16:57:09 -0700328 void InvokeSetPowerState(const uint32_t &power_state,
329 Error *error,
330 const ResultCallback &callback,
331 int timeout) {
332 callback.Run(Error());
333 }
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400334 void ExpectCdmaStartModem(string network_technology) {
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400335 if (!device_->IsUnderlyingDeviceEnabled())
336 EXPECT_CALL(*proxy_,
337 Enable(true, _, _, CellularCapability::kTimeoutEnable))
338 .WillOnce(Invoke(this, &CellularTest::InvokeEnable));
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400339 EXPECT_CALL(*simple_proxy_,
340 GetModemStatus(_, _, CellularCapability::kTimeoutDefault))
341 .WillOnce(Invoke(this, &CellularTest::InvokeGetModemStatus));
342 EXPECT_CALL(*proxy_,
343 GetModemInfo(_, _, CellularCapability::kTimeoutDefault))
344 .WillOnce(Invoke(this, &CellularTest::InvokeGetModemInfo));
Ben Chan7ea768e2013-09-20 15:08:40 -0700345 if (network_technology == kNetworkTechnology1Xrtt)
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400346 EXPECT_CALL(*cdma_proxy_, GetRegistrationState(NULL, _, _))
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400347 .WillOnce(Invoke(this, &CellularTest::InvokeGetRegistrationState1X));
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400348 else
349 EXPECT_CALL(*cdma_proxy_, GetRegistrationState(NULL, _, _))
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400350 .WillOnce(Invoke(this, &CellularTest::InvokeGetRegistrationState));
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400351 EXPECT_CALL(*cdma_proxy_, GetSignalQuality(NULL, _, _))
352 .Times(2)
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400353 .WillRepeatedly(Invoke(this, &CellularTest::InvokeGetSignalQuality));
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400354 EXPECT_CALL(*this, TestCallback(IsSuccess()));
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700355 EXPECT_CALL(*modem_info_.mock_manager(), RegisterService(_));
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400356 }
357
mukesh agrawalee10f372013-08-14 14:07:11 -0700358 void ExpectDisconnectCapabilityUniversal() {
359 SetCellularType(Cellular::kTypeUniversal);
360 device_->state_ = Cellular::kStateConnected;
361 EXPECT_CALL(*mm1_simple_proxy_, Disconnect(_, _, _, _))
362 .WillOnce(Invoke(this, &CellularTest::InvokeDisconnectMM1));
363 GetCapabilityUniversal()->modem_simple_proxy_.reset(
364 mm1_simple_proxy_.release());
mukesh agrawalee10f372013-08-14 14:07:11 -0700365 }
366
367 void VerifyDisconnect() {
368 EXPECT_EQ(Cellular::kStateRegistered, device_->state_);
369 }
370
mukesh agrawalf407d592013-07-31 11:37:57 -0700371 void StartPPP(int pid) {
372 MockGLib &mock_glib(*dynamic_cast<MockGLib *>(modem_info_.glib()));
373 EXPECT_CALL(mock_glib, ChildWatchAdd(pid, _, _));
374 EXPECT_CALL(mock_glib, SpawnAsync(_, _, _, _, _, _, _, _))
375 .WillOnce(DoAll(SetArgumentPointee<6>(pid), Return(true)));
376 device_->StartPPP("fake_serial_device");
377 EXPECT_FALSE(device_->ipconfig()); // No DHCP client.
378 EXPECT_FALSE(device_->selected_service());
mukesh agrawalfc362912013-08-06 18:10:07 -0700379 EXPECT_FALSE(device_->is_ppp_authenticating_);
mukesh agrawalf407d592013-07-31 11:37:57 -0700380 EXPECT_TRUE(device_->ppp_task_);
381 Mock::VerifyAndClearExpectations(&mock_glib);
382 }
383
mukesh agrawal28185512013-10-18 16:57:09 -0700384 void FakeUpConnectedPPP() {
385 const char kInterfaceName[] = "fake-ppp-device";
386 const int kInterfaceIndex = -1;
387 auto mock_ppp_device = make_scoped_refptr(new MockPPPDevice(
388 modem_info_.control_interface(), NULL, NULL, NULL, kInterfaceName,
389 kInterfaceIndex));
390 device_->ppp_device_ = mock_ppp_device;
391 device_->state_ = Cellular::kStateConnected;
392 }
393
394 void ExpectPPPStopped() {
395 auto mock_ppp_device =
396 dynamic_cast<MockPPPDevice *>(device_->ppp_device_.get());
397 EXPECT_CALL(*mock_ppp_device, DropConnection());
398 }
399
400 void VerifyPPPStopped() {
401 EXPECT_FALSE(device_->ppp_task_);
402 EXPECT_FALSE(device_->ppp_device_);
403 }
404
405 void SetCommonOnAfterResumeExpectations() {
406 EXPECT_CALL(*dbus_properties_proxy_, GetAll(_))
407 .WillRepeatedly(Return(DBusPropertiesMap()));
408 EXPECT_CALL(*mm1_proxy_, set_state_changed_callback(_)).Times(AnyNumber());
409 EXPECT_CALL(*modem_info_.mock_metrics(), NotifyDeviceScanStarted(_))
410 .Times(AnyNumber());
411 EXPECT_CALL(*modem_info_.mock_cellular_operator_info(),
412 GetOLPByMCCMNC(_)).Times(AnyNumber());
413 EXPECT_CALL(*modem_info_.mock_manager(), UpdateEnabledTechnologies())
414 .Times(AnyNumber());
415 EXPECT_CALL(*dynamic_cast<DeviceMockAdaptor *>(device_->adaptor()),
416 EmitBoolChanged(_, _)).Times(AnyNumber());
417 }
418
419 mm1::MockModemProxy *SetupOnAfterResume() {
420 SetCellularType(Cellular::kTypeUniversal);
421 SetCommonOnAfterResumeExpectations();
422 return mm1_proxy_.get(); // Before the capability snags it.
423 }
424
Eric Shienbrood9a245532012-03-07 14:20:39 -0500425 MOCK_METHOD1(TestCallback, void(const Error &error));
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500426
Darin Petkov0828f5f2011-08-11 10:18:52 -0700427 protected:
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500428 static const char kTestDeviceName[];
429 static const char kTestDeviceAddress[];
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500430 static const char kDBusOwner[];
Jason Glasgowa585fc32012-06-06 11:04:09 -0400431 static const char kDBusService[];
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500432 static const char kDBusPath[];
433 static const char kTestCarrier[];
Darin Petkova4ca3c32012-08-17 16:05:24 +0200434 static const char kTestCarrierSPN[];
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500435 static const char kMEID[];
436 static const char kIMEI[];
437 static const char kIMSI[];
438 static const char kMSISDN[];
439 static const char kTestMobileProviderDBPath[];
Prathmesh Prabhu49ffffd2014-01-09 18:28:55 -0800440 static const Stringmaps kTestNetworksGSM;
441 static const Stringmaps kTestNetworksCellular;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500442 static const int kStrength;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500443
Darin Petkov0828f5f2011-08-11 10:18:52 -0700444 class TestProxyFactory : public ProxyFactory {
445 public:
Paul Stewart7355ce12011-09-02 10:47:01 -0700446 explicit TestProxyFactory(CellularTest *test) : test_(test) {}
Darin Petkov0828f5f2011-08-11 10:18:52 -0700447
mukesh agrawal28185512013-10-18 16:57:09 -0700448 virtual DBusPropertiesProxyInterface *CreateDBusPropertiesProxy(
449 const std::string &path,
450 const std::string &service) {
451 return test_->dbus_properties_proxy_.release();
452 }
453
mukesh agrawal1830fa12011-09-26 14:31:40 -0700454 virtual ModemProxyInterface *CreateModemProxy(
mukesh agrawal1830fa12011-09-26 14:31:40 -0700455 const string &/*path*/,
456 const string &/*service*/) {
Darin Petkov0828f5f2011-08-11 10:18:52 -0700457 return test_->proxy_.release();
458 }
459
460 virtual ModemSimpleProxyInterface *CreateModemSimpleProxy(
mukesh agrawal1830fa12011-09-26 14:31:40 -0700461 const string &/*path*/,
462 const string &/*service*/) {
Darin Petkov0828f5f2011-08-11 10:18:52 -0700463 return test_->simple_proxy_.release();
464 }
465
466 virtual ModemCDMAProxyInterface *CreateModemCDMAProxy(
mukesh agrawal1830fa12011-09-26 14:31:40 -0700467 const string &/*path*/,
468 const string &/*service*/) {
Darin Petkov0828f5f2011-08-11 10:18:52 -0700469 return test_->cdma_proxy_.release();
470 }
471
Darin Petkov975b5e72011-08-30 11:48:08 -0700472 virtual ModemGSMCardProxyInterface *CreateModemGSMCardProxy(
mukesh agrawal1830fa12011-09-26 14:31:40 -0700473 const string &/*path*/,
474 const string &/*service*/) {
Ben Chan3ecdf822012-08-06 12:29:23 -0700475 // TODO(benchan): This code conditionally returns a NULL pointer to avoid
476 // CellularCapabilityGSM::InitProperties (and thus
477 // CellularCapabilityGSM::GetIMSI) from being called during the
478 // construction. Remove this workaround after refactoring the tests.
479 return test_->create_gsm_card_proxy_from_factory_ ?
480 test_->gsm_card_proxy_.release() : NULL;
Darin Petkov975b5e72011-08-30 11:48:08 -0700481 }
482
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700483 virtual ModemGSMNetworkProxyInterface *CreateModemGSMNetworkProxy(
mukesh agrawal1830fa12011-09-26 14:31:40 -0700484 const string &/*path*/,
485 const string &/*service*/) {
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700486 return test_->gsm_network_proxy_.release();
487 }
488
mukesh agrawal28185512013-10-18 16:57:09 -0700489 virtual mm1::ModemModem3gppProxyInterface *CreateMM1ModemModem3gppProxy(
490 const std::string &path,
491 const std::string &service) {
492 return test_->mm1_modem_3gpp_proxy_.release();
493 }
494
495 virtual mm1::ModemProxyInterface *CreateMM1ModemProxy(
496 const std::string &path,
497 const std::string &service) {
498 return test_->mm1_proxy_.release();
499 }
500
501 virtual mm1::ModemSimpleProxyInterface *CreateMM1ModemSimpleProxy(
502 const string &/*path*/,
503 const string &/*service*/) {
504 return test_->mm1_simple_proxy_.release();
505 }
506
Darin Petkov0828f5f2011-08-11 10:18:52 -0700507 private:
508 CellularTest *test_;
509 };
Darin Petkov0828f5f2011-08-11 10:18:52 -0700510 void StartRTNLHandler();
511 void StopRTNLHandler();
512
Ben Chan3ecdf822012-08-06 12:29:23 -0700513 void AllowCreateGSMCardProxyFromFactory() {
514 create_gsm_card_proxy_from_factory_ = true;
515 }
516
Darin Petkov721ac932011-11-16 15:43:09 +0100517 void SetCellularType(Cellular::Type type) {
Ben Chan3ecdf822012-08-06 12:29:23 -0700518 device_->InitCapability(type);
Darin Petkov721ac932011-11-16 15:43:09 +0100519 }
520
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400521 CellularCapabilityClassic *GetCapabilityClassic() {
522 return dynamic_cast<CellularCapabilityClassic *>(
523 device_->capability_.get());
524 }
525
Darin Petkovae0c64e2011-11-15 15:50:27 +0100526 CellularCapabilityCDMA *GetCapabilityCDMA() {
527 return dynamic_cast<CellularCapabilityCDMA *>(device_->capability_.get());
528 }
529
530 CellularCapabilityGSM *GetCapabilityGSM() {
531 return dynamic_cast<CellularCapabilityGSM *>(device_->capability_.get());
532 }
533
Ben Chan09fa2a02012-11-07 22:09:09 -0800534 CellularCapabilityUniversal *GetCapabilityUniversal() {
535 return dynamic_cast<CellularCapabilityUniversal *>(
536 device_->capability_.get());
537 }
538
Prathmesh Prabhu0d36b4f2013-04-01 11:45:54 -0700539 // Different tests simulate a cellular service being set using a real /mock
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700540 // service.
Arman Ugurayf84a4242013-04-09 20:01:07 -0700541 CellularService *SetService() {
Prathmesh Prabhu0d36b4f2013-04-01 11:45:54 -0700542 device_->service_ = new CellularService(&modem_info_, device_);
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700543 return device_->service_;
544 }
Arman Ugurayf84a4242013-04-09 20:01:07 -0700545 MockCellularService *SetMockService() {
Prathmesh Prabhu0d36b4f2013-04-01 11:45:54 -0700546 device_->service_ = new MockCellularService(&modem_info_, device_);
547 return static_cast<MockCellularService *>(device_->service_.get());
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700548 }
549
mukesh agrawal28185512013-10-18 16:57:09 -0700550 void set_enabled_persistent(bool new_value) {
551 device_->enabled_persistent_ = new_value;
552 }
553
Ben Chan539ab022014-02-03 16:34:57 -0800554 void SetCapabilityUniversalActiveBearer(scoped_ptr<CellularBearer> bearer) {
555 SetCellularType(Cellular::kTypeUniversal);
556 CellularCapabilityUniversal *capability = GetCapabilityUniversal();
557 capability->active_bearer_ = bearer.Pass();
558 }
559
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700560 EventDispatcher dispatcher_;
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700561 MockModemInfo modem_info_;
Chris Masone2ae797d2011-08-23 20:41:00 -0700562 MockDeviceInfo device_info_;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700563 NiceMock<MockRTNLHandler> rtnl_handler_;
564
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500565 MockDHCPProvider dhcp_provider_;
566 scoped_refptr<MockDHCPConfig> dhcp_config_;
567
Ben Chan3ecdf822012-08-06 12:29:23 -0700568 bool create_gsm_card_proxy_from_factory_;
mukesh agrawal28185512013-10-18 16:57:09 -0700569 scoped_ptr<MockDBusPropertiesProxy> dbus_properties_proxy_;
Darin Petkov0828f5f2011-08-11 10:18:52 -0700570 scoped_ptr<MockModemProxy> proxy_;
571 scoped_ptr<MockModemSimpleProxy> simple_proxy_;
572 scoped_ptr<MockModemCDMAProxy> cdma_proxy_;
Darin Petkov975b5e72011-08-30 11:48:08 -0700573 scoped_ptr<MockModemGSMCardProxy> gsm_card_proxy_;
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700574 scoped_ptr<MockModemGSMNetworkProxy> gsm_network_proxy_;
mukesh agrawal28185512013-10-18 16:57:09 -0700575 scoped_ptr<mm1::MockModemModem3gppProxy> mm1_modem_3gpp_proxy_;
576 scoped_ptr<mm1::MockModemProxy> mm1_proxy_;
mukesh agrawalf407d592013-07-31 11:37:57 -0700577 scoped_ptr<mm1::MockModemSimpleProxy> mm1_simple_proxy_;
Darin Petkov0828f5f2011-08-11 10:18:52 -0700578 TestProxyFactory proxy_factory_;
Prathmesh Prabhu95afcbb2014-04-08 16:05:42 -0700579 MockMobileOperatorInfo *mock_home_provider_info_;
580 MockMobileOperatorInfo *mock_serving_operator_info_;
Ben Chan3ecdf822012-08-06 12:29:23 -0700581 CellularRefPtr device_;
Darin Petkov0828f5f2011-08-11 10:18:52 -0700582};
583
Darin Petkov77cb6812011-08-15 16:19:41 -0700584const char CellularTest::kTestDeviceName[] = "usb0";
Chris Masone626719f2011-08-18 16:58:48 -0700585const char CellularTest::kTestDeviceAddress[] = "00:01:02:03:04:05";
Darin Petkov0828f5f2011-08-11 10:18:52 -0700586const char CellularTest::kDBusOwner[] = ":1.19";
Jason Glasgowa585fc32012-06-06 11:04:09 -0400587const char CellularTest::kDBusService[] = "org.chromium.ModemManager";
Darin Petkov0828f5f2011-08-11 10:18:52 -0700588const char CellularTest::kDBusPath[] = "/org/chromium/ModemManager/Gobi/0";
Darin Petkov51489002011-08-18 13:13:20 -0700589const char CellularTest::kTestCarrier[] = "The Cellular Carrier";
Darin Petkova4ca3c32012-08-17 16:05:24 +0200590const char CellularTest::kTestCarrierSPN[] = "Home Provider";
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500591const char CellularTest::kMEID[] = "01234567EF8901";
Darin Petkov975b5e72011-08-30 11:48:08 -0700592const char CellularTest::kIMEI[] = "987654321098765";
593const char CellularTest::kIMSI[] = "123456789012345";
594const char CellularTest::kMSISDN[] = "12345678901";
Darin Petkov137884a2011-10-26 18:52:47 +0200595const char CellularTest::kTestMobileProviderDBPath[] =
596 "provider_db_unittest.bfd";
Prathmesh Prabhu49ffffd2014-01-09 18:28:55 -0800597const Stringmaps CellularTest::kTestNetworksGSM =
598 {{{CellularCapabilityGSM::kNetworkPropertyStatus, "1"},
599 {CellularCapabilityGSM::kNetworkPropertyID, "0000"},
600 {CellularCapabilityGSM::kNetworkPropertyLongName, "some_long_name"},
601 {CellularCapabilityGSM::kNetworkPropertyShortName, "short"}}};
602const Stringmaps CellularTest::kTestNetworksCellular =
603 {{{kStatusProperty, "available"},
604 {kNetworkIdProperty, "0000"},
605 {kLongNameProperty, "some_long_name"},
606 {kShortNameProperty, "short"}}};
Eric Shienbrood9a245532012-03-07 14:20:39 -0500607const int CellularTest::kStrength = 90;
Darin Petkov0828f5f2011-08-11 10:18:52 -0700608
Darin Petkove9d12e02011-07-27 15:09:37 -0700609TEST_F(CellularTest, GetStateString) {
Darin Petkovcc044422011-08-17 13:30:06 -0700610 EXPECT_EQ("CellularStateDisabled",
Ben Chanf45b3232013-10-08 20:54:01 -0700611 Cellular::GetStateString(Cellular::kStateDisabled));
Darin Petkovcc044422011-08-17 13:30:06 -0700612 EXPECT_EQ("CellularStateEnabled",
Ben Chanf45b3232013-10-08 20:54:01 -0700613 Cellular::GetStateString(Cellular::kStateEnabled));
Darin Petkovcc044422011-08-17 13:30:06 -0700614 EXPECT_EQ("CellularStateRegistered",
Ben Chanf45b3232013-10-08 20:54:01 -0700615 Cellular::GetStateString(Cellular::kStateRegistered));
Darin Petkovcc044422011-08-17 13:30:06 -0700616 EXPECT_EQ("CellularStateConnected",
Ben Chanf45b3232013-10-08 20:54:01 -0700617 Cellular::GetStateString(Cellular::kStateConnected));
Darin Petkovcc044422011-08-17 13:30:06 -0700618 EXPECT_EQ("CellularStateLinked",
Ben Chanf45b3232013-10-08 20:54:01 -0700619 Cellular::GetStateString(Cellular::kStateLinked));
620}
621
622TEST_F(CellularTest, GetModemStateString) {
Ben Chan7b7d63d2013-10-18 14:53:05 -0700623 EXPECT_EQ("CellularModemStateFailed",
624 Cellular::GetModemStateString(Cellular::kModemStateFailed));
Ben Chanf45b3232013-10-08 20:54:01 -0700625 EXPECT_EQ("CellularModemStateUnknown",
626 Cellular::GetModemStateString(Cellular::kModemStateUnknown));
627 EXPECT_EQ("CellularModemStateInitializing",
628 Cellular::GetModemStateString(Cellular::kModemStateInitializing));
629 EXPECT_EQ("CellularModemStateLocked",
630 Cellular::GetModemStateString(Cellular::kModemStateLocked));
631 EXPECT_EQ("CellularModemStateDisabled",
632 Cellular::GetModemStateString(Cellular::kModemStateDisabled));
633 EXPECT_EQ("CellularModemStateDisabling",
634 Cellular::GetModemStateString(Cellular::kModemStateDisabling));
635 EXPECT_EQ("CellularModemStateEnabling",
636 Cellular::GetModemStateString(Cellular::kModemStateEnabling));
637 EXPECT_EQ("CellularModemStateEnabled",
638 Cellular::GetModemStateString(Cellular::kModemStateEnabled));
639 EXPECT_EQ("CellularModemStateSearching",
640 Cellular::GetModemStateString(Cellular::kModemStateSearching));
641 EXPECT_EQ("CellularModemStateRegistered",
642 Cellular::GetModemStateString(Cellular::kModemStateRegistered));
643 EXPECT_EQ("CellularModemStateDisconnecting",
644 Cellular::GetModemStateString(Cellular::kModemStateDisconnecting));
645 EXPECT_EQ("CellularModemStateConnecting",
646 Cellular::GetModemStateString(Cellular::kModemStateConnecting));
647 EXPECT_EQ("CellularModemStateConnected",
648 Cellular::GetModemStateString(Cellular::kModemStateConnected));
Darin Petkove9d12e02011-07-27 15:09:37 -0700649}
650
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700651TEST_F(CellularTest, StartCDMARegister) {
Darin Petkov721ac932011-11-16 15:43:09 +0100652 SetCellularType(Cellular::kTypeCDMA);
Ben Chan7ea768e2013-09-20 15:08:40 -0700653 ExpectCdmaStartModem(kNetworkTechnology1Xrtt);
Darin Petkov975b5e72011-08-30 11:48:08 -0700654 EXPECT_CALL(*cdma_proxy_, MEID()).WillOnce(Return(kMEID));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500655 Error error;
656 device_->Start(&error, Bind(&CellularTest::TestCallback, Unretained(this)));
Darin Petkov0828f5f2011-08-11 10:18:52 -0700657 dispatcher_.DispatchPendingEvents();
Prathmesh Prabhu9f06c872013-11-21 14:08:23 -0800658 EXPECT_EQ(kMEID, device_->meid());
659 EXPECT_EQ(kTestCarrier, device_->carrier());
Darin Petkovbac96002011-08-09 13:22:00 -0700660 EXPECT_EQ(Cellular::kStateRegistered, device_->state_);
Darin Petkovd2045802011-08-23 11:09:25 -0700661 ASSERT_TRUE(device_->service_.get());
Ben Chan7ea768e2013-09-20 15:08:40 -0700662 EXPECT_EQ(kNetworkTechnology1Xrtt, device_->service_->network_technology());
Darin Petkov22b72bf2011-08-29 14:01:20 -0700663 EXPECT_EQ(kStrength, device_->service_->strength());
Ben Chan7ea768e2013-09-20 15:08:40 -0700664 EXPECT_EQ(kRoamingStateHome, device_->service_->roaming_state());
Darin Petkovbac96002011-08-09 13:22:00 -0700665}
666
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700667TEST_F(CellularTest, StartGSMRegister) {
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700668 InitProviderDB();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500669 EXPECT_CALL(*proxy_, Enable(true, _, _, CellularCapability::kTimeoutEnable))
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500670 .WillOnce(Invoke(this, &CellularTest::InvokeEnable));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500671 EXPECT_CALL(*gsm_card_proxy_,
672 GetIMEI(_, _, CellularCapability::kTimeoutDefault))
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500673 .WillOnce(Invoke(this, &CellularTest::InvokeGetIMEI));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500674 EXPECT_CALL(*gsm_card_proxy_,
675 GetIMSI(_, _, CellularCapability::kTimeoutDefault))
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500676 .WillOnce(Invoke(this, &CellularTest::InvokeGetIMSI));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500677 EXPECT_CALL(*gsm_card_proxy_,
678 GetSPN(_, _, CellularCapability::kTimeoutDefault))
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500679 .WillOnce(Invoke(this, &CellularTest::InvokeGetSPN));
680 EXPECT_CALL(*gsm_card_proxy_,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500681 GetMSISDN(_, _, CellularCapability::kTimeoutDefault))
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500682 .WillOnce(Invoke(this, &CellularTest::InvokeGetMSISDN));
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700683 EXPECT_CALL(*gsm_network_proxy_, AccessTechnology())
684 .WillOnce(Return(MM_MODEM_GSM_ACCESS_TECH_EDGE));
Darin Petkov63138a92012-02-06 14:09:15 +0100685 EXPECT_CALL(*gsm_card_proxy_, EnabledFacilityLocks())
686 .WillOnce(Return(MM_MODEM_GSM_FACILITY_SIM));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500687 EXPECT_CALL(*proxy_, GetModemInfo(_, _, CellularCapability::kTimeoutDefault))
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500688 .WillOnce(Invoke(this, &CellularTest::InvokeGetModemInfo));
Darin Petkov137884a2011-10-26 18:52:47 +0200689 static const char kNetworkID[] = "22803";
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500690 EXPECT_CALL(*gsm_network_proxy_,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500691 GetRegistrationInfo(_, _, CellularCapability::kTimeoutDefault))
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500692 .WillOnce(Invoke(this, &CellularTest::InvokeGetRegistrationInfo));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500693 EXPECT_CALL(*gsm_network_proxy_, GetSignalQuality(NULL, _, _))
694 .Times(2)
695 .WillRepeatedly(Invoke(this,
696 &CellularTest::InvokeGetSignalQuality));
697 EXPECT_CALL(*this, TestCallback(IsSuccess()));
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700698 EXPECT_CALL(*modem_info_.mock_manager(), RegisterService(_));
Ben Chan3ecdf822012-08-06 12:29:23 -0700699 AllowCreateGSMCardProxyFromFactory();
700
Eric Shienbrood9a245532012-03-07 14:20:39 -0500701 Error error;
702 device_->Start(&error, Bind(&CellularTest::TestCallback, Unretained(this)));
703 EXPECT_TRUE(error.IsSuccess());
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700704 dispatcher_.DispatchPendingEvents();
Prathmesh Prabhu9f06c872013-11-21 14:08:23 -0800705 EXPECT_EQ(kIMEI, device_->imei());
706 EXPECT_EQ(kIMSI, device_->imsi());
Darin Petkova4ca3c32012-08-17 16:05:24 +0200707 EXPECT_EQ(kTestCarrierSPN, GetCapabilityGSM()->spn_);
Prathmesh Prabhu9f06c872013-11-21 14:08:23 -0800708 EXPECT_EQ(kMSISDN, device_->mdn());
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700709 EXPECT_EQ(Cellular::kStateRegistered, device_->state_);
710 ASSERT_TRUE(device_->service_.get());
Ben Chan7ea768e2013-09-20 15:08:40 -0700711 EXPECT_EQ(kNetworkTechnologyEdge, device_->service_->network_technology());
Darin Petkov63138a92012-02-06 14:09:15 +0100712 EXPECT_TRUE(GetCapabilityGSM()->sim_lock_status_.enabled);
Darin Petkov22b72bf2011-08-29 14:01:20 -0700713 EXPECT_EQ(kStrength, device_->service_->strength());
Ben Chan7ea768e2013-09-20 15:08:40 -0700714 EXPECT_EQ(kRoamingStateRoaming, device_->service_->roaming_state());
Darin Petkov137884a2011-10-26 18:52:47 +0200715 EXPECT_EQ(kNetworkID, device_->service_->serving_operator().GetCode());
Darin Petkova4ca3c32012-08-17 16:05:24 +0200716 EXPECT_EQ(kTestCarrier, device_->service_->serving_operator().GetName());
Darin Petkov137884a2011-10-26 18:52:47 +0200717 EXPECT_EQ("ch", device_->service_->serving_operator().GetCountry());
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700718}
719
Darin Petkovbac96002011-08-09 13:22:00 -0700720TEST_F(CellularTest, StartConnected) {
Chris Masone2ae797d2011-08-23 20:41:00 -0700721 EXPECT_CALL(device_info_, GetFlags(device_->interface_index(), _))
722 .WillOnce(Return(true));
Darin Petkov721ac932011-11-16 15:43:09 +0100723 SetCellularType(Cellular::kTypeCDMA);
Darin Petkovbac96002011-08-09 13:22:00 -0700724 device_->set_modem_state(Cellular::kModemStateConnected);
Prathmesh Prabhu9f06c872013-11-21 14:08:23 -0800725 device_->set_meid(kMEID);
Ben Chan7ea768e2013-09-20 15:08:40 -0700726 ExpectCdmaStartModem(kNetworkTechnologyEvdo);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500727 Error error;
728 device_->Start(&error, Bind(&CellularTest::TestCallback, Unretained(this)));
729 EXPECT_TRUE(error.IsSuccess());
Darin Petkov0828f5f2011-08-11 10:18:52 -0700730 dispatcher_.DispatchPendingEvents();
Darin Petkovbac96002011-08-09 13:22:00 -0700731 EXPECT_EQ(Cellular::kStateConnected, device_->state_);
732}
733
Darin Petkov0828f5f2011-08-11 10:18:52 -0700734TEST_F(CellularTest, StartLinked) {
Chris Masone2ae797d2011-08-23 20:41:00 -0700735 EXPECT_CALL(device_info_, GetFlags(device_->interface_index(), _))
736 .WillOnce(DoAll(SetArgumentPointee<1>(IFF_UP), Return(true)));
Darin Petkov721ac932011-11-16 15:43:09 +0100737 SetCellularType(Cellular::kTypeCDMA);
Darin Petkov0828f5f2011-08-11 10:18:52 -0700738 device_->set_modem_state(Cellular::kModemStateConnected);
Prathmesh Prabhu9f06c872013-11-21 14:08:23 -0800739 device_->set_meid(kMEID);
Ben Chan7ea768e2013-09-20 15:08:40 -0700740 ExpectCdmaStartModem(kNetworkTechnologyEvdo);
Paul Stewart75a68b92013-10-24 10:50:27 -0700741 EXPECT_CALL(dhcp_provider_, CreateConfig(kTestDeviceName, _, _, _, _))
Darin Petkov77cb6812011-08-15 16:19:41 -0700742 .WillOnce(Return(dhcp_config_));
743 EXPECT_CALL(*dhcp_config_, RequestIP()).WillOnce(Return(true));
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700744 EXPECT_CALL(*modem_info_.mock_manager(), UpdateService(_)).Times(3);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500745 Error error;
746 device_->Start(&error, Bind(&CellularTest::TestCallback, Unretained(this)));
747 EXPECT_TRUE(error.IsSuccess());
Darin Petkov0828f5f2011-08-11 10:18:52 -0700748 dispatcher_.DispatchPendingEvents();
749 EXPECT_EQ(Cellular::kStateLinked, device_->state_);
Darin Petkov60b8c3b2011-08-25 11:03:20 -0700750 EXPECT_EQ(Service::kStateConfiguring, device_->service_->state());
751 device_->SelectService(NULL);
Darin Petkov0828f5f2011-08-11 10:18:52 -0700752}
753
Darin Petkov3335b372011-08-22 11:05:32 -0700754TEST_F(CellularTest, CreateService) {
Darin Petkov721ac932011-11-16 15:43:09 +0100755 SetCellularType(Cellular::kTypeCDMA);
Darin Petkov3335b372011-08-22 11:05:32 -0700756 static const char kPaymentURL[] = "https://payment.url";
757 static const char kUsageURL[] = "https://usage.url";
Darin Petkov3335b372011-08-22 11:05:32 -0700758 device_->home_provider_.SetName(kTestCarrier);
Darin Petkov381928f2012-02-02 23:00:12 +0100759 GetCapabilityCDMA()->olp_.SetURL(kPaymentURL);
Darin Petkovae0c64e2011-11-15 15:50:27 +0100760 GetCapabilityCDMA()->usage_url_ = kUsageURL;
Darin Petkov3335b372011-08-22 11:05:32 -0700761 device_->CreateService();
762 ASSERT_TRUE(device_->service_.get());
Darin Petkov381928f2012-02-02 23:00:12 +0100763 EXPECT_EQ(kPaymentURL, device_->service_->olp().GetURL());
Darin Petkov3335b372011-08-22 11:05:32 -0700764 EXPECT_EQ(kUsageURL, device_->service_->usage_url());
765 EXPECT_EQ(kTestCarrier, device_->service_->serving_operator().GetName());
Ben Chan3d6de0e2012-12-10 12:01:34 -0800766 ASSERT_FALSE(device_->service_->activate_over_non_cellular_network());
Darin Petkov3335b372011-08-22 11:05:32 -0700767}
768
Prathmesh Prabhu95afcbb2014-04-08 16:05:42 -0700769TEST_F(CellularTest, ServiceFriendlyName) {
770 SetMockMobileOperatorInfoObjects();
771 CHECK(mock_home_provider_info_);
772 CHECK(mock_serving_operator_info_);
773
774 // Test that the name created for the service is sensible under different
775 // scenarios w.r.t. information about the mobile network operator.
776 const string home_provider_name {"HomeProviderName"};
777 const string serving_operator_name {"ServingOperatorName"};
778 SetCellularType(Cellular::kTypeCDMA);
779
780 // (1) Service created, MNO not known => Default name.
781 EXPECT_CALL(*mock_home_provider_info_, IsMobileNetworkOperatorKnown())
782 .WillOnce(Return(false));
783 EXPECT_CALL(*mock_serving_operator_info_, IsMobileNetworkOperatorKnown())
784 .WillOnce(Return(false));
785 device_->CreateService();
786 // Compare substrings explicitly using EXPECT_EQ for better error message.
787 size_t prefix_len = strlen(Cellular::kGenericServiceNamePrefix);
788 EXPECT_EQ(Cellular::kGenericServiceNamePrefix,
789 device_->service_->friendly_name().substr(0, prefix_len));
790 Mock::VerifyAndClearExpectations(mock_home_provider_info_);
791 Mock::VerifyAndClearExpectations(mock_serving_operator_info_);
792 device_->DestroyService();
793
794 // (2) Service created, then home provider determined => Name provided by
795 // home provider.
796 mock_home_provider_info_->SetEmptyDefaultsForProperties();
797 EXPECT_CALL(*mock_home_provider_info_, IsMobileNetworkOperatorKnown())
798 .WillOnce(Return(false))
799 .WillOnce(Return(true));
800 EXPECT_CALL(*mock_serving_operator_info_, IsMobileNetworkOperatorKnown())
801 .WillRepeatedly(Return(false));
802 EXPECT_CALL(*mock_home_provider_info_, operator_name())
803 .WillRepeatedly(ReturnRef(home_provider_name));
804 device_->CreateService();
805 // Now emulate an event for updated home provider information.
806 device_->mobile_operator_info_observer_->OnOperatorChanged();
807 EXPECT_EQ(home_provider_name, device_->service_->friendly_name());
808 Mock::VerifyAndClearExpectations(mock_home_provider_info_);
809 Mock::VerifyAndClearExpectations(mock_serving_operator_info_);
810 device_->DestroyService();
811
812 // (3) Service created, then serving operator determined => Name provided by
813 // serving operator.
814 mock_serving_operator_info_->SetEmptyDefaultsForProperties();
815 EXPECT_CALL(*mock_serving_operator_info_, IsMobileNetworkOperatorKnown())
816 .WillOnce(Return(false))
817 .WillOnce(Return(true));
818 EXPECT_CALL(*mock_home_provider_info_, IsMobileNetworkOperatorKnown())
819 .WillRepeatedly(Return(false));
820 EXPECT_CALL(*mock_serving_operator_info_, operator_name())
821 .WillRepeatedly(ReturnRef(serving_operator_name));
822 device_->CreateService();
823 // Now emulate an event for updated serving operator information.
824 device_->mobile_operator_info_observer_->OnOperatorChanged();
825 EXPECT_EQ(serving_operator_name, device_->service_->friendly_name());
826 Mock::VerifyAndClearExpectations(mock_home_provider_info_);
827 Mock::VerifyAndClearExpectations(mock_serving_operator_info_);
828 device_->DestroyService();
829
830 // (4) Service created, then home provider determined, then serving operator
831 // determined => final name is serving operator.
832 mock_home_provider_info_->SetEmptyDefaultsForProperties();
833 mock_serving_operator_info_->SetEmptyDefaultsForProperties();
834 EXPECT_CALL(*mock_serving_operator_info_, IsMobileNetworkOperatorKnown())
835 .WillOnce(Return(false))
836 .WillOnce(Return(true))
837 .WillOnce(Return(true));
838 EXPECT_CALL(*mock_home_provider_info_, IsMobileNetworkOperatorKnown())
839 .WillOnce(Return(false))
840 .WillOnce(Return(false))
841 .WillOnce(Return(true));
842 EXPECT_CALL(*mock_home_provider_info_, operator_name())
843 .WillRepeatedly(ReturnRef(home_provider_name));
844 EXPECT_CALL(*mock_serving_operator_info_, operator_name())
845 .WillRepeatedly(ReturnRef(serving_operator_name));
846 device_->CreateService();
847 device_->mobile_operator_info_observer_->OnOperatorChanged();
848 device_->mobile_operator_info_observer_->OnOperatorChanged();
849 EXPECT_EQ(serving_operator_name, device_->service_->friendly_name());
850 Mock::VerifyAndClearExpectations(mock_home_provider_info_);
851 Mock::VerifyAndClearExpectations(mock_serving_operator_info_);
852 device_->DestroyService();
853
854 // (5) Service created, then serving operator determined, then home provider
855 // determined => final name is serving operator.
856 mock_home_provider_info_->SetEmptyDefaultsForProperties();
857 mock_serving_operator_info_->SetEmptyDefaultsForProperties();
858 EXPECT_CALL(*mock_serving_operator_info_, IsMobileNetworkOperatorKnown())
859 .WillOnce(Return(false))
860 .WillOnce(Return(false))
861 .WillOnce(Return(true));
862 EXPECT_CALL(*mock_home_provider_info_, IsMobileNetworkOperatorKnown())
863 .WillOnce(Return(false))
864 .WillOnce(Return(true))
865 .WillOnce(Return(true));
866 EXPECT_CALL(*mock_home_provider_info_, operator_name())
867 .WillRepeatedly(ReturnRef(home_provider_name));
868 EXPECT_CALL(*mock_serving_operator_info_, operator_name())
869 .WillRepeatedly(ReturnRef(serving_operator_name));
870 device_->CreateService();
871 device_->mobile_operator_info_observer_->OnOperatorChanged();
872 device_->mobile_operator_info_observer_->OnOperatorChanged();
873 EXPECT_EQ(serving_operator_name, device_->service_->friendly_name());
874 Mock::VerifyAndClearExpectations(mock_home_provider_info_);
875 Mock::VerifyAndClearExpectations(mock_serving_operator_info_);
876 device_->DestroyService();
877
878 // (6) Serving operator known, home provider known, and then service created
879 // => Name is serving operator.
880 mock_home_provider_info_->SetEmptyDefaultsForProperties();
881 mock_serving_operator_info_->SetEmptyDefaultsForProperties();
882 EXPECT_CALL(*mock_serving_operator_info_, IsMobileNetworkOperatorKnown())
883 .WillOnce(Return(true));
884 EXPECT_CALL(*mock_home_provider_info_, IsMobileNetworkOperatorKnown())
885 .WillOnce(Return(true));
886 EXPECT_CALL(*mock_home_provider_info_, operator_name())
887 .WillRepeatedly(ReturnRef(home_provider_name));
888 EXPECT_CALL(*mock_serving_operator_info_, operator_name())
889 .WillRepeatedly(ReturnRef(serving_operator_name));
890 device_->CreateService();
891 EXPECT_EQ(serving_operator_name, device_->service_->friendly_name());
892}
893
Darin Petkovc5f56562011-08-06 16:40:05 -0700894namespace {
895
896MATCHER(ContainsPhoneNumber, "") {
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400897 return ContainsKey(arg,
898 CellularCapabilityClassic::kConnectPropertyPhoneNumber);
Darin Petkovc5f56562011-08-06 16:40:05 -0700899}
900
Eric Shienbrood9a245532012-03-07 14:20:39 -0500901} // namespace
Darin Petkovc5f56562011-08-06 16:40:05 -0700902
903TEST_F(CellularTest, Connect) {
Darin Petkov4d6d9412011-08-24 13:19:54 -0700904 Error error;
Chris Masone2ae797d2011-08-23 20:41:00 -0700905 EXPECT_CALL(device_info_, GetFlags(device_->interface_index(), _))
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700906 .Times(2)
907 .WillRepeatedly(Return(true));
Darin Petkovc5f56562011-08-06 16:40:05 -0700908 device_->state_ = Cellular::kStateConnected;
Darin Petkov4d6d9412011-08-24 13:19:54 -0700909 device_->Connect(&error);
910 EXPECT_EQ(Error::kAlreadyConnected, error.type());
911 error.Populate(Error::kSuccess);
Darin Petkovc5f56562011-08-06 16:40:05 -0700912
Darin Petkov0828f5f2011-08-11 10:18:52 -0700913 device_->state_ = Cellular::kStateLinked;
Darin Petkov4d6d9412011-08-24 13:19:54 -0700914 device_->Connect(&error);
915 EXPECT_EQ(Error::kAlreadyConnected, error.type());
Darin Petkov0828f5f2011-08-11 10:18:52 -0700916
Thieu Lec7d8cd12013-02-13 11:38:14 -0800917 device_->state_ = Cellular::kStateEnabled;
918 device_->Connect(&error);
919 EXPECT_EQ(Error::kNotRegistered, error.type());
920
921 error.Reset();
922 device_->state_ = Cellular::kStateDisabled;
923 device_->Connect(&error);
924 EXPECT_EQ(Error::kNotRegistered, error.type());
925
Darin Petkovc5f56562011-08-06 16:40:05 -0700926 device_->state_ = Cellular::kStateRegistered;
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700927 SetService();
Darin Petkovd2045802011-08-23 11:09:25 -0700928
Jason Glasgow7b461df2012-05-01 16:38:45 -0400929 device_->allow_roaming_ = false;
Ben Chan7ea768e2013-09-20 15:08:40 -0700930 device_->service_->roaming_state_ = kRoamingStateRoaming;
Darin Petkov4d6d9412011-08-24 13:19:54 -0700931 device_->Connect(&error);
Darin Petkov4d6d9412011-08-24 13:19:54 -0700932 EXPECT_EQ(Error::kNotOnHomeNetwork, error.type());
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500933
Darin Petkov4d6d9412011-08-24 13:19:54 -0700934 error.Populate(Error::kSuccess);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500935 EXPECT_CALL(*simple_proxy_,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500936 Connect(ContainsPhoneNumber(), _, _,
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500937 CellularCapability::kTimeoutConnect))
938 .Times(2)
939 .WillRepeatedly(Invoke(this, &CellularTest::InvokeConnect));
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400940 GetCapabilityClassic()->simple_proxy_.reset(simple_proxy_.release());
Ben Chan7ea768e2013-09-20 15:08:40 -0700941 device_->service_->roaming_state_ = kRoamingStateHome;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500942 device_->state_ = Cellular::kStateRegistered;
Darin Petkov4d6d9412011-08-24 13:19:54 -0700943 device_->Connect(&error);
944 EXPECT_TRUE(error.IsSuccess());
Darin Petkovc5f56562011-08-06 16:40:05 -0700945 dispatcher_.DispatchPendingEvents();
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500946 EXPECT_EQ(Cellular::kStateConnected, device_->state_);
947
Jason Glasgow7b461df2012-05-01 16:38:45 -0400948 device_->allow_roaming_ = true;
Ben Chan7ea768e2013-09-20 15:08:40 -0700949 device_->service_->roaming_state_ = kRoamingStateRoaming;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500950 device_->state_ = Cellular::kStateRegistered;
951 device_->Connect(&error);
952 EXPECT_TRUE(error.IsSuccess());
953 dispatcher_.DispatchPendingEvents();
954 EXPECT_EQ(Cellular::kStateConnected, device_->state_);
Darin Petkovc5f56562011-08-06 16:40:05 -0700955}
956
Eric Shienbrood9a245532012-03-07 14:20:39 -0500957TEST_F(CellularTest, Disconnect) {
958 Error error;
959 device_->state_ = Cellular::kStateRegistered;
960 device_->Disconnect(&error);
961 EXPECT_EQ(Error::kNotConnected, error.type());
962 error.Reset();
963
Darin Petkovfb0625e2012-01-16 13:05:56 +0100964 device_->state_ = Cellular::kStateConnected;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500965 EXPECT_CALL(*proxy_,
Thieu Le049adb52012-11-12 17:14:51 -0800966 Disconnect(_, _, CellularCapability::kTimeoutDisconnect))
Eric Shienbrood9a245532012-03-07 14:20:39 -0500967 .WillOnce(Invoke(this, &CellularTest::InvokeDisconnect));
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400968 GetCapabilityClassic()->proxy_.reset(proxy_.release());
Eric Shienbrood9a245532012-03-07 14:20:39 -0500969 device_->Disconnect(&error);
970 EXPECT_TRUE(error.IsSuccess());
971 EXPECT_EQ(Cellular::kStateRegistered, device_->state_);
Darin Petkovfb0625e2012-01-16 13:05:56 +0100972}
973
Arman Uguray539c4232012-09-11 10:00:22 -0700974TEST_F(CellularTest, DisconnectFailure) {
975 // Test the case where the underlying modem state is set
976 // to disconnecting, but shill thinks it's still connected
977 Error error;
978 device_->state_ = Cellular::kStateConnected;
979 EXPECT_CALL(*proxy_,
Thieu Le049adb52012-11-12 17:14:51 -0800980 Disconnect(_, _, CellularCapability::kTimeoutDisconnect))
Arman Uguray539c4232012-09-11 10:00:22 -0700981 .Times(2)
982 .WillRepeatedly(Invoke(this, &CellularTest::InvokeDisconnectFail));
983 GetCapabilityClassic()->proxy_.reset(proxy_.release());
984 device_->modem_state_ = Cellular::kModemStateDisconnecting;
985 device_->Disconnect(&error);
986 EXPECT_TRUE(error.IsFailure());
987 EXPECT_EQ(Cellular::kStateConnected, device_->state_);
988
989 device_->modem_state_ = Cellular::kModemStateConnected;
990 device_->Disconnect(&error);
991 EXPECT_TRUE(error.IsFailure());
992 EXPECT_EQ(Cellular::kStateRegistered, device_->state_);
993}
994
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400995TEST_F(CellularTest, ConnectFailure) {
996 SetCellularType(Cellular::kTypeCDMA);
997 device_->state_ = Cellular::kStateRegistered;
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700998 SetService();
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400999 ASSERT_EQ(Service::kStateIdle, device_->service_->state());
1000 EXPECT_CALL(*simple_proxy_,
1001 Connect(_, _, _, CellularCapability::kTimeoutConnect))
1002 .WillOnce(Invoke(this, &CellularTest::InvokeConnectFail));
Jason Glasgow82f9ab32012-04-04 14:27:19 -04001003 GetCapabilityClassic()->simple_proxy_.reset(simple_proxy_.release());
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -04001004 Error error;
1005 device_->Connect(&error);
1006 EXPECT_EQ(Service::kStateFailure, device_->service_->state());
1007}
1008
Thieu Leb5954a22012-05-18 10:37:34 -07001009TEST_F(CellularTest, ConnectFailureNoService) {
1010 // Make sure we don't crash if the connect failed and there is no
1011 // CellularService object. This can happen if the modem is enabled and
1012 // then quick disabled.
1013 SetCellularType(Cellular::kTypeCDMA);
1014 device_->state_ = Cellular::kStateRegistered;
Prathmesh Prabhu27526f12013-03-25 19:42:18 -07001015 SetService();
Thieu Leb5954a22012-05-18 10:37:34 -07001016 EXPECT_CALL(
1017 *simple_proxy_,
1018 Connect(_, _, _, CellularCapability::kTimeoutConnect))
1019 .WillOnce(Invoke(this, &CellularTest::InvokeConnectFailNoService));
Prathmesh Prabhu27526f12013-03-25 19:42:18 -07001020 EXPECT_CALL(*modem_info_.mock_manager(), UpdateService(_));
Thieu Leb5954a22012-05-18 10:37:34 -07001021 GetCapabilityClassic()->simple_proxy_.reset(simple_proxy_.release());
1022 Error error;
1023 device_->Connect(&error);
1024}
1025
Thieu Led4974cd2013-05-23 10:39:28 -07001026TEST_F(CellularTest, ConnectSuccessNoService) {
1027 // Make sure we don't crash if the connect succeeds but the service was
1028 // destroyed before the connect request completes.
1029 SetCellularType(Cellular::kTypeCDMA);
1030 device_->state_ = Cellular::kStateRegistered;
1031 SetService();
1032 EXPECT_CALL(
1033 *simple_proxy_,
1034 Connect(_, _, _, CellularCapability::kTimeoutConnect))
1035 .WillOnce(Invoke(this, &CellularTest::InvokeConnectSuccessNoService));
1036 EXPECT_CALL(*modem_info_.mock_manager(), UpdateService(_));
1037 GetCapabilityClassic()->simple_proxy_.reset(simple_proxy_.release());
1038 Error error;
1039 device_->Connect(&error);
1040}
1041
Arman Uguray32c76402012-11-27 14:01:13 -08001042TEST_F(CellularTest, LinkEventWontDestroyService) {
1043 // If the network interface goes down, Cellular::LinkEvent should
1044 // drop the connection but the service object should persist.
1045 device_->state_ = Cellular::kStateLinked;
Prathmesh Prabhu27526f12013-03-25 19:42:18 -07001046 CellularService *service = SetService();
Arman Uguray32c76402012-11-27 14:01:13 -08001047 device_->LinkEvent(0, 0); // flags doesn't contain IFF_UP
1048 EXPECT_EQ(device_->state_, Cellular::kStateConnected);
1049 EXPECT_EQ(device_->service_, service);
1050}
1051
Arman Ugurayed8e6102012-11-29 14:47:20 -08001052TEST_F(CellularTest, UseNoArpGateway) {
Paul Stewart75a68b92013-10-24 10:50:27 -07001053 EXPECT_CALL(dhcp_provider_, CreateConfig(kTestDeviceName, _, _, false, false))
Arman Ugurayed8e6102012-11-29 14:47:20 -08001054 .WillOnce(Return(dhcp_config_));
1055 device_->AcquireIPConfig();
1056}
1057
Ben Chan09fa2a02012-11-07 22:09:09 -08001058TEST_F(CellularTest, HandleNewRegistrationStateForServiceRequiringActivation) {
1059 SetCellularType(Cellular::kTypeUniversal);
1060
1061 // Service activation is needed
Prathmesh Prabhu9f06c872013-11-21 14:08:23 -08001062 device_->set_mdn("0000000000");
Arman Ugurayf4c61812013-01-10 18:58:39 -08001063 CellularService::OLP olp;
Prathmesh Prabhu27526f12013-03-25 19:42:18 -07001064 EXPECT_CALL(*modem_info_.mock_cellular_operator_info(), GetOLPByMCCMNC(_))
Arman Ugurayf4c61812013-01-10 18:58:39 -08001065 .WillRepeatedly(Return(&olp));
Arman Uguray41cc6342013-03-29 16:34:39 -07001066 EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1067 GetActivationState(_,_))
1068 .WillRepeatedly(Return(PendingActivationStore::kStateUnknown));
Ben Chan09fa2a02012-11-07 22:09:09 -08001069
1070 device_->state_ = Cellular::kStateDisabled;
1071 device_->HandleNewRegistrationState();
1072 EXPECT_FALSE(device_->service_.get());
1073
1074 device_->state_ = Cellular::kStateEnabled;
1075 device_->HandleNewRegistrationState();
1076 EXPECT_TRUE(device_->service_.get());
Ben Chan3d6de0e2012-12-10 12:01:34 -08001077 EXPECT_TRUE(device_->service_->activate_over_non_cellular_network());
Ben Chan09fa2a02012-11-07 22:09:09 -08001078}
1079
Eric Shienbrood7fce52c2012-04-13 19:11:02 -04001080TEST_F(CellularTest, ModemStateChangeEnable) {
1081 EXPECT_CALL(*simple_proxy_,
1082 GetModemStatus(_, _, CellularCapability::kTimeoutDefault))
1083 .WillOnce(Invoke(this, &CellularTest::InvokeGetModemStatus));
1084 EXPECT_CALL(*cdma_proxy_, MEID()).WillOnce(Return(kMEID));
1085 EXPECT_CALL(*proxy_,
1086 GetModemInfo(_, _, CellularCapability::kTimeoutDefault))
1087 .WillOnce(Invoke(this, &CellularTest::InvokeGetModemInfo));
1088 EXPECT_CALL(*cdma_proxy_, GetRegistrationState(NULL, _, _))
1089 .WillOnce(Invoke(this,
1090 &CellularTest::InvokeGetRegistrationStateUnregistered));
1091 EXPECT_CALL(*cdma_proxy_, GetSignalQuality(NULL, _, _))
1092 .WillOnce(Invoke(this, &CellularTest::InvokeGetSignalQuality));
Prathmesh Prabhu27526f12013-03-25 19:42:18 -07001093 EXPECT_CALL(*modem_info_.mock_manager(), UpdateEnabledTechnologies());
Eric Shienbrood7fce52c2012-04-13 19:11:02 -04001094 device_->state_ = Cellular::kStateDisabled;
1095 device_->set_modem_state(Cellular::kModemStateDisabled);
1096 SetCellularType(Cellular::kTypeCDMA);
1097
1098 DBusPropertiesMap props;
1099 props[CellularCapabilityClassic::kModemPropertyEnabled].writer().
1100 append_bool(true);
1101 device_->OnDBusPropertiesChanged(MM_MODEM_INTERFACE, props, vector<string>());
1102 dispatcher_.DispatchPendingEvents();
1103
1104 EXPECT_EQ(Cellular::kModemStateEnabled, device_->modem_state());
1105 EXPECT_EQ(Cellular::kStateEnabled, device_->state());
1106 EXPECT_TRUE(device_->enabled());
1107}
1108
1109TEST_F(CellularTest, ModemStateChangeDisable) {
1110 EXPECT_CALL(*proxy_,
Thieu Le049adb52012-11-12 17:14:51 -08001111 Disconnect(_, _, CellularCapability::kTimeoutDisconnect))
Eric Shienbrood7fce52c2012-04-13 19:11:02 -04001112 .WillOnce(Invoke(this, &CellularTest::InvokeDisconnect));
1113 EXPECT_CALL(*proxy_,
Thieu Lec8d2d962012-05-15 14:31:18 -07001114 Enable(false, _, _, CellularCapability::kTimeoutEnable))
Eric Shienbrood7fce52c2012-04-13 19:11:02 -04001115 .WillOnce(Invoke(this, &CellularTest::InvokeEnable));
Prathmesh Prabhu27526f12013-03-25 19:42:18 -07001116 EXPECT_CALL(*modem_info_.mock_manager(), UpdateEnabledTechnologies());
Eric Shienbrood7fce52c2012-04-13 19:11:02 -04001117 device_->enabled_ = true;
1118 device_->enabled_pending_ = true;
1119 device_->state_ = Cellular::kStateEnabled;
1120 device_->set_modem_state(Cellular::kModemStateEnabled);
1121 SetCellularType(Cellular::kTypeCDMA);
1122 GetCapabilityClassic()->InitProxies();
1123
1124 GetCapabilityClassic()->OnModemStateChangedSignal(kModemClassicStateEnabled,
1125 kModemClassicStateDisabled,
1126 0);
1127 dispatcher_.DispatchPendingEvents();
1128
1129 EXPECT_EQ(Cellular::kModemStateDisabled, device_->modem_state());
1130 EXPECT_EQ(Cellular::kStateDisabled, device_->state());
1131 EXPECT_FALSE(device_->enabled());
1132}
1133
Thieu Led0012052012-07-25 16:09:09 -07001134TEST_F(CellularTest, ModemStateChangeStaleConnected) {
1135 // Test to make sure that we ignore stale modem Connected state transitions.
1136 // When a modem is asked to connect and before the connect completes, the
1137 // modem is disabled, it may send a stale Connected state transition after
1138 // it has been disabled.
1139 device_->state_ = Cellular::kStateDisabled;
Arman Uguray1ee93912013-09-24 21:24:10 -07001140 device_->modem_state_ = Cellular::kModemStateEnabling;
1141 device_->OnModemStateChanged(Cellular::kModemStateConnected);
Thieu Led0012052012-07-25 16:09:09 -07001142 EXPECT_EQ(Cellular::kStateDisabled, device_->state());
1143}
1144
1145TEST_F(CellularTest, ModemStateChangeValidConnected) {
1146 device_->state_ = Cellular::kStateEnabled;
Arman Uguray1ee93912013-09-24 21:24:10 -07001147 device_->modem_state_ = Cellular::kModemStateConnecting;
Prathmesh Prabhu27526f12013-03-25 19:42:18 -07001148 SetService();
Arman Uguray1ee93912013-09-24 21:24:10 -07001149 device_->OnModemStateChanged(Cellular::kModemStateConnected);
Thieu Led0012052012-07-25 16:09:09 -07001150 EXPECT_EQ(Cellular::kStateConnected, device_->state());
1151}
1152
Thieu Le5218cf22012-11-26 11:52:57 -08001153TEST_F(CellularTest, ModemStateChangeLostRegistration) {
1154 SetCellularType(Cellular::kTypeUniversal);
1155 CellularCapabilityUniversal *capability = GetCapabilityUniversal();
1156 capability->registration_state_ = MM_MODEM_3GPP_REGISTRATION_STATE_HOME;
1157 EXPECT_TRUE(capability->IsRegistered());
Arman Uguray1ee93912013-09-24 21:24:10 -07001158 device_->set_modem_state(Cellular::kModemStateRegistered);
1159 device_->OnModemStateChanged(Cellular::kModemStateEnabled);
Thieu Le5218cf22012-11-26 11:52:57 -08001160 EXPECT_FALSE(capability->IsRegistered());
1161}
1162
Thieu Le37b90032012-05-15 15:18:41 -07001163TEST_F(CellularTest, StartModemCallback) {
Gary Morainbaeefdf2012-04-30 14:53:35 -07001164 EXPECT_CALL(*this, TestCallback(IsSuccess()));
1165 EXPECT_EQ(device_->state_, Cellular::kStateDisabled);
Thieu Le37b90032012-05-15 15:18:41 -07001166 device_->StartModemCallback(Bind(&CellularTest::TestCallback,
1167 Unretained(this)),
1168 Error(Error::kSuccess));
Gary Morainbaeefdf2012-04-30 14:53:35 -07001169 EXPECT_EQ(device_->state_, Cellular::kStateEnabled);
1170}
1171
Thieu Le37b90032012-05-15 15:18:41 -07001172TEST_F(CellularTest, StartModemCallbackFail) {
Gary Morainbaeefdf2012-04-30 14:53:35 -07001173 EXPECT_CALL(*this, TestCallback(IsFailure()));
1174 EXPECT_EQ(device_->state_, Cellular::kStateDisabled);
Thieu Le37b90032012-05-15 15:18:41 -07001175 device_->StartModemCallback(Bind(&CellularTest::TestCallback,
1176 Unretained(this)),
1177 Error(Error::kOperationFailed));
Gary Morainbaeefdf2012-04-30 14:53:35 -07001178 EXPECT_EQ(device_->state_, Cellular::kStateDisabled);
1179}
1180
Thieu Le37b90032012-05-15 15:18:41 -07001181TEST_F(CellularTest, StopModemCallback) {
1182 EXPECT_CALL(*this, TestCallback(IsSuccess()));
Prathmesh Prabhu27526f12013-03-25 19:42:18 -07001183 SetMockService();
Thieu Le37b90032012-05-15 15:18:41 -07001184 device_->StopModemCallback(Bind(&CellularTest::TestCallback,
1185 Unretained(this)),
1186 Error(Error::kSuccess));
1187 EXPECT_EQ(device_->state_, Cellular::kStateDisabled);
1188 EXPECT_FALSE(device_->service_.get());
1189}
1190
1191TEST_F(CellularTest, StopModemCallbackFail) {
1192 EXPECT_CALL(*this, TestCallback(IsFailure()));
Prathmesh Prabhu27526f12013-03-25 19:42:18 -07001193 SetMockService();
Thieu Le37b90032012-05-15 15:18:41 -07001194 device_->StopModemCallback(Bind(&CellularTest::TestCallback,
1195 Unretained(this)),
1196 Error(Error::kOperationFailed));
1197 EXPECT_EQ(device_->state_, Cellular::kStateDisabled);
1198 EXPECT_FALSE(device_->service_.get());
1199}
1200
Darin Petkove7c6ad32012-06-29 10:22:09 +02001201TEST_F(CellularTest, SetAllowRoaming) {
1202 EXPECT_FALSE(device_->allow_roaming_);
Prathmesh Prabhu27526f12013-03-25 19:42:18 -07001203 EXPECT_CALL(*modem_info_.mock_manager(), UpdateDevice(_));
Darin Petkove7c6ad32012-06-29 10:22:09 +02001204 Error error;
1205 device_->SetAllowRoaming(true, &error);
1206 EXPECT_TRUE(error.IsSuccess());
1207 EXPECT_TRUE(device_->allow_roaming_);
1208}
1209
mukesh agrawalfbc40d22013-06-28 00:25:13 -07001210class TestRPCTaskDelegate :
1211 public RPCTaskDelegate,
1212 public base::SupportsWeakPtr<TestRPCTaskDelegate> {
1213 public:
1214 virtual void GetLogin(std::string *user, std::string *password) {}
1215 virtual void Notify(const std::string &reason,
1216 const std::map<std::string, std::string> &dict) {}
1217};
1218
1219TEST_F(CellularTest, LinkEventUpWithPPP) {
1220 // If PPP is running, don't run DHCP as well.
1221 TestRPCTaskDelegate task_delegate;
1222 base::Callback<void(pid_t, int)> death_callback;
1223 scoped_ptr<NiceMock<MockExternalTask>> mock_task(
1224 new NiceMock<MockExternalTask>(modem_info_.control_interface(),
1225 modem_info_.glib(),
1226 task_delegate.AsWeakPtr(),
1227 death_callback));
1228 EXPECT_CALL(*mock_task, OnDelete()).Times(AnyNumber());
1229 device_->ppp_task_ = mock_task.Pass();
1230 device_->state_ = Cellular::kStateConnected;
Paul Stewart75a68b92013-10-24 10:50:27 -07001231 EXPECT_CALL(dhcp_provider_, CreateConfig(kTestDeviceName, _, _, _, _))
mukesh agrawalfbc40d22013-06-28 00:25:13 -07001232 .Times(0);
1233 EXPECT_CALL(*dhcp_config_, RequestIP()).Times(0);
1234 device_->LinkEvent(IFF_UP, 0);
1235}
1236
1237TEST_F(CellularTest, LinkEventUpWithoutPPP) {
1238 // If PPP is not running, fire up DHCP.
1239 device_->state_ = Cellular::kStateConnected;
Paul Stewart75a68b92013-10-24 10:50:27 -07001240 EXPECT_CALL(dhcp_provider_, CreateConfig(kTestDeviceName, _, _, _, _))
mukesh agrawalfbc40d22013-06-28 00:25:13 -07001241 .WillOnce(Return(dhcp_config_));
1242 EXPECT_CALL(*dhcp_config_, RequestIP());
1243 EXPECT_CALL(*dhcp_config_, ReleaseIP(_)).Times(AnyNumber());
1244 device_->LinkEvent(IFF_UP, 0);
1245}
1246
mukesh agrawalf407d592013-07-31 11:37:57 -07001247TEST_F(CellularTest, StartPPP) {
1248 const int kPID = 234;
1249 EXPECT_FALSE(device_->ppp_task_);
1250 StartPPP(kPID);
1251}
1252
1253TEST_F(CellularTest, StartPPPAlreadyStarted) {
1254 const int kPID = 234;
1255 StartPPP(kPID);
1256
1257 const int kPID2 = 235;
1258 StartPPP(kPID2);
1259}
1260
mukesh agrawal35ec8402013-07-19 14:49:08 -07001261TEST_F(CellularTest, StartPPPAfterEthernetUp) {
1262 CellularService *service(SetService());
mukesh agrawalfbc40d22013-06-28 00:25:13 -07001263 device_->state_ = Cellular::kStateLinked;
mukesh agrawal35ec8402013-07-19 14:49:08 -07001264 device_->set_ipconfig(dhcp_config_);
1265 device_->SelectService(service);
mukesh agrawalfbc40d22013-06-28 00:25:13 -07001266 EXPECT_CALL(*dhcp_config_, ReleaseIP(_))
1267 .Times(AnyNumber())
1268 .WillRepeatedly(Return(true));
mukesh agrawalf407d592013-07-31 11:37:57 -07001269 const int kPID = 234;
1270 EXPECT_FALSE(device_->ppp_task_);
1271 StartPPP(kPID);
mukesh agrawalfbc40d22013-06-28 00:25:13 -07001272 EXPECT_EQ(Cellular::kStateLinked, device_->state());
mukesh agrawalfbc40d22013-06-28 00:25:13 -07001273}
1274
mukesh agrawal3ffe52c2013-06-20 15:21:29 -07001275TEST_F(CellularTest, GetLogin) {
1276 // Doesn't crash when there is no service.
1277 string username_to_pppd;
1278 string password_to_pppd;
1279 EXPECT_FALSE(device_->service());
1280 device_->GetLogin(&username_to_pppd, &password_to_pppd);
1281
1282 // Provides expected username and password in normal case.
1283 const char kFakeUsername[] = "fake-user";
1284 const char kFakePassword[] = "fake-password";
1285 CellularService &service(*SetService());
1286 service.ppp_username_ = kFakeUsername;
1287 service.ppp_password_ = kFakePassword;
1288 device_->GetLogin(&username_to_pppd, &password_to_pppd);
1289}
1290
mukesh agrawalf407d592013-07-31 11:37:57 -07001291TEST_F(CellularTest, Notify) {
1292 // Common setup.
1293 MockPPPDeviceFactory *ppp_device_factory =
1294 MockPPPDeviceFactory::GetInstance();
mukesh agrawalfc362912013-08-06 18:10:07 -07001295 const int kPID = 91;
mukesh agrawalf407d592013-07-31 11:37:57 -07001296 device_->ppp_device_factory_ = ppp_device_factory;
1297 SetMockService();
mukesh agrawalfc362912013-08-06 18:10:07 -07001298 StartPPP(kPID);
1299
1300 const map<string, string> kEmptyArgs;
1301 device_->Notify(kPPPReasonAuthenticating, kEmptyArgs);
1302 EXPECT_TRUE(device_->is_ppp_authenticating_);
1303 device_->Notify(kPPPReasonAuthenticated, kEmptyArgs);
1304 EXPECT_FALSE(device_->is_ppp_authenticating_);
mukesh agrawalf407d592013-07-31 11:37:57 -07001305
1306 // Normal connect.
1307 const string kInterfaceName("fake-device");
1308 const int kInterfaceIndex = 1;
1309 scoped_refptr<MockPPPDevice> ppp_device;
1310 map<string, string> ppp_config;
1311 ppp_device =
1312 new MockPPPDevice(modem_info_.control_interface(),
1313 NULL, NULL, NULL, kInterfaceName, kInterfaceIndex);
1314 ppp_config[kPPPInterfaceName] = kInterfaceName;
1315 EXPECT_CALL(device_info_, GetIndex(kInterfaceName))
1316 .WillOnce(Return(kInterfaceIndex));
1317 EXPECT_CALL(device_info_, RegisterDevice(_));
1318 EXPECT_CALL(*ppp_device_factory,
1319 CreatePPPDevice(_, _, _, _, kInterfaceName, kInterfaceIndex))
1320 .WillOnce(Return(ppp_device));
1321 EXPECT_CALL(*ppp_device, SetEnabled(true));
1322 EXPECT_CALL(*ppp_device, SelectService(_));
1323 EXPECT_CALL(*ppp_device, UpdateIPConfigFromPPP(ppp_config, false));
1324 device_->Notify(kPPPReasonConnect, ppp_config);
1325 Mock::VerifyAndClearExpectations(&device_info_);
1326 Mock::VerifyAndClearExpectations(ppp_device);
1327
1328 // Re-connect on same network device: if pppd sends us multiple connect
1329 // events, we behave sanely.
1330 EXPECT_CALL(device_info_, GetIndex(kInterfaceName))
1331 .WillOnce(Return(kInterfaceIndex));
1332 EXPECT_CALL(*ppp_device, SetEnabled(true));
1333 EXPECT_CALL(*ppp_device, SelectService(_));
1334 EXPECT_CALL(*ppp_device, UpdateIPConfigFromPPP(ppp_config, false));
1335 device_->Notify(kPPPReasonConnect, ppp_config);
1336 Mock::VerifyAndClearExpectations(&device_info_);
1337 Mock::VerifyAndClearExpectations(ppp_device);
1338
1339 // Re-connect on new network device: if we still have the PPPDevice
1340 // from a prior connect, this new connect should DTRT. This is
1341 // probably an unlikely case.
1342 const string kInterfaceName2("fake-device2");
1343 const int kInterfaceIndex2 = 2;
1344 scoped_refptr<MockPPPDevice> ppp_device2;
1345 map<string, string> ppp_config2;
1346 ppp_device2 =
1347 new MockPPPDevice(modem_info_.control_interface(),
1348 NULL, NULL, NULL, kInterfaceName2, kInterfaceIndex2);
1349 ppp_config2[kPPPInterfaceName] = kInterfaceName2;
1350 EXPECT_CALL(device_info_, GetIndex(kInterfaceName2))
1351 .WillOnce(Return(kInterfaceIndex2));
1352 EXPECT_CALL(device_info_,
1353 RegisterDevice(static_cast<DeviceRefPtr>(ppp_device2)));
1354 EXPECT_CALL(*ppp_device_factory,
1355 CreatePPPDevice(_, _, _, _, kInterfaceName2, kInterfaceIndex2))
1356 .WillOnce(Return(ppp_device2));
1357 EXPECT_CALL(*ppp_device, SelectService(ServiceRefPtr(nullptr)));
1358 EXPECT_CALL(*ppp_device2, SetEnabled(true));
1359 EXPECT_CALL(*ppp_device2, SelectService(_));
1360 EXPECT_CALL(*ppp_device2, UpdateIPConfigFromPPP(ppp_config2, false));
1361 device_->Notify(kPPPReasonConnect, ppp_config2);
1362 Mock::VerifyAndClearExpectations(&device_info_);
mukesh agrawalfc362912013-08-06 18:10:07 -07001363 Mock::VerifyAndClearExpectations(ppp_device);
mukesh agrawalf407d592013-07-31 11:37:57 -07001364 Mock::VerifyAndClearExpectations(ppp_device2);
1365
mukesh agrawalfc362912013-08-06 18:10:07 -07001366 // Disconnect should report unknown failure, since we had a
1367 // Notify(kPPPReasonAuthenticated, ...).
1368 EXPECT_CALL(*ppp_device2, SetServiceFailure(Service::kFailureUnknown));
1369 device_->Notify(kPPPReasonDisconnect, kEmptyArgs);
1370 EXPECT_FALSE(device_->ppp_task_);
1371}
1372
1373TEST_F(CellularTest, PPPConnectionFailedBeforeAuth) {
1374 // Test that we properly set Service state in the case where pppd
1375 // disconnects before authenticating (as opposed to the Notify test,
1376 // where pppd disconnects after connecting).
1377 const int kPID = 52;
1378 const map<string, string> kEmptyArgs;
1379 MockCellularService *service = SetMockService();
1380 StartPPP(kPID);
mukesh agrawalee10f372013-08-14 14:07:11 -07001381
1382 ExpectDisconnectCapabilityUniversal();
mukesh agrawalfc362912013-08-06 18:10:07 -07001383 EXPECT_CALL(*service, SetFailure(Service::kFailureUnknown));
1384 device_->Notify(kPPPReasonDisconnect, kEmptyArgs);
1385 EXPECT_FALSE(device_->ppp_task_);
mukesh agrawalee10f372013-08-14 14:07:11 -07001386 VerifyDisconnect();
mukesh agrawalfc362912013-08-06 18:10:07 -07001387}
1388
1389TEST_F(CellularTest, PPPConnectionFailedDuringAuth) {
1390 // Test that we properly set Service state in the case where pppd
1391 // disconnects during authentication (as opposed to the Notify test,
1392 // where pppd disconnects after connecting).
1393 const int kPID = 52;
1394 const map<string, string> kEmptyArgs;
1395 MockCellularService *service = SetMockService();
1396 StartPPP(kPID);
mukesh agrawalee10f372013-08-14 14:07:11 -07001397
1398 ExpectDisconnectCapabilityUniversal();
mukesh agrawalfc362912013-08-06 18:10:07 -07001399 EXPECT_CALL(*service, SetFailure(Service::kFailurePPPAuth));
1400 device_->Notify(kPPPReasonAuthenticating, kEmptyArgs);
1401 device_->Notify(kPPPReasonDisconnect, kEmptyArgs);
1402 EXPECT_FALSE(device_->ppp_task_);
mukesh agrawalee10f372013-08-14 14:07:11 -07001403 VerifyDisconnect();
mukesh agrawalfc362912013-08-06 18:10:07 -07001404}
1405
1406TEST_F(CellularTest, PPPConnectionFailedAfterAuth) {
1407 // Test that we properly set Service state in the case where pppd
1408 // disconnects after authenticating, but before connecting (as
1409 // opposed to the Notify test, where pppd disconnects after
1410 // connecting).
1411 const int kPID = 52;
1412 const map<string, string> kEmptyArgs;
1413 MockCellularService *service = SetMockService();
1414 StartPPP(kPID);
mukesh agrawalee10f372013-08-14 14:07:11 -07001415
mukesh agrawalfc362912013-08-06 18:10:07 -07001416 EXPECT_CALL(*service, SetFailure(Service::kFailureUnknown));
mukesh agrawalee10f372013-08-14 14:07:11 -07001417 ExpectDisconnectCapabilityUniversal();
mukesh agrawalfc362912013-08-06 18:10:07 -07001418 device_->Notify(kPPPReasonAuthenticating, kEmptyArgs);
1419 device_->Notify(kPPPReasonAuthenticated, kEmptyArgs);
1420 device_->Notify(kPPPReasonDisconnect, kEmptyArgs);
mukesh agrawalf407d592013-07-31 11:37:57 -07001421 EXPECT_FALSE(device_->ppp_task_);
mukesh agrawalee10f372013-08-14 14:07:11 -07001422 VerifyDisconnect();
mukesh agrawalf407d592013-07-31 11:37:57 -07001423}
1424
1425TEST_F(CellularTest, OnPPPDied) {
1426 const int kPID = 1234;
1427 const int kExitStatus = 5;
mukesh agrawalee10f372013-08-14 14:07:11 -07001428 ExpectDisconnectCapabilityUniversal();
mukesh agrawalf407d592013-07-31 11:37:57 -07001429 device_->OnPPPDied(kPID, kExitStatus);
mukesh agrawalee10f372013-08-14 14:07:11 -07001430 VerifyDisconnect();
mukesh agrawalf407d592013-07-31 11:37:57 -07001431}
1432
mukesh agrawal5d851b12013-07-11 14:09:41 -07001433TEST_F(CellularTest, DropConnection) {
1434 device_->set_ipconfig(dhcp_config_);
1435 EXPECT_CALL(*dhcp_config_, ReleaseIP(_));
1436 device_->DropConnection();
1437 Mock::VerifyAndClearExpectations(dhcp_config_); // verify before dtor
1438 EXPECT_FALSE(device_->ipconfig());
1439}
1440
1441TEST_F(CellularTest, DropConnectionPPP) {
1442 scoped_refptr<MockPPPDevice> ppp_device(
1443 new MockPPPDevice(modem_info_.control_interface(),
1444 NULL, NULL, NULL, "fake_ppp0", -1));
1445 EXPECT_CALL(*ppp_device, DropConnection());
1446 device_->ppp_device_ = ppp_device;
1447 device_->DropConnection();
1448}
1449
mukesh agrawal0381f9a2013-07-11 16:41:52 -07001450TEST_F(CellularTest, ChangeServiceState) {
1451 MockCellularService *service(SetMockService());
1452 EXPECT_CALL(*service, SetState(_));
1453 EXPECT_CALL(*service, SetFailure(_));
1454 EXPECT_CALL(*service, SetFailureSilent(_));
Thieu Le43ce4d42013-10-04 16:08:55 -07001455 ON_CALL(*service, state()).WillByDefault(Return(Service::kStateUnknown));
mukesh agrawal0381f9a2013-07-11 16:41:52 -07001456
1457 // Without PPP, these should be handled by our selected_service().
1458 device_->SelectService(service);
1459 device_->SetServiceState(Service::kStateConfiguring);
1460 device_->SetServiceFailure(Service::kFailurePPPAuth);
1461 device_->SetServiceFailureSilent(Service::kFailureUnknown);
1462 Mock::VerifyAndClearExpectations(service); // before Cellular dtor
1463}
1464
1465TEST_F(CellularTest, ChangeServiceStatePPP) {
1466 MockCellularService *service(SetMockService());
1467 scoped_refptr<MockPPPDevice> ppp_device(
1468 new MockPPPDevice(modem_info_.control_interface(),
1469 NULL, NULL, NULL, "fake_ppp0", -1));
1470 EXPECT_CALL(*ppp_device, SetServiceState(_));
1471 EXPECT_CALL(*ppp_device, SetServiceFailure(_));
1472 EXPECT_CALL(*ppp_device, SetServiceFailureSilent(_));
1473 EXPECT_CALL(*service, SetState(_)).Times(0);
1474 EXPECT_CALL(*service, SetFailure(_)).Times(0);
1475 EXPECT_CALL(*service, SetFailureSilent(_)).Times(0);
1476 device_->ppp_device_ = ppp_device;
1477
1478 // With PPP, these should all be punted over to the |ppp_device|.
1479 // Note in particular that Cellular does not manipulate |service| in
1480 // this case.
1481 device_->SetServiceState(Service::kStateConfiguring);
1482 device_->SetServiceFailure(Service::kFailurePPPAuth);
1483 device_->SetServiceFailureSilent(Service::kFailureUnknown);
1484}
1485
mukesh agrawal5c8ed242013-10-04 11:59:58 -07001486TEST_F(CellularTest, StopPPPOnDisconnect) {
1487 const int kPID = 123;
mukesh agrawal5c8ed242013-10-04 11:59:58 -07001488 Error error;
mukesh agrawal28185512013-10-18 16:57:09 -07001489 StartPPP(kPID);
1490 FakeUpConnectedPPP();
1491 ExpectPPPStopped();
mukesh agrawal5c8ed242013-10-04 11:59:58 -07001492 device_->Disconnect(&error);
mukesh agrawal28185512013-10-18 16:57:09 -07001493 VerifyPPPStopped();
1494}
1495
1496TEST_F(CellularTest, StopPPPOnTermination) {
1497 const int kPID = 123;
1498 StartPPP(kPID);
1499 FakeUpConnectedPPP();
1500 ExpectPPPStopped();
1501 device_->StartTermination();
1502 VerifyPPPStopped();
1503}
1504
1505TEST_F(CellularTest, OnAfterResumeDisabledWantDisabled) {
1506 // The Device was disabled prior to resume, and the profile settings
1507 // indicate that the device should be disabled. We should leave
1508 // things alone.
1509
1510 // Initial state.
1511 mm1::MockModemProxy *mm1_proxy = SetupOnAfterResume();
1512 set_enabled_persistent(false);
1513 EXPECT_FALSE(device_->running());
1514 EXPECT_FALSE(device_->enabled_persistent());
1515 EXPECT_EQ(Cellular::kStateDisabled, device_->state_);
1516
1517 // Resume, while device is disabled.
1518 EXPECT_CALL(*mm1_proxy, Enable(_, _, _, _)).Times(0);
1519 device_->OnAfterResume();
1520 EXPECT_FALSE(device_->running());
1521 EXPECT_FALSE(device_->enabled_persistent());
1522 EXPECT_EQ(Cellular::kStateDisabled, device_->state_);
1523}
1524
1525TEST_F(CellularTest, OnAfterResumeDisableInProgressWantDisabled) {
1526 // The Device was not disabled prior to resume, but the profile
1527 // settings indicate that the device _should be_ disabled. Most
1528 // likely, we started disabling the device, but that did not
1529 // complete before we suspended. We should leave things alone.
1530
1531 // Initial state.
1532 mm1::MockModemProxy *mm1_proxy = SetupOnAfterResume();
1533 Error error;
1534 EXPECT_CALL(*mm1_proxy, Enable(true, _, _, _))
1535 .WillOnce(Invoke(this, &CellularTest::InvokeEnable));
1536 device_->SetEnabled(true);
1537 EXPECT_TRUE(device_->running());
1538 EXPECT_EQ(Cellular::kStateEnabled, device_->state_);
1539
1540 // Start disable.
1541 EXPECT_CALL(*modem_info_.mock_manager(), UpdateDevice(_));
1542 device_->SetEnabledPersistent(false, &error, ResultCallback());
1543 EXPECT_FALSE(device_->running()); // changes immediately
1544 EXPECT_FALSE(device_->enabled_persistent()); // changes immediately
1545 EXPECT_EQ(Cellular::kStateEnabled, device_->state_); // changes on completion
1546
1547 // Resume, with disable still in progress.
1548 device_->OnAfterResume();
1549 EXPECT_FALSE(device_->running());
1550 EXPECT_FALSE(device_->enabled_persistent());
1551 EXPECT_EQ(Cellular::kStateEnabled, device_->state_);
1552
1553 // Finish the disable operation.
1554 EXPECT_CALL(*mm1_proxy, Enable(false, _, _, _))
1555 .WillOnce(Invoke(this, &CellularTest::InvokeEnable));
1556 EXPECT_CALL(*mm1_proxy, SetPowerState(_, _, _, _))
1557 .WillOnce(Invoke(this, &CellularTest::InvokeSetPowerState));
1558 dispatcher_.DispatchPendingEvents();
1559 EXPECT_FALSE(device_->running());
1560 EXPECT_FALSE(device_->enabled_persistent());
1561 EXPECT_EQ(Cellular::kStateDisabled, device_->state_);
1562}
1563
1564TEST_F(CellularTest, OnAfterResumeDisableQueuedWantEnabled) {
1565 // The Device was not disabled prior to resume, and the profile
1566 // settings indicate that the device should be enabled. In
1567 // particular, we went into suspend before we actually processed the
1568 // task queued by CellularCapabilityUniversal::StopModem.
1569 //
1570 // This is unlikely, and a case where we fail to do the right thing.
1571 // The tests exists to document this corner case, which we get wrong.
1572
1573 // Initial state.
1574 mm1::MockModemProxy *mm1_proxy = SetupOnAfterResume();
1575 EXPECT_CALL(*mm1_proxy, Enable(true, _, _, _))
1576 .WillOnce(Invoke(this, &CellularTest::InvokeEnable));
1577 device_->SetEnabled(true);
1578 EXPECT_TRUE(device_->running());
1579 EXPECT_TRUE(device_->enabled_persistent());
1580 EXPECT_EQ(Cellular::kStateEnabled, device_->state_);
1581
1582 // Start disable.
1583 device_->SetEnabled(false);
1584 EXPECT_FALSE(device_->running()); // changes immediately
1585 EXPECT_TRUE(device_->enabled_persistent()); // no change
1586 EXPECT_EQ(Cellular::kStateEnabled, device_->state_); // changes on completion
1587
1588 // Refresh proxies, since CellularCapabilityUniversal::StartModem wants
1589 // new proxies. Also, stash away references for later.
1590 PopulateProxies();
1591 SetCommonOnAfterResumeExpectations();
1592 mm1_proxy = mm1_proxy_.get();
1593 auto dbus_properties_proxy = dbus_properties_proxy_.get();
1594
1595 // Resume, with disable still in progress.
1596 EXPECT_CALL(*mm1_proxy, Enable(true, _, _, _))
1597 .WillOnce(Invoke(this, &CellularTest::InvokeEnableReturningWrongState));
1598 EXPECT_EQ(Cellular::kStateEnabled, device_->state_); // disable still pending
1599 device_->OnAfterResume();
1600 EXPECT_TRUE(device_->running()); // changes immediately
1601 EXPECT_TRUE(device_->enabled_persistent()); // no change
1602 EXPECT_EQ(Cellular::kStateDisabled, device_->state_); // by OnAfterResume
1603
1604 // Set up state that we need.
1605 DBusPropertiesMap modem_properties;
1606 DBus::Variant modem_state;
1607 modem_state.writer().append_int32(Cellular::kModemStateDisabled);
1608 modem_properties = DBusPropertiesMap{{MM_MODEM_PROPERTY_STATE, modem_state}};
1609
1610 // Let the disable complete.
1611 EXPECT_CALL(*mm1_proxy, Enable(false, _, _, _))
1612 .WillOnce(Invoke(this, &CellularTest::InvokeEnable));
1613 EXPECT_CALL(*mm1_proxy, SetPowerState(_, _, _, _))
1614 .WillOnce(Invoke(this, &CellularTest::InvokeSetPowerState));
1615 EXPECT_CALL(*dbus_properties_proxy, GetAll(_))
1616 .WillRepeatedly(Return(modem_properties));
1617 dispatcher_.DispatchPendingEvents();
1618 EXPECT_TRUE(device_->running()); // last changed by OnAfterResume
1619 EXPECT_TRUE(device_->enabled_persistent()); // last changed by OnAfterResume
1620 EXPECT_EQ(Cellular::kStateDisabled, device_->state_);
1621
1622 // There's nothing queued up to restart the modem. Even though we
1623 // want to be running, we're stuck in the disabled state.
1624 dispatcher_.DispatchPendingEvents();
1625 EXPECT_TRUE(device_->running());
1626 EXPECT_TRUE(device_->enabled_persistent());
1627 EXPECT_EQ(Cellular::kStateDisabled, device_->state_);
1628}
1629
1630TEST_F(CellularTest, OnAfterResumePowerDownInProgressWantEnabled) {
1631 // The Device was not fully disabled prior to resume, and the
1632 // profile settings indicate that the device should be enabled. In
1633 // this case, we have disabled the device, but are waiting for the
1634 // power-down (switch to low power) to complete.
1635 //
1636 // This test emulates the behavior of the Huawei E303 dongle, when
1637 // Manager::kTerminationActionsTimeoutMilliseconds is 9500
1638 // msec. (The dongle takes 10-11 seconds to go through the whole
1639 // disable, power-down sequence).
1640 //
1641 // Eventually, the power-down would complete, and the device would
1642 // be stuck in the disabled state. To counter-act that,
1643 // OnAfterResume tries to enable the device now, even though the
1644 // device is currently enabled.
1645
1646 // Initial state.
1647 mm1::MockModemProxy *mm1_proxy = SetupOnAfterResume();
1648 EXPECT_CALL(*mm1_proxy, Enable(true, _, _, _))
1649 .WillOnce(Invoke(this, &CellularTest::InvokeEnable));
1650 device_->SetEnabled(true);
1651 EXPECT_TRUE(device_->running());
1652 EXPECT_TRUE(device_->enabled_persistent());
1653 EXPECT_EQ(Cellular::kStateEnabled, device_->state_);
1654
1655 // Start disable.
1656 ResultCallback modem_proxy_enable_callback;
1657 EXPECT_CALL(*mm1_proxy, Enable(false, _, _, _))
1658 .WillOnce(SaveArg<2>(&modem_proxy_enable_callback));
1659 device_->SetEnabled(false);
1660 dispatcher_.DispatchPendingEvents(); // SetEnabled yields a deferred task
1661 EXPECT_FALSE(device_->running()); // changes immediately
1662 EXPECT_TRUE(device_->enabled_persistent()); // no change
1663 EXPECT_EQ(Cellular::kStateEnabled, device_->state_); // changes on completion
1664
1665 // Let the disable complete. That will trigger power-down.
1666 //
1667 // Note that, unlike for mm1_proxy->Enable, we don't save the
1668 // callback for mm1_proxy->SetPowerState. We expect the callback not
1669 // to be executed, as explained in the comment about having a fresh
1670 // proxy OnAfterResume, below.
1671 Error error;
1672 ASSERT_TRUE(error.IsSuccess());
1673 EXPECT_CALL(*mm1_proxy, SetPowerState(MM_MODEM_POWER_STATE_LOW, _, _, _));
1674 modem_proxy_enable_callback.Run(error);
1675
1676 // No response to power-down yet. It probably completed while the host
1677 // was asleep, and so the reply from the modem was lost.
1678
1679 // Refresh proxies, since CellularCapabilityUniversal::StartModem wants
1680 // new proxies. Also, stash away references for later.
1681 PopulateProxies();
1682 SetCommonOnAfterResumeExpectations();
1683 auto new_mm1_proxy = mm1_proxy_.get();
1684 auto dbus_properties_proxy = dbus_properties_proxy_.get();
1685
1686 // Resume.
1687 ResultCallback new_callback;
1688 EXPECT_EQ(Cellular::kStateEnabled, device_->state_); // disable still pending
1689 EXPECT_CALL(*new_mm1_proxy, Enable(true, _, _, _))
1690 .WillOnce(SaveArg<2>(&modem_proxy_enable_callback));
1691 device_->OnAfterResume();
1692 EXPECT_TRUE(device_->running()); // changes immediately
1693 EXPECT_TRUE(device_->enabled_persistent()); // no change
1694 EXPECT_EQ(Cellular::kStateDisabled, device_->state_); // by OnAfterResume
1695
1696 // We should have a fresh proxy OnAfterResume. Otherwise, we may get
1697 // confused when the SetPowerState call completes (either naturally,
1698 // or via a time-out from dbus-c++).
1699 //
1700 // The pointers must differ, because the new proxy is constructed
1701 // before the old one is destructed.
1702 EXPECT_FALSE(new_mm1_proxy == mm1_proxy);
1703
1704 // Set up state that we need.
1705 DBusPropertiesMap modem_properties;
1706 DBus::Variant modem_state;
1707 modem_state.writer().append_int32(Cellular::kModemStateEnabled);
1708 modem_properties = DBusPropertiesMap{{MM_MODEM_PROPERTY_STATE, modem_state}};
1709
1710 // Let the enable complete.
1711 ASSERT_TRUE(error.IsSuccess());
1712 EXPECT_CALL(*dbus_properties_proxy, GetAll(_))
1713 .WillRepeatedly(Return(modem_properties));
1714 ASSERT_TRUE(!modem_proxy_enable_callback.is_null());
1715 modem_proxy_enable_callback.Run(error);
1716 EXPECT_TRUE(device_->running());
1717 EXPECT_TRUE(device_->enabled_persistent());
1718 EXPECT_EQ(Cellular::kStateEnabled, device_->state_);
1719}
1720
1721TEST_F(CellularTest, OnAfterResumeDisabledWantEnabled) {
1722 // This is the ideal case. The disable process completed before
1723 // going into suspend.
1724 mm1::MockModemProxy *mm1_proxy = SetupOnAfterResume();
1725 EXPECT_FALSE(device_->running());
1726 EXPECT_TRUE(device_->enabled_persistent());
1727 EXPECT_EQ(Cellular::kStateDisabled, device_->state_);
1728
1729 // Resume.
1730 ResultCallback modem_proxy_enable_callback;
1731 EXPECT_CALL(*mm1_proxy, Enable(true, _, _, _))
1732 .WillOnce(SaveArg<2>(&modem_proxy_enable_callback));
1733 device_->OnAfterResume();
1734
1735 // Complete enable.
1736 Error error;
1737 ASSERT_TRUE(error.IsSuccess());
1738 modem_proxy_enable_callback.Run(error);
1739 EXPECT_TRUE(device_->running());
1740 EXPECT_TRUE(device_->enabled_persistent());
1741 EXPECT_EQ(Cellular::kStateEnabled, device_->state_);
mukesh agrawal5c8ed242013-10-04 11:59:58 -07001742}
1743
mukesh agrawalbebf1b82013-04-23 15:06:33 -07001744// Custom property setters should return false, and make no changes, if
1745// the new value is the same as the old value.
1746TEST_F(CellularTest, CustomSetterNoopChange) {
1747 Error error;
1748 EXPECT_FALSE(device_->allow_roaming_);
1749 EXPECT_FALSE(device_->SetAllowRoaming(false, &error));
1750 EXPECT_TRUE(error.IsSuccess());
1751}
1752
Prathmesh Prabhu49ffffd2014-01-09 18:28:55 -08001753TEST_F(CellularTest, ScanImmediateFailure) {
1754 Error error;
1755
1756 device_->set_found_networks(kTestNetworksCellular);
1757 EXPECT_CALL(*gsm_network_proxy_, Scan(&error, _, _))
1758 .WillOnce(Invoke(this, &CellularTest::InvokeScanFailed));
1759 EXPECT_FALSE(device_->scanning_);
1760 // |InitProxies| must be called before calling any functions on the
1761 // Capability*, to set up the modem proxies.
1762 // Warning: The test loses all references to the proxies when |InitProxies| is
1763 // called.
1764 GetCapabilityGSM()->InitProxies();
1765 device_->Scan(Device::kFullScan, &error, "");
1766 EXPECT_TRUE(error.IsFailure());
1767 EXPECT_FALSE(device_->scanning_);
1768 EXPECT_EQ(kTestNetworksCellular, device_->found_networks());
1769}
1770
1771TEST_F(CellularTest, ScanAsynchronousFailure) {
1772 Error error;
1773 ScanResultsCallback results_callback;
1774
1775 device_->set_found_networks(kTestNetworksCellular);
1776 EXPECT_CALL(*gsm_network_proxy_, Scan(&error, _, _))
1777 .WillOnce(DoAll(Invoke(this, &CellularTest::InvokeScanInitiated),
1778 SaveArg<1>(&results_callback)));
1779 EXPECT_FALSE(device_->scanning_);
1780 // |InitProxies| must be called before calling any functions on the
1781 // Capability*, to set up the modem proxies.
1782 // Warning: The test loses all references to the proxies when |InitProxies| is
1783 // called.
1784 GetCapabilityGSM()->InitProxies();
1785 device_->Scan(Device::kFullScan, &error, "");
1786 EXPECT_TRUE(error.IsOngoing());
1787 EXPECT_TRUE(device_->scanning_);
1788
1789 // Asynchronously fail the scan.
1790 error.Populate(Error::kOperationFailed);
1791 results_callback.Run(kTestNetworksGSM, error);
1792 EXPECT_FALSE(device_->scanning_);
1793 EXPECT_TRUE(device_->found_networks().empty());
1794}
1795
1796TEST_F(CellularTest, ScanSuccess) {
1797 Error error;
1798 ScanResultsCallback results_callback;
1799
1800 device_->clear_found_networks();
1801 EXPECT_CALL(*gsm_network_proxy_, Scan(&error, _, _))
1802 .WillOnce(DoAll(Invoke(this, &CellularTest::InvokeScanInitiated),
1803 SaveArg<1>(&results_callback)));
1804 EXPECT_FALSE(device_->scanning_);
1805 // |InitProxies| must be called before calling any functions on the
1806 // Capability*, to set up the modem proxies.
1807 // Warning: The test loses all references to the proxies when |InitProxies| is
1808 // called.
1809 GetCapabilityGSM()->InitProxies();
1810 device_->Scan(Device::kFullScan, &error, "");
1811 EXPECT_TRUE(error.IsOngoing());
1812 EXPECT_TRUE(device_->scanning_);
1813
1814 // Successfully complete the scan.
1815 const GSMScanResults gsm_results{};
1816 error.Populate(Error::kSuccess);
1817 results_callback.Run(kTestNetworksGSM, error);
1818 EXPECT_FALSE(device_->scanning_);
1819 EXPECT_EQ(kTestNetworksCellular, device_->found_networks());
1820}
1821
Ben Chan539ab022014-02-03 16:34:57 -08001822TEST_F(CellularTest, EstablishLinkDHCP) {
1823 scoped_ptr<CellularBearer> bearer(
1824 new CellularBearer(&proxy_factory_, "", ""));
1825 bearer->set_ipv4_config_method(IPConfig::kMethodDHCP);
1826 SetCapabilityUniversalActiveBearer(bearer.Pass());
1827 device_->state_ = Cellular::kStateConnected;
1828
1829 MockCellularService *service = SetMockService();
1830 ON_CALL(*service, state()).WillByDefault(Return(Service::kStateUnknown));
1831
1832 EXPECT_CALL(device_info_, GetFlags(device_->interface_index(), _))
1833 .WillOnce(DoAll(SetArgumentPointee<1>(IFF_UP), Return(true)));
1834 EXPECT_CALL(dhcp_provider_, CreateConfig(kTestDeviceName, _, _, _, _))
1835 .WillOnce(Return(dhcp_config_));
1836 EXPECT_CALL(*dhcp_config_, RequestIP()).WillOnce(Return(true));
1837 EXPECT_CALL(*service, SetState(Service::kStateConfiguring));
1838 device_->EstablishLink();
1839 EXPECT_EQ(service, device_->selected_service());
1840 Mock::VerifyAndClearExpectations(service); // before Cellular dtor
1841}
1842
1843TEST_F(CellularTest, EstablishLinkPPP) {
1844 scoped_ptr<CellularBearer> bearer(
1845 new CellularBearer(&proxy_factory_, "", ""));
1846 bearer->set_ipv4_config_method(IPConfig::kMethodPPP);
1847 SetCapabilityUniversalActiveBearer(bearer.Pass());
1848 device_->state_ = Cellular::kStateConnected;
1849
1850 const int kPID = 123;
1851 MockGLib &mock_glib(*dynamic_cast<MockGLib *>(modem_info_.glib()));
1852 EXPECT_CALL(mock_glib, ChildWatchAdd(kPID, _, _));
1853 EXPECT_CALL(mock_glib, SpawnAsync(_, _, _, _, _, _, _, _))
1854 .WillOnce(DoAll(SetArgumentPointee<6>(kPID), Return(true)));
1855 device_->EstablishLink();
1856 EXPECT_FALSE(device_->ipconfig()); // No DHCP client.
1857 EXPECT_FALSE(device_->selected_service());
1858 EXPECT_FALSE(device_->is_ppp_authenticating_);
1859 EXPECT_TRUE(device_->ppp_task_);
1860}
1861
1862TEST_F(CellularTest, EstablishLinkStatic) {
1863 IPAddress::Family kAddressFamily = IPAddress::kFamilyIPv4;
1864 const char kAddress[] = "10.0.0.1";
1865 const char kGateway[] = "10.0.0.254";
1866 const int32 kSubnetPrefix = 16;
1867 const char *const kDNS[] = {"10.0.0.2", "8.8.4.4", "8.8.8.8"};
1868
1869 scoped_ptr<IPConfig::Properties> ipconfig_properties(
1870 new IPConfig::Properties);
1871 ipconfig_properties->address_family = kAddressFamily;
1872 ipconfig_properties->address = kAddress;
1873 ipconfig_properties->gateway = kGateway;
1874 ipconfig_properties->subnet_prefix = kSubnetPrefix;
1875 ipconfig_properties->dns_servers = vector<string>{kDNS[0], kDNS[1], kDNS[2]};
1876
1877 scoped_ptr<CellularBearer> bearer(
1878 new CellularBearer(&proxy_factory_, "", ""));
1879 bearer->set_ipv4_config_method(IPConfig::kMethodStatic);
1880 bearer->set_ipv4_config_properties(ipconfig_properties.Pass());
1881 SetCapabilityUniversalActiveBearer(bearer.Pass());
1882 device_->state_ = Cellular::kStateConnected;
1883
1884 MockCellularService *service = SetMockService();
1885 ON_CALL(*service, state()).WillByDefault(Return(Service::kStateUnknown));
1886
1887 EXPECT_CALL(device_info_, GetFlags(device_->interface_index(), _))
1888 .WillOnce(DoAll(SetArgumentPointee<1>(IFF_UP), Return(true)));
1889 EXPECT_CALL(*service, SetState(Service::kStateConfiguring));
1890 device_->EstablishLink();
1891 EXPECT_EQ(service, device_->selected_service());
1892 ASSERT_TRUE(device_->ipconfig());
1893 EXPECT_EQ(kAddressFamily, device_->ipconfig()->properties().address_family);
1894 EXPECT_EQ(kAddress, device_->ipconfig()->properties().address);
1895 EXPECT_EQ(kGateway, device_->ipconfig()->properties().gateway);
1896 EXPECT_EQ(kSubnetPrefix, device_->ipconfig()->properties().subnet_prefix);
1897 ASSERT_EQ(3, device_->ipconfig()->properties().dns_servers.size());
1898 EXPECT_EQ(kDNS[0], device_->ipconfig()->properties().dns_servers[0]);
1899 EXPECT_EQ(kDNS[1], device_->ipconfig()->properties().dns_servers[1]);
1900 EXPECT_EQ(kDNS[2], device_->ipconfig()->properties().dns_servers[2]);
1901 Mock::VerifyAndClearExpectations(service); // before Cellular dtor
1902}
1903
Chris Masoneb925cc82011-06-22 15:39:57 -07001904} // namespace shill