blob: 424c949f907dfb83a96c7774c0a2abb76f32df13 [file] [log] [blame]
Paul Stewartd8ad3c42012-01-09 12:39:38 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Masone3bd3c8c2011-06-13 08:20:26 -07002// 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/service.h"
6
7#include <map>
8#include <string>
9#include <vector>
10
Eric Shienbrood9a245532012-03-07 14:20:39 -050011#include <base/bind.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070012#include <chromeos/dbus/service_constants.h>
mukesh agrawal1830fa12011-09-26 14:31:40 -070013#include <dbus-c++/dbus.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070014#include <gtest/gtest.h>
15#include <gmock/gmock.h>
16
17#include "shill/dbus_adaptor.h"
18#include "shill/ethernet_service.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070019#include "shill/event_dispatcher.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070020#include "shill/manager.h"
Chris Masone95207da2011-06-29 16:50:49 -070021#include "shill/mock_adaptors.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070022#include "shill/mock_control.h"
Paul Stewart10241e32012-04-23 18:15:06 -070023#include "shill/mock_connection.h"
24#include "shill/mock_device_info.h"
Paul Stewart03dba0b2011-08-22 16:32:45 -070025#include "shill/mock_manager.h"
Paul Stewartff14b022012-04-24 20:06:23 -070026#include "shill/mock_profile.h"
Darin Petkovba40dd32011-07-11 20:06:39 -070027#include "shill/mock_store.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070028#include "shill/property_store_unittest.h"
Chris Masone6515aab2011-10-12 16:19:09 -070029#include "shill/service_under_test.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070030
Eric Shienbrood9a245532012-03-07 14:20:39 -050031using base::Bind;
32using base::Unretained;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070033using std::map;
34using std::string;
35using std::vector;
Darin Petkovba40dd32011-07-11 20:06:39 -070036using testing::_;
37using testing::AtLeast;
Paul Stewartd8ad3c42012-01-09 12:39:38 -080038using testing::DoAll;
Darin Petkovba40dd32011-07-11 20:06:39 -070039using testing::NiceMock;
40using testing::Return;
Paul Stewart10241e32012-04-23 18:15:06 -070041using testing::ReturnRef;
Darin Petkovba40dd32011-07-11 20:06:39 -070042using testing::StrictMock;
Paul Stewartd8ad3c42012-01-09 12:39:38 -080043using testing::SetArgumentPointee;
Darin Petkovba40dd32011-07-11 20:06:39 -070044using testing::Test;
Paul Stewart9f32d192012-01-30 20:37:50 -080045using testing::Values;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070046
47namespace shill {
48
49class ServiceTest : public PropertyStoreTest {
50 public:
Chris Masoneb925cc82011-06-22 15:39:57 -070051 ServiceTest()
Thieu Le3426c8f2012-01-11 17:35:11 -080052 : mock_manager_(control_interface(), dispatcher(), metrics(), glib()),
Chris Masone2176a882011-09-14 22:29:15 -070053 service_(new ServiceUnderTest(control_interface(),
54 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080055 metrics(),
Paul Stewart03dba0b2011-08-22 16:32:45 -070056 &mock_manager_)),
Chris Masone9d779932011-08-25 16:33:41 -070057 storage_id_(ServiceUnderTest::kStorageId) {
58 }
Chris Masoneb925cc82011-06-22 15:39:57 -070059
Chris Masone3bd3c8c2011-06-13 08:20:26 -070060 virtual ~ServiceTest() {}
Chris Masoneb925cc82011-06-22 15:39:57 -070061
Eric Shienbrood9a245532012-03-07 14:20:39 -050062 MOCK_METHOD1(TestCallback, void(const Error &error));
63
Chris Masoneb925cc82011-06-22 15:39:57 -070064 protected:
Eric Shienbrood9a245532012-03-07 14:20:39 -050065
Paul Stewart03dba0b2011-08-22 16:32:45 -070066 MockManager mock_manager_;
67 scoped_refptr<ServiceUnderTest> service_;
Chris Masone34af2182011-08-22 11:59:36 -070068 string storage_id_;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070069};
70
Darin Petkovba40dd32011-07-11 20:06:39 -070071TEST_F(ServiceTest, Constructor) {
72 EXPECT_TRUE(service_->save_credentials_);
73 EXPECT_EQ(Service::kCheckPortalAuto, service_->check_portal_);
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -040074 EXPECT_EQ(Service::kStateIdle, service_->state());
Darin Petkovba40dd32011-07-11 20:06:39 -070075}
76
Chris Masonea8a2c252011-06-27 22:16:30 -070077TEST_F(ServiceTest, GetProperties) {
78 map<string, ::DBus::Variant> props;
79 Error error(Error::kInvalidProperty, "");
80 {
81 ::DBus::Error dbus_error;
Paul Stewartd215af62012-04-24 23:25:50 -070082 string expected("true");
mukesh agrawalde29fa82011-09-16 16:16:36 -070083 service_->mutable_store()->SetStringProperty(flimflam::kCheckPortalProperty,
84 expected,
85 &error);
Chris Masone27c4aa52011-07-02 13:10:14 -070086 DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -070087 ASSERT_FALSE(props.find(flimflam::kCheckPortalProperty) == props.end());
88 EXPECT_EQ(props[flimflam::kCheckPortalProperty].reader().get_string(),
89 expected);
90 }
91 {
92 ::DBus::Error dbus_error;
93 bool expected = true;
Thieu Le284fe792012-01-31 17:53:19 -080094 service_->set_favorite(true);
mukesh agrawalde29fa82011-09-16 16:16:36 -070095 service_->mutable_store()->SetBoolProperty(flimflam::kAutoConnectProperty,
96 expected,
97 &error);
Chris Masone27c4aa52011-07-02 13:10:14 -070098 DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -070099 ASSERT_FALSE(props.find(flimflam::kAutoConnectProperty) == props.end());
100 EXPECT_EQ(props[flimflam::kAutoConnectProperty].reader().get_bool(),
101 expected);
102 }
103 {
104 ::DBus::Error dbus_error;
Chris Masone27c4aa52011-07-02 13:10:14 -0700105 DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700106 ASSERT_FALSE(props.find(flimflam::kConnectableProperty) == props.end());
107 EXPECT_EQ(props[flimflam::kConnectableProperty].reader().get_bool(), false);
108 }
109 {
110 ::DBus::Error dbus_error;
111 int32 expected = 127;
mukesh agrawalde29fa82011-09-16 16:16:36 -0700112 service_->mutable_store()->SetInt32Property(flimflam::kPriorityProperty,
113 expected,
114 &error);
Chris Masone27c4aa52011-07-02 13:10:14 -0700115 DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700116 ASSERT_FALSE(props.find(flimflam::kPriorityProperty) == props.end());
117 EXPECT_EQ(props[flimflam::kPriorityProperty].reader().get_int32(),
118 expected);
119 }
Chris Masone95207da2011-06-29 16:50:49 -0700120 {
121 ::DBus::Error dbus_error;
Chris Masone27c4aa52011-07-02 13:10:14 -0700122 DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error);
Chris Masone95207da2011-06-29 16:50:49 -0700123 ASSERT_FALSE(props.find(flimflam::kDeviceProperty) == props.end());
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400124 EXPECT_EQ(props[flimflam::kDeviceProperty].reader().get_path(),
Paul Stewart03dba0b2011-08-22 16:32:45 -0700125 string(ServiceUnderTest::kRpcId));
Chris Masone95207da2011-06-29 16:50:49 -0700126 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700127}
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700128
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800129TEST_F(ServiceTest, SetProperty) {
Chris Masonea8a2c252011-06-27 22:16:30 -0700130 {
131 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800132 EXPECT_TRUE(DBusAdaptor::SetProperty(service_->mutable_store(),
133 flimflam::kSaveCredentialsProperty,
134 PropertyStoreTest::kBoolV,
135 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -0700136 }
137 {
138 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800139 EXPECT_TRUE(DBusAdaptor::SetProperty(service_->mutable_store(),
140 flimflam::kPriorityProperty,
141 PropertyStoreTest::kInt32V,
142 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -0700143 }
144 {
145 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800146 EXPECT_TRUE(DBusAdaptor::SetProperty(service_->mutable_store(),
147 flimflam::kEAPEAPProperty,
148 PropertyStoreTest::kStringV,
149 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -0700150 }
Chris Masoneb925cc82011-06-22 15:39:57 -0700151 // Ensure that an attempt to write a R/O property returns InvalidArgs error.
Chris Masonea8a2c252011-06-27 22:16:30 -0700152 {
153 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800154 EXPECT_FALSE(DBusAdaptor::SetProperty(service_->mutable_store(),
155 flimflam::kFavoriteProperty,
156 PropertyStoreTest::kBoolV,
157 &error));
Chris Masone9d779932011-08-25 16:33:41 -0700158 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -0700159 }
Thieu Le284fe792012-01-31 17:53:19 -0800160 {
161 ::DBus::Error error;
162 service_->set_favorite(true);
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800163 EXPECT_TRUE(DBusAdaptor::SetProperty(service_->mutable_store(),
164 flimflam::kAutoConnectProperty,
165 PropertyStoreTest::kBoolV,
166 &error));
Thieu Le284fe792012-01-31 17:53:19 -0800167 }
168 {
169 ::DBus::Error error;
170 service_->set_favorite(false);
Wade Guthrie4f28e8b2012-04-11 10:52:07 -0700171 EXPECT_TRUE(DBusAdaptor::SetProperty(service_->mutable_store(),
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800172 flimflam::kAutoConnectProperty,
173 PropertyStoreTest::kBoolV,
174 &error));
Thieu Le284fe792012-01-31 17:53:19 -0800175 }
Paul Stewart0c438332012-04-11 07:55:27 -0700176 // Ensure that we can perform a trivial set of the Name property (to its
177 // current value) but an attempt to set the property to a different value
178 // fails.
179 {
180 ::DBus::Error error;
181 EXPECT_TRUE(DBusAdaptor::SetProperty(service_->mutable_store(),
182 flimflam::kNameProperty,
183 DBusAdaptor::StringToVariant(
184 service_->friendly_name()),
185 &error));
186 }
187 {
188 ::DBus::Error error;
189 EXPECT_FALSE(DBusAdaptor::SetProperty(service_->mutable_store(),
190 flimflam::kNameProperty,
191 PropertyStoreTest::kStringV,
192 &error));
193 EXPECT_EQ(invalid_args(), error.name());
194 }
Chris Masoneb925cc82011-06-22 15:39:57 -0700195}
196
Darin Petkovba40dd32011-07-11 20:06:39 -0700197TEST_F(ServiceTest, Load) {
198 NiceMock<MockStore> storage;
Chris Masone34af2182011-08-22 11:59:36 -0700199 EXPECT_CALL(storage, ContainsGroup(storage_id_)).WillOnce(Return(true));
200 EXPECT_CALL(storage, GetString(storage_id_, _, _))
Darin Petkovba40dd32011-07-11 20:06:39 -0700201 .Times(AtLeast(1))
202 .WillRepeatedly(Return(true));
Chris Masone9d779932011-08-25 16:33:41 -0700203 EXPECT_TRUE(service_->Load(&storage));
Darin Petkovba40dd32011-07-11 20:06:39 -0700204}
205
206TEST_F(ServiceTest, LoadFail) {
207 StrictMock<MockStore> storage;
Chris Masone34af2182011-08-22 11:59:36 -0700208 EXPECT_CALL(storage, ContainsGroup(storage_id_)).WillOnce(Return(false));
Chris Masone9d779932011-08-25 16:33:41 -0700209 EXPECT_FALSE(service_->Load(&storage));
Darin Petkovba40dd32011-07-11 20:06:39 -0700210}
211
212TEST_F(ServiceTest, SaveString) {
213 MockStore storage;
214 static const char kKey[] = "test-key";
215 static const char kData[] = "test-data";
Chris Masone34af2182011-08-22 11:59:36 -0700216 EXPECT_CALL(storage, SetString(storage_id_, kKey, kData))
Darin Petkovba40dd32011-07-11 20:06:39 -0700217 .WillOnce(Return(true));
Chris Masone34af2182011-08-22 11:59:36 -0700218 service_->SaveString(&storage, storage_id_, kKey, kData, false, true);
Darin Petkovba40dd32011-07-11 20:06:39 -0700219}
220
221TEST_F(ServiceTest, SaveStringCrypted) {
222 MockStore storage;
223 static const char kKey[] = "test-key";
224 static const char kData[] = "test-data";
Chris Masone34af2182011-08-22 11:59:36 -0700225 EXPECT_CALL(storage, SetCryptedString(storage_id_, kKey, kData))
Darin Petkovba40dd32011-07-11 20:06:39 -0700226 .WillOnce(Return(true));
Chris Masone34af2182011-08-22 11:59:36 -0700227 service_->SaveString(&storage, storage_id_, kKey, kData, true, true);
Darin Petkovba40dd32011-07-11 20:06:39 -0700228}
229
230TEST_F(ServiceTest, SaveStringDontSave) {
231 MockStore storage;
232 static const char kKey[] = "test-key";
Chris Masone34af2182011-08-22 11:59:36 -0700233 EXPECT_CALL(storage, DeleteKey(storage_id_, kKey))
Darin Petkovba40dd32011-07-11 20:06:39 -0700234 .WillOnce(Return(true));
Chris Masone34af2182011-08-22 11:59:36 -0700235 service_->SaveString(&storage, storage_id_, kKey, "data", false, false);
Darin Petkovba40dd32011-07-11 20:06:39 -0700236}
237
238TEST_F(ServiceTest, SaveStringEmpty) {
239 MockStore storage;
240 static const char kKey[] = "test-key";
Chris Masone34af2182011-08-22 11:59:36 -0700241 EXPECT_CALL(storage, DeleteKey(storage_id_, kKey))
Darin Petkovba40dd32011-07-11 20:06:39 -0700242 .WillOnce(Return(true));
Chris Masone34af2182011-08-22 11:59:36 -0700243 service_->SaveString(&storage, storage_id_, kKey, "", true, true);
Darin Petkovba40dd32011-07-11 20:06:39 -0700244}
245
246TEST_F(ServiceTest, Save) {
247 NiceMock<MockStore> storage;
Chris Masone34af2182011-08-22 11:59:36 -0700248 EXPECT_CALL(storage, SetString(storage_id_, _, _))
Darin Petkovba40dd32011-07-11 20:06:39 -0700249 .Times(AtLeast(1))
250 .WillRepeatedly(Return(true));
Chris Masone34af2182011-08-22 11:59:36 -0700251 EXPECT_CALL(storage, DeleteKey(storage_id_, _))
Darin Petkovba40dd32011-07-11 20:06:39 -0700252 .Times(AtLeast(1))
253 .WillRepeatedly(Return(true));
Chris Masone9d779932011-08-25 16:33:41 -0700254 EXPECT_TRUE(service_->Save(&storage));
Darin Petkovba40dd32011-07-11 20:06:39 -0700255}
256
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800257TEST_F(ServiceTest, Unload) {
258 NiceMock<MockStore> storage;
259 EXPECT_CALL(storage, ContainsGroup(storage_id_)).WillOnce(Return(true));
260 static const string string_value("value");
261 EXPECT_CALL(storage, GetString(storage_id_, _, _))
262 .Times(AtLeast(1))
263 .WillRepeatedly(DoAll(SetArgumentPointee<2>(string_value), Return(true)));
264 ASSERT_TRUE(service_->Load(&storage));
265 // TODO(pstew): A single string property in the service is tested as
266 // a sentinel that properties are being set and reset at the rit times.
267 // However, since property load/store is essentially a manual process,
268 // it is error prone and should either be exhaustively unit-tested or
269 // a generic framework for registering loaded/stored properties should
270 // be created. crosbug.com/24859
271 EXPECT_EQ(string_value, service_->ui_data_);
272 service_->Unload();
273 EXPECT_EQ(string(""), service_->ui_data_);
274}
275
Paul Stewart03dba0b2011-08-22 16:32:45 -0700276TEST_F(ServiceTest, State) {
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400277 EXPECT_EQ(Service::kStateIdle, service_->state());
Paul Stewart03dba0b2011-08-22 16:32:45 -0700278 EXPECT_EQ(Service::kFailureUnknown, service_->failure());
279
mukesh agrawal00917ce2011-11-22 23:56:55 +0000280 ServiceRefPtr service_ref(service_);
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700281
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400282 EXPECT_CALL(*dynamic_cast<ServiceMockAdaptor *>(service_->adaptor_.get()),
283 EmitStringChanged(flimflam::kStateProperty, _)).Times(5);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700284 EXPECT_CALL(mock_manager_, UpdateService(service_ref));
285 service_->SetState(Service::kStateConnected);
286 // A second state change shouldn't cause another update
287 service_->SetState(Service::kStateConnected);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700288 EXPECT_EQ(Service::kStateConnected, service_->state());
289 EXPECT_EQ(Service::kFailureUnknown, service_->failure());
mukesh agrawal568b5c62012-02-28 14:44:47 -0800290
Paul Stewart03dba0b2011-08-22 16:32:45 -0700291 EXPECT_CALL(mock_manager_, UpdateService(service_ref));
292 service_->SetState(Service::kStateDisconnected);
293
294 EXPECT_CALL(mock_manager_, UpdateService(service_ref));
295 service_->SetFailure(Service::kFailureOutOfRange);
mukesh agrawal568b5c62012-02-28 14:44:47 -0800296 EXPECT_TRUE(service_->IsFailed());
297 EXPECT_GT(service_->failed_time_, 0);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700298 EXPECT_EQ(Service::kStateFailure, service_->state());
299 EXPECT_EQ(Service::kFailureOutOfRange, service_->failure());
mukesh agrawal568b5c62012-02-28 14:44:47 -0800300
301 EXPECT_CALL(mock_manager_, UpdateService(service_ref));
302 service_->SetState(Service::kStateConnected);
303 EXPECT_FALSE(service_->IsFailed());
304 EXPECT_EQ(service_->failed_time_, 0);
305
306 EXPECT_CALL(mock_manager_, UpdateService(service_ref));
307 service_->SetFailureSilent(Service::kFailurePinMissing);
308 EXPECT_TRUE(service_->IsFailed());
309 EXPECT_GT(service_->failed_time_, 0);
310 EXPECT_EQ(Service::kStateIdle, service_->state());
311 EXPECT_EQ(Service::kFailurePinMissing, service_->failure());
Paul Stewart03dba0b2011-08-22 16:32:45 -0700312}
313
Darin Petkovb100ae72011-08-24 16:19:45 -0700314TEST_F(ServiceTest, ActivateCellularModem) {
Eric Shienbrood9a245532012-03-07 14:20:39 -0500315 ResultCallback callback =
316 Bind(&ServiceTest::TestCallback, Unretained(this));
317 EXPECT_CALL(*this, TestCallback(_)).Times(0);
Darin Petkovb100ae72011-08-24 16:19:45 -0700318 Error error;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500319 service_->ActivateCellularModem("Carrier", &error, callback);
320 EXPECT_TRUE(error.IsFailure());
Darin Petkovb100ae72011-08-24 16:19:45 -0700321}
322
mukesh agrawal00917ce2011-11-22 23:56:55 +0000323TEST_F(ServiceTest, MakeFavorite) {
324 EXPECT_FALSE(service_->favorite());
325 EXPECT_FALSE(service_->auto_connect());
326
327 service_->MakeFavorite();
328 EXPECT_TRUE(service_->favorite());
329 EXPECT_TRUE(service_->auto_connect());
330}
331
332TEST_F(ServiceTest, ReMakeFavorite) {
333 service_->MakeFavorite();
334 EXPECT_TRUE(service_->favorite());
335 EXPECT_TRUE(service_->auto_connect());
336
337 service_->set_auto_connect(false);
338 service_->MakeFavorite();
339 EXPECT_TRUE(service_->favorite());
340 EXPECT_FALSE(service_->auto_connect());
341}
342
mukesh agrawal76d13882012-01-12 15:23:11 -0800343TEST_F(ServiceTest, IsAutoConnectable) {
mukesh agrawalbf14e942012-03-02 14:36:34 -0800344 const char *reason;
mukesh agrawal76d13882012-01-12 15:23:11 -0800345 service_->set_connectable(true);
mukesh agrawalbf14e942012-03-02 14:36:34 -0800346 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
mukesh agrawal76d13882012-01-12 15:23:11 -0800347
mukesh agrawaladb68482012-01-17 16:31:51 -0800348 // We should not auto-connect to a Service that a user has
349 // deliberately disconnected.
350 Error error;
351 service_->Disconnect(&error);
mukesh agrawalbf14e942012-03-02 14:36:34 -0800352 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
353 EXPECT_STREQ(Service::kAutoConnExplicitDisconnect, reason);
mukesh agrawaladb68482012-01-17 16:31:51 -0800354
355 // But if the Service is reloaded, it is eligible for auto-connect
356 // again.
357 NiceMock<MockStore> storage;
358 EXPECT_CALL(storage, ContainsGroup(storage_id_)).WillOnce(Return(true));
359 EXPECT_TRUE(service_->Load(&storage));
mukesh agrawalbf14e942012-03-02 14:36:34 -0800360 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
mukesh agrawaladb68482012-01-17 16:31:51 -0800361
362 // A deliberate Connect should also re-enable auto-connect.
363 service_->Disconnect(&error);
mukesh agrawalbf14e942012-03-02 14:36:34 -0800364 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
mukesh agrawaladb68482012-01-17 16:31:51 -0800365 service_->Connect(&error);
mukesh agrawalbf14e942012-03-02 14:36:34 -0800366 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
mukesh agrawaladb68482012-01-17 16:31:51 -0800367
368 // TODO(quiche): After we have resume handling in place, test that
369 // we re-enable auto-connect on resume. crosbug.com/25213
370
mukesh agrawal76d13882012-01-12 15:23:11 -0800371 service_->SetState(Service::kStateConnected);
mukesh agrawalbf14e942012-03-02 14:36:34 -0800372 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
373 EXPECT_STREQ(Service::kAutoConnConnected, reason);
mukesh agrawal76d13882012-01-12 15:23:11 -0800374
375 service_->SetState(Service::kStateAssociating);
mukesh agrawalbf14e942012-03-02 14:36:34 -0800376 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
377 EXPECT_STREQ(Service::kAutoConnConnecting, reason);
mukesh agrawal76d13882012-01-12 15:23:11 -0800378}
379
Paul Stewartcb59fed2012-03-21 21:14:46 -0700380TEST_F(ServiceTest, ConfigureBadProperty) {
381 KeyValueStore args;
382 args.SetString("XXXInvalid", "Value");
383 Error error;
384 service_->Configure(args, &error);
385 EXPECT_FALSE(error.IsSuccess());
386}
387
388TEST_F(ServiceTest, ConfigureBoolProperty) {
389 service_->MakeFavorite();
390 service_->set_auto_connect(false);
391 ASSERT_FALSE(service_->auto_connect());
392 KeyValueStore args;
393 args.SetBool(flimflam::kAutoConnectProperty, true);
394 Error error;
395 service_->Configure(args, &error);
396 EXPECT_TRUE(error.IsSuccess());
397 EXPECT_TRUE(service_->auto_connect());
398}
399
400TEST_F(ServiceTest, ConfigureStringProperty) {
401 const string kEAPManagement0 = "management_zero";
402 const string kEAPManagement1 = "management_one";
403 service_->SetEAPKeyManagement(kEAPManagement0);
404 ASSERT_EQ(kEAPManagement0, service_->GetEAPKeyManagement());
405 KeyValueStore args;
406 args.SetString(flimflam::kEapKeyMgmtProperty, kEAPManagement1);
407 Error error;
408 service_->Configure(args, &error);
409 EXPECT_TRUE(error.IsSuccess());
410 EXPECT_EQ(kEAPManagement1, service_->GetEAPKeyManagement());
411}
412
413TEST_F(ServiceTest, ConfigureIgnoredProperty) {
414 service_->MakeFavorite();
415 service_->set_auto_connect(false);
416 ASSERT_FALSE(service_->auto_connect());
417 KeyValueStore args;
418 args.SetBool(flimflam::kAutoConnectProperty, true);
419 Error error;
420 service_->IgnoreParameterForConfigure(flimflam::kAutoConnectProperty);
421 service_->Configure(args, &error);
422 EXPECT_TRUE(error.IsSuccess());
423 EXPECT_FALSE(service_->auto_connect());
424}
425
Paul Stewart10ccbb32012-04-26 15:59:30 -0700426TEST_F(ServiceTest, IsRemembered) {
427 ServiceConstRefPtr service_ref(service_);
428 service_->set_profile(NULL);
429 EXPECT_CALL(mock_manager_, IsServiceEphemeral(_)).Times(0);
430 EXPECT_FALSE(service_->IsRemembered());
431
432 scoped_refptr<MockProfile> profile(
433 new StrictMock<MockProfile>(control_interface(), manager()));
434 service_->set_profile(profile);
435 EXPECT_CALL(mock_manager_, IsServiceEphemeral(service_ref))
436 .WillOnce(Return(true))
437 .WillOnce(Return(false));
438 EXPECT_FALSE(service_->IsRemembered());
439 EXPECT_TRUE(service_->IsRemembered());
440}
441
Paul Stewartff14b022012-04-24 20:06:23 -0700442TEST_F(ServiceTest, OnPropertyChanged) {
443 scoped_refptr<MockProfile> profile(
444 new StrictMock<MockProfile>(control_interface(), manager()));
445 service_->set_profile(NULL);
446 // Expect no crash.
447 service_->OnPropertyChanged("");
448
449 // Expect no call to Update if the profile has no storage.
450 service_->set_profile(profile);
451 EXPECT_CALL(*profile, UpdateService(_)).Times(0);
452 EXPECT_CALL(*profile, GetConstStorage())
453 .WillOnce(Return(reinterpret_cast<StoreInterface *>(NULL)));
454 service_->OnPropertyChanged("");
455
456 // Expect call to Update if the profile has storage.
457 EXPECT_CALL(*profile, UpdateService(_)).Times(1);
458 NiceMock<MockStore> storage;
459 EXPECT_CALL(*profile, GetConstStorage()).WillOnce(Return(&storage));
460 service_->OnPropertyChanged("");
461}
462
Paul Stewartd215af62012-04-24 23:25:50 -0700463
464TEST_F(ServiceTest, RecheckPortal) {
465 ServiceRefPtr service_ref(service_);
466 service_->state_ = Service::kStateIdle;
467 EXPECT_CALL(mock_manager_, RecheckPortalOnService(_)).Times(0);
468 service_->OnPropertyChanged(flimflam::kCheckPortalProperty);
469
470 service_->state_ = Service::kStatePortal;
471 EXPECT_CALL(mock_manager_, RecheckPortalOnService(service_ref)).Times(1);
472 service_->OnPropertyChanged(flimflam::kCheckPortalProperty);
473
474 service_->state_ = Service::kStateConnected;
475 EXPECT_CALL(mock_manager_, RecheckPortalOnService(service_ref)).Times(1);
476 service_->OnPropertyChanged(flimflam::kProxyConfigProperty);
477
478 service_->state_ = Service::kStateOnline;
479 EXPECT_CALL(mock_manager_, RecheckPortalOnService(service_ref)).Times(1);
480 service_->OnPropertyChanged(flimflam::kCheckPortalProperty);
481
482 service_->state_ = Service::kStatePortal;
483 EXPECT_CALL(mock_manager_, RecheckPortalOnService(_)).Times(0);
484 service_->OnPropertyChanged(flimflam::kEAPKeyIDProperty);
485}
486
487TEST_F(ServiceTest, SetCheckPortal) {
488 ServiceRefPtr service_ref(service_);
489 {
490 Error error;
491 service_->SetCheckPortal("false", &error);
492 EXPECT_TRUE(error.IsSuccess());
493 EXPECT_EQ(Service::kCheckPortalFalse, service_->check_portal_);
494 }
495 {
496 Error error;
497 service_->SetCheckPortal("true", &error);
498 EXPECT_TRUE(error.IsSuccess());
499 EXPECT_EQ(Service::kCheckPortalTrue, service_->check_portal_);
500 }
501 {
502 Error error;
503 service_->SetCheckPortal("auto", &error);
504 EXPECT_TRUE(error.IsSuccess());
505 EXPECT_EQ(Service::kCheckPortalAuto, service_->check_portal_);
506 }
507 {
508 Error error;
509 service_->SetCheckPortal("xxx", &error);
510 EXPECT_FALSE(error.IsSuccess());
511 EXPECT_EQ(Error::kInvalidArguments, error.type());
512 EXPECT_EQ(Service::kCheckPortalAuto, service_->check_portal_);
513 }
514}
515
Paul Stewart9f32d192012-01-30 20:37:50 -0800516// Make sure a property is registered as a write only property
517// by reading and comparing all string properties returned on the store.
518// Subtle: We need to convert the test argument back and forth between
519// string and ::DBus::Variant because this is the parameter type that
520// our supeclass (PropertyStoreTest) is declared with.
521class ReadOnlyServicePropertyTest : public ServiceTest {};
522TEST_P(ReadOnlyServicePropertyTest, PropertyWriteOnly) {
523 ReadablePropertyConstIterator<string> it =
524 (service_->store()).GetStringPropertiesIter();
525 string property(GetParam().reader().get_string());
526 for( ; !it.AtEnd(); it.Advance())
527 EXPECT_NE(it.Key(), property);
528}
529
530INSTANTIATE_TEST_CASE_P(
531 ReadOnlyServicePropertyTestInstance,
532 ReadOnlyServicePropertyTest,
533 Values(
534 DBusAdaptor::StringToVariant(flimflam::kEapPrivateKeyPasswordProperty),
535 DBusAdaptor::StringToVariant(flimflam::kEapPasswordProperty)));
536
Paul Stewart10241e32012-04-23 18:15:06 -0700537
538TEST_F(ServiceTest, GetIPConfigRpcIdentifier) {
539 {
540 Error error;
541 EXPECT_EQ("/", service_->GetIPConfigRpcIdentifier(&error));
542 EXPECT_EQ(Error::kNotFound, error.type());
543 }
544
545 scoped_ptr<MockDeviceInfo> mock_device_info(
546 new NiceMock<MockDeviceInfo>(control_interface(), dispatcher(), metrics(),
547 &mock_manager_));
548 scoped_refptr<MockConnection> mock_connection(
549 new NiceMock<MockConnection>(mock_device_info.get()));
550
551 service_->connection_ = mock_connection;
552
553 {
554 Error error;
555 const string empty_string;
556 EXPECT_CALL(*mock_connection, ipconfig_rpc_identifier())
557 .WillOnce(ReturnRef(empty_string));
558 EXPECT_EQ("/", service_->GetIPConfigRpcIdentifier(&error));
559 EXPECT_EQ(Error::kNotFound, error.type());
560 }
561
562 {
563 Error error;
564 const string nonempty_string("/ipconfig/path");
565 EXPECT_CALL(*mock_connection, ipconfig_rpc_identifier())
566 .WillOnce(ReturnRef(nonempty_string));
567 EXPECT_EQ(nonempty_string, service_->GetIPConfigRpcIdentifier(&error));
568 EXPECT_EQ(Error::kSuccess, error.type());
569 }
570
571 // Assure orderly destruction of the Connection before DeviceInfo.
572 service_->connection_ = NULL;
573 mock_connection = NULL;
574 mock_device_info.reset();
575}
576
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700577} // namespace shill