Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "shill/service.h" |
| 6 | |
| 7 | #include <map> |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 11 | #include <base/bind.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 12 | #include <chromeos/dbus/service_constants.h> |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 13 | #include <dbus-c++/dbus.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 14 | #include <gtest/gtest.h> |
| 15 | #include <gmock/gmock.h> |
| 16 | |
| 17 | #include "shill/dbus_adaptor.h" |
| 18 | #include "shill/ethernet_service.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 19 | #include "shill/event_dispatcher.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 20 | #include "shill/manager.h" |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 21 | #include "shill/mock_adaptors.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 22 | #include "shill/mock_control.h" |
Paul Stewart | 10241e3 | 2012-04-23 18:15:06 -0700 | [diff] [blame] | 23 | #include "shill/mock_connection.h" |
| 24 | #include "shill/mock_device_info.h" |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 25 | #include "shill/mock_manager.h" |
Paul Stewart | ff14b02 | 2012-04-24 20:06:23 -0700 | [diff] [blame] | 26 | #include "shill/mock_profile.h" |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 27 | #include "shill/mock_store.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 28 | #include "shill/property_store_unittest.h" |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 29 | #include "shill/service_under_test.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 30 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 31 | using base::Bind; |
| 32 | using base::Unretained; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 33 | using std::map; |
| 34 | using std::string; |
| 35 | using std::vector; |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 36 | using testing::_; |
| 37 | using testing::AtLeast; |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 38 | using testing::DoAll; |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 39 | using testing::NiceMock; |
| 40 | using testing::Return; |
Paul Stewart | 10241e3 | 2012-04-23 18:15:06 -0700 | [diff] [blame] | 41 | using testing::ReturnRef; |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 42 | using testing::StrictMock; |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 43 | using testing::SetArgumentPointee; |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 44 | using testing::Test; |
Paul Stewart | 9f32d19 | 2012-01-30 20:37:50 -0800 | [diff] [blame] | 45 | using testing::Values; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 46 | |
| 47 | namespace shill { |
| 48 | |
| 49 | class ServiceTest : public PropertyStoreTest { |
| 50 | public: |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 51 | ServiceTest() |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 52 | : mock_manager_(control_interface(), dispatcher(), metrics(), glib()), |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 53 | service_(new ServiceUnderTest(control_interface(), |
| 54 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 55 | metrics(), |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 56 | &mock_manager_)), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 57 | storage_id_(ServiceUnderTest::kStorageId) { |
| 58 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 59 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 60 | virtual ~ServiceTest() {} |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 61 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 62 | MOCK_METHOD1(TestCallback, void(const Error &error)); |
| 63 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 64 | protected: |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 65 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 66 | MockManager mock_manager_; |
| 67 | scoped_refptr<ServiceUnderTest> service_; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 68 | string storage_id_; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 71 | TEST_F(ServiceTest, Constructor) { |
| 72 | EXPECT_TRUE(service_->save_credentials_); |
| 73 | EXPECT_EQ(Service::kCheckPortalAuto, service_->check_portal_); |
Eric Shienbrood | cc95c5d | 2012-03-30 15:25:49 -0400 | [diff] [blame] | 74 | EXPECT_EQ(Service::kStateIdle, service_->state()); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 77 | TEST_F(ServiceTest, GetProperties) { |
| 78 | map<string, ::DBus::Variant> props; |
| 79 | Error error(Error::kInvalidProperty, ""); |
| 80 | { |
| 81 | ::DBus::Error dbus_error; |
| 82 | string expected("portal_list"); |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 83 | service_->mutable_store()->SetStringProperty(flimflam::kCheckPortalProperty, |
| 84 | expected, |
| 85 | &error); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 86 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 87 | ASSERT_FALSE(props.find(flimflam::kCheckPortalProperty) == props.end()); |
| 88 | EXPECT_EQ(props[flimflam::kCheckPortalProperty].reader().get_string(), |
| 89 | expected); |
| 90 | } |
| 91 | { |
| 92 | ::DBus::Error dbus_error; |
| 93 | bool expected = true; |
Thieu Le | 284fe79 | 2012-01-31 17:53:19 -0800 | [diff] [blame] | 94 | service_->set_favorite(true); |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 95 | service_->mutable_store()->SetBoolProperty(flimflam::kAutoConnectProperty, |
| 96 | expected, |
| 97 | &error); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 98 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 99 | ASSERT_FALSE(props.find(flimflam::kAutoConnectProperty) == props.end()); |
| 100 | EXPECT_EQ(props[flimflam::kAutoConnectProperty].reader().get_bool(), |
| 101 | expected); |
| 102 | } |
| 103 | { |
| 104 | ::DBus::Error dbus_error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 105 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 106 | ASSERT_FALSE(props.find(flimflam::kConnectableProperty) == props.end()); |
| 107 | EXPECT_EQ(props[flimflam::kConnectableProperty].reader().get_bool(), false); |
| 108 | } |
| 109 | { |
| 110 | ::DBus::Error dbus_error; |
| 111 | int32 expected = 127; |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 112 | service_->mutable_store()->SetInt32Property(flimflam::kPriorityProperty, |
| 113 | expected, |
| 114 | &error); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 115 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 116 | ASSERT_FALSE(props.find(flimflam::kPriorityProperty) == props.end()); |
| 117 | EXPECT_EQ(props[flimflam::kPriorityProperty].reader().get_int32(), |
| 118 | expected); |
| 119 | } |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 120 | { |
| 121 | ::DBus::Error dbus_error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 122 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 123 | ASSERT_FALSE(props.find(flimflam::kDeviceProperty) == props.end()); |
Jason Glasgow | acdc11f | 2012-03-30 14:12:22 -0400 | [diff] [blame] | 124 | EXPECT_EQ(props[flimflam::kDeviceProperty].reader().get_path(), |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 125 | string(ServiceUnderTest::kRpcId)); |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 126 | } |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 127 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 128 | |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 129 | TEST_F(ServiceTest, SetProperty) { |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 130 | { |
| 131 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 132 | EXPECT_TRUE(DBusAdaptor::SetProperty(service_->mutable_store(), |
| 133 | flimflam::kSaveCredentialsProperty, |
| 134 | PropertyStoreTest::kBoolV, |
| 135 | &error)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 136 | } |
| 137 | { |
| 138 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 139 | EXPECT_TRUE(DBusAdaptor::SetProperty(service_->mutable_store(), |
| 140 | flimflam::kPriorityProperty, |
| 141 | PropertyStoreTest::kInt32V, |
| 142 | &error)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 143 | } |
| 144 | { |
| 145 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 146 | EXPECT_TRUE(DBusAdaptor::SetProperty(service_->mutable_store(), |
| 147 | flimflam::kEAPEAPProperty, |
| 148 | PropertyStoreTest::kStringV, |
| 149 | &error)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 150 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 151 | // 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] | 152 | { |
| 153 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 154 | EXPECT_FALSE(DBusAdaptor::SetProperty(service_->mutable_store(), |
| 155 | flimflam::kFavoriteProperty, |
| 156 | PropertyStoreTest::kBoolV, |
| 157 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 158 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 159 | } |
Thieu Le | 284fe79 | 2012-01-31 17:53:19 -0800 | [diff] [blame] | 160 | { |
| 161 | ::DBus::Error error; |
| 162 | service_->set_favorite(true); |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 163 | EXPECT_TRUE(DBusAdaptor::SetProperty(service_->mutable_store(), |
| 164 | flimflam::kAutoConnectProperty, |
| 165 | PropertyStoreTest::kBoolV, |
| 166 | &error)); |
Thieu Le | 284fe79 | 2012-01-31 17:53:19 -0800 | [diff] [blame] | 167 | } |
| 168 | { |
| 169 | ::DBus::Error error; |
| 170 | service_->set_favorite(false); |
Wade Guthrie | 4f28e8b | 2012-04-11 10:52:07 -0700 | [diff] [blame] | 171 | EXPECT_TRUE(DBusAdaptor::SetProperty(service_->mutable_store(), |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 172 | flimflam::kAutoConnectProperty, |
| 173 | PropertyStoreTest::kBoolV, |
| 174 | &error)); |
Thieu Le | 284fe79 | 2012-01-31 17:53:19 -0800 | [diff] [blame] | 175 | } |
Paul Stewart | 0c43833 | 2012-04-11 07:55:27 -0700 | [diff] [blame] | 176 | // Ensure that we can perform a trivial set of the Name property (to its |
| 177 | // current value) but an attempt to set the property to a different value |
| 178 | // fails. |
| 179 | { |
| 180 | ::DBus::Error error; |
| 181 | EXPECT_TRUE(DBusAdaptor::SetProperty(service_->mutable_store(), |
| 182 | flimflam::kNameProperty, |
| 183 | DBusAdaptor::StringToVariant( |
| 184 | service_->friendly_name()), |
| 185 | &error)); |
| 186 | } |
| 187 | { |
| 188 | ::DBus::Error error; |
| 189 | EXPECT_FALSE(DBusAdaptor::SetProperty(service_->mutable_store(), |
| 190 | flimflam::kNameProperty, |
| 191 | PropertyStoreTest::kStringV, |
| 192 | &error)); |
| 193 | EXPECT_EQ(invalid_args(), error.name()); |
| 194 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 197 | TEST_F(ServiceTest, Load) { |
| 198 | NiceMock<MockStore> storage; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 199 | EXPECT_CALL(storage, ContainsGroup(storage_id_)).WillOnce(Return(true)); |
| 200 | EXPECT_CALL(storage, GetString(storage_id_, _, _)) |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 201 | .Times(AtLeast(1)) |
| 202 | .WillRepeatedly(Return(true)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 203 | EXPECT_TRUE(service_->Load(&storage)); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | TEST_F(ServiceTest, LoadFail) { |
| 207 | StrictMock<MockStore> storage; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 208 | EXPECT_CALL(storage, ContainsGroup(storage_id_)).WillOnce(Return(false)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 209 | EXPECT_FALSE(service_->Load(&storage)); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | TEST_F(ServiceTest, SaveString) { |
| 213 | MockStore storage; |
| 214 | static const char kKey[] = "test-key"; |
| 215 | static const char kData[] = "test-data"; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 216 | EXPECT_CALL(storage, SetString(storage_id_, kKey, kData)) |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 217 | .WillOnce(Return(true)); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 218 | service_->SaveString(&storage, storage_id_, kKey, kData, false, true); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | TEST_F(ServiceTest, SaveStringCrypted) { |
| 222 | MockStore storage; |
| 223 | static const char kKey[] = "test-key"; |
| 224 | static const char kData[] = "test-data"; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 225 | EXPECT_CALL(storage, SetCryptedString(storage_id_, kKey, kData)) |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 226 | .WillOnce(Return(true)); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 227 | service_->SaveString(&storage, storage_id_, kKey, kData, true, true); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | TEST_F(ServiceTest, SaveStringDontSave) { |
| 231 | MockStore storage; |
| 232 | static const char kKey[] = "test-key"; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 233 | EXPECT_CALL(storage, DeleteKey(storage_id_, kKey)) |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 234 | .WillOnce(Return(true)); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 235 | service_->SaveString(&storage, storage_id_, kKey, "data", false, false); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | TEST_F(ServiceTest, SaveStringEmpty) { |
| 239 | MockStore storage; |
| 240 | static const char kKey[] = "test-key"; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 241 | EXPECT_CALL(storage, DeleteKey(storage_id_, kKey)) |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 242 | .WillOnce(Return(true)); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 243 | service_->SaveString(&storage, storage_id_, kKey, "", true, true); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | TEST_F(ServiceTest, Save) { |
| 247 | NiceMock<MockStore> storage; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 248 | EXPECT_CALL(storage, SetString(storage_id_, _, _)) |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 249 | .Times(AtLeast(1)) |
| 250 | .WillRepeatedly(Return(true)); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 251 | EXPECT_CALL(storage, DeleteKey(storage_id_, _)) |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 252 | .Times(AtLeast(1)) |
| 253 | .WillRepeatedly(Return(true)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 254 | EXPECT_TRUE(service_->Save(&storage)); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 257 | TEST_F(ServiceTest, Unload) { |
| 258 | NiceMock<MockStore> storage; |
| 259 | EXPECT_CALL(storage, ContainsGroup(storage_id_)).WillOnce(Return(true)); |
| 260 | static const string string_value("value"); |
| 261 | EXPECT_CALL(storage, GetString(storage_id_, _, _)) |
| 262 | .Times(AtLeast(1)) |
| 263 | .WillRepeatedly(DoAll(SetArgumentPointee<2>(string_value), Return(true))); |
| 264 | ASSERT_TRUE(service_->Load(&storage)); |
| 265 | // TODO(pstew): A single string property in the service is tested as |
| 266 | // a sentinel that properties are being set and reset at the rit times. |
| 267 | // However, since property load/store is essentially a manual process, |
| 268 | // it is error prone and should either be exhaustively unit-tested or |
| 269 | // a generic framework for registering loaded/stored properties should |
| 270 | // be created. crosbug.com/24859 |
| 271 | EXPECT_EQ(string_value, service_->ui_data_); |
| 272 | service_->Unload(); |
| 273 | EXPECT_EQ(string(""), service_->ui_data_); |
| 274 | } |
| 275 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 276 | TEST_F(ServiceTest, State) { |
Eric Shienbrood | cc95c5d | 2012-03-30 15:25:49 -0400 | [diff] [blame] | 277 | EXPECT_EQ(Service::kStateIdle, service_->state()); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 278 | EXPECT_EQ(Service::kFailureUnknown, service_->failure()); |
| 279 | |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 280 | ServiceRefPtr service_ref(service_); |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 281 | |
Eric Shienbrood | cc95c5d | 2012-03-30 15:25:49 -0400 | [diff] [blame] | 282 | EXPECT_CALL(*dynamic_cast<ServiceMockAdaptor *>(service_->adaptor_.get()), |
| 283 | EmitStringChanged(flimflam::kStateProperty, _)).Times(5); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 284 | EXPECT_CALL(mock_manager_, UpdateService(service_ref)); |
| 285 | service_->SetState(Service::kStateConnected); |
| 286 | // A second state change shouldn't cause another update |
| 287 | service_->SetState(Service::kStateConnected); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 288 | EXPECT_EQ(Service::kStateConnected, service_->state()); |
| 289 | EXPECT_EQ(Service::kFailureUnknown, service_->failure()); |
mukesh agrawal | 568b5c6 | 2012-02-28 14:44:47 -0800 | [diff] [blame] | 290 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 291 | EXPECT_CALL(mock_manager_, UpdateService(service_ref)); |
| 292 | service_->SetState(Service::kStateDisconnected); |
| 293 | |
| 294 | EXPECT_CALL(mock_manager_, UpdateService(service_ref)); |
| 295 | service_->SetFailure(Service::kFailureOutOfRange); |
mukesh agrawal | 568b5c6 | 2012-02-28 14:44:47 -0800 | [diff] [blame] | 296 | EXPECT_TRUE(service_->IsFailed()); |
| 297 | EXPECT_GT(service_->failed_time_, 0); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 298 | EXPECT_EQ(Service::kStateFailure, service_->state()); |
| 299 | EXPECT_EQ(Service::kFailureOutOfRange, service_->failure()); |
mukesh agrawal | 568b5c6 | 2012-02-28 14:44:47 -0800 | [diff] [blame] | 300 | |
| 301 | EXPECT_CALL(mock_manager_, UpdateService(service_ref)); |
| 302 | service_->SetState(Service::kStateConnected); |
| 303 | EXPECT_FALSE(service_->IsFailed()); |
| 304 | EXPECT_EQ(service_->failed_time_, 0); |
| 305 | |
| 306 | EXPECT_CALL(mock_manager_, UpdateService(service_ref)); |
| 307 | service_->SetFailureSilent(Service::kFailurePinMissing); |
| 308 | EXPECT_TRUE(service_->IsFailed()); |
| 309 | EXPECT_GT(service_->failed_time_, 0); |
| 310 | EXPECT_EQ(Service::kStateIdle, service_->state()); |
| 311 | EXPECT_EQ(Service::kFailurePinMissing, service_->failure()); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Darin Petkov | b100ae7 | 2011-08-24 16:19:45 -0700 | [diff] [blame] | 314 | TEST_F(ServiceTest, ActivateCellularModem) { |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 315 | ResultCallback callback = |
| 316 | Bind(&ServiceTest::TestCallback, Unretained(this)); |
| 317 | EXPECT_CALL(*this, TestCallback(_)).Times(0); |
Darin Petkov | b100ae7 | 2011-08-24 16:19:45 -0700 | [diff] [blame] | 318 | Error error; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 319 | service_->ActivateCellularModem("Carrier", &error, callback); |
| 320 | EXPECT_TRUE(error.IsFailure()); |
Darin Petkov | b100ae7 | 2011-08-24 16:19:45 -0700 | [diff] [blame] | 321 | } |
| 322 | |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 323 | TEST_F(ServiceTest, MakeFavorite) { |
| 324 | EXPECT_FALSE(service_->favorite()); |
| 325 | EXPECT_FALSE(service_->auto_connect()); |
| 326 | |
| 327 | service_->MakeFavorite(); |
| 328 | EXPECT_TRUE(service_->favorite()); |
| 329 | EXPECT_TRUE(service_->auto_connect()); |
| 330 | } |
| 331 | |
| 332 | TEST_F(ServiceTest, ReMakeFavorite) { |
| 333 | service_->MakeFavorite(); |
| 334 | EXPECT_TRUE(service_->favorite()); |
| 335 | EXPECT_TRUE(service_->auto_connect()); |
| 336 | |
| 337 | service_->set_auto_connect(false); |
| 338 | service_->MakeFavorite(); |
| 339 | EXPECT_TRUE(service_->favorite()); |
| 340 | EXPECT_FALSE(service_->auto_connect()); |
| 341 | } |
| 342 | |
mukesh agrawal | 76d1388 | 2012-01-12 15:23:11 -0800 | [diff] [blame] | 343 | TEST_F(ServiceTest, IsAutoConnectable) { |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 344 | const char *reason; |
mukesh agrawal | 76d1388 | 2012-01-12 15:23:11 -0800 | [diff] [blame] | 345 | service_->set_connectable(true); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 346 | EXPECT_TRUE(service_->IsAutoConnectable(&reason)); |
mukesh agrawal | 76d1388 | 2012-01-12 15:23:11 -0800 | [diff] [blame] | 347 | |
mukesh agrawal | adb6848 | 2012-01-17 16:31:51 -0800 | [diff] [blame] | 348 | // We should not auto-connect to a Service that a user has |
| 349 | // deliberately disconnected. |
| 350 | Error error; |
| 351 | service_->Disconnect(&error); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 352 | EXPECT_FALSE(service_->IsAutoConnectable(&reason)); |
| 353 | EXPECT_STREQ(Service::kAutoConnExplicitDisconnect, reason); |
mukesh agrawal | adb6848 | 2012-01-17 16:31:51 -0800 | [diff] [blame] | 354 | |
| 355 | // But if the Service is reloaded, it is eligible for auto-connect |
| 356 | // again. |
| 357 | NiceMock<MockStore> storage; |
| 358 | EXPECT_CALL(storage, ContainsGroup(storage_id_)).WillOnce(Return(true)); |
| 359 | EXPECT_TRUE(service_->Load(&storage)); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 360 | EXPECT_TRUE(service_->IsAutoConnectable(&reason)); |
mukesh agrawal | adb6848 | 2012-01-17 16:31:51 -0800 | [diff] [blame] | 361 | |
| 362 | // A deliberate Connect should also re-enable auto-connect. |
| 363 | service_->Disconnect(&error); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 364 | EXPECT_FALSE(service_->IsAutoConnectable(&reason)); |
mukesh agrawal | adb6848 | 2012-01-17 16:31:51 -0800 | [diff] [blame] | 365 | service_->Connect(&error); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 366 | EXPECT_TRUE(service_->IsAutoConnectable(&reason)); |
mukesh agrawal | adb6848 | 2012-01-17 16:31:51 -0800 | [diff] [blame] | 367 | |
| 368 | // TODO(quiche): After we have resume handling in place, test that |
| 369 | // we re-enable auto-connect on resume. crosbug.com/25213 |
| 370 | |
mukesh agrawal | 76d1388 | 2012-01-12 15:23:11 -0800 | [diff] [blame] | 371 | service_->SetState(Service::kStateConnected); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 372 | EXPECT_FALSE(service_->IsAutoConnectable(&reason)); |
| 373 | EXPECT_STREQ(Service::kAutoConnConnected, reason); |
mukesh agrawal | 76d1388 | 2012-01-12 15:23:11 -0800 | [diff] [blame] | 374 | |
| 375 | service_->SetState(Service::kStateAssociating); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 376 | EXPECT_FALSE(service_->IsAutoConnectable(&reason)); |
| 377 | EXPECT_STREQ(Service::kAutoConnConnecting, reason); |
mukesh agrawal | 76d1388 | 2012-01-12 15:23:11 -0800 | [diff] [blame] | 378 | } |
| 379 | |
Paul Stewart | cb59fed | 2012-03-21 21:14:46 -0700 | [diff] [blame] | 380 | TEST_F(ServiceTest, ConfigureBadProperty) { |
| 381 | KeyValueStore args; |
| 382 | args.SetString("XXXInvalid", "Value"); |
| 383 | Error error; |
| 384 | service_->Configure(args, &error); |
| 385 | EXPECT_FALSE(error.IsSuccess()); |
| 386 | } |
| 387 | |
| 388 | TEST_F(ServiceTest, ConfigureBoolProperty) { |
| 389 | service_->MakeFavorite(); |
| 390 | service_->set_auto_connect(false); |
| 391 | ASSERT_FALSE(service_->auto_connect()); |
| 392 | KeyValueStore args; |
| 393 | args.SetBool(flimflam::kAutoConnectProperty, true); |
| 394 | Error error; |
| 395 | service_->Configure(args, &error); |
| 396 | EXPECT_TRUE(error.IsSuccess()); |
| 397 | EXPECT_TRUE(service_->auto_connect()); |
| 398 | } |
| 399 | |
| 400 | TEST_F(ServiceTest, ConfigureStringProperty) { |
| 401 | const string kEAPManagement0 = "management_zero"; |
| 402 | const string kEAPManagement1 = "management_one"; |
| 403 | service_->SetEAPKeyManagement(kEAPManagement0); |
| 404 | ASSERT_EQ(kEAPManagement0, service_->GetEAPKeyManagement()); |
| 405 | KeyValueStore args; |
| 406 | args.SetString(flimflam::kEapKeyMgmtProperty, kEAPManagement1); |
| 407 | Error error; |
| 408 | service_->Configure(args, &error); |
| 409 | EXPECT_TRUE(error.IsSuccess()); |
| 410 | EXPECT_EQ(kEAPManagement1, service_->GetEAPKeyManagement()); |
| 411 | } |
| 412 | |
| 413 | TEST_F(ServiceTest, ConfigureIgnoredProperty) { |
| 414 | service_->MakeFavorite(); |
| 415 | service_->set_auto_connect(false); |
| 416 | ASSERT_FALSE(service_->auto_connect()); |
| 417 | KeyValueStore args; |
| 418 | args.SetBool(flimflam::kAutoConnectProperty, true); |
| 419 | Error error; |
| 420 | service_->IgnoreParameterForConfigure(flimflam::kAutoConnectProperty); |
| 421 | service_->Configure(args, &error); |
| 422 | EXPECT_TRUE(error.IsSuccess()); |
| 423 | EXPECT_FALSE(service_->auto_connect()); |
| 424 | } |
| 425 | |
Paul Stewart | ff14b02 | 2012-04-24 20:06:23 -0700 | [diff] [blame] | 426 | TEST_F(ServiceTest, OnPropertyChanged) { |
| 427 | scoped_refptr<MockProfile> profile( |
| 428 | new StrictMock<MockProfile>(control_interface(), manager())); |
| 429 | service_->set_profile(NULL); |
| 430 | // Expect no crash. |
| 431 | service_->OnPropertyChanged(""); |
| 432 | |
| 433 | // Expect no call to Update if the profile has no storage. |
| 434 | service_->set_profile(profile); |
| 435 | EXPECT_CALL(*profile, UpdateService(_)).Times(0); |
| 436 | EXPECT_CALL(*profile, GetConstStorage()) |
| 437 | .WillOnce(Return(reinterpret_cast<StoreInterface *>(NULL))); |
| 438 | service_->OnPropertyChanged(""); |
| 439 | |
| 440 | // Expect call to Update if the profile has storage. |
| 441 | EXPECT_CALL(*profile, UpdateService(_)).Times(1); |
| 442 | NiceMock<MockStore> storage; |
| 443 | EXPECT_CALL(*profile, GetConstStorage()).WillOnce(Return(&storage)); |
| 444 | service_->OnPropertyChanged(""); |
| 445 | } |
| 446 | |
Paul Stewart | 9f32d19 | 2012-01-30 20:37:50 -0800 | [diff] [blame] | 447 | // Make sure a property is registered as a write only property |
| 448 | // by reading and comparing all string properties returned on the store. |
| 449 | // Subtle: We need to convert the test argument back and forth between |
| 450 | // string and ::DBus::Variant because this is the parameter type that |
| 451 | // our supeclass (PropertyStoreTest) is declared with. |
| 452 | class ReadOnlyServicePropertyTest : public ServiceTest {}; |
| 453 | TEST_P(ReadOnlyServicePropertyTest, PropertyWriteOnly) { |
| 454 | ReadablePropertyConstIterator<string> it = |
| 455 | (service_->store()).GetStringPropertiesIter(); |
| 456 | string property(GetParam().reader().get_string()); |
| 457 | for( ; !it.AtEnd(); it.Advance()) |
| 458 | EXPECT_NE(it.Key(), property); |
| 459 | } |
| 460 | |
| 461 | INSTANTIATE_TEST_CASE_P( |
| 462 | ReadOnlyServicePropertyTestInstance, |
| 463 | ReadOnlyServicePropertyTest, |
| 464 | Values( |
| 465 | DBusAdaptor::StringToVariant(flimflam::kEapPrivateKeyPasswordProperty), |
| 466 | DBusAdaptor::StringToVariant(flimflam::kEapPasswordProperty))); |
| 467 | |
Paul Stewart | 10241e3 | 2012-04-23 18:15:06 -0700 | [diff] [blame] | 468 | |
| 469 | TEST_F(ServiceTest, GetIPConfigRpcIdentifier) { |
| 470 | { |
| 471 | Error error; |
| 472 | EXPECT_EQ("/", service_->GetIPConfigRpcIdentifier(&error)); |
| 473 | EXPECT_EQ(Error::kNotFound, error.type()); |
| 474 | } |
| 475 | |
| 476 | scoped_ptr<MockDeviceInfo> mock_device_info( |
| 477 | new NiceMock<MockDeviceInfo>(control_interface(), dispatcher(), metrics(), |
| 478 | &mock_manager_)); |
| 479 | scoped_refptr<MockConnection> mock_connection( |
| 480 | new NiceMock<MockConnection>(mock_device_info.get())); |
| 481 | |
| 482 | service_->connection_ = mock_connection; |
| 483 | |
| 484 | { |
| 485 | Error error; |
| 486 | const string empty_string; |
| 487 | EXPECT_CALL(*mock_connection, ipconfig_rpc_identifier()) |
| 488 | .WillOnce(ReturnRef(empty_string)); |
| 489 | EXPECT_EQ("/", service_->GetIPConfigRpcIdentifier(&error)); |
| 490 | EXPECT_EQ(Error::kNotFound, error.type()); |
| 491 | } |
| 492 | |
| 493 | { |
| 494 | Error error; |
| 495 | const string nonempty_string("/ipconfig/path"); |
| 496 | EXPECT_CALL(*mock_connection, ipconfig_rpc_identifier()) |
| 497 | .WillOnce(ReturnRef(nonempty_string)); |
| 498 | EXPECT_EQ(nonempty_string, service_->GetIPConfigRpcIdentifier(&error)); |
| 499 | EXPECT_EQ(Error::kSuccess, error.type()); |
| 500 | } |
| 501 | |
| 502 | // Assure orderly destruction of the Connection before DeviceInfo. |
| 503 | service_->connection_ = NULL; |
| 504 | mock_connection = NULL; |
| 505 | mock_device_info.reset(); |
| 506 | } |
| 507 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 508 | } // namespace shill |