Arman Uguray | 2717a10 | 2013-01-29 23:36:06 -0800 | [diff] [blame] | 1 | // Copyright (c) 2013 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" |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 25 | #include "shill/mock_diagnostics_reporter.h" |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 26 | #include "shill/mock_eap_credentials.h" |
Christopher Wiley | 0801d19 | 2012-09-24 11:57:15 -0700 | [diff] [blame] | 27 | #include "shill/mock_event_dispatcher.h" |
Paul Stewart | bc6e739 | 2012-05-24 07:07:48 -0700 | [diff] [blame] | 28 | #include "shill/mock_log.h" |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 29 | #include "shill/mock_manager.h" |
Darin Petkov | cb0b566 | 2012-12-13 09:59:44 +0100 | [diff] [blame] | 30 | #include "shill/mock_power_manager.h" |
Paul Stewart | ff14b02 | 2012-04-24 20:06:23 -0700 | [diff] [blame] | 31 | #include "shill/mock_profile.h" |
Darin Petkov | cb0b566 | 2012-12-13 09:59:44 +0100 | [diff] [blame] | 32 | #include "shill/mock_proxy_factory.h" |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 33 | #include "shill/mock_store.h" |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 34 | #include "shill/mock_time.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 35 | #include "shill/property_store_unittest.h" |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 36 | #include "shill/service_property_change_test.h" |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 37 | #include "shill/service_under_test.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 38 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 39 | using base::Bind; |
| 40 | using base::Unretained; |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 41 | using std::deque; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 42 | using std::map; |
| 43 | using std::string; |
| 44 | using std::vector; |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 45 | using testing::_; |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 46 | using testing::AnyNumber; |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 47 | using testing::AtLeast; |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 48 | using testing::DefaultValue; |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 49 | using testing::DoAll; |
Paul Stewart | bc6e739 | 2012-05-24 07:07:48 -0700 | [diff] [blame] | 50 | using testing::HasSubstr; |
Paul Stewart | 8142613 | 2012-05-16 10:05:10 -0700 | [diff] [blame] | 51 | using testing::Mock; |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 52 | using testing::NiceMock; |
| 53 | using testing::Return; |
Paul Stewart | 10241e3 | 2012-04-23 18:15:06 -0700 | [diff] [blame] | 54 | using testing::ReturnRef; |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 55 | using testing::StrictMock; |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 56 | using testing::SetArgumentPointee; |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 57 | using testing::Test; |
Paul Stewart | 9f32d19 | 2012-01-30 20:37:50 -0800 | [diff] [blame] | 58 | using testing::Values; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 59 | |
| 60 | namespace shill { |
| 61 | |
| 62 | class ServiceTest : public PropertyStoreTest { |
| 63 | public: |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 64 | ServiceTest() |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 65 | : mock_manager_(control_interface(), dispatcher(), metrics(), glib()), |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 66 | service_(new ServiceUnderTest(control_interface(), |
| 67 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 68 | metrics(), |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 69 | &mock_manager_)), |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame] | 70 | service2_(new ServiceUnderTest(control_interface(), |
| 71 | dispatcher(), |
| 72 | metrics(), |
| 73 | &mock_manager_)), |
Darin Petkov | cb0b566 | 2012-12-13 09:59:44 +0100 | [diff] [blame] | 74 | storage_id_(ServiceUnderTest::kStorageId), |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 75 | power_manager_(new MockPowerManager(NULL, &proxy_factory_)), |
| 76 | eap_(new MockEapCredentials()) { |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 77 | service_->time_ = &time_; |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 78 | DefaultValue<Timestamp>::Set(Timestamp()); |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 79 | service_->diagnostics_reporter_ = &diagnostics_reporter_; |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 80 | service_->eap_.reset(eap_); // Passes ownership. |
Darin Petkov | cb0b566 | 2012-12-13 09:59:44 +0100 | [diff] [blame] | 81 | mock_manager_.running_ = true; |
| 82 | mock_manager_.set_power_manager(power_manager_); // Passes ownership. |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 83 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 84 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 85 | virtual ~ServiceTest() {} |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 86 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 87 | MOCK_METHOD1(TestCallback, void(const Error &error)); |
| 88 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 89 | protected: |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 90 | typedef scoped_refptr<MockProfile> MockProfileRefPtr; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 91 | |
Darin Petkov | cb0b566 | 2012-12-13 09:59:44 +0100 | [diff] [blame] | 92 | class TestProxyFactory : public ProxyFactory { |
| 93 | public: |
| 94 | TestProxyFactory() {} |
| 95 | |
| 96 | virtual PowerManagerProxyInterface *CreatePowerManagerProxy( |
| 97 | PowerManagerProxyDelegate *delegate) { |
| 98 | return NULL; |
| 99 | } |
| 100 | |
| 101 | private: |
| 102 | DISALLOW_COPY_AND_ASSIGN(TestProxyFactory); |
| 103 | }; |
| 104 | |
Darin Petkov | aba8932 | 2013-03-11 14:48:22 +0100 | [diff] [blame] | 105 | ServiceMockAdaptor *GetAdaptor() { |
| 106 | return dynamic_cast<ServiceMockAdaptor *>(service_->adaptor()); |
| 107 | } |
| 108 | |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 109 | string GetFriendlyName() { return service_->friendly_name(); } |
| 110 | |
Darin Petkov | cb0b566 | 2012-12-13 09:59:44 +0100 | [diff] [blame] | 111 | void SetManagerRunning(bool running) { mock_manager_.running_ = running; } |
| 112 | |
| 113 | void SetPowerState(PowerManager::SuspendState state) { |
| 114 | power_manager_->power_state_ = state; |
| 115 | } |
| 116 | |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 117 | void SetExplicitlyDisconnected(bool explicitly) { |
| 118 | service_->explicitly_disconnected_ = explicitly; |
| 119 | } |
| 120 | |
| 121 | void SetStateField(Service::ConnectState state) { service_->state_ = state; } |
| 122 | |
Darin Petkov | c8d91e5 | 2013-01-21 11:43:47 +0100 | [diff] [blame] | 123 | Service::ConnectState GetPreviousState() const { |
| 124 | return service_->previous_state_; |
| 125 | } |
| 126 | |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 127 | void NoteDisconnectEvent() { |
| 128 | service_->NoteDisconnectEvent(); |
| 129 | } |
| 130 | |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 131 | deque<Timestamp> *GetDisconnects() { |
| 132 | return &service_->disconnects_; |
| 133 | } |
| 134 | deque<Timestamp> *GetMisconnects() { |
| 135 | return &service_->misconnects_; |
| 136 | } |
| 137 | |
| 138 | Timestamp GetTimestamp(int monotonic_seconds, const string &wall_clock) { |
| 139 | struct timeval monotonic = { .tv_sec = monotonic_seconds, .tv_usec = 0 }; |
| 140 | return Timestamp(monotonic, wall_clock); |
| 141 | } |
| 142 | |
| 143 | void PushTimestamp(deque<Timestamp> *timestamps, |
| 144 | int monotonic_seconds, |
| 145 | const string &wall_clock) { |
| 146 | timestamps->push_back(GetTimestamp(monotonic_seconds, wall_clock)); |
| 147 | } |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 148 | |
| 149 | int GetDisconnectsMonitorSeconds() { |
| 150 | return Service::kDisconnectsMonitorSeconds; |
| 151 | } |
| 152 | |
| 153 | int GetMisconnectsMonitorSeconds() { |
| 154 | return Service::kMisconnectsMonitorSeconds; |
| 155 | } |
| 156 | |
| 157 | int GetReportDisconnectsThreshold() { |
| 158 | return Service::kReportDisconnectsThreshold; |
| 159 | } |
| 160 | |
| 161 | int GetReportMisconnectsThreshold() { |
| 162 | return Service::kReportMisconnectsThreshold; |
| 163 | } |
| 164 | |
| 165 | int GetMaxDisconnectEventHistory() { |
| 166 | return Service::kMaxDisconnectEventHistory; |
| 167 | } |
| 168 | |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 169 | static Strings ExtractWallClockToStrings(const deque<Timestamp> ×tamps) { |
| 170 | return Service::ExtractWallClockToStrings(timestamps); |
| 171 | } |
| 172 | |
Darin Petkov | 36d962d | 2013-03-25 13:03:14 +0100 | [diff] [blame] | 173 | bool GetAutoConnect(Error *error) { |
| 174 | return service_->GetAutoConnect(error); |
| 175 | } |
| 176 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 177 | bool SetAutoConnectFull(bool connect, Error *error) { |
| 178 | return service_->SetAutoConnectFull(connect, error); |
Darin Petkov | 36d962d | 2013-03-25 13:03:14 +0100 | [diff] [blame] | 179 | } |
| 180 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 181 | MockManager mock_manager_; |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 182 | MockDiagnosticsReporter diagnostics_reporter_; |
| 183 | MockTime time_; |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 184 | scoped_refptr<ServiceUnderTest> service_; |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame] | 185 | scoped_refptr<ServiceUnderTest> service2_; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 186 | string storage_id_; |
Darin Petkov | cb0b566 | 2012-12-13 09:59:44 +0100 | [diff] [blame] | 187 | TestProxyFactory proxy_factory_; |
| 188 | MockPowerManager *power_manager_; // Owned by |mock_manager_|. |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 189 | MockEapCredentials *eap_; // Owned by |service_|. |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 190 | }; |
| 191 | |
Christopher Wiley | 0801d19 | 2012-09-24 11:57:15 -0700 | [diff] [blame] | 192 | class AllMockServiceTest : public testing::Test { |
| 193 | public: |
| 194 | AllMockServiceTest() |
Thieu Le | 6c1e3bb | 2013-02-06 15:20:35 -0800 | [diff] [blame] | 195 | : metrics_(&dispatcher_), |
| 196 | manager_(&control_interface_, &dispatcher_, &metrics_, &glib_), |
Christopher Wiley | 0801d19 | 2012-09-24 11:57:15 -0700 | [diff] [blame] | 197 | service_(new ServiceUnderTest(&control_interface_, |
| 198 | &dispatcher_, |
| 199 | &metrics_, |
| 200 | &manager_)) { } |
| 201 | virtual ~AllMockServiceTest() {} |
| 202 | |
| 203 | protected: |
| 204 | MockControl control_interface_; |
| 205 | StrictMock<MockEventDispatcher> dispatcher_; |
| 206 | MockGLib glib_; |
| 207 | NiceMock<MockMetrics> metrics_; |
| 208 | MockManager manager_; |
| 209 | scoped_refptr<ServiceUnderTest> service_; |
| 210 | }; |
| 211 | |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 212 | TEST_F(ServiceTest, Constructor) { |
| 213 | EXPECT_TRUE(service_->save_credentials_); |
| 214 | EXPECT_EQ(Service::kCheckPortalAuto, service_->check_portal_); |
Eric Shienbrood | cc95c5d | 2012-03-30 15:25:49 -0400 | [diff] [blame] | 215 | EXPECT_EQ(Service::kStateIdle, service_->state()); |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 216 | EXPECT_FALSE(service_->has_ever_connected()); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Darin Petkov | 58f0b6d | 2012-06-12 12:52:30 +0200 | [diff] [blame] | 219 | TEST_F(ServiceTest, CalculateState) { |
| 220 | service_->state_ = Service::kStateConnected; |
| 221 | Error error; |
| 222 | EXPECT_EQ(flimflam::kStateReady, service_->CalculateState(&error)); |
| 223 | EXPECT_TRUE(error.IsSuccess()); |
| 224 | } |
| 225 | |
| 226 | TEST_F(ServiceTest, CalculateTechnology) { |
| 227 | service_->technology_ = Technology::kWifi; |
| 228 | Error error; |
| 229 | EXPECT_EQ(flimflam::kTypeWifi, service_->CalculateTechnology(&error)); |
| 230 | EXPECT_TRUE(error.IsSuccess()); |
| 231 | } |
| 232 | |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 233 | TEST_F(ServiceTest, GetProperties) { |
| 234 | map<string, ::DBus::Variant> props; |
| 235 | Error error(Error::kInvalidProperty, ""); |
| 236 | { |
| 237 | ::DBus::Error dbus_error; |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 238 | string expected("true"); |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 239 | service_->mutable_store()->SetStringProperty(flimflam::kCheckPortalProperty, |
| 240 | expected, |
| 241 | &error); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 242 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 243 | ASSERT_FALSE(props.find(flimflam::kCheckPortalProperty) == props.end()); |
| 244 | EXPECT_EQ(props[flimflam::kCheckPortalProperty].reader().get_string(), |
| 245 | expected); |
| 246 | } |
| 247 | { |
| 248 | ::DBus::Error dbus_error; |
| 249 | bool expected = true; |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 250 | service_->mutable_store()->SetBoolProperty(flimflam::kAutoConnectProperty, |
| 251 | expected, |
| 252 | &error); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 253 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 254 | ASSERT_FALSE(props.find(flimflam::kAutoConnectProperty) == props.end()); |
| 255 | EXPECT_EQ(props[flimflam::kAutoConnectProperty].reader().get_bool(), |
| 256 | expected); |
| 257 | } |
| 258 | { |
| 259 | ::DBus::Error dbus_error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 260 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 261 | ASSERT_FALSE(props.find(flimflam::kConnectableProperty) == props.end()); |
| 262 | EXPECT_EQ(props[flimflam::kConnectableProperty].reader().get_bool(), false); |
| 263 | } |
| 264 | { |
| 265 | ::DBus::Error dbus_error; |
| 266 | int32 expected = 127; |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 267 | service_->mutable_store()->SetInt32Property(flimflam::kPriorityProperty, |
| 268 | expected, |
| 269 | &error); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 270 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 271 | ASSERT_FALSE(props.find(flimflam::kPriorityProperty) == props.end()); |
| 272 | EXPECT_EQ(props[flimflam::kPriorityProperty].reader().get_int32(), |
| 273 | expected); |
| 274 | } |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 275 | { |
| 276 | ::DBus::Error dbus_error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 277 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 278 | ASSERT_FALSE(props.find(flimflam::kDeviceProperty) == props.end()); |
Jason Glasgow | acdc11f | 2012-03-30 14:12:22 -0400 | [diff] [blame] | 279 | EXPECT_EQ(props[flimflam::kDeviceProperty].reader().get_path(), |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 280 | string(ServiceUnderTest::kRpcId)); |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 281 | } |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 282 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 283 | |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 284 | TEST_F(ServiceTest, SetProperty) { |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 285 | { |
| 286 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 287 | EXPECT_TRUE(DBusAdaptor::SetProperty(service_->mutable_store(), |
| 288 | flimflam::kSaveCredentialsProperty, |
| 289 | PropertyStoreTest::kBoolV, |
| 290 | &error)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 291 | } |
| 292 | { |
| 293 | ::DBus::Error error; |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 294 | ::DBus::Variant priority; |
| 295 | priority.writer().append_int32(1); |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 296 | EXPECT_TRUE(DBusAdaptor::SetProperty(service_->mutable_store(), |
| 297 | flimflam::kPriorityProperty, |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 298 | priority, |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 299 | &error)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 300 | } |
| 301 | { |
| 302 | ::DBus::Error error; |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 303 | ::DBus::Variant guid; |
| 304 | guid.writer().append_string("not default"); |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 305 | EXPECT_TRUE(DBusAdaptor::SetProperty(service_->mutable_store(), |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 306 | flimflam::kGuidProperty, |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 307 | guid, |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 308 | &error)); |
| 309 | } |
| 310 | // Ensure that EAP properties cannot be set on services with no EAP |
| 311 | // credentials. Use service2_ here since we're have some code in |
| 312 | // ServiceTest::SetUp() that fiddles with service_->eap_. |
| 313 | { |
| 314 | ::DBus::Error error; |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 315 | ::DBus::Variant eap; |
| 316 | eap.writer().append_string("eap eep eip!"); |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 317 | EXPECT_FALSE(DBusAdaptor::SetProperty(service2_->mutable_store(), |
| 318 | flimflam::kEAPEAPProperty, |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 319 | eap, |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 320 | &error)); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 321 | ASSERT_TRUE(error.is_set()); // name() may be invalid otherwise |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 322 | EXPECT_EQ(invalid_prop(), error.name()); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 323 | // Now plumb in eap credentials, and try again. |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 324 | service2_->SetEapCredentials(new EapCredentials()); |
| 325 | EXPECT_TRUE(DBusAdaptor::SetProperty(service2_->mutable_store(), |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 326 | flimflam::kEAPEAPProperty, |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 327 | eap, |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 328 | &error)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 329 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 330 | // 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] | 331 | { |
| 332 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 333 | EXPECT_FALSE(DBusAdaptor::SetProperty(service_->mutable_store(), |
| 334 | flimflam::kFavoriteProperty, |
| 335 | PropertyStoreTest::kBoolV, |
| 336 | &error)); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 337 | ASSERT_TRUE(error.is_set()); // name() may be invalid otherwise |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 338 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 339 | } |
Thieu Le | 284fe79 | 2012-01-31 17:53:19 -0800 | [diff] [blame] | 340 | { |
| 341 | ::DBus::Error error; |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 342 | ::DBus::Variant auto_connect; |
| 343 | auto_connect.writer().append_bool(true); |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 344 | EXPECT_TRUE(DBusAdaptor::SetProperty(service_->mutable_store(), |
| 345 | flimflam::kAutoConnectProperty, |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 346 | auto_connect, |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 347 | &error)); |
Thieu Le | 284fe79 | 2012-01-31 17:53:19 -0800 | [diff] [blame] | 348 | } |
Paul Stewart | 0c43833 | 2012-04-11 07:55:27 -0700 | [diff] [blame] | 349 | // Ensure that we can perform a trivial set of the Name property (to its |
| 350 | // current value) but an attempt to set the property to a different value |
| 351 | // fails. |
| 352 | { |
| 353 | ::DBus::Error error; |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 354 | EXPECT_FALSE(DBusAdaptor::SetProperty(service_->mutable_store(), |
| 355 | flimflam::kNameProperty, |
| 356 | DBusAdaptor::StringToVariant( |
| 357 | GetFriendlyName()), |
| 358 | &error)); |
| 359 | EXPECT_FALSE(error.is_set()); |
Paul Stewart | 0c43833 | 2012-04-11 07:55:27 -0700 | [diff] [blame] | 360 | } |
| 361 | { |
| 362 | ::DBus::Error error; |
| 363 | EXPECT_FALSE(DBusAdaptor::SetProperty(service_->mutable_store(), |
| 364 | flimflam::kNameProperty, |
| 365 | PropertyStoreTest::kStringV, |
| 366 | &error)); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 367 | ASSERT_TRUE(error.is_set()); // name() may be invalid otherwise |
Paul Stewart | 0c43833 | 2012-04-11 07:55:27 -0700 | [diff] [blame] | 368 | EXPECT_EQ(invalid_args(), error.name()); |
| 369 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Paul Stewart | e7de294 | 2013-04-25 17:07:31 -0700 | [diff] [blame] | 372 | TEST_F(ServiceTest, GetLoadableStorageIdentifier) { |
| 373 | NiceMock<MockStore> storage; |
| 374 | EXPECT_CALL(storage, ContainsGroup(storage_id_)) |
| 375 | .WillOnce(Return(false)) |
| 376 | .WillOnce(Return(true)); |
| 377 | EXPECT_EQ("", service_->GetLoadableStorageIdentifier(storage)); |
| 378 | EXPECT_EQ(storage_id_, service_->GetLoadableStorageIdentifier(storage)); |
| 379 | } |
| 380 | |
| 381 | TEST_F(ServiceTest, IsLoadableFrom) { |
| 382 | NiceMock<MockStore> storage; |
| 383 | EXPECT_CALL(storage, ContainsGroup(storage_id_)) |
| 384 | .WillOnce(Return(false)) |
| 385 | .WillOnce(Return(true)); |
| 386 | EXPECT_FALSE(service_->IsLoadableFrom(storage)); |
| 387 | EXPECT_TRUE(service_->IsLoadableFrom(storage)); |
| 388 | } |
| 389 | |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 390 | TEST_F(ServiceTest, Load) { |
| 391 | NiceMock<MockStore> storage; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 392 | EXPECT_CALL(storage, ContainsGroup(storage_id_)).WillOnce(Return(true)); |
Paul Stewart | c3dbff1 | 2013-07-17 10:32:48 -0700 | [diff] [blame] | 393 | const string kCheckPortal("check-portal"); |
| 394 | const string kGUID("guid"); |
| 395 | const int kPriority = 20; |
| 396 | const string kProxyConfig("proxy-config"); |
| 397 | const string kUIData("ui-data"); |
| 398 | EXPECT_CALL(storage, GetString(storage_id_, _, _)).Times(AnyNumber()); |
| 399 | EXPECT_CALL(storage, GetInt(storage_id_, _, _)).Times(AnyNumber()); |
| 400 | EXPECT_CALL(storage, GetString(storage_id_, Service::kStorageCheckPortal, _)) |
| 401 | .WillRepeatedly(DoAll(SetArgumentPointee<2>(kCheckPortal), Return(true))); |
| 402 | EXPECT_CALL(storage, GetString(storage_id_, Service::kStorageGUID, _)) |
| 403 | .WillRepeatedly(DoAll(SetArgumentPointee<2>(kGUID), Return(true))); |
| 404 | EXPECT_CALL(storage, GetInt(storage_id_, Service::kStoragePriority, _)) |
| 405 | .WillRepeatedly(DoAll(SetArgumentPointee<2>(kPriority), Return(true))); |
| 406 | EXPECT_CALL(storage, GetString(storage_id_, Service::kStorageProxyConfig, _)) |
| 407 | .WillRepeatedly(DoAll(SetArgumentPointee<2>(kProxyConfig), Return(true))); |
| 408 | EXPECT_CALL(storage, GetString(storage_id_, Service::kStorageUIData, _)) |
| 409 | .WillRepeatedly(DoAll(SetArgumentPointee<2>(kUIData), Return(true))); |
Darin Petkov | 0debec0 | 2013-01-22 10:40:05 +0100 | [diff] [blame] | 410 | EXPECT_CALL(storage, GetBool(storage_id_, _, _)).Times(AnyNumber()); |
| 411 | EXPECT_CALL(storage, |
| 412 | GetBool(storage_id_, Service::kStorageSaveCredentials, _)); |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 413 | EXPECT_CALL(*eap_, Load(&storage, storage_id_)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 414 | EXPECT_TRUE(service_->Load(&storage)); |
Paul Stewart | c3dbff1 | 2013-07-17 10:32:48 -0700 | [diff] [blame] | 415 | |
| 416 | EXPECT_EQ(kCheckPortal, service_->check_portal_); |
| 417 | EXPECT_EQ(kGUID, service_->guid_); |
| 418 | EXPECT_EQ(kProxyConfig, service_->proxy_config_); |
| 419 | EXPECT_EQ(kUIData, service_->ui_data_); |
| 420 | |
| 421 | Mock::VerifyAndClearExpectations(&storage); |
| 422 | Mock::VerifyAndClearExpectations(eap_); |
| 423 | |
| 424 | // Assure that parameters are set to default if not available in the profile. |
| 425 | EXPECT_CALL(storage, ContainsGroup(storage_id_)).WillOnce(Return(true)); |
| 426 | EXPECT_CALL(storage, GetBool(storage_id_, _, _)) |
| 427 | .WillRepeatedly(Return(false)); |
| 428 | EXPECT_CALL(storage, GetString(storage_id_, _, _)) |
| 429 | .WillRepeatedly(Return(false)); |
| 430 | EXPECT_CALL(storage, GetInt(storage_id_, _, _)) |
| 431 | .WillRepeatedly(Return(false)); |
| 432 | EXPECT_CALL(*eap_, Load(&storage, storage_id_)); |
| 433 | EXPECT_TRUE(service_->Load(&storage)); |
| 434 | |
| 435 | EXPECT_EQ(Service::kCheckPortalAuto, service_->check_portal_); |
| 436 | EXPECT_EQ("", service_->guid_); |
| 437 | EXPECT_EQ("", service_->proxy_config_); |
| 438 | EXPECT_EQ("", service_->ui_data_); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | TEST_F(ServiceTest, LoadFail) { |
| 442 | StrictMock<MockStore> storage; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 443 | EXPECT_CALL(storage, ContainsGroup(storage_id_)).WillOnce(Return(false)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 444 | EXPECT_FALSE(service_->Load(&storage)); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | TEST_F(ServiceTest, SaveString) { |
| 448 | MockStore storage; |
| 449 | static const char kKey[] = "test-key"; |
| 450 | static const char kData[] = "test-data"; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 451 | EXPECT_CALL(storage, SetString(storage_id_, kKey, kData)) |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 452 | .WillOnce(Return(true)); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 453 | service_->SaveString(&storage, storage_id_, kKey, kData, false, true); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | TEST_F(ServiceTest, SaveStringCrypted) { |
| 457 | MockStore storage; |
| 458 | static const char kKey[] = "test-key"; |
| 459 | static const char kData[] = "test-data"; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 460 | EXPECT_CALL(storage, SetCryptedString(storage_id_, kKey, kData)) |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 461 | .WillOnce(Return(true)); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 462 | service_->SaveString(&storage, storage_id_, kKey, kData, true, true); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | TEST_F(ServiceTest, SaveStringDontSave) { |
| 466 | MockStore storage; |
| 467 | static const char kKey[] = "test-key"; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 468 | EXPECT_CALL(storage, DeleteKey(storage_id_, kKey)) |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 469 | .WillOnce(Return(true)); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 470 | service_->SaveString(&storage, storage_id_, kKey, "data", false, false); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | TEST_F(ServiceTest, SaveStringEmpty) { |
| 474 | MockStore storage; |
| 475 | static const char kKey[] = "test-key"; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 476 | EXPECT_CALL(storage, DeleteKey(storage_id_, kKey)) |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 477 | .WillOnce(Return(true)); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 478 | service_->SaveString(&storage, storage_id_, kKey, "", true, true); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | TEST_F(ServiceTest, Save) { |
| 482 | NiceMock<MockStore> storage; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 483 | EXPECT_CALL(storage, SetString(storage_id_, _, _)) |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 484 | .Times(AtLeast(1)) |
| 485 | .WillRepeatedly(Return(true)); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 486 | EXPECT_CALL(storage, DeleteKey(storage_id_, _)) |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 487 | .Times(AtLeast(1)) |
| 488 | .WillRepeatedly(Return(true)); |
Darin Petkov | 0debec0 | 2013-01-22 10:40:05 +0100 | [diff] [blame] | 489 | EXPECT_CALL(storage, SetBool(storage_id_, _, _)).Times(AnyNumber()); |
| 490 | EXPECT_CALL(storage, |
| 491 | SetBool(storage_id_, |
| 492 | Service::kStorageSaveCredentials, |
| 493 | service_->save_credentials())); |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 494 | EXPECT_CALL(*eap_, Save(&storage, storage_id_, true)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 495 | EXPECT_TRUE(service_->Save(&storage)); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 498 | TEST_F(ServiceTest, Unload) { |
| 499 | NiceMock<MockStore> storage; |
| 500 | EXPECT_CALL(storage, ContainsGroup(storage_id_)).WillOnce(Return(true)); |
| 501 | static const string string_value("value"); |
| 502 | EXPECT_CALL(storage, GetString(storage_id_, _, _)) |
| 503 | .Times(AtLeast(1)) |
| 504 | .WillRepeatedly(DoAll(SetArgumentPointee<2>(string_value), Return(true))); |
Paul Stewart | 88769de | 2012-09-21 13:14:36 -0700 | [diff] [blame] | 505 | EXPECT_CALL(storage, GetBool(storage_id_, _, _)) |
| 506 | .Times(AtLeast(1)) |
| 507 | .WillRepeatedly(DoAll(SetArgumentPointee<2>(true), Return(true))); |
| 508 | EXPECT_FALSE(service_->explicitly_disconnected_); |
| 509 | service_->explicitly_disconnected_ = true; |
| 510 | EXPECT_FALSE(service_->has_ever_connected_); |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 511 | EXPECT_CALL(*eap_, Load(&storage, storage_id_)); |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 512 | ASSERT_TRUE(service_->Load(&storage)); |
Paul Stewart | e7cce8f | 2012-09-11 10:56:38 -0700 | [diff] [blame] | 513 | // TODO(pstew): Only two string properties in the service are tested as |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 514 | // 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] | 515 | // However, since property load/store is essentially a manual process, |
| 516 | // it is error prone and should either be exhaustively unit-tested or |
| 517 | // a generic framework for registering loaded/stored properties should |
Paul Stewart | ee6b3d7 | 2013-07-12 16:07:51 -0700 | [diff] [blame] | 518 | // be created. crbug.com/207798 |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 519 | EXPECT_EQ(string_value, service_->ui_data_); |
Paul Stewart | e7cce8f | 2012-09-11 10:56:38 -0700 | [diff] [blame] | 520 | EXPECT_EQ(string_value, service_->guid_); |
Paul Stewart | 88769de | 2012-09-21 13:14:36 -0700 | [diff] [blame] | 521 | EXPECT_FALSE(service_->explicitly_disconnected_); |
| 522 | EXPECT_TRUE(service_->has_ever_connected_); |
| 523 | service_->explicitly_disconnected_ = true; |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 524 | EXPECT_CALL(*eap_, Reset()); |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 525 | service_->Unload(); |
| 526 | EXPECT_EQ(string(""), service_->ui_data_); |
Paul Stewart | e7cce8f | 2012-09-11 10:56:38 -0700 | [diff] [blame] | 527 | EXPECT_EQ(string(""), service_->guid_); |
Philipp Neubeck | f883a7b | 2012-09-14 19:52:44 +0200 | [diff] [blame] | 528 | EXPECT_FALSE(service_->explicitly_disconnected_); |
Paul Stewart | 88769de | 2012-09-21 13:14:36 -0700 | [diff] [blame] | 529 | EXPECT_FALSE(service_->has_ever_connected_); |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 530 | } |
| 531 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 532 | TEST_F(ServiceTest, State) { |
Eric Shienbrood | cc95c5d | 2012-03-30 15:25:49 -0400 | [diff] [blame] | 533 | EXPECT_EQ(Service::kStateIdle, service_->state()); |
Darin Petkov | c8d91e5 | 2013-01-21 11:43:47 +0100 | [diff] [blame] | 534 | EXPECT_EQ(Service::kStateIdle, GetPreviousState()); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 535 | EXPECT_EQ(Service::kFailureUnknown, service_->failure()); |
Paul Stewart | f2d6091 | 2012-07-15 08:37:30 -0700 | [diff] [blame] | 536 | const string unknown_error( |
| 537 | Service::ConnectFailureToString(Service::kFailureUnknown)); |
| 538 | EXPECT_EQ(unknown_error, service_->error()); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 539 | |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 540 | ServiceRefPtr service_ref(service_); |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 541 | |
Darin Petkov | aba8932 | 2013-03-11 14:48:22 +0100 | [diff] [blame] | 542 | EXPECT_CALL(*GetAdaptor(), |
Wade Guthrie | cb09435 | 2013-05-08 15:24:59 -0700 | [diff] [blame] | 543 | EmitStringChanged(flimflam::kStateProperty, _)).Times(6); |
Darin Petkov | aba8932 | 2013-03-11 14:48:22 +0100 | [diff] [blame] | 544 | EXPECT_CALL(*GetAdaptor(), |
Paul Stewart | f2d6091 | 2012-07-15 08:37:30 -0700 | [diff] [blame] | 545 | EmitStringChanged(flimflam::kErrorProperty, _)).Times(4); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 546 | EXPECT_CALL(mock_manager_, UpdateService(service_ref)); |
| 547 | service_->SetState(Service::kStateConnected); |
Darin Petkov | c8d91e5 | 2013-01-21 11:43:47 +0100 | [diff] [blame] | 548 | EXPECT_EQ(Service::kStateIdle, GetPreviousState()); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 549 | // A second state change shouldn't cause another update |
| 550 | service_->SetState(Service::kStateConnected); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 551 | EXPECT_EQ(Service::kStateConnected, service_->state()); |
Darin Petkov | c8d91e5 | 2013-01-21 11:43:47 +0100 | [diff] [blame] | 552 | EXPECT_EQ(Service::kStateIdle, GetPreviousState()); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 553 | EXPECT_EQ(Service::kFailureUnknown, service_->failure()); |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 554 | EXPECT_TRUE(service_->has_ever_connected_); |
mukesh agrawal | 568b5c6 | 2012-02-28 14:44:47 -0800 | [diff] [blame] | 555 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 556 | EXPECT_CALL(mock_manager_, UpdateService(service_ref)); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 557 | service_->SetFailure(Service::kFailureOutOfRange); |
mukesh agrawal | 568b5c6 | 2012-02-28 14:44:47 -0800 | [diff] [blame] | 558 | EXPECT_TRUE(service_->IsFailed()); |
| 559 | EXPECT_GT(service_->failed_time_, 0); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 560 | EXPECT_EQ(Service::kStateFailure, service_->state()); |
| 561 | EXPECT_EQ(Service::kFailureOutOfRange, service_->failure()); |
Paul Stewart | f2d6091 | 2012-07-15 08:37:30 -0700 | [diff] [blame] | 562 | const string out_of_range_error( |
| 563 | Service::ConnectFailureToString(Service::kFailureOutOfRange)); |
| 564 | EXPECT_EQ(out_of_range_error, service_->error()); |
mukesh agrawal | 568b5c6 | 2012-02-28 14:44:47 -0800 | [diff] [blame] | 565 | |
| 566 | EXPECT_CALL(mock_manager_, UpdateService(service_ref)); |
| 567 | service_->SetState(Service::kStateConnected); |
| 568 | EXPECT_FALSE(service_->IsFailed()); |
| 569 | EXPECT_EQ(service_->failed_time_, 0); |
Paul Stewart | f2d6091 | 2012-07-15 08:37:30 -0700 | [diff] [blame] | 570 | EXPECT_EQ(unknown_error, service_->error()); |
mukesh agrawal | 568b5c6 | 2012-02-28 14:44:47 -0800 | [diff] [blame] | 571 | |
| 572 | EXPECT_CALL(mock_manager_, UpdateService(service_ref)); |
| 573 | service_->SetFailureSilent(Service::kFailurePinMissing); |
| 574 | EXPECT_TRUE(service_->IsFailed()); |
| 575 | EXPECT_GT(service_->failed_time_, 0); |
| 576 | EXPECT_EQ(Service::kStateIdle, service_->state()); |
| 577 | EXPECT_EQ(Service::kFailurePinMissing, service_->failure()); |
Paul Stewart | f2d6091 | 2012-07-15 08:37:30 -0700 | [diff] [blame] | 578 | const string pin_missing_error( |
| 579 | Service::ConnectFailureToString(Service::kFailurePinMissing)); |
| 580 | EXPECT_EQ(pin_missing_error, service_->error()); |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 581 | |
| 582 | // If the Service has a Profile, the profile should be saved when |
| 583 | // the service enters kStateConnected. (The case where the service |
| 584 | // doesn't have a profile is tested above.) |
| 585 | MockProfileRefPtr mock_profile( |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 586 | new MockProfile(control_interface(), metrics(), &mock_manager_)); |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 587 | NiceMock<MockStore> storage; |
| 588 | service_->set_profile(mock_profile); |
| 589 | service_->has_ever_connected_ = false; |
| 590 | EXPECT_CALL(mock_manager_, UpdateService(service_ref)); |
| 591 | EXPECT_CALL(*mock_profile, GetConstStorage()) |
| 592 | .WillOnce(Return(&storage)); |
| 593 | EXPECT_CALL(*mock_profile, UpdateService(service_ref)); |
| 594 | service_->SetState(Service::kStateConnected); |
| 595 | EXPECT_TRUE(service_->has_ever_connected_); |
| 596 | service_->set_profile(NULL); // Break reference cycle. |
| 597 | |
| 598 | // Similar to the above, but emulate an emphemeral profile, which |
| 599 | // has no storage. We can't update the service in the profile, but |
| 600 | // we should not crash. |
| 601 | service_->state_ = Service::kStateIdle; // Skips state change logic. |
| 602 | service_->set_profile(mock_profile); |
| 603 | service_->has_ever_connected_ = false; |
| 604 | EXPECT_CALL(mock_manager_, UpdateService(service_ref)); |
| 605 | EXPECT_CALL(*mock_profile, GetConstStorage()). |
| 606 | WillOnce(Return(static_cast<StoreInterface *>(NULL))); |
| 607 | service_->SetState(Service::kStateConnected); |
| 608 | EXPECT_TRUE(service_->has_ever_connected_); |
| 609 | service_->set_profile(NULL); // Break reference cycle. |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 610 | } |
| 611 | |
Thieu Le | af47141 | 2013-06-27 14:12:37 -0700 | [diff] [blame] | 612 | TEST_F(ServiceTest, StateResetAfterFailure) { |
| 613 | service_->SetFailure(Service::kFailureOutOfRange); |
| 614 | EXPECT_EQ(Service::kStateFailure, service_->state()); |
| 615 | Error error; |
| 616 | service_->Connect(&error, "in test"); |
| 617 | EXPECT_EQ(Service::kStateIdle, service_->state()); |
| 618 | EXPECT_EQ(Service::kFailureUnknown, service_->failure()); |
| 619 | |
| 620 | service_->SetState(Service::kStateConnected); |
| 621 | service_->Connect(&error, "in test"); |
| 622 | EXPECT_EQ(Service::kStateConnected, service_->state()); |
| 623 | } |
| 624 | |
Darin Petkov | b100ae7 | 2011-08-24 16:19:45 -0700 | [diff] [blame] | 625 | TEST_F(ServiceTest, ActivateCellularModem) { |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 626 | ResultCallback callback = |
| 627 | Bind(&ServiceTest::TestCallback, Unretained(this)); |
| 628 | EXPECT_CALL(*this, TestCallback(_)).Times(0); |
Darin Petkov | b100ae7 | 2011-08-24 16:19:45 -0700 | [diff] [blame] | 629 | Error error; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 630 | service_->ActivateCellularModem("Carrier", &error, callback); |
| 631 | EXPECT_TRUE(error.IsFailure()); |
Darin Petkov | b100ae7 | 2011-08-24 16:19:45 -0700 | [diff] [blame] | 632 | } |
| 633 | |
Ben Chan | 5d92454 | 2013-02-14 17:49:08 -0800 | [diff] [blame] | 634 | TEST_F(ServiceTest, CompleteCellularActivation) { |
| 635 | Error error; |
| 636 | service_->CompleteCellularActivation(&error); |
| 637 | EXPECT_EQ(Error::kNotSupported, error.type()); |
| 638 | } |
| 639 | |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 640 | TEST_F(ServiceTest, MakeFavorite) { |
| 641 | EXPECT_FALSE(service_->favorite()); |
| 642 | EXPECT_FALSE(service_->auto_connect()); |
| 643 | |
| 644 | service_->MakeFavorite(); |
| 645 | EXPECT_TRUE(service_->favorite()); |
| 646 | EXPECT_TRUE(service_->auto_connect()); |
| 647 | } |
| 648 | |
| 649 | TEST_F(ServiceTest, ReMakeFavorite) { |
| 650 | service_->MakeFavorite(); |
| 651 | EXPECT_TRUE(service_->favorite()); |
| 652 | EXPECT_TRUE(service_->auto_connect()); |
| 653 | |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 654 | service_->SetAutoConnect(false); |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 655 | service_->MakeFavorite(); |
| 656 | EXPECT_TRUE(service_->favorite()); |
| 657 | EXPECT_FALSE(service_->auto_connect()); |
| 658 | } |
| 659 | |
mukesh agrawal | 76d1388 | 2012-01-12 15:23:11 -0800 | [diff] [blame] | 660 | TEST_F(ServiceTest, IsAutoConnectable) { |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 661 | const char *reason = NULL; |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 662 | service_->SetConnectable(true); |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 663 | |
| 664 | // Services with non-primary connectivity technologies should not auto-connect |
| 665 | // when the system is offline. |
| 666 | EXPECT_EQ(Technology::kUnknown, service_->technology()); |
| 667 | EXPECT_CALL(mock_manager_, IsOnline()).WillOnce(Return(false)); |
| 668 | EXPECT_FALSE(service_->IsAutoConnectable(&reason)); |
| 669 | EXPECT_STREQ(Service::kAutoConnOffline, reason); |
| 670 | |
| 671 | service_->technology_ = Technology::kEthernet; |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 672 | EXPECT_TRUE(service_->IsAutoConnectable(&reason)); |
mukesh agrawal | 76d1388 | 2012-01-12 15:23:11 -0800 | [diff] [blame] | 673 | |
mukesh agrawal | adb6848 | 2012-01-17 16:31:51 -0800 | [diff] [blame] | 674 | // We should not auto-connect to a Service that a user has |
| 675 | // deliberately disconnected. |
| 676 | Error error; |
Christopher Wiley | abd3b50 | 2012-09-26 13:08:52 -0700 | [diff] [blame] | 677 | service_->UserInitiatedDisconnect(&error); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 678 | EXPECT_FALSE(service_->IsAutoConnectable(&reason)); |
| 679 | EXPECT_STREQ(Service::kAutoConnExplicitDisconnect, reason); |
mukesh agrawal | adb6848 | 2012-01-17 16:31:51 -0800 | [diff] [blame] | 680 | |
| 681 | // But if the Service is reloaded, it is eligible for auto-connect |
| 682 | // again. |
| 683 | NiceMock<MockStore> storage; |
| 684 | EXPECT_CALL(storage, ContainsGroup(storage_id_)).WillOnce(Return(true)); |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 685 | EXPECT_CALL(*eap_, Load(&storage, storage_id_)); |
mukesh agrawal | adb6848 | 2012-01-17 16:31:51 -0800 | [diff] [blame] | 686 | EXPECT_TRUE(service_->Load(&storage)); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 687 | EXPECT_TRUE(service_->IsAutoConnectable(&reason)); |
mukesh agrawal | adb6848 | 2012-01-17 16:31:51 -0800 | [diff] [blame] | 688 | |
| 689 | // A deliberate Connect should also re-enable auto-connect. |
Christopher Wiley | abd3b50 | 2012-09-26 13:08:52 -0700 | [diff] [blame] | 690 | service_->UserInitiatedDisconnect(&error); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 691 | EXPECT_FALSE(service_->IsAutoConnectable(&reason)); |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 692 | service_->Connect(&error, "in test"); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 693 | EXPECT_TRUE(service_->IsAutoConnectable(&reason)); |
mukesh agrawal | adb6848 | 2012-01-17 16:31:51 -0800 | [diff] [blame] | 694 | |
Christopher Wiley | abd3b50 | 2012-09-26 13:08:52 -0700 | [diff] [blame] | 695 | // A non-user initiated Disconnect doesn't change anything. |
| 696 | service_->Disconnect(&error); |
| 697 | EXPECT_TRUE(service_->IsAutoConnectable(&reason)); |
| 698 | |
Christopher Wiley | a4c61ae | 2012-10-01 11:04:30 -0700 | [diff] [blame] | 699 | // A resume also re-enables auto-connect. |
| 700 | service_->UserInitiatedDisconnect(&error); |
| 701 | EXPECT_FALSE(service_->IsAutoConnectable(&reason)); |
| 702 | service_->OnAfterResume(); |
| 703 | EXPECT_TRUE(service_->IsAutoConnectable(&reason)); |
mukesh agrawal | adb6848 | 2012-01-17 16:31:51 -0800 | [diff] [blame] | 704 | |
mukesh agrawal | 76d1388 | 2012-01-12 15:23:11 -0800 | [diff] [blame] | 705 | service_->SetState(Service::kStateConnected); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 706 | EXPECT_FALSE(service_->IsAutoConnectable(&reason)); |
| 707 | EXPECT_STREQ(Service::kAutoConnConnected, reason); |
mukesh agrawal | 76d1388 | 2012-01-12 15:23:11 -0800 | [diff] [blame] | 708 | |
| 709 | service_->SetState(Service::kStateAssociating); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 710 | EXPECT_FALSE(service_->IsAutoConnectable(&reason)); |
| 711 | EXPECT_STREQ(Service::kAutoConnConnecting, reason); |
mukesh agrawal | 76d1388 | 2012-01-12 15:23:11 -0800 | [diff] [blame] | 712 | } |
| 713 | |
mukesh agrawal | 03c15ce | 2012-11-29 17:36:21 -0800 | [diff] [blame] | 714 | TEST_F(ServiceTest, AutoConnectLogging) { |
| 715 | ScopedMockLog log; |
| 716 | EXPECT_CALL(log, Log(_, _, _)); |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 717 | service_->SetConnectable(true); |
mukesh agrawal | 03c15ce | 2012-11-29 17:36:21 -0800 | [diff] [blame] | 718 | |
| 719 | ScopeLogger::GetInstance()->EnableScopesByName("+service"); |
| 720 | ScopeLogger::GetInstance()->set_verbose_level(1); |
| 721 | service_->SetState(Service::kStateConnected); |
| 722 | EXPECT_CALL(log, Log(-1, _, HasSubstr(Service::kAutoConnConnected))); |
| 723 | service_->AutoConnect(); |
| 724 | |
| 725 | ScopeLogger::GetInstance()->EnableScopesByName("-service"); |
| 726 | ScopeLogger::GetInstance()->set_verbose_level(0); |
| 727 | EXPECT_CALL(log, Log(logging::LOG_INFO, _, |
| 728 | HasSubstr(Service::kAutoConnNotConnectable))); |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 729 | service_->SetConnectable(false); |
mukesh agrawal | 03c15ce | 2012-11-29 17:36:21 -0800 | [diff] [blame] | 730 | service_->AutoConnect(); |
| 731 | } |
| 732 | |
| 733 | |
Christopher Wiley | 0801d19 | 2012-09-24 11:57:15 -0700 | [diff] [blame] | 734 | TEST_F(AllMockServiceTest, AutoConnectWithFailures) { |
| 735 | const char *reason; |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 736 | service_->SetConnectable(true); |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 737 | service_->technology_ = Technology::kEthernet; |
Christopher Wiley | 0801d19 | 2012-09-24 11:57:15 -0700 | [diff] [blame] | 738 | EXPECT_TRUE(service_->IsAutoConnectable(&reason)); |
| 739 | |
| 740 | // The very first AutoConnect() doesn't trigger any throttling. |
| 741 | EXPECT_CALL(dispatcher_, PostDelayedTask(_, _)).Times(0); |
| 742 | service_->AutoConnect(); |
| 743 | Mock::VerifyAndClearExpectations(&dispatcher_); |
| 744 | EXPECT_TRUE(service_->IsAutoConnectable(&reason)); |
| 745 | |
| 746 | // The second call does trigger some throttling. |
| 747 | EXPECT_CALL(dispatcher_, PostDelayedTask(_, |
| 748 | Service::kMinAutoConnectCooldownTimeMilliseconds)); |
| 749 | service_->AutoConnect(); |
| 750 | Mock::VerifyAndClearExpectations(&dispatcher_); |
| 751 | EXPECT_FALSE(service_->IsAutoConnectable(&reason)); |
| 752 | EXPECT_STREQ(Service::kAutoConnThrottled, reason); |
| 753 | |
| 754 | // Calling AutoConnect() again before the cooldown terminates does not change |
| 755 | // the timeout. |
| 756 | EXPECT_CALL(dispatcher_, PostDelayedTask(_, _)).Times(0); |
| 757 | service_->AutoConnect(); |
| 758 | Mock::VerifyAndClearExpectations(&dispatcher_); |
| 759 | EXPECT_FALSE(service_->IsAutoConnectable(&reason)); |
| 760 | EXPECT_STREQ(Service::kAutoConnThrottled, reason); |
| 761 | |
| 762 | // Once the timeout expires, we can AutoConnect() again. |
| 763 | service_->ReEnableAutoConnectTask(); |
| 764 | EXPECT_TRUE(service_->IsAutoConnectable(&reason)); |
| 765 | |
| 766 | // Timeouts increase exponentially. |
| 767 | uint64 next_cooldown_time = service_->auto_connect_cooldown_milliseconds_; |
| 768 | EXPECT_EQ(next_cooldown_time, |
| 769 | Service::kAutoConnectCooldownBackoffFactor * |
| 770 | Service::kMinAutoConnectCooldownTimeMilliseconds); |
| 771 | while (next_cooldown_time <= |
| 772 | Service::kMaxAutoConnectCooldownTimeMilliseconds) { |
| 773 | EXPECT_CALL(dispatcher_, PostDelayedTask(_, next_cooldown_time)); |
| 774 | service_->AutoConnect(); |
| 775 | Mock::VerifyAndClearExpectations(&dispatcher_); |
| 776 | EXPECT_FALSE(service_->IsAutoConnectable(&reason)); |
| 777 | EXPECT_STREQ(Service::kAutoConnThrottled, reason); |
| 778 | service_->ReEnableAutoConnectTask(); |
| 779 | next_cooldown_time *= Service::kAutoConnectCooldownBackoffFactor; |
| 780 | } |
| 781 | |
| 782 | // Once we hit our cap, future timeouts are the same. |
| 783 | for (int32 i = 0; i < 2; i++) { |
| 784 | EXPECT_CALL(dispatcher_, PostDelayedTask(_, |
| 785 | Service::kMaxAutoConnectCooldownTimeMilliseconds)); |
| 786 | service_->AutoConnect(); |
| 787 | Mock::VerifyAndClearExpectations(&dispatcher_); |
| 788 | EXPECT_FALSE(service_->IsAutoConnectable(&reason)); |
| 789 | EXPECT_STREQ(Service::kAutoConnThrottled, reason); |
| 790 | service_->ReEnableAutoConnectTask(); |
| 791 | } |
| 792 | |
| 793 | // Connecting successfully resets our cooldown. |
| 794 | service_->SetState(Service::kStateConnected); |
| 795 | service_->SetState(Service::kStateIdle); |
| 796 | reason = ""; |
| 797 | EXPECT_TRUE(service_->IsAutoConnectable(&reason)); |
| 798 | EXPECT_STREQ("", reason); |
| 799 | EXPECT_EQ(service_->auto_connect_cooldown_milliseconds_, 0); |
| 800 | |
| 801 | // But future AutoConnects behave as before |
| 802 | EXPECT_CALL(dispatcher_, PostDelayedTask(_, |
| 803 | Service::kMinAutoConnectCooldownTimeMilliseconds)).Times(1); |
| 804 | service_->AutoConnect(); |
| 805 | service_->AutoConnect(); |
| 806 | Mock::VerifyAndClearExpectations(&dispatcher_); |
| 807 | EXPECT_FALSE(service_->IsAutoConnectable(&reason)); |
| 808 | EXPECT_STREQ(Service::kAutoConnThrottled, reason); |
| 809 | |
| 810 | // Cooldowns are forgotten if we go through a suspend/resume cycle. |
| 811 | service_->OnAfterResume(); |
| 812 | reason = ""; |
| 813 | EXPECT_TRUE(service_->IsAutoConnectable(&reason)); |
| 814 | EXPECT_STREQ("", reason); |
| 815 | } |
| 816 | |
Paul Stewart | cb59fed | 2012-03-21 21:14:46 -0700 | [diff] [blame] | 817 | TEST_F(ServiceTest, ConfigureBadProperty) { |
| 818 | KeyValueStore args; |
| 819 | args.SetString("XXXInvalid", "Value"); |
| 820 | Error error; |
| 821 | service_->Configure(args, &error); |
| 822 | EXPECT_FALSE(error.IsSuccess()); |
| 823 | } |
| 824 | |
| 825 | TEST_F(ServiceTest, ConfigureBoolProperty) { |
| 826 | service_->MakeFavorite(); |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 827 | service_->SetAutoConnect(false); |
Paul Stewart | cb59fed | 2012-03-21 21:14:46 -0700 | [diff] [blame] | 828 | ASSERT_FALSE(service_->auto_connect()); |
| 829 | KeyValueStore args; |
| 830 | args.SetBool(flimflam::kAutoConnectProperty, true); |
| 831 | Error error; |
| 832 | service_->Configure(args, &error); |
| 833 | EXPECT_TRUE(error.IsSuccess()); |
| 834 | EXPECT_TRUE(service_->auto_connect()); |
| 835 | } |
| 836 | |
| 837 | TEST_F(ServiceTest, ConfigureStringProperty) { |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 838 | const string kGuid0 = "guid_zero"; |
| 839 | const string kGuid1 = "guid_one"; |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 840 | service_->SetGuid(kGuid0, NULL); |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 841 | ASSERT_EQ(kGuid0, service_->guid()); |
Paul Stewart | cb59fed | 2012-03-21 21:14:46 -0700 | [diff] [blame] | 842 | KeyValueStore args; |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 843 | args.SetString(flimflam::kGuidProperty, kGuid1); |
Paul Stewart | cb59fed | 2012-03-21 21:14:46 -0700 | [diff] [blame] | 844 | Error error; |
| 845 | service_->Configure(args, &error); |
| 846 | EXPECT_TRUE(error.IsSuccess()); |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 847 | EXPECT_EQ(kGuid1, service_->guid()); |
| 848 | } |
| 849 | |
Paul Stewart | 99dc9f3 | 2013-06-27 07:39:25 -0700 | [diff] [blame] | 850 | TEST_F(ServiceTest, ConfigureStringsProperty) { |
| 851 | const vector<string> kStrings0{ "string0", "string1" }; |
| 852 | const vector<string> kStrings1{ "string2", "string3" }; |
| 853 | service_->set_strings(kStrings0); |
| 854 | ASSERT_EQ(kStrings0, service_->strings()); |
| 855 | KeyValueStore args; |
| 856 | args.SetStrings(ServiceUnderTest::kStringsProperty, kStrings1); |
| 857 | Error error; |
| 858 | service_->Configure(args, &error); |
| 859 | EXPECT_TRUE(error.IsSuccess()); |
| 860 | EXPECT_EQ(kStrings1, service_->strings()); |
| 861 | } |
| 862 | |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 863 | TEST_F(ServiceTest, ConfigureEapStringProperty) { |
| 864 | MockEapCredentials *eap = new MockEapCredentials(); |
| 865 | service2_->SetEapCredentials(eap); // Passes ownership. |
| 866 | |
| 867 | const string kEAPManagement0 = "management_zero"; |
| 868 | const string kEAPManagement1 = "management_one"; |
| 869 | service2_->SetEAPKeyManagement(kEAPManagement0); |
| 870 | |
| 871 | EXPECT_CALL(*eap, key_management()) |
| 872 | .WillOnce(ReturnRef(kEAPManagement0)); |
| 873 | ASSERT_EQ(kEAPManagement0, service2_->GetEAPKeyManagement()); |
| 874 | KeyValueStore args; |
| 875 | EXPECT_CALL(*eap, SetKeyManagement(kEAPManagement1, _)); |
| 876 | args.SetString(flimflam::kEapKeyMgmtProperty, kEAPManagement1); |
| 877 | Error error; |
| 878 | service2_->Configure(args, &error); |
| 879 | EXPECT_TRUE(error.IsSuccess()); |
Paul Stewart | cb59fed | 2012-03-21 21:14:46 -0700 | [diff] [blame] | 880 | } |
| 881 | |
Paul Stewart | 7a20aa4 | 2013-01-17 12:21:41 -0800 | [diff] [blame] | 882 | TEST_F(ServiceTest, ConfigureIntProperty) { |
| 883 | const int kPriority0 = 100; |
| 884 | const int kPriority1 = 200; |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 885 | service_->SetPriority(kPriority0, NULL); |
Paul Stewart | 7a20aa4 | 2013-01-17 12:21:41 -0800 | [diff] [blame] | 886 | ASSERT_EQ(kPriority0, service_->priority()); |
| 887 | KeyValueStore args; |
| 888 | args.SetInt(flimflam::kPriorityProperty, kPriority1); |
| 889 | Error error; |
| 890 | service_->Configure(args, &error); |
| 891 | EXPECT_TRUE(error.IsSuccess()); |
| 892 | EXPECT_EQ(kPriority1, service_->priority()); |
| 893 | } |
| 894 | |
Paul Stewart | cb59fed | 2012-03-21 21:14:46 -0700 | [diff] [blame] | 895 | TEST_F(ServiceTest, ConfigureIgnoredProperty) { |
| 896 | service_->MakeFavorite(); |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 897 | service_->SetAutoConnect(false); |
Paul Stewart | cb59fed | 2012-03-21 21:14:46 -0700 | [diff] [blame] | 898 | ASSERT_FALSE(service_->auto_connect()); |
| 899 | KeyValueStore args; |
| 900 | args.SetBool(flimflam::kAutoConnectProperty, true); |
| 901 | Error error; |
| 902 | service_->IgnoreParameterForConfigure(flimflam::kAutoConnectProperty); |
| 903 | service_->Configure(args, &error); |
| 904 | EXPECT_TRUE(error.IsSuccess()); |
| 905 | EXPECT_FALSE(service_->auto_connect()); |
| 906 | } |
| 907 | |
Paul Stewart | ad0e598 | 2013-07-02 08:47:47 -0700 | [diff] [blame] | 908 | TEST_F(ServiceTest, ConfigureProfileProperty) { |
| 909 | // Ensure that the Profile property is always ignored. |
| 910 | KeyValueStore args; |
| 911 | args.SetString(flimflam::kProfileProperty, "profile"); |
| 912 | Error error; |
| 913 | EXPECT_CALL(mock_manager_, SetProfileForService(_, _, _)).Times(0); |
| 914 | service_->Configure(args, &error); |
| 915 | EXPECT_TRUE(error.IsSuccess()); |
| 916 | } |
| 917 | |
Paul Stewart | 7a20aa4 | 2013-01-17 12:21:41 -0800 | [diff] [blame] | 918 | TEST_F(ServiceTest, DoPropertiesMatch) { |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 919 | service_->SetAutoConnect(false); |
Paul Stewart | 7a20aa4 | 2013-01-17 12:21:41 -0800 | [diff] [blame] | 920 | const string kGUID0 = "guid_zero"; |
| 921 | const string kGUID1 = "guid_one"; |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 922 | service_->SetGuid(kGUID0, NULL); |
Paul Stewart | 7a20aa4 | 2013-01-17 12:21:41 -0800 | [diff] [blame] | 923 | const uint32 kPriority0 = 100; |
| 924 | const uint32 kPriority1 = 200; |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 925 | service_->SetPriority(kPriority0, NULL); |
Paul Stewart | 99dc9f3 | 2013-06-27 07:39:25 -0700 | [diff] [blame] | 926 | const vector<string> kStrings0{ "string0", "string1" }; |
| 927 | const vector<string> kStrings1{ "string2", "string3" }; |
| 928 | service_->set_strings(kStrings0); |
Paul Stewart | 7a20aa4 | 2013-01-17 12:21:41 -0800 | [diff] [blame] | 929 | |
| 930 | { |
| 931 | KeyValueStore args; |
| 932 | args.SetString(flimflam::kGuidProperty, kGUID0); |
| 933 | args.SetBool(flimflam::kAutoConnectProperty, false); |
| 934 | args.SetInt(flimflam::kPriorityProperty, kPriority0); |
Paul Stewart | 99dc9f3 | 2013-06-27 07:39:25 -0700 | [diff] [blame] | 935 | args.SetStrings(ServiceUnderTest::kStringsProperty, kStrings0); |
Paul Stewart | 7a20aa4 | 2013-01-17 12:21:41 -0800 | [diff] [blame] | 936 | EXPECT_TRUE(service_->DoPropertiesMatch(args)); |
| 937 | } |
| 938 | { |
| 939 | KeyValueStore args; |
| 940 | args.SetString(flimflam::kGuidProperty, kGUID1); |
| 941 | args.SetBool(flimflam::kAutoConnectProperty, false); |
| 942 | args.SetInt(flimflam::kPriorityProperty, kPriority0); |
Paul Stewart | 99dc9f3 | 2013-06-27 07:39:25 -0700 | [diff] [blame] | 943 | args.SetStrings(ServiceUnderTest::kStringsProperty, kStrings0); |
Paul Stewart | 7a20aa4 | 2013-01-17 12:21:41 -0800 | [diff] [blame] | 944 | EXPECT_FALSE(service_->DoPropertiesMatch(args)); |
| 945 | } |
| 946 | { |
| 947 | KeyValueStore args; |
| 948 | args.SetString(flimflam::kGuidProperty, kGUID0); |
| 949 | args.SetBool(flimflam::kAutoConnectProperty, true); |
| 950 | args.SetInt(flimflam::kPriorityProperty, kPriority0); |
Paul Stewart | 99dc9f3 | 2013-06-27 07:39:25 -0700 | [diff] [blame] | 951 | args.SetStrings(ServiceUnderTest::kStringsProperty, kStrings0); |
Paul Stewart | 7a20aa4 | 2013-01-17 12:21:41 -0800 | [diff] [blame] | 952 | EXPECT_FALSE(service_->DoPropertiesMatch(args)); |
| 953 | } |
| 954 | { |
| 955 | KeyValueStore args; |
| 956 | args.SetString(flimflam::kGuidProperty, kGUID0); |
| 957 | args.SetBool(flimflam::kAutoConnectProperty, false); |
| 958 | args.SetInt(flimflam::kPriorityProperty, kPriority1); |
Paul Stewart | 99dc9f3 | 2013-06-27 07:39:25 -0700 | [diff] [blame] | 959 | args.SetStrings(ServiceUnderTest::kStringsProperty, kStrings0); |
| 960 | EXPECT_FALSE(service_->DoPropertiesMatch(args)); |
| 961 | } |
| 962 | { |
| 963 | KeyValueStore args; |
| 964 | args.SetString(flimflam::kGuidProperty, kGUID0); |
| 965 | args.SetBool(flimflam::kAutoConnectProperty, false); |
| 966 | args.SetInt(flimflam::kPriorityProperty, kPriority0); |
| 967 | args.SetStrings(ServiceUnderTest::kStringsProperty, kStrings1); |
Paul Stewart | 7a20aa4 | 2013-01-17 12:21:41 -0800 | [diff] [blame] | 968 | EXPECT_FALSE(service_->DoPropertiesMatch(args)); |
| 969 | } |
| 970 | } |
| 971 | |
Paul Stewart | 10ccbb3 | 2012-04-26 15:59:30 -0700 | [diff] [blame] | 972 | TEST_F(ServiceTest, IsRemembered) { |
| 973 | ServiceConstRefPtr service_ref(service_); |
| 974 | service_->set_profile(NULL); |
| 975 | EXPECT_CALL(mock_manager_, IsServiceEphemeral(_)).Times(0); |
| 976 | EXPECT_FALSE(service_->IsRemembered()); |
| 977 | |
| 978 | scoped_refptr<MockProfile> profile( |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 979 | new StrictMock<MockProfile>(control_interface(), metrics(), manager())); |
Paul Stewart | 10ccbb3 | 2012-04-26 15:59:30 -0700 | [diff] [blame] | 980 | service_->set_profile(profile); |
| 981 | EXPECT_CALL(mock_manager_, IsServiceEphemeral(service_ref)) |
| 982 | .WillOnce(Return(true)) |
| 983 | .WillOnce(Return(false)); |
| 984 | EXPECT_FALSE(service_->IsRemembered()); |
| 985 | EXPECT_TRUE(service_->IsRemembered()); |
| 986 | } |
| 987 | |
Paul Stewart | df3c0a8 | 2012-11-09 15:54:33 -0800 | [diff] [blame] | 988 | TEST_F(ServiceTest, IsDependentOn) { |
| 989 | EXPECT_FALSE(service_->IsDependentOn(NULL)); |
| 990 | |
| 991 | scoped_ptr<MockDeviceInfo> mock_device_info( |
| 992 | new NiceMock<MockDeviceInfo>(control_interface(), dispatcher(), metrics(), |
| 993 | &mock_manager_)); |
| 994 | scoped_refptr<MockConnection> mock_connection0( |
| 995 | new NiceMock<MockConnection>(mock_device_info.get())); |
| 996 | scoped_refptr<MockConnection> mock_connection1( |
| 997 | new NiceMock<MockConnection>(mock_device_info.get())); |
| 998 | |
| 999 | service_->connection_ = mock_connection0; |
Paul Stewart | cd7f585 | 2013-03-27 13:54:23 -0700 | [diff] [blame] | 1000 | EXPECT_CALL(*mock_connection0, GetLowerConnection()) |
Paul Stewart | df3c0a8 | 2012-11-09 15:54:33 -0800 | [diff] [blame] | 1001 | .WillRepeatedly(Return(mock_connection1)); |
Paul Stewart | cd7f585 | 2013-03-27 13:54:23 -0700 | [diff] [blame] | 1002 | EXPECT_CALL(*mock_connection1, GetLowerConnection()) |
| 1003 | .WillRepeatedly(Return(ConnectionRefPtr())); |
Paul Stewart | df3c0a8 | 2012-11-09 15:54:33 -0800 | [diff] [blame] | 1004 | EXPECT_FALSE(service_->IsDependentOn(NULL)); |
| 1005 | |
| 1006 | scoped_refptr<ServiceUnderTest> service1 = |
| 1007 | new ServiceUnderTest(control_interface(), |
| 1008 | dispatcher(), |
| 1009 | metrics(), |
| 1010 | &mock_manager_); |
| 1011 | EXPECT_FALSE(service_->IsDependentOn(service1)); |
| 1012 | |
| 1013 | service1->connection_ = mock_connection0; |
| 1014 | EXPECT_FALSE(service_->IsDependentOn(service1)); |
| 1015 | |
| 1016 | service1->connection_ = mock_connection1; |
| 1017 | EXPECT_TRUE(service_->IsDependentOn(service1)); |
| 1018 | |
Paul Stewart | cd7f585 | 2013-03-27 13:54:23 -0700 | [diff] [blame] | 1019 | service_->connection_ = mock_connection1; |
Paul Stewart | df3c0a8 | 2012-11-09 15:54:33 -0800 | [diff] [blame] | 1020 | service1->connection_ = NULL; |
Paul Stewart | cd7f585 | 2013-03-27 13:54:23 -0700 | [diff] [blame] | 1021 | EXPECT_FALSE(service_->IsDependentOn(service1)); |
| 1022 | |
| 1023 | service_->connection_ = NULL; |
Paul Stewart | df3c0a8 | 2012-11-09 15:54:33 -0800 | [diff] [blame] | 1024 | } |
| 1025 | |
Paul Stewart | ff14b02 | 2012-04-24 20:06:23 -0700 | [diff] [blame] | 1026 | TEST_F(ServiceTest, OnPropertyChanged) { |
| 1027 | scoped_refptr<MockProfile> profile( |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 1028 | new StrictMock<MockProfile>(control_interface(), metrics(), manager())); |
Paul Stewart | ff14b02 | 2012-04-24 20:06:23 -0700 | [diff] [blame] | 1029 | service_->set_profile(NULL); |
| 1030 | // Expect no crash. |
| 1031 | service_->OnPropertyChanged(""); |
| 1032 | |
| 1033 | // Expect no call to Update if the profile has no storage. |
| 1034 | service_->set_profile(profile); |
| 1035 | EXPECT_CALL(*profile, UpdateService(_)).Times(0); |
| 1036 | EXPECT_CALL(*profile, GetConstStorage()) |
| 1037 | .WillOnce(Return(reinterpret_cast<StoreInterface *>(NULL))); |
| 1038 | service_->OnPropertyChanged(""); |
| 1039 | |
| 1040 | // Expect call to Update if the profile has storage. |
| 1041 | EXPECT_CALL(*profile, UpdateService(_)).Times(1); |
| 1042 | NiceMock<MockStore> storage; |
| 1043 | EXPECT_CALL(*profile, GetConstStorage()).WillOnce(Return(&storage)); |
| 1044 | service_->OnPropertyChanged(""); |
| 1045 | } |
| 1046 | |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 1047 | |
| 1048 | TEST_F(ServiceTest, RecheckPortal) { |
| 1049 | ServiceRefPtr service_ref(service_); |
| 1050 | service_->state_ = Service::kStateIdle; |
| 1051 | EXPECT_CALL(mock_manager_, RecheckPortalOnService(_)).Times(0); |
| 1052 | service_->OnPropertyChanged(flimflam::kCheckPortalProperty); |
| 1053 | |
| 1054 | service_->state_ = Service::kStatePortal; |
| 1055 | EXPECT_CALL(mock_manager_, RecheckPortalOnService(service_ref)).Times(1); |
| 1056 | service_->OnPropertyChanged(flimflam::kCheckPortalProperty); |
| 1057 | |
| 1058 | service_->state_ = Service::kStateConnected; |
| 1059 | EXPECT_CALL(mock_manager_, RecheckPortalOnService(service_ref)).Times(1); |
| 1060 | service_->OnPropertyChanged(flimflam::kProxyConfigProperty); |
| 1061 | |
| 1062 | service_->state_ = Service::kStateOnline; |
| 1063 | EXPECT_CALL(mock_manager_, RecheckPortalOnService(service_ref)).Times(1); |
| 1064 | service_->OnPropertyChanged(flimflam::kCheckPortalProperty); |
| 1065 | |
| 1066 | service_->state_ = Service::kStatePortal; |
| 1067 | EXPECT_CALL(mock_manager_, RecheckPortalOnService(_)).Times(0); |
| 1068 | service_->OnPropertyChanged(flimflam::kEAPKeyIDProperty); |
| 1069 | } |
| 1070 | |
| 1071 | TEST_F(ServiceTest, SetCheckPortal) { |
| 1072 | ServiceRefPtr service_ref(service_); |
| 1073 | { |
| 1074 | Error error; |
| 1075 | service_->SetCheckPortal("false", &error); |
| 1076 | EXPECT_TRUE(error.IsSuccess()); |
| 1077 | EXPECT_EQ(Service::kCheckPortalFalse, service_->check_portal_); |
| 1078 | } |
| 1079 | { |
| 1080 | Error error; |
| 1081 | service_->SetCheckPortal("true", &error); |
| 1082 | EXPECT_TRUE(error.IsSuccess()); |
| 1083 | EXPECT_EQ(Service::kCheckPortalTrue, service_->check_portal_); |
| 1084 | } |
| 1085 | { |
| 1086 | Error error; |
| 1087 | service_->SetCheckPortal("auto", &error); |
| 1088 | EXPECT_TRUE(error.IsSuccess()); |
| 1089 | EXPECT_EQ(Service::kCheckPortalAuto, service_->check_portal_); |
| 1090 | } |
| 1091 | { |
| 1092 | Error error; |
| 1093 | service_->SetCheckPortal("xxx", &error); |
| 1094 | EXPECT_FALSE(error.IsSuccess()); |
| 1095 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 1096 | EXPECT_EQ(Service::kCheckPortalAuto, service_->check_portal_); |
| 1097 | } |
| 1098 | } |
| 1099 | |
Arman Uguray | 2717a10 | 2013-01-29 23:36:06 -0800 | [diff] [blame] | 1100 | TEST_F(ServiceTest, SetFriendlyName) { |
| 1101 | EXPECT_EQ(service_->unique_name_, service_->friendly_name_); |
Darin Petkov | aba8932 | 2013-03-11 14:48:22 +0100 | [diff] [blame] | 1102 | ServiceMockAdaptor *adaptor = GetAdaptor(); |
Arman Uguray | 2717a10 | 2013-01-29 23:36:06 -0800 | [diff] [blame] | 1103 | |
| 1104 | EXPECT_CALL(*adaptor, EmitStringChanged(_, _)).Times(0); |
| 1105 | service_->SetFriendlyName(service_->unique_name_); |
| 1106 | EXPECT_EQ(service_->unique_name_, service_->friendly_name_); |
| 1107 | |
| 1108 | EXPECT_CALL(*adaptor, EmitStringChanged(flimflam::kNameProperty, |
| 1109 | "Test Name 1")); |
| 1110 | service_->SetFriendlyName("Test Name 1"); |
| 1111 | EXPECT_EQ("Test Name 1", service_->friendly_name_); |
| 1112 | |
| 1113 | EXPECT_CALL(*adaptor, EmitStringChanged(_, _)).Times(0); |
| 1114 | service_->SetFriendlyName("Test Name 1"); |
| 1115 | EXPECT_EQ("Test Name 1", service_->friendly_name_); |
| 1116 | |
| 1117 | EXPECT_CALL(*adaptor, EmitStringChanged(flimflam::kNameProperty, |
| 1118 | "Test Name 2")); |
| 1119 | service_->SetFriendlyName("Test Name 2"); |
| 1120 | EXPECT_EQ("Test Name 2", service_->friendly_name_); |
| 1121 | } |
| 1122 | |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 1123 | TEST_F(ServiceTest, SetConnectableFull) { |
Darin Petkov | b2ba39f | 2012-06-06 10:33:43 +0200 | [diff] [blame] | 1124 | EXPECT_FALSE(service_->connectable()); |
| 1125 | |
Darin Petkov | aba8932 | 2013-03-11 14:48:22 +0100 | [diff] [blame] | 1126 | ServiceMockAdaptor *adaptor = GetAdaptor(); |
Darin Petkov | b2ba39f | 2012-06-06 10:33:43 +0200 | [diff] [blame] | 1127 | |
| 1128 | EXPECT_CALL(*adaptor, EmitBoolChanged(_, _)).Times(0); |
| 1129 | EXPECT_CALL(mock_manager_, HasService(_)).Times(0); |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 1130 | service_->SetConnectableFull(false); |
Darin Petkov | b2ba39f | 2012-06-06 10:33:43 +0200 | [diff] [blame] | 1131 | EXPECT_FALSE(service_->connectable()); |
| 1132 | |
| 1133 | EXPECT_CALL(*adaptor, EmitBoolChanged(flimflam::kConnectableProperty, true)); |
| 1134 | EXPECT_CALL(mock_manager_, HasService(_)).WillOnce(Return(false)); |
| 1135 | EXPECT_CALL(mock_manager_, UpdateService(_)).Times(0); |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 1136 | service_->SetConnectableFull(true); |
Darin Petkov | b2ba39f | 2012-06-06 10:33:43 +0200 | [diff] [blame] | 1137 | EXPECT_TRUE(service_->connectable()); |
| 1138 | |
| 1139 | EXPECT_CALL(*adaptor, EmitBoolChanged(flimflam::kConnectableProperty, false)); |
| 1140 | EXPECT_CALL(mock_manager_, HasService(_)).WillOnce(Return(true)); |
| 1141 | EXPECT_CALL(mock_manager_, UpdateService(_)); |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 1142 | service_->SetConnectableFull(false); |
Darin Petkov | b2ba39f | 2012-06-06 10:33:43 +0200 | [diff] [blame] | 1143 | EXPECT_FALSE(service_->connectable()); |
| 1144 | |
| 1145 | EXPECT_CALL(*adaptor, EmitBoolChanged(flimflam::kConnectableProperty, true)); |
| 1146 | EXPECT_CALL(mock_manager_, HasService(_)).WillOnce(Return(true)); |
| 1147 | EXPECT_CALL(mock_manager_, UpdateService(_)); |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 1148 | service_->SetConnectableFull(true); |
Darin Petkov | b2ba39f | 2012-06-06 10:33:43 +0200 | [diff] [blame] | 1149 | EXPECT_TRUE(service_->connectable()); |
| 1150 | } |
| 1151 | |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 1152 | class WriteOnlyServicePropertyTest : public ServiceTest {}; |
| 1153 | TEST_P(WriteOnlyServicePropertyTest, PropertyWriteOnly) { |
| 1154 | // Use a real EapCredentials instance since the base Service class |
| 1155 | // contains no write-only properties. |
| 1156 | EapCredentials eap; |
| 1157 | eap.InitPropertyStore(service_->mutable_store()); |
| 1158 | |
Paul Stewart | 9f32d19 | 2012-01-30 20:37:50 -0800 | [diff] [blame] | 1159 | string property(GetParam().reader().get_string()); |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 1160 | Error error; |
| 1161 | EXPECT_FALSE(service_->store().GetStringProperty(property, NULL, &error)); |
| 1162 | EXPECT_EQ(Error::kPermissionDenied, error.type()); |
Paul Stewart | 9f32d19 | 2012-01-30 20:37:50 -0800 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | INSTANTIATE_TEST_CASE_P( |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 1166 | WriteOnlyServicePropertyTestInstance, |
| 1167 | WriteOnlyServicePropertyTest, |
Paul Stewart | 9f32d19 | 2012-01-30 20:37:50 -0800 | [diff] [blame] | 1168 | Values( |
| 1169 | DBusAdaptor::StringToVariant(flimflam::kEapPrivateKeyPasswordProperty), |
| 1170 | DBusAdaptor::StringToVariant(flimflam::kEapPasswordProperty))); |
| 1171 | |
Paul Stewart | 10241e3 | 2012-04-23 18:15:06 -0700 | [diff] [blame] | 1172 | |
| 1173 | TEST_F(ServiceTest, GetIPConfigRpcIdentifier) { |
| 1174 | { |
| 1175 | Error error; |
| 1176 | EXPECT_EQ("/", service_->GetIPConfigRpcIdentifier(&error)); |
| 1177 | EXPECT_EQ(Error::kNotFound, error.type()); |
| 1178 | } |
| 1179 | |
| 1180 | scoped_ptr<MockDeviceInfo> mock_device_info( |
| 1181 | new NiceMock<MockDeviceInfo>(control_interface(), dispatcher(), metrics(), |
| 1182 | &mock_manager_)); |
| 1183 | scoped_refptr<MockConnection> mock_connection( |
| 1184 | new NiceMock<MockConnection>(mock_device_info.get())); |
| 1185 | |
| 1186 | service_->connection_ = mock_connection; |
| 1187 | |
| 1188 | { |
| 1189 | Error error; |
| 1190 | const string empty_string; |
| 1191 | EXPECT_CALL(*mock_connection, ipconfig_rpc_identifier()) |
| 1192 | .WillOnce(ReturnRef(empty_string)); |
| 1193 | EXPECT_EQ("/", service_->GetIPConfigRpcIdentifier(&error)); |
| 1194 | EXPECT_EQ(Error::kNotFound, error.type()); |
| 1195 | } |
| 1196 | |
| 1197 | { |
| 1198 | Error error; |
| 1199 | const string nonempty_string("/ipconfig/path"); |
| 1200 | EXPECT_CALL(*mock_connection, ipconfig_rpc_identifier()) |
| 1201 | .WillOnce(ReturnRef(nonempty_string)); |
| 1202 | EXPECT_EQ(nonempty_string, service_->GetIPConfigRpcIdentifier(&error)); |
| 1203 | EXPECT_EQ(Error::kSuccess, error.type()); |
| 1204 | } |
| 1205 | |
| 1206 | // Assure orderly destruction of the Connection before DeviceInfo. |
| 1207 | service_->connection_ = NULL; |
| 1208 | mock_connection = NULL; |
| 1209 | mock_device_info.reset(); |
| 1210 | } |
| 1211 | |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 1212 | class ServiceWithMockOnEapCredentialsChanged : public ServiceUnderTest { |
Paul Stewart | 8142613 | 2012-05-16 10:05:10 -0700 | [diff] [blame] | 1213 | public: |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 1214 | ServiceWithMockOnEapCredentialsChanged(ControlInterface *control_interface, |
| 1215 | EventDispatcher *dispatcher, |
| 1216 | Metrics *metrics, |
| 1217 | Manager *manager) |
Paul Stewart | 8142613 | 2012-05-16 10:05:10 -0700 | [diff] [blame] | 1218 | : ServiceUnderTest(control_interface, dispatcher, metrics, manager), |
| 1219 | is_8021x_(false) {} |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 1220 | MOCK_METHOD0(OnEapCredentialsChanged, void()); |
Paul Stewart | 8142613 | 2012-05-16 10:05:10 -0700 | [diff] [blame] | 1221 | virtual bool Is8021x() const { return is_8021x_; } |
| 1222 | void set_is_8021x(bool is_8021x) { is_8021x_ = is_8021x; } |
| 1223 | |
| 1224 | private: |
| 1225 | bool is_8021x_; |
| 1226 | }; |
| 1227 | |
| 1228 | TEST_F(ServiceTest, SetEAPCredentialsOverRPC) { |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 1229 | scoped_refptr<ServiceWithMockOnEapCredentialsChanged> service( |
| 1230 | new ServiceWithMockOnEapCredentialsChanged(control_interface(), |
| 1231 | dispatcher(), |
| 1232 | metrics(), |
| 1233 | &mock_manager_)); |
Paul Stewart | 8142613 | 2012-05-16 10:05:10 -0700 | [diff] [blame] | 1234 | string eap_credential_properties[] = { |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 1235 | flimflam::kEapAnonymousIdentityProperty, |
Paul Stewart | 8142613 | 2012-05-16 10:05:10 -0700 | [diff] [blame] | 1236 | flimflam::kEAPCertIDProperty, |
| 1237 | flimflam::kEAPClientCertProperty, |
Paul Stewart | 8142613 | 2012-05-16 10:05:10 -0700 | [diff] [blame] | 1238 | flimflam::kEapIdentityProperty, |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 1239 | flimflam::kEAPKeyIDProperty, |
Paul Stewart | 8142613 | 2012-05-16 10:05:10 -0700 | [diff] [blame] | 1240 | flimflam::kEapPasswordProperty, |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 1241 | flimflam::kEAPPINProperty, |
| 1242 | flimflam::kEapPrivateKeyProperty, |
| 1243 | flimflam::kEapPrivateKeyPasswordProperty |
Paul Stewart | 8142613 | 2012-05-16 10:05:10 -0700 | [diff] [blame] | 1244 | }; |
| 1245 | string eap_non_credential_properties[] = { |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 1246 | flimflam::kEapCaCertIDProperty, |
| 1247 | flimflam::kEapCaCertNssProperty, |
Paul Stewart | 8142613 | 2012-05-16 10:05:10 -0700 | [diff] [blame] | 1248 | flimflam::kEAPEAPProperty, |
| 1249 | flimflam::kEapPhase2AuthProperty, |
Paul Stewart | 8142613 | 2012-05-16 10:05:10 -0700 | [diff] [blame] | 1250 | flimflam::kEapUseSystemCAsProperty |
| 1251 | }; |
| 1252 | // While this is not an 802.1x-based service, none of these property |
| 1253 | // changes should cause a call to set_eap(). |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 1254 | EXPECT_CALL(*service, OnEapCredentialsChanged()).Times(0); |
Paul Stewart | 8142613 | 2012-05-16 10:05:10 -0700 | [diff] [blame] | 1255 | for (size_t i = 0; i < arraysize(eap_credential_properties); ++i) |
| 1256 | service->OnPropertyChanged(eap_credential_properties[i]); |
| 1257 | for (size_t i = 0; i < arraysize(eap_non_credential_properties); ++i) |
| 1258 | service->OnPropertyChanged(eap_non_credential_properties[i]); |
Paul Stewart | adf79d8 | 2012-07-18 16:09:56 -0700 | [diff] [blame] | 1259 | service->OnPropertyChanged(flimflam::kEapKeyMgmtProperty); |
Paul Stewart | 8142613 | 2012-05-16 10:05:10 -0700 | [diff] [blame] | 1260 | |
| 1261 | service->set_is_8021x(true); |
| 1262 | |
| 1263 | // When this is an 802.1x-based service, set_eap should be called for |
| 1264 | // all credential-carrying properties. |
| 1265 | for (size_t i = 0; i < arraysize(eap_credential_properties); ++i) { |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 1266 | EXPECT_CALL(*service, OnEapCredentialsChanged()).Times(1); |
Paul Stewart | 8142613 | 2012-05-16 10:05:10 -0700 | [diff] [blame] | 1267 | service->OnPropertyChanged(eap_credential_properties[i]); |
| 1268 | Mock::VerifyAndClearExpectations(service.get()); |
| 1269 | } |
Paul Stewart | adf79d8 | 2012-07-18 16:09:56 -0700 | [diff] [blame] | 1270 | |
| 1271 | // The key management property is a special case. While not strictly |
| 1272 | // a credential, it can change which credentials are used. Therefore it |
| 1273 | // should also trigger a call to set_eap(); |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 1274 | EXPECT_CALL(*service, OnEapCredentialsChanged()).Times(1); |
Paul Stewart | adf79d8 | 2012-07-18 16:09:56 -0700 | [diff] [blame] | 1275 | service->OnPropertyChanged(flimflam::kEapKeyMgmtProperty); |
| 1276 | Mock::VerifyAndClearExpectations(service.get()); |
| 1277 | |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 1278 | EXPECT_CALL(*service, OnEapCredentialsChanged()).Times(0); |
Paul Stewart | 8142613 | 2012-05-16 10:05:10 -0700 | [diff] [blame] | 1279 | for (size_t i = 0; i < arraysize(eap_non_credential_properties); ++i) |
| 1280 | service->OnPropertyChanged(eap_non_credential_properties[i]); |
| 1281 | } |
| 1282 | |
Paul Stewart | bc6e739 | 2012-05-24 07:07:48 -0700 | [diff] [blame] | 1283 | TEST_F(ServiceTest, Certification) { |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 1284 | EXPECT_FALSE(service_->remote_certification_.size()); |
Paul Stewart | bc6e739 | 2012-05-24 07:07:48 -0700 | [diff] [blame] | 1285 | |
| 1286 | ScopedMockLog log; |
| 1287 | EXPECT_CALL(log, Log(logging::LOG_WARNING, _, |
| 1288 | HasSubstr("exceeds our maximum"))).Times(2); |
| 1289 | string kSubject("foo"); |
| 1290 | EXPECT_FALSE(service_->AddEAPCertification( |
| 1291 | kSubject, Service::kEAPMaxCertificationElements)); |
| 1292 | EXPECT_FALSE(service_->AddEAPCertification( |
| 1293 | kSubject, Service::kEAPMaxCertificationElements + 1)); |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 1294 | EXPECT_FALSE(service_->remote_certification_.size()); |
Paul Stewart | bc6e739 | 2012-05-24 07:07:48 -0700 | [diff] [blame] | 1295 | Mock::VerifyAndClearExpectations(&log); |
| 1296 | |
| 1297 | EXPECT_CALL(log, |
| 1298 | Log(logging::LOG_INFO, _, HasSubstr("Received certification"))) |
| 1299 | .Times(1); |
| 1300 | EXPECT_TRUE(service_->AddEAPCertification( |
| 1301 | kSubject, Service::kEAPMaxCertificationElements - 1)); |
| 1302 | Mock::VerifyAndClearExpectations(&log); |
| 1303 | EXPECT_EQ(Service::kEAPMaxCertificationElements, |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 1304 | service_->remote_certification_.size()); |
Paul Stewart | bc6e739 | 2012-05-24 07:07:48 -0700 | [diff] [blame] | 1305 | for (size_t i = 0; i < Service::kEAPMaxCertificationElements - 1; ++i) { |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 1306 | EXPECT_TRUE(service_->remote_certification_[i].empty()); |
Paul Stewart | bc6e739 | 2012-05-24 07:07:48 -0700 | [diff] [blame] | 1307 | } |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 1308 | EXPECT_EQ(kSubject, service_->remote_certification_[ |
Paul Stewart | bc6e739 | 2012-05-24 07:07:48 -0700 | [diff] [blame] | 1309 | Service::kEAPMaxCertificationElements - 1]); |
| 1310 | |
| 1311 | // Re-adding the same name in the same position should not generate a log. |
| 1312 | EXPECT_CALL(log, Log(_, _, _)).Times(0); |
| 1313 | EXPECT_TRUE(service_->AddEAPCertification( |
| 1314 | kSubject, Service::kEAPMaxCertificationElements - 1)); |
| 1315 | |
| 1316 | // Replacing the item should generate a log message. |
| 1317 | EXPECT_CALL(log, |
| 1318 | Log(logging::LOG_INFO, _, HasSubstr("Received certification"))) |
| 1319 | .Times(1); |
| 1320 | EXPECT_TRUE(service_->AddEAPCertification( |
| 1321 | kSubject + "x", Service::kEAPMaxCertificationElements - 1)); |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 1322 | |
| 1323 | service_->ClearEAPCertification(); |
| 1324 | EXPECT_TRUE(service_->remote_certification_.empty()); |
Paul Stewart | bc6e739 | 2012-05-24 07:07:48 -0700 | [diff] [blame] | 1325 | } |
| 1326 | |
Darin Petkov | c8d91e5 | 2013-01-21 11:43:47 +0100 | [diff] [blame] | 1327 | TEST_F(ServiceTest, NoteDisconnectEventIdle) { |
Paul Stewart | 483e472 | 2012-12-18 10:23:17 -0800 | [diff] [blame] | 1328 | Timestamp timestamp; |
Darin Petkov | c8d91e5 | 2013-01-21 11:43:47 +0100 | [diff] [blame] | 1329 | EXPECT_CALL(time_, GetNow()).Times(4).WillRepeatedly((Return(timestamp))); |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 1330 | SetStateField(Service::kStateOnline); |
Darin Petkov | 0857f8e | 2012-12-21 10:49:17 +0100 | [diff] [blame] | 1331 | EXPECT_FALSE(service_->HasRecentConnectionIssues()); |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 1332 | service_->SetState(Service::kStateIdle); |
Darin Petkov | c8d91e5 | 2013-01-21 11:43:47 +0100 | [diff] [blame] | 1333 | // The transition Online->Idle is not an event. |
Darin Petkov | 0857f8e | 2012-12-21 10:49:17 +0100 | [diff] [blame] | 1334 | EXPECT_FALSE(service_->HasRecentConnectionIssues()); |
Darin Petkov | c8d91e5 | 2013-01-21 11:43:47 +0100 | [diff] [blame] | 1335 | service_->SetState(Service::kStateFailure); |
| 1336 | // The transition Online->Idle->Failure is a connection drop. |
| 1337 | EXPECT_TRUE(service_->HasRecentConnectionIssues()); |
Darin Petkov | 0857f8e | 2012-12-21 10:49:17 +0100 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | TEST_F(ServiceTest, NoteDisconnectEventOnSetStateFailure) { |
| 1341 | Timestamp timestamp; |
| 1342 | EXPECT_CALL(time_, GetNow()).Times(3).WillRepeatedly((Return(timestamp))); |
| 1343 | SetStateField(Service::kStateOnline); |
| 1344 | EXPECT_FALSE(service_->HasRecentConnectionIssues()); |
| 1345 | service_->SetState(Service::kStateFailure); |
| 1346 | EXPECT_TRUE(service_->HasRecentConnectionIssues()); |
| 1347 | } |
| 1348 | |
| 1349 | TEST_F(ServiceTest, NoteDisconnectEventOnSetFailureSilent) { |
| 1350 | Timestamp timestamp; |
| 1351 | EXPECT_CALL(time_, GetNow()).Times(3).WillRepeatedly((Return(timestamp))); |
| 1352 | SetStateField(Service::kStateConfiguring); |
| 1353 | EXPECT_FALSE(service_->HasRecentConnectionIssues()); |
| 1354 | service_->SetFailureSilent(Service::kFailureEAPAuthentication); |
Paul Stewart | 483e472 | 2012-12-18 10:23:17 -0800 | [diff] [blame] | 1355 | EXPECT_TRUE(service_->HasRecentConnectionIssues()); |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | TEST_F(ServiceTest, NoteDisconnectEventNonEvent) { |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 1359 | EXPECT_CALL(time_, GetNow()).Times(0); |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 1360 | EXPECT_CALL(diagnostics_reporter_, OnConnectivityEvent()).Times(0); |
| 1361 | |
| 1362 | // Explicit disconnect is a non-event. |
| 1363 | SetStateField(Service::kStateOnline); |
| 1364 | SetExplicitlyDisconnected(true); |
| 1365 | NoteDisconnectEvent(); |
| 1366 | EXPECT_TRUE(GetDisconnects()->empty()); |
| 1367 | EXPECT_TRUE(GetMisconnects()->empty()); |
| 1368 | |
| 1369 | // Failure to idle transition is a non-event. |
| 1370 | SetStateField(Service::kStateFailure); |
| 1371 | SetExplicitlyDisconnected(false); |
| 1372 | NoteDisconnectEvent(); |
| 1373 | EXPECT_TRUE(GetDisconnects()->empty()); |
| 1374 | EXPECT_TRUE(GetMisconnects()->empty()); |
Darin Petkov | cb0b566 | 2012-12-13 09:59:44 +0100 | [diff] [blame] | 1375 | |
| 1376 | // Disconnect while manager is stopped is a non-event. |
| 1377 | SetStateField(Service::kStateOnline); |
| 1378 | SetManagerRunning(false); |
| 1379 | NoteDisconnectEvent(); |
| 1380 | EXPECT_TRUE(GetDisconnects()->empty()); |
| 1381 | EXPECT_TRUE(GetMisconnects()->empty()); |
| 1382 | |
| 1383 | // Disconnect while suspending is a non-event. |
| 1384 | SetManagerRunning(true); |
| 1385 | SetPowerState(PowerManager::kSuspending); |
| 1386 | NoteDisconnectEvent(); |
| 1387 | EXPECT_TRUE(GetDisconnects()->empty()); |
| 1388 | EXPECT_TRUE(GetMisconnects()->empty()); |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | TEST_F(ServiceTest, NoteDisconnectEventDisconnectOnce) { |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 1392 | const int kNow = 5; |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 1393 | EXPECT_FALSE(service_->explicitly_disconnected()); |
| 1394 | SetStateField(Service::kStateOnline); |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 1395 | EXPECT_CALL(time_, GetNow()).WillOnce(Return(GetTimestamp(kNow, ""))); |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 1396 | EXPECT_CALL(diagnostics_reporter_, OnConnectivityEvent()).Times(0); |
| 1397 | NoteDisconnectEvent(); |
| 1398 | ASSERT_EQ(1, GetDisconnects()->size()); |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 1399 | EXPECT_EQ(kNow, GetDisconnects()->front().monotonic.tv_sec); |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 1400 | EXPECT_TRUE(GetMisconnects()->empty()); |
Paul Stewart | 483e472 | 2012-12-18 10:23:17 -0800 | [diff] [blame] | 1401 | |
| 1402 | Mock::VerifyAndClearExpectations(&time_); |
| 1403 | EXPECT_CALL(time_, GetNow()).WillOnce(Return(GetTimestamp( |
| 1404 | kNow + GetDisconnectsMonitorSeconds() - 1, ""))); |
| 1405 | EXPECT_TRUE(service_->HasRecentConnectionIssues()); |
| 1406 | ASSERT_EQ(1, GetDisconnects()->size()); |
| 1407 | |
| 1408 | Mock::VerifyAndClearExpectations(&time_); |
| 1409 | EXPECT_CALL(time_, GetNow()).WillOnce(Return(GetTimestamp( |
| 1410 | kNow + GetDisconnectsMonitorSeconds(), ""))); |
| 1411 | EXPECT_FALSE(service_->HasRecentConnectionIssues()); |
| 1412 | ASSERT_TRUE(GetDisconnects()->empty()); |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | TEST_F(ServiceTest, NoteDisconnectEventDisconnectThreshold) { |
| 1416 | EXPECT_FALSE(service_->explicitly_disconnected()); |
| 1417 | SetStateField(Service::kStateOnline); |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 1418 | const int kNow = 6; |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 1419 | for (int i = 0; i < GetReportDisconnectsThreshold() - 1; i++) { |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 1420 | PushTimestamp(GetDisconnects(), kNow, ""); |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 1421 | } |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 1422 | EXPECT_CALL(time_, GetNow()).WillOnce(Return(GetTimestamp(kNow, ""))); |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 1423 | EXPECT_CALL(diagnostics_reporter_, OnConnectivityEvent()).Times(1); |
| 1424 | NoteDisconnectEvent(); |
| 1425 | EXPECT_EQ(GetReportDisconnectsThreshold(), GetDisconnects()->size()); |
| 1426 | } |
| 1427 | |
| 1428 | TEST_F(ServiceTest, NoteDisconnectEventMisconnectOnce) { |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 1429 | const int kNow = 7; |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 1430 | EXPECT_FALSE(service_->explicitly_disconnected()); |
| 1431 | SetStateField(Service::kStateConfiguring); |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 1432 | EXPECT_CALL(time_, GetNow()).WillOnce(Return(GetTimestamp(kNow, ""))); |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 1433 | EXPECT_CALL(diagnostics_reporter_, OnConnectivityEvent()).Times(0); |
| 1434 | NoteDisconnectEvent(); |
| 1435 | EXPECT_TRUE(GetDisconnects()->empty()); |
| 1436 | ASSERT_EQ(1, GetMisconnects()->size()); |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 1437 | EXPECT_EQ(kNow, GetMisconnects()->front().monotonic.tv_sec); |
Paul Stewart | 483e472 | 2012-12-18 10:23:17 -0800 | [diff] [blame] | 1438 | |
| 1439 | Mock::VerifyAndClearExpectations(&time_); |
| 1440 | EXPECT_CALL(time_, GetNow()).WillOnce(Return(GetTimestamp( |
| 1441 | kNow + GetMisconnectsMonitorSeconds() - 1, ""))); |
| 1442 | EXPECT_TRUE(service_->HasRecentConnectionIssues()); |
| 1443 | ASSERT_EQ(1, GetMisconnects()->size()); |
| 1444 | |
| 1445 | Mock::VerifyAndClearExpectations(&time_); |
| 1446 | EXPECT_CALL(time_, GetNow()).WillOnce(Return(GetTimestamp( |
| 1447 | kNow + GetMisconnectsMonitorSeconds(), ""))); |
| 1448 | EXPECT_FALSE(service_->HasRecentConnectionIssues()); |
| 1449 | ASSERT_TRUE(GetMisconnects()->empty()); |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 1450 | } |
| 1451 | |
| 1452 | TEST_F(ServiceTest, NoteDisconnectEventMisconnectThreshold) { |
| 1453 | EXPECT_FALSE(service_->explicitly_disconnected()); |
| 1454 | SetStateField(Service::kStateConfiguring); |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 1455 | const int kNow = 8; |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 1456 | for (int i = 0; i < GetReportMisconnectsThreshold() - 1; i++) { |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 1457 | PushTimestamp(GetMisconnects(), kNow, ""); |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 1458 | } |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 1459 | EXPECT_CALL(time_, GetNow()).WillOnce(Return(GetTimestamp(kNow, ""))); |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 1460 | EXPECT_CALL(diagnostics_reporter_, OnConnectivityEvent()).Times(1); |
| 1461 | NoteDisconnectEvent(); |
| 1462 | EXPECT_EQ(GetReportMisconnectsThreshold(), GetMisconnects()->size()); |
| 1463 | } |
| 1464 | |
| 1465 | TEST_F(ServiceTest, NoteDisconnectEventDiscardOld) { |
| 1466 | EXPECT_FALSE(service_->explicitly_disconnected()); |
| 1467 | EXPECT_CALL(diagnostics_reporter_, OnConnectivityEvent()).Times(0); |
| 1468 | for (int i = 0; i < 2; i++) { |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 1469 | int now = 0; |
| 1470 | deque<Timestamp> *events = NULL; |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 1471 | if (i == 0) { |
| 1472 | SetStateField(Service::kStateConnected); |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 1473 | now = GetDisconnectsMonitorSeconds() + 1; |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 1474 | events = GetDisconnects(); |
| 1475 | } else { |
| 1476 | SetStateField(Service::kStateAssociating); |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 1477 | now = GetMisconnectsMonitorSeconds() + 1; |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 1478 | events = GetMisconnects(); |
| 1479 | } |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 1480 | PushTimestamp(events, 0, ""); |
| 1481 | PushTimestamp(events, 0, ""); |
| 1482 | EXPECT_CALL(time_, GetNow()).WillOnce(Return(GetTimestamp(now, ""))); |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 1483 | NoteDisconnectEvent(); |
| 1484 | ASSERT_EQ(1, events->size()); |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 1485 | EXPECT_EQ(now, events->front().monotonic.tv_sec); |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 1486 | } |
| 1487 | } |
| 1488 | |
| 1489 | TEST_F(ServiceTest, NoteDisconnectEventDiscardExcessive) { |
| 1490 | EXPECT_FALSE(service_->explicitly_disconnected()); |
| 1491 | SetStateField(Service::kStateOnline); |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 1492 | for (int i = 0; i < 2 * GetMaxDisconnectEventHistory(); i++) { |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 1493 | PushTimestamp(GetDisconnects(), 0, ""); |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 1494 | } |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 1495 | EXPECT_CALL(time_, GetNow()).WillOnce(Return(Timestamp())); |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 1496 | EXPECT_CALL(diagnostics_reporter_, OnConnectivityEvent()).Times(1); |
| 1497 | NoteDisconnectEvent(); |
| 1498 | EXPECT_EQ(GetMaxDisconnectEventHistory(), GetDisconnects()->size()); |
| 1499 | } |
| 1500 | |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 1501 | TEST_F(ServiceTest, ConvertTimestampsToStrings) { |
| 1502 | EXPECT_TRUE(ExtractWallClockToStrings(deque<Timestamp>()).empty()); |
| 1503 | |
| 1504 | const Timestamp kValues[] = { |
Darin Petkov | 0857f8e | 2012-12-21 10:49:17 +0100 | [diff] [blame] | 1505 | GetTimestamp(123, "2012-12-09T12:41:22.123456+0100"), |
| 1506 | GetTimestamp(234, "2012-12-31T23:59:59.012345+0100") |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 1507 | }; |
| 1508 | Strings strings = |
| 1509 | ExtractWallClockToStrings( |
| 1510 | deque<Timestamp>(kValues, kValues + arraysize(kValues))); |
| 1511 | EXPECT_GT(arraysize(kValues), 0); |
| 1512 | ASSERT_EQ(arraysize(kValues), strings.size()); |
| 1513 | for (size_t i = 0; i < arraysize(kValues); i++) { |
| 1514 | EXPECT_EQ(kValues[i].wall_clock, strings[i]); |
| 1515 | } |
| 1516 | } |
| 1517 | |
| 1518 | TEST_F(ServiceTest, DiagnosticsProperties) { |
Darin Petkov | 0857f8e | 2012-12-21 10:49:17 +0100 | [diff] [blame] | 1519 | const string kWallClock0 = "2012-12-09T12:41:22.234567-0800"; |
| 1520 | const string kWallClock1 = "2012-12-31T23:59:59.345678-0800"; |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 1521 | Strings values; |
| 1522 | |
| 1523 | PushTimestamp(GetDisconnects(), 0, kWallClock0); |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 1524 | Error unused_error; |
| 1525 | ASSERT_TRUE(service_->store().GetStringsProperty( |
| 1526 | kDiagnosticsDisconnectsProperty, &values, &unused_error)); |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 1527 | ASSERT_EQ(1, values.size()); |
| 1528 | EXPECT_EQ(kWallClock0, values[0]); |
| 1529 | |
| 1530 | PushTimestamp(GetMisconnects(), 0, kWallClock1); |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 1531 | ASSERT_TRUE(service_->store().GetStringsProperty( |
| 1532 | kDiagnosticsMisconnectsProperty, &values, &unused_error)); |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 1533 | ASSERT_EQ(1, values.size()); |
| 1534 | EXPECT_EQ(kWallClock1, values[0]); |
| 1535 | } |
| 1536 | |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame] | 1537 | TEST_F(ServiceTest, SecurityLevel) { |
| 1538 | // Encrypted is better than not. |
| 1539 | service_->SetSecurity(Service::kCryptoNone, false, false); |
| 1540 | service2_->SetSecurity(Service::kCryptoRc4, false, false); |
| 1541 | EXPECT_GT(service2_->SecurityLevel(), service_->SecurityLevel()); |
| 1542 | |
| 1543 | // AES encryption is better than RC4 encryption. |
| 1544 | service_->SetSecurity(Service::kCryptoRc4, false, false); |
| 1545 | service2_->SetSecurity(Service::kCryptoAes, false, false); |
| 1546 | EXPECT_GT(service2_->SecurityLevel(), service_->SecurityLevel()); |
| 1547 | |
| 1548 | // Crypto algorithm is more important than key rotation. |
| 1549 | service_->SetSecurity(Service::kCryptoNone, true, false); |
| 1550 | service2_->SetSecurity(Service::kCryptoAes, false, false); |
| 1551 | EXPECT_GT(service2_->SecurityLevel(), service_->SecurityLevel()); |
| 1552 | |
| 1553 | // Encrypted-but-unauthenticated is better than clear-but-authenticated. |
| 1554 | service_->SetSecurity(Service::kCryptoNone, false, true); |
| 1555 | service2_->SetSecurity(Service::kCryptoAes, false, false); |
| 1556 | EXPECT_GT(service2_->SecurityLevel(), service_->SecurityLevel()); |
| 1557 | |
| 1558 | // For same encryption, prefer key rotation. |
| 1559 | service_->SetSecurity(Service::kCryptoRc4, false, false); |
| 1560 | service2_->SetSecurity(Service::kCryptoRc4, true, false); |
| 1561 | EXPECT_GT(service2_->SecurityLevel(), service_->SecurityLevel()); |
| 1562 | |
| 1563 | // For same encryption, prefer authenticated AP. |
| 1564 | service_->SetSecurity(Service::kCryptoRc4, false, false); |
| 1565 | service2_->SetSecurity(Service::kCryptoRc4, false, true); |
| 1566 | EXPECT_GT(service2_->SecurityLevel(), service_->SecurityLevel()); |
| 1567 | } |
| 1568 | |
Darin Petkov | aba8932 | 2013-03-11 14:48:22 +0100 | [diff] [blame] | 1569 | TEST_F(ServiceTest, SetErrorDetails) { |
| 1570 | EXPECT_EQ(Service::kErrorDetailsNone, service_->error_details()); |
| 1571 | static const char kDetails[] = "Certificate revoked."; |
| 1572 | ServiceMockAdaptor *adaptor = GetAdaptor(); |
| 1573 | EXPECT_CALL(*adaptor, EmitStringChanged(shill::kErrorDetailsProperty, |
| 1574 | kDetails)); |
| 1575 | service_->SetErrorDetails(Service::kErrorDetailsNone); |
| 1576 | EXPECT_EQ(Service::kErrorDetailsNone, service_->error_details()); |
| 1577 | service_->SetErrorDetails(kDetails); |
| 1578 | EXPECT_EQ(kDetails, service_->error_details()); |
| 1579 | service_->SetErrorDetails(kDetails); |
| 1580 | } |
| 1581 | |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 1582 | TEST_F(ServiceTest, SetAutoConnectFull) { |
Darin Petkov | 36d962d | 2013-03-25 13:03:14 +0100 | [diff] [blame] | 1583 | EXPECT_FALSE(service_->auto_connect()); |
| 1584 | Error error; |
| 1585 | EXPECT_FALSE(GetAutoConnect(&error)); |
| 1586 | EXPECT_TRUE(error.IsSuccess()); |
| 1587 | |
| 1588 | // false -> false |
| 1589 | EXPECT_CALL(mock_manager_, UpdateService(_)).Times(0); |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 1590 | SetAutoConnectFull(false, &error); |
Darin Petkov | 36d962d | 2013-03-25 13:03:14 +0100 | [diff] [blame] | 1591 | EXPECT_TRUE(error.IsSuccess()); |
| 1592 | EXPECT_FALSE(service_->auto_connect()); |
| 1593 | EXPECT_FALSE(GetAutoConnect(NULL)); |
| 1594 | Mock::VerifyAndClearExpectations(&mock_manager_); |
| 1595 | |
| 1596 | // false -> true |
| 1597 | EXPECT_CALL(mock_manager_, UpdateService(_)).Times(1); |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 1598 | SetAutoConnectFull(true, &error); |
Darin Petkov | 36d962d | 2013-03-25 13:03:14 +0100 | [diff] [blame] | 1599 | EXPECT_TRUE(error.IsSuccess()); |
| 1600 | EXPECT_TRUE(service_->auto_connect()); |
| 1601 | EXPECT_TRUE(GetAutoConnect(NULL)); |
| 1602 | Mock::VerifyAndClearExpectations(&mock_manager_); |
| 1603 | |
| 1604 | // true -> true |
| 1605 | EXPECT_CALL(mock_manager_, UpdateService(_)).Times(0); |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 1606 | SetAutoConnectFull(true, &error); |
Darin Petkov | 36d962d | 2013-03-25 13:03:14 +0100 | [diff] [blame] | 1607 | EXPECT_TRUE(error.IsSuccess()); |
| 1608 | EXPECT_TRUE(service_->auto_connect()); |
| 1609 | EXPECT_TRUE(GetAutoConnect(NULL)); |
| 1610 | Mock::VerifyAndClearExpectations(&mock_manager_); |
| 1611 | |
| 1612 | // true -> false |
| 1613 | EXPECT_CALL(mock_manager_, UpdateService(_)).Times(1); |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 1614 | SetAutoConnectFull(false, &error); |
Darin Petkov | 36d962d | 2013-03-25 13:03:14 +0100 | [diff] [blame] | 1615 | EXPECT_TRUE(error.IsSuccess()); |
| 1616 | EXPECT_FALSE(service_->auto_connect()); |
| 1617 | EXPECT_FALSE(GetAutoConnect(NULL)); |
| 1618 | Mock::VerifyAndClearExpectations(&mock_manager_); |
| 1619 | } |
| 1620 | |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 1621 | TEST_F(ServiceTest, PropertyChanges) { |
| 1622 | TestCommonPropertyChanges(service_, GetAdaptor()); |
| 1623 | TestAutoConnectPropertyChange(service_, GetAdaptor()); |
| 1624 | } |
| 1625 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 1626 | // Custom property setters should return false, and make no changes, if |
| 1627 | // the new value is the same as the old value. |
| 1628 | TEST_F(ServiceTest, CustomSetterNoopChange) { |
| 1629 | TestCustomSetterNoopChange(service_, &mock_manager_); |
| 1630 | } |
| 1631 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 1632 | } // namespace shill |