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> |
| 9 | |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 10 | #include "shill/cellular_capability.h" |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 11 | #include "shill/cellular_capability_gsm.h" |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 12 | #include "shill/nice_mock_control.h" |
| 13 | #include "shill/mock_adaptors.h" |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 14 | #include "shill/mock_metrics.h" |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 15 | #include "shill/mock_profile.h" |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 16 | |
Darin Petkov | 3133241 | 2012-01-28 01:50:02 +0100 | [diff] [blame] | 17 | using std::string; |
Darin Petkov | 9cb0268 | 2012-01-28 00:17:38 +0100 | [diff] [blame] | 18 | using testing::_; |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 19 | using testing::InSequence; |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 20 | using testing::NiceMock; |
| 21 | |
| 22 | namespace shill { |
| 23 | |
| 24 | class CellularServiceTest : public testing::Test { |
| 25 | public: |
| 26 | CellularServiceTest() |
Darin Petkov | ac635a8 | 2012-01-10 16:51:58 +0100 | [diff] [blame] | 27 | : device_(new Cellular(&control_, |
| 28 | NULL, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 29 | &metrics_, |
Darin Petkov | ac635a8 | 2012-01-10 16:51:58 +0100 | [diff] [blame] | 30 | NULL, |
| 31 | "usb0", |
Darin Petkov | 3133241 | 2012-01-28 01:50:02 +0100 | [diff] [blame] | 32 | kAddress, |
Darin Petkov | ac635a8 | 2012-01-10 16:51:58 +0100 | [diff] [blame] | 33 | 3, |
| 34 | Cellular::kTypeGSM, |
| 35 | "", |
| 36 | "", |
Jason Glasgow | a585fc3 | 2012-06-06 11:04:09 -0400 | [diff] [blame] | 37 | "", |
Darin Petkov | ac635a8 | 2012-01-10 16:51:58 +0100 | [diff] [blame] | 38 | NULL)), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 39 | service_(new CellularService(&control_, NULL, &metrics_, NULL, |
| 40 | device_)), |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 41 | adaptor_(NULL) {} |
| 42 | |
| 43 | virtual ~CellularServiceTest() { |
| 44 | adaptor_ = NULL; |
| 45 | } |
| 46 | |
| 47 | virtual void SetUp() { |
| 48 | adaptor_ = |
| 49 | dynamic_cast<NiceMock<ServiceMockAdaptor> *>(service_->adaptor()); |
| 50 | } |
| 51 | |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 52 | CellularCapabilityGSM *GetCapabilityGSM() { |
| 53 | return dynamic_cast<CellularCapabilityGSM *>(device_->capability_.get()); |
| 54 | } |
| 55 | |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 56 | protected: |
Darin Petkov | 3133241 | 2012-01-28 01:50:02 +0100 | [diff] [blame] | 57 | static const char kAddress[]; |
| 58 | |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 59 | NiceMockControl control_; |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 60 | MockMetrics metrics_; |
Darin Petkov | ac635a8 | 2012-01-10 16:51:58 +0100 | [diff] [blame] | 61 | CellularRefPtr device_; |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 62 | CellularServiceRefPtr service_; |
| 63 | NiceMock<ServiceMockAdaptor> *adaptor_; // Owned by |service_|. |
| 64 | }; |
| 65 | |
Darin Petkov | 3133241 | 2012-01-28 01:50:02 +0100 | [diff] [blame] | 66 | const char CellularServiceTest::kAddress[] = "000102030405"; |
| 67 | |
Thieu Le | 9fdf137 | 2012-05-14 15:20:48 -0700 | [diff] [blame] | 68 | TEST_F(CellularServiceTest, Constructor) { |
| 69 | EXPECT_TRUE(service_->connectable()); |
| 70 | } |
| 71 | |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 72 | TEST_F(CellularServiceTest, SetNetworkTechnology) { |
| 73 | EXPECT_CALL(*adaptor_, EmitStringChanged(flimflam::kNetworkTechnologyProperty, |
| 74 | flimflam::kNetworkTechnologyUmts)); |
| 75 | EXPECT_TRUE(service_->network_technology().empty()); |
| 76 | service_->SetNetworkTechnology(flimflam::kNetworkTechnologyUmts); |
| 77 | EXPECT_EQ(flimflam::kNetworkTechnologyUmts, service_->network_technology()); |
| 78 | service_->SetNetworkTechnology(flimflam::kNetworkTechnologyUmts); |
| 79 | } |
| 80 | |
| 81 | TEST_F(CellularServiceTest, SetRoamingState) { |
| 82 | EXPECT_CALL(*adaptor_, EmitStringChanged(flimflam::kRoamingStateProperty, |
| 83 | flimflam::kRoamingStateHome)); |
| 84 | EXPECT_TRUE(service_->roaming_state().empty()); |
| 85 | service_->SetRoamingState(flimflam::kRoamingStateHome); |
| 86 | EXPECT_EQ(flimflam::kRoamingStateHome, service_->roaming_state()); |
| 87 | service_->SetRoamingState(flimflam::kRoamingStateHome); |
| 88 | } |
| 89 | |
Darin Petkov | ac635a8 | 2012-01-10 16:51:58 +0100 | [diff] [blame] | 90 | TEST_F(CellularServiceTest, FriendlyName) { |
| 91 | static const char kCarrier[] = "Cellular Carrier"; |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 92 | GetCapabilityGSM()->carrier_ = kCarrier; |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 93 | service_ = new CellularService(&control_, NULL, &metrics_, NULL, device_); |
Darin Petkov | ac635a8 | 2012-01-10 16:51:58 +0100 | [diff] [blame] | 94 | EXPECT_EQ(kCarrier, service_->friendly_name()); |
| 95 | } |
| 96 | |
Darin Petkov | 3133241 | 2012-01-28 01:50:02 +0100 | [diff] [blame] | 97 | TEST_F(CellularServiceTest, SetStorageIdentifier) { |
Darin Petkov | dd3e866 | 2012-02-03 13:16:20 +0100 | [diff] [blame] | 98 | EXPECT_EQ(string(flimflam::kTypeCellular) + "_" + |
| 99 | kAddress + "_" + service_->friendly_name(), |
Darin Petkov | 3133241 | 2012-01-28 01:50:02 +0100 | [diff] [blame] | 100 | service_->GetStorageIdentifier()); |
| 101 | service_->SetStorageIdentifier("a b c"); |
| 102 | EXPECT_EQ("a_b_c", service_->GetStorageIdentifier()); |
| 103 | } |
| 104 | |
Darin Petkov | 9cb0268 | 2012-01-28 00:17:38 +0100 | [diff] [blame] | 105 | TEST_F(CellularServiceTest, SetServingOperator) { |
| 106 | EXPECT_CALL(*adaptor_, |
| 107 | EmitStringmapChanged(flimflam::kServingOperatorProperty, _)); |
| 108 | static const char kCode[] = "123456"; |
| 109 | static const char kName[] = "Some Cellular Operator"; |
| 110 | Cellular::Operator oper; |
| 111 | service_->SetServingOperator(oper); |
| 112 | oper.SetCode(kCode); |
| 113 | oper.SetName(kName); |
| 114 | service_->SetServingOperator(oper); |
| 115 | EXPECT_EQ(kCode, service_->serving_operator().GetCode()); |
| 116 | EXPECT_EQ(kName, service_->serving_operator().GetName()); |
| 117 | service_->SetServingOperator(oper); |
| 118 | } |
| 119 | |
Darin Petkov | 381928f | 2012-02-02 23:00:12 +0100 | [diff] [blame] | 120 | TEST_F(CellularServiceTest, SetOLP) { |
| 121 | EXPECT_CALL(*adaptor_, |
| 122 | EmitStringmapChanged(flimflam::kPaymentPortalProperty, _)); |
| 123 | static const char kURL[] = "payment.url"; |
| 124 | static const char kMethod[] = "GET"; |
| 125 | CellularService::OLP olp; |
| 126 | service_->SetOLP(olp); |
| 127 | olp.SetURL(kURL); |
| 128 | olp.SetMethod(kMethod); |
| 129 | service_->SetOLP(olp); |
| 130 | EXPECT_EQ(kURL, service_->olp().GetURL()); |
| 131 | EXPECT_EQ(kMethod, service_->olp().GetMethod()); |
| 132 | service_->SetOLP(olp); |
| 133 | } |
| 134 | |
| 135 | TEST_F(CellularServiceTest, SetUsageURL) { |
| 136 | static const char kUsageURL[] = "usage.url"; |
| 137 | EXPECT_CALL(*adaptor_, EmitStringChanged(flimflam::kUsageURLProperty, |
| 138 | kUsageURL)); |
| 139 | EXPECT_TRUE(service_->usage_url().empty()); |
| 140 | service_->SetUsageURL(kUsageURL); |
| 141 | EXPECT_EQ(kUsageURL, service_->usage_url()); |
| 142 | service_->SetUsageURL(kUsageURL); |
| 143 | } |
| 144 | |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 145 | TEST_F(CellularServiceTest, SetApn) { |
| 146 | static const char kApn[] = "TheAPN"; |
| 147 | static const char kUsername[] = "commander.data"; |
| 148 | ProfileRefPtr profile(new NiceMock<MockProfile>( |
| 149 | &control_, reinterpret_cast<Manager *>(NULL))); |
| 150 | service_->set_profile(profile); |
| 151 | Error error; |
| 152 | Stringmap testapn; |
| 153 | testapn[flimflam::kApnProperty] = kApn; |
| 154 | testapn[flimflam::kApnUsernameProperty] = kUsername; |
| 155 | { |
| 156 | InSequence seq; |
| 157 | EXPECT_CALL(*adaptor_, |
| 158 | EmitStringmapChanged(flimflam::kCellularLastGoodApnProperty, |
| 159 | _)); |
| 160 | EXPECT_CALL(*adaptor_, |
| 161 | EmitStringmapChanged(flimflam::kCellularApnProperty, _)); |
| 162 | } |
| 163 | service_->SetApn(testapn, &error); |
| 164 | EXPECT_TRUE(error.IsSuccess()); |
| 165 | Stringmap resultapn = service_->GetApn(&error); |
| 166 | EXPECT_TRUE(error.IsSuccess()); |
| 167 | EXPECT_EQ(2, resultapn.size()); |
| 168 | Stringmap::const_iterator it = resultapn.find(flimflam::kApnProperty); |
| 169 | EXPECT_TRUE(it != resultapn.end() && it->second == kApn); |
| 170 | it = resultapn.find(flimflam::kApnUsernameProperty); |
| 171 | EXPECT_TRUE(it != resultapn.end() && it->second == kUsername); |
| 172 | EXPECT_FALSE(service_->GetUserSpecifiedApn() == NULL); |
| 173 | } |
| 174 | |
| 175 | TEST_F(CellularServiceTest, ClearApn) { |
| 176 | static const char kApn[] = "TheAPN"; |
| 177 | static const char kUsername[] = "commander.data"; |
| 178 | ProfileRefPtr profile(new NiceMock<MockProfile>( |
| 179 | &control_, reinterpret_cast<Manager *>(NULL))); |
| 180 | service_->set_profile(profile); |
| 181 | Error error; |
| 182 | // Set up an APN to make sure that it later gets cleared. |
| 183 | Stringmap testapn; |
| 184 | testapn[flimflam::kApnProperty] = kApn; |
| 185 | testapn[flimflam::kApnUsernameProperty] = kUsername; |
| 186 | { |
| 187 | InSequence seq; |
| 188 | EXPECT_CALL(*adaptor_, |
| 189 | EmitStringmapChanged(flimflam::kCellularLastGoodApnProperty, |
| 190 | _)); |
| 191 | EXPECT_CALL(*adaptor_, |
| 192 | EmitStringmapChanged(flimflam::kCellularApnProperty, _)); |
| 193 | } |
| 194 | service_->SetApn(testapn, &error); |
| 195 | Stringmap resultapn = service_->GetApn(&error); |
| 196 | ASSERT_TRUE(error.IsSuccess()); |
| 197 | ASSERT_EQ(2, service_->GetApn(&error).size()); |
| 198 | |
| 199 | Stringmap emptyapn; |
| 200 | EXPECT_CALL(*adaptor_, |
| 201 | EmitStringmapChanged(flimflam::kCellularLastGoodApnProperty, |
| 202 | _)).Times(0); |
| 203 | EXPECT_CALL(*adaptor_, |
| 204 | EmitStringmapChanged(flimflam::kCellularApnProperty, _)).Times(1); |
| 205 | service_->SetApn(emptyapn, &error); |
| 206 | EXPECT_TRUE(error.IsSuccess()); |
| 207 | resultapn = service_->GetApn(&error); |
| 208 | EXPECT_TRUE(resultapn.empty()); |
| 209 | EXPECT_TRUE(service_->GetUserSpecifiedApn() == NULL); |
| 210 | } |
| 211 | |
| 212 | TEST_F(CellularServiceTest, LastGoodApn) { |
| 213 | static const char kApn[] = "TheAPN"; |
| 214 | static const char kUsername[] = "commander.data"; |
| 215 | ProfileRefPtr profile(new NiceMock<MockProfile>( |
| 216 | &control_, reinterpret_cast<Manager *>(NULL))); |
| 217 | service_->set_profile(profile); |
| 218 | Stringmap testapn; |
| 219 | testapn[flimflam::kApnProperty] = kApn; |
| 220 | testapn[flimflam::kApnUsernameProperty] = kUsername; |
| 221 | EXPECT_CALL(*adaptor_, |
| 222 | EmitStringmapChanged(flimflam::kCellularLastGoodApnProperty, _)); |
| 223 | service_->SetLastGoodApn(testapn); |
| 224 | Stringmap *resultapn = service_->GetLastGoodApn(); |
| 225 | EXPECT_FALSE(resultapn == NULL); |
| 226 | EXPECT_EQ(2, resultapn->size()); |
| 227 | Stringmap::const_iterator it = resultapn->find(flimflam::kApnProperty); |
| 228 | EXPECT_TRUE(it != resultapn->end() && it->second == kApn); |
| 229 | it = resultapn->find(flimflam::kApnUsernameProperty); |
| 230 | EXPECT_TRUE(it != resultapn->end() && it->second == kUsername); |
| 231 | // Now set the user-specified APN, and check that LastGoodApn got |
| 232 | // cleared. |
| 233 | Stringmap userapn; |
| 234 | userapn[flimflam::kApnProperty] = kApn; |
| 235 | userapn[flimflam::kApnUsernameProperty] = kUsername; |
| 236 | { |
| 237 | InSequence seq; |
| 238 | EXPECT_CALL(*adaptor_, |
| 239 | EmitStringmapChanged(flimflam::kCellularLastGoodApnProperty, |
| 240 | _)); |
| 241 | EXPECT_CALL(*adaptor_, |
| 242 | EmitStringmapChanged(flimflam::kCellularApnProperty, _)); |
| 243 | } |
| 244 | Error error; |
| 245 | service_->SetApn(userapn, &error); |
| 246 | EXPECT_TRUE(service_->GetLastGoodApn() == NULL); |
| 247 | } |
| 248 | |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 249 | } // namespace shill |