blob: 3ed43d5b1f1be9a1c26315c978b664a13cef0443 [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 Petkov1e52a1b2012-05-21 10:35:56 +020063 scoped_ptr<MockWiMaxNetworkProxy> proxy_;
Ben Chanc07362b2012-05-12 10:54:11 -070064 NiceMockControl control_;
65 MockManager manager_;
66 MockMetrics metrics_;
Darin Petkovc63dcf02012-05-24 11:51:43 +020067 scoped_refptr<MockWiMax> device_;
Ben Chanc07362b2012-05-12 10:54:11 -070068 WiMaxServiceRefPtr service_;
69};
70
Ben Chan4e5c1312012-05-18 18:45:38 -070071TEST_F(WiMaxServiceTest, GetConnectParameters) {
72 {
Darin Petkov25665aa2012-05-21 14:08:12 +020073 KeyValueStore parameters;
Ben Chan4e5c1312012-05-18 18:45:38 -070074 service_->GetConnectParameters(&parameters);
75
Darin Petkov25665aa2012-05-21 14:08:12 +020076 EXPECT_FALSE(parameters.ContainsString(kEAPAnonymousIdentity));
77 EXPECT_FALSE(parameters.ContainsString(kEAPUserIdentity));
78 EXPECT_FALSE(parameters.ContainsString(kEAPUserPassword));
Ben Chan4e5c1312012-05-18 18:45:38 -070079 }
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 Petkov25665aa2012-05-21 14:08:12 +020087 KeyValueStore parameters;
Ben Chan4e5c1312012-05-18 18:45:38 -070088 service_->GetConnectParameters(&parameters);
89
Ben Chan4e5c1312012-05-18 18:45:38 -070090 EXPECT_EQ(eap.anonymous_identity,
Darin Petkov25665aa2012-05-21 14:08:12 +020091 parameters.LookupString(kEAPAnonymousIdentity, ""));
92 EXPECT_EQ(eap.identity, parameters.LookupString(kEAPUserIdentity, ""));
93 EXPECT_EQ(eap.password, parameters.LookupString(kEAPUserPassword, ""));
Ben Chan4e5c1312012-05-18 18:45:38 -070094 }
95}
96
Darin Petkove4b27022012-05-16 13:28:50 +020097TEST_F(WiMaxServiceTest, GetDeviceRpcId) {
98 Error error;
Darin Petkovc63dcf02012-05-24 11:51:43 +020099 EXPECT_EQ("/", service_->GetDeviceRpcId(&error));
100 EXPECT_EQ(Error::kNotSupported, error.type());
101 service_->device_ = device_;
102 error.Reset();
Darin Petkove4b27022012-05-16 13:28:50 +0200103 EXPECT_EQ(DeviceMockAdaptor::kRpcId, service_->GetDeviceRpcId(&error));
104 EXPECT_TRUE(error.IsSuccess());
105}
106
Darin Petkov1e52a1b2012-05-21 10:35:56 +0200107TEST_F(WiMaxServiceTest, OnSignalStrengthChanged) {
108 const int kStrength = 55;
109 service_->OnSignalStrengthChanged(kStrength);
110 EXPECT_EQ(kStrength, service_->strength());
111}
112
Darin Petkovd1cd7972012-05-22 15:26:15 +0200113TEST_F(WiMaxServiceTest, StartStop) {
114 static const char kName[] = "My WiMAX Network";
115 const uint32 kIdentifier = 0x1234abcd;
Darin Petkov1e52a1b2012-05-21 10:35:56 +0200116 const int kStrength = 66;
Darin Petkova3f9f772012-05-31 12:11:28 +0200117 EXPECT_FALSE(service_->connectable());
Darin Petkovd1cd7972012-05-22 15:26:15 +0200118 EXPECT_FALSE(service_->IsStarted());
119 EXPECT_EQ(0, service_->strength());
120 EXPECT_FALSE(service_->proxy_.get());
Darin Petkov1e52a1b2012-05-21 10:35:56 +0200121 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 Petkova3f9f772012-05-31 12:11:28 +0200125 service_->need_passphrase_ = false;
Darin Petkov1e52a1b2012-05-21 10:35:56 +0200126 EXPECT_TRUE(service_->Start(proxy_.release()));
Darin Petkovd1cd7972012-05-22 15:26:15 +0200127 EXPECT_TRUE(service_->IsStarted());
Darin Petkov1e52a1b2012-05-21 10:35:56 +0200128 EXPECT_EQ(kStrength, service_->strength());
129 EXPECT_EQ(kName, service_->network_name());
Darin Petkovd1cd7972012-05-22 15:26:15 +0200130 EXPECT_EQ(kTestName, service_->friendly_name());
131 EXPECT_EQ(kTestNetworkId, service_->network_id());
Darin Petkova3f9f772012-05-31 12:11:28 +0200132 EXPECT_TRUE(service_->connectable());
Darin Petkovd1cd7972012-05-22 15:26:15 +0200133 EXPECT_TRUE(service_->proxy_.get());
Darin Petkovc63dcf02012-05-24 11:51:43 +0200134
135 service_->device_ = device_;
136 EXPECT_CALL(*device_, OnServiceStopped(_));
Darin Petkovd1cd7972012-05-22 15:26:15 +0200137 service_->Stop();
138 EXPECT_FALSE(service_->IsStarted());
139 EXPECT_EQ(0, service_->strength());
140 EXPECT_FALSE(service_->proxy_.get());
Darin Petkov1e52a1b2012-05-21 10:35:56 +0200141}
142
Darin Petkov8021e7f2012-05-21 12:15:00 +0200143TEST_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 Petkovc1e52732012-05-25 15:23:45 +0200148
149 // No password.
Darin Petkov8021e7f2012-05-21 12:15:00 +0200150 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 Petkovc1e52732012-05-25 15:23:45 +0200155
156 // Not started.
Darin Petkov8021e7f2012-05-21 12:15:00 +0200157 eap.password = "TestPassword";
158 base_service->set_eap(eap);
Darin Petkova3f9f772012-05-31 12:11:28 +0200159 EXPECT_FALSE(service_->need_passphrase_);
Darin Petkovc1e52732012-05-25 15:23:45 +0200160 EXPECT_FALSE(base_service->connectable());
161
162 // Connectable.
163 service_->proxy_.reset(proxy_.release());
164 base_service->set_eap(eap);
Darin Petkova3f9f772012-05-31 12:11:28 +0200165 EXPECT_FALSE(service_->need_passphrase_);
Darin Petkov8021e7f2012-05-21 12:15:00 +0200166 EXPECT_TRUE(base_service->connectable());
Darin Petkova3f9f772012-05-31 12:11:28 +0200167
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 Petkov8021e7f2012-05-21 12:15:00 +0200173}
174
Darin Petkovd1cd7972012-05-22 15:26:15 +0200175TEST_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
181TEST_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
188TEST_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 Petkovc63dcf02012-05-24 11:51:43 +0200200TEST_F(WiMaxServiceTest, Connect) {
Darin Petkova3f9f772012-05-31 12:11:28 +0200201 // 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 Petkovc63dcf02012-05-24 11:51:43 +0200209 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 Petkova3f9f772012-05-31 12:11:28 +0200213 error.Reset();
Darin Petkovc63dcf02012-05-24 11:51:43 +0200214 service_->Connect(&error);
215 EXPECT_EQ(Error::kNoCarrier, error.type());
216
Darin Petkova3f9f772012-05-31 12:11:28 +0200217 // Successful connect.
Darin Petkovc63dcf02012-05-24 11:51:43 +0200218 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 Petkova3f9f772012-05-31 12:11:28 +0200225 // Connect while already connected.
Darin Petkovc63dcf02012-05-24 11:51:43 +0200226 service_->Connect(&error);
227 EXPECT_EQ(Error::kAlreadyConnected, error.type());
228
Darin Petkova3f9f772012-05-31 12:11:28 +0200229 // Successful disconnect.
Darin Petkovc63dcf02012-05-24 11:51:43 +0200230 EXPECT_CALL(*device_, DisconnectFrom(_, _));
231 error.Reset();
232 service_->Disconnect(&error);
233 EXPECT_TRUE(error.IsSuccess());
234
Darin Petkova3f9f772012-05-31 12:11:28 +0200235 // Disconnect while not connected.
Darin Petkovc63dcf02012-05-24 11:51:43 +0200236 service_->Disconnect(&error);
237 EXPECT_EQ(Error::kNotConnected, error.type());
238}
239
Darin Petkovc1e52732012-05-25 15:23:45 +0200240TEST_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 Petkov8ea0eaf2012-05-29 11:21:33 +0200258TEST_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 Chanc07362b2012-05-12 10:54:11 -0700274} // namespace shill