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