blob: f21e8ee3882413cce11c6f4be830c09fec4342c5 [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"
Christopher Wiley0801d192012-09-24 11:57:15 -070025#include "shill/mock_event_dispatcher.h"
Paul Stewartbc6e7392012-05-24 07:07:48 -070026#include "shill/mock_log.h"
Paul Stewart03dba0b2011-08-22 16:32:45 -070027#include "shill/mock_manager.h"
Paul Stewartff14b022012-04-24 20:06:23 -070028#include "shill/mock_profile.h"
Darin Petkovba40dd32011-07-11 20:06:39 -070029#include "shill/mock_store.h"
Paul Stewart8e7e4592012-04-29 09:47:48 -070030#include "shill/property_store_inspector.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070031#include "shill/property_store_unittest.h"
Chris Masone6515aab2011-10-12 16:19:09 -070032#include "shill/service_under_test.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070033
Eric Shienbrood9a245532012-03-07 14:20:39 -050034using base::Bind;
35using base::Unretained;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070036using std::map;
37using std::string;
38using std::vector;
Darin Petkovba40dd32011-07-11 20:06:39 -070039using testing::_;
mukesh agrawalcf24a242012-05-21 16:46:11 -070040using testing::AnyNumber;
Darin Petkovba40dd32011-07-11 20:06:39 -070041using testing::AtLeast;
Paul Stewartd8ad3c42012-01-09 12:39:38 -080042using testing::DoAll;
Paul Stewartbc6e7392012-05-24 07:07:48 -070043using testing::HasSubstr;
Paul Stewart81426132012-05-16 10:05:10 -070044using testing::Mock;
Darin Petkovba40dd32011-07-11 20:06:39 -070045using testing::NiceMock;
46using testing::Return;
Paul Stewart10241e32012-04-23 18:15:06 -070047using testing::ReturnRef;
Darin Petkovba40dd32011-07-11 20:06:39 -070048using testing::StrictMock;
Paul Stewartd8ad3c42012-01-09 12:39:38 -080049using testing::SetArgumentPointee;
Darin Petkovba40dd32011-07-11 20:06:39 -070050using testing::Test;
Paul Stewart9f32d192012-01-30 20:37:50 -080051using testing::Values;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070052
53namespace shill {
54
55class ServiceTest : public PropertyStoreTest {
56 public:
Chris Masoneb925cc82011-06-22 15:39:57 -070057 ServiceTest()
Thieu Le3426c8f2012-01-11 17:35:11 -080058 : mock_manager_(control_interface(), dispatcher(), metrics(), glib()),
Chris Masone2176a882011-09-14 22:29:15 -070059 service_(new ServiceUnderTest(control_interface(),
60 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080061 metrics(),
Paul Stewart03dba0b2011-08-22 16:32:45 -070062 &mock_manager_)),
Chris Masone9d779932011-08-25 16:33:41 -070063 storage_id_(ServiceUnderTest::kStorageId) {
64 }
Chris Masoneb925cc82011-06-22 15:39:57 -070065
Chris Masone3bd3c8c2011-06-13 08:20:26 -070066 virtual ~ServiceTest() {}
Chris Masoneb925cc82011-06-22 15:39:57 -070067
Eric Shienbrood9a245532012-03-07 14:20:39 -050068 MOCK_METHOD1(TestCallback, void(const Error &error));
69
Chris Masoneb925cc82011-06-22 15:39:57 -070070 protected:
mukesh agrawalcf24a242012-05-21 16:46:11 -070071 typedef scoped_refptr<MockProfile> MockProfileRefPtr;
Eric Shienbrood9a245532012-03-07 14:20:39 -050072
Paul Stewart03dba0b2011-08-22 16:32:45 -070073 MockManager mock_manager_;
74 scoped_refptr<ServiceUnderTest> service_;
Chris Masone34af2182011-08-22 11:59:36 -070075 string storage_id_;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070076};
77
Christopher Wiley0801d192012-09-24 11:57:15 -070078class AllMockServiceTest : public testing::Test {
79 public:
80 AllMockServiceTest()
81 : manager_(&control_interface_, &dispatcher_, &metrics_, &glib_),
82 service_(new ServiceUnderTest(&control_interface_,
83 &dispatcher_,
84 &metrics_,
85 &manager_)) { }
86 virtual ~AllMockServiceTest() {}
87
88 protected:
89 MockControl control_interface_;
90 StrictMock<MockEventDispatcher> dispatcher_;
91 MockGLib glib_;
92 NiceMock<MockMetrics> metrics_;
93 MockManager manager_;
94 scoped_refptr<ServiceUnderTest> service_;
95};
96
Darin Petkovba40dd32011-07-11 20:06:39 -070097TEST_F(ServiceTest, Constructor) {
98 EXPECT_TRUE(service_->save_credentials_);
99 EXPECT_EQ(Service::kCheckPortalAuto, service_->check_portal_);
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400100 EXPECT_EQ(Service::kStateIdle, service_->state());
mukesh agrawalcf24a242012-05-21 16:46:11 -0700101 EXPECT_FALSE(service_->has_ever_connected());
Darin Petkovba40dd32011-07-11 20:06:39 -0700102}
103
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200104TEST_F(ServiceTest, CalculateState) {
105 service_->state_ = Service::kStateConnected;
106 Error error;
107 EXPECT_EQ(flimflam::kStateReady, service_->CalculateState(&error));
108 EXPECT_TRUE(error.IsSuccess());
109}
110
111TEST_F(ServiceTest, CalculateTechnology) {
112 service_->technology_ = Technology::kWifi;
113 Error error;
114 EXPECT_EQ(flimflam::kTypeWifi, service_->CalculateTechnology(&error));
115 EXPECT_TRUE(error.IsSuccess());
116}
117
Chris Masonea8a2c252011-06-27 22:16:30 -0700118TEST_F(ServiceTest, GetProperties) {
119 map<string, ::DBus::Variant> props;
120 Error error(Error::kInvalidProperty, "");
121 {
122 ::DBus::Error dbus_error;
Paul Stewartd215af62012-04-24 23:25:50 -0700123 string expected("true");
mukesh agrawalde29fa82011-09-16 16:16:36 -0700124 service_->mutable_store()->SetStringProperty(flimflam::kCheckPortalProperty,
125 expected,
126 &error);
Chris Masone27c4aa52011-07-02 13:10:14 -0700127 DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700128 ASSERT_FALSE(props.find(flimflam::kCheckPortalProperty) == props.end());
129 EXPECT_EQ(props[flimflam::kCheckPortalProperty].reader().get_string(),
130 expected);
131 }
132 {
133 ::DBus::Error dbus_error;
134 bool expected = true;
Thieu Le284fe792012-01-31 17:53:19 -0800135 service_->set_favorite(true);
mukesh agrawalde29fa82011-09-16 16:16:36 -0700136 service_->mutable_store()->SetBoolProperty(flimflam::kAutoConnectProperty,
137 expected,
138 &error);
Chris Masone27c4aa52011-07-02 13:10:14 -0700139 DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700140 ASSERT_FALSE(props.find(flimflam::kAutoConnectProperty) == props.end());
141 EXPECT_EQ(props[flimflam::kAutoConnectProperty].reader().get_bool(),
142 expected);
143 }
144 {
145 ::DBus::Error dbus_error;
Chris Masone27c4aa52011-07-02 13:10:14 -0700146 DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700147 ASSERT_FALSE(props.find(flimflam::kConnectableProperty) == props.end());
148 EXPECT_EQ(props[flimflam::kConnectableProperty].reader().get_bool(), false);
149 }
150 {
151 ::DBus::Error dbus_error;
152 int32 expected = 127;
mukesh agrawalde29fa82011-09-16 16:16:36 -0700153 service_->mutable_store()->SetInt32Property(flimflam::kPriorityProperty,
154 expected,
155 &error);
Chris Masone27c4aa52011-07-02 13:10:14 -0700156 DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700157 ASSERT_FALSE(props.find(flimflam::kPriorityProperty) == props.end());
158 EXPECT_EQ(props[flimflam::kPriorityProperty].reader().get_int32(),
159 expected);
160 }
Chris Masone95207da2011-06-29 16:50:49 -0700161 {
162 ::DBus::Error dbus_error;
Chris Masone27c4aa52011-07-02 13:10:14 -0700163 DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error);
Chris Masone95207da2011-06-29 16:50:49 -0700164 ASSERT_FALSE(props.find(flimflam::kDeviceProperty) == props.end());
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400165 EXPECT_EQ(props[flimflam::kDeviceProperty].reader().get_path(),
Paul Stewart03dba0b2011-08-22 16:32:45 -0700166 string(ServiceUnderTest::kRpcId));
Chris Masone95207da2011-06-29 16:50:49 -0700167 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700168}
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700169
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800170TEST_F(ServiceTest, SetProperty) {
Chris Masonea8a2c252011-06-27 22:16:30 -0700171 {
172 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800173 EXPECT_TRUE(DBusAdaptor::SetProperty(service_->mutable_store(),
174 flimflam::kSaveCredentialsProperty,
175 PropertyStoreTest::kBoolV,
176 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -0700177 }
178 {
179 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800180 EXPECT_TRUE(DBusAdaptor::SetProperty(service_->mutable_store(),
181 flimflam::kPriorityProperty,
182 PropertyStoreTest::kInt32V,
183 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -0700184 }
185 {
186 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800187 EXPECT_TRUE(DBusAdaptor::SetProperty(service_->mutable_store(),
188 flimflam::kEAPEAPProperty,
189 PropertyStoreTest::kStringV,
190 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -0700191 }
Chris Masoneb925cc82011-06-22 15:39:57 -0700192 // Ensure that an attempt to write a R/O property returns InvalidArgs error.
Chris Masonea8a2c252011-06-27 22:16:30 -0700193 {
194 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800195 EXPECT_FALSE(DBusAdaptor::SetProperty(service_->mutable_store(),
196 flimflam::kFavoriteProperty,
197 PropertyStoreTest::kBoolV,
198 &error));
Chris Masone9d779932011-08-25 16:33:41 -0700199 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -0700200 }
Thieu Le284fe792012-01-31 17:53:19 -0800201 {
202 ::DBus::Error error;
203 service_->set_favorite(true);
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800204 EXPECT_TRUE(DBusAdaptor::SetProperty(service_->mutable_store(),
205 flimflam::kAutoConnectProperty,
206 PropertyStoreTest::kBoolV,
207 &error));
Thieu Le284fe792012-01-31 17:53:19 -0800208 }
209 {
210 ::DBus::Error error;
211 service_->set_favorite(false);
Wade Guthrie4f28e8b2012-04-11 10:52:07 -0700212 EXPECT_TRUE(DBusAdaptor::SetProperty(service_->mutable_store(),
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800213 flimflam::kAutoConnectProperty,
214 PropertyStoreTest::kBoolV,
215 &error));
Thieu Le284fe792012-01-31 17:53:19 -0800216 }
Paul Stewart0c438332012-04-11 07:55:27 -0700217 // Ensure that we can perform a trivial set of the Name property (to its
218 // current value) but an attempt to set the property to a different value
219 // fails.
220 {
221 ::DBus::Error error;
222 EXPECT_TRUE(DBusAdaptor::SetProperty(service_->mutable_store(),
223 flimflam::kNameProperty,
224 DBusAdaptor::StringToVariant(
225 service_->friendly_name()),
226 &error));
227 }
228 {
229 ::DBus::Error error;
230 EXPECT_FALSE(DBusAdaptor::SetProperty(service_->mutable_store(),
231 flimflam::kNameProperty,
232 PropertyStoreTest::kStringV,
233 &error));
234 EXPECT_EQ(invalid_args(), error.name());
235 }
Chris Masoneb925cc82011-06-22 15:39:57 -0700236}
237
Darin Petkovba40dd32011-07-11 20:06:39 -0700238TEST_F(ServiceTest, Load) {
239 NiceMock<MockStore> storage;
Chris Masone34af2182011-08-22 11:59:36 -0700240 EXPECT_CALL(storage, ContainsGroup(storage_id_)).WillOnce(Return(true));
241 EXPECT_CALL(storage, GetString(storage_id_, _, _))
Darin Petkovba40dd32011-07-11 20:06:39 -0700242 .Times(AtLeast(1))
243 .WillRepeatedly(Return(true));
Chris Masone9d779932011-08-25 16:33:41 -0700244 EXPECT_TRUE(service_->Load(&storage));
Darin Petkovba40dd32011-07-11 20:06:39 -0700245}
246
247TEST_F(ServiceTest, LoadFail) {
248 StrictMock<MockStore> storage;
Chris Masone34af2182011-08-22 11:59:36 -0700249 EXPECT_CALL(storage, ContainsGroup(storage_id_)).WillOnce(Return(false));
Chris Masone9d779932011-08-25 16:33:41 -0700250 EXPECT_FALSE(service_->Load(&storage));
Darin Petkovba40dd32011-07-11 20:06:39 -0700251}
252
253TEST_F(ServiceTest, SaveString) {
254 MockStore storage;
255 static const char kKey[] = "test-key";
256 static const char kData[] = "test-data";
Chris Masone34af2182011-08-22 11:59:36 -0700257 EXPECT_CALL(storage, SetString(storage_id_, kKey, kData))
Darin Petkovba40dd32011-07-11 20:06:39 -0700258 .WillOnce(Return(true));
Chris Masone34af2182011-08-22 11:59:36 -0700259 service_->SaveString(&storage, storage_id_, kKey, kData, false, true);
Darin Petkovba40dd32011-07-11 20:06:39 -0700260}
261
262TEST_F(ServiceTest, SaveStringCrypted) {
263 MockStore storage;
264 static const char kKey[] = "test-key";
265 static const char kData[] = "test-data";
Chris Masone34af2182011-08-22 11:59:36 -0700266 EXPECT_CALL(storage, SetCryptedString(storage_id_, kKey, kData))
Darin Petkovba40dd32011-07-11 20:06:39 -0700267 .WillOnce(Return(true));
Chris Masone34af2182011-08-22 11:59:36 -0700268 service_->SaveString(&storage, storage_id_, kKey, kData, true, true);
Darin Petkovba40dd32011-07-11 20:06:39 -0700269}
270
271TEST_F(ServiceTest, SaveStringDontSave) {
272 MockStore storage;
273 static const char kKey[] = "test-key";
Chris Masone34af2182011-08-22 11:59:36 -0700274 EXPECT_CALL(storage, DeleteKey(storage_id_, kKey))
Darin Petkovba40dd32011-07-11 20:06:39 -0700275 .WillOnce(Return(true));
Chris Masone34af2182011-08-22 11:59:36 -0700276 service_->SaveString(&storage, storage_id_, kKey, "data", false, false);
Darin Petkovba40dd32011-07-11 20:06:39 -0700277}
278
279TEST_F(ServiceTest, SaveStringEmpty) {
280 MockStore storage;
281 static const char kKey[] = "test-key";
Chris Masone34af2182011-08-22 11:59:36 -0700282 EXPECT_CALL(storage, DeleteKey(storage_id_, kKey))
Darin Petkovba40dd32011-07-11 20:06:39 -0700283 .WillOnce(Return(true));
Chris Masone34af2182011-08-22 11:59:36 -0700284 service_->SaveString(&storage, storage_id_, kKey, "", true, true);
Darin Petkovba40dd32011-07-11 20:06:39 -0700285}
286
287TEST_F(ServiceTest, Save) {
288 NiceMock<MockStore> storage;
Chris Masone34af2182011-08-22 11:59:36 -0700289 EXPECT_CALL(storage, SetString(storage_id_, _, _))
Darin Petkovba40dd32011-07-11 20:06:39 -0700290 .Times(AtLeast(1))
291 .WillRepeatedly(Return(true));
Chris Masone34af2182011-08-22 11:59:36 -0700292 EXPECT_CALL(storage, DeleteKey(storage_id_, _))
Darin Petkovba40dd32011-07-11 20:06:39 -0700293 .Times(AtLeast(1))
294 .WillRepeatedly(Return(true));
Chris Masone9d779932011-08-25 16:33:41 -0700295 EXPECT_TRUE(service_->Save(&storage));
Darin Petkovba40dd32011-07-11 20:06:39 -0700296}
297
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800298TEST_F(ServiceTest, Unload) {
299 NiceMock<MockStore> storage;
300 EXPECT_CALL(storage, ContainsGroup(storage_id_)).WillOnce(Return(true));
301 static const string string_value("value");
302 EXPECT_CALL(storage, GetString(storage_id_, _, _))
303 .Times(AtLeast(1))
304 .WillRepeatedly(DoAll(SetArgumentPointee<2>(string_value), Return(true)));
Paul Stewart88769de2012-09-21 13:14:36 -0700305 EXPECT_CALL(storage, GetBool(storage_id_, _, _))
306 .Times(AtLeast(1))
307 .WillRepeatedly(DoAll(SetArgumentPointee<2>(true), Return(true)));
308 EXPECT_FALSE(service_->explicitly_disconnected_);
309 service_->explicitly_disconnected_ = true;
310 EXPECT_FALSE(service_->has_ever_connected_);
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800311 ASSERT_TRUE(service_->Load(&storage));
Paul Stewarte7cce8f2012-09-11 10:56:38 -0700312 // TODO(pstew): Only two string properties in the service are tested as
mukesh agrawalcf24a242012-05-21 16:46:11 -0700313 // a sentinel that properties are being set and reset at the right times.
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800314 // However, since property load/store is essentially a manual process,
315 // it is error prone and should either be exhaustively unit-tested or
316 // a generic framework for registering loaded/stored properties should
317 // be created. crosbug.com/24859
318 EXPECT_EQ(string_value, service_->ui_data_);
Paul Stewarte7cce8f2012-09-11 10:56:38 -0700319 EXPECT_EQ(string_value, service_->guid_);
Paul Stewart88769de2012-09-21 13:14:36 -0700320 EXPECT_FALSE(service_->explicitly_disconnected_);
321 EXPECT_TRUE(service_->has_ever_connected_);
322 service_->explicitly_disconnected_ = true;
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800323 service_->Unload();
324 EXPECT_EQ(string(""), service_->ui_data_);
Paul Stewarte7cce8f2012-09-11 10:56:38 -0700325 EXPECT_EQ(string(""), service_->guid_);
Philipp Neubeckf883a7b2012-09-14 19:52:44 +0200326 EXPECT_FALSE(service_->explicitly_disconnected_);
Paul Stewart88769de2012-09-21 13:14:36 -0700327 EXPECT_FALSE(service_->has_ever_connected_);
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800328}
329
Paul Stewart03dba0b2011-08-22 16:32:45 -0700330TEST_F(ServiceTest, State) {
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400331 EXPECT_EQ(Service::kStateIdle, service_->state());
Paul Stewart03dba0b2011-08-22 16:32:45 -0700332 EXPECT_EQ(Service::kFailureUnknown, service_->failure());
Paul Stewartf2d60912012-07-15 08:37:30 -0700333 const string unknown_error(
334 Service::ConnectFailureToString(Service::kFailureUnknown));
335 EXPECT_EQ(unknown_error, service_->error());
Paul Stewart03dba0b2011-08-22 16:32:45 -0700336
mukesh agrawal00917ce2011-11-22 23:56:55 +0000337 ServiceRefPtr service_ref(service_);
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700338
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400339 EXPECT_CALL(*dynamic_cast<ServiceMockAdaptor *>(service_->adaptor_.get()),
mukesh agrawalcf24a242012-05-21 16:46:11 -0700340 EmitStringChanged(flimflam::kStateProperty, _)).Times(7);
Paul Stewartf2d60912012-07-15 08:37:30 -0700341 EXPECT_CALL(*dynamic_cast<ServiceMockAdaptor *>(service_->adaptor_.get()),
342 EmitStringChanged(flimflam::kErrorProperty, _)).Times(4);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700343 EXPECT_CALL(mock_manager_, UpdateService(service_ref));
344 service_->SetState(Service::kStateConnected);
345 // A second state change shouldn't cause another update
346 service_->SetState(Service::kStateConnected);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700347 EXPECT_EQ(Service::kStateConnected, service_->state());
348 EXPECT_EQ(Service::kFailureUnknown, service_->failure());
mukesh agrawalcf24a242012-05-21 16:46:11 -0700349 EXPECT_TRUE(service_->has_ever_connected_);
mukesh agrawal568b5c62012-02-28 14:44:47 -0800350
Paul Stewart03dba0b2011-08-22 16:32:45 -0700351 EXPECT_CALL(mock_manager_, UpdateService(service_ref));
352 service_->SetState(Service::kStateDisconnected);
353
354 EXPECT_CALL(mock_manager_, UpdateService(service_ref));
355 service_->SetFailure(Service::kFailureOutOfRange);
mukesh agrawal568b5c62012-02-28 14:44:47 -0800356 EXPECT_TRUE(service_->IsFailed());
357 EXPECT_GT(service_->failed_time_, 0);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700358 EXPECT_EQ(Service::kStateFailure, service_->state());
359 EXPECT_EQ(Service::kFailureOutOfRange, service_->failure());
Paul Stewartf2d60912012-07-15 08:37:30 -0700360 const string out_of_range_error(
361 Service::ConnectFailureToString(Service::kFailureOutOfRange));
362 EXPECT_EQ(out_of_range_error, service_->error());
mukesh agrawal568b5c62012-02-28 14:44:47 -0800363
364 EXPECT_CALL(mock_manager_, UpdateService(service_ref));
365 service_->SetState(Service::kStateConnected);
366 EXPECT_FALSE(service_->IsFailed());
367 EXPECT_EQ(service_->failed_time_, 0);
Paul Stewartf2d60912012-07-15 08:37:30 -0700368 EXPECT_EQ(unknown_error, service_->error());
mukesh agrawal568b5c62012-02-28 14:44:47 -0800369
370 EXPECT_CALL(mock_manager_, UpdateService(service_ref));
371 service_->SetFailureSilent(Service::kFailurePinMissing);
372 EXPECT_TRUE(service_->IsFailed());
373 EXPECT_GT(service_->failed_time_, 0);
374 EXPECT_EQ(Service::kStateIdle, service_->state());
375 EXPECT_EQ(Service::kFailurePinMissing, service_->failure());
Paul Stewartf2d60912012-07-15 08:37:30 -0700376 const string pin_missing_error(
377 Service::ConnectFailureToString(Service::kFailurePinMissing));
378 EXPECT_EQ(pin_missing_error, service_->error());
mukesh agrawalcf24a242012-05-21 16:46:11 -0700379
380 // If the Service has a Profile, the profile should be saved when
381 // the service enters kStateConnected. (The case where the service
382 // doesn't have a profile is tested above.)
383 MockProfileRefPtr mock_profile(
384 new MockProfile(control_interface(), &mock_manager_));
385 NiceMock<MockStore> storage;
386 service_->set_profile(mock_profile);
387 service_->has_ever_connected_ = false;
388 EXPECT_CALL(mock_manager_, UpdateService(service_ref));
389 EXPECT_CALL(*mock_profile, GetConstStorage())
390 .WillOnce(Return(&storage));
391 EXPECT_CALL(*mock_profile, UpdateService(service_ref));
392 service_->SetState(Service::kStateConnected);
393 EXPECT_TRUE(service_->has_ever_connected_);
394 service_->set_profile(NULL); // Break reference cycle.
395
396 // Similar to the above, but emulate an emphemeral profile, which
397 // has no storage. We can't update the service in the profile, but
398 // we should not crash.
399 service_->state_ = Service::kStateIdle; // Skips state change logic.
400 service_->set_profile(mock_profile);
401 service_->has_ever_connected_ = false;
402 EXPECT_CALL(mock_manager_, UpdateService(service_ref));
403 EXPECT_CALL(*mock_profile, GetConstStorage()).
404 WillOnce(Return(static_cast<StoreInterface *>(NULL)));
405 service_->SetState(Service::kStateConnected);
406 EXPECT_TRUE(service_->has_ever_connected_);
407 service_->set_profile(NULL); // Break reference cycle.
Paul Stewart03dba0b2011-08-22 16:32:45 -0700408}
409
Darin Petkovb100ae72011-08-24 16:19:45 -0700410TEST_F(ServiceTest, ActivateCellularModem) {
Eric Shienbrood9a245532012-03-07 14:20:39 -0500411 ResultCallback callback =
412 Bind(&ServiceTest::TestCallback, Unretained(this));
413 EXPECT_CALL(*this, TestCallback(_)).Times(0);
Darin Petkovb100ae72011-08-24 16:19:45 -0700414 Error error;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500415 service_->ActivateCellularModem("Carrier", &error, callback);
416 EXPECT_TRUE(error.IsFailure());
Darin Petkovb100ae72011-08-24 16:19:45 -0700417}
418
mukesh agrawal00917ce2011-11-22 23:56:55 +0000419TEST_F(ServiceTest, MakeFavorite) {
420 EXPECT_FALSE(service_->favorite());
421 EXPECT_FALSE(service_->auto_connect());
422
423 service_->MakeFavorite();
424 EXPECT_TRUE(service_->favorite());
425 EXPECT_TRUE(service_->auto_connect());
426}
427
428TEST_F(ServiceTest, ReMakeFavorite) {
429 service_->MakeFavorite();
430 EXPECT_TRUE(service_->favorite());
431 EXPECT_TRUE(service_->auto_connect());
432
433 service_->set_auto_connect(false);
434 service_->MakeFavorite();
435 EXPECT_TRUE(service_->favorite());
436 EXPECT_FALSE(service_->auto_connect());
437}
438
mukesh agrawal76d13882012-01-12 15:23:11 -0800439TEST_F(ServiceTest, IsAutoConnectable) {
mukesh agrawalbf14e942012-03-02 14:36:34 -0800440 const char *reason;
mukesh agrawal76d13882012-01-12 15:23:11 -0800441 service_->set_connectable(true);
mukesh agrawalbf14e942012-03-02 14:36:34 -0800442 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
mukesh agrawal76d13882012-01-12 15:23:11 -0800443
mukesh agrawaladb68482012-01-17 16:31:51 -0800444 // We should not auto-connect to a Service that a user has
445 // deliberately disconnected.
446 Error error;
Christopher Wileyabd3b502012-09-26 13:08:52 -0700447 service_->UserInitiatedDisconnect(&error);
mukesh agrawalbf14e942012-03-02 14:36:34 -0800448 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
449 EXPECT_STREQ(Service::kAutoConnExplicitDisconnect, reason);
mukesh agrawaladb68482012-01-17 16:31:51 -0800450
451 // But if the Service is reloaded, it is eligible for auto-connect
452 // again.
453 NiceMock<MockStore> storage;
454 EXPECT_CALL(storage, ContainsGroup(storage_id_)).WillOnce(Return(true));
455 EXPECT_TRUE(service_->Load(&storage));
mukesh agrawalbf14e942012-03-02 14:36:34 -0800456 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
mukesh agrawaladb68482012-01-17 16:31:51 -0800457
458 // A deliberate Connect should also re-enable auto-connect.
Christopher Wileyabd3b502012-09-26 13:08:52 -0700459 service_->UserInitiatedDisconnect(&error);
mukesh agrawalbf14e942012-03-02 14:36:34 -0800460 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
mukesh agrawaladb68482012-01-17 16:31:51 -0800461 service_->Connect(&error);
mukesh agrawalbf14e942012-03-02 14:36:34 -0800462 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
mukesh agrawaladb68482012-01-17 16:31:51 -0800463
Christopher Wileyabd3b502012-09-26 13:08:52 -0700464 // A non-user initiated Disconnect doesn't change anything.
465 service_->Disconnect(&error);
466 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
467
Christopher Wileya4c61ae2012-10-01 11:04:30 -0700468 // A resume also re-enables auto-connect.
469 service_->UserInitiatedDisconnect(&error);
470 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
471 service_->OnAfterResume();
472 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
mukesh agrawaladb68482012-01-17 16:31:51 -0800473
mukesh agrawal76d13882012-01-12 15:23:11 -0800474 service_->SetState(Service::kStateConnected);
mukesh agrawalbf14e942012-03-02 14:36:34 -0800475 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
476 EXPECT_STREQ(Service::kAutoConnConnected, reason);
mukesh agrawal76d13882012-01-12 15:23:11 -0800477
478 service_->SetState(Service::kStateAssociating);
mukesh agrawalbf14e942012-03-02 14:36:34 -0800479 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
480 EXPECT_STREQ(Service::kAutoConnConnecting, reason);
mukesh agrawal76d13882012-01-12 15:23:11 -0800481}
482
Christopher Wiley0801d192012-09-24 11:57:15 -0700483TEST_F(AllMockServiceTest, AutoConnectWithFailures) {
484 const char *reason;
485 service_->set_connectable(true);
486 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
487
488 // The very first AutoConnect() doesn't trigger any throttling.
489 EXPECT_CALL(dispatcher_, PostDelayedTask(_, _)).Times(0);
490 service_->AutoConnect();
491 Mock::VerifyAndClearExpectations(&dispatcher_);
492 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
493
494 // The second call does trigger some throttling.
495 EXPECT_CALL(dispatcher_, PostDelayedTask(_,
496 Service::kMinAutoConnectCooldownTimeMilliseconds));
497 service_->AutoConnect();
498 Mock::VerifyAndClearExpectations(&dispatcher_);
499 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
500 EXPECT_STREQ(Service::kAutoConnThrottled, reason);
501
502 // Calling AutoConnect() again before the cooldown terminates does not change
503 // the timeout.
504 EXPECT_CALL(dispatcher_, PostDelayedTask(_, _)).Times(0);
505 service_->AutoConnect();
506 Mock::VerifyAndClearExpectations(&dispatcher_);
507 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
508 EXPECT_STREQ(Service::kAutoConnThrottled, reason);
509
510 // Once the timeout expires, we can AutoConnect() again.
511 service_->ReEnableAutoConnectTask();
512 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
513
514 // Timeouts increase exponentially.
515 uint64 next_cooldown_time = service_->auto_connect_cooldown_milliseconds_;
516 EXPECT_EQ(next_cooldown_time,
517 Service::kAutoConnectCooldownBackoffFactor *
518 Service::kMinAutoConnectCooldownTimeMilliseconds);
519 while (next_cooldown_time <=
520 Service::kMaxAutoConnectCooldownTimeMilliseconds) {
521 EXPECT_CALL(dispatcher_, PostDelayedTask(_, next_cooldown_time));
522 service_->AutoConnect();
523 Mock::VerifyAndClearExpectations(&dispatcher_);
524 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
525 EXPECT_STREQ(Service::kAutoConnThrottled, reason);
526 service_->ReEnableAutoConnectTask();
527 next_cooldown_time *= Service::kAutoConnectCooldownBackoffFactor;
528 }
529
530 // Once we hit our cap, future timeouts are the same.
531 for (int32 i = 0; i < 2; i++) {
532 EXPECT_CALL(dispatcher_, PostDelayedTask(_,
533 Service::kMaxAutoConnectCooldownTimeMilliseconds));
534 service_->AutoConnect();
535 Mock::VerifyAndClearExpectations(&dispatcher_);
536 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
537 EXPECT_STREQ(Service::kAutoConnThrottled, reason);
538 service_->ReEnableAutoConnectTask();
539 }
540
541 // Connecting successfully resets our cooldown.
542 service_->SetState(Service::kStateConnected);
543 service_->SetState(Service::kStateIdle);
544 reason = "";
545 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
546 EXPECT_STREQ("", reason);
547 EXPECT_EQ(service_->auto_connect_cooldown_milliseconds_, 0);
548
549 // But future AutoConnects behave as before
550 EXPECT_CALL(dispatcher_, PostDelayedTask(_,
551 Service::kMinAutoConnectCooldownTimeMilliseconds)).Times(1);
552 service_->AutoConnect();
553 service_->AutoConnect();
554 Mock::VerifyAndClearExpectations(&dispatcher_);
555 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
556 EXPECT_STREQ(Service::kAutoConnThrottled, reason);
557
558 // Cooldowns are forgotten if we go through a suspend/resume cycle.
559 service_->OnAfterResume();
560 reason = "";
561 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
562 EXPECT_STREQ("", reason);
563}
564
Paul Stewartcb59fed2012-03-21 21:14:46 -0700565TEST_F(ServiceTest, ConfigureBadProperty) {
566 KeyValueStore args;
567 args.SetString("XXXInvalid", "Value");
568 Error error;
569 service_->Configure(args, &error);
570 EXPECT_FALSE(error.IsSuccess());
571}
572
573TEST_F(ServiceTest, ConfigureBoolProperty) {
574 service_->MakeFavorite();
575 service_->set_auto_connect(false);
576 ASSERT_FALSE(service_->auto_connect());
577 KeyValueStore args;
578 args.SetBool(flimflam::kAutoConnectProperty, true);
579 Error error;
580 service_->Configure(args, &error);
581 EXPECT_TRUE(error.IsSuccess());
582 EXPECT_TRUE(service_->auto_connect());
583}
584
585TEST_F(ServiceTest, ConfigureStringProperty) {
586 const string kEAPManagement0 = "management_zero";
587 const string kEAPManagement1 = "management_one";
588 service_->SetEAPKeyManagement(kEAPManagement0);
589 ASSERT_EQ(kEAPManagement0, service_->GetEAPKeyManagement());
590 KeyValueStore args;
591 args.SetString(flimflam::kEapKeyMgmtProperty, kEAPManagement1);
592 Error error;
593 service_->Configure(args, &error);
594 EXPECT_TRUE(error.IsSuccess());
595 EXPECT_EQ(kEAPManagement1, service_->GetEAPKeyManagement());
596}
597
598TEST_F(ServiceTest, ConfigureIgnoredProperty) {
599 service_->MakeFavorite();
600 service_->set_auto_connect(false);
601 ASSERT_FALSE(service_->auto_connect());
602 KeyValueStore args;
603 args.SetBool(flimflam::kAutoConnectProperty, true);
604 Error error;
605 service_->IgnoreParameterForConfigure(flimflam::kAutoConnectProperty);
606 service_->Configure(args, &error);
607 EXPECT_TRUE(error.IsSuccess());
608 EXPECT_FALSE(service_->auto_connect());
609}
610
Paul Stewart10ccbb32012-04-26 15:59:30 -0700611TEST_F(ServiceTest, IsRemembered) {
612 ServiceConstRefPtr service_ref(service_);
613 service_->set_profile(NULL);
614 EXPECT_CALL(mock_manager_, IsServiceEphemeral(_)).Times(0);
615 EXPECT_FALSE(service_->IsRemembered());
616
617 scoped_refptr<MockProfile> profile(
618 new StrictMock<MockProfile>(control_interface(), manager()));
619 service_->set_profile(profile);
620 EXPECT_CALL(mock_manager_, IsServiceEphemeral(service_ref))
621 .WillOnce(Return(true))
622 .WillOnce(Return(false));
623 EXPECT_FALSE(service_->IsRemembered());
624 EXPECT_TRUE(service_->IsRemembered());
625}
626
Paul Stewartdf3c0a82012-11-09 15:54:33 -0800627TEST_F(ServiceTest, IsDependentOn) {
628 EXPECT_FALSE(service_->IsDependentOn(NULL));
629
630 scoped_ptr<MockDeviceInfo> mock_device_info(
631 new NiceMock<MockDeviceInfo>(control_interface(), dispatcher(), metrics(),
632 &mock_manager_));
633 scoped_refptr<MockConnection> mock_connection0(
634 new NiceMock<MockConnection>(mock_device_info.get()));
635 scoped_refptr<MockConnection> mock_connection1(
636 new NiceMock<MockConnection>(mock_device_info.get()));
637
638 service_->connection_ = mock_connection0;
639 EXPECT_CALL(*mock_connection0.get(), GetLowerConnection())
640 .WillRepeatedly(Return(mock_connection1));
641 EXPECT_FALSE(service_->IsDependentOn(NULL));
642
643 scoped_refptr<ServiceUnderTest> service1 =
644 new ServiceUnderTest(control_interface(),
645 dispatcher(),
646 metrics(),
647 &mock_manager_);
648 EXPECT_FALSE(service_->IsDependentOn(service1));
649
650 service1->connection_ = mock_connection0;
651 EXPECT_FALSE(service_->IsDependentOn(service1));
652
653 service1->connection_ = mock_connection1;
654 EXPECT_TRUE(service_->IsDependentOn(service1));
655
656 service_->connection_ = NULL;
657 service1->connection_ = NULL;
658}
659
Paul Stewartff14b022012-04-24 20:06:23 -0700660TEST_F(ServiceTest, OnPropertyChanged) {
661 scoped_refptr<MockProfile> profile(
662 new StrictMock<MockProfile>(control_interface(), manager()));
663 service_->set_profile(NULL);
664 // Expect no crash.
665 service_->OnPropertyChanged("");
666
667 // Expect no call to Update if the profile has no storage.
668 service_->set_profile(profile);
669 EXPECT_CALL(*profile, UpdateService(_)).Times(0);
670 EXPECT_CALL(*profile, GetConstStorage())
671 .WillOnce(Return(reinterpret_cast<StoreInterface *>(NULL)));
672 service_->OnPropertyChanged("");
673
674 // Expect call to Update if the profile has storage.
675 EXPECT_CALL(*profile, UpdateService(_)).Times(1);
676 NiceMock<MockStore> storage;
677 EXPECT_CALL(*profile, GetConstStorage()).WillOnce(Return(&storage));
678 service_->OnPropertyChanged("");
679}
680
Paul Stewartd215af62012-04-24 23:25:50 -0700681
682TEST_F(ServiceTest, RecheckPortal) {
683 ServiceRefPtr service_ref(service_);
684 service_->state_ = Service::kStateIdle;
685 EXPECT_CALL(mock_manager_, RecheckPortalOnService(_)).Times(0);
686 service_->OnPropertyChanged(flimflam::kCheckPortalProperty);
687
688 service_->state_ = Service::kStatePortal;
689 EXPECT_CALL(mock_manager_, RecheckPortalOnService(service_ref)).Times(1);
690 service_->OnPropertyChanged(flimflam::kCheckPortalProperty);
691
692 service_->state_ = Service::kStateConnected;
693 EXPECT_CALL(mock_manager_, RecheckPortalOnService(service_ref)).Times(1);
694 service_->OnPropertyChanged(flimflam::kProxyConfigProperty);
695
696 service_->state_ = Service::kStateOnline;
697 EXPECT_CALL(mock_manager_, RecheckPortalOnService(service_ref)).Times(1);
698 service_->OnPropertyChanged(flimflam::kCheckPortalProperty);
699
700 service_->state_ = Service::kStatePortal;
701 EXPECT_CALL(mock_manager_, RecheckPortalOnService(_)).Times(0);
702 service_->OnPropertyChanged(flimflam::kEAPKeyIDProperty);
703}
704
705TEST_F(ServiceTest, SetCheckPortal) {
706 ServiceRefPtr service_ref(service_);
707 {
708 Error error;
709 service_->SetCheckPortal("false", &error);
710 EXPECT_TRUE(error.IsSuccess());
711 EXPECT_EQ(Service::kCheckPortalFalse, service_->check_portal_);
712 }
713 {
714 Error error;
715 service_->SetCheckPortal("true", &error);
716 EXPECT_TRUE(error.IsSuccess());
717 EXPECT_EQ(Service::kCheckPortalTrue, service_->check_portal_);
718 }
719 {
720 Error error;
721 service_->SetCheckPortal("auto", &error);
722 EXPECT_TRUE(error.IsSuccess());
723 EXPECT_EQ(Service::kCheckPortalAuto, service_->check_portal_);
724 }
725 {
726 Error error;
727 service_->SetCheckPortal("xxx", &error);
728 EXPECT_FALSE(error.IsSuccess());
729 EXPECT_EQ(Error::kInvalidArguments, error.type());
730 EXPECT_EQ(Service::kCheckPortalAuto, service_->check_portal_);
731 }
732}
733
Darin Petkovb2ba39f2012-06-06 10:33:43 +0200734TEST_F(ServiceTest, SetConnectable) {
735 EXPECT_FALSE(service_->connectable());
736
737 ServiceMockAdaptor *adaptor =
738 dynamic_cast<ServiceMockAdaptor *>(service_->adaptor());
739
740 EXPECT_CALL(*adaptor, EmitBoolChanged(_, _)).Times(0);
741 EXPECT_CALL(mock_manager_, HasService(_)).Times(0);
742 service_->SetConnectable(false);
743 EXPECT_FALSE(service_->connectable());
744
745 EXPECT_CALL(*adaptor, EmitBoolChanged(flimflam::kConnectableProperty, true));
746 EXPECT_CALL(mock_manager_, HasService(_)).WillOnce(Return(false));
747 EXPECT_CALL(mock_manager_, UpdateService(_)).Times(0);
748 service_->SetConnectable(true);
749 EXPECT_TRUE(service_->connectable());
750
751 EXPECT_CALL(*adaptor, EmitBoolChanged(flimflam::kConnectableProperty, false));
752 EXPECT_CALL(mock_manager_, HasService(_)).WillOnce(Return(true));
753 EXPECT_CALL(mock_manager_, UpdateService(_));
754 service_->SetConnectable(false);
755 EXPECT_FALSE(service_->connectable());
756
757 EXPECT_CALL(*adaptor, EmitBoolChanged(flimflam::kConnectableProperty, true));
758 EXPECT_CALL(mock_manager_, HasService(_)).WillOnce(Return(true));
759 EXPECT_CALL(mock_manager_, UpdateService(_));
760 service_->SetConnectable(true);
761 EXPECT_TRUE(service_->connectable());
762}
763
Paul Stewart9f32d192012-01-30 20:37:50 -0800764// Make sure a property is registered as a write only property
765// by reading and comparing all string properties returned on the store.
766// Subtle: We need to convert the test argument back and forth between
767// string and ::DBus::Variant because this is the parameter type that
768// our supeclass (PropertyStoreTest) is declared with.
769class ReadOnlyServicePropertyTest : public ServiceTest {};
770TEST_P(ReadOnlyServicePropertyTest, PropertyWriteOnly) {
Paul Stewart9f32d192012-01-30 20:37:50 -0800771 string property(GetParam().reader().get_string());
Paul Stewart8e7e4592012-04-29 09:47:48 -0700772 PropertyStoreInspector inspector(&service_->store());
Darin Petkov4682aa82012-05-31 16:24:11 +0200773 EXPECT_FALSE(inspector.GetStringProperty(property, NULL));
Paul Stewart9f32d192012-01-30 20:37:50 -0800774}
775
776INSTANTIATE_TEST_CASE_P(
777 ReadOnlyServicePropertyTestInstance,
778 ReadOnlyServicePropertyTest,
779 Values(
780 DBusAdaptor::StringToVariant(flimflam::kEapPrivateKeyPasswordProperty),
781 DBusAdaptor::StringToVariant(flimflam::kEapPasswordProperty)));
782
Paul Stewart10241e32012-04-23 18:15:06 -0700783
784TEST_F(ServiceTest, GetIPConfigRpcIdentifier) {
785 {
786 Error error;
787 EXPECT_EQ("/", service_->GetIPConfigRpcIdentifier(&error));
788 EXPECT_EQ(Error::kNotFound, error.type());
789 }
790
791 scoped_ptr<MockDeviceInfo> mock_device_info(
792 new NiceMock<MockDeviceInfo>(control_interface(), dispatcher(), metrics(),
793 &mock_manager_));
794 scoped_refptr<MockConnection> mock_connection(
795 new NiceMock<MockConnection>(mock_device_info.get()));
796
797 service_->connection_ = mock_connection;
798
799 {
800 Error error;
801 const string empty_string;
802 EXPECT_CALL(*mock_connection, ipconfig_rpc_identifier())
803 .WillOnce(ReturnRef(empty_string));
804 EXPECT_EQ("/", service_->GetIPConfigRpcIdentifier(&error));
805 EXPECT_EQ(Error::kNotFound, error.type());
806 }
807
808 {
809 Error error;
810 const string nonempty_string("/ipconfig/path");
811 EXPECT_CALL(*mock_connection, ipconfig_rpc_identifier())
812 .WillOnce(ReturnRef(nonempty_string));
813 EXPECT_EQ(nonempty_string, service_->GetIPConfigRpcIdentifier(&error));
814 EXPECT_EQ(Error::kSuccess, error.type());
815 }
816
817 // Assure orderly destruction of the Connection before DeviceInfo.
818 service_->connection_ = NULL;
819 mock_connection = NULL;
820 mock_device_info.reset();
821}
822
Paul Stewart81426132012-05-16 10:05:10 -0700823class ServiceWithMockSetEap : public ServiceUnderTest {
824 public:
825 ServiceWithMockSetEap(ControlInterface *control_interface,
826 EventDispatcher *dispatcher,
827 Metrics *metrics,
828 Manager *manager)
829 : ServiceUnderTest(control_interface, dispatcher, metrics, manager),
830 is_8021x_(false) {}
831 MOCK_METHOD1(set_eap, void(const EapCredentials &eap));
832 virtual bool Is8021x() const { return is_8021x_; }
833 void set_is_8021x(bool is_8021x) { is_8021x_ = is_8021x; }
834
835 private:
836 bool is_8021x_;
837};
838
839TEST_F(ServiceTest, SetEAPCredentialsOverRPC) {
840 scoped_refptr<ServiceWithMockSetEap> service(
841 new ServiceWithMockSetEap(control_interface(),
842 dispatcher(),
843 metrics(),
844 &mock_manager_));
845 string eap_credential_properties[] = {
846 flimflam::kEAPCertIDProperty,
847 flimflam::kEAPClientCertProperty,
848 flimflam::kEAPKeyIDProperty,
849 flimflam::kEAPPINProperty,
850 flimflam::kEapCaCertIDProperty,
851 flimflam::kEapIdentityProperty,
852 flimflam::kEapPasswordProperty,
853 flimflam::kEapPrivateKeyProperty
854 };
855 string eap_non_credential_properties[] = {
856 flimflam::kEAPEAPProperty,
857 flimflam::kEapPhase2AuthProperty,
858 flimflam::kEapAnonymousIdentityProperty,
859 flimflam::kEapPrivateKeyPasswordProperty,
Paul Stewart81426132012-05-16 10:05:10 -0700860 flimflam::kEapCaCertNssProperty,
861 flimflam::kEapUseSystemCAsProperty
862 };
863 // While this is not an 802.1x-based service, none of these property
864 // changes should cause a call to set_eap().
865 EXPECT_CALL(*service, set_eap(_)).Times(0);
866 for (size_t i = 0; i < arraysize(eap_credential_properties); ++i)
867 service->OnPropertyChanged(eap_credential_properties[i]);
868 for (size_t i = 0; i < arraysize(eap_non_credential_properties); ++i)
869 service->OnPropertyChanged(eap_non_credential_properties[i]);
Paul Stewartadf79d82012-07-18 16:09:56 -0700870 service->OnPropertyChanged(flimflam::kEapKeyMgmtProperty);
Paul Stewart81426132012-05-16 10:05:10 -0700871
872 service->set_is_8021x(true);
873
874 // When this is an 802.1x-based service, set_eap should be called for
875 // all credential-carrying properties.
876 for (size_t i = 0; i < arraysize(eap_credential_properties); ++i) {
877 EXPECT_CALL(*service, set_eap(_)).Times(1);
878 service->OnPropertyChanged(eap_credential_properties[i]);
879 Mock::VerifyAndClearExpectations(service.get());
880 }
Paul Stewartadf79d82012-07-18 16:09:56 -0700881
882 // The key management property is a special case. While not strictly
883 // a credential, it can change which credentials are used. Therefore it
884 // should also trigger a call to set_eap();
885 EXPECT_CALL(*service, set_eap(_)).Times(1);
886 service->OnPropertyChanged(flimflam::kEapKeyMgmtProperty);
887 Mock::VerifyAndClearExpectations(service.get());
888
Paul Stewart81426132012-05-16 10:05:10 -0700889 EXPECT_CALL(*service, set_eap(_)).Times(0);
890 for (size_t i = 0; i < arraysize(eap_non_credential_properties); ++i)
891 service->OnPropertyChanged(eap_non_credential_properties[i]);
892}
893
Paul Stewartbc6e7392012-05-24 07:07:48 -0700894TEST_F(ServiceTest, Certification) {
895 EXPECT_FALSE(service_->eap_.remote_certification.size());
896
897 ScopedMockLog log;
898 EXPECT_CALL(log, Log(logging::LOG_WARNING, _,
899 HasSubstr("exceeds our maximum"))).Times(2);
900 string kSubject("foo");
901 EXPECT_FALSE(service_->AddEAPCertification(
902 kSubject, Service::kEAPMaxCertificationElements));
903 EXPECT_FALSE(service_->AddEAPCertification(
904 kSubject, Service::kEAPMaxCertificationElements + 1));
905 EXPECT_FALSE(service_->eap_.remote_certification.size());
906 Mock::VerifyAndClearExpectations(&log);
907
908 EXPECT_CALL(log,
909 Log(logging::LOG_INFO, _, HasSubstr("Received certification")))
910 .Times(1);
911 EXPECT_TRUE(service_->AddEAPCertification(
912 kSubject, Service::kEAPMaxCertificationElements - 1));
913 Mock::VerifyAndClearExpectations(&log);
914 EXPECT_EQ(Service::kEAPMaxCertificationElements,
915 service_->eap_.remote_certification.size());
916 for (size_t i = 0; i < Service::kEAPMaxCertificationElements - 1; ++i) {
917 EXPECT_TRUE(service_->eap_.remote_certification[i].empty());
918 }
919 EXPECT_EQ(kSubject, service_->eap_.remote_certification[
920 Service::kEAPMaxCertificationElements - 1]);
921
922 // Re-adding the same name in the same position should not generate a log.
923 EXPECT_CALL(log, Log(_, _, _)).Times(0);
924 EXPECT_TRUE(service_->AddEAPCertification(
925 kSubject, Service::kEAPMaxCertificationElements - 1));
926
927 // Replacing the item should generate a log message.
928 EXPECT_CALL(log,
929 Log(logging::LOG_INFO, _, HasSubstr("Received certification")))
930 .Times(1);
931 EXPECT_TRUE(service_->AddEAPCertification(
932 kSubject + "x", Service::kEAPMaxCertificationElements - 1));
933}
934
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700935} // namespace shill