Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 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/service.h" |
| 6 | |
| 7 | #include <map> |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 11 | #include <base/bind.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 12 | #include <chromeos/dbus/service_constants.h> |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 13 | #include <dbus-c++/dbus.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 14 | #include <gtest/gtest.h> |
| 15 | #include <gmock/gmock.h> |
| 16 | |
| 17 | #include "shill/dbus_adaptor.h" |
| 18 | #include "shill/ethernet_service.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 19 | #include "shill/event_dispatcher.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 20 | #include "shill/manager.h" |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 21 | #include "shill/mock_adaptors.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 22 | #include "shill/mock_control.h" |
Paul Stewart | 10241e3 | 2012-04-23 18:15:06 -0700 | [diff] [blame] | 23 | #include "shill/mock_connection.h" |
| 24 | #include "shill/mock_device_info.h" |
Christopher Wiley | 0801d19 | 2012-09-24 11:57:15 -0700 | [diff] [blame] | 25 | #include "shill/mock_event_dispatcher.h" |
Paul Stewart | bc6e739 | 2012-05-24 07:07:48 -0700 | [diff] [blame] | 26 | #include "shill/mock_log.h" |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 27 | #include "shill/mock_manager.h" |
Paul Stewart | ff14b02 | 2012-04-24 20:06:23 -0700 | [diff] [blame] | 28 | #include "shill/mock_profile.h" |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 29 | #include "shill/mock_store.h" |
Paul Stewart | 8e7e459 | 2012-04-29 09:47:48 -0700 | [diff] [blame] | 30 | #include "shill/property_store_inspector.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 31 | #include "shill/property_store_unittest.h" |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 32 | #include "shill/service_under_test.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 33 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 34 | using base::Bind; |
| 35 | using base::Unretained; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 36 | using std::map; |
| 37 | using std::string; |
| 38 | using std::vector; |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 39 | using testing::_; |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 40 | using testing::AnyNumber; |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 41 | using testing::AtLeast; |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 42 | using testing::DoAll; |
Paul Stewart | bc6e739 | 2012-05-24 07:07:48 -0700 | [diff] [blame] | 43 | using testing::HasSubstr; |
Paul Stewart | 8142613 | 2012-05-16 10:05:10 -0700 | [diff] [blame] | 44 | using testing::Mock; |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 45 | using testing::NiceMock; |
| 46 | using testing::Return; |
Paul Stewart | 10241e3 | 2012-04-23 18:15:06 -0700 | [diff] [blame] | 47 | using testing::ReturnRef; |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 48 | using testing::StrictMock; |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 49 | using testing::SetArgumentPointee; |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 50 | using testing::Test; |
Paul Stewart | 9f32d19 | 2012-01-30 20:37:50 -0800 | [diff] [blame] | 51 | using testing::Values; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 52 | |
| 53 | namespace shill { |
| 54 | |
| 55 | class ServiceTest : public PropertyStoreTest { |
| 56 | public: |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 57 | ServiceTest() |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 58 | : mock_manager_(control_interface(), dispatcher(), metrics(), glib()), |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 59 | service_(new ServiceUnderTest(control_interface(), |
| 60 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 61 | metrics(), |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 62 | &mock_manager_)), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 63 | storage_id_(ServiceUnderTest::kStorageId) { |
| 64 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 65 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 66 | virtual ~ServiceTest() {} |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 67 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 68 | MOCK_METHOD1(TestCallback, void(const Error &error)); |
| 69 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 70 | protected: |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 71 | typedef scoped_refptr<MockProfile> MockProfileRefPtr; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 72 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 73 | MockManager mock_manager_; |
| 74 | scoped_refptr<ServiceUnderTest> service_; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 75 | string storage_id_; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
Christopher Wiley | 0801d19 | 2012-09-24 11:57:15 -0700 | [diff] [blame] | 78 | class 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 Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 97 | TEST_F(ServiceTest, Constructor) { |
| 98 | EXPECT_TRUE(service_->save_credentials_); |
| 99 | EXPECT_EQ(Service::kCheckPortalAuto, service_->check_portal_); |
Eric Shienbrood | cc95c5d | 2012-03-30 15:25:49 -0400 | [diff] [blame] | 100 | EXPECT_EQ(Service::kStateIdle, service_->state()); |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 101 | EXPECT_FALSE(service_->has_ever_connected()); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Darin Petkov | 58f0b6d | 2012-06-12 12:52:30 +0200 | [diff] [blame] | 104 | TEST_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 | |
| 111 | TEST_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 Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 118 | TEST_F(ServiceTest, GetProperties) { |
| 119 | map<string, ::DBus::Variant> props; |
| 120 | Error error(Error::kInvalidProperty, ""); |
| 121 | { |
| 122 | ::DBus::Error dbus_error; |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 123 | string expected("true"); |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 124 | service_->mutable_store()->SetStringProperty(flimflam::kCheckPortalProperty, |
| 125 | expected, |
| 126 | &error); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 127 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 128 | 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 Le | 284fe79 | 2012-01-31 17:53:19 -0800 | [diff] [blame] | 135 | service_->set_favorite(true); |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 136 | service_->mutable_store()->SetBoolProperty(flimflam::kAutoConnectProperty, |
| 137 | expected, |
| 138 | &error); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 139 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 140 | 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 Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 146 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 147 | 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 agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 153 | service_->mutable_store()->SetInt32Property(flimflam::kPriorityProperty, |
| 154 | expected, |
| 155 | &error); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 156 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 157 | ASSERT_FALSE(props.find(flimflam::kPriorityProperty) == props.end()); |
| 158 | EXPECT_EQ(props[flimflam::kPriorityProperty].reader().get_int32(), |
| 159 | expected); |
| 160 | } |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 161 | { |
| 162 | ::DBus::Error dbus_error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 163 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 164 | ASSERT_FALSE(props.find(flimflam::kDeviceProperty) == props.end()); |
Jason Glasgow | acdc11f | 2012-03-30 14:12:22 -0400 | [diff] [blame] | 165 | EXPECT_EQ(props[flimflam::kDeviceProperty].reader().get_path(), |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 166 | string(ServiceUnderTest::kRpcId)); |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 167 | } |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 168 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 169 | |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 170 | TEST_F(ServiceTest, SetProperty) { |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 171 | { |
| 172 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 173 | EXPECT_TRUE(DBusAdaptor::SetProperty(service_->mutable_store(), |
| 174 | flimflam::kSaveCredentialsProperty, |
| 175 | PropertyStoreTest::kBoolV, |
| 176 | &error)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 177 | } |
| 178 | { |
| 179 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 180 | EXPECT_TRUE(DBusAdaptor::SetProperty(service_->mutable_store(), |
| 181 | flimflam::kPriorityProperty, |
| 182 | PropertyStoreTest::kInt32V, |
| 183 | &error)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 184 | } |
| 185 | { |
| 186 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 187 | EXPECT_TRUE(DBusAdaptor::SetProperty(service_->mutable_store(), |
| 188 | flimflam::kEAPEAPProperty, |
| 189 | PropertyStoreTest::kStringV, |
| 190 | &error)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 191 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 192 | // Ensure that an attempt to write a R/O property returns InvalidArgs error. |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 193 | { |
| 194 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 195 | EXPECT_FALSE(DBusAdaptor::SetProperty(service_->mutable_store(), |
| 196 | flimflam::kFavoriteProperty, |
| 197 | PropertyStoreTest::kBoolV, |
| 198 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 199 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 200 | } |
Thieu Le | 284fe79 | 2012-01-31 17:53:19 -0800 | [diff] [blame] | 201 | { |
| 202 | ::DBus::Error error; |
| 203 | service_->set_favorite(true); |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 204 | EXPECT_TRUE(DBusAdaptor::SetProperty(service_->mutable_store(), |
| 205 | flimflam::kAutoConnectProperty, |
| 206 | PropertyStoreTest::kBoolV, |
| 207 | &error)); |
Thieu Le | 284fe79 | 2012-01-31 17:53:19 -0800 | [diff] [blame] | 208 | } |
| 209 | { |
| 210 | ::DBus::Error error; |
| 211 | service_->set_favorite(false); |
Wade Guthrie | 4f28e8b | 2012-04-11 10:52:07 -0700 | [diff] [blame] | 212 | EXPECT_TRUE(DBusAdaptor::SetProperty(service_->mutable_store(), |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 213 | flimflam::kAutoConnectProperty, |
| 214 | PropertyStoreTest::kBoolV, |
| 215 | &error)); |
Thieu Le | 284fe79 | 2012-01-31 17:53:19 -0800 | [diff] [blame] | 216 | } |
Paul Stewart | 0c43833 | 2012-04-11 07:55:27 -0700 | [diff] [blame] | 217 | // 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 Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 238 | TEST_F(ServiceTest, Load) { |
| 239 | NiceMock<MockStore> storage; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 240 | EXPECT_CALL(storage, ContainsGroup(storage_id_)).WillOnce(Return(true)); |
| 241 | EXPECT_CALL(storage, GetString(storage_id_, _, _)) |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 242 | .Times(AtLeast(1)) |
| 243 | .WillRepeatedly(Return(true)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 244 | EXPECT_TRUE(service_->Load(&storage)); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | TEST_F(ServiceTest, LoadFail) { |
| 248 | StrictMock<MockStore> storage; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 249 | EXPECT_CALL(storage, ContainsGroup(storage_id_)).WillOnce(Return(false)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 250 | EXPECT_FALSE(service_->Load(&storage)); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | TEST_F(ServiceTest, SaveString) { |
| 254 | MockStore storage; |
| 255 | static const char kKey[] = "test-key"; |
| 256 | static const char kData[] = "test-data"; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 257 | EXPECT_CALL(storage, SetString(storage_id_, kKey, kData)) |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 258 | .WillOnce(Return(true)); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 259 | service_->SaveString(&storage, storage_id_, kKey, kData, false, true); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | TEST_F(ServiceTest, SaveStringCrypted) { |
| 263 | MockStore storage; |
| 264 | static const char kKey[] = "test-key"; |
| 265 | static const char kData[] = "test-data"; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 266 | EXPECT_CALL(storage, SetCryptedString(storage_id_, kKey, kData)) |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 267 | .WillOnce(Return(true)); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 268 | service_->SaveString(&storage, storage_id_, kKey, kData, true, true); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | TEST_F(ServiceTest, SaveStringDontSave) { |
| 272 | MockStore storage; |
| 273 | static const char kKey[] = "test-key"; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 274 | EXPECT_CALL(storage, DeleteKey(storage_id_, kKey)) |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 275 | .WillOnce(Return(true)); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 276 | service_->SaveString(&storage, storage_id_, kKey, "data", false, false); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | TEST_F(ServiceTest, SaveStringEmpty) { |
| 280 | MockStore storage; |
| 281 | static const char kKey[] = "test-key"; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 282 | EXPECT_CALL(storage, DeleteKey(storage_id_, kKey)) |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 283 | .WillOnce(Return(true)); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 284 | service_->SaveString(&storage, storage_id_, kKey, "", true, true); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | TEST_F(ServiceTest, Save) { |
| 288 | NiceMock<MockStore> storage; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 289 | EXPECT_CALL(storage, SetString(storage_id_, _, _)) |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 290 | .Times(AtLeast(1)) |
| 291 | .WillRepeatedly(Return(true)); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 292 | EXPECT_CALL(storage, DeleteKey(storage_id_, _)) |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 293 | .Times(AtLeast(1)) |
| 294 | .WillRepeatedly(Return(true)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 295 | EXPECT_TRUE(service_->Save(&storage)); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 298 | TEST_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 Stewart | 88769de | 2012-09-21 13:14:36 -0700 | [diff] [blame] | 305 | 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 Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 311 | ASSERT_TRUE(service_->Load(&storage)); |
Paul Stewart | e7cce8f | 2012-09-11 10:56:38 -0700 | [diff] [blame] | 312 | // TODO(pstew): Only two string properties in the service are tested as |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 313 | // a sentinel that properties are being set and reset at the right times. |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 314 | // 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 Stewart | e7cce8f | 2012-09-11 10:56:38 -0700 | [diff] [blame] | 319 | EXPECT_EQ(string_value, service_->guid_); |
Paul Stewart | 88769de | 2012-09-21 13:14:36 -0700 | [diff] [blame] | 320 | EXPECT_FALSE(service_->explicitly_disconnected_); |
| 321 | EXPECT_TRUE(service_->has_ever_connected_); |
| 322 | service_->explicitly_disconnected_ = true; |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 323 | service_->Unload(); |
| 324 | EXPECT_EQ(string(""), service_->ui_data_); |
Paul Stewart | e7cce8f | 2012-09-11 10:56:38 -0700 | [diff] [blame] | 325 | EXPECT_EQ(string(""), service_->guid_); |
Philipp Neubeck | f883a7b | 2012-09-14 19:52:44 +0200 | [diff] [blame] | 326 | EXPECT_FALSE(service_->explicitly_disconnected_); |
Paul Stewart | 88769de | 2012-09-21 13:14:36 -0700 | [diff] [blame] | 327 | EXPECT_FALSE(service_->has_ever_connected_); |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 328 | } |
| 329 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 330 | TEST_F(ServiceTest, State) { |
Eric Shienbrood | cc95c5d | 2012-03-30 15:25:49 -0400 | [diff] [blame] | 331 | EXPECT_EQ(Service::kStateIdle, service_->state()); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 332 | EXPECT_EQ(Service::kFailureUnknown, service_->failure()); |
Paul Stewart | f2d6091 | 2012-07-15 08:37:30 -0700 | [diff] [blame] | 333 | const string unknown_error( |
| 334 | Service::ConnectFailureToString(Service::kFailureUnknown)); |
| 335 | EXPECT_EQ(unknown_error, service_->error()); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 336 | |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 337 | ServiceRefPtr service_ref(service_); |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 338 | |
Eric Shienbrood | cc95c5d | 2012-03-30 15:25:49 -0400 | [diff] [blame] | 339 | EXPECT_CALL(*dynamic_cast<ServiceMockAdaptor *>(service_->adaptor_.get()), |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 340 | EmitStringChanged(flimflam::kStateProperty, _)).Times(7); |
Paul Stewart | f2d6091 | 2012-07-15 08:37:30 -0700 | [diff] [blame] | 341 | EXPECT_CALL(*dynamic_cast<ServiceMockAdaptor *>(service_->adaptor_.get()), |
| 342 | EmitStringChanged(flimflam::kErrorProperty, _)).Times(4); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 343 | 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 Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 347 | EXPECT_EQ(Service::kStateConnected, service_->state()); |
| 348 | EXPECT_EQ(Service::kFailureUnknown, service_->failure()); |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 349 | EXPECT_TRUE(service_->has_ever_connected_); |
mukesh agrawal | 568b5c6 | 2012-02-28 14:44:47 -0800 | [diff] [blame] | 350 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 351 | 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 agrawal | 568b5c6 | 2012-02-28 14:44:47 -0800 | [diff] [blame] | 356 | EXPECT_TRUE(service_->IsFailed()); |
| 357 | EXPECT_GT(service_->failed_time_, 0); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 358 | EXPECT_EQ(Service::kStateFailure, service_->state()); |
| 359 | EXPECT_EQ(Service::kFailureOutOfRange, service_->failure()); |
Paul Stewart | f2d6091 | 2012-07-15 08:37:30 -0700 | [diff] [blame] | 360 | const string out_of_range_error( |
| 361 | Service::ConnectFailureToString(Service::kFailureOutOfRange)); |
| 362 | EXPECT_EQ(out_of_range_error, service_->error()); |
mukesh agrawal | 568b5c6 | 2012-02-28 14:44:47 -0800 | [diff] [blame] | 363 | |
| 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 Stewart | f2d6091 | 2012-07-15 08:37:30 -0700 | [diff] [blame] | 368 | EXPECT_EQ(unknown_error, service_->error()); |
mukesh agrawal | 568b5c6 | 2012-02-28 14:44:47 -0800 | [diff] [blame] | 369 | |
| 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 Stewart | f2d6091 | 2012-07-15 08:37:30 -0700 | [diff] [blame] | 376 | const string pin_missing_error( |
| 377 | Service::ConnectFailureToString(Service::kFailurePinMissing)); |
| 378 | EXPECT_EQ(pin_missing_error, service_->error()); |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 379 | |
| 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 Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Darin Petkov | b100ae7 | 2011-08-24 16:19:45 -0700 | [diff] [blame] | 410 | TEST_F(ServiceTest, ActivateCellularModem) { |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 411 | ResultCallback callback = |
| 412 | Bind(&ServiceTest::TestCallback, Unretained(this)); |
| 413 | EXPECT_CALL(*this, TestCallback(_)).Times(0); |
Darin Petkov | b100ae7 | 2011-08-24 16:19:45 -0700 | [diff] [blame] | 414 | Error error; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 415 | service_->ActivateCellularModem("Carrier", &error, callback); |
| 416 | EXPECT_TRUE(error.IsFailure()); |
Darin Petkov | b100ae7 | 2011-08-24 16:19:45 -0700 | [diff] [blame] | 417 | } |
| 418 | |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 419 | TEST_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 | |
| 428 | TEST_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 agrawal | 76d1388 | 2012-01-12 15:23:11 -0800 | [diff] [blame] | 439 | TEST_F(ServiceTest, IsAutoConnectable) { |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 440 | const char *reason; |
mukesh agrawal | 76d1388 | 2012-01-12 15:23:11 -0800 | [diff] [blame] | 441 | service_->set_connectable(true); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 442 | EXPECT_TRUE(service_->IsAutoConnectable(&reason)); |
mukesh agrawal | 76d1388 | 2012-01-12 15:23:11 -0800 | [diff] [blame] | 443 | |
mukesh agrawal | adb6848 | 2012-01-17 16:31:51 -0800 | [diff] [blame] | 444 | // We should not auto-connect to a Service that a user has |
| 445 | // deliberately disconnected. |
| 446 | Error error; |
Christopher Wiley | abd3b50 | 2012-09-26 13:08:52 -0700 | [diff] [blame] | 447 | service_->UserInitiatedDisconnect(&error); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 448 | EXPECT_FALSE(service_->IsAutoConnectable(&reason)); |
| 449 | EXPECT_STREQ(Service::kAutoConnExplicitDisconnect, reason); |
mukesh agrawal | adb6848 | 2012-01-17 16:31:51 -0800 | [diff] [blame] | 450 | |
| 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 agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 456 | EXPECT_TRUE(service_->IsAutoConnectable(&reason)); |
mukesh agrawal | adb6848 | 2012-01-17 16:31:51 -0800 | [diff] [blame] | 457 | |
| 458 | // A deliberate Connect should also re-enable auto-connect. |
Christopher Wiley | abd3b50 | 2012-09-26 13:08:52 -0700 | [diff] [blame] | 459 | service_->UserInitiatedDisconnect(&error); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 460 | EXPECT_FALSE(service_->IsAutoConnectable(&reason)); |
mukesh agrawal | adb6848 | 2012-01-17 16:31:51 -0800 | [diff] [blame] | 461 | service_->Connect(&error); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 462 | EXPECT_TRUE(service_->IsAutoConnectable(&reason)); |
mukesh agrawal | adb6848 | 2012-01-17 16:31:51 -0800 | [diff] [blame] | 463 | |
Christopher Wiley | abd3b50 | 2012-09-26 13:08:52 -0700 | [diff] [blame] | 464 | // A non-user initiated Disconnect doesn't change anything. |
| 465 | service_->Disconnect(&error); |
| 466 | EXPECT_TRUE(service_->IsAutoConnectable(&reason)); |
| 467 | |
Christopher Wiley | a4c61ae | 2012-10-01 11:04:30 -0700 | [diff] [blame] | 468 | // 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 agrawal | adb6848 | 2012-01-17 16:31:51 -0800 | [diff] [blame] | 473 | |
mukesh agrawal | 76d1388 | 2012-01-12 15:23:11 -0800 | [diff] [blame] | 474 | service_->SetState(Service::kStateConnected); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 475 | EXPECT_FALSE(service_->IsAutoConnectable(&reason)); |
| 476 | EXPECT_STREQ(Service::kAutoConnConnected, reason); |
mukesh agrawal | 76d1388 | 2012-01-12 15:23:11 -0800 | [diff] [blame] | 477 | |
| 478 | service_->SetState(Service::kStateAssociating); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 479 | EXPECT_FALSE(service_->IsAutoConnectable(&reason)); |
| 480 | EXPECT_STREQ(Service::kAutoConnConnecting, reason); |
mukesh agrawal | 76d1388 | 2012-01-12 15:23:11 -0800 | [diff] [blame] | 481 | } |
| 482 | |
Christopher Wiley | 0801d19 | 2012-09-24 11:57:15 -0700 | [diff] [blame] | 483 | TEST_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 Stewart | cb59fed | 2012-03-21 21:14:46 -0700 | [diff] [blame] | 565 | TEST_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 | |
| 573 | TEST_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 | |
| 585 | TEST_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 | |
| 598 | TEST_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 Stewart | 10ccbb3 | 2012-04-26 15:59:30 -0700 | [diff] [blame] | 611 | TEST_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 Stewart | ff14b02 | 2012-04-24 20:06:23 -0700 | [diff] [blame] | 627 | TEST_F(ServiceTest, OnPropertyChanged) { |
| 628 | scoped_refptr<MockProfile> profile( |
| 629 | new StrictMock<MockProfile>(control_interface(), manager())); |
| 630 | service_->set_profile(NULL); |
| 631 | // Expect no crash. |
| 632 | service_->OnPropertyChanged(""); |
| 633 | |
| 634 | // Expect no call to Update if the profile has no storage. |
| 635 | service_->set_profile(profile); |
| 636 | EXPECT_CALL(*profile, UpdateService(_)).Times(0); |
| 637 | EXPECT_CALL(*profile, GetConstStorage()) |
| 638 | .WillOnce(Return(reinterpret_cast<StoreInterface *>(NULL))); |
| 639 | service_->OnPropertyChanged(""); |
| 640 | |
| 641 | // Expect call to Update if the profile has storage. |
| 642 | EXPECT_CALL(*profile, UpdateService(_)).Times(1); |
| 643 | NiceMock<MockStore> storage; |
| 644 | EXPECT_CALL(*profile, GetConstStorage()).WillOnce(Return(&storage)); |
| 645 | service_->OnPropertyChanged(""); |
| 646 | } |
| 647 | |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 648 | |
| 649 | TEST_F(ServiceTest, RecheckPortal) { |
| 650 | ServiceRefPtr service_ref(service_); |
| 651 | service_->state_ = Service::kStateIdle; |
| 652 | EXPECT_CALL(mock_manager_, RecheckPortalOnService(_)).Times(0); |
| 653 | service_->OnPropertyChanged(flimflam::kCheckPortalProperty); |
| 654 | |
| 655 | service_->state_ = Service::kStatePortal; |
| 656 | EXPECT_CALL(mock_manager_, RecheckPortalOnService(service_ref)).Times(1); |
| 657 | service_->OnPropertyChanged(flimflam::kCheckPortalProperty); |
| 658 | |
| 659 | service_->state_ = Service::kStateConnected; |
| 660 | EXPECT_CALL(mock_manager_, RecheckPortalOnService(service_ref)).Times(1); |
| 661 | service_->OnPropertyChanged(flimflam::kProxyConfigProperty); |
| 662 | |
| 663 | service_->state_ = Service::kStateOnline; |
| 664 | EXPECT_CALL(mock_manager_, RecheckPortalOnService(service_ref)).Times(1); |
| 665 | service_->OnPropertyChanged(flimflam::kCheckPortalProperty); |
| 666 | |
| 667 | service_->state_ = Service::kStatePortal; |
| 668 | EXPECT_CALL(mock_manager_, RecheckPortalOnService(_)).Times(0); |
| 669 | service_->OnPropertyChanged(flimflam::kEAPKeyIDProperty); |
| 670 | } |
| 671 | |
| 672 | TEST_F(ServiceTest, SetCheckPortal) { |
| 673 | ServiceRefPtr service_ref(service_); |
| 674 | { |
| 675 | Error error; |
| 676 | service_->SetCheckPortal("false", &error); |
| 677 | EXPECT_TRUE(error.IsSuccess()); |
| 678 | EXPECT_EQ(Service::kCheckPortalFalse, service_->check_portal_); |
| 679 | } |
| 680 | { |
| 681 | Error error; |
| 682 | service_->SetCheckPortal("true", &error); |
| 683 | EXPECT_TRUE(error.IsSuccess()); |
| 684 | EXPECT_EQ(Service::kCheckPortalTrue, service_->check_portal_); |
| 685 | } |
| 686 | { |
| 687 | Error error; |
| 688 | service_->SetCheckPortal("auto", &error); |
| 689 | EXPECT_TRUE(error.IsSuccess()); |
| 690 | EXPECT_EQ(Service::kCheckPortalAuto, service_->check_portal_); |
| 691 | } |
| 692 | { |
| 693 | Error error; |
| 694 | service_->SetCheckPortal("xxx", &error); |
| 695 | EXPECT_FALSE(error.IsSuccess()); |
| 696 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 697 | EXPECT_EQ(Service::kCheckPortalAuto, service_->check_portal_); |
| 698 | } |
| 699 | } |
| 700 | |
Darin Petkov | b2ba39f | 2012-06-06 10:33:43 +0200 | [diff] [blame] | 701 | TEST_F(ServiceTest, SetConnectable) { |
| 702 | EXPECT_FALSE(service_->connectable()); |
| 703 | |
| 704 | ServiceMockAdaptor *adaptor = |
| 705 | dynamic_cast<ServiceMockAdaptor *>(service_->adaptor()); |
| 706 | |
| 707 | EXPECT_CALL(*adaptor, EmitBoolChanged(_, _)).Times(0); |
| 708 | EXPECT_CALL(mock_manager_, HasService(_)).Times(0); |
| 709 | service_->SetConnectable(false); |
| 710 | EXPECT_FALSE(service_->connectable()); |
| 711 | |
| 712 | EXPECT_CALL(*adaptor, EmitBoolChanged(flimflam::kConnectableProperty, true)); |
| 713 | EXPECT_CALL(mock_manager_, HasService(_)).WillOnce(Return(false)); |
| 714 | EXPECT_CALL(mock_manager_, UpdateService(_)).Times(0); |
| 715 | service_->SetConnectable(true); |
| 716 | EXPECT_TRUE(service_->connectable()); |
| 717 | |
| 718 | EXPECT_CALL(*adaptor, EmitBoolChanged(flimflam::kConnectableProperty, false)); |
| 719 | EXPECT_CALL(mock_manager_, HasService(_)).WillOnce(Return(true)); |
| 720 | EXPECT_CALL(mock_manager_, UpdateService(_)); |
| 721 | service_->SetConnectable(false); |
| 722 | EXPECT_FALSE(service_->connectable()); |
| 723 | |
| 724 | EXPECT_CALL(*adaptor, EmitBoolChanged(flimflam::kConnectableProperty, true)); |
| 725 | EXPECT_CALL(mock_manager_, HasService(_)).WillOnce(Return(true)); |
| 726 | EXPECT_CALL(mock_manager_, UpdateService(_)); |
| 727 | service_->SetConnectable(true); |
| 728 | EXPECT_TRUE(service_->connectable()); |
| 729 | } |
| 730 | |
Paul Stewart | 9f32d19 | 2012-01-30 20:37:50 -0800 | [diff] [blame] | 731 | // Make sure a property is registered as a write only property |
| 732 | // by reading and comparing all string properties returned on the store. |
| 733 | // Subtle: We need to convert the test argument back and forth between |
| 734 | // string and ::DBus::Variant because this is the parameter type that |
| 735 | // our supeclass (PropertyStoreTest) is declared with. |
| 736 | class ReadOnlyServicePropertyTest : public ServiceTest {}; |
| 737 | TEST_P(ReadOnlyServicePropertyTest, PropertyWriteOnly) { |
Paul Stewart | 9f32d19 | 2012-01-30 20:37:50 -0800 | [diff] [blame] | 738 | string property(GetParam().reader().get_string()); |
Paul Stewart | 8e7e459 | 2012-04-29 09:47:48 -0700 | [diff] [blame] | 739 | PropertyStoreInspector inspector(&service_->store()); |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 740 | EXPECT_FALSE(inspector.GetStringProperty(property, NULL)); |
Paul Stewart | 9f32d19 | 2012-01-30 20:37:50 -0800 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | INSTANTIATE_TEST_CASE_P( |
| 744 | ReadOnlyServicePropertyTestInstance, |
| 745 | ReadOnlyServicePropertyTest, |
| 746 | Values( |
| 747 | DBusAdaptor::StringToVariant(flimflam::kEapPrivateKeyPasswordProperty), |
| 748 | DBusAdaptor::StringToVariant(flimflam::kEapPasswordProperty))); |
| 749 | |
Paul Stewart | 10241e3 | 2012-04-23 18:15:06 -0700 | [diff] [blame] | 750 | |
| 751 | TEST_F(ServiceTest, GetIPConfigRpcIdentifier) { |
| 752 | { |
| 753 | Error error; |
| 754 | EXPECT_EQ("/", service_->GetIPConfigRpcIdentifier(&error)); |
| 755 | EXPECT_EQ(Error::kNotFound, error.type()); |
| 756 | } |
| 757 | |
| 758 | scoped_ptr<MockDeviceInfo> mock_device_info( |
| 759 | new NiceMock<MockDeviceInfo>(control_interface(), dispatcher(), metrics(), |
| 760 | &mock_manager_)); |
| 761 | scoped_refptr<MockConnection> mock_connection( |
| 762 | new NiceMock<MockConnection>(mock_device_info.get())); |
| 763 | |
| 764 | service_->connection_ = mock_connection; |
| 765 | |
| 766 | { |
| 767 | Error error; |
| 768 | const string empty_string; |
| 769 | EXPECT_CALL(*mock_connection, ipconfig_rpc_identifier()) |
| 770 | .WillOnce(ReturnRef(empty_string)); |
| 771 | EXPECT_EQ("/", service_->GetIPConfigRpcIdentifier(&error)); |
| 772 | EXPECT_EQ(Error::kNotFound, error.type()); |
| 773 | } |
| 774 | |
| 775 | { |
| 776 | Error error; |
| 777 | const string nonempty_string("/ipconfig/path"); |
| 778 | EXPECT_CALL(*mock_connection, ipconfig_rpc_identifier()) |
| 779 | .WillOnce(ReturnRef(nonempty_string)); |
| 780 | EXPECT_EQ(nonempty_string, service_->GetIPConfigRpcIdentifier(&error)); |
| 781 | EXPECT_EQ(Error::kSuccess, error.type()); |
| 782 | } |
| 783 | |
| 784 | // Assure orderly destruction of the Connection before DeviceInfo. |
| 785 | service_->connection_ = NULL; |
| 786 | mock_connection = NULL; |
| 787 | mock_device_info.reset(); |
| 788 | } |
| 789 | |
Paul Stewart | 8142613 | 2012-05-16 10:05:10 -0700 | [diff] [blame] | 790 | class ServiceWithMockSetEap : public ServiceUnderTest { |
| 791 | public: |
| 792 | ServiceWithMockSetEap(ControlInterface *control_interface, |
| 793 | EventDispatcher *dispatcher, |
| 794 | Metrics *metrics, |
| 795 | Manager *manager) |
| 796 | : ServiceUnderTest(control_interface, dispatcher, metrics, manager), |
| 797 | is_8021x_(false) {} |
| 798 | MOCK_METHOD1(set_eap, void(const EapCredentials &eap)); |
| 799 | virtual bool Is8021x() const { return is_8021x_; } |
| 800 | void set_is_8021x(bool is_8021x) { is_8021x_ = is_8021x; } |
| 801 | |
| 802 | private: |
| 803 | bool is_8021x_; |
| 804 | }; |
| 805 | |
| 806 | TEST_F(ServiceTest, SetEAPCredentialsOverRPC) { |
| 807 | scoped_refptr<ServiceWithMockSetEap> service( |
| 808 | new ServiceWithMockSetEap(control_interface(), |
| 809 | dispatcher(), |
| 810 | metrics(), |
| 811 | &mock_manager_)); |
| 812 | string eap_credential_properties[] = { |
| 813 | flimflam::kEAPCertIDProperty, |
| 814 | flimflam::kEAPClientCertProperty, |
| 815 | flimflam::kEAPKeyIDProperty, |
| 816 | flimflam::kEAPPINProperty, |
| 817 | flimflam::kEapCaCertIDProperty, |
| 818 | flimflam::kEapIdentityProperty, |
| 819 | flimflam::kEapPasswordProperty, |
| 820 | flimflam::kEapPrivateKeyProperty |
| 821 | }; |
| 822 | string eap_non_credential_properties[] = { |
| 823 | flimflam::kEAPEAPProperty, |
| 824 | flimflam::kEapPhase2AuthProperty, |
| 825 | flimflam::kEapAnonymousIdentityProperty, |
| 826 | flimflam::kEapPrivateKeyPasswordProperty, |
Paul Stewart | 8142613 | 2012-05-16 10:05:10 -0700 | [diff] [blame] | 827 | flimflam::kEapCaCertNssProperty, |
| 828 | flimflam::kEapUseSystemCAsProperty |
| 829 | }; |
| 830 | // While this is not an 802.1x-based service, none of these property |
| 831 | // changes should cause a call to set_eap(). |
| 832 | EXPECT_CALL(*service, set_eap(_)).Times(0); |
| 833 | for (size_t i = 0; i < arraysize(eap_credential_properties); ++i) |
| 834 | service->OnPropertyChanged(eap_credential_properties[i]); |
| 835 | for (size_t i = 0; i < arraysize(eap_non_credential_properties); ++i) |
| 836 | service->OnPropertyChanged(eap_non_credential_properties[i]); |
Paul Stewart | adf79d8 | 2012-07-18 16:09:56 -0700 | [diff] [blame] | 837 | service->OnPropertyChanged(flimflam::kEapKeyMgmtProperty); |
Paul Stewart | 8142613 | 2012-05-16 10:05:10 -0700 | [diff] [blame] | 838 | |
| 839 | service->set_is_8021x(true); |
| 840 | |
| 841 | // When this is an 802.1x-based service, set_eap should be called for |
| 842 | // all credential-carrying properties. |
| 843 | for (size_t i = 0; i < arraysize(eap_credential_properties); ++i) { |
| 844 | EXPECT_CALL(*service, set_eap(_)).Times(1); |
| 845 | service->OnPropertyChanged(eap_credential_properties[i]); |
| 846 | Mock::VerifyAndClearExpectations(service.get()); |
| 847 | } |
Paul Stewart | adf79d8 | 2012-07-18 16:09:56 -0700 | [diff] [blame] | 848 | |
| 849 | // The key management property is a special case. While not strictly |
| 850 | // a credential, it can change which credentials are used. Therefore it |
| 851 | // should also trigger a call to set_eap(); |
| 852 | EXPECT_CALL(*service, set_eap(_)).Times(1); |
| 853 | service->OnPropertyChanged(flimflam::kEapKeyMgmtProperty); |
| 854 | Mock::VerifyAndClearExpectations(service.get()); |
| 855 | |
Paul Stewart | 8142613 | 2012-05-16 10:05:10 -0700 | [diff] [blame] | 856 | EXPECT_CALL(*service, set_eap(_)).Times(0); |
| 857 | for (size_t i = 0; i < arraysize(eap_non_credential_properties); ++i) |
| 858 | service->OnPropertyChanged(eap_non_credential_properties[i]); |
| 859 | } |
| 860 | |
Paul Stewart | bc6e739 | 2012-05-24 07:07:48 -0700 | [diff] [blame] | 861 | TEST_F(ServiceTest, Certification) { |
| 862 | EXPECT_FALSE(service_->eap_.remote_certification.size()); |
| 863 | |
| 864 | ScopedMockLog log; |
| 865 | EXPECT_CALL(log, Log(logging::LOG_WARNING, _, |
| 866 | HasSubstr("exceeds our maximum"))).Times(2); |
| 867 | string kSubject("foo"); |
| 868 | EXPECT_FALSE(service_->AddEAPCertification( |
| 869 | kSubject, Service::kEAPMaxCertificationElements)); |
| 870 | EXPECT_FALSE(service_->AddEAPCertification( |
| 871 | kSubject, Service::kEAPMaxCertificationElements + 1)); |
| 872 | EXPECT_FALSE(service_->eap_.remote_certification.size()); |
| 873 | Mock::VerifyAndClearExpectations(&log); |
| 874 | |
| 875 | EXPECT_CALL(log, |
| 876 | Log(logging::LOG_INFO, _, HasSubstr("Received certification"))) |
| 877 | .Times(1); |
| 878 | EXPECT_TRUE(service_->AddEAPCertification( |
| 879 | kSubject, Service::kEAPMaxCertificationElements - 1)); |
| 880 | Mock::VerifyAndClearExpectations(&log); |
| 881 | EXPECT_EQ(Service::kEAPMaxCertificationElements, |
| 882 | service_->eap_.remote_certification.size()); |
| 883 | for (size_t i = 0; i < Service::kEAPMaxCertificationElements - 1; ++i) { |
| 884 | EXPECT_TRUE(service_->eap_.remote_certification[i].empty()); |
| 885 | } |
| 886 | EXPECT_EQ(kSubject, service_->eap_.remote_certification[ |
| 887 | Service::kEAPMaxCertificationElements - 1]); |
| 888 | |
| 889 | // Re-adding the same name in the same position should not generate a log. |
| 890 | EXPECT_CALL(log, Log(_, _, _)).Times(0); |
| 891 | EXPECT_TRUE(service_->AddEAPCertification( |
| 892 | kSubject, Service::kEAPMaxCertificationElements - 1)); |
| 893 | |
| 894 | // Replacing the item should generate a log message. |
| 895 | EXPECT_CALL(log, |
| 896 | Log(logging::LOG_INFO, _, HasSubstr("Received certification"))) |
| 897 | .Times(1); |
| 898 | EXPECT_TRUE(service_->AddEAPCertification( |
| 899 | kSubject + "x", Service::kEAPMaxCertificationElements - 1)); |
| 900 | } |
| 901 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 902 | } // namespace shill |