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 | df3c0a8 | 2012-11-09 15:54:33 -0800 | [diff] [blame] | 627 | TEST_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 Stewart | ff14b02 | 2012-04-24 20:06:23 -0700 | [diff] [blame] | 660 | TEST_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 Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 681 | |
| 682 | TEST_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 | |
| 705 | TEST_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 Petkov | b2ba39f | 2012-06-06 10:33:43 +0200 | [diff] [blame] | 734 | TEST_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 Stewart | 9f32d19 | 2012-01-30 20:37:50 -0800 | [diff] [blame] | 764 | // 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. |
| 769 | class ReadOnlyServicePropertyTest : public ServiceTest {}; |
| 770 | TEST_P(ReadOnlyServicePropertyTest, PropertyWriteOnly) { |
Paul Stewart | 9f32d19 | 2012-01-30 20:37:50 -0800 | [diff] [blame] | 771 | string property(GetParam().reader().get_string()); |
Paul Stewart | 8e7e459 | 2012-04-29 09:47:48 -0700 | [diff] [blame] | 772 | PropertyStoreInspector inspector(&service_->store()); |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 773 | EXPECT_FALSE(inspector.GetStringProperty(property, NULL)); |
Paul Stewart | 9f32d19 | 2012-01-30 20:37:50 -0800 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | INSTANTIATE_TEST_CASE_P( |
| 777 | ReadOnlyServicePropertyTestInstance, |
| 778 | ReadOnlyServicePropertyTest, |
| 779 | Values( |
| 780 | DBusAdaptor::StringToVariant(flimflam::kEapPrivateKeyPasswordProperty), |
| 781 | DBusAdaptor::StringToVariant(flimflam::kEapPasswordProperty))); |
| 782 | |
Paul Stewart | 10241e3 | 2012-04-23 18:15:06 -0700 | [diff] [blame] | 783 | |
| 784 | TEST_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 Stewart | 8142613 | 2012-05-16 10:05:10 -0700 | [diff] [blame] | 823 | class 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 | |
| 839 | TEST_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 Stewart | 8142613 | 2012-05-16 10:05:10 -0700 | [diff] [blame] | 860 | 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 Stewart | adf79d8 | 2012-07-18 16:09:56 -0700 | [diff] [blame] | 870 | service->OnPropertyChanged(flimflam::kEapKeyMgmtProperty); |
Paul Stewart | 8142613 | 2012-05-16 10:05:10 -0700 | [diff] [blame] | 871 | |
| 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 Stewart | adf79d8 | 2012-07-18 16:09:56 -0700 | [diff] [blame] | 881 | |
| 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 Stewart | 8142613 | 2012-05-16 10:05:10 -0700 | [diff] [blame] | 889 | 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 Stewart | bc6e739 | 2012-05-24 07:07:48 -0700 | [diff] [blame] | 894 | TEST_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 Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 935 | } // namespace shill |