Ben Chan | c07362b | 2012-05-12 10:54:11 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 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/wimax_service.h" |
| 6 | |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 7 | #include <base/string_util.h> |
| 8 | #include <chromeos/dbus/service_constants.h> |
Ben Chan | c07362b | 2012-05-12 10:54:11 -0700 | [diff] [blame] | 9 | #include <gtest/gtest.h> |
| 10 | |
| 11 | #include "shill/error.h" |
| 12 | #include "shill/nice_mock_control.h" |
| 13 | #include "shill/mock_adaptors.h" |
| 14 | #include "shill/mock_manager.h" |
| 15 | #include "shill/mock_metrics.h" |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 16 | #include "shill/mock_store.h" |
Ben Chan | c07362b | 2012-05-12 10:54:11 -0700 | [diff] [blame] | 17 | #include "shill/mock_wimax.h" |
Darin Petkov | 1e52a1b | 2012-05-21 10:35:56 +0200 | [diff] [blame] | 18 | #include "shill/mock_wimax_network_proxy.h" |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 19 | #include "shill/mock_wimax_provider.h" |
Ben Chan | c07362b | 2012-05-12 10:54:11 -0700 | [diff] [blame] | 20 | |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 21 | using std::string; |
Ben Chan | c07362b | 2012-05-12 10:54:11 -0700 | [diff] [blame] | 22 | using testing::_; |
| 23 | using testing::NiceMock; |
| 24 | using testing::Return; |
Ben Chan | 4e5c131 | 2012-05-18 18:45:38 -0700 | [diff] [blame] | 25 | using wimax_manager::kEAPAnonymousIdentity; |
| 26 | using wimax_manager::kEAPUserIdentity; |
| 27 | using wimax_manager::kEAPUserPassword; |
Ben Chan | c07362b | 2012-05-12 10:54:11 -0700 | [diff] [blame] | 28 | |
| 29 | namespace shill { |
| 30 | |
| 31 | namespace { |
| 32 | |
| 33 | const char kTestLinkName[] = "wm0"; |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 34 | const char kTestAddress[] = "0123456789AB"; |
Ben Chan | c07362b | 2012-05-12 10:54:11 -0700 | [diff] [blame] | 35 | const int kTestInterfaceIndex = 5; |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 36 | const char kTestPath[] = "/org/chromium/WiMaxManager/Device/wm7"; |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 37 | const char kTestName[] = "Test WiMAX Network"; |
| 38 | const char kTestNetworkId[] = "1234abcd"; |
Ben Chan | c07362b | 2012-05-12 10:54:11 -0700 | [diff] [blame] | 39 | |
| 40 | } // namespace |
| 41 | |
| 42 | class WiMaxServiceTest : public testing::Test { |
| 43 | public: |
| 44 | WiMaxServiceTest() |
Darin Petkov | 1e52a1b | 2012-05-21 10:35:56 +0200 | [diff] [blame] | 45 | : proxy_(new MockWiMaxNetworkProxy()), |
| 46 | manager_(&control_, NULL, NULL, NULL), |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 47 | device_(new MockWiMax(&control_, NULL, &metrics_, &manager_, |
| 48 | kTestLinkName, kTestAddress, kTestInterfaceIndex, |
| 49 | kTestPath)), |
| 50 | service_(new WiMaxService(&control_, NULL, &metrics_, &manager_)) { |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 51 | service_->set_friendly_name(kTestName); |
| 52 | service_->set_network_id(kTestNetworkId); |
| 53 | service_->InitStorageIdentifier(); |
| 54 | } |
Ben Chan | c07362b | 2012-05-12 10:54:11 -0700 | [diff] [blame] | 55 | |
| 56 | virtual ~WiMaxServiceTest() {} |
| 57 | |
| 58 | protected: |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 59 | virtual void TearDown() { |
| 60 | service_->device_ = NULL; |
| 61 | } |
| 62 | |
Darin Petkov | 1e52a1b | 2012-05-21 10:35:56 +0200 | [diff] [blame] | 63 | scoped_ptr<MockWiMaxNetworkProxy> proxy_; |
Ben Chan | c07362b | 2012-05-12 10:54:11 -0700 | [diff] [blame] | 64 | NiceMockControl control_; |
| 65 | MockManager manager_; |
| 66 | MockMetrics metrics_; |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 67 | scoped_refptr<MockWiMax> device_; |
Ben Chan | c07362b | 2012-05-12 10:54:11 -0700 | [diff] [blame] | 68 | WiMaxServiceRefPtr service_; |
| 69 | }; |
| 70 | |
Ben Chan | 4e5c131 | 2012-05-18 18:45:38 -0700 | [diff] [blame] | 71 | TEST_F(WiMaxServiceTest, GetConnectParameters) { |
| 72 | { |
Darin Petkov | 25665aa | 2012-05-21 14:08:12 +0200 | [diff] [blame] | 73 | KeyValueStore parameters; |
Ben Chan | 4e5c131 | 2012-05-18 18:45:38 -0700 | [diff] [blame] | 74 | service_->GetConnectParameters(¶meters); |
| 75 | |
Darin Petkov | 25665aa | 2012-05-21 14:08:12 +0200 | [diff] [blame] | 76 | EXPECT_FALSE(parameters.ContainsString(kEAPAnonymousIdentity)); |
| 77 | EXPECT_FALSE(parameters.ContainsString(kEAPUserIdentity)); |
| 78 | EXPECT_FALSE(parameters.ContainsString(kEAPUserPassword)); |
Ben Chan | 4e5c131 | 2012-05-18 18:45:38 -0700 | [diff] [blame] | 79 | } |
| 80 | { |
| 81 | Service::EapCredentials eap; |
| 82 | eap.anonymous_identity = "TestAnonymousIdentity"; |
| 83 | eap.identity = "TestUserIdentity"; |
| 84 | eap.password = "TestPassword"; |
| 85 | service_->set_eap(eap); |
| 86 | |
Darin Petkov | 25665aa | 2012-05-21 14:08:12 +0200 | [diff] [blame] | 87 | KeyValueStore parameters; |
Ben Chan | 4e5c131 | 2012-05-18 18:45:38 -0700 | [diff] [blame] | 88 | service_->GetConnectParameters(¶meters); |
| 89 | |
Ben Chan | 4e5c131 | 2012-05-18 18:45:38 -0700 | [diff] [blame] | 90 | EXPECT_EQ(eap.anonymous_identity, |
Darin Petkov | 25665aa | 2012-05-21 14:08:12 +0200 | [diff] [blame] | 91 | parameters.LookupString(kEAPAnonymousIdentity, "")); |
| 92 | EXPECT_EQ(eap.identity, parameters.LookupString(kEAPUserIdentity, "")); |
| 93 | EXPECT_EQ(eap.password, parameters.LookupString(kEAPUserPassword, "")); |
Ben Chan | 4e5c131 | 2012-05-18 18:45:38 -0700 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 97 | TEST_F(WiMaxServiceTest, GetDeviceRpcId) { |
| 98 | Error error; |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 99 | EXPECT_EQ("/", service_->GetDeviceRpcId(&error)); |
| 100 | EXPECT_EQ(Error::kNotSupported, error.type()); |
| 101 | service_->device_ = device_; |
| 102 | error.Reset(); |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 103 | EXPECT_EQ(DeviceMockAdaptor::kRpcId, service_->GetDeviceRpcId(&error)); |
| 104 | EXPECT_TRUE(error.IsSuccess()); |
| 105 | } |
| 106 | |
Darin Petkov | 1e52a1b | 2012-05-21 10:35:56 +0200 | [diff] [blame] | 107 | TEST_F(WiMaxServiceTest, OnSignalStrengthChanged) { |
| 108 | const int kStrength = 55; |
| 109 | service_->OnSignalStrengthChanged(kStrength); |
| 110 | EXPECT_EQ(kStrength, service_->strength()); |
| 111 | } |
| 112 | |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 113 | TEST_F(WiMaxServiceTest, StartStop) { |
| 114 | static const char kName[] = "My WiMAX Network"; |
| 115 | const uint32 kIdentifier = 0x1234abcd; |
Darin Petkov | 1e52a1b | 2012-05-21 10:35:56 +0200 | [diff] [blame] | 116 | const int kStrength = 66; |
Darin Petkov | a3f9f77 | 2012-05-31 12:11:28 +0200 | [diff] [blame] | 117 | EXPECT_FALSE(service_->connectable()); |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 118 | EXPECT_FALSE(service_->IsStarted()); |
| 119 | EXPECT_EQ(0, service_->strength()); |
| 120 | EXPECT_FALSE(service_->proxy_.get()); |
Darin Petkov | 1e52a1b | 2012-05-21 10:35:56 +0200 | [diff] [blame] | 121 | EXPECT_CALL(*proxy_, Name(_)).WillOnce(Return(kName)); |
| 122 | EXPECT_CALL(*proxy_, Identifier(_)).WillOnce(Return(kIdentifier)); |
| 123 | EXPECT_CALL(*proxy_, SignalStrength(_)).WillOnce(Return(kStrength)); |
| 124 | EXPECT_CALL(*proxy_, set_signal_strength_changed_callback(_)); |
Darin Petkov | a3f9f77 | 2012-05-31 12:11:28 +0200 | [diff] [blame] | 125 | service_->need_passphrase_ = false; |
Darin Petkov | 1e52a1b | 2012-05-21 10:35:56 +0200 | [diff] [blame] | 126 | EXPECT_TRUE(service_->Start(proxy_.release())); |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 127 | EXPECT_TRUE(service_->IsStarted()); |
Darin Petkov | 1e52a1b | 2012-05-21 10:35:56 +0200 | [diff] [blame] | 128 | EXPECT_EQ(kStrength, service_->strength()); |
| 129 | EXPECT_EQ(kName, service_->network_name()); |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 130 | EXPECT_EQ(kTestName, service_->friendly_name()); |
| 131 | EXPECT_EQ(kTestNetworkId, service_->network_id()); |
Darin Petkov | a3f9f77 | 2012-05-31 12:11:28 +0200 | [diff] [blame] | 132 | EXPECT_TRUE(service_->connectable()); |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 133 | EXPECT_TRUE(service_->proxy_.get()); |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 134 | |
| 135 | service_->device_ = device_; |
| 136 | EXPECT_CALL(*device_, OnServiceStopped(_)); |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 137 | service_->Stop(); |
| 138 | EXPECT_FALSE(service_->IsStarted()); |
| 139 | EXPECT_EQ(0, service_->strength()); |
| 140 | EXPECT_FALSE(service_->proxy_.get()); |
Darin Petkov | 1e52a1b | 2012-05-21 10:35:56 +0200 | [diff] [blame] | 141 | } |
| 142 | |
Darin Petkov | 8021e7f | 2012-05-21 12:15:00 +0200 | [diff] [blame] | 143 | TEST_F(WiMaxServiceTest, SetEAP) { |
| 144 | ServiceRefPtr base_service = service_; |
| 145 | EXPECT_TRUE(base_service->Is8021x()); |
| 146 | EXPECT_TRUE(service_->need_passphrase_); |
| 147 | EXPECT_FALSE(base_service->connectable()); |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 148 | |
| 149 | // No password. |
Darin Petkov | 8021e7f | 2012-05-21 12:15:00 +0200 | [diff] [blame] | 150 | Service::EapCredentials eap; |
| 151 | eap.identity = "TestIdentity"; |
| 152 | base_service->set_eap(eap); |
| 153 | EXPECT_TRUE(service_->need_passphrase_); |
| 154 | EXPECT_FALSE(base_service->connectable()); |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 155 | |
| 156 | // Not started. |
Darin Petkov | 8021e7f | 2012-05-21 12:15:00 +0200 | [diff] [blame] | 157 | eap.password = "TestPassword"; |
| 158 | base_service->set_eap(eap); |
Darin Petkov | a3f9f77 | 2012-05-31 12:11:28 +0200 | [diff] [blame] | 159 | EXPECT_FALSE(service_->need_passphrase_); |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 160 | EXPECT_FALSE(base_service->connectable()); |
| 161 | |
| 162 | // Connectable. |
| 163 | service_->proxy_.reset(proxy_.release()); |
| 164 | base_service->set_eap(eap); |
Darin Petkov | a3f9f77 | 2012-05-31 12:11:28 +0200 | [diff] [blame] | 165 | EXPECT_FALSE(service_->need_passphrase_); |
Darin Petkov | 8021e7f | 2012-05-21 12:15:00 +0200 | [diff] [blame] | 166 | EXPECT_TRUE(base_service->connectable()); |
Darin Petkov | a3f9f77 | 2012-05-31 12:11:28 +0200 | [diff] [blame] | 167 | |
| 168 | // Reset password. |
| 169 | service_->ClearPassphrase(); |
| 170 | EXPECT_TRUE(service_->need_passphrase_); |
| 171 | EXPECT_FALSE(base_service->connectable()); |
| 172 | EXPECT_TRUE(base_service->eap().password.empty()); |
Darin Petkov | 8021e7f | 2012-05-21 12:15:00 +0200 | [diff] [blame] | 173 | } |
| 174 | |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 175 | TEST_F(WiMaxServiceTest, ConvertIdentifierToNetworkId) { |
| 176 | EXPECT_EQ("00000000", WiMaxService::ConvertIdentifierToNetworkId(0)); |
| 177 | EXPECT_EQ("abcd1234", WiMaxService::ConvertIdentifierToNetworkId(0xabcd1234)); |
| 178 | EXPECT_EQ("ffffffff", WiMaxService::ConvertIdentifierToNetworkId(0xffffffff)); |
| 179 | } |
| 180 | |
| 181 | TEST_F(WiMaxServiceTest, StorageIdentifier) { |
| 182 | static const char kStorageId[] = "wimax_test_wimax_network_1234abcd"; |
| 183 | EXPECT_EQ(kStorageId, service_->GetStorageIdentifier()); |
| 184 | EXPECT_EQ(kStorageId, |
| 185 | WiMaxService::CreateStorageIdentifier(kTestNetworkId, kTestName)); |
| 186 | } |
| 187 | |
| 188 | TEST_F(WiMaxServiceTest, Save) { |
| 189 | NiceMock<MockStore> storage; |
| 190 | string storage_id = service_->GetStorageIdentifier(); |
| 191 | EXPECT_CALL(storage, SetString(storage_id, _, _)) |
| 192 | .WillRepeatedly(Return(true)); |
| 193 | EXPECT_CALL(storage, DeleteKey(storage_id, _)).WillRepeatedly(Return(true)); |
| 194 | EXPECT_CALL(storage, SetString(storage_id, |
| 195 | WiMaxService::kStorageNetworkId, |
| 196 | kTestNetworkId)); |
| 197 | EXPECT_TRUE(service_->Save(&storage)); |
| 198 | } |
| 199 | |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 200 | TEST_F(WiMaxServiceTest, Connect) { |
Darin Petkov | a3f9f77 | 2012-05-31 12:11:28 +0200 | [diff] [blame] | 201 | // Connect but not connectable. |
| 202 | Error error; |
| 203 | EXPECT_FALSE(service_->connectable()); |
| 204 | service_->Connect(&error); |
| 205 | EXPECT_EQ(Error::kOperationFailed, error.type()); |
| 206 | service_->set_connectable(true); |
| 207 | |
| 208 | // No carrier device available. |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 209 | MockWiMaxProvider provider; |
| 210 | scoped_refptr<MockWiMax> null_device; |
| 211 | EXPECT_CALL(manager_, wimax_provider()).WillOnce(Return(&provider)); |
| 212 | EXPECT_CALL(provider, SelectCarrier(_)).WillOnce(Return(null_device)); |
Darin Petkov | a3f9f77 | 2012-05-31 12:11:28 +0200 | [diff] [blame] | 213 | error.Reset(); |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 214 | service_->Connect(&error); |
| 215 | EXPECT_EQ(Error::kNoCarrier, error.type()); |
| 216 | |
Darin Petkov | a3f9f77 | 2012-05-31 12:11:28 +0200 | [diff] [blame] | 217 | // Successful connect. |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 218 | EXPECT_CALL(manager_, wimax_provider()).WillOnce(Return(&provider)); |
| 219 | EXPECT_CALL(provider, SelectCarrier(_)).WillOnce(Return(device_)); |
| 220 | EXPECT_CALL(*device_, ConnectTo(_, _)); |
| 221 | error.Reset(); |
| 222 | service_->Connect(&error); |
| 223 | EXPECT_TRUE(error.IsSuccess()); |
| 224 | |
Darin Petkov | a3f9f77 | 2012-05-31 12:11:28 +0200 | [diff] [blame] | 225 | // Connect while already connected. |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 226 | service_->Connect(&error); |
| 227 | EXPECT_EQ(Error::kAlreadyConnected, error.type()); |
| 228 | |
Darin Petkov | a3f9f77 | 2012-05-31 12:11:28 +0200 | [diff] [blame] | 229 | // Successful disconnect. |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 230 | EXPECT_CALL(*device_, DisconnectFrom(_, _)); |
| 231 | error.Reset(); |
| 232 | service_->Disconnect(&error); |
| 233 | EXPECT_TRUE(error.IsSuccess()); |
| 234 | |
Darin Petkov | a3f9f77 | 2012-05-31 12:11:28 +0200 | [diff] [blame] | 235 | // Disconnect while not connected. |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 236 | service_->Disconnect(&error); |
| 237 | EXPECT_EQ(Error::kNotConnected, error.type()); |
| 238 | } |
| 239 | |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 240 | TEST_F(WiMaxServiceTest, Unload) { |
| 241 | MockWiMaxProvider provider; |
| 242 | EXPECT_CALL(manager_, wimax_provider()) |
| 243 | .Times(2) |
| 244 | .WillRepeatedly(Return(&provider)); |
| 245 | Service::EapCredentials eap; |
| 246 | eap.identity = "TestUserIdentity"; |
| 247 | service_->set_eap(eap); |
| 248 | EXPECT_CALL(provider, OnServiceUnloaded(_)).WillOnce(Return(false)); |
| 249 | EXPECT_FALSE(service_->Unload()); |
| 250 | EXPECT_TRUE(service_->eap().identity.empty()); |
| 251 | eap.identity = "TestUserIdentity"; |
| 252 | service_->set_eap(eap); |
| 253 | EXPECT_CALL(provider, OnServiceUnloaded(_)).WillOnce(Return(true)); |
| 254 | EXPECT_TRUE(service_->Unload()); |
| 255 | EXPECT_TRUE(service_->eap().identity.empty()); |
| 256 | } |
| 257 | |
Darin Petkov | 8ea0eaf | 2012-05-29 11:21:33 +0200 | [diff] [blame] | 258 | TEST_F(WiMaxServiceTest, SetState) { |
| 259 | service_->device_ = device_; |
| 260 | ServiceRefPtr base_service = service_; |
| 261 | EXPECT_EQ(Service::kStateIdle, service_->state()); |
| 262 | |
| 263 | EXPECT_CALL(manager_, UpdateService(_)); |
| 264 | base_service->SetState(Service::kStateAssociating); |
| 265 | EXPECT_EQ(Service::kStateAssociating, service_->state()); |
| 266 | EXPECT_TRUE(service_->device_); |
| 267 | |
| 268 | EXPECT_CALL(manager_, UpdateService(_)); |
| 269 | base_service->SetState(Service::kStateFailure); |
| 270 | EXPECT_EQ(Service::kStateFailure, service_->state()); |
| 271 | EXPECT_FALSE(service_->device_); |
| 272 | } |
| 273 | |
Ben Chan | c07362b | 2012-05-12 10:54:11 -0700 | [diff] [blame] | 274 | } // namespace shill |