blob: a91eff4361d83191ee848c8b3def0cce8551005c [file] [log] [blame]
Ben Chanc07362b2012-05-12 10:54:11 -07001// 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 Petkove4b27022012-05-16 13:28:50 +02007#include <base/string_util.h>
8#include <chromeos/dbus/service_constants.h>
Ben Chanc07362b2012-05-12 10:54:11 -07009#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 Petkovd1cd7972012-05-22 15:26:15 +020016#include "shill/mock_store.h"
Ben Chanc07362b2012-05-12 10:54:11 -070017#include "shill/mock_wimax.h"
Darin Petkov1e52a1b2012-05-21 10:35:56 +020018#include "shill/mock_wimax_network_proxy.h"
Darin Petkovc63dcf02012-05-24 11:51:43 +020019#include "shill/mock_wimax_provider.h"
Ben Chanc07362b2012-05-12 10:54:11 -070020
Darin Petkove4b27022012-05-16 13:28:50 +020021using std::string;
Ben Chanc07362b2012-05-12 10:54:11 -070022using testing::_;
23using testing::NiceMock;
24using testing::Return;
Ben Chan4e5c1312012-05-18 18:45:38 -070025using wimax_manager::kEAPAnonymousIdentity;
26using wimax_manager::kEAPUserIdentity;
27using wimax_manager::kEAPUserPassword;
Ben Chanc07362b2012-05-12 10:54:11 -070028
29namespace shill {
30
31namespace {
32
33const char kTestLinkName[] = "wm0";
Darin Petkove4b27022012-05-16 13:28:50 +020034const char kTestAddress[] = "0123456789AB";
Ben Chanc07362b2012-05-12 10:54:11 -070035const int kTestInterfaceIndex = 5;
Darin Petkove4b27022012-05-16 13:28:50 +020036const char kTestPath[] = "/org/chromium/WiMaxManager/Device/wm7";
Darin Petkovd1cd7972012-05-22 15:26:15 +020037const char kTestName[] = "Test WiMAX Network";
38const char kTestNetworkId[] = "1234abcd";
Ben Chanc07362b2012-05-12 10:54:11 -070039
40} // namespace
41
42class WiMaxServiceTest : public testing::Test {
43 public:
44 WiMaxServiceTest()
Darin Petkov1e52a1b2012-05-21 10:35:56 +020045 : proxy_(new MockWiMaxNetworkProxy()),
46 manager_(&control_, NULL, NULL, NULL),
Darin Petkovc63dcf02012-05-24 11:51:43 +020047 device_(new MockWiMax(&control_, NULL, &metrics_, &manager_,
48 kTestLinkName, kTestAddress, kTestInterfaceIndex,
49 kTestPath)),
50 service_(new WiMaxService(&control_, NULL, &metrics_, &manager_)) {
Darin Petkovd1cd7972012-05-22 15:26:15 +020051 service_->set_friendly_name(kTestName);
52 service_->set_network_id(kTestNetworkId);
53 service_->InitStorageIdentifier();
54 }
Ben Chanc07362b2012-05-12 10:54:11 -070055
56 virtual ~WiMaxServiceTest() {}
57
58 protected:
Darin Petkovc63dcf02012-05-24 11:51:43 +020059 virtual void TearDown() {
60 service_->device_ = NULL;
61 }
62
Darin Petkovb2ba39f2012-06-06 10:33:43 +020063 void ExpectUpdateService() {
64 EXPECT_CALL(manager_, HasService(_)).WillOnce(Return(true));
65 EXPECT_CALL(manager_, UpdateService(_));
66 }
67
Darin Petkov6b9b2e12012-07-10 15:51:42 +020068 void SetConnectable(bool connectable) {
69 service_->connectable_ = connectable;
70 }
71
Darin Petkov1e52a1b2012-05-21 10:35:56 +020072 scoped_ptr<MockWiMaxNetworkProxy> proxy_;
Ben Chanc07362b2012-05-12 10:54:11 -070073 NiceMockControl control_;
74 MockManager manager_;
Darin Petkov3a4100c2012-06-14 11:36:59 +020075 NiceMock<MockMetrics> metrics_;
Darin Petkovc63dcf02012-05-24 11:51:43 +020076 scoped_refptr<MockWiMax> device_;
Ben Chanc07362b2012-05-12 10:54:11 -070077 WiMaxServiceRefPtr service_;
78};
79
Ben Chan4e5c1312012-05-18 18:45:38 -070080TEST_F(WiMaxServiceTest, GetConnectParameters) {
81 {
Darin Petkov25665aa2012-05-21 14:08:12 +020082 KeyValueStore parameters;
Ben Chan4e5c1312012-05-18 18:45:38 -070083 service_->GetConnectParameters(&parameters);
84
Darin Petkov25665aa2012-05-21 14:08:12 +020085 EXPECT_FALSE(parameters.ContainsString(kEAPAnonymousIdentity));
86 EXPECT_FALSE(parameters.ContainsString(kEAPUserIdentity));
87 EXPECT_FALSE(parameters.ContainsString(kEAPUserPassword));
Ben Chan4e5c1312012-05-18 18:45:38 -070088 }
89 {
90 Service::EapCredentials eap;
91 eap.anonymous_identity = "TestAnonymousIdentity";
92 eap.identity = "TestUserIdentity";
93 eap.password = "TestPassword";
94 service_->set_eap(eap);
95
Darin Petkov25665aa2012-05-21 14:08:12 +020096 KeyValueStore parameters;
Ben Chan4e5c1312012-05-18 18:45:38 -070097 service_->GetConnectParameters(&parameters);
98
Ben Chan4e5c1312012-05-18 18:45:38 -070099 EXPECT_EQ(eap.anonymous_identity,
Darin Petkov25665aa2012-05-21 14:08:12 +0200100 parameters.LookupString(kEAPAnonymousIdentity, ""));
101 EXPECT_EQ(eap.identity, parameters.LookupString(kEAPUserIdentity, ""));
102 EXPECT_EQ(eap.password, parameters.LookupString(kEAPUserPassword, ""));
Ben Chan4e5c1312012-05-18 18:45:38 -0700103 }
104}
105
Darin Petkove4b27022012-05-16 13:28:50 +0200106TEST_F(WiMaxServiceTest, GetDeviceRpcId) {
107 Error error;
Darin Petkovc63dcf02012-05-24 11:51:43 +0200108 EXPECT_EQ("/", service_->GetDeviceRpcId(&error));
109 EXPECT_EQ(Error::kNotSupported, error.type());
110 service_->device_ = device_;
111 error.Reset();
Darin Petkove4b27022012-05-16 13:28:50 +0200112 EXPECT_EQ(DeviceMockAdaptor::kRpcId, service_->GetDeviceRpcId(&error));
113 EXPECT_TRUE(error.IsSuccess());
114}
115
Darin Petkov1e52a1b2012-05-21 10:35:56 +0200116TEST_F(WiMaxServiceTest, OnSignalStrengthChanged) {
117 const int kStrength = 55;
118 service_->OnSignalStrengthChanged(kStrength);
119 EXPECT_EQ(kStrength, service_->strength());
120}
121
Darin Petkovd1cd7972012-05-22 15:26:15 +0200122TEST_F(WiMaxServiceTest, StartStop) {
123 static const char kName[] = "My WiMAX Network";
124 const uint32 kIdentifier = 0x1234abcd;
Darin Petkov1e52a1b2012-05-21 10:35:56 +0200125 const int kStrength = 66;
Darin Petkova3f9f772012-05-31 12:11:28 +0200126 EXPECT_FALSE(service_->connectable());
Darin Petkovd1cd7972012-05-22 15:26:15 +0200127 EXPECT_FALSE(service_->IsStarted());
128 EXPECT_EQ(0, service_->strength());
129 EXPECT_FALSE(service_->proxy_.get());
Darin Petkov1e52a1b2012-05-21 10:35:56 +0200130 EXPECT_CALL(*proxy_, Name(_)).WillOnce(Return(kName));
131 EXPECT_CALL(*proxy_, Identifier(_)).WillOnce(Return(kIdentifier));
132 EXPECT_CALL(*proxy_, SignalStrength(_)).WillOnce(Return(kStrength));
133 EXPECT_CALL(*proxy_, set_signal_strength_changed_callback(_));
Darin Petkovb2ba39f2012-06-06 10:33:43 +0200134 ExpectUpdateService();
Darin Petkova3f9f772012-05-31 12:11:28 +0200135 service_->need_passphrase_ = false;
Darin Petkov1e52a1b2012-05-21 10:35:56 +0200136 EXPECT_TRUE(service_->Start(proxy_.release()));
Darin Petkovd1cd7972012-05-22 15:26:15 +0200137 EXPECT_TRUE(service_->IsStarted());
Darin Petkov1e52a1b2012-05-21 10:35:56 +0200138 EXPECT_EQ(kStrength, service_->strength());
139 EXPECT_EQ(kName, service_->network_name());
Darin Petkovd1cd7972012-05-22 15:26:15 +0200140 EXPECT_EQ(kTestName, service_->friendly_name());
141 EXPECT_EQ(kTestNetworkId, service_->network_id());
Darin Petkova3f9f772012-05-31 12:11:28 +0200142 EXPECT_TRUE(service_->connectable());
Darin Petkovd1cd7972012-05-22 15:26:15 +0200143 EXPECT_TRUE(service_->proxy_.get());
Darin Petkovc63dcf02012-05-24 11:51:43 +0200144
145 service_->device_ = device_;
146 EXPECT_CALL(*device_, OnServiceStopped(_));
Darin Petkovb2ba39f2012-06-06 10:33:43 +0200147 ExpectUpdateService();
Darin Petkovd1cd7972012-05-22 15:26:15 +0200148 service_->Stop();
149 EXPECT_FALSE(service_->IsStarted());
150 EXPECT_EQ(0, service_->strength());
151 EXPECT_FALSE(service_->proxy_.get());
Darin Petkov1e52a1b2012-05-21 10:35:56 +0200152}
153
Darin Petkov8021e7f2012-05-21 12:15:00 +0200154TEST_F(WiMaxServiceTest, SetEAP) {
155 ServiceRefPtr base_service = service_;
156 EXPECT_TRUE(base_service->Is8021x());
157 EXPECT_TRUE(service_->need_passphrase_);
158 EXPECT_FALSE(base_service->connectable());
Darin Petkovc1e52732012-05-25 15:23:45 +0200159
160 // No password.
Darin Petkov8021e7f2012-05-21 12:15:00 +0200161 Service::EapCredentials eap;
162 eap.identity = "TestIdentity";
163 base_service->set_eap(eap);
164 EXPECT_TRUE(service_->need_passphrase_);
165 EXPECT_FALSE(base_service->connectable());
Darin Petkovc1e52732012-05-25 15:23:45 +0200166
167 // Not started.
Darin Petkov8021e7f2012-05-21 12:15:00 +0200168 eap.password = "TestPassword";
169 base_service->set_eap(eap);
Darin Petkova3f9f772012-05-31 12:11:28 +0200170 EXPECT_FALSE(service_->need_passphrase_);
Darin Petkovc1e52732012-05-25 15:23:45 +0200171 EXPECT_FALSE(base_service->connectable());
172
173 // Connectable.
174 service_->proxy_.reset(proxy_.release());
Darin Petkovb2ba39f2012-06-06 10:33:43 +0200175 ExpectUpdateService();
Darin Petkovc1e52732012-05-25 15:23:45 +0200176 base_service->set_eap(eap);
Darin Petkova3f9f772012-05-31 12:11:28 +0200177 EXPECT_FALSE(service_->need_passphrase_);
Darin Petkov8021e7f2012-05-21 12:15:00 +0200178 EXPECT_TRUE(base_service->connectable());
Darin Petkova3f9f772012-05-31 12:11:28 +0200179
180 // Reset password.
Darin Petkovb2ba39f2012-06-06 10:33:43 +0200181 ExpectUpdateService();
Darin Petkova3f9f772012-05-31 12:11:28 +0200182 service_->ClearPassphrase();
183 EXPECT_TRUE(service_->need_passphrase_);
184 EXPECT_FALSE(base_service->connectable());
185 EXPECT_TRUE(base_service->eap().password.empty());
Darin Petkov8021e7f2012-05-21 12:15:00 +0200186}
187
Darin Petkovd1cd7972012-05-22 15:26:15 +0200188TEST_F(WiMaxServiceTest, ConvertIdentifierToNetworkId) {
189 EXPECT_EQ("00000000", WiMaxService::ConvertIdentifierToNetworkId(0));
190 EXPECT_EQ("abcd1234", WiMaxService::ConvertIdentifierToNetworkId(0xabcd1234));
191 EXPECT_EQ("ffffffff", WiMaxService::ConvertIdentifierToNetworkId(0xffffffff));
192}
193
194TEST_F(WiMaxServiceTest, StorageIdentifier) {
195 static const char kStorageId[] = "wimax_test_wimax_network_1234abcd";
196 EXPECT_EQ(kStorageId, service_->GetStorageIdentifier());
197 EXPECT_EQ(kStorageId,
198 WiMaxService::CreateStorageIdentifier(kTestNetworkId, kTestName));
199}
200
201TEST_F(WiMaxServiceTest, Save) {
202 NiceMock<MockStore> storage;
203 string storage_id = service_->GetStorageIdentifier();
204 EXPECT_CALL(storage, SetString(storage_id, _, _))
205 .WillRepeatedly(Return(true));
206 EXPECT_CALL(storage, DeleteKey(storage_id, _)).WillRepeatedly(Return(true));
207 EXPECT_CALL(storage, SetString(storage_id,
208 WiMaxService::kStorageNetworkId,
209 kTestNetworkId));
210 EXPECT_TRUE(service_->Save(&storage));
211}
212
Darin Petkovc63dcf02012-05-24 11:51:43 +0200213TEST_F(WiMaxServiceTest, Connect) {
Darin Petkova3f9f772012-05-31 12:11:28 +0200214 // Connect but not connectable.
215 Error error;
216 EXPECT_FALSE(service_->connectable());
217 service_->Connect(&error);
218 EXPECT_EQ(Error::kOperationFailed, error.type());
Darin Petkov6b9b2e12012-07-10 15:51:42 +0200219 SetConnectable(true);
Darin Petkova3f9f772012-05-31 12:11:28 +0200220
221 // No carrier device available.
Darin Petkovc63dcf02012-05-24 11:51:43 +0200222 MockWiMaxProvider provider;
223 scoped_refptr<MockWiMax> null_device;
224 EXPECT_CALL(manager_, wimax_provider()).WillOnce(Return(&provider));
225 EXPECT_CALL(provider, SelectCarrier(_)).WillOnce(Return(null_device));
Darin Petkova3f9f772012-05-31 12:11:28 +0200226 error.Reset();
Darin Petkovc63dcf02012-05-24 11:51:43 +0200227 service_->Connect(&error);
228 EXPECT_EQ(Error::kNoCarrier, error.type());
229
Darin Petkova3f9f772012-05-31 12:11:28 +0200230 // Successful connect.
Darin Petkovc63dcf02012-05-24 11:51:43 +0200231 EXPECT_CALL(manager_, wimax_provider()).WillOnce(Return(&provider));
232 EXPECT_CALL(provider, SelectCarrier(_)).WillOnce(Return(device_));
233 EXPECT_CALL(*device_, ConnectTo(_, _));
234 error.Reset();
235 service_->Connect(&error);
236 EXPECT_TRUE(error.IsSuccess());
237
Darin Petkova3f9f772012-05-31 12:11:28 +0200238 // Connect while already connected.
Ben Chanb5310a12012-06-15 13:53:29 -0700239 // TODO(benchan): Check for error if we populate error again after changing
240 // the way that Chrome handles Error::kAlreadyConnected situation.
Darin Petkovc63dcf02012-05-24 11:51:43 +0200241 service_->Connect(&error);
Darin Petkovc63dcf02012-05-24 11:51:43 +0200242
Darin Petkova3f9f772012-05-31 12:11:28 +0200243 // Successful disconnect.
Darin Petkovc63dcf02012-05-24 11:51:43 +0200244 EXPECT_CALL(*device_, DisconnectFrom(_, _));
245 error.Reset();
Darin Petkov3a4100c2012-06-14 11:36:59 +0200246 ExpectUpdateService();
Darin Petkovc63dcf02012-05-24 11:51:43 +0200247 service_->Disconnect(&error);
248 EXPECT_TRUE(error.IsSuccess());
249
Darin Petkova3f9f772012-05-31 12:11:28 +0200250 // Disconnect while not connected.
Darin Petkovc63dcf02012-05-24 11:51:43 +0200251 service_->Disconnect(&error);
252 EXPECT_EQ(Error::kNotConnected, error.type());
253}
254
Darin Petkovc1e52732012-05-25 15:23:45 +0200255TEST_F(WiMaxServiceTest, Unload) {
256 MockWiMaxProvider provider;
257 EXPECT_CALL(manager_, wimax_provider())
258 .Times(2)
259 .WillRepeatedly(Return(&provider));
260 Service::EapCredentials eap;
261 eap.identity = "TestUserIdentity";
262 service_->set_eap(eap);
263 EXPECT_CALL(provider, OnServiceUnloaded(_)).WillOnce(Return(false));
264 EXPECT_FALSE(service_->Unload());
265 EXPECT_TRUE(service_->eap().identity.empty());
266 eap.identity = "TestUserIdentity";
267 service_->set_eap(eap);
268 EXPECT_CALL(provider, OnServiceUnloaded(_)).WillOnce(Return(true));
269 EXPECT_TRUE(service_->Unload());
270 EXPECT_TRUE(service_->eap().identity.empty());
271}
272
Darin Petkov8ea0eaf2012-05-29 11:21:33 +0200273TEST_F(WiMaxServiceTest, SetState) {
274 service_->device_ = device_;
275 ServiceRefPtr base_service = service_;
276 EXPECT_EQ(Service::kStateIdle, service_->state());
277
278 EXPECT_CALL(manager_, UpdateService(_));
279 base_service->SetState(Service::kStateAssociating);
280 EXPECT_EQ(Service::kStateAssociating, service_->state());
281 EXPECT_TRUE(service_->device_);
282
283 EXPECT_CALL(manager_, UpdateService(_));
284 base_service->SetState(Service::kStateFailure);
285 EXPECT_EQ(Service::kStateFailure, service_->state());
286 EXPECT_FALSE(service_->device_);
287}
288
Darin Petkov6b9b2e12012-07-10 15:51:42 +0200289TEST_F(WiMaxServiceTest, IsAutoConnectable) {
290 EXPECT_FALSE(service_->connectable());
291 const char *reason = "";
292
293 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
294
295 MockWiMaxProvider provider;
296 EXPECT_CALL(manager_, wimax_provider())
297 .Times(2)
298 .WillRepeatedly(Return(&provider));
299
300 SetConnectable(true);
301 EXPECT_CALL(provider, SelectCarrier(_)).WillOnce(Return(device_));
302 EXPECT_CALL(*device_, IsIdle()).WillOnce(Return(false));
303 reason = "";
304 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
305 EXPECT_EQ(WiMaxService::kAutoConnBusy, reason);
306
307 EXPECT_CALL(provider, SelectCarrier(_)).WillOnce(Return(device_));
308 EXPECT_CALL(*device_, IsIdle()).WillOnce(Return(true));
309 reason = "";
310 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
311 EXPECT_STREQ("", reason);
312}
313
Ben Chanc07362b2012-05-12 10:54:11 -0700314} // namespace shill