1 //
2 // Copyright (C) 2013 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include "shill/cellular/cellular_capability_universal.h"
18 
19 #include <string>
20 #include <tuple>
21 #include <vector>
22 
23 #include <base/bind.h>
24 #include <base/strings/string_util.h>
25 #include <base/strings/stringprintf.h>
26 #if defined(__ANDROID__)
27 #include <dbus/service_constants.h>
28 #else
29 #include <chromeos/dbus/service_constants.h>
30 #endif  // __ANDROID__
31 #include <ModemManager/ModemManager.h>
32 
33 #include "shill/cellular/cellular.h"
34 #include "shill/cellular/cellular_bearer.h"
35 #include "shill/cellular/cellular_service.h"
36 #include "shill/cellular/mock_cellular.h"
37 #include "shill/cellular/mock_cellular_service.h"
38 #include "shill/cellular/mock_mm1_modem_modem3gpp_proxy.h"
39 #include "shill/cellular/mock_mm1_modem_modemcdma_proxy.h"
40 #include "shill/cellular/mock_mm1_modem_proxy.h"
41 #include "shill/cellular/mock_mm1_modem_simple_proxy.h"
42 #include "shill/cellular/mock_mm1_sim_proxy.h"
43 #include "shill/cellular/mock_mobile_operator_info.h"
44 #include "shill/cellular/mock_modem_info.h"
45 #include "shill/error.h"
46 #include "shill/mock_adaptors.h"
47 #include "shill/mock_control.h"
48 #include "shill/mock_dbus_properties_proxy.h"
49 #include "shill/mock_event_dispatcher.h"
50 #include "shill/mock_pending_activation_store.h"
51 #include "shill/mock_profile.h"
52 #include "shill/net/mock_rtnl_handler.h"
53 #include "shill/test_event_dispatcher.h"
54 #include "shill/testing.h"
55 
56 using base::Bind;
57 using base::StringPrintf;
58 using base::Unretained;
59 using std::string;
60 using std::unique_ptr;
61 using std::vector;
62 using testing::AnyNumber;
63 using testing::InSequence;
64 using testing::Invoke;
65 using testing::InvokeWithoutArgs;
66 using testing::Mock;
67 using testing::NiceMock;
68 using testing::Return;
69 using testing::ReturnRef;
70 using testing::SaveArg;
71 using testing::_;
72 
73 namespace shill {
74 
75 MATCHER_P(HasApn, expected_apn, "") {
76   return arg.ContainsString(CellularCapabilityUniversal::kConnectApn) &&
77       expected_apn == arg.GetString(CellularCapabilityUniversal::kConnectApn);
78 }
79 
80 class CellularCapabilityUniversalTest : public testing::TestWithParam<string> {
81  public:
CellularCapabilityUniversalTest(EventDispatcher * dispatcher)82   explicit CellularCapabilityUniversalTest(EventDispatcher* dispatcher)
83       : dispatcher_(dispatcher),
84         control_interface_(this),
85         modem_info_(&control_interface_, dispatcher, nullptr, nullptr),
86         modem_3gpp_proxy_(new mm1::MockModemModem3gppProxy()),
87         modem_cdma_proxy_(new mm1::MockModemModemCdmaProxy()),
88         modem_proxy_(new mm1::MockModemProxy()),
89         modem_simple_proxy_(new mm1::MockModemSimpleProxy()),
90         sim_proxy_(new mm1::MockSimProxy()),
91         properties_proxy_(new MockDBusPropertiesProxy()),
92         capability_(nullptr),
93         device_adaptor_(nullptr),
94         cellular_(new Cellular(&modem_info_,
95                                "",
96                                "00:01:02:03:04:05",
97                                0,
98                                Cellular::kTypeUniversal,
99                                "",
100                                "")),
101         service_(new MockCellularService(&modem_info_, cellular_)),
102         mock_home_provider_info_(nullptr),
103         mock_serving_operator_info_(nullptr) {
104     modem_info_.metrics()->RegisterDevice(cellular_->interface_index(),
105                                           Technology::kCellular);
106   }
107 
~CellularCapabilityUniversalTest()108   virtual ~CellularCapabilityUniversalTest() {
109     cellular_->service_ = nullptr;
110     capability_ = nullptr;
111     device_adaptor_ = nullptr;
112   }
113 
SetUp()114   virtual void SetUp() {
115     capability_ = static_cast<CellularCapabilityUniversal*>(
116         cellular_->capability_.get());
117     device_adaptor_ =
118         static_cast<DeviceMockAdaptor*>(cellular_->adaptor());
119     cellular_->service_ = service_;
120 
121     // kStateUnknown leads to minimal extra work in maintaining
122     // activation state.
123     ON_CALL(*modem_info_.mock_pending_activation_store(),
124             GetActivationState(PendingActivationStore::kIdentifierICCID, _))
125         .WillByDefault(Return(PendingActivationStore::kStateUnknown));
126 
127     SetMockMobileOperatorInfoObjects();
128   }
129 
TearDown()130   virtual void TearDown() {
131     capability_->control_interface_ = nullptr;
132   }
133 
CreateService()134   void CreateService() {
135     // The following constants are never directly accessed by the tests.
136     const char kStorageIdentifier[] = "default_test_storage_id";
137     const char kFriendlyServiceName[] = "default_test_service_name";
138     const char kOperatorCode[] = "10010";
139     const char kOperatorName[] = "default_test_operator_name";
140     const char kOperatorCountry[] = "us";
141 
142     // Simulate all the side-effects of Cellular::CreateService
143     auto service = new CellularService(&modem_info_, cellular_);
144     service->SetStorageIdentifier(kStorageIdentifier);
145     service->SetFriendlyName(kFriendlyServiceName);
146 
147     Stringmap serving_operator;
148     serving_operator[kOperatorCodeKey] = kOperatorCode;
149     serving_operator[kOperatorNameKey] = kOperatorName;
150     serving_operator[kOperatorCountryKey] = kOperatorCountry;
151     service->set_serving_operator(serving_operator);
152     cellular_->set_home_provider(serving_operator);
153     cellular_->service_ = service;
154   }
155 
ClearService()156   void ClearService() {
157     cellular_->service_ = nullptr;
158   }
159 
ExpectModemAndModem3gppProperties()160   void ExpectModemAndModem3gppProperties() {
161     // Set up mock modem properties.
162     KeyValueStore modem_properties;
163     string operator_name = "TestOperator";
164     string operator_code = "001400";
165 
166     modem_properties.SetUint(MM_MODEM_PROPERTY_ACCESSTECHNOLOGIES,
167                              kAccessTechnologies);
168     std::tuple<uint32_t, bool> signal_signal { 90, true };
169     modem_properties.Set(MM_MODEM_PROPERTY_SIGNALQUALITY,
170                          brillo::Any(signal_signal));
171 
172     // Set up mock modem 3gpp properties.
173     KeyValueStore modem3gpp_properties;
174     modem3gpp_properties.SetUint(
175         MM_MODEM_MODEM3GPP_PROPERTY_ENABLEDFACILITYLOCKS, 0);
176     modem3gpp_properties.SetString(MM_MODEM_MODEM3GPP_PROPERTY_IMEI, kImei);
177 
178     EXPECT_CALL(*properties_proxy_,
179                 GetAll(MM_DBUS_INTERFACE_MODEM))
180         .WillOnce(Return(modem_properties));
181     EXPECT_CALL(*properties_proxy_,
182                 GetAll(MM_DBUS_INTERFACE_MODEM_MODEM3GPP))
183         .WillOnce(Return(modem3gpp_properties));
184   }
185 
InvokeEnable(bool enable,Error * error,const ResultCallback & callback,int timeout)186   void InvokeEnable(bool enable, Error* error,
187                     const ResultCallback& callback, int timeout) {
188     callback.Run(Error());
189   }
InvokeEnableFail(bool enable,Error * error,const ResultCallback & callback,int timeout)190   void InvokeEnableFail(bool enable, Error* error,
191                         const ResultCallback& callback, int timeout) {
192     callback.Run(Error(Error::kOperationFailed));
193   }
InvokeEnableInWrongState(bool enable,Error * error,const ResultCallback & callback,int timeout)194   void InvokeEnableInWrongState(bool enable, Error* error,
195                                 const ResultCallback& callback, int timeout) {
196     callback.Run(Error(Error::kWrongState));
197   }
InvokeRegister(const string & operator_id,Error * error,const ResultCallback & callback,int timeout)198   void InvokeRegister(const string& operator_id, Error* error,
199                       const ResultCallback& callback, int timeout) {
200     callback.Run(Error());
201   }
InvokeSetPowerState(const uint32_t & power_state,Error * error,const ResultCallback & callback,int timeout)202   void InvokeSetPowerState(const uint32_t& power_state,
203                            Error* error,
204                            const ResultCallback& callback,
205                            int timeout) {
206     callback.Run(Error());
207   }
Set3gppProxy()208   void Set3gppProxy() {
209     capability_->modem_3gpp_proxy_.reset(modem_3gpp_proxy_.release());
210   }
211 
SetSimpleProxy()212   void SetSimpleProxy() {
213     capability_->modem_simple_proxy_.reset(modem_simple_proxy_.release());
214   }
215 
SetMockMobileOperatorInfoObjects()216   void SetMockMobileOperatorInfoObjects() {
217     CHECK(!mock_home_provider_info_);
218     CHECK(!mock_serving_operator_info_);
219     mock_home_provider_info_ =
220         new MockMobileOperatorInfo(dispatcher_, "HomeProvider");
221     mock_serving_operator_info_ =
222         new MockMobileOperatorInfo(dispatcher_, "ServingOperator");
223     cellular_->set_home_provider_info(mock_home_provider_info_);
224     cellular_->set_serving_operator_info(mock_serving_operator_info_);
225   }
226 
ReleaseCapabilityProxies()227   void ReleaseCapabilityProxies() {
228     capability_->ReleaseProxies();
229   }
230 
SetRegistrationDroppedUpdateTimeout(int64_t timeout_milliseconds)231   void SetRegistrationDroppedUpdateTimeout(int64_t timeout_milliseconds) {
232     capability_->registration_dropped_update_timeout_milliseconds_ =
233         timeout_milliseconds;
234   }
235 
236   MOCK_METHOD1(TestCallback, void(const Error& error));
237 
238   MOCK_METHOD0(DummyCallback, void(void));
239 
SetMockRegistrationDroppedUpdateCallback()240   void SetMockRegistrationDroppedUpdateCallback() {
241     capability_->registration_dropped_update_callback_.Reset(
242         Bind(&CellularCapabilityUniversalTest::DummyCallback,
243              Unretained(this)));
244   }
245 
246  protected:
247   static const char kActiveBearerPathPrefix[];
248   static const char kImei[];
249   static const char kInactiveBearerPathPrefix[];
250   static const char kSimPath[];
251   static const uint32_t kAccessTechnologies;
252   static const char kTestMobileProviderDBPath[];
253 
254   class TestControl : public MockControl {
255    public:
TestControl(CellularCapabilityUniversalTest * test)256     explicit TestControl(CellularCapabilityUniversalTest* test)
257         : test_(test) {
258       active_bearer_properties_.SetBool(MM_BEARER_PROPERTY_CONNECTED, true);
259       active_bearer_properties_.SetString(MM_BEARER_PROPERTY_INTERFACE,
260                                           "/dev/fake");
261 
262       KeyValueStore ip4config;
263       ip4config.SetUint("method", MM_BEARER_IP_METHOD_DHCP);
264       active_bearer_properties_.SetKeyValueStore(
265           MM_BEARER_PROPERTY_IP4CONFIG, ip4config);
266 
267       inactive_bearer_properties_.SetBool(MM_BEARER_PROPERTY_CONNECTED, false);
268     }
269 
mutable_active_bearer_properties()270     KeyValueStore* mutable_active_bearer_properties() {
271       return &active_bearer_properties_;
272     }
273 
mutable_inactive_bearer_properties()274     KeyValueStore* mutable_inactive_bearer_properties() {
275       return &inactive_bearer_properties_;
276     }
277 
CreateMM1ModemModem3gppProxy(const std::string &,const std::string &)278     virtual mm1::ModemModem3gppProxyInterface* CreateMM1ModemModem3gppProxy(
279         const std::string& /*path*/,
280         const std::string& /*service*/) {
281       return test_->modem_3gpp_proxy_.release();
282     }
283 
CreateMM1ModemModemCdmaProxy(const std::string &,const std::string &)284     virtual mm1::ModemModemCdmaProxyInterface* CreateMM1ModemModemCdmaProxy(
285         const std::string& /*path*/,
286         const std::string& /*service*/) {
287       return test_->modem_cdma_proxy_.release();
288     }
289 
CreateMM1ModemProxy(const std::string &,const std::string &)290     virtual mm1::ModemProxyInterface* CreateMM1ModemProxy(
291         const std::string& /*path*/,
292         const std::string& /*service*/) {
293       return test_->modem_proxy_.release();
294     }
295 
CreateMM1ModemSimpleProxy(const std::string &,const std::string &)296     virtual mm1::ModemSimpleProxyInterface* CreateMM1ModemSimpleProxy(
297         const std::string& /*path*/,
298         const std::string& /*service*/) {
299       return test_->modem_simple_proxy_.release();
300     }
301 
CreateSimProxy(const std::string &,const std::string &)302     virtual mm1::SimProxyInterface* CreateSimProxy(
303         const std::string& /*path*/,
304         const std::string& /*service*/) {
305       mm1::MockSimProxy* sim_proxy = test_->sim_proxy_.release();
306       test_->sim_proxy_.reset(new mm1::MockSimProxy());
307       return sim_proxy;
308     }
309 
CreateDBusPropertiesProxy(const std::string & path,const std::string &)310     virtual DBusPropertiesProxyInterface* CreateDBusPropertiesProxy(
311         const std::string& path,
312         const std::string& /*service*/) {
313       MockDBusPropertiesProxy* properties_proxy =
314           test_->properties_proxy_.release();
315       if (path.find(kActiveBearerPathPrefix) != std::string::npos) {
316         EXPECT_CALL(*properties_proxy, GetAll(MM_DBUS_INTERFACE_BEARER))
317             .Times(AnyNumber())
318             .WillRepeatedly(Return(active_bearer_properties_));
319       } else {
320         EXPECT_CALL(*properties_proxy, GetAll(MM_DBUS_INTERFACE_BEARER))
321             .Times(AnyNumber())
322             .WillRepeatedly(Return(inactive_bearer_properties_));
323       }
324       test_->properties_proxy_.reset(new MockDBusPropertiesProxy());
325       return properties_proxy;
326     }
327 
328    private:
329     CellularCapabilityUniversalTest* test_;
330     KeyValueStore active_bearer_properties_;
331     KeyValueStore inactive_bearer_properties_;
332   };
333 
334   EventDispatcher* dispatcher_;
335   TestControl control_interface_;
336   MockModemInfo modem_info_;
337   unique_ptr<mm1::MockModemModem3gppProxy> modem_3gpp_proxy_;
338   unique_ptr<mm1::MockModemModemCdmaProxy> modem_cdma_proxy_;
339   unique_ptr<mm1::MockModemProxy> modem_proxy_;
340   unique_ptr<mm1::MockModemSimpleProxy> modem_simple_proxy_;
341   unique_ptr<mm1::MockSimProxy> sim_proxy_;
342   unique_ptr<MockDBusPropertiesProxy> properties_proxy_;
343   CellularCapabilityUniversal* capability_;  // Owned by |cellular_|.
344   DeviceMockAdaptor* device_adaptor_;  // Owned by |cellular_|.
345   CellularRefPtr cellular_;
346   MockCellularService* service_;  // owned by cellular_
347   // saved for testing connect operations.
348   RpcIdentifierCallback connect_callback_;
349 
350   // Set when required and passed to |cellular_|. Owned by |cellular_|.
351   MockMobileOperatorInfo* mock_home_provider_info_;
352   MockMobileOperatorInfo* mock_serving_operator_info_;
353 };
354 
355 // Most of our tests involve using a real EventDispatcher object.
356 class CellularCapabilityUniversalMainTest
357     : public CellularCapabilityUniversalTest {
358  public:
CellularCapabilityUniversalMainTest()359   CellularCapabilityUniversalMainTest() :
360       CellularCapabilityUniversalTest(&dispatcher_) {}
361 
362  protected:
363   EventDispatcherForTest dispatcher_;
364 };
365 
366 // Tests that involve timers will (or may) use a mock of the event dispatcher
367 // instead of a real one.
368 class CellularCapabilityUniversalTimerTest
369     : public CellularCapabilityUniversalTest {
370  public:
CellularCapabilityUniversalTimerTest()371   CellularCapabilityUniversalTimerTest()
372       : CellularCapabilityUniversalTest(&mock_dispatcher_) {}
373 
374  protected:
375   ::testing::StrictMock<MockEventDispatcher> mock_dispatcher_;
376 };
377 
378 const char CellularCapabilityUniversalTest::kActiveBearerPathPrefix[] =
379     "/bearer/active";
380 const char CellularCapabilityUniversalTest::kImei[] = "999911110000";
381 const char CellularCapabilityUniversalTest::kInactiveBearerPathPrefix[] =
382     "/bearer/inactive";
383 const char CellularCapabilityUniversalTest::kSimPath[] = "/foo/sim";
384 const uint32_t CellularCapabilityUniversalTest::kAccessTechnologies =
385     MM_MODEM_ACCESS_TECHNOLOGY_LTE |
386     MM_MODEM_ACCESS_TECHNOLOGY_HSPA_PLUS;
387 const char CellularCapabilityUniversalTest::kTestMobileProviderDBPath[] =
388     "provider_db_unittest.bfd";
389 
TEST_F(CellularCapabilityUniversalMainTest,StartModem)390 TEST_F(CellularCapabilityUniversalMainTest, StartModem) {
391   ExpectModemAndModem3gppProperties();
392 
393   EXPECT_CALL(*modem_proxy_,
394               Enable(true, _, _, CellularCapability::kTimeoutEnable))
395       .WillOnce(Invoke(
396            this, &CellularCapabilityUniversalTest::InvokeEnable));
397 
398   Error error;
399   EXPECT_CALL(*this, TestCallback(IsSuccess()));
400   ResultCallback callback =
401       Bind(&CellularCapabilityUniversalTest::TestCallback, Unretained(this));
402   capability_->StartModem(&error, callback);
403 
404   EXPECT_TRUE(error.IsOngoing());
405   EXPECT_EQ(kImei, cellular_->imei());
406   EXPECT_EQ(kAccessTechnologies, capability_->access_technologies_);
407 }
408 
TEST_F(CellularCapabilityUniversalMainTest,StartModemFailure)409 TEST_F(CellularCapabilityUniversalMainTest, StartModemFailure) {
410   EXPECT_CALL(*modem_proxy_,
411               Enable(true, _, _, CellularCapability::kTimeoutEnable))
412       .WillOnce(Invoke(
413            this, &CellularCapabilityUniversalTest::InvokeEnableFail));
414   EXPECT_CALL(*properties_proxy_, GetAll(MM_DBUS_INTERFACE_MODEM)).Times(0);
415   EXPECT_CALL(*properties_proxy_, GetAll(MM_DBUS_INTERFACE_MODEM_MODEM3GPP))
416       .Times(0);
417 
418   Error error;
419   EXPECT_CALL(*this, TestCallback(IsFailure()));
420   ResultCallback callback =
421       Bind(&CellularCapabilityUniversalTest::TestCallback, Unretained(this));
422   capability_->StartModem(&error, callback);
423   EXPECT_TRUE(error.IsOngoing());
424 }
425 
TEST_F(CellularCapabilityUniversalMainTest,StartModemInWrongState)426 TEST_F(CellularCapabilityUniversalMainTest, StartModemInWrongState) {
427   ExpectModemAndModem3gppProperties();
428 
429   EXPECT_CALL(*modem_proxy_,
430               Enable(true, _, _, CellularCapability::kTimeoutEnable))
431       .WillOnce(Invoke(
432            this, &CellularCapabilityUniversalTest::InvokeEnableInWrongState))
433       .WillOnce(Invoke(
434            this, &CellularCapabilityUniversalTest::InvokeEnable));
435 
436   Error error;
437   EXPECT_CALL(*this, TestCallback(_)).Times(0);
438   ResultCallback callback =
439       Bind(&CellularCapabilityUniversalTest::TestCallback, Unretained(this));
440   capability_->StartModem(&error, callback);
441   EXPECT_TRUE(error.IsOngoing());
442 
443   // Verify that the modem has not been enabled.
444   EXPECT_TRUE(cellular_->imei().empty());
445   EXPECT_EQ(0, capability_->access_technologies_);
446   Mock::VerifyAndClearExpectations(this);
447 
448   // Change the state to kModemStateEnabling and verify that it still has not
449   // been enabled.
450   capability_->OnModemStateChanged(Cellular::kModemStateEnabling);
451   EXPECT_TRUE(cellular_->imei().empty());
452   EXPECT_EQ(0, capability_->access_technologies_);
453   Mock::VerifyAndClearExpectations(this);
454 
455   // Change the state to kModemStateDisabling and verify that it still has not
456   // been enabled.
457   EXPECT_CALL(*this, TestCallback(_)).Times(0);
458   capability_->OnModemStateChanged(Cellular::kModemStateDisabling);
459   EXPECT_TRUE(cellular_->imei().empty());
460   EXPECT_EQ(0, capability_->access_technologies_);
461   Mock::VerifyAndClearExpectations(this);
462 
463   // Change the state of the modem to disabled and verify that it gets enabled.
464   EXPECT_CALL(*this, TestCallback(IsSuccess()));
465   capability_->OnModemStateChanged(Cellular::kModemStateDisabled);
466   EXPECT_EQ(kImei, cellular_->imei());
467   EXPECT_EQ(kAccessTechnologies, capability_->access_technologies_);
468 }
469 
TEST_F(CellularCapabilityUniversalMainTest,StartModemWithDeferredEnableFailure)470 TEST_F(CellularCapabilityUniversalMainTest,
471        StartModemWithDeferredEnableFailure) {
472   EXPECT_CALL(*modem_proxy_,
473               Enable(true, _, _, CellularCapability::kTimeoutEnable))
474       .Times(2)
475       .WillRepeatedly(Invoke(
476            this, &CellularCapabilityUniversalTest::InvokeEnableInWrongState));
477   EXPECT_CALL(*properties_proxy_, GetAll(MM_DBUS_INTERFACE_MODEM)).Times(0);
478   EXPECT_CALL(*properties_proxy_, GetAll(MM_DBUS_INTERFACE_MODEM_MODEM3GPP))
479       .Times(0);
480 
481   Error error;
482   EXPECT_CALL(*this, TestCallback(_)).Times(0);
483   ResultCallback callback =
484       Bind(&CellularCapabilityUniversalTest::TestCallback, Unretained(this));
485   capability_->StartModem(&error, callback);
486   EXPECT_TRUE(error.IsOngoing());
487   Mock::VerifyAndClearExpectations(this);
488 
489   // Change the state of the modem to disabled but fail the deferred enable
490   // operation with the WrongState error in order to verify that the deferred
491   // enable operation does not trigger another deferred enable operation.
492   EXPECT_CALL(*this, TestCallback(IsFailure()));
493   capability_->OnModemStateChanged(Cellular::kModemStateDisabled);
494 }
495 
TEST_F(CellularCapabilityUniversalMainTest,StopModem)496 TEST_F(CellularCapabilityUniversalMainTest, StopModem) {
497   // Save pointers to proxies before they are lost by the call to InitProxies
498   mm1::MockModemProxy* modem_proxy = modem_proxy_.get();
499   EXPECT_CALL(*modem_proxy, set_state_changed_callback(_));
500   capability_->InitProxies();
501 
502   Error error;
503   ResultCallback callback =
504       Bind(&CellularCapabilityUniversalTest::TestCallback, Unretained(this));
505   capability_->StopModem(&error, callback);
506   EXPECT_TRUE(error.IsSuccess());
507 
508   ResultCallback disable_callback;
509   EXPECT_CALL(*modem_proxy,
510               Enable(false, _, _, CellularCapability::kTimeoutEnable))
511       .WillOnce(SaveArg<2>(&disable_callback));
512   dispatcher_.DispatchPendingEvents();
513 
514   ResultCallback set_power_state_callback;
515   EXPECT_CALL(
516       *modem_proxy,
517       SetPowerState(
518           MM_MODEM_POWER_STATE_LOW, _, _,
519           CellularCapabilityUniversal::kSetPowerStateTimeoutMilliseconds))
520       .WillOnce(SaveArg<2>(&set_power_state_callback));
521   disable_callback.Run(Error(Error::kSuccess));
522 
523   EXPECT_CALL(*this, TestCallback(IsSuccess()));
524   set_power_state_callback.Run(Error(Error::kSuccess));
525   Mock::VerifyAndClearExpectations(this);
526 
527   // TestCallback should get called with success even if the power state
528   // callback gets called with an error
529   EXPECT_CALL(*this, TestCallback(IsSuccess()));
530   set_power_state_callback.Run(Error(Error::kOperationFailed));
531 }
532 
TEST_F(CellularCapabilityUniversalMainTest,StopModemAltair)533 TEST_F(CellularCapabilityUniversalMainTest, StopModemAltair) {
534   // Save pointers to proxies before they are lost by the call to InitProxies
535   mm1::MockModemProxy* modem_proxy = modem_proxy_.get();
536   EXPECT_CALL(*modem_proxy, set_state_changed_callback(_));
537   capability_->InitProxies();
538 
539   const char kBearerDBusPath[] = "/bearer/dbus/path";
540   capability_->set_active_bearer(
541       new CellularBearer(&control_interface_,
542                          kBearerDBusPath,
543                          cellular_->dbus_service()));  // Passes ownership.
544 
545   cellular_->set_mm_plugin(CellularCapabilityUniversal::kAltairLTEMMPlugin);
546 
547   Error error;
548   ResultCallback callback =
549       Bind(&CellularCapabilityUniversalTest::TestCallback, Unretained(this));
550   capability_->StopModem(&error, callback);
551   EXPECT_TRUE(error.IsSuccess());
552 
553   ResultCallback delete_bearer_callback;
554   EXPECT_CALL(*modem_proxy,
555               DeleteBearer(kBearerDBusPath, _, _,
556                            CellularCapability::kTimeoutDefault))
557       .WillOnce(SaveArg<2>(&delete_bearer_callback));
558   dispatcher_.DispatchPendingEvents();
559 
560   ResultCallback disable_callback;
561   EXPECT_CALL(*modem_proxy,
562               Enable(false, _, _, CellularCapability::kTimeoutEnable))
563       .WillOnce(SaveArg<2>(&disable_callback));
564   delete_bearer_callback.Run(Error(Error::kSuccess));
565 
566   ResultCallback set_power_state_callback;
567   EXPECT_CALL(
568       *modem_proxy,
569       SetPowerState(
570           MM_MODEM_POWER_STATE_LOW, _, _,
571           CellularCapabilityUniversal::kSetPowerStateTimeoutMilliseconds))
572       .WillOnce(SaveArg<2>(&set_power_state_callback));
573   disable_callback.Run(Error(Error::kSuccess));
574 
575   EXPECT_CALL(*this, TestCallback(IsSuccess()));
576   set_power_state_callback.Run(Error(Error::kSuccess));
577 }
578 
TEST_F(CellularCapabilityUniversalMainTest,StopModemAltairDeleteBearerFailure)579 TEST_F(CellularCapabilityUniversalMainTest,
580        StopModemAltairDeleteBearerFailure) {
581   // Save pointers to proxies before they are lost by the call to InitProxies
582   mm1::MockModemProxy* modem_proxy = modem_proxy_.get();
583   EXPECT_CALL(*modem_proxy, set_state_changed_callback(_));
584   capability_->InitProxies();
585 
586   const char kBearerDBusPath[] = "/bearer/dbus/path";
587   capability_->set_active_bearer(
588       new CellularBearer(&control_interface_,
589                          kBearerDBusPath,
590                          cellular_->dbus_service()));  // Passes ownership.
591 
592   cellular_->set_mm_plugin(CellularCapabilityUniversal::kAltairLTEMMPlugin);
593 
594   Error error;
595   ResultCallback callback =
596       Bind(&CellularCapabilityUniversalTest::TestCallback, Unretained(this));
597   capability_->StopModem(&error, callback);
598   EXPECT_TRUE(error.IsSuccess());
599 
600   ResultCallback delete_bearer_callback;
601   EXPECT_CALL(*modem_proxy,
602               DeleteBearer(kBearerDBusPath, _, _,
603                            CellularCapability::kTimeoutDefault))
604       .WillOnce(SaveArg<2>(&delete_bearer_callback));
605   dispatcher_.DispatchPendingEvents();
606 
607   ResultCallback disable_callback;
608   EXPECT_CALL(*modem_proxy,
609               Enable(false, _, _, CellularCapability::kTimeoutEnable))
610       .WillOnce(SaveArg<2>(&disable_callback));
611   delete_bearer_callback.Run(Error(Error::kOperationFailed));
612 
613   ResultCallback set_power_state_callback;
614   EXPECT_CALL(
615       *modem_proxy,
616       SetPowerState(
617           MM_MODEM_POWER_STATE_LOW, _, _,
618           CellularCapabilityUniversal::kSetPowerStateTimeoutMilliseconds))
619       .WillOnce(SaveArg<2>(&set_power_state_callback));
620   disable_callback.Run(Error(Error::kSuccess));
621 
622   EXPECT_CALL(*this, TestCallback(IsSuccess()));
623   set_power_state_callback.Run(Error(Error::kSuccess));
624 }
625 
TEST_F(CellularCapabilityUniversalMainTest,StopModemAltairNotConnected)626 TEST_F(CellularCapabilityUniversalMainTest, StopModemAltairNotConnected) {
627   // Save pointers to proxies before they are lost by the call to InitProxies
628   mm1::MockModemProxy* modem_proxy = modem_proxy_.get();
629   EXPECT_CALL(*modem_proxy, set_state_changed_callback(_));
630   capability_->InitProxies();
631   capability_->set_active_bearer(nullptr);
632   cellular_->set_mm_plugin(CellularCapabilityUniversal::kAltairLTEMMPlugin);
633 
634   Error error;
635   ResultCallback callback =
636       Bind(&CellularCapabilityUniversalTest::TestCallback, Unretained(this));
637   capability_->StopModem(&error, callback);
638   EXPECT_TRUE(error.IsSuccess());
639 
640   ResultCallback disable_callback;
641   EXPECT_CALL(*modem_proxy,
642               Enable(false, _, _, CellularCapability::kTimeoutEnable))
643       .WillOnce(SaveArg<2>(&disable_callback));
644   dispatcher_.DispatchPendingEvents();
645 
646   ResultCallback set_power_state_callback;
647   EXPECT_CALL(
648       *modem_proxy,
649       SetPowerState(
650           MM_MODEM_POWER_STATE_LOW, _, _,
651           CellularCapabilityUniversal::kSetPowerStateTimeoutMilliseconds))
652       .WillOnce(SaveArg<2>(&set_power_state_callback));
653   disable_callback.Run(Error(Error::kSuccess));
654 
655   EXPECT_CALL(*this, TestCallback(IsSuccess()));
656   set_power_state_callback.Run(Error(Error::kSuccess));
657   Mock::VerifyAndClearExpectations(this);
658 
659   // TestCallback should get called with success even if the power state
660   // callback gets called with an error
661   EXPECT_CALL(*this, TestCallback(IsSuccess()));
662   set_power_state_callback.Run(Error(Error::kOperationFailed));
663 }
664 
TEST_F(CellularCapabilityUniversalMainTest,TerminationAction)665 TEST_F(CellularCapabilityUniversalMainTest, TerminationAction) {
666   ExpectModemAndModem3gppProperties();
667 
668   {
669     InSequence seq;
670 
671     EXPECT_CALL(*modem_proxy_,
672                 Enable(true, _, _, CellularCapability::kTimeoutEnable))
673         .WillOnce(Invoke(this, &CellularCapabilityUniversalTest::InvokeEnable));
674     EXPECT_CALL(*modem_proxy_,
675                 Enable(false, _, _, CellularCapability::kTimeoutEnable))
676         .WillOnce(Invoke(this, &CellularCapabilityUniversalTest::InvokeEnable));
677     EXPECT_CALL(
678         *modem_proxy_,
679         SetPowerState(
680             MM_MODEM_POWER_STATE_LOW, _, _,
681             CellularCapabilityUniversal::kSetPowerStateTimeoutMilliseconds))
682         .WillOnce(Invoke(
683              this, &CellularCapabilityUniversalTest::InvokeSetPowerState));
684   }
685   EXPECT_CALL(*this, TestCallback(IsSuccess())).Times(2);
686 
687   EXPECT_EQ(Cellular::kStateDisabled, cellular_->state());
688   EXPECT_EQ(Cellular::kModemStateUnknown, cellular_->modem_state());
689   EXPECT_TRUE(modem_info_.manager()->termination_actions_.IsEmpty());
690 
691   // Here we mimic the modem state change from ModemManager. When the modem is
692   // enabled, a termination action should be added.
693   cellular_->OnModemStateChanged(Cellular::kModemStateEnabled);
694   dispatcher_.DispatchPendingEvents();
695   EXPECT_EQ(Cellular::kStateEnabled, cellular_->state());
696   EXPECT_EQ(Cellular::kModemStateEnabled, cellular_->modem_state());
697   EXPECT_FALSE(modem_info_.manager()->termination_actions_.IsEmpty());
698 
699   // Running the termination action should disable the modem.
700   modem_info_.manager()->RunTerminationActions(Bind(
701       &CellularCapabilityUniversalMainTest::TestCallback, Unretained(this)));
702   dispatcher_.DispatchPendingEvents();
703   // Here we mimic the modem state change from ModemManager. When the modem is
704   // disabled, the termination action should be removed.
705   cellular_->OnModemStateChanged(Cellular::kModemStateDisabled);
706   dispatcher_.DispatchPendingEvents();
707   EXPECT_EQ(Cellular::kStateDisabled, cellular_->state());
708   EXPECT_EQ(Cellular::kModemStateDisabled, cellular_->modem_state());
709   EXPECT_TRUE(modem_info_.manager()->termination_actions_.IsEmpty());
710 
711   // No termination action should be called here.
712   modem_info_.manager()->RunTerminationActions(Bind(
713       &CellularCapabilityUniversalMainTest::TestCallback, Unretained(this)));
714   dispatcher_.DispatchPendingEvents();
715 }
716 
TEST_F(CellularCapabilityUniversalMainTest,TerminationActionRemovedByStopModem)717 TEST_F(CellularCapabilityUniversalMainTest,
718        TerminationActionRemovedByStopModem) {
719   ExpectModemAndModem3gppProperties();
720 
721   {
722     InSequence seq;
723 
724     EXPECT_CALL(*modem_proxy_,
725                 Enable(true, _, _, CellularCapability::kTimeoutEnable))
726         .WillOnce(Invoke(this, &CellularCapabilityUniversalTest::InvokeEnable));
727     EXPECT_CALL(*modem_proxy_,
728                 Enable(false, _, _, CellularCapability::kTimeoutEnable))
729         .WillOnce(Invoke(this, &CellularCapabilityUniversalTest::InvokeEnable));
730     EXPECT_CALL(
731         *modem_proxy_,
732         SetPowerState(
733             MM_MODEM_POWER_STATE_LOW, _, _,
734             CellularCapabilityUniversal::kSetPowerStateTimeoutMilliseconds))
735         .WillOnce(Invoke(
736              this, &CellularCapabilityUniversalTest::InvokeSetPowerState));
737   }
738   EXPECT_CALL(*this, TestCallback(IsSuccess())).Times(1);
739 
740   EXPECT_EQ(Cellular::kStateDisabled, cellular_->state());
741   EXPECT_EQ(Cellular::kModemStateUnknown, cellular_->modem_state());
742   EXPECT_TRUE(modem_info_.manager()->termination_actions_.IsEmpty());
743 
744   // Here we mimic the modem state change from ModemManager. When the modem is
745   // enabled, a termination action should be added.
746   cellular_->OnModemStateChanged(Cellular::kModemStateEnabled);
747   dispatcher_.DispatchPendingEvents();
748   EXPECT_EQ(Cellular::kStateEnabled, cellular_->state());
749   EXPECT_EQ(Cellular::kModemStateEnabled, cellular_->modem_state());
750   EXPECT_FALSE(modem_info_.manager()->termination_actions_.IsEmpty());
751 
752   // Verify that the termination action is removed when the modem is disabled
753   // not due to a suspend request.
754   cellular_->SetEnabled(false);
755   dispatcher_.DispatchPendingEvents();
756   EXPECT_EQ(Cellular::kStateDisabled, cellular_->state());
757   EXPECT_TRUE(modem_info_.manager()->termination_actions_.IsEmpty());
758 
759   // No termination action should be called here.
760   modem_info_.manager()->RunTerminationActions(Bind(
761       &CellularCapabilityUniversalMainTest::TestCallback, Unretained(this)));
762   dispatcher_.DispatchPendingEvents();
763 }
764 
TEST_F(CellularCapabilityUniversalMainTest,DisconnectModemNoBearer)765 TEST_F(CellularCapabilityUniversalMainTest, DisconnectModemNoBearer) {
766   Error error;
767   ResultCallback disconnect_callback;
768   EXPECT_CALL(*modem_simple_proxy_,
769               Disconnect(_, _, _, CellularCapability::kTimeoutDisconnect))
770       .Times(0);
771   capability_->Disconnect(&error, disconnect_callback);
772 }
773 
TEST_F(CellularCapabilityUniversalMainTest,DisconnectNoProxy)774 TEST_F(CellularCapabilityUniversalMainTest, DisconnectNoProxy) {
775   Error error;
776   ResultCallback disconnect_callback;
777   EXPECT_CALL(*modem_simple_proxy_,
778               Disconnect(_, _, _, CellularCapability::kTimeoutDisconnect))
779       .Times(0);
780   ReleaseCapabilityProxies();
781   capability_->Disconnect(&error, disconnect_callback);
782 }
783 
TEST_F(CellularCapabilityUniversalMainTest,SimLockStatusChanged)784 TEST_F(CellularCapabilityUniversalMainTest, SimLockStatusChanged) {
785   // Set up mock SIM properties
786   const char kImsi[] = "310100000001";
787   const char kSimIdentifier[] = "9999888";
788   const char kOperatorIdentifier[] = "310240";
789   const char kOperatorName[] = "Custom SPN";
790   KeyValueStore sim_properties;
791   sim_properties.SetString(MM_SIM_PROPERTY_IMSI, kImsi);
792   sim_properties.SetString(MM_SIM_PROPERTY_SIMIDENTIFIER, kSimIdentifier);
793   sim_properties.SetString(MM_SIM_PROPERTY_OPERATORIDENTIFIER,
794                            kOperatorIdentifier);
795   sim_properties.SetString(MM_SIM_PROPERTY_OPERATORNAME, kOperatorName);
796 
797   EXPECT_CALL(*properties_proxy_, GetAll(MM_DBUS_INTERFACE_SIM))
798       .WillOnce(Return(sim_properties));
799   EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
800               GetActivationState(PendingActivationStore::kIdentifierICCID, _))
801       .Times(1);
802 
803   EXPECT_FALSE(cellular_->sim_present());
804   EXPECT_EQ(nullptr, capability_->sim_proxy_);;
805 
806   capability_->OnSimPathChanged(kSimPath);
807   EXPECT_TRUE(cellular_->sim_present());
808   EXPECT_NE(nullptr, capability_->sim_proxy_);;
809   EXPECT_EQ(kSimPath, capability_->sim_path_);
810 
811   cellular_->set_imsi("");
812   cellular_->set_sim_identifier("");
813   capability_->spn_ = "";
814 
815   // SIM is locked.
816   capability_->sim_lock_status_.lock_type = MM_MODEM_LOCK_SIM_PIN;
817   capability_->OnSimLockStatusChanged();
818   Mock::VerifyAndClearExpectations(modem_info_.mock_pending_activation_store());
819 
820   EXPECT_EQ("", cellular_->imsi());
821   EXPECT_EQ("", cellular_->sim_identifier());
822   EXPECT_EQ("", capability_->spn_);
823 
824   // SIM is unlocked.
825   properties_proxy_.reset(new MockDBusPropertiesProxy());
826   EXPECT_CALL(*properties_proxy_, GetAll(MM_DBUS_INTERFACE_SIM))
827       .WillOnce(Return(sim_properties));
828   EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
829               GetActivationState(PendingActivationStore::kIdentifierICCID, _))
830       .Times(1);
831 
832   capability_->sim_lock_status_.lock_type = MM_MODEM_LOCK_NONE;
833   capability_->OnSimLockStatusChanged();
834   Mock::VerifyAndClearExpectations(modem_info_.mock_pending_activation_store());
835 
836   EXPECT_EQ(kImsi, cellular_->imsi());
837   EXPECT_EQ(kSimIdentifier, cellular_->sim_identifier());
838   EXPECT_EQ(kOperatorName, capability_->spn_);
839 
840   // SIM is missing and SIM path is "/".
841   capability_->OnSimPathChanged(CellularCapabilityUniversal::kRootPath);
842   EXPECT_FALSE(cellular_->sim_present());
843   EXPECT_EQ(nullptr, capability_->sim_proxy_);;
844   EXPECT_EQ(CellularCapabilityUniversal::kRootPath, capability_->sim_path_);
845 
846   EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
847               GetActivationState(_, _)).Times(0);
848   capability_->OnSimLockStatusChanged();
849   Mock::VerifyAndClearExpectations(modem_info_.mock_pending_activation_store());
850 
851   EXPECT_EQ("", cellular_->imsi());
852   EXPECT_EQ("", cellular_->sim_identifier());
853   EXPECT_EQ("", capability_->spn_);
854 
855   // SIM is missing and SIM path is empty.
856   capability_->OnSimPathChanged("");
857   EXPECT_FALSE(cellular_->sim_present());
858   EXPECT_EQ(nullptr, capability_->sim_proxy_);;
859   EXPECT_EQ("", capability_->sim_path_);
860 
861   EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
862               GetActivationState(_, _)).Times(0);
863   capability_->OnSimLockStatusChanged();
864   Mock::VerifyAndClearExpectations(modem_info_.mock_pending_activation_store());
865 
866   EXPECT_EQ("", cellular_->imsi());
867   EXPECT_EQ("", cellular_->sim_identifier());
868   EXPECT_EQ("", capability_->spn_);
869 }
870 
TEST_F(CellularCapabilityUniversalMainTest,PropertiesChanged)871 TEST_F(CellularCapabilityUniversalMainTest, PropertiesChanged) {
872   // Set up mock modem properties
873   KeyValueStore modem_properties;
874   modem_properties.SetUint(MM_MODEM_PROPERTY_ACCESSTECHNOLOGIES,
875                            kAccessTechnologies);
876   modem_properties.SetRpcIdentifier(MM_MODEM_PROPERTY_SIM, kSimPath);
877 
878   // Set up mock modem 3gpp properties
879   KeyValueStore modem3gpp_properties;
880   modem3gpp_properties.SetUint(MM_MODEM_MODEM3GPP_PROPERTY_ENABLEDFACILITYLOCKS,
881                                0);
882   modem3gpp_properties.SetString(MM_MODEM_MODEM3GPP_PROPERTY_IMEI, kImei);
883 
884   // Set up mock modem sim properties
885   KeyValueStore sim_properties;
886 
887   EXPECT_CALL(*properties_proxy_,
888               GetAll(MM_DBUS_INTERFACE_SIM))
889       .WillOnce(Return(sim_properties));
890 
891   EXPECT_EQ("", cellular_->imei());
892   EXPECT_EQ(MM_MODEM_ACCESS_TECHNOLOGY_UNKNOWN,
893             capability_->access_technologies_);
894   EXPECT_FALSE(capability_->sim_proxy_.get());
895   EXPECT_CALL(*device_adaptor_, EmitStringChanged(
896       kTechnologyFamilyProperty, kTechnologyFamilyGsm));
897   EXPECT_CALL(*device_adaptor_, EmitStringChanged(kImeiProperty, kImei));
898   capability_->OnPropertiesChanged(MM_DBUS_INTERFACE_MODEM,
899                                    modem_properties, vector<string>());
900   EXPECT_EQ(kAccessTechnologies, capability_->access_technologies_);
901   EXPECT_EQ(kSimPath, capability_->sim_path_);
902   EXPECT_TRUE(capability_->sim_proxy_.get());
903 
904   // Changing properties on wrong interface will not have an effect
905   capability_->OnPropertiesChanged(MM_DBUS_INTERFACE_MODEM,
906                                    modem3gpp_properties,
907                                    vector<string>());
908   EXPECT_EQ("", cellular_->imei());
909 
910   // Changing properties on the right interface gets reflected in the
911   // capabilities object
912   capability_->OnPropertiesChanged(MM_DBUS_INTERFACE_MODEM_MODEM3GPP,
913                                    modem3gpp_properties,
914                                    vector<string>());
915   EXPECT_EQ(kImei, cellular_->imei());
916   Mock::VerifyAndClearExpectations(device_adaptor_);
917 
918   // Expect to see changes when the family changes
919   modem_properties.Clear();
920   modem_properties.SetUint(MM_MODEM_PROPERTY_ACCESSTECHNOLOGIES,
921                            MM_MODEM_ACCESS_TECHNOLOGY_1XRTT);
922   EXPECT_CALL(*device_adaptor_, EmitStringChanged(
923       kTechnologyFamilyProperty, kTechnologyFamilyCdma)).
924       Times(1);
925   capability_->OnPropertiesChanged(MM_DBUS_INTERFACE_MODEM,
926                                        modem_properties,
927                                        vector<string>());
928   Mock::VerifyAndClearExpectations(device_adaptor_);
929 
930   // Back to LTE
931   modem_properties.Clear();
932   modem_properties.SetUint(MM_MODEM_PROPERTY_ACCESSTECHNOLOGIES,
933                            MM_MODEM_ACCESS_TECHNOLOGY_LTE);
934   EXPECT_CALL(*device_adaptor_, EmitStringChanged(
935       kTechnologyFamilyProperty, kTechnologyFamilyGsm)).
936       Times(1);
937   capability_->OnPropertiesChanged(MM_DBUS_INTERFACE_MODEM,
938                                    modem_properties,
939                                    vector<string>());
940   Mock::VerifyAndClearExpectations(device_adaptor_);
941 
942   // LTE & CDMA - the device adaptor should not be called!
943   modem_properties.Clear();
944   modem_properties.SetUint(MM_MODEM_PROPERTY_ACCESSTECHNOLOGIES,
945                            MM_MODEM_ACCESS_TECHNOLOGY_LTE |
946                            MM_MODEM_ACCESS_TECHNOLOGY_1XRTT);
947   EXPECT_CALL(*device_adaptor_, EmitStringChanged(_, _)).Times(0);
948   capability_->OnPropertiesChanged(MM_DBUS_INTERFACE_MODEM,
949                                    modem_properties,
950                                    vector<string>());
951 }
952 
TEST_F(CellularCapabilityUniversalMainTest,UpdateRegistrationState)953 TEST_F(CellularCapabilityUniversalMainTest, UpdateRegistrationState) {
954   capability_->InitProxies();
955 
956   CreateService();
957   cellular_->set_imsi("310240123456789");
958   cellular_->set_modem_state(Cellular::kModemStateConnected);
959   SetRegistrationDroppedUpdateTimeout(0);
960 
961   const Stringmap& home_provider_map = cellular_->home_provider();
962   ASSERT_NE(home_provider_map.end(), home_provider_map.find(kOperatorNameKey));
963   string home_provider = home_provider_map.find(kOperatorNameKey)->second;
964   string ota_name = cellular_->service_->friendly_name();
965 
966   // Home --> Roaming should be effective immediately.
967   capability_->On3GPPRegistrationChanged(
968       MM_MODEM_3GPP_REGISTRATION_STATE_HOME,
969       home_provider,
970       ota_name);
971   EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_HOME,
972             capability_->registration_state_);
973   capability_->On3GPPRegistrationChanged(
974       MM_MODEM_3GPP_REGISTRATION_STATE_ROAMING,
975       home_provider,
976       ota_name);
977   EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_ROAMING,
978             capability_->registration_state_);
979 
980   // Idle --> Roaming should be effective immediately.
981   capability_->On3GPPRegistrationChanged(
982       MM_MODEM_3GPP_REGISTRATION_STATE_IDLE,
983       home_provider,
984       ota_name);
985   dispatcher_.DispatchPendingEvents();
986   EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_IDLE,
987             capability_->registration_state_);
988   capability_->On3GPPRegistrationChanged(
989       MM_MODEM_3GPP_REGISTRATION_STATE_ROAMING,
990       home_provider,
991       ota_name);
992   EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_ROAMING,
993             capability_->registration_state_);
994 
995   // Idle --> Searching should be effective immediately.
996   capability_->On3GPPRegistrationChanged(
997       MM_MODEM_3GPP_REGISTRATION_STATE_IDLE,
998       home_provider,
999       ota_name);
1000   dispatcher_.DispatchPendingEvents();
1001   EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_IDLE,
1002             capability_->registration_state_);
1003   capability_->On3GPPRegistrationChanged(
1004       MM_MODEM_3GPP_REGISTRATION_STATE_SEARCHING,
1005       home_provider,
1006       ota_name);
1007   EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_SEARCHING,
1008             capability_->registration_state_);
1009 
1010   // Home --> Searching --> Home should never see Searching.
1011   EXPECT_CALL(*(modem_info_.mock_metrics()),
1012       Notify3GPPRegistrationDelayedDropPosted());
1013   EXPECT_CALL(*(modem_info_.mock_metrics()),
1014       Notify3GPPRegistrationDelayedDropCanceled());
1015 
1016   capability_->On3GPPRegistrationChanged(
1017       MM_MODEM_3GPP_REGISTRATION_STATE_HOME,
1018       home_provider,
1019       ota_name);
1020   EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_HOME,
1021             capability_->registration_state_);
1022   capability_->On3GPPRegistrationChanged(
1023       MM_MODEM_3GPP_REGISTRATION_STATE_SEARCHING,
1024       home_provider,
1025       ota_name);
1026   EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_HOME,
1027             capability_->registration_state_);
1028   capability_->On3GPPRegistrationChanged(
1029       MM_MODEM_3GPP_REGISTRATION_STATE_HOME,
1030       home_provider,
1031       ota_name);
1032   EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_HOME,
1033             capability_->registration_state_);
1034   dispatcher_.DispatchPendingEvents();
1035   EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_HOME,
1036             capability_->registration_state_);
1037   Mock::VerifyAndClearExpectations(modem_info_.mock_metrics());
1038 
1039   // Home --> Searching --> wait till dispatch should see Searching
1040   EXPECT_CALL(*(modem_info_.mock_metrics()),
1041       Notify3GPPRegistrationDelayedDropPosted());
1042   capability_->On3GPPRegistrationChanged(
1043       MM_MODEM_3GPP_REGISTRATION_STATE_HOME,
1044       home_provider,
1045       ota_name);
1046   EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_HOME,
1047             capability_->registration_state_);
1048   capability_->On3GPPRegistrationChanged(
1049       MM_MODEM_3GPP_REGISTRATION_STATE_SEARCHING,
1050       home_provider,
1051       ota_name);
1052   EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_HOME,
1053             capability_->registration_state_);
1054   dispatcher_.DispatchPendingEvents();
1055   EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_SEARCHING,
1056             capability_->registration_state_);
1057   Mock::VerifyAndClearExpectations(modem_info_.mock_metrics());
1058 
1059   // Home --> Searching --> Searching --> wait till dispatch should see
1060   // Searching *and* the first callback should be cancelled.
1061   EXPECT_CALL(*this, DummyCallback()).Times(0);
1062   EXPECT_CALL(*(modem_info_.mock_metrics()),
1063       Notify3GPPRegistrationDelayedDropPosted());
1064 
1065   capability_->On3GPPRegistrationChanged(
1066       MM_MODEM_3GPP_REGISTRATION_STATE_HOME,
1067       home_provider,
1068       ota_name);
1069   EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_HOME,
1070             capability_->registration_state_);
1071   capability_->On3GPPRegistrationChanged(
1072       MM_MODEM_3GPP_REGISTRATION_STATE_SEARCHING,
1073       home_provider,
1074       ota_name);
1075   SetMockRegistrationDroppedUpdateCallback();
1076   capability_->On3GPPRegistrationChanged(
1077       MM_MODEM_3GPP_REGISTRATION_STATE_SEARCHING,
1078       home_provider,
1079       ota_name);
1080   EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_HOME,
1081             capability_->registration_state_);
1082   dispatcher_.DispatchPendingEvents();
1083   EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_SEARCHING,
1084             capability_->registration_state_);
1085 }
1086 
TEST_F(CellularCapabilityUniversalMainTest,IsRegistered)1087 TEST_F(CellularCapabilityUniversalMainTest, IsRegistered) {
1088   capability_->registration_state_ = MM_MODEM_3GPP_REGISTRATION_STATE_IDLE;
1089   EXPECT_FALSE(capability_->IsRegistered());
1090 
1091   capability_->registration_state_ = MM_MODEM_3GPP_REGISTRATION_STATE_HOME;
1092   EXPECT_TRUE(capability_->IsRegistered());
1093 
1094   capability_->registration_state_ = MM_MODEM_3GPP_REGISTRATION_STATE_SEARCHING;
1095   EXPECT_FALSE(capability_->IsRegistered());
1096 
1097   capability_->registration_state_ = MM_MODEM_3GPP_REGISTRATION_STATE_DENIED;
1098   EXPECT_FALSE(capability_->IsRegistered());
1099 
1100   capability_->registration_state_ = MM_MODEM_3GPP_REGISTRATION_STATE_UNKNOWN;
1101   EXPECT_FALSE(capability_->IsRegistered());
1102 
1103   capability_->registration_state_ = MM_MODEM_3GPP_REGISTRATION_STATE_ROAMING;
1104   EXPECT_TRUE(capability_->IsRegistered());
1105 }
1106 
TEST_F(CellularCapabilityUniversalMainTest,UpdateRegistrationStateModemNotConnected)1107 TEST_F(CellularCapabilityUniversalMainTest,
1108        UpdateRegistrationStateModemNotConnected) {
1109   capability_->InitProxies();
1110   CreateService();
1111 
1112   cellular_->set_imsi("310240123456789");
1113   cellular_->set_modem_state(Cellular::kModemStateRegistered);
1114   SetRegistrationDroppedUpdateTimeout(0);
1115 
1116   const Stringmap& home_provider_map = cellular_->home_provider();
1117   ASSERT_NE(home_provider_map.end(), home_provider_map.find(kOperatorNameKey));
1118   string home_provider = home_provider_map.find(kOperatorNameKey)->second;
1119   string ota_name = cellular_->service_->friendly_name();
1120 
1121   // Home --> Searching should be effective immediately.
1122   capability_->On3GPPRegistrationChanged(
1123       MM_MODEM_3GPP_REGISTRATION_STATE_HOME,
1124       home_provider,
1125       ota_name);
1126   EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_HOME,
1127             capability_->registration_state_);
1128   capability_->On3GPPRegistrationChanged(
1129       MM_MODEM_3GPP_REGISTRATION_STATE_SEARCHING,
1130       home_provider,
1131       ota_name);
1132   EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_SEARCHING,
1133             capability_->registration_state_);
1134 }
1135 
TEST_F(CellularCapabilityUniversalMainTest,IsValidSimPath)1136 TEST_F(CellularCapabilityUniversalMainTest, IsValidSimPath) {
1137   // Invalid paths
1138   EXPECT_FALSE(capability_->IsValidSimPath(""));
1139   EXPECT_FALSE(capability_->IsValidSimPath("/"));
1140 
1141   // A valid path
1142   EXPECT_TRUE(capability_->IsValidSimPath(
1143       "/org/freedesktop/ModemManager1/SIM/0"));
1144 
1145   // Note that any string that is not one of the above invalid paths is
1146   // currently regarded as valid, since the ModemManager spec doesn't impose
1147   // a strict format on the path. The validity of this is subject to change.
1148   EXPECT_TRUE(capability_->IsValidSimPath("path"));
1149 }
1150 
TEST_F(CellularCapabilityUniversalMainTest,NormalizeMdn)1151 TEST_F(CellularCapabilityUniversalMainTest, NormalizeMdn) {
1152   EXPECT_EQ("", capability_->NormalizeMdn(""));
1153   EXPECT_EQ("12345678901", capability_->NormalizeMdn("12345678901"));
1154   EXPECT_EQ("12345678901", capability_->NormalizeMdn("+1 234 567 8901"));
1155   EXPECT_EQ("12345678901", capability_->NormalizeMdn("+1-234-567-8901"));
1156   EXPECT_EQ("12345678901", capability_->NormalizeMdn("+1 (234) 567-8901"));
1157   EXPECT_EQ("12345678901", capability_->NormalizeMdn("1 234  567 8901 "));
1158   EXPECT_EQ("2345678901", capability_->NormalizeMdn("(234) 567-8901"));
1159 }
1160 
TEST_F(CellularCapabilityUniversalMainTest,SimPathChanged)1161 TEST_F(CellularCapabilityUniversalMainTest, SimPathChanged) {
1162   // Set up mock modem SIM properties
1163   const char kImsi[] = "310100000001";
1164   const char kSimIdentifier[] = "9999888";
1165   const char kOperatorIdentifier[] = "310240";
1166   const char kOperatorName[] = "Custom SPN";
1167   KeyValueStore sim_properties;
1168   sim_properties.SetString(MM_SIM_PROPERTY_IMSI, kImsi);
1169   sim_properties.SetString(MM_SIM_PROPERTY_SIMIDENTIFIER, kSimIdentifier);
1170   sim_properties.SetString(MM_SIM_PROPERTY_OPERATORIDENTIFIER,
1171                            kOperatorIdentifier);
1172   sim_properties.SetString(MM_SIM_PROPERTY_OPERATORNAME, kOperatorName);
1173 
1174   EXPECT_CALL(*properties_proxy_, GetAll(MM_DBUS_INTERFACE_SIM))
1175       .Times(1).WillOnce(Return(sim_properties));
1176   EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1177               GetActivationState(PendingActivationStore::kIdentifierICCID, _))
1178       .Times(1);
1179 
1180   EXPECT_FALSE(cellular_->sim_present());
1181   EXPECT_EQ(nullptr, capability_->sim_proxy_);;
1182   EXPECT_EQ("", capability_->sim_path_);
1183   EXPECT_EQ("", cellular_->imsi());
1184   EXPECT_EQ("", cellular_->sim_identifier());
1185   EXPECT_EQ("", capability_->spn_);
1186 
1187   capability_->OnSimPathChanged(kSimPath);
1188   EXPECT_TRUE(cellular_->sim_present());
1189   EXPECT_NE(nullptr, capability_->sim_proxy_);;
1190   EXPECT_EQ(kSimPath, capability_->sim_path_);
1191   EXPECT_EQ(kImsi, cellular_->imsi());
1192   EXPECT_EQ(kSimIdentifier, cellular_->sim_identifier());
1193   EXPECT_EQ(kOperatorName, capability_->spn_);
1194 
1195   // Changing to the same SIM path should be a no-op.
1196   capability_->OnSimPathChanged(kSimPath);
1197   EXPECT_TRUE(cellular_->sim_present());
1198   EXPECT_NE(nullptr, capability_->sim_proxy_);;
1199   EXPECT_EQ(kSimPath, capability_->sim_path_);
1200   EXPECT_EQ(kImsi, cellular_->imsi());
1201   EXPECT_EQ(kSimIdentifier, cellular_->sim_identifier());
1202   EXPECT_EQ(kOperatorName, capability_->spn_);
1203 
1204   capability_->OnSimPathChanged("");
1205   Mock::VerifyAndClearExpectations(modem_info_.mock_pending_activation_store());
1206   Mock::VerifyAndClearExpectations(properties_proxy_.get());
1207   EXPECT_FALSE(cellular_->sim_present());
1208   EXPECT_EQ(nullptr, capability_->sim_proxy_);;
1209   EXPECT_EQ("", capability_->sim_path_);
1210   EXPECT_EQ("", cellular_->imsi());
1211   EXPECT_EQ("", cellular_->sim_identifier());
1212   EXPECT_EQ("", capability_->spn_);
1213 
1214   EXPECT_CALL(*properties_proxy_, GetAll(MM_DBUS_INTERFACE_SIM))
1215       .Times(1).WillOnce(Return(sim_properties));
1216   EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1217               GetActivationState(PendingActivationStore::kIdentifierICCID, _))
1218       .Times(1);
1219 
1220   capability_->OnSimPathChanged(kSimPath);
1221   EXPECT_TRUE(cellular_->sim_present());
1222   EXPECT_NE(nullptr, capability_->sim_proxy_);;
1223   EXPECT_EQ(kSimPath, capability_->sim_path_);
1224   EXPECT_EQ(kImsi, cellular_->imsi());
1225   EXPECT_EQ(kSimIdentifier, cellular_->sim_identifier());
1226   EXPECT_EQ(kOperatorName, capability_->spn_);
1227 
1228   capability_->OnSimPathChanged("/");
1229   EXPECT_FALSE(cellular_->sim_present());
1230   EXPECT_EQ(nullptr, capability_->sim_proxy_);;
1231   EXPECT_EQ("/", capability_->sim_path_);
1232   EXPECT_EQ("", cellular_->imsi());
1233   EXPECT_EQ("", cellular_->sim_identifier());
1234   EXPECT_EQ("", capability_->spn_);
1235 }
1236 
TEST_F(CellularCapabilityUniversalMainTest,SimPropertiesChanged)1237 TEST_F(CellularCapabilityUniversalMainTest, SimPropertiesChanged) {
1238   // Set up mock modem properties
1239   KeyValueStore modem_properties;
1240   modem_properties.SetRpcIdentifier(MM_MODEM_PROPERTY_SIM, kSimPath);
1241 
1242   // Set up mock modem sim properties
1243   const char kImsi[] = "310100000001";
1244   KeyValueStore sim_properties;
1245   sim_properties.SetString(MM_SIM_PROPERTY_IMSI, kImsi);
1246 
1247   EXPECT_CALL(*properties_proxy_, GetAll(MM_DBUS_INTERFACE_SIM))
1248       .WillOnce(Return(sim_properties));
1249   EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1250               GetActivationState(PendingActivationStore::kIdentifierICCID, _))
1251       .Times(0);
1252 
1253   EXPECT_FALSE(capability_->sim_proxy_.get());
1254   capability_->OnPropertiesChanged(MM_DBUS_INTERFACE_MODEM,
1255                                    modem_properties, vector<string>());
1256   EXPECT_EQ(kSimPath, capability_->sim_path_);
1257   EXPECT_TRUE(capability_->sim_proxy_.get());
1258   EXPECT_EQ(kImsi, cellular_->imsi());
1259   Mock::VerifyAndClearExpectations(modem_info_.mock_pending_activation_store());
1260 
1261   // Updating the SIM
1262   KeyValueStore new_properties;
1263   const char kNewImsi[] = "310240123456789";
1264   const char kSimIdentifier[] = "9999888";
1265   const char kOperatorIdentifier[] = "310240";
1266   const char kOperatorName[] = "Custom SPN";
1267   EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1268               GetActivationState(PendingActivationStore::kIdentifierICCID, _))
1269       .Times(2);
1270   EXPECT_CALL(*mock_home_provider_info_, UpdateIMSI(kNewImsi)).Times(2);
1271   new_properties.SetString(MM_SIM_PROPERTY_IMSI, kNewImsi);
1272   new_properties.SetString(MM_SIM_PROPERTY_SIMIDENTIFIER, kSimIdentifier);
1273   new_properties.SetString(MM_SIM_PROPERTY_OPERATORIDENTIFIER,
1274                            kOperatorIdentifier);
1275   capability_->OnPropertiesChanged(MM_DBUS_INTERFACE_SIM,
1276                                    new_properties,
1277                                    vector<string>());
1278   EXPECT_EQ(kNewImsi, cellular_->imsi());
1279   EXPECT_EQ(kSimIdentifier, cellular_->sim_identifier());
1280   EXPECT_EQ("", capability_->spn_);
1281 
1282   new_properties.SetString(MM_SIM_PROPERTY_OPERATORNAME, kOperatorName);
1283   capability_->OnPropertiesChanged(MM_DBUS_INTERFACE_SIM,
1284                                    new_properties,
1285                                    vector<string>());
1286   EXPECT_EQ(kOperatorName, capability_->spn_);
1287 }
1288 
1289 MATCHER_P(SizeIs, value, "") {
1290   return static_cast<size_t>(value) == arg.size();
1291 }
1292 
TEST_F(CellularCapabilityUniversalMainTest,Reset)1293 TEST_F(CellularCapabilityUniversalMainTest, Reset) {
1294   // Save pointers to proxies before they are lost by the call to InitProxies
1295   mm1::MockModemProxy* modem_proxy = modem_proxy_.get();
1296   EXPECT_CALL(*modem_proxy, set_state_changed_callback(_));
1297   capability_->InitProxies();
1298 
1299   Error error;
1300   ResultCallback reset_callback;
1301 
1302   EXPECT_CALL(*modem_proxy, Reset(_, _, CellularCapability::kTimeoutReset))
1303       .WillOnce(SaveArg<1>(&reset_callback));
1304 
1305   capability_->Reset(&error, ResultCallback());
1306   EXPECT_TRUE(capability_->resetting_);
1307   reset_callback.Run(error);
1308   EXPECT_FALSE(capability_->resetting_);
1309 }
1310 
TEST_F(CellularCapabilityUniversalMainTest,UpdateActiveBearer)1311 TEST_F(CellularCapabilityUniversalMainTest, UpdateActiveBearer) {
1312   // Common resources.
1313   const size_t kPathCount = 3;
1314   string active_paths[kPathCount], inactive_paths[kPathCount];
1315   for (size_t i = 0; i < kPathCount; ++i) {
1316     active_paths[i] = base::StringPrintf("%s/%zu", kActiveBearerPathPrefix, i);
1317     inactive_paths[i] =
1318         base::StringPrintf("%s/%zu", kInactiveBearerPathPrefix, i);
1319   }
1320 
1321   EXPECT_EQ(nullptr, capability_->GetActiveBearer());;
1322 
1323   // Check that |active_bearer_| is set correctly when an active bearer is
1324   // returned.
1325   capability_->OnBearersChanged({inactive_paths[0],
1326                                  inactive_paths[1],
1327                                  active_paths[2],
1328                                  inactive_paths[1],
1329                                  inactive_paths[2]});
1330   capability_->UpdateActiveBearer();
1331   ASSERT_NE(nullptr, capability_->GetActiveBearer());;
1332   EXPECT_EQ(active_paths[2], capability_->GetActiveBearer()->dbus_path());
1333 
1334   // Check that |active_bearer_| is nullptr if no active bearers are returned.
1335   capability_->OnBearersChanged({inactive_paths[0],
1336                                  inactive_paths[1],
1337                                  inactive_paths[2],
1338                                  inactive_paths[1]});
1339   capability_->UpdateActiveBearer();
1340   EXPECT_EQ(nullptr, capability_->GetActiveBearer());;
1341 
1342   // Check that returning multiple bearers causes death.
1343   capability_->OnBearersChanged({active_paths[0],
1344                                  inactive_paths[1],
1345                                  inactive_paths[2],
1346                                  active_paths[1],
1347                                  inactive_paths[1]});
1348   EXPECT_DEATH(capability_->UpdateActiveBearer(),
1349                "Found more than one active bearer.");
1350 
1351   capability_->OnBearersChanged({});
1352   capability_->UpdateActiveBearer();
1353   EXPECT_EQ(nullptr, capability_->GetActiveBearer());;
1354 }
1355 
1356 // Validates expected behavior of Connect function
TEST_F(CellularCapabilityUniversalMainTest,Connect)1357 TEST_F(CellularCapabilityUniversalMainTest, Connect) {
1358   mm1::MockModemSimpleProxy* modem_simple_proxy = modem_simple_proxy_.get();
1359   SetSimpleProxy();
1360   Error error;
1361   KeyValueStore properties;
1362   capability_->apn_try_list_.clear();
1363   ResultCallback callback =
1364       Bind(&CellularCapabilityUniversalTest::TestCallback, Unretained(this));
1365   string bearer("/foo");
1366 
1367   // Test connect failures
1368   EXPECT_CALL(*modem_simple_proxy, Connect(_, _, _, _))
1369       .WillRepeatedly(SaveArg<2>(&connect_callback_));
1370   capability_->Connect(properties, &error, callback);
1371   EXPECT_TRUE(error.IsSuccess());
1372   EXPECT_CALL(*this, TestCallback(IsFailure()));
1373   EXPECT_CALL(*service_, ClearLastGoodApn());
1374   connect_callback_.Run(bearer, Error(Error::kOperationFailed));
1375   Mock::VerifyAndClearExpectations(this);
1376 
1377   // Test connect success
1378   capability_->Connect(properties, &error, callback);
1379   EXPECT_TRUE(error.IsSuccess());
1380   EXPECT_CALL(*this, TestCallback(IsSuccess()));
1381   connect_callback_.Run(bearer, Error(Error::kSuccess));
1382   Mock::VerifyAndClearExpectations(this);
1383 
1384   // Test connect failures without a service.  Make sure that shill
1385   // does not crash if the connect failed and there is no
1386   // CellularService object.  This can happen if the modem is enabled
1387   // and then quickly disabled.
1388   cellular_->service_ = nullptr;
1389   EXPECT_FALSE(capability_->cellular()->service());
1390   capability_->Connect(properties, &error, callback);
1391   EXPECT_TRUE(error.IsSuccess());
1392   EXPECT_CALL(*this, TestCallback(IsFailure()));
1393   connect_callback_.Run(bearer, Error(Error::kOperationFailed));
1394 }
1395 
1396 // Validates Connect iterates over APNs
TEST_F(CellularCapabilityUniversalMainTest,ConnectApns)1397 TEST_F(CellularCapabilityUniversalMainTest, ConnectApns) {
1398   mm1::MockModemSimpleProxy* modem_simple_proxy = modem_simple_proxy_.get();
1399   SetSimpleProxy();
1400   Error error;
1401   KeyValueStore properties;
1402   capability_->apn_try_list_.clear();
1403   ResultCallback callback =
1404       Bind(&CellularCapabilityUniversalTest::TestCallback, Unretained(this));
1405   string bearer("/bearer0");
1406 
1407   const char apn_name_foo[] = "foo";
1408   const char apn_name_bar[] = "bar";
1409   EXPECT_CALL(*modem_simple_proxy, Connect(HasApn(apn_name_foo), _, _, _))
1410       .WillOnce(SaveArg<2>(&connect_callback_));
1411   Stringmap apn1;
1412   apn1[kApnProperty] = apn_name_foo;
1413   capability_->apn_try_list_.push_back(apn1);
1414   Stringmap apn2;
1415   apn2[kApnProperty] = apn_name_bar;
1416   capability_->apn_try_list_.push_back(apn2);
1417   capability_->FillConnectPropertyMap(&properties);
1418   capability_->Connect(properties, &error, callback);
1419   EXPECT_TRUE(error.IsSuccess());
1420   Mock::VerifyAndClearExpectations(modem_simple_proxy);
1421 
1422   EXPECT_CALL(*modem_simple_proxy, Connect(HasApn(apn_name_bar), _, _, _))
1423       .WillOnce(SaveArg<2>(&connect_callback_));
1424   EXPECT_CALL(*service_, ClearLastGoodApn());
1425   connect_callback_.Run(bearer, Error(Error::kInvalidApn));
1426 
1427   EXPECT_CALL(*service_, SetLastGoodApn(apn2));
1428   EXPECT_CALL(*this, TestCallback(IsSuccess()));
1429   connect_callback_.Run(bearer, Error(Error::kSuccess));
1430 }
1431 
1432 // Validates GetTypeString and AccessTechnologyToTechnologyFamily
TEST_F(CellularCapabilityUniversalMainTest,GetTypeString)1433 TEST_F(CellularCapabilityUniversalMainTest, GetTypeString) {
1434   const int gsm_technologies[] = {
1435     MM_MODEM_ACCESS_TECHNOLOGY_LTE,
1436     MM_MODEM_ACCESS_TECHNOLOGY_HSPA_PLUS,
1437     MM_MODEM_ACCESS_TECHNOLOGY_HSPA,
1438     MM_MODEM_ACCESS_TECHNOLOGY_HSUPA,
1439     MM_MODEM_ACCESS_TECHNOLOGY_HSDPA,
1440     MM_MODEM_ACCESS_TECHNOLOGY_UMTS,
1441     MM_MODEM_ACCESS_TECHNOLOGY_EDGE,
1442     MM_MODEM_ACCESS_TECHNOLOGY_GPRS,
1443     MM_MODEM_ACCESS_TECHNOLOGY_GSM_COMPACT,
1444     MM_MODEM_ACCESS_TECHNOLOGY_GSM,
1445     MM_MODEM_ACCESS_TECHNOLOGY_LTE | MM_MODEM_ACCESS_TECHNOLOGY_EVDO0,
1446     MM_MODEM_ACCESS_TECHNOLOGY_GSM | MM_MODEM_ACCESS_TECHNOLOGY_EVDO0,
1447     MM_MODEM_ACCESS_TECHNOLOGY_LTE | MM_MODEM_ACCESS_TECHNOLOGY_EVDOA,
1448     MM_MODEM_ACCESS_TECHNOLOGY_GSM | MM_MODEM_ACCESS_TECHNOLOGY_EVDOA,
1449     MM_MODEM_ACCESS_TECHNOLOGY_LTE | MM_MODEM_ACCESS_TECHNOLOGY_EVDOB,
1450     MM_MODEM_ACCESS_TECHNOLOGY_GSM | MM_MODEM_ACCESS_TECHNOLOGY_EVDOB,
1451     MM_MODEM_ACCESS_TECHNOLOGY_GSM | MM_MODEM_ACCESS_TECHNOLOGY_1XRTT,
1452   };
1453   for (size_t i = 0; i < arraysize(gsm_technologies); ++i) {
1454     capability_->access_technologies_ = gsm_technologies[i];
1455     ASSERT_EQ(capability_->GetTypeString(), kTechnologyFamilyGsm);
1456   }
1457   const int cdma_technologies[] = {
1458     MM_MODEM_ACCESS_TECHNOLOGY_EVDO0,
1459     MM_MODEM_ACCESS_TECHNOLOGY_EVDOA,
1460     MM_MODEM_ACCESS_TECHNOLOGY_EVDOA | MM_MODEM_ACCESS_TECHNOLOGY_EVDO0,
1461     MM_MODEM_ACCESS_TECHNOLOGY_EVDOB,
1462     MM_MODEM_ACCESS_TECHNOLOGY_EVDOB | MM_MODEM_ACCESS_TECHNOLOGY_EVDO0,
1463     MM_MODEM_ACCESS_TECHNOLOGY_1XRTT,
1464   };
1465   for (size_t i = 0; i < arraysize(cdma_technologies); ++i) {
1466     capability_->access_technologies_ = cdma_technologies[i];
1467     ASSERT_EQ(capability_->GetTypeString(), kTechnologyFamilyCdma);
1468   }
1469   capability_->access_technologies_ = MM_MODEM_ACCESS_TECHNOLOGY_UNKNOWN;
1470   ASSERT_EQ(capability_->GetTypeString(), "");
1471 }
1472 
TEST_F(CellularCapabilityUniversalMainTest,AllowRoaming)1473 TEST_F(CellularCapabilityUniversalMainTest, AllowRoaming) {
1474   EXPECT_FALSE(cellular_->allow_roaming_);
1475   EXPECT_FALSE(cellular_->provider_requires_roaming());
1476   EXPECT_FALSE(capability_->AllowRoaming());
1477   cellular_->set_provider_requires_roaming(true);
1478   EXPECT_TRUE(capability_->AllowRoaming());
1479   cellular_->set_provider_requires_roaming(false);
1480   cellular_->allow_roaming_ = true;
1481   EXPECT_TRUE(capability_->AllowRoaming());
1482 }
1483 
TEST_F(CellularCapabilityUniversalMainTest,GetMdnForOLP)1484 TEST_F(CellularCapabilityUniversalMainTest, GetMdnForOLP) {
1485   const string kVzwUUID = "c83d6597-dc91-4d48-a3a7-d86b80123751";
1486   const string kFooUUID = "foo";
1487   MockMobileOperatorInfo mock_operator_info(&dispatcher_,
1488                                             "MobileOperatorInfo");
1489 
1490   mock_operator_info.SetEmptyDefaultsForProperties();
1491   EXPECT_CALL(mock_operator_info, IsMobileNetworkOperatorKnown())
1492       .WillRepeatedly(Return(true));
1493   EXPECT_CALL(mock_operator_info, uuid()).WillRepeatedly(ReturnRef(kVzwUUID));
1494   capability_->subscription_state_ =
1495       CellularCapabilityUniversal::kSubscriptionStateUnknown;
1496 
1497   cellular_->set_mdn("");
1498   EXPECT_EQ("0000000000", capability_->GetMdnForOLP(&mock_operator_info));
1499   cellular_->set_mdn("0123456789");
1500   EXPECT_EQ("0123456789", capability_->GetMdnForOLP(&mock_operator_info));
1501   cellular_->set_mdn("10123456789");
1502   EXPECT_EQ("0123456789", capability_->GetMdnForOLP(&mock_operator_info));
1503 
1504   cellular_->set_mdn("1021232333");
1505   capability_->subscription_state_ =
1506       CellularCapabilityUniversal::kSubscriptionStateUnprovisioned;
1507   EXPECT_EQ("0000000000", capability_->GetMdnForOLP(&mock_operator_info));
1508   Mock::VerifyAndClearExpectations(&mock_operator_info);
1509 
1510   mock_operator_info.SetEmptyDefaultsForProperties();
1511   EXPECT_CALL(mock_operator_info, IsMobileNetworkOperatorKnown())
1512       .WillRepeatedly(Return(true));
1513   EXPECT_CALL(mock_operator_info, uuid()).WillRepeatedly(ReturnRef(kFooUUID));
1514 
1515   cellular_->set_mdn("");
1516   EXPECT_EQ("", capability_->GetMdnForOLP(&mock_operator_info));
1517   cellular_->set_mdn("0123456789");
1518   EXPECT_EQ("0123456789", capability_->GetMdnForOLP(&mock_operator_info));
1519   cellular_->set_mdn("10123456789");
1520   EXPECT_EQ("10123456789", capability_->GetMdnForOLP(&mock_operator_info));
1521 }
1522 
TEST_F(CellularCapabilityUniversalMainTest,UpdateServiceOLP)1523 TEST_F(CellularCapabilityUniversalMainTest, UpdateServiceOLP) {
1524   const MobileOperatorInfo::OnlinePortal kOlp {
1525       "http://testurl",
1526       "POST",
1527       "imei=${imei}&imsi=${imsi}&mdn=${mdn}&min=${min}&iccid=${iccid}"};
1528   const vector<MobileOperatorInfo::OnlinePortal> kOlpList {kOlp};
1529   const string kUuidVzw = "c83d6597-dc91-4d48-a3a7-d86b80123751";
1530   const string kUuidFoo = "foo";
1531 
1532   cellular_->set_imei("1");
1533   cellular_->set_imsi("2");
1534   cellular_->set_mdn("10123456789");
1535   cellular_->set_min("5");
1536   cellular_->set_sim_identifier("6");
1537 
1538   mock_home_provider_info_->SetEmptyDefaultsForProperties();
1539   EXPECT_CALL(*mock_home_provider_info_, IsMobileNetworkOperatorKnown())
1540       .WillRepeatedly(Return(true));
1541   EXPECT_CALL(*mock_home_provider_info_, olp_list())
1542       .WillRepeatedly(ReturnRef(kOlpList));
1543   EXPECT_CALL(*mock_home_provider_info_, uuid())
1544       .WillOnce(ReturnRef(kUuidVzw));
1545   CreateService();
1546   capability_->UpdateServiceOLP();
1547   // Copy to simplify assertions below.
1548   Stringmap vzw_olp = cellular_->service()->olp();
1549   EXPECT_EQ("http://testurl", vzw_olp[kPaymentPortalURL]);
1550   EXPECT_EQ("POST", vzw_olp[kPaymentPortalMethod]);
1551   EXPECT_EQ("imei=1&imsi=2&mdn=0123456789&min=5&iccid=6",
1552             vzw_olp[kPaymentPortalPostData]);
1553   Mock::VerifyAndClearExpectations(mock_home_provider_info_);
1554 
1555   mock_home_provider_info_->SetEmptyDefaultsForProperties();
1556   EXPECT_CALL(*mock_home_provider_info_, IsMobileNetworkOperatorKnown())
1557       .WillRepeatedly(Return(true));
1558   EXPECT_CALL(*mock_home_provider_info_, olp_list())
1559       .WillRepeatedly(ReturnRef(kOlpList));
1560   EXPECT_CALL(*mock_home_provider_info_, uuid())
1561       .WillOnce(ReturnRef(kUuidFoo));
1562   capability_->UpdateServiceOLP();
1563   // Copy to simplify assertions below.
1564   Stringmap olp = cellular_->service()->olp();
1565   EXPECT_EQ("http://testurl", olp[kPaymentPortalURL]);
1566   EXPECT_EQ("POST", olp[kPaymentPortalMethod]);
1567   EXPECT_EQ("imei=1&imsi=2&mdn=10123456789&min=5&iccid=6",
1568             olp[kPaymentPortalPostData]);
1569 }
1570 
TEST_F(CellularCapabilityUniversalMainTest,IsMdnValid)1571 TEST_F(CellularCapabilityUniversalMainTest, IsMdnValid) {
1572   cellular_->set_mdn("");
1573   EXPECT_FALSE(capability_->IsMdnValid());
1574   cellular_->set_mdn("0000000");
1575   EXPECT_FALSE(capability_->IsMdnValid());
1576   cellular_->set_mdn("0000001");
1577   EXPECT_TRUE(capability_->IsMdnValid());
1578   cellular_->set_mdn("1231223");
1579   EXPECT_TRUE(capability_->IsMdnValid());
1580 }
1581 
TEST_F(CellularCapabilityUniversalTimerTest,CompleteActivation)1582 TEST_F(CellularCapabilityUniversalTimerTest, CompleteActivation) {
1583   const char kIccid[] = "1234567";
1584 
1585   cellular_->set_sim_identifier(kIccid);
1586   EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1587               SetActivationState(PendingActivationStore::kIdentifierICCID,
1588                                  kIccid,
1589                                  PendingActivationStore::kStatePending))
1590       .Times(1);
1591   EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1592               GetActivationState(PendingActivationStore::kIdentifierICCID,
1593                                  kIccid))
1594       .WillOnce(Return(PendingActivationStore::kStatePending));
1595   EXPECT_CALL(*service_, SetActivationState(kActivationStateActivating))
1596       .Times(1);
1597   EXPECT_CALL(*modem_proxy_.get(), Reset(_, _, _)).Times(1);
1598   Error error;
1599   capability_->InitProxies();
1600   capability_->CompleteActivation(&error);
1601   Mock::VerifyAndClearExpectations(modem_info_.mock_pending_activation_store());
1602   Mock::VerifyAndClearExpectations(service_);
1603   Mock::VerifyAndClearExpectations(&mock_dispatcher_);
1604 }
1605 
TEST_F(CellularCapabilityUniversalMainTest,UpdateServiceActivationState)1606 TEST_F(CellularCapabilityUniversalMainTest, UpdateServiceActivationState) {
1607   const char kIccid[] = "1234567";
1608   const vector<MobileOperatorInfo::OnlinePortal> olp_list {
1609     {"some@url", "some_method", "some_post_data"}
1610   };
1611   capability_->subscription_state_ =
1612       CellularCapabilityUniversal::kSubscriptionStateUnprovisioned;
1613   cellular_->set_sim_identifier("");
1614   cellular_->set_mdn("0000000000");
1615   EXPECT_CALL(*mock_home_provider_info_, IsMobileNetworkOperatorKnown())
1616       .WillRepeatedly(Return(true));
1617   EXPECT_CALL(*mock_home_provider_info_, olp_list())
1618       .WillRepeatedly(ReturnRef(olp_list));
1619 
1620   service_->SetAutoConnect(false);
1621   EXPECT_CALL(*service_, SetActivationState(kActivationStateNotActivated))
1622       .Times(1);
1623   capability_->UpdateServiceActivationState();
1624   Mock::VerifyAndClearExpectations(service_);
1625   EXPECT_FALSE(service_->auto_connect());
1626 
1627   cellular_->set_mdn("1231231122");
1628   capability_->subscription_state_ =
1629       CellularCapabilityUniversal::kSubscriptionStateUnknown;
1630   EXPECT_CALL(*service_, SetActivationState(kActivationStateActivated))
1631       .Times(1);
1632   capability_->UpdateServiceActivationState();
1633   Mock::VerifyAndClearExpectations(service_);
1634   EXPECT_TRUE(service_->auto_connect());
1635 
1636   // Make sure we don't overwrite auto-connect if a service is already
1637   // activated before calling UpdateServiceActivationState().
1638   service_->SetAutoConnect(false);
1639   EXPECT_FALSE(service_->auto_connect());
1640   const string activation_state = kActivationStateActivated;
1641   EXPECT_CALL(*service_, activation_state())
1642       .WillOnce(ReturnRef(activation_state));
1643   EXPECT_CALL(*service_, SetActivationState(kActivationStateActivated))
1644       .Times(1);
1645   capability_->UpdateServiceActivationState();
1646   Mock::VerifyAndClearExpectations(service_);
1647   EXPECT_FALSE(service_->auto_connect());
1648 
1649   service_->SetAutoConnect(false);
1650   cellular_->set_mdn("0000000000");
1651   cellular_->set_sim_identifier(kIccid);
1652   EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1653               GetActivationState(PendingActivationStore::kIdentifierICCID,
1654                                  kIccid))
1655       .Times(1)
1656       .WillRepeatedly(Return(PendingActivationStore::kStatePending));
1657   EXPECT_CALL(*service_, SetActivationState(kActivationStateActivating))
1658       .Times(1);
1659   capability_->UpdateServiceActivationState();
1660   Mock::VerifyAndClearExpectations(service_);
1661   Mock::VerifyAndClearExpectations(modem_info_.mock_pending_activation_store());
1662   EXPECT_FALSE(service_->auto_connect());
1663 
1664   EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1665               GetActivationState(PendingActivationStore::kIdentifierICCID,
1666                                  kIccid))
1667       .Times(2)
1668       .WillRepeatedly(Return(PendingActivationStore::kStateActivated));
1669   EXPECT_CALL(*service_, SetActivationState(kActivationStateActivated))
1670       .Times(1);
1671   capability_->UpdateServiceActivationState();
1672   Mock::VerifyAndClearExpectations(service_);
1673   Mock::VerifyAndClearExpectations(modem_info_.mock_pending_activation_store());
1674   EXPECT_TRUE(service_->auto_connect());
1675 
1676   // SubscriptionStateUnprovisioned overrides valid MDN.
1677   capability_->subscription_state_ =
1678       CellularCapabilityUniversal::kSubscriptionStateUnprovisioned;
1679   cellular_->set_mdn("1231231122");
1680   cellular_->set_sim_identifier("");
1681   service_->SetAutoConnect(false);
1682   EXPECT_CALL(*service_, SetActivationState(kActivationStateNotActivated))
1683       .Times(1);
1684   capability_->UpdateServiceActivationState();
1685   Mock::VerifyAndClearExpectations(service_);
1686   EXPECT_FALSE(service_->auto_connect());
1687 
1688   // SubscriptionStateProvisioned overrides invalid MDN.
1689   capability_->subscription_state_ =
1690       CellularCapabilityUniversal::kSubscriptionStateProvisioned;
1691   cellular_->set_mdn("0000000000");
1692   cellular_->set_sim_identifier("");
1693   service_->SetAutoConnect(false);
1694   EXPECT_CALL(*service_, SetActivationState(kActivationStateActivated))
1695       .Times(1);
1696   capability_->UpdateServiceActivationState();
1697   Mock::VerifyAndClearExpectations(service_);
1698   EXPECT_TRUE(service_->auto_connect());
1699 }
1700 
TEST_F(CellularCapabilityUniversalMainTest,UpdatePendingActivationState)1701 TEST_F(CellularCapabilityUniversalMainTest, UpdatePendingActivationState) {
1702   const char kIccid[] = "1234567";
1703 
1704   capability_->InitProxies();
1705   capability_->registration_state_ =
1706       MM_MODEM_3GPP_REGISTRATION_STATE_SEARCHING;
1707 
1708   // No MDN, no ICCID.
1709   cellular_->set_mdn("0000000");
1710   capability_->subscription_state_ =
1711       CellularCapabilityUniversal::kSubscriptionStateUnknown;
1712   cellular_->set_sim_identifier("");
1713   EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1714               GetActivationState(PendingActivationStore::kIdentifierICCID, _))
1715       .Times(0);
1716   capability_->UpdatePendingActivationState();
1717   Mock::VerifyAndClearExpectations(modem_info_.mock_pending_activation_store());
1718 
1719   // Valid MDN, but subsciption_state_ Unprovisioned
1720   cellular_->set_mdn("1234567");
1721   capability_->subscription_state_ =
1722       CellularCapabilityUniversal::kSubscriptionStateUnprovisioned;
1723   cellular_->set_sim_identifier("");
1724   EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1725               GetActivationState(PendingActivationStore::kIdentifierICCID, _))
1726       .Times(0);
1727   capability_->UpdatePendingActivationState();
1728   Mock::VerifyAndClearExpectations(modem_info_.mock_pending_activation_store());
1729 
1730   // ICCID known.
1731   cellular_->set_sim_identifier(kIccid);
1732 
1733   // After the modem has reset.
1734   capability_->reset_done_ = true;
1735   EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1736               GetActivationState(PendingActivationStore::kIdentifierICCID,
1737                                  kIccid))
1738       .Times(1).WillOnce(Return(PendingActivationStore::kStatePending));
1739   EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1740               SetActivationState(PendingActivationStore::kIdentifierICCID,
1741                                  kIccid,
1742                                  PendingActivationStore::kStateActivated))
1743       .Times(1);
1744   capability_->UpdatePendingActivationState();
1745   Mock::VerifyAndClearExpectations(modem_info_.mock_pending_activation_store());
1746 
1747   // Not registered.
1748   capability_->registration_state_ =
1749       MM_MODEM_3GPP_REGISTRATION_STATE_SEARCHING;
1750   EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1751               GetActivationState(PendingActivationStore::kIdentifierICCID,
1752                                  kIccid))
1753       .Times(2).WillRepeatedly(Return(PendingActivationStore::kStateActivated));
1754   EXPECT_CALL(*service_, AutoConnect()).Times(0);
1755   capability_->UpdatePendingActivationState();
1756   Mock::VerifyAndClearExpectations(service_);
1757 
1758   // Service, registered.
1759   capability_->registration_state_ =
1760       MM_MODEM_3GPP_REGISTRATION_STATE_HOME;
1761   EXPECT_CALL(*service_, AutoConnect()).Times(1);
1762   capability_->UpdatePendingActivationState();
1763 
1764   cellular_->service_->activation_state_ = kActivationStateNotActivated;
1765 
1766   Mock::VerifyAndClearExpectations(service_);
1767   Mock::VerifyAndClearExpectations(modem_info_.mock_pending_activation_store());
1768 
1769   // Device is connected.
1770   cellular_->state_ = Cellular::kStateConnected;
1771   capability_->UpdatePendingActivationState();
1772 
1773   // Device is linked.
1774   cellular_->state_ = Cellular::kStateLinked;
1775   capability_->UpdatePendingActivationState();
1776 
1777   // Got valid MDN, subscription_state_ is kSubscriptionStateUnknown
1778   EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1779               RemoveEntry(PendingActivationStore::kIdentifierICCID, kIccid));
1780   cellular_->state_ = Cellular::kStateRegistered;
1781   cellular_->set_mdn("1020304");
1782   capability_->subscription_state_ =
1783       CellularCapabilityUniversal::kSubscriptionStateUnknown;
1784   capability_->UpdatePendingActivationState();
1785   Mock::VerifyAndClearExpectations(modem_info_.mock_pending_activation_store());
1786 
1787   // Got invalid MDN, subscription_state_ is kSubscriptionStateProvisioned
1788   EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1789               RemoveEntry(PendingActivationStore::kIdentifierICCID, kIccid));
1790   cellular_->state_ = Cellular::kStateRegistered;
1791   cellular_->set_mdn("0000000");
1792   capability_->subscription_state_ =
1793       CellularCapabilityUniversal::kSubscriptionStateProvisioned;
1794   capability_->UpdatePendingActivationState();
1795   Mock::VerifyAndClearExpectations(modem_info_.mock_pending_activation_store());
1796 }
1797 
TEST_F(CellularCapabilityUniversalMainTest,IsServiceActivationRequired)1798 TEST_F(CellularCapabilityUniversalMainTest, IsServiceActivationRequired) {
1799   const vector<MobileOperatorInfo::OnlinePortal> empty_list;
1800   const vector<MobileOperatorInfo::OnlinePortal> olp_list {
1801     {"some@url", "some_method", "some_post_data"}
1802   };
1803 
1804   capability_->subscription_state_ =
1805       CellularCapabilityUniversal::kSubscriptionStateProvisioned;
1806   EXPECT_FALSE(capability_->IsServiceActivationRequired());
1807 
1808   capability_->subscription_state_ =
1809       CellularCapabilityUniversal::kSubscriptionStateUnprovisioned;
1810   EXPECT_TRUE(capability_->IsServiceActivationRequired());
1811 
1812   capability_->subscription_state_ =
1813       CellularCapabilityUniversal::kSubscriptionStateUnknown;
1814   cellular_->set_mdn("0000000000");
1815   EXPECT_FALSE(capability_->IsServiceActivationRequired());
1816 
1817   EXPECT_CALL(*mock_home_provider_info_, IsMobileNetworkOperatorKnown())
1818       .WillRepeatedly(Return(false));
1819   EXPECT_FALSE(capability_->IsServiceActivationRequired());
1820   Mock::VerifyAndClearExpectations(mock_home_provider_info_);
1821 
1822   EXPECT_CALL(*mock_home_provider_info_, IsMobileNetworkOperatorKnown())
1823       .WillRepeatedly(Return(true));
1824   EXPECT_CALL(*mock_home_provider_info_, olp_list())
1825       .WillRepeatedly(ReturnRef(empty_list));
1826   EXPECT_FALSE(capability_->IsServiceActivationRequired());
1827   Mock::VerifyAndClearExpectations(mock_home_provider_info_);
1828 
1829   // Set expectations for all subsequent cases.
1830   EXPECT_CALL(*mock_home_provider_info_, IsMobileNetworkOperatorKnown())
1831       .WillRepeatedly(Return(true));
1832   EXPECT_CALL(*mock_home_provider_info_, olp_list())
1833       .WillRepeatedly(ReturnRef(olp_list));
1834 
1835   cellular_->set_mdn("");
1836   EXPECT_TRUE(capability_->IsServiceActivationRequired());
1837   cellular_->set_mdn("1234567890");
1838   EXPECT_FALSE(capability_->IsServiceActivationRequired());
1839   cellular_->set_mdn("0000000000");
1840   EXPECT_TRUE(capability_->IsServiceActivationRequired());
1841 
1842   const char kIccid[] = "1234567890";
1843   cellular_->set_sim_identifier(kIccid);
1844   EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1845               GetActivationState(PendingActivationStore::kIdentifierICCID,
1846                                  kIccid))
1847       .WillOnce(Return(PendingActivationStore::kStateActivated))
1848       .WillOnce(Return(PendingActivationStore::kStatePending))
1849       .WillOnce(Return(PendingActivationStore::kStateUnknown));
1850   EXPECT_FALSE(capability_->IsServiceActivationRequired());
1851   EXPECT_FALSE(capability_->IsServiceActivationRequired());
1852   EXPECT_TRUE(capability_->IsServiceActivationRequired());
1853   Mock::VerifyAndClearExpectations(modem_info_.mock_pending_activation_store());
1854 }
1855 
TEST_F(CellularCapabilityUniversalMainTest,OnModemCurrentCapabilitiesChanged)1856 TEST_F(CellularCapabilityUniversalMainTest, OnModemCurrentCapabilitiesChanged) {
1857   EXPECT_FALSE(cellular_->scanning_supported());
1858   capability_->OnModemCurrentCapabilitiesChanged(MM_MODEM_CAPABILITY_LTE);
1859   EXPECT_FALSE(cellular_->scanning_supported());
1860   capability_->OnModemCurrentCapabilitiesChanged(MM_MODEM_CAPABILITY_CDMA_EVDO);
1861   EXPECT_FALSE(cellular_->scanning_supported());
1862   capability_->OnModemCurrentCapabilitiesChanged(MM_MODEM_CAPABILITY_GSM_UMTS);
1863   EXPECT_TRUE(cellular_->scanning_supported());
1864   capability_->OnModemCurrentCapabilitiesChanged(
1865       MM_MODEM_CAPABILITY_GSM_UMTS | MM_MODEM_CAPABILITY_CDMA_EVDO);
1866   EXPECT_TRUE(cellular_->scanning_supported());
1867 }
1868 
TEST_F(CellularCapabilityUniversalMainTest,GetNetworkTechnologyStringOnE362)1869 TEST_F(CellularCapabilityUniversalMainTest, GetNetworkTechnologyStringOnE362) {
1870   cellular_->set_model_id("");;
1871   capability_->access_technologies_ = 0;
1872   EXPECT_TRUE(capability_->GetNetworkTechnologyString().empty());
1873 
1874   cellular_->set_mm_plugin(CellularCapabilityUniversal::kNovatelLTEMMPlugin);
1875   EXPECT_EQ(kNetworkTechnologyLte, capability_->GetNetworkTechnologyString());
1876 
1877   capability_->access_technologies_ = MM_MODEM_ACCESS_TECHNOLOGY_GPRS;
1878   EXPECT_EQ(kNetworkTechnologyLte, capability_->GetNetworkTechnologyString());
1879 
1880   cellular_->set_mm_plugin("");
1881   EXPECT_EQ(kNetworkTechnologyGprs, capability_->GetNetworkTechnologyString());
1882 }
1883 
TEST_F(CellularCapabilityUniversalMainTest,GetOutOfCreditsDetectionType)1884 TEST_F(CellularCapabilityUniversalMainTest, GetOutOfCreditsDetectionType) {
1885   cellular_->set_model_id("");;
1886   EXPECT_EQ(OutOfCreditsDetector::OOCTypeNone,
1887             capability_->GetOutOfCreditsDetectionType());
1888   cellular_->set_mm_plugin(CellularCapabilityUniversal::kAltairLTEMMPlugin);
1889   EXPECT_EQ(OutOfCreditsDetector::OOCTypeSubscriptionState,
1890             capability_->GetOutOfCreditsDetectionType());
1891 }
1892 
TEST_F(CellularCapabilityUniversalMainTest,SimLockStatusToProperty)1893 TEST_F(CellularCapabilityUniversalMainTest, SimLockStatusToProperty) {
1894   Error error;
1895   KeyValueStore store = capability_->SimLockStatusToProperty(&error);
1896   EXPECT_FALSE(store.GetBool(kSIMLockEnabledProperty));
1897   EXPECT_TRUE(store.GetString(kSIMLockTypeProperty).empty());
1898   EXPECT_EQ(0, store.GetUint(kSIMLockRetriesLeftProperty));
1899 
1900   capability_->sim_lock_status_.enabled = true;
1901   capability_->sim_lock_status_.retries_left = 3;
1902   capability_->sim_lock_status_.lock_type = MM_MODEM_LOCK_SIM_PIN;
1903   store = capability_->SimLockStatusToProperty(&error);
1904   EXPECT_TRUE(store.GetBool(kSIMLockEnabledProperty));
1905   EXPECT_EQ("sim-pin", store.GetString(kSIMLockTypeProperty));
1906   EXPECT_EQ(3, store.GetUint(kSIMLockRetriesLeftProperty));
1907 
1908   capability_->sim_lock_status_.lock_type = MM_MODEM_LOCK_SIM_PUK;
1909   store = capability_->SimLockStatusToProperty(&error);
1910   EXPECT_EQ("sim-puk", store.GetString(kSIMLockTypeProperty));
1911 
1912   capability_->sim_lock_status_.lock_type = MM_MODEM_LOCK_SIM_PIN2;
1913   store = capability_->SimLockStatusToProperty(&error);
1914   EXPECT_TRUE(store.GetString(kSIMLockTypeProperty).empty());
1915 
1916   capability_->sim_lock_status_.lock_type = MM_MODEM_LOCK_SIM_PUK2;
1917   store = capability_->SimLockStatusToProperty(&error);
1918   EXPECT_TRUE(store.GetString(kSIMLockTypeProperty).empty());
1919 }
1920 
TEST_F(CellularCapabilityUniversalMainTest,OnLockRetriesChanged)1921 TEST_F(CellularCapabilityUniversalMainTest, OnLockRetriesChanged) {
1922   CellularCapabilityUniversal::LockRetryData data;
1923   const uint32_t kDefaultRetries = 999;
1924 
1925   capability_->OnLockRetriesChanged(data);
1926   EXPECT_EQ(kDefaultRetries, capability_->sim_lock_status_.retries_left);
1927 
1928   data[MM_MODEM_LOCK_SIM_PIN] = 3;
1929   data[MM_MODEM_LOCK_SIM_PUK] = 10;
1930   capability_->OnLockRetriesChanged(data);
1931   EXPECT_EQ(3, capability_->sim_lock_status_.retries_left);
1932 
1933   capability_->sim_lock_status_.lock_type = MM_MODEM_LOCK_SIM_PUK;
1934   capability_->OnLockRetriesChanged(data);
1935   EXPECT_EQ(10, capability_->sim_lock_status_.retries_left);
1936 
1937   capability_->sim_lock_status_.lock_type = MM_MODEM_LOCK_SIM_PIN;
1938   capability_->OnLockRetriesChanged(data);
1939   EXPECT_EQ(3, capability_->sim_lock_status_.retries_left);
1940 
1941   data.clear();
1942   capability_->OnLockRetriesChanged(data);
1943   EXPECT_EQ(kDefaultRetries, capability_->sim_lock_status_.retries_left);
1944 }
1945 
TEST_F(CellularCapabilityUniversalMainTest,OnLockTypeChanged)1946 TEST_F(CellularCapabilityUniversalMainTest, OnLockTypeChanged) {
1947   EXPECT_EQ(MM_MODEM_LOCK_UNKNOWN, capability_->sim_lock_status_.lock_type);
1948 
1949   capability_->OnLockTypeChanged(MM_MODEM_LOCK_NONE);
1950   EXPECT_EQ(MM_MODEM_LOCK_NONE, capability_->sim_lock_status_.lock_type);
1951   EXPECT_FALSE(capability_->sim_lock_status_.enabled);
1952 
1953   capability_->OnLockTypeChanged(MM_MODEM_LOCK_SIM_PIN);
1954   EXPECT_EQ(MM_MODEM_LOCK_SIM_PIN, capability_->sim_lock_status_.lock_type);
1955   EXPECT_TRUE(capability_->sim_lock_status_.enabled);
1956 
1957   capability_->sim_lock_status_.enabled = false;
1958   capability_->OnLockTypeChanged(MM_MODEM_LOCK_SIM_PUK);
1959   EXPECT_EQ(MM_MODEM_LOCK_SIM_PUK, capability_->sim_lock_status_.lock_type);
1960   EXPECT_TRUE(capability_->sim_lock_status_.enabled);
1961 }
1962 
TEST_F(CellularCapabilityUniversalMainTest,OnSimLockPropertiesChanged)1963 TEST_F(CellularCapabilityUniversalMainTest, OnSimLockPropertiesChanged) {
1964   EXPECT_EQ(MM_MODEM_LOCK_UNKNOWN, capability_->sim_lock_status_.lock_type);
1965   EXPECT_EQ(0, capability_->sim_lock_status_.retries_left);
1966 
1967   KeyValueStore changed;
1968   vector<string> invalidated;
1969 
1970   capability_->OnModemPropertiesChanged(changed, invalidated);
1971   EXPECT_EQ(MM_MODEM_LOCK_UNKNOWN, capability_->sim_lock_status_.lock_type);
1972   EXPECT_EQ(0, capability_->sim_lock_status_.retries_left);
1973 
1974   // Unlock retries changed, but the SIM wasn't locked.
1975   CellularCapabilityUniversal::LockRetryData retry_data;
1976   retry_data[MM_MODEM_LOCK_SIM_PIN] = 3;
1977   changed.Set(MM_MODEM_PROPERTY_UNLOCKRETRIES, brillo::Any(retry_data));
1978 
1979   capability_->OnModemPropertiesChanged(changed, invalidated);
1980   EXPECT_EQ(MM_MODEM_LOCK_UNKNOWN, capability_->sim_lock_status_.lock_type);
1981   EXPECT_EQ(3, capability_->sim_lock_status_.retries_left);
1982 
1983   // Unlock retries changed and the SIM got locked.
1984   changed.SetUint(MM_MODEM_PROPERTY_UNLOCKREQUIRED,
1985                   static_cast<uint32_t>(MM_MODEM_LOCK_SIM_PIN));
1986   capability_->OnModemPropertiesChanged(changed, invalidated);
1987   EXPECT_EQ(MM_MODEM_LOCK_SIM_PIN, capability_->sim_lock_status_.lock_type);
1988   EXPECT_EQ(3, capability_->sim_lock_status_.retries_left);
1989 
1990   // Only unlock retries changed.
1991   changed.Remove(MM_MODEM_PROPERTY_UNLOCKREQUIRED);
1992   retry_data[MM_MODEM_LOCK_SIM_PIN] = 2;
1993   changed.Set(MM_MODEM_PROPERTY_UNLOCKRETRIES, brillo::Any(retry_data));
1994   capability_->OnModemPropertiesChanged(changed, invalidated);
1995   EXPECT_EQ(MM_MODEM_LOCK_SIM_PIN, capability_->sim_lock_status_.lock_type);
1996   EXPECT_EQ(2, capability_->sim_lock_status_.retries_left);
1997 
1998   // Unlock retries changed with a value that doesn't match the current
1999   // lock type. Default to whatever count is available.
2000   retry_data.clear();
2001   retry_data[MM_MODEM_LOCK_SIM_PIN2] = 2;
2002   changed.Set(MM_MODEM_PROPERTY_UNLOCKRETRIES, brillo::Any(retry_data));
2003   capability_->OnModemPropertiesChanged(changed, invalidated);
2004   EXPECT_EQ(MM_MODEM_LOCK_SIM_PIN, capability_->sim_lock_status_.lock_type);
2005   EXPECT_EQ(2, capability_->sim_lock_status_.retries_left);
2006 }
2007 
2008 }  // namespace shill
2009