Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 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 | |
| 11 | #include <dbus-c++/dbus.h> |
| 12 | #include <chromeos/dbus/service_constants.h> |
| 13 | #include <gtest/gtest.h> |
| 14 | #include <gmock/gmock.h> |
| 15 | |
| 16 | #include "shill/dbus_adaptor.h" |
| 17 | #include "shill/ethernet_service.h" |
| 18 | #include "shill/manager.h" |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 19 | #include "shill/mock_adaptors.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 20 | #include "shill/mock_control.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 21 | #include "shill/mock_profile.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 22 | #include "shill/mock_service.h" |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame^] | 23 | #include "shill/mock_store.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 24 | #include "shill/property_store_unittest.h" |
| 25 | #include "shill/shill_event.h" |
| 26 | |
| 27 | using std::map; |
| 28 | using std::string; |
| 29 | using std::vector; |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame^] | 30 | using testing::_; |
| 31 | using testing::AtLeast; |
| 32 | using testing::NiceMock; |
| 33 | using testing::Return; |
| 34 | using testing::StrictMock; |
| 35 | using testing::Test; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 36 | |
| 37 | namespace shill { |
| 38 | |
| 39 | class ServiceTest : public PropertyStoreTest { |
| 40 | public: |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 41 | static const char kMockServiceName[]; |
| 42 | static const char kMockDeviceRpcId[]; |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 43 | static const char kProfileName[]; |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 44 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 45 | ServiceTest() |
| 46 | : service_(new MockService(&control_interface_, |
| 47 | &dispatcher_, |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 48 | new MockProfile(&control_interface_, &glib_), |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 49 | kMockServiceName)) { |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 52 | virtual ~ServiceTest() {} |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 53 | |
| 54 | protected: |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 55 | scoped_refptr<MockService> service_; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 58 | const char ServiceTest::kMockServiceName[] = "mock-service"; |
| 59 | |
| 60 | const char ServiceTest::kMockDeviceRpcId[] = "mock-device-rpc"; |
| 61 | |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 62 | const char ServiceTest::kProfileName[] = "profile"; |
| 63 | |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame^] | 64 | TEST_F(ServiceTest, Constructor) { |
| 65 | EXPECT_TRUE(service_->save_credentials_); |
| 66 | EXPECT_EQ(Service::kCheckPortalAuto, service_->check_portal_); |
| 67 | } |
| 68 | |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 69 | TEST_F(ServiceTest, GetProperties) { |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 70 | EXPECT_CALL(*service_.get(), CalculateState()).WillRepeatedly(Return("")); |
| 71 | EXPECT_CALL(*service_.get(), GetDeviceRpcId()) |
| 72 | .WillRepeatedly(Return(ServiceTest::kMockDeviceRpcId)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 73 | map<string, ::DBus::Variant> props; |
| 74 | Error error(Error::kInvalidProperty, ""); |
| 75 | { |
| 76 | ::DBus::Error dbus_error; |
| 77 | string expected("portal_list"); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 78 | service_->store()->SetStringProperty(flimflam::kCheckPortalProperty, |
| 79 | expected, |
| 80 | &error); |
| 81 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 82 | ASSERT_FALSE(props.find(flimflam::kCheckPortalProperty) == props.end()); |
| 83 | EXPECT_EQ(props[flimflam::kCheckPortalProperty].reader().get_string(), |
| 84 | expected); |
| 85 | } |
| 86 | { |
| 87 | ::DBus::Error dbus_error; |
| 88 | bool expected = true; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 89 | service_->store()->SetBoolProperty(flimflam::kAutoConnectProperty, |
| 90 | expected, |
| 91 | &error); |
| 92 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 93 | ASSERT_FALSE(props.find(flimflam::kAutoConnectProperty) == props.end()); |
| 94 | EXPECT_EQ(props[flimflam::kAutoConnectProperty].reader().get_bool(), |
| 95 | expected); |
| 96 | } |
| 97 | { |
| 98 | ::DBus::Error dbus_error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 99 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 100 | ASSERT_FALSE(props.find(flimflam::kConnectableProperty) == props.end()); |
| 101 | EXPECT_EQ(props[flimflam::kConnectableProperty].reader().get_bool(), false); |
| 102 | } |
| 103 | { |
| 104 | ::DBus::Error dbus_error; |
| 105 | int32 expected = 127; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 106 | service_->store()->SetInt32Property(flimflam::kPriorityProperty, |
| 107 | expected, |
| 108 | &error); |
| 109 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 110 | ASSERT_FALSE(props.find(flimflam::kPriorityProperty) == props.end()); |
| 111 | EXPECT_EQ(props[flimflam::kPriorityProperty].reader().get_int32(), |
| 112 | expected); |
| 113 | } |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 114 | { |
| 115 | ::DBus::Error dbus_error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 116 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 117 | ASSERT_FALSE(props.find(flimflam::kDeviceProperty) == props.end()); |
| 118 | EXPECT_EQ(props[flimflam::kDeviceProperty].reader().get_string(), |
| 119 | string(ServiceTest::kMockDeviceRpcId)); |
| 120 | } |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 121 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 122 | |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 123 | TEST_F(ServiceTest, Dispatch) { |
| 124 | { |
| 125 | ::DBus::Error error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 126 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(service_->store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 127 | flimflam::kSaveCredentialsProperty, |
| 128 | PropertyStoreTest::kBoolV, |
| 129 | &error)); |
| 130 | } |
| 131 | { |
| 132 | ::DBus::Error error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 133 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(service_->store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 134 | flimflam::kPriorityProperty, |
| 135 | PropertyStoreTest::kInt32V, |
| 136 | &error)); |
| 137 | } |
| 138 | { |
| 139 | ::DBus::Error error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 140 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(service_->store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 141 | flimflam::kEAPEAPProperty, |
| 142 | PropertyStoreTest::kStringV, |
| 143 | &error)); |
| 144 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 145 | // 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] | 146 | { |
| 147 | ::DBus::Error error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 148 | EXPECT_FALSE(DBusAdaptor::DispatchOnType(service_->store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 149 | flimflam::kFavoriteProperty, |
| 150 | PropertyStoreTest::kBoolV, |
| 151 | &error)); |
| 152 | EXPECT_EQ(invalid_args_, error.name()); |
| 153 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Chris Masone | b2e326b | 2011-07-12 13:28:51 -0700 | [diff] [blame] | 156 | TEST_F(ServiceTest, MoveService) { |
| 157 | // I want to ensure that the Profiles are managing this Service object |
| 158 | // lifetime properly, so I can't hold a ref to it here. |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 159 | ProfileRefPtr profile(new Profile(&control_interface_, &glib_)); |
Chris Masone | b2e326b | 2011-07-12 13:28:51 -0700 | [diff] [blame] | 160 | { |
| 161 | ServiceRefPtr s2( |
| 162 | new MockService(&control_interface_, |
| 163 | &dispatcher_, |
| 164 | new MockProfile(&control_interface_, &glib_), |
| 165 | kMockServiceName)); |
| 166 | profile->services_[kMockServiceName] = s2; |
| 167 | } |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 168 | |
Chris Masone | b2e326b | 2011-07-12 13:28:51 -0700 | [diff] [blame] | 169 | // Now, move the service to another profile. |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 170 | ProfileRefPtr profile2(new Profile(&control_interface_, &glib_)); |
Chris Masone | b2e326b | 2011-07-12 13:28:51 -0700 | [diff] [blame] | 171 | map<string, ServiceRefPtr>::iterator it = |
| 172 | profile->services_.find(kMockServiceName); |
| 173 | ASSERT_TRUE(it != profile->services_.end()); |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 174 | |
Chris Masone | b2e326b | 2011-07-12 13:28:51 -0700 | [diff] [blame] | 175 | profile2->AdoptService(it->first, it->second); |
| 176 | // Force destruction of the original Profile, to ensure that the Service |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 177 | // is kept alive and populated with data. |
| 178 | profile = NULL; |
| 179 | { |
Chris Masone | b2e326b | 2011-07-12 13:28:51 -0700 | [diff] [blame] | 180 | map<string, ServiceRefPtr>::iterator it = |
| 181 | profile2->services_.find(kMockServiceName); |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 182 | Error error(Error::kInvalidProperty, ""); |
| 183 | ::DBus::Error dbus_error; |
| 184 | map<string, ::DBus::Variant> props; |
| 185 | bool expected = true; |
Chris Masone | b2e326b | 2011-07-12 13:28:51 -0700 | [diff] [blame] | 186 | it->second->store()->SetBoolProperty(flimflam::kAutoConnectProperty, |
| 187 | expected, |
| 188 | &error); |
| 189 | DBusAdaptor::GetProperties(it->second->store(), &props, &dbus_error); |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 190 | ASSERT_FALSE(props.find(flimflam::kAutoConnectProperty) == props.end()); |
| 191 | EXPECT_EQ(props[flimflam::kAutoConnectProperty].reader().get_bool(), |
| 192 | expected); |
| 193 | } |
| 194 | } |
| 195 | |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame^] | 196 | TEST_F(ServiceTest, GetStorageIdentifier) { |
| 197 | EXPECT_EQ(kMockServiceName, service_->GetStorageIdentifier()); |
| 198 | } |
| 199 | |
| 200 | TEST_F(ServiceTest, Load) { |
| 201 | NiceMock<MockStore> storage; |
| 202 | const string id = kMockServiceName; |
| 203 | EXPECT_CALL(storage, ContainsGroup(id)).WillOnce(Return(true)); |
| 204 | EXPECT_CALL(storage, GetString(id, _, _)) |
| 205 | .Times(AtLeast(1)) |
| 206 | .WillRepeatedly(Return(true)); |
| 207 | EXPECT_TRUE(service_->Load(&storage)); |
| 208 | } |
| 209 | |
| 210 | TEST_F(ServiceTest, LoadFail) { |
| 211 | StrictMock<MockStore> storage; |
| 212 | const string id = kMockServiceName; |
| 213 | EXPECT_CALL(storage, ContainsGroup(kMockServiceName)).WillOnce(Return(false)); |
| 214 | EXPECT_FALSE(service_->Load(&storage)); |
| 215 | } |
| 216 | |
| 217 | TEST_F(ServiceTest, SaveString) { |
| 218 | MockStore storage; |
| 219 | static const char kKey[] = "test-key"; |
| 220 | static const char kData[] = "test-data"; |
| 221 | EXPECT_CALL(storage, SetString(kMockServiceName, kKey, kData)) |
| 222 | .WillOnce(Return(true)); |
| 223 | service_->SaveString(&storage, kKey, kData, false, true); |
| 224 | } |
| 225 | |
| 226 | TEST_F(ServiceTest, SaveStringCrypted) { |
| 227 | MockStore storage; |
| 228 | static const char kKey[] = "test-key"; |
| 229 | static const char kData[] = "test-data"; |
| 230 | EXPECT_CALL(storage, SetCryptedString(kMockServiceName, kKey, kData)) |
| 231 | .WillOnce(Return(true)); |
| 232 | service_->SaveString(&storage, kKey, kData, true, true); |
| 233 | } |
| 234 | |
| 235 | TEST_F(ServiceTest, SaveStringDontSave) { |
| 236 | MockStore storage; |
| 237 | static const char kKey[] = "test-key"; |
| 238 | EXPECT_CALL(storage, DeleteKey(kMockServiceName, kKey)) |
| 239 | .WillOnce(Return(true)); |
| 240 | service_->SaveString(&storage, kKey, "data", false, false); |
| 241 | } |
| 242 | |
| 243 | TEST_F(ServiceTest, SaveStringEmpty) { |
| 244 | MockStore storage; |
| 245 | static const char kKey[] = "test-key"; |
| 246 | EXPECT_CALL(storage, DeleteKey(kMockServiceName, kKey)) |
| 247 | .WillOnce(Return(true)); |
| 248 | service_->SaveString(&storage, kKey, "", true, true); |
| 249 | } |
| 250 | |
| 251 | TEST_F(ServiceTest, Save) { |
| 252 | NiceMock<MockStore> storage; |
| 253 | const string id = kMockServiceName; |
| 254 | EXPECT_CALL(storage, SetString(id, _, _)) |
| 255 | .Times(AtLeast(1)) |
| 256 | .WillRepeatedly(Return(true)); |
| 257 | EXPECT_CALL(storage, DeleteKey(id, _)) |
| 258 | .Times(AtLeast(1)) |
| 259 | .WillRepeatedly(Return(true)); |
| 260 | EXPECT_TRUE(service_->Save(&storage)); |
| 261 | } |
| 262 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 263 | } // namespace shill |