Darin Petkov | ac635a8 | 2012-01-10 16:51:58 +0100 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "shill/cellular_service.h" |
| 6 | |
| 7 | #include <chromeos/dbus/service_constants.h> |
| 8 | #include <gtest/gtest.h> |
Christopher Wiley | 1582bdd | 2012-11-15 11:31:14 -0800 | [diff] [blame] | 9 | #include <mm/mm-modem.h> |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 10 | |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 11 | #include "shill/cellular_capability.h" |
Ben Chan | 3ecdf82 | 2012-08-06 12:29:23 -0700 | [diff] [blame] | 12 | #include "shill/cellular_capability_cdma.h" |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 13 | #include "shill/mock_adaptors.h" |
Thieu Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 14 | #include "shill/mock_cellular.h" |
Ben Chan | 9658044 | 2012-09-19 16:19:39 -0700 | [diff] [blame] | 15 | #include "shill/mock_manager.h" |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 16 | #include "shill/mock_metrics.h" |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 17 | #include "shill/mock_modem_info.h" |
Thieu Le | 43ce4d4 | 2013-10-04 16:08:55 -0700 | [diff] [blame] | 18 | #include "shill/mock_out_of_credits_detector.h" |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 19 | #include "shill/mock_profile.h" |
Ben Chan | 19f8397 | 2012-10-03 23:25:56 -0700 | [diff] [blame] | 20 | #include "shill/mock_store.h" |
Ben Chan | 3ecdf82 | 2012-08-06 12:29:23 -0700 | [diff] [blame] | 21 | #include "shill/nice_mock_control.h" |
| 22 | #include "shill/proxy_factory.h" |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 23 | #include "shill/service_property_change_test.h" |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 24 | |
Darin Petkov | 3133241 | 2012-01-28 01:50:02 +0100 | [diff] [blame] | 25 | using std::string; |
Darin Petkov | 9cb0268 | 2012-01-28 00:17:38 +0100 | [diff] [blame] | 26 | using testing::_; |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 27 | using testing::InSequence; |
mukesh agrawal | 81599e0 | 2013-04-22 16:40:00 -0700 | [diff] [blame] | 28 | using testing::Mock; |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 29 | using testing::NiceMock; |
Ben Chan | 9658044 | 2012-09-19 16:19:39 -0700 | [diff] [blame] | 30 | using testing::Return; |
mukesh agrawal | a70ec27 | 2013-08-19 16:08:31 -0700 | [diff] [blame] | 31 | using testing::SetArgumentPointee; |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 32 | |
| 33 | namespace shill { |
| 34 | |
| 35 | class CellularServiceTest : public testing::Test { |
| 36 | public: |
| 37 | CellularServiceTest() |
Prathmesh Prabhu | 0d36b4f | 2013-04-01 11:45:54 -0700 | [diff] [blame] | 38 | : modem_info_(NULL, &dispatcher_, NULL, NULL, NULL), |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 39 | device_(new MockCellular(&modem_info_, |
Thieu Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 40 | "usb0", |
| 41 | kAddress, |
| 42 | 3, |
| 43 | Cellular::kTypeCDMA, |
| 44 | "", |
| 45 | "", |
| 46 | "", |
Thieu Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 47 | ProxyFactory::GetInstance())), |
Prathmesh Prabhu | 0d36b4f | 2013-04-01 11:45:54 -0700 | [diff] [blame] | 48 | service_(new CellularService(&modem_info_, device_)), |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 49 | adaptor_(NULL) {} |
| 50 | |
| 51 | virtual ~CellularServiceTest() { |
| 52 | adaptor_ = NULL; |
| 53 | } |
| 54 | |
| 55 | virtual void SetUp() { |
| 56 | adaptor_ = |
Prathmesh Prabhu | 0d36b4f | 2013-04-01 11:45:54 -0700 | [diff] [blame] | 57 | dynamic_cast<ServiceMockAdaptor *>(service_->adaptor()); |
Thieu Le | 43ce4d4 | 2013-10-04 16:08:55 -0700 | [diff] [blame] | 58 | out_of_credits_detector_ = |
| 59 | new MockOutOfCreditsDetector(NULL, NULL, NULL, service_); |
| 60 | // Passes ownership. |
| 61 | service_->set_out_of_credits_detector(out_of_credits_detector_); |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Ben Chan | 3ecdf82 | 2012-08-06 12:29:23 -0700 | [diff] [blame] | 64 | CellularCapabilityCDMA *GetCapabilityCDMA() { |
| 65 | return dynamic_cast<CellularCapabilityCDMA *>(device_->capability_.get()); |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 66 | } |
| 67 | |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 68 | protected: |
Darin Petkov | 3133241 | 2012-01-28 01:50:02 +0100 | [diff] [blame] | 69 | static const char kAddress[]; |
| 70 | |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 71 | string GetFriendlyName() const { return service_->friendly_name(); } |
| 72 | |
Ben Chan | 9658044 | 2012-09-19 16:19:39 -0700 | [diff] [blame] | 73 | EventDispatcher dispatcher_; |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 74 | MockModemInfo modem_info_; |
Thieu Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 75 | scoped_refptr<MockCellular> device_; |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 76 | CellularServiceRefPtr service_; |
Prathmesh Prabhu | 0d36b4f | 2013-04-01 11:45:54 -0700 | [diff] [blame] | 77 | ServiceMockAdaptor *adaptor_; // Owned by |service_|. |
Thieu Le | 43ce4d4 | 2013-10-04 16:08:55 -0700 | [diff] [blame] | 78 | MockOutOfCreditsDetector *out_of_credits_detector_; // Owned by |service_|. |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 79 | }; |
| 80 | |
Darin Petkov | 3133241 | 2012-01-28 01:50:02 +0100 | [diff] [blame] | 81 | const char CellularServiceTest::kAddress[] = "000102030405"; |
| 82 | |
Thieu Le | 9fdf137 | 2012-05-14 15:20:48 -0700 | [diff] [blame] | 83 | TEST_F(CellularServiceTest, Constructor) { |
| 84 | EXPECT_TRUE(service_->connectable()); |
| 85 | } |
| 86 | |
Ben Chan | 9658044 | 2012-09-19 16:19:39 -0700 | [diff] [blame] | 87 | TEST_F(CellularServiceTest, SetActivationState) { |
| 88 | { |
| 89 | InSequence call_sequence; |
| 90 | EXPECT_CALL(*adaptor_, EmitStringChanged( |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 91 | kActivationStateProperty, |
| 92 | kActivationStateNotActivated)); |
Ben Chan | 9658044 | 2012-09-19 16:19:39 -0700 | [diff] [blame] | 93 | EXPECT_CALL(*adaptor_, EmitBoolChanged( |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 94 | kConnectableProperty, false)); |
Ben Chan | 9658044 | 2012-09-19 16:19:39 -0700 | [diff] [blame] | 95 | EXPECT_CALL(*adaptor_, EmitStringChanged( |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 96 | kActivationStateProperty, |
| 97 | kActivationStateActivating)); |
Ben Chan | 9658044 | 2012-09-19 16:19:39 -0700 | [diff] [blame] | 98 | EXPECT_CALL(*adaptor_, EmitBoolChanged( |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 99 | kConnectableProperty, true)); |
Ben Chan | 9658044 | 2012-09-19 16:19:39 -0700 | [diff] [blame] | 100 | EXPECT_CALL(*adaptor_, EmitStringChanged( |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 101 | kActivationStateProperty, |
| 102 | kActivationStatePartiallyActivated)); |
Ben Chan | 9658044 | 2012-09-19 16:19:39 -0700 | [diff] [blame] | 103 | EXPECT_CALL(*adaptor_, EmitStringChanged( |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 104 | kActivationStateProperty, |
| 105 | kActivationStateActivated)); |
Ben Chan | 9658044 | 2012-09-19 16:19:39 -0700 | [diff] [blame] | 106 | EXPECT_CALL(*adaptor_, EmitStringChanged( |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 107 | kActivationStateProperty, |
| 108 | kActivationStateNotActivated)); |
Ben Chan | 9658044 | 2012-09-19 16:19:39 -0700 | [diff] [blame] | 109 | EXPECT_CALL(*adaptor_, EmitBoolChanged( |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 110 | kConnectableProperty, false)); |
Ben Chan | 9658044 | 2012-09-19 16:19:39 -0700 | [diff] [blame] | 111 | } |
Prathmesh Prabhu | 0d36b4f | 2013-04-01 11:45:54 -0700 | [diff] [blame] | 112 | EXPECT_CALL(*modem_info_.mock_manager(), HasService(_)) |
| 113 | .WillRepeatedly(Return(false)); |
Ben Chan | 9658044 | 2012-09-19 16:19:39 -0700 | [diff] [blame] | 114 | |
| 115 | EXPECT_TRUE(service_->activation_state().empty()); |
| 116 | EXPECT_TRUE(service_->connectable()); |
| 117 | |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 118 | service_->SetActivationState(kActivationStateNotActivated); |
| 119 | EXPECT_EQ(kActivationStateNotActivated, service_->activation_state()); |
Ben Chan | 9658044 | 2012-09-19 16:19:39 -0700 | [diff] [blame] | 120 | EXPECT_FALSE(service_->connectable()); |
| 121 | |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 122 | service_->SetActivationState(kActivationStateActivating); |
| 123 | EXPECT_EQ(kActivationStateActivating, service_->activation_state()); |
Ben Chan | 9658044 | 2012-09-19 16:19:39 -0700 | [diff] [blame] | 124 | EXPECT_TRUE(service_->connectable()); |
| 125 | |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 126 | service_->SetActivationState(kActivationStatePartiallyActivated); |
| 127 | EXPECT_EQ(kActivationStatePartiallyActivated, service_->activation_state()); |
Ben Chan | 9658044 | 2012-09-19 16:19:39 -0700 | [diff] [blame] | 128 | EXPECT_TRUE(service_->connectable()); |
| 129 | |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 130 | service_->SetActivationState(kActivationStateActivated); |
| 131 | EXPECT_EQ(kActivationStateActivated, service_->activation_state()); |
Ben Chan | 9658044 | 2012-09-19 16:19:39 -0700 | [diff] [blame] | 132 | EXPECT_TRUE(service_->connectable()); |
| 133 | |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 134 | service_->SetActivationState(kActivationStateNotActivated); |
| 135 | EXPECT_EQ(kActivationStateNotActivated, service_->activation_state()); |
Ben Chan | 9658044 | 2012-09-19 16:19:39 -0700 | [diff] [blame] | 136 | EXPECT_FALSE(service_->connectable()); |
| 137 | } |
| 138 | |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 139 | TEST_F(CellularServiceTest, SetNetworkTechnology) { |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 140 | EXPECT_CALL(*adaptor_, EmitStringChanged(kNetworkTechnologyProperty, |
| 141 | kNetworkTechnologyUmts)); |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 142 | EXPECT_TRUE(service_->network_technology().empty()); |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 143 | service_->SetNetworkTechnology(kNetworkTechnologyUmts); |
| 144 | EXPECT_EQ(kNetworkTechnologyUmts, service_->network_technology()); |
| 145 | service_->SetNetworkTechnology(kNetworkTechnologyUmts); |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | TEST_F(CellularServiceTest, SetRoamingState) { |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 149 | EXPECT_CALL(*adaptor_, EmitStringChanged(kRoamingStateProperty, |
| 150 | kRoamingStateHome)); |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 151 | EXPECT_TRUE(service_->roaming_state().empty()); |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 152 | service_->SetRoamingState(kRoamingStateHome); |
| 153 | EXPECT_EQ(kRoamingStateHome, service_->roaming_state()); |
| 154 | service_->SetRoamingState(kRoamingStateHome); |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 155 | } |
| 156 | |
Darin Petkov | ac635a8 | 2012-01-10 16:51:58 +0100 | [diff] [blame] | 157 | TEST_F(CellularServiceTest, FriendlyName) { |
| 158 | static const char kCarrier[] = "Cellular Carrier"; |
Prathmesh Prabhu | 9f06c87 | 2013-11-21 14:08:23 -0800 | [diff] [blame] | 159 | device_->set_carrier(kCarrier); |
Prathmesh Prabhu | 0d36b4f | 2013-04-01 11:45:54 -0700 | [diff] [blame] | 160 | service_ = new CellularService(&modem_info_, device_); |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 161 | EXPECT_EQ(kCarrier, GetFriendlyName()); |
Darin Petkov | ac635a8 | 2012-01-10 16:51:58 +0100 | [diff] [blame] | 162 | } |
| 163 | |
Darin Petkov | 3133241 | 2012-01-28 01:50:02 +0100 | [diff] [blame] | 164 | TEST_F(CellularServiceTest, SetStorageIdentifier) { |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 165 | EXPECT_EQ(string(kTypeCellular) + "_" + |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 166 | kAddress + "_" + GetFriendlyName(), |
Darin Petkov | 3133241 | 2012-01-28 01:50:02 +0100 | [diff] [blame] | 167 | service_->GetStorageIdentifier()); |
| 168 | service_->SetStorageIdentifier("a b c"); |
| 169 | EXPECT_EQ("a_b_c", service_->GetStorageIdentifier()); |
| 170 | } |
| 171 | |
Darin Petkov | 9cb0268 | 2012-01-28 00:17:38 +0100 | [diff] [blame] | 172 | TEST_F(CellularServiceTest, SetServingOperator) { |
| 173 | EXPECT_CALL(*adaptor_, |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 174 | EmitStringmapChanged(kServingOperatorProperty, _)); |
Darin Petkov | 9cb0268 | 2012-01-28 00:17:38 +0100 | [diff] [blame] | 175 | static const char kCode[] = "123456"; |
| 176 | static const char kName[] = "Some Cellular Operator"; |
| 177 | Cellular::Operator oper; |
| 178 | service_->SetServingOperator(oper); |
| 179 | oper.SetCode(kCode); |
| 180 | oper.SetName(kName); |
| 181 | service_->SetServingOperator(oper); |
| 182 | EXPECT_EQ(kCode, service_->serving_operator().GetCode()); |
| 183 | EXPECT_EQ(kName, service_->serving_operator().GetName()); |
| 184 | service_->SetServingOperator(oper); |
| 185 | } |
| 186 | |
Darin Petkov | 381928f | 2012-02-02 23:00:12 +0100 | [diff] [blame] | 187 | TEST_F(CellularServiceTest, SetOLP) { |
| 188 | EXPECT_CALL(*adaptor_, |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 189 | EmitStringmapChanged(kPaymentPortalProperty, _)); |
Darin Petkov | 381928f | 2012-02-02 23:00:12 +0100 | [diff] [blame] | 190 | static const char kURL[] = "payment.url"; |
| 191 | static const char kMethod[] = "GET"; |
| 192 | CellularService::OLP olp; |
| 193 | service_->SetOLP(olp); |
| 194 | olp.SetURL(kURL); |
| 195 | olp.SetMethod(kMethod); |
| 196 | service_->SetOLP(olp); |
| 197 | EXPECT_EQ(kURL, service_->olp().GetURL()); |
| 198 | EXPECT_EQ(kMethod, service_->olp().GetMethod()); |
| 199 | service_->SetOLP(olp); |
| 200 | } |
| 201 | |
| 202 | TEST_F(CellularServiceTest, SetUsageURL) { |
| 203 | static const char kUsageURL[] = "usage.url"; |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 204 | EXPECT_CALL(*adaptor_, EmitStringChanged(kUsageURLProperty, |
Darin Petkov | 381928f | 2012-02-02 23:00:12 +0100 | [diff] [blame] | 205 | kUsageURL)); |
| 206 | EXPECT_TRUE(service_->usage_url().empty()); |
| 207 | service_->SetUsageURL(kUsageURL); |
| 208 | EXPECT_EQ(kUsageURL, service_->usage_url()); |
| 209 | service_->SetUsageURL(kUsageURL); |
| 210 | } |
| 211 | |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 212 | TEST_F(CellularServiceTest, SetApn) { |
| 213 | static const char kApn[] = "TheAPN"; |
| 214 | static const char kUsername[] = "commander.data"; |
| 215 | ProfileRefPtr profile(new NiceMock<MockProfile>( |
Prathmesh Prabhu | 0d36b4f | 2013-04-01 11:45:54 -0700 | [diff] [blame] | 216 | modem_info_.control_interface(), modem_info_.metrics(), |
| 217 | modem_info_.manager())); |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 218 | service_->set_profile(profile); |
| 219 | Error error; |
| 220 | Stringmap testapn; |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 221 | testapn[kApnProperty] = kApn; |
| 222 | testapn[kApnUsernameProperty] = kUsername; |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 223 | { |
| 224 | InSequence seq; |
| 225 | EXPECT_CALL(*adaptor_, |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 226 | EmitStringmapChanged(kCellularLastGoodApnProperty, |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 227 | _)); |
| 228 | EXPECT_CALL(*adaptor_, |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 229 | EmitStringmapChanged(kCellularApnProperty, _)); |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 230 | } |
| 231 | service_->SetApn(testapn, &error); |
| 232 | EXPECT_TRUE(error.IsSuccess()); |
| 233 | Stringmap resultapn = service_->GetApn(&error); |
| 234 | EXPECT_TRUE(error.IsSuccess()); |
| 235 | EXPECT_EQ(2, resultapn.size()); |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 236 | Stringmap::const_iterator it = resultapn.find(kApnProperty); |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 237 | EXPECT_TRUE(it != resultapn.end() && it->second == kApn); |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 238 | it = resultapn.find(kApnUsernameProperty); |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 239 | EXPECT_TRUE(it != resultapn.end() && it->second == kUsername); |
| 240 | EXPECT_FALSE(service_->GetUserSpecifiedApn() == NULL); |
| 241 | } |
| 242 | |
| 243 | TEST_F(CellularServiceTest, ClearApn) { |
| 244 | static const char kApn[] = "TheAPN"; |
| 245 | static const char kUsername[] = "commander.data"; |
| 246 | ProfileRefPtr profile(new NiceMock<MockProfile>( |
Prathmesh Prabhu | 0d36b4f | 2013-04-01 11:45:54 -0700 | [diff] [blame] | 247 | modem_info_.control_interface(), modem_info_.metrics(), |
| 248 | modem_info_.manager())); |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 249 | service_->set_profile(profile); |
| 250 | Error error; |
| 251 | // Set up an APN to make sure that it later gets cleared. |
| 252 | Stringmap testapn; |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 253 | testapn[kApnProperty] = kApn; |
| 254 | testapn[kApnUsernameProperty] = kUsername; |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 255 | { |
| 256 | InSequence seq; |
| 257 | EXPECT_CALL(*adaptor_, |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 258 | EmitStringmapChanged(kCellularLastGoodApnProperty, |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 259 | _)); |
| 260 | EXPECT_CALL(*adaptor_, |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 261 | EmitStringmapChanged(kCellularApnProperty, _)); |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 262 | } |
| 263 | service_->SetApn(testapn, &error); |
| 264 | Stringmap resultapn = service_->GetApn(&error); |
| 265 | ASSERT_TRUE(error.IsSuccess()); |
| 266 | ASSERT_EQ(2, service_->GetApn(&error).size()); |
| 267 | |
| 268 | Stringmap emptyapn; |
| 269 | EXPECT_CALL(*adaptor_, |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 270 | EmitStringmapChanged(kCellularLastGoodApnProperty, |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 271 | _)).Times(0); |
| 272 | EXPECT_CALL(*adaptor_, |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 273 | EmitStringmapChanged(kCellularApnProperty, _)).Times(1); |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 274 | service_->SetApn(emptyapn, &error); |
| 275 | EXPECT_TRUE(error.IsSuccess()); |
| 276 | resultapn = service_->GetApn(&error); |
| 277 | EXPECT_TRUE(resultapn.empty()); |
| 278 | EXPECT_TRUE(service_->GetUserSpecifiedApn() == NULL); |
| 279 | } |
| 280 | |
| 281 | TEST_F(CellularServiceTest, LastGoodApn) { |
| 282 | static const char kApn[] = "TheAPN"; |
| 283 | static const char kUsername[] = "commander.data"; |
| 284 | ProfileRefPtr profile(new NiceMock<MockProfile>( |
Prathmesh Prabhu | 0d36b4f | 2013-04-01 11:45:54 -0700 | [diff] [blame] | 285 | modem_info_.control_interface(), modem_info_.metrics(), |
| 286 | modem_info_.manager())); |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 287 | service_->set_profile(profile); |
| 288 | Stringmap testapn; |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 289 | testapn[kApnProperty] = kApn; |
| 290 | testapn[kApnUsernameProperty] = kUsername; |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 291 | EXPECT_CALL(*adaptor_, |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 292 | EmitStringmapChanged(kCellularLastGoodApnProperty, _)); |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 293 | service_->SetLastGoodApn(testapn); |
| 294 | Stringmap *resultapn = service_->GetLastGoodApn(); |
| 295 | EXPECT_FALSE(resultapn == NULL); |
| 296 | EXPECT_EQ(2, resultapn->size()); |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 297 | Stringmap::const_iterator it = resultapn->find(kApnProperty); |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 298 | EXPECT_TRUE(it != resultapn->end() && it->second == kApn); |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 299 | it = resultapn->find(kApnUsernameProperty); |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 300 | EXPECT_TRUE(it != resultapn->end() && it->second == kUsername); |
| 301 | // Now set the user-specified APN, and check that LastGoodApn got |
| 302 | // cleared. |
| 303 | Stringmap userapn; |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 304 | userapn[kApnProperty] = kApn; |
| 305 | userapn[kApnUsernameProperty] = kUsername; |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 306 | { |
| 307 | InSequence seq; |
| 308 | EXPECT_CALL(*adaptor_, |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 309 | EmitStringmapChanged(kCellularLastGoodApnProperty, |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 310 | _)); |
| 311 | EXPECT_CALL(*adaptor_, |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 312 | EmitStringmapChanged(kCellularApnProperty, _)); |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 313 | } |
| 314 | Error error; |
| 315 | service_->SetApn(userapn, &error); |
| 316 | EXPECT_TRUE(service_->GetLastGoodApn() == NULL); |
| 317 | } |
| 318 | |
Ben Chan | 19f8397 | 2012-10-03 23:25:56 -0700 | [diff] [blame] | 319 | TEST_F(CellularServiceTest, IsAutoConnectable) { |
| 320 | const char *reason = NULL; |
| 321 | |
Thieu Le | 43ce4d4 | 2013-10-04 16:08:55 -0700 | [diff] [blame] | 322 | ON_CALL(*out_of_credits_detector_, IsDetecting()) |
| 323 | .WillByDefault(Return(false)); |
| 324 | |
Ben Chan | 19f8397 | 2012-10-03 23:25:56 -0700 | [diff] [blame] | 325 | // Auto-connect should be suppressed if the device is not running. |
| 326 | device_->running_ = false; |
| 327 | EXPECT_FALSE(service_->IsAutoConnectable(&reason)); |
| 328 | EXPECT_STREQ(CellularService::kAutoConnDeviceDisabled, reason); |
| 329 | |
| 330 | device_->running_ = true; |
| 331 | |
Christopher Wiley | 1582bdd | 2012-11-15 11:31:14 -0800 | [diff] [blame] | 332 | // If we're waiting on a disconnect before an activation, don't auto-connect. |
| 333 | GetCapabilityCDMA()->activation_starting_ = true; |
| 334 | EXPECT_FALSE(service_->IsAutoConnectable(&reason)); |
| 335 | |
| 336 | // If we're waiting on an activation, also don't auto-connect. |
| 337 | GetCapabilityCDMA()->activation_starting_ = false; |
| 338 | GetCapabilityCDMA()->activation_state_ = |
| 339 | MM_MODEM_CDMA_ACTIVATION_STATE_ACTIVATING; |
| 340 | EXPECT_FALSE(service_->IsAutoConnectable(&reason)); |
| 341 | |
Thieu Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 342 | GetCapabilityCDMA()->activation_state_ = |
| 343 | MM_MODEM_CDMA_ACTIVATION_STATE_ACTIVATED; |
| 344 | |
| 345 | // Auto-connect should be suppressed if the we're undergoing an |
| 346 | // out-of-credits detection. |
Thieu Le | 43ce4d4 | 2013-10-04 16:08:55 -0700 | [diff] [blame] | 347 | EXPECT_CALL(*out_of_credits_detector_, IsDetecting()) |
| 348 | .WillOnce(Return(true)); |
Thieu Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 349 | EXPECT_FALSE(service_->IsAutoConnectable(&reason)); |
| 350 | EXPECT_STREQ(CellularService::kAutoConnOutOfCreditsDetectionInProgress, |
| 351 | reason); |
Thieu Le | 43ce4d4 | 2013-10-04 16:08:55 -0700 | [diff] [blame] | 352 | Mock::VerifyAndClearExpectations(out_of_credits_detector_); |
Thieu Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 353 | |
| 354 | // Auto-connect should be suppressed if we're out of credits. |
Thieu Le | 43ce4d4 | 2013-10-04 16:08:55 -0700 | [diff] [blame] | 355 | EXPECT_CALL(*out_of_credits_detector_, IsDetecting()) |
| 356 | .WillOnce(Return(false)); |
| 357 | EXPECT_CALL(*out_of_credits_detector_, out_of_credits()) |
| 358 | .WillOnce(Return(true)); |
Thieu Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 359 | EXPECT_FALSE(service_->IsAutoConnectable(&reason)); |
| 360 | EXPECT_STREQ(CellularService::kAutoConnOutOfCredits, reason); |
Thieu Le | 43ce4d4 | 2013-10-04 16:08:55 -0700 | [diff] [blame] | 361 | Mock::VerifyAndClearExpectations(out_of_credits_detector_); |
Thieu Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 362 | |
Thieu Le | 43ce4d4 | 2013-10-04 16:08:55 -0700 | [diff] [blame] | 363 | EXPECT_CALL(*out_of_credits_detector_, out_of_credits()) |
| 364 | .WillRepeatedly(Return(false)); |
Thieu Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 365 | |
Christopher Wiley | 1582bdd | 2012-11-15 11:31:14 -0800 | [diff] [blame] | 366 | // But other activation states are fine. |
| 367 | GetCapabilityCDMA()->activation_state_ = |
| 368 | MM_MODEM_CDMA_ACTIVATION_STATE_ACTIVATED; |
| 369 | EXPECT_TRUE(service_->IsAutoConnectable(&reason)); |
| 370 | GetCapabilityCDMA()->activation_state_ = |
| 371 | MM_MODEM_CDMA_ACTIVATION_STATE_NOT_ACTIVATED; |
| 372 | EXPECT_TRUE(service_->IsAutoConnectable(&reason)); |
| 373 | GetCapabilityCDMA()->activation_state_ = |
| 374 | MM_MODEM_CDMA_ACTIVATION_STATE_PARTIALLY_ACTIVATED; |
| 375 | EXPECT_TRUE(service_->IsAutoConnectable(&reason)); |
| 376 | |
mukesh agrawal | d477388 | 2013-08-14 17:21:14 -0700 | [diff] [blame] | 377 | // A PPP authentication failure means the Service is not auto-connectable. |
| 378 | service_->SetFailure(Service::kFailurePPPAuth); |
| 379 | EXPECT_FALSE(service_->IsAutoConnectable(&reason)); |
| 380 | |
| 381 | // Reset failure state, to make the Service auto-connectable again. |
| 382 | service_->SetState(Service::kStateIdle); |
| 383 | EXPECT_TRUE(service_->IsAutoConnectable(&reason)); |
| 384 | |
Ben Chan | 19f8397 | 2012-10-03 23:25:56 -0700 | [diff] [blame] | 385 | // The following test cases are copied from ServiceTest.IsAutoConnectable |
| 386 | |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 387 | service_->SetConnectable(true); |
Ben Chan | 19f8397 | 2012-10-03 23:25:56 -0700 | [diff] [blame] | 388 | EXPECT_TRUE(service_->IsAutoConnectable(&reason)); |
| 389 | |
| 390 | // We should not auto-connect to a Service that a user has |
| 391 | // deliberately disconnected. |
| 392 | Error error; |
| 393 | service_->UserInitiatedDisconnect(&error); |
| 394 | EXPECT_FALSE(service_->IsAutoConnectable(&reason)); |
| 395 | EXPECT_STREQ(Service::kAutoConnExplicitDisconnect, reason); |
| 396 | |
| 397 | // But if the Service is reloaded, it is eligible for auto-connect |
| 398 | // again. |
| 399 | NiceMock<MockStore> storage; |
| 400 | EXPECT_CALL(storage, ContainsGroup(service_->GetStorageIdentifier())) |
| 401 | .WillOnce(Return(true)); |
| 402 | EXPECT_TRUE(service_->Load(&storage)); |
| 403 | EXPECT_TRUE(service_->IsAutoConnectable(&reason)); |
| 404 | |
| 405 | // A non-user initiated Disconnect doesn't change anything. |
| 406 | service_->Disconnect(&error); |
| 407 | EXPECT_TRUE(service_->IsAutoConnectable(&reason)); |
| 408 | |
| 409 | // A resume also re-enables auto-connect. |
| 410 | service_->UserInitiatedDisconnect(&error); |
| 411 | EXPECT_FALSE(service_->IsAutoConnectable(&reason)); |
| 412 | service_->OnAfterResume(); |
| 413 | EXPECT_TRUE(service_->IsAutoConnectable(&reason)); |
| 414 | |
| 415 | service_->SetState(Service::kStateConnected); |
| 416 | EXPECT_FALSE(service_->IsAutoConnectable(&reason)); |
| 417 | EXPECT_STREQ(Service::kAutoConnConnected, reason); |
| 418 | |
| 419 | service_->SetState(Service::kStateAssociating); |
| 420 | EXPECT_FALSE(service_->IsAutoConnectable(&reason)); |
| 421 | EXPECT_STREQ(Service::kAutoConnConnecting, reason); |
| 422 | } |
| 423 | |
mukesh agrawal | a70ec27 | 2013-08-19 16:08:31 -0700 | [diff] [blame] | 424 | TEST_F(CellularServiceTest, LoadResetsPPPAuthFailure) { |
| 425 | NiceMock<MockStore> storage; |
| 426 | EXPECT_CALL(storage, ContainsGroup(_)).WillRepeatedly(Return(true)); |
| 427 | EXPECT_CALL(storage, GetString(_, _, _)).WillRepeatedly(Return(true)); |
| 428 | |
| 429 | const string kDefaultUser; |
| 430 | const string kDefaultPass; |
| 431 | const string kNewUser("new-username"); |
| 432 | const string kNewPass("new-password"); |
| 433 | for (const auto change_username : { false, true }) { |
| 434 | for (const auto change_password : { false, true }) { |
| 435 | service_->ppp_username_ = kDefaultUser; |
| 436 | service_->ppp_password_ = kDefaultPass; |
| 437 | service_->SetFailure(Service::kFailurePPPAuth); |
| 438 | EXPECT_TRUE(service_->IsFailed()); |
| 439 | EXPECT_EQ(Service::kFailurePPPAuth, service_->failure()); |
| 440 | if (change_username) { |
| 441 | EXPECT_CALL(storage, |
| 442 | GetString(_, CellularService::kStoragePPPUsername, _)) |
| 443 | .WillOnce(DoAll(SetArgumentPointee<2>(kNewUser), Return(true))) |
| 444 | .RetiresOnSaturation(); |
| 445 | } |
| 446 | if (change_password) { |
| 447 | EXPECT_CALL(storage, |
| 448 | GetString(_, CellularService::kStoragePPPPassword, _)) |
| 449 | .WillOnce(DoAll(SetArgumentPointee<2>(kNewPass), Return(true))) |
| 450 | .RetiresOnSaturation(); |
| 451 | } |
| 452 | EXPECT_TRUE(service_->Load(&storage)); |
| 453 | if (change_username || change_password) { |
| 454 | EXPECT_NE(Service::kFailurePPPAuth, service_->failure()); |
| 455 | } else { |
| 456 | EXPECT_EQ(Service::kFailurePPPAuth, service_->failure()); |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | |
mukesh agrawal | 81599e0 | 2013-04-22 16:40:00 -0700 | [diff] [blame] | 462 | // Some of these tests duplicate signals tested above. However, it's |
| 463 | // convenient to have all the property change notifications documented |
| 464 | // (and tested) in one place. |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 465 | TEST_F(CellularServiceTest, PropertyChanges) { |
| 466 | TestCommonPropertyChanges(service_, adaptor_); |
| 467 | TestAutoConnectPropertyChange(service_, adaptor_); |
mukesh agrawal | 81599e0 | 2013-04-22 16:40:00 -0700 | [diff] [blame] | 468 | |
| 469 | bool activate_over_non_cellular = |
| 470 | service_->activate_over_non_cellular_network(); |
| 471 | EXPECT_CALL(*adaptor_, |
| 472 | EmitBoolChanged(kActivateOverNonCellularNetworkProperty, _)); |
| 473 | service_->SetActivateOverNonCellularNetwork(!activate_over_non_cellular); |
| 474 | Mock::VerifyAndClearExpectations(adaptor_); |
| 475 | |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 476 | EXPECT_NE(kActivationStateNotActivated, service_->activation_state()); |
| 477 | EXPECT_CALL(*adaptor_, EmitStringChanged(kActivationStateProperty, _)); |
| 478 | service_->SetActivationState(kActivationStateNotActivated); |
mukesh agrawal | 81599e0 | 2013-04-22 16:40:00 -0700 | [diff] [blame] | 479 | Mock::VerifyAndClearExpectations(adaptor_); |
| 480 | |
| 481 | string network_technology = service_->network_technology(); |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 482 | EXPECT_CALL(*adaptor_, EmitStringChanged(kNetworkTechnologyProperty, _)); |
mukesh agrawal | 81599e0 | 2013-04-22 16:40:00 -0700 | [diff] [blame] | 483 | service_->SetNetworkTechnology(network_technology + "and some new stuff"); |
| 484 | Mock::VerifyAndClearExpectations(adaptor_); |
| 485 | |
Thieu Le | 43ce4d4 | 2013-10-04 16:08:55 -0700 | [diff] [blame] | 486 | bool out_of_credits = true; |
| 487 | EXPECT_CALL(*adaptor_, |
| 488 | EmitBoolChanged(kOutOfCreditsProperty, out_of_credits)); |
| 489 | service_->SignalOutOfCreditsChanged(out_of_credits); |
mukesh agrawal | 81599e0 | 2013-04-22 16:40:00 -0700 | [diff] [blame] | 490 | Mock::VerifyAndClearExpectations(adaptor_); |
| 491 | |
| 492 | string roaming_state = service_->roaming_state(); |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 493 | EXPECT_CALL(*adaptor_, EmitStringChanged(kRoamingStateProperty, _)); |
mukesh agrawal | 81599e0 | 2013-04-22 16:40:00 -0700 | [diff] [blame] | 494 | service_->SetRoamingState(roaming_state + "and some new stuff"); |
| 495 | Mock::VerifyAndClearExpectations(adaptor_); |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 496 | } |
| 497 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 498 | // Custom property setters should return false, and make no changes, if |
| 499 | // the new value is the same as the old value. |
| 500 | TEST_F(CellularServiceTest, CustomSetterNoopChange) { |
| 501 | // Test that we didn't break any setters provided by the base class. |
| 502 | TestCustomSetterNoopChange(service_, modem_info_.mock_manager()); |
| 503 | |
| 504 | // Test the new setter we added. |
| 505 | // First set up our environment... |
| 506 | static const char kApn[] = "TheAPN"; |
| 507 | static const char kUsername[] = "commander.data"; |
| 508 | Error error; |
| 509 | Stringmap testapn; |
| 510 | ProfileRefPtr profile(new NiceMock<MockProfile>(nullptr, nullptr, nullptr)); |
| 511 | service_->set_profile(profile); |
Ben Chan | 7ea768e | 2013-09-20 15:08:40 -0700 | [diff] [blame] | 512 | testapn[kApnProperty] = kApn; |
| 513 | testapn[kApnUsernameProperty] = kUsername; |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 514 | // ... then set to a known value ... |
| 515 | EXPECT_TRUE(service_->SetApn(testapn, &error)); |
| 516 | EXPECT_TRUE(error.IsSuccess()); |
| 517 | // ... then set to same value. |
| 518 | EXPECT_FALSE(service_->SetApn(testapn, &error)); |
| 519 | EXPECT_TRUE(error.IsSuccess()); |
| 520 | } |
| 521 | |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 522 | } // namespace shill |