blob: ce741915e77d71c77d888b6c38c2c08ebfac1362 [file] [log] [blame]
Thieu Le3426c8f2012-01-11 17:35:11 -08001// Copyright (c) 2012 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>
Darin Petkovbec79a22011-08-01 14:47:17 -070013#include <mm/mm-modem.h>
Darin Petkov137884a2011-10-26 18:52:47 +020014#include <mobile_provider.h>
Chris Masoneb925cc82011-06-22 15:39:57 -070015
Darin Petkovae0c64e2011-11-15 15:50:27 +010016#include "shill/cellular_capability_cdma.h"
Jason Glasgow82f9ab32012-04-04 14:27:19 -040017#include "shill/cellular_capability_classic.h"
Darin Petkov184c54e2011-11-15 12:44:39 +010018#include "shill/cellular_capability_gsm.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"
Thieu Le37b90032012-05-15 15:18:41 -070022#include "shill/mock_cellular_service.h"
Chris Masone2ae797d2011-08-23 20:41:00 -070023#include "shill/mock_device_info.h"
Darin Petkov77cb6812011-08-15 16:19:41 -070024#include "shill/mock_dhcp_config.h"
25#include "shill/mock_dhcp_provider.h"
Chris Masone2ae797d2011-08-23 20:41:00 -070026#include "shill/mock_manager.h"
Thieu Le3426c8f2012-01-11 17:35:11 -080027#include "shill/mock_metrics.h"
Darin Petkovbec79a22011-08-01 14:47:17 -070028#include "shill/mock_modem_cdma_proxy.h"
Darin Petkov975b5e72011-08-30 11:48:08 -070029#include "shill/mock_modem_gsm_card_proxy.h"
Darin Petkova1e0a1c2011-08-25 15:08:33 -070030#include "shill/mock_modem_gsm_network_proxy.h"
Darin Petkove9d12e02011-07-27 15:09:37 -070031#include "shill/mock_modem_proxy.h"
Darin Petkove604f702011-07-28 15:51:17 -070032#include "shill/mock_modem_simple_proxy.h"
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070033#include "shill/mock_rtnl_handler.h"
Darin Petkov3335b372011-08-22 11:05:32 -070034#include "shill/nice_mock_control.h"
Chris Masoneb925cc82011-06-22 15:39:57 -070035#include "shill/property_store_unittest.h"
Darin Petkove9d12e02011-07-27 15:09:37 -070036#include "shill/proxy_factory.h"
Chris Masoneb925cc82011-06-22 15:39:57 -070037
Eric Shienbrood9a245532012-03-07 14:20:39 -050038using base::Bind;
39using base::Unretained;
Darin Petkovc0865312011-09-16 15:31:20 -070040using std::map;
Chris Masoneb925cc82011-06-22 15:39:57 -070041using std::string;
Eric Shienbrood7fce52c2012-04-13 19:11:02 -040042using std::vector;
Darin Petkovbec79a22011-08-01 14:47:17 -070043using testing::_;
Chris Masoneb9c00592011-10-06 13:10:39 -070044using testing::AnyNumber;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050045using testing::Invoke;
Darin Petkov0828f5f2011-08-11 10:18:52 -070046using testing::NiceMock;
Darin Petkove604f702011-07-28 15:51:17 -070047using testing::Return;
Darin Petkovbec79a22011-08-01 14:47:17 -070048using testing::SetArgumentPointee;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050049using testing::Unused;
Chris Masoneb925cc82011-06-22 15:39:57 -070050
51namespace shill {
52
Eric Shienbrood9a245532012-03-07 14:20:39 -050053MATCHER(IsSuccess, "") {
54 return arg.IsSuccess();
55}
56
Gary Morainbaeefdf2012-04-30 14:53:35 -070057MATCHER(IsFailure, "") {
58 return arg.IsFailure();
59}
60
Darin Petkov0828f5f2011-08-11 10:18:52 -070061class CellularPropertyTest : public PropertyStoreTest {
62 public:
63 CellularPropertyTest()
Chris Masone2176a882011-09-14 22:29:15 -070064 : device_(new Cellular(control_interface(),
Darin Petkov0828f5f2011-08-11 10:18:52 -070065 NULL,
66 NULL,
Thieu Le3426c8f2012-01-11 17:35:11 -080067 NULL,
Darin Petkove9d12e02011-07-27 15:09:37 -070068 "usb0",
Chris Masone626719f2011-08-18 16:58:48 -070069 "00:01:02:03:04:05",
Darin Petkove9d12e02011-07-27 15:09:37 -070070 3,
Ben Chan3ecdf822012-08-06 12:29:23 -070071 Cellular::kTypeCDMA,
Darin Petkov0828f5f2011-08-11 10:18:52 -070072 "",
Darin Petkov137884a2011-10-26 18:52:47 +020073 "",
Jason Glasgowa585fc32012-06-06 11:04:09 -040074 "",
Ben Chan3ecdf822012-08-06 12:29:23 -070075 NULL,
76 ProxyFactory::GetInstance())) {}
Darin Petkov0828f5f2011-08-11 10:18:52 -070077 virtual ~CellularPropertyTest() {}
Darin Petkove9d12e02011-07-27 15:09:37 -070078
Chris Masoneb925cc82011-06-22 15:39:57 -070079 protected:
Darin Petkov0828f5f2011-08-11 10:18:52 -070080 DeviceRefPtr device_;
Chris Masoneb925cc82011-06-22 15:39:57 -070081};
82
Darin Petkov0828f5f2011-08-11 10:18:52 -070083TEST_F(CellularPropertyTest, Contains) {
mukesh agrawalde29fa82011-09-16 16:16:36 -070084 EXPECT_TRUE(device_->store().Contains(flimflam::kNameProperty));
85 EXPECT_FALSE(device_->store().Contains(""));
Chris Masoneb925cc82011-06-22 15:39:57 -070086}
87
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -080088TEST_F(CellularPropertyTest, SetProperty) {
Chris Masonea8a2c252011-06-27 22:16:30 -070089 {
90 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -080091 EXPECT_TRUE(DBusAdaptor::SetProperty(
mukesh agrawalde29fa82011-09-16 16:16:36 -070092 device_->mutable_store(),
Chris Masonea8a2c252011-06-27 22:16:30 -070093 flimflam::kCellularAllowRoamingProperty,
94 PropertyStoreTest::kBoolV,
95 &error));
96 }
Chris Masonea8a2c252011-06-27 22:16:30 -070097 // Ensure that attempting to write a R/O property returns InvalidArgs error.
98 {
99 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800100 EXPECT_FALSE(DBusAdaptor::SetProperty(device_->mutable_store(),
101 flimflam::kAddressProperty,
102 PropertyStoreTest::kStringV,
103 &error));
Chris Masone9d779932011-08-25 16:33:41 -0700104 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -0700105 }
106 {
107 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800108 EXPECT_FALSE(DBusAdaptor::SetProperty(device_->mutable_store(),
109 flimflam::kCarrierProperty,
110 PropertyStoreTest::kStringV,
111 &error));
Chris Masone9d779932011-08-25 16:33:41 -0700112 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -0700113 }
Chris Masoneb925cc82011-06-22 15:39:57 -0700114}
115
Darin Petkov0828f5f2011-08-11 10:18:52 -0700116class CellularTest : public testing::Test {
117 public:
118 CellularTest()
Thieu Le3426c8f2012-01-11 17:35:11 -0800119 : manager_(&control_interface_, &dispatcher_, &metrics_, &glib_),
120 device_info_(&control_interface_, &dispatcher_, &metrics_, &manager_),
Darin Petkov77cb6812011-08-15 16:19:41 -0700121 dhcp_config_(new MockDHCPConfig(&control_interface_,
Paul Stewartd408fdf2012-05-07 17:15:57 -0700122 kTestDeviceName)),
Ben Chan3ecdf822012-08-06 12:29:23 -0700123 create_gsm_card_proxy_from_factory_(false),
124 proxy_(new MockModemProxy()),
125 simple_proxy_(new MockModemSimpleProxy()),
126 cdma_proxy_(new MockModemCDMAProxy()),
127 gsm_card_proxy_(new MockModemGSMCardProxy()),
128 gsm_network_proxy_(new MockModemGSMNetworkProxy()),
129 proxy_factory_(this),
130 provider_db_(NULL),
Darin Petkov0828f5f2011-08-11 10:18:52 -0700131 device_(new Cellular(&control_interface_,
132 &dispatcher_,
Thieu Le3426c8f2012-01-11 17:35:11 -0800133 &metrics_,
Darin Petkov0828f5f2011-08-11 10:18:52 -0700134 &manager_,
Darin Petkov77cb6812011-08-15 16:19:41 -0700135 kTestDeviceName,
Chris Masone626719f2011-08-18 16:58:48 -0700136 kTestDeviceAddress,
Darin Petkov0828f5f2011-08-11 10:18:52 -0700137 3,
138 Cellular::kTypeGSM,
139 kDBusOwner,
Jason Glasgowa585fc32012-06-06 11:04:09 -0400140 kDBusService,
Darin Petkov137884a2011-10-26 18:52:47 +0200141 kDBusPath,
Ben Chan3ecdf822012-08-06 12:29:23 -0700142 NULL,
143 &proxy_factory_)) {}
Darin Petkov137884a2011-10-26 18:52:47 +0200144
145 virtual ~CellularTest() {
146 mobile_provider_close_db(provider_db_);
147 provider_db_ = NULL;
148 }
Darin Petkov0828f5f2011-08-11 10:18:52 -0700149
150 virtual void SetUp() {
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700151 static_cast<Device *>(device_)->rtnl_handler_ = &rtnl_handler_;
Darin Petkov77cb6812011-08-15 16:19:41 -0700152 device_->set_dhcp_provider(&dhcp_provider_);
Chris Masone2ae797d2011-08-23 20:41:00 -0700153 EXPECT_CALL(manager_, device_info()).WillRepeatedly(Return(&device_info_));
Chris Masoneb9c00592011-10-06 13:10:39 -0700154 EXPECT_CALL(manager_, DeregisterService(_)).Times(AnyNumber());
Darin Petkov0828f5f2011-08-11 10:18:52 -0700155 }
156
157 virtual void TearDown() {
Eric Shienbrood9a245532012-03-07 14:20:39 -0500158 device_->DestroyIPConfig();
Darin Petkovfb0625e2012-01-16 13:05:56 +0100159 device_->state_ = Cellular::kStateDisabled;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500160 device_->capability_->ReleaseProxies();
Darin Petkov77cb6812011-08-15 16:19:41 -0700161 device_->set_dhcp_provider(NULL);
Darin Petkov0828f5f2011-08-11 10:18:52 -0700162 }
163
Eric Shienbrood9a245532012-03-07 14:20:39 -0500164 void InvokeEnable(bool enable, Error *error,
165 const ResultCallback &callback, int timeout) {
166 callback.Run(Error());
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500167 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500168 void InvokeGetSignalQuality(Error *error,
169 const SignalQualityCallback &callback,
170 int timeout) {
171 callback.Run(kStrength, Error());
172 }
173 void InvokeGetModemStatus(Error *error,
174 const DBusPropertyMapCallback &callback,
175 int timeout) {
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500176 DBusPropertiesMap props;
177 props["carrier"].writer().append_string(kTestCarrier);
178 props["unknown-property"].writer().append_string("irrelevant-value");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500179 callback.Run(props, Error());
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500180 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500181 void InvokeGetModemInfo(Error *error, const ModemInfoCallback &callback,
182 int timeout) {
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500183 static const char kManufacturer[] = "Company";
184 static const char kModelID[] = "Gobi 2000";
185 static const char kHWRev[] = "A00B1234";
186 ModemHardwareInfo info;
187 info._1 = kManufacturer;
188 info._2 = kModelID;
189 info._3 = kHWRev;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500190 callback.Run(info, Error());
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500191 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500192 void InvokeGetRegistrationState1X(Error *error,
193 const RegistrationStateCallback &callback,
194 int timeout) {
195 callback.Run(MM_MODEM_CDMA_REGISTRATION_STATE_HOME,
196 MM_MODEM_CDMA_REGISTRATION_STATE_UNKNOWN,
197 Error());
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500198 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500199 void InvokeGetIMEI(Error *error, const GSMIdentifierCallback &callback,
200 int timeout) {
201 callback.Run(kIMEI, Error());
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500202 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500203 void InvokeGetIMSI(Error *error, const GSMIdentifierCallback &callback,
204 int timeout) {
205 callback.Run(kIMSI, Error());
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500206 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500207 void InvokeGetMSISDN(Error *error, const GSMIdentifierCallback &callback,
208 int timeout) {
209 callback.Run(kMSISDN, Error());
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500210 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500211 void InvokeGetSPN(Error *error, const GSMIdentifierCallback &callback,
212 int timeout) {
213 callback.Run(kTestCarrier, Error());
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500214 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500215 void InvokeGetRegistrationInfo(Error *error,
216 const RegistrationInfoCallback &callback,
217 int timeout) {
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500218 static const char kNetworkID[] = "22803";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500219 callback.Run(MM_MODEM_GSM_NETWORK_REG_STATUS_ROAMING,
220 kNetworkID, kTestCarrier, Error());
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500221 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500222 void InvokeRegister(const string &network_id,
223 Error *error,
224 const ResultCallback &callback,
225 int timeout) {
226 callback.Run(Error());
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500227 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500228 void InvokeGetRegistrationState(Error *error,
229 const RegistrationStateCallback &callback,
230 int timeout) {
231 callback.Run(MM_MODEM_CDMA_REGISTRATION_STATE_REGISTERED,
232 MM_MODEM_CDMA_REGISTRATION_STATE_HOME,
233 Error());
234 }
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400235 void InvokeGetRegistrationStateUnregistered(
236 Error *error,
237 const RegistrationStateCallback &callback,
238 int timeout) {
239 callback.Run(MM_MODEM_CDMA_REGISTRATION_STATE_UNKNOWN,
240 MM_MODEM_CDMA_REGISTRATION_STATE_UNKNOWN,
241 Error());
242 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500243 void InvokeConnect(DBusPropertiesMap props, Error *error,
244 const ResultCallback &callback, int timeout) {
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400245 EXPECT_EQ(Service::kStateAssociating, device_->service_->state());
Eric Shienbrood9a245532012-03-07 14:20:39 -0500246 callback.Run(Error());
247 }
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400248 void InvokeConnectFail(DBusPropertiesMap props, Error *error,
Thieu Leb5954a22012-05-18 10:37:34 -0700249 const ResultCallback &callback, int timeout) {
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400250 EXPECT_EQ(Service::kStateAssociating, device_->service_->state());
251 callback.Run(Error(Error::kNotOnHomeNetwork));
252 }
Thieu Leb5954a22012-05-18 10:37:34 -0700253 void InvokeConnectFailNoService(DBusPropertiesMap props, Error *error,
254 const ResultCallback &callback, int timeout) {
255 device_->service_ = NULL;
256 callback.Run(Error(Error::kNotOnHomeNetwork));
257 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500258 void InvokeDisconnect(Error *error, const ResultCallback &callback,
259 int timeout) {
260 if (!callback.is_null())
261 callback.Run(Error());
262 }
263
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400264 void ExpectCdmaStartModem(string network_technology) {
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400265 if (!device_->IsUnderlyingDeviceEnabled())
266 EXPECT_CALL(*proxy_,
267 Enable(true, _, _, CellularCapability::kTimeoutEnable))
268 .WillOnce(Invoke(this, &CellularTest::InvokeEnable));
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400269 EXPECT_CALL(*simple_proxy_,
270 GetModemStatus(_, _, CellularCapability::kTimeoutDefault))
271 .WillOnce(Invoke(this, &CellularTest::InvokeGetModemStatus));
272 EXPECT_CALL(*proxy_,
273 GetModemInfo(_, _, CellularCapability::kTimeoutDefault))
274 .WillOnce(Invoke(this, &CellularTest::InvokeGetModemInfo));
275 if (network_technology == flimflam::kNetworkTechnology1Xrtt)
276 EXPECT_CALL(*cdma_proxy_, GetRegistrationState(NULL, _, _))
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400277 .WillOnce(Invoke(this, &CellularTest::InvokeGetRegistrationState1X));
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400278 else
279 EXPECT_CALL(*cdma_proxy_, GetRegistrationState(NULL, _, _))
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400280 .WillOnce(Invoke(this, &CellularTest::InvokeGetRegistrationState));
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400281 EXPECT_CALL(*cdma_proxy_, GetSignalQuality(NULL, _, _))
282 .Times(2)
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400283 .WillRepeatedly(Invoke(this, &CellularTest::InvokeGetSignalQuality));
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400284 EXPECT_CALL(*this, TestCallback(IsSuccess()));
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400285 EXPECT_CALL(manager_, RegisterService(_));
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400286 }
287
Eric Shienbrood9a245532012-03-07 14:20:39 -0500288 MOCK_METHOD1(TestCallback, void(const Error &error));
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500289
Darin Petkov0828f5f2011-08-11 10:18:52 -0700290 protected:
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500291 static const char kTestDeviceName[];
292 static const char kTestDeviceAddress[];
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500293 static const char kDBusOwner[];
Jason Glasgowa585fc32012-06-06 11:04:09 -0400294 static const char kDBusService[];
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500295 static const char kDBusPath[];
296 static const char kTestCarrier[];
297 static const char kMEID[];
298 static const char kIMEI[];
299 static const char kIMSI[];
300 static const char kMSISDN[];
301 static const char kTestMobileProviderDBPath[];
Eric Shienbrood9a245532012-03-07 14:20:39 -0500302 static const int kStrength;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500303
Darin Petkov0828f5f2011-08-11 10:18:52 -0700304 class TestProxyFactory : public ProxyFactory {
305 public:
Paul Stewart7355ce12011-09-02 10:47:01 -0700306 explicit TestProxyFactory(CellularTest *test) : test_(test) {}
Darin Petkov0828f5f2011-08-11 10:18:52 -0700307
mukesh agrawal1830fa12011-09-26 14:31:40 -0700308 virtual ModemProxyInterface *CreateModemProxy(
mukesh agrawal1830fa12011-09-26 14:31:40 -0700309 const string &/*path*/,
310 const string &/*service*/) {
Darin Petkov0828f5f2011-08-11 10:18:52 -0700311 return test_->proxy_.release();
312 }
313
314 virtual ModemSimpleProxyInterface *CreateModemSimpleProxy(
mukesh agrawal1830fa12011-09-26 14:31:40 -0700315 const string &/*path*/,
316 const string &/*service*/) {
Darin Petkov0828f5f2011-08-11 10:18:52 -0700317 return test_->simple_proxy_.release();
318 }
319
320 virtual ModemCDMAProxyInterface *CreateModemCDMAProxy(
mukesh agrawal1830fa12011-09-26 14:31:40 -0700321 const string &/*path*/,
322 const string &/*service*/) {
Darin Petkov0828f5f2011-08-11 10:18:52 -0700323 return test_->cdma_proxy_.release();
324 }
325
Darin Petkov975b5e72011-08-30 11:48:08 -0700326 virtual ModemGSMCardProxyInterface *CreateModemGSMCardProxy(
mukesh agrawal1830fa12011-09-26 14:31:40 -0700327 const string &/*path*/,
328 const string &/*service*/) {
Ben Chan3ecdf822012-08-06 12:29:23 -0700329 // TODO(benchan): This code conditionally returns a NULL pointer to avoid
330 // CellularCapabilityGSM::InitProperties (and thus
331 // CellularCapabilityGSM::GetIMSI) from being called during the
332 // construction. Remove this workaround after refactoring the tests.
333 return test_->create_gsm_card_proxy_from_factory_ ?
334 test_->gsm_card_proxy_.release() : NULL;
Darin Petkov975b5e72011-08-30 11:48:08 -0700335 }
336
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700337 virtual ModemGSMNetworkProxyInterface *CreateModemGSMNetworkProxy(
mukesh agrawal1830fa12011-09-26 14:31:40 -0700338 const string &/*path*/,
339 const string &/*service*/) {
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700340 return test_->gsm_network_proxy_.release();
341 }
342
Darin Petkov0828f5f2011-08-11 10:18:52 -0700343 private:
344 CellularTest *test_;
345 };
Darin Petkov0828f5f2011-08-11 10:18:52 -0700346 void StartRTNLHandler();
347 void StopRTNLHandler();
348
Ben Chan3ecdf822012-08-06 12:29:23 -0700349 void AllowCreateGSMCardProxyFromFactory() {
350 create_gsm_card_proxy_from_factory_ = true;
351 }
352
Darin Petkov721ac932011-11-16 15:43:09 +0100353 void SetCellularType(Cellular::Type type) {
Ben Chan3ecdf822012-08-06 12:29:23 -0700354 device_->InitCapability(type);
Darin Petkov721ac932011-11-16 15:43:09 +0100355 }
356
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400357 CellularCapabilityClassic *GetCapabilityClassic() {
358 return dynamic_cast<CellularCapabilityClassic *>(
359 device_->capability_.get());
360 }
361
Darin Petkovae0c64e2011-11-15 15:50:27 +0100362 CellularCapabilityCDMA *GetCapabilityCDMA() {
363 return dynamic_cast<CellularCapabilityCDMA *>(device_->capability_.get());
364 }
365
366 CellularCapabilityGSM *GetCapabilityGSM() {
367 return dynamic_cast<CellularCapabilityGSM *>(device_->capability_.get());
368 }
369
Darin Petkov3335b372011-08-22 11:05:32 -0700370 NiceMockControl control_interface_;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700371 EventDispatcher dispatcher_;
Thieu Le3426c8f2012-01-11 17:35:11 -0800372 MockMetrics metrics_;
Darin Petkov0828f5f2011-08-11 10:18:52 -0700373 MockGLib glib_;
Chris Masone2ae797d2011-08-23 20:41:00 -0700374 MockManager manager_;
375 MockDeviceInfo device_info_;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700376 NiceMock<MockRTNLHandler> rtnl_handler_;
377
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500378 MockDHCPProvider dhcp_provider_;
379 scoped_refptr<MockDHCPConfig> dhcp_config_;
380
Ben Chan3ecdf822012-08-06 12:29:23 -0700381 bool create_gsm_card_proxy_from_factory_;
Darin Petkov0828f5f2011-08-11 10:18:52 -0700382 scoped_ptr<MockModemProxy> proxy_;
383 scoped_ptr<MockModemSimpleProxy> simple_proxy_;
384 scoped_ptr<MockModemCDMAProxy> cdma_proxy_;
Darin Petkov975b5e72011-08-30 11:48:08 -0700385 scoped_ptr<MockModemGSMCardProxy> gsm_card_proxy_;
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700386 scoped_ptr<MockModemGSMNetworkProxy> gsm_network_proxy_;
Darin Petkov0828f5f2011-08-11 10:18:52 -0700387 TestProxyFactory proxy_factory_;
Darin Petkov137884a2011-10-26 18:52:47 +0200388 mobile_provider_db *provider_db_;
Ben Chan3ecdf822012-08-06 12:29:23 -0700389 CellularRefPtr device_;
Darin Petkov0828f5f2011-08-11 10:18:52 -0700390};
391
Darin Petkov77cb6812011-08-15 16:19:41 -0700392const char CellularTest::kTestDeviceName[] = "usb0";
Chris Masone626719f2011-08-18 16:58:48 -0700393const char CellularTest::kTestDeviceAddress[] = "00:01:02:03:04:05";
Darin Petkov0828f5f2011-08-11 10:18:52 -0700394const char CellularTest::kDBusOwner[] = ":1.19";
Jason Glasgowa585fc32012-06-06 11:04:09 -0400395const char CellularTest::kDBusService[] = "org.chromium.ModemManager";
Darin Petkov0828f5f2011-08-11 10:18:52 -0700396const char CellularTest::kDBusPath[] = "/org/chromium/ModemManager/Gobi/0";
Darin Petkov51489002011-08-18 13:13:20 -0700397const char CellularTest::kTestCarrier[] = "The Cellular Carrier";
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500398const char CellularTest::kMEID[] = "01234567EF8901";
Darin Petkov975b5e72011-08-30 11:48:08 -0700399const char CellularTest::kIMEI[] = "987654321098765";
400const char CellularTest::kIMSI[] = "123456789012345";
401const char CellularTest::kMSISDN[] = "12345678901";
Darin Petkov137884a2011-10-26 18:52:47 +0200402const char CellularTest::kTestMobileProviderDBPath[] =
403 "provider_db_unittest.bfd";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500404const int CellularTest::kStrength = 90;
Darin Petkov0828f5f2011-08-11 10:18:52 -0700405
Darin Petkove9d12e02011-07-27 15:09:37 -0700406TEST_F(CellularTest, GetStateString) {
Darin Petkovcc044422011-08-17 13:30:06 -0700407 EXPECT_EQ("CellularStateDisabled",
408 device_->GetStateString(Cellular::kStateDisabled));
409 EXPECT_EQ("CellularStateEnabled",
410 device_->GetStateString(Cellular::kStateEnabled));
411 EXPECT_EQ("CellularStateRegistered",
412 device_->GetStateString(Cellular::kStateRegistered));
413 EXPECT_EQ("CellularStateConnected",
414 device_->GetStateString(Cellular::kStateConnected));
415 EXPECT_EQ("CellularStateLinked",
416 device_->GetStateString(Cellular::kStateLinked));
Darin Petkove9d12e02011-07-27 15:09:37 -0700417}
418
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700419TEST_F(CellularTest, StartCDMARegister) {
Darin Petkov721ac932011-11-16 15:43:09 +0100420 SetCellularType(Cellular::kTypeCDMA);
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400421 ExpectCdmaStartModem(flimflam::kNetworkTechnology1Xrtt);
Darin Petkov975b5e72011-08-30 11:48:08 -0700422 EXPECT_CALL(*cdma_proxy_, MEID()).WillOnce(Return(kMEID));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500423 Error error;
424 device_->Start(&error, Bind(&CellularTest::TestCallback, Unretained(this)));
Darin Petkov0828f5f2011-08-11 10:18:52 -0700425 dispatcher_.DispatchPendingEvents();
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400426 EXPECT_EQ(kMEID, GetCapabilityClassic()->meid_);
427 EXPECT_EQ(kTestCarrier, GetCapabilityClassic()->carrier_);
Darin Petkovbac96002011-08-09 13:22:00 -0700428 EXPECT_EQ(Cellular::kStateRegistered, device_->state_);
Darin Petkovd2045802011-08-23 11:09:25 -0700429 ASSERT_TRUE(device_->service_.get());
430 EXPECT_EQ(flimflam::kNetworkTechnology1Xrtt,
Darin Petkovb72cf402011-11-22 14:51:39 +0100431 device_->service_->network_technology());
Darin Petkov22b72bf2011-08-29 14:01:20 -0700432 EXPECT_EQ(kStrength, device_->service_->strength());
Darin Petkovd2045802011-08-23 11:09:25 -0700433 EXPECT_EQ(flimflam::kRoamingStateHome, device_->service_->roaming_state());
Darin Petkovbac96002011-08-09 13:22:00 -0700434}
435
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700436TEST_F(CellularTest, StartGSMRegister) {
Darin Petkov137884a2011-10-26 18:52:47 +0200437 provider_db_ = mobile_provider_open_db(kTestMobileProviderDBPath);
438 ASSERT_TRUE(provider_db_);
439 device_->provider_db_ = provider_db_;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500440 EXPECT_CALL(*proxy_, Enable(true, _, _, CellularCapability::kTimeoutEnable))
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500441 .WillOnce(Invoke(this, &CellularTest::InvokeEnable));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500442 EXPECT_CALL(*gsm_card_proxy_,
443 GetIMEI(_, _, CellularCapability::kTimeoutDefault))
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500444 .WillOnce(Invoke(this, &CellularTest::InvokeGetIMEI));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500445 EXPECT_CALL(*gsm_card_proxy_,
446 GetIMSI(_, _, CellularCapability::kTimeoutDefault))
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500447 .WillOnce(Invoke(this, &CellularTest::InvokeGetIMSI));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500448 EXPECT_CALL(*gsm_card_proxy_,
449 GetSPN(_, _, CellularCapability::kTimeoutDefault))
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500450 .WillOnce(Invoke(this, &CellularTest::InvokeGetSPN));
451 EXPECT_CALL(*gsm_card_proxy_,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500452 GetMSISDN(_, _, CellularCapability::kTimeoutDefault))
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500453 .WillOnce(Invoke(this, &CellularTest::InvokeGetMSISDN));
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700454 EXPECT_CALL(*gsm_network_proxy_, AccessTechnology())
455 .WillOnce(Return(MM_MODEM_GSM_ACCESS_TECH_EDGE));
Darin Petkov63138a92012-02-06 14:09:15 +0100456 EXPECT_CALL(*gsm_card_proxy_, EnabledFacilityLocks())
457 .WillOnce(Return(MM_MODEM_GSM_FACILITY_SIM));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500458 EXPECT_CALL(*proxy_, GetModemInfo(_, _, CellularCapability::kTimeoutDefault))
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500459 .WillOnce(Invoke(this, &CellularTest::InvokeGetModemInfo));
Darin Petkov137884a2011-10-26 18:52:47 +0200460 static const char kNetworkID[] = "22803";
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500461 EXPECT_CALL(*gsm_network_proxy_,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500462 GetRegistrationInfo(_, _, CellularCapability::kTimeoutDefault))
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500463 .WillOnce(Invoke(this, &CellularTest::InvokeGetRegistrationInfo));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500464 EXPECT_CALL(*gsm_network_proxy_, GetSignalQuality(NULL, _, _))
465 .Times(2)
466 .WillRepeatedly(Invoke(this,
467 &CellularTest::InvokeGetSignalQuality));
468 EXPECT_CALL(*this, TestCallback(IsSuccess()));
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400469 EXPECT_CALL(manager_, RegisterService(_));
Ben Chan3ecdf822012-08-06 12:29:23 -0700470 AllowCreateGSMCardProxyFromFactory();
471
Eric Shienbrood9a245532012-03-07 14:20:39 -0500472 Error error;
473 device_->Start(&error, Bind(&CellularTest::TestCallback, Unretained(this)));
474 EXPECT_TRUE(error.IsSuccess());
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700475 dispatcher_.DispatchPendingEvents();
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400476 EXPECT_EQ(kIMEI, GetCapabilityGSM()->imei_);
477 EXPECT_EQ(kIMSI, GetCapabilityGSM()->imsi_);
Darin Petkov63138a92012-02-06 14:09:15 +0100478 EXPECT_EQ(kTestCarrier, GetCapabilityGSM()->spn_);
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400479 EXPECT_EQ(kMSISDN, GetCapabilityGSM()->mdn_);
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700480 EXPECT_EQ(Cellular::kStateRegistered, device_->state_);
481 ASSERT_TRUE(device_->service_.get());
482 EXPECT_EQ(flimflam::kNetworkTechnologyEdge,
Darin Petkovb72cf402011-11-22 14:51:39 +0100483 device_->service_->network_technology());
Darin Petkov63138a92012-02-06 14:09:15 +0100484 EXPECT_TRUE(GetCapabilityGSM()->sim_lock_status_.enabled);
Darin Petkov22b72bf2011-08-29 14:01:20 -0700485 EXPECT_EQ(kStrength, device_->service_->strength());
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700486 EXPECT_EQ(flimflam::kRoamingStateRoaming, device_->service_->roaming_state());
Darin Petkov137884a2011-10-26 18:52:47 +0200487 EXPECT_EQ(kNetworkID, device_->service_->serving_operator().GetCode());
488 EXPECT_EQ("Orange", device_->service_->serving_operator().GetName());
489 EXPECT_EQ("ch", device_->service_->serving_operator().GetCountry());
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700490}
491
Darin Petkovbac96002011-08-09 13:22:00 -0700492TEST_F(CellularTest, StartConnected) {
Chris Masone2ae797d2011-08-23 20:41:00 -0700493 EXPECT_CALL(device_info_, GetFlags(device_->interface_index(), _))
494 .WillOnce(Return(true));
Darin Petkov721ac932011-11-16 15:43:09 +0100495 SetCellularType(Cellular::kTypeCDMA);
Darin Petkovbac96002011-08-09 13:22:00 -0700496 device_->set_modem_state(Cellular::kModemStateConnected);
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400497 GetCapabilityClassic()->meid_ = kMEID;
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400498 ExpectCdmaStartModem(flimflam::kNetworkTechnologyEvdo);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500499 Error error;
500 device_->Start(&error, Bind(&CellularTest::TestCallback, Unretained(this)));
501 EXPECT_TRUE(error.IsSuccess());
Darin Petkov0828f5f2011-08-11 10:18:52 -0700502 dispatcher_.DispatchPendingEvents();
Darin Petkovbac96002011-08-09 13:22:00 -0700503 EXPECT_EQ(Cellular::kStateConnected, device_->state_);
504}
505
Darin Petkov0828f5f2011-08-11 10:18:52 -0700506TEST_F(CellularTest, StartLinked) {
Chris Masone2ae797d2011-08-23 20:41:00 -0700507 EXPECT_CALL(device_info_, GetFlags(device_->interface_index(), _))
508 .WillOnce(DoAll(SetArgumentPointee<1>(IFF_UP), Return(true)));
Darin Petkov721ac932011-11-16 15:43:09 +0100509 SetCellularType(Cellular::kTypeCDMA);
Darin Petkov0828f5f2011-08-11 10:18:52 -0700510 device_->set_modem_state(Cellular::kModemStateConnected);
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400511 GetCapabilityClassic()->meid_ = kMEID;
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400512 ExpectCdmaStartModem(flimflam::kNetworkTechnologyEvdo);
Paul Stewartd408fdf2012-05-07 17:15:57 -0700513 EXPECT_CALL(dhcp_provider_, CreateConfig(kTestDeviceName, _, _, _))
Darin Petkov77cb6812011-08-15 16:19:41 -0700514 .WillOnce(Return(dhcp_config_));
515 EXPECT_CALL(*dhcp_config_, RequestIP()).WillOnce(Return(true));
Darin Petkov60b8c3b2011-08-25 11:03:20 -0700516 EXPECT_CALL(manager_, UpdateService(_)).Times(2);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500517 Error error;
518 device_->Start(&error, Bind(&CellularTest::TestCallback, Unretained(this)));
519 EXPECT_TRUE(error.IsSuccess());
Darin Petkov0828f5f2011-08-11 10:18:52 -0700520 dispatcher_.DispatchPendingEvents();
521 EXPECT_EQ(Cellular::kStateLinked, device_->state_);
Darin Petkov60b8c3b2011-08-25 11:03:20 -0700522 EXPECT_EQ(Service::kStateConfiguring, device_->service_->state());
523 device_->SelectService(NULL);
Darin Petkov0828f5f2011-08-11 10:18:52 -0700524}
525
Darin Petkov3335b372011-08-22 11:05:32 -0700526TEST_F(CellularTest, CreateService) {
Darin Petkov721ac932011-11-16 15:43:09 +0100527 SetCellularType(Cellular::kTypeCDMA);
Darin Petkov3335b372011-08-22 11:05:32 -0700528 static const char kPaymentURL[] = "https://payment.url";
529 static const char kUsageURL[] = "https://usage.url";
Darin Petkov3335b372011-08-22 11:05:32 -0700530 device_->home_provider_.SetName(kTestCarrier);
Darin Petkov381928f2012-02-02 23:00:12 +0100531 GetCapabilityCDMA()->olp_.SetURL(kPaymentURL);
Darin Petkovae0c64e2011-11-15 15:50:27 +0100532 GetCapabilityCDMA()->usage_url_ = kUsageURL;
Darin Petkov3335b372011-08-22 11:05:32 -0700533 device_->CreateService();
534 ASSERT_TRUE(device_->service_.get());
Darin Petkov381928f2012-02-02 23:00:12 +0100535 EXPECT_EQ(kPaymentURL, device_->service_->olp().GetURL());
Darin Petkov3335b372011-08-22 11:05:32 -0700536 EXPECT_EQ(kUsageURL, device_->service_->usage_url());
537 EXPECT_EQ(kTestCarrier, device_->service_->serving_operator().GetName());
538}
539
Darin Petkovc5f56562011-08-06 16:40:05 -0700540namespace {
541
542MATCHER(ContainsPhoneNumber, "") {
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400543 return ContainsKey(arg,
544 CellularCapabilityClassic::kConnectPropertyPhoneNumber);
Darin Petkovc5f56562011-08-06 16:40:05 -0700545}
546
Eric Shienbrood9a245532012-03-07 14:20:39 -0500547} // namespace
Darin Petkovc5f56562011-08-06 16:40:05 -0700548
549TEST_F(CellularTest, Connect) {
Darin Petkov4d6d9412011-08-24 13:19:54 -0700550 Error error;
Chris Masone2ae797d2011-08-23 20:41:00 -0700551 EXPECT_CALL(device_info_, GetFlags(device_->interface_index(), _))
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700552 .Times(2)
553 .WillRepeatedly(Return(true));
Darin Petkovc5f56562011-08-06 16:40:05 -0700554 device_->state_ = Cellular::kStateConnected;
Darin Petkov4d6d9412011-08-24 13:19:54 -0700555 device_->Connect(&error);
556 EXPECT_EQ(Error::kAlreadyConnected, error.type());
557 error.Populate(Error::kSuccess);
Darin Petkovc5f56562011-08-06 16:40:05 -0700558
Darin Petkov0828f5f2011-08-11 10:18:52 -0700559 device_->state_ = Cellular::kStateLinked;
Darin Petkov4d6d9412011-08-24 13:19:54 -0700560 device_->Connect(&error);
561 EXPECT_EQ(Error::kAlreadyConnected, error.type());
Darin Petkov0828f5f2011-08-11 10:18:52 -0700562
Darin Petkovc5f56562011-08-06 16:40:05 -0700563 device_->state_ = Cellular::kStateRegistered;
Darin Petkovd2045802011-08-23 11:09:25 -0700564 device_->service_ = new CellularService(
Thieu Le3426c8f2012-01-11 17:35:11 -0800565 &control_interface_, &dispatcher_, &metrics_, &manager_, device_);
Darin Petkovd2045802011-08-23 11:09:25 -0700566
Jason Glasgow7b461df2012-05-01 16:38:45 -0400567 device_->allow_roaming_ = false;
Darin Petkovb72cf402011-11-22 14:51:39 +0100568 device_->service_->roaming_state_ = flimflam::kRoamingStateRoaming;
Darin Petkov4d6d9412011-08-24 13:19:54 -0700569 device_->Connect(&error);
Darin Petkov4d6d9412011-08-24 13:19:54 -0700570 EXPECT_EQ(Error::kNotOnHomeNetwork, error.type());
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500571
Darin Petkov4d6d9412011-08-24 13:19:54 -0700572 error.Populate(Error::kSuccess);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500573 EXPECT_CALL(*simple_proxy_,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500574 Connect(ContainsPhoneNumber(), _, _,
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500575 CellularCapability::kTimeoutConnect))
576 .Times(2)
577 .WillRepeatedly(Invoke(this, &CellularTest::InvokeConnect));
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400578 GetCapabilityClassic()->simple_proxy_.reset(simple_proxy_.release());
Darin Petkovb72cf402011-11-22 14:51:39 +0100579 device_->service_->roaming_state_ = flimflam::kRoamingStateHome;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500580 device_->state_ = Cellular::kStateRegistered;
Darin Petkov4d6d9412011-08-24 13:19:54 -0700581 device_->Connect(&error);
582 EXPECT_TRUE(error.IsSuccess());
Darin Petkovc5f56562011-08-06 16:40:05 -0700583 dispatcher_.DispatchPendingEvents();
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500584 EXPECT_EQ(Cellular::kStateConnected, device_->state_);
585
Jason Glasgow7b461df2012-05-01 16:38:45 -0400586 device_->allow_roaming_ = true;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500587 device_->service_->roaming_state_ = flimflam::kRoamingStateRoaming;
588 device_->state_ = Cellular::kStateRegistered;
589 device_->Connect(&error);
590 EXPECT_TRUE(error.IsSuccess());
591 dispatcher_.DispatchPendingEvents();
592 EXPECT_EQ(Cellular::kStateConnected, device_->state_);
Darin Petkovc5f56562011-08-06 16:40:05 -0700593}
594
Eric Shienbrood9a245532012-03-07 14:20:39 -0500595TEST_F(CellularTest, Disconnect) {
596 Error error;
597 device_->state_ = Cellular::kStateRegistered;
598 device_->Disconnect(&error);
599 EXPECT_EQ(Error::kNotConnected, error.type());
600 error.Reset();
601
Darin Petkovfb0625e2012-01-16 13:05:56 +0100602 device_->state_ = Cellular::kStateConnected;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500603 EXPECT_CALL(*proxy_,
604 Disconnect(_, _, CellularCapability::kTimeoutDefault))
605 .WillOnce(Invoke(this, &CellularTest::InvokeDisconnect));
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400606 GetCapabilityClassic()->proxy_.reset(proxy_.release());
Eric Shienbrood9a245532012-03-07 14:20:39 -0500607 device_->Disconnect(&error);
608 EXPECT_TRUE(error.IsSuccess());
609 EXPECT_EQ(Cellular::kStateRegistered, device_->state_);
Darin Petkovfb0625e2012-01-16 13:05:56 +0100610}
611
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400612TEST_F(CellularTest, ConnectFailure) {
613 SetCellularType(Cellular::kTypeCDMA);
614 device_->state_ = Cellular::kStateRegistered;
615 device_->service_ = new CellularService(
616 &control_interface_, &dispatcher_, &metrics_, &manager_, device_);
617 ASSERT_EQ(Service::kStateIdle, device_->service_->state());
618 EXPECT_CALL(*simple_proxy_,
619 Connect(_, _, _, CellularCapability::kTimeoutConnect))
620 .WillOnce(Invoke(this, &CellularTest::InvokeConnectFail));
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400621 GetCapabilityClassic()->simple_proxy_.reset(simple_proxy_.release());
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400622 Error error;
623 device_->Connect(&error);
624 EXPECT_EQ(Service::kStateFailure, device_->service_->state());
625}
626
Thieu Leb5954a22012-05-18 10:37:34 -0700627TEST_F(CellularTest, ConnectFailureNoService) {
628 // Make sure we don't crash if the connect failed and there is no
629 // CellularService object. This can happen if the modem is enabled and
630 // then quick disabled.
631 SetCellularType(Cellular::kTypeCDMA);
632 device_->state_ = Cellular::kStateRegistered;
633 device_->service_ = new CellularService(
634 &control_interface_, &dispatcher_, &metrics_, &manager_, device_);
635 EXPECT_CALL(
636 *simple_proxy_,
637 Connect(_, _, _, CellularCapability::kTimeoutConnect))
638 .WillOnce(Invoke(this, &CellularTest::InvokeConnectFailNoService));
639 EXPECT_CALL(manager_, UpdateService(_));
640 GetCapabilityClassic()->simple_proxy_.reset(simple_proxy_.release());
641 Error error;
642 device_->Connect(&error);
643}
644
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400645TEST_F(CellularTest, ModemStateChangeEnable) {
646 EXPECT_CALL(*simple_proxy_,
647 GetModemStatus(_, _, CellularCapability::kTimeoutDefault))
648 .WillOnce(Invoke(this, &CellularTest::InvokeGetModemStatus));
649 EXPECT_CALL(*cdma_proxy_, MEID()).WillOnce(Return(kMEID));
650 EXPECT_CALL(*proxy_,
651 GetModemInfo(_, _, CellularCapability::kTimeoutDefault))
652 .WillOnce(Invoke(this, &CellularTest::InvokeGetModemInfo));
653 EXPECT_CALL(*cdma_proxy_, GetRegistrationState(NULL, _, _))
654 .WillOnce(Invoke(this,
655 &CellularTest::InvokeGetRegistrationStateUnregistered));
656 EXPECT_CALL(*cdma_proxy_, GetSignalQuality(NULL, _, _))
657 .WillOnce(Invoke(this, &CellularTest::InvokeGetSignalQuality));
658 EXPECT_CALL(manager_, UpdateEnabledTechnologies());
659 device_->state_ = Cellular::kStateDisabled;
660 device_->set_modem_state(Cellular::kModemStateDisabled);
661 SetCellularType(Cellular::kTypeCDMA);
662
663 DBusPropertiesMap props;
664 props[CellularCapabilityClassic::kModemPropertyEnabled].writer().
665 append_bool(true);
666 device_->OnDBusPropertiesChanged(MM_MODEM_INTERFACE, props, vector<string>());
667 dispatcher_.DispatchPendingEvents();
668
669 EXPECT_EQ(Cellular::kModemStateEnabled, device_->modem_state());
670 EXPECT_EQ(Cellular::kStateEnabled, device_->state());
671 EXPECT_TRUE(device_->enabled());
672}
673
674TEST_F(CellularTest, ModemStateChangeDisable) {
675 EXPECT_CALL(*proxy_,
676 Disconnect(_, _, CellularCapability::kTimeoutDefault))
677 .WillOnce(Invoke(this, &CellularTest::InvokeDisconnect));
678 EXPECT_CALL(*proxy_,
Thieu Lec8d2d962012-05-15 14:31:18 -0700679 Enable(false, _, _, CellularCapability::kTimeoutEnable))
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400680 .WillOnce(Invoke(this, &CellularTest::InvokeEnable));
681 EXPECT_CALL(manager_, UpdateEnabledTechnologies());
682 device_->enabled_ = true;
683 device_->enabled_pending_ = true;
684 device_->state_ = Cellular::kStateEnabled;
685 device_->set_modem_state(Cellular::kModemStateEnabled);
686 SetCellularType(Cellular::kTypeCDMA);
687 GetCapabilityClassic()->InitProxies();
688
689 GetCapabilityClassic()->OnModemStateChangedSignal(kModemClassicStateEnabled,
690 kModemClassicStateDisabled,
691 0);
692 dispatcher_.DispatchPendingEvents();
693
694 EXPECT_EQ(Cellular::kModemStateDisabled, device_->modem_state());
695 EXPECT_EQ(Cellular::kStateDisabled, device_->state());
696 EXPECT_FALSE(device_->enabled());
697}
698
Thieu Led0012052012-07-25 16:09:09 -0700699TEST_F(CellularTest, ModemStateChangeStaleConnected) {
700 // Test to make sure that we ignore stale modem Connected state transitions.
701 // When a modem is asked to connect and before the connect completes, the
702 // modem is disabled, it may send a stale Connected state transition after
703 // it has been disabled.
704 device_->state_ = Cellular::kStateDisabled;
705 device_->OnModemStateChanged(Cellular::kModemStateEnabling,
706 Cellular::kModemStateConnected,
707 0);
708 EXPECT_EQ(Cellular::kStateDisabled, device_->state());
709}
710
711TEST_F(CellularTest, ModemStateChangeValidConnected) {
712 device_->state_ = Cellular::kStateEnabled;
713 device_->service_ = new CellularService(
714 &control_interface_, &dispatcher_, &metrics_, &manager_, device_);
715 device_->OnModemStateChanged(Cellular::kModemStateConnecting,
716 Cellular::kModemStateConnected,
717 0);
718 EXPECT_EQ(Cellular::kStateConnected, device_->state());
719}
720
Thieu Le37b90032012-05-15 15:18:41 -0700721TEST_F(CellularTest, StartModemCallback) {
Gary Morainbaeefdf2012-04-30 14:53:35 -0700722 EXPECT_CALL(*this, TestCallback(IsSuccess()));
723 EXPECT_EQ(device_->state_, Cellular::kStateDisabled);
Thieu Le37b90032012-05-15 15:18:41 -0700724 device_->StartModemCallback(Bind(&CellularTest::TestCallback,
725 Unretained(this)),
726 Error(Error::kSuccess));
Gary Morainbaeefdf2012-04-30 14:53:35 -0700727 EXPECT_EQ(device_->state_, Cellular::kStateEnabled);
728}
729
Thieu Le37b90032012-05-15 15:18:41 -0700730TEST_F(CellularTest, StartModemCallbackFail) {
Gary Morainbaeefdf2012-04-30 14:53:35 -0700731 EXPECT_CALL(*this, TestCallback(IsFailure()));
732 EXPECT_EQ(device_->state_, Cellular::kStateDisabled);
Thieu Le37b90032012-05-15 15:18:41 -0700733 device_->StartModemCallback(Bind(&CellularTest::TestCallback,
734 Unretained(this)),
735 Error(Error::kOperationFailed));
Gary Morainbaeefdf2012-04-30 14:53:35 -0700736 EXPECT_EQ(device_->state_, Cellular::kStateDisabled);
737}
738
Thieu Le37b90032012-05-15 15:18:41 -0700739TEST_F(CellularTest, StopModemCallback) {
740 EXPECT_CALL(*this, TestCallback(IsSuccess()));
741 device_->service_ = new MockCellularService(&control_interface_,
742 &dispatcher_,
743 &metrics_,
744 &manager_,
745 device_);
746 device_->StopModemCallback(Bind(&CellularTest::TestCallback,
747 Unretained(this)),
748 Error(Error::kSuccess));
749 EXPECT_EQ(device_->state_, Cellular::kStateDisabled);
750 EXPECT_FALSE(device_->service_.get());
751}
752
753TEST_F(CellularTest, StopModemCallbackFail) {
754 EXPECT_CALL(*this, TestCallback(IsFailure()));
755 device_->service_ = new MockCellularService(&control_interface_,
756 &dispatcher_,
757 &metrics_,
758 &manager_,
759 device_);
760 device_->StopModemCallback(Bind(&CellularTest::TestCallback,
761 Unretained(this)),
762 Error(Error::kOperationFailed));
763 EXPECT_EQ(device_->state_, Cellular::kStateDisabled);
764 EXPECT_FALSE(device_->service_.get());
765}
766
Gary Moraina9fb3252012-05-31 12:05:31 -0700767TEST_F(CellularTest, ConnectAddsTerminationAction) {
768 Error error;
769 EXPECT_CALL(*simple_proxy_,
770 Connect(ContainsPhoneNumber(), _, _,
771 CellularCapability::kTimeoutConnect))
772 .WillRepeatedly(Invoke(this, &CellularTest::InvokeConnect));
773 EXPECT_CALL(*proxy_,
774 Disconnect(_, _, CellularCapability::kTimeoutDefault))
775 .WillOnce(Invoke(this, &CellularTest::InvokeDisconnect));
776
777 // TestCallback() will be called when the termination actions complete. This
778 // verifies that the actions were registered, invoked, and report their
779 // status.
780 EXPECT_CALL(*this, TestCallback(IsSuccess())).Times(2);
781
782 device_->service_ = new CellularService(
783 &control_interface_, &dispatcher_, &metrics_, &manager_, device_);
784 GetCapabilityClassic()->proxy_.reset(proxy_.release());
785 GetCapabilityClassic()->simple_proxy_.reset(simple_proxy_.release());
786 device_->state_ = Cellular::kStateRegistered;
787 device_->Connect(&error);
788 EXPECT_TRUE(error.IsSuccess());
789 dispatcher_.DispatchPendingEvents();
790 EXPECT_EQ(Cellular::kStateConnected, device_->state_);
791
792 // If the action of establishing a connection registered a termination action
793 // with the manager, then running the termination action will result in a
794 // disconnect.
795 manager_.RunTerminationActions(
796 0, Bind(&CellularTest::TestCallback, Unretained(this)));
797 EXPECT_EQ(Cellular::kStateRegistered, device_->state_);
798 dispatcher_.DispatchPendingEvents();
799
800 // Verify that the termination action has been removed from the manager.
801 // Running the registered termination actions again should result in
802 // TestCallback being called with success because there are no registered
803 // termination actions.. If the termination action is not removed, then
804 // TestCallback will be called with kOperationTimeout.
805 manager_.RunTerminationActions(
806 0, Bind(&CellularTest::TestCallback, Unretained(this)));
807 dispatcher_.DispatchPendingEvents();
808}
809
Darin Petkove7c6ad32012-06-29 10:22:09 +0200810TEST_F(CellularTest, SetAllowRoaming) {
811 EXPECT_FALSE(device_->allow_roaming_);
812 EXPECT_CALL(manager_, UpdateDevice(_));
813 Error error;
814 device_->SetAllowRoaming(true, &error);
815 EXPECT_TRUE(error.IsSuccess());
816 EXPECT_TRUE(device_->allow_roaming_);
817}
818
Chris Masoneb925cc82011-06-22 15:39:57 -0700819} // namespace shill