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 | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 21 | #include "shill/mock_service.h" |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 22 | #include "shill/mock_store.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 23 | #include "shill/property_store_unittest.h" |
| 24 | #include "shill/shill_event.h" |
| 25 | |
| 26 | using std::map; |
| 27 | using std::string; |
| 28 | using std::vector; |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 29 | using testing::_; |
| 30 | using testing::AtLeast; |
| 31 | using testing::NiceMock; |
| 32 | using testing::Return; |
| 33 | using testing::StrictMock; |
| 34 | using testing::Test; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 35 | |
| 36 | namespace shill { |
| 37 | |
| 38 | class ServiceTest : public PropertyStoreTest { |
| 39 | public: |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 40 | static const char kMockDeviceRpcId[]; |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 41 | static const char kProfileName[]; |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 42 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 43 | ServiceTest() |
| 44 | : service_(new MockService(&control_interface_, |
| 45 | &dispatcher_, |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 46 | &manager_)), |
| 47 | service_name_(service_->UniqueName()) { |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 50 | virtual ~ServiceTest() {} |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 51 | |
| 52 | protected: |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 53 | scoped_refptr<MockService> service_; |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 54 | string service_name_; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 57 | const char ServiceTest::kMockDeviceRpcId[] = "mock-device-rpc"; |
| 58 | |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 59 | const char ServiceTest::kProfileName[] = "profile"; |
| 60 | |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 61 | TEST_F(ServiceTest, Constructor) { |
| 62 | EXPECT_TRUE(service_->save_credentials_); |
| 63 | EXPECT_EQ(Service::kCheckPortalAuto, service_->check_portal_); |
| 64 | } |
| 65 | |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 66 | TEST_F(ServiceTest, GetProperties) { |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 67 | EXPECT_CALL(*service_.get(), CalculateState()).WillRepeatedly(Return("")); |
| 68 | EXPECT_CALL(*service_.get(), GetDeviceRpcId()) |
| 69 | .WillRepeatedly(Return(ServiceTest::kMockDeviceRpcId)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 70 | map<string, ::DBus::Variant> props; |
| 71 | Error error(Error::kInvalidProperty, ""); |
| 72 | { |
| 73 | ::DBus::Error dbus_error; |
| 74 | string expected("portal_list"); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 75 | service_->store()->SetStringProperty(flimflam::kCheckPortalProperty, |
| 76 | expected, |
| 77 | &error); |
| 78 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 79 | ASSERT_FALSE(props.find(flimflam::kCheckPortalProperty) == props.end()); |
| 80 | EXPECT_EQ(props[flimflam::kCheckPortalProperty].reader().get_string(), |
| 81 | expected); |
| 82 | } |
| 83 | { |
| 84 | ::DBus::Error dbus_error; |
| 85 | bool expected = true; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 86 | service_->store()->SetBoolProperty(flimflam::kAutoConnectProperty, |
| 87 | expected, |
| 88 | &error); |
| 89 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 90 | ASSERT_FALSE(props.find(flimflam::kAutoConnectProperty) == props.end()); |
| 91 | EXPECT_EQ(props[flimflam::kAutoConnectProperty].reader().get_bool(), |
| 92 | expected); |
| 93 | } |
| 94 | { |
| 95 | ::DBus::Error dbus_error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 96 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 97 | ASSERT_FALSE(props.find(flimflam::kConnectableProperty) == props.end()); |
| 98 | EXPECT_EQ(props[flimflam::kConnectableProperty].reader().get_bool(), false); |
| 99 | } |
| 100 | { |
| 101 | ::DBus::Error dbus_error; |
| 102 | int32 expected = 127; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 103 | service_->store()->SetInt32Property(flimflam::kPriorityProperty, |
| 104 | expected, |
| 105 | &error); |
| 106 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 107 | ASSERT_FALSE(props.find(flimflam::kPriorityProperty) == props.end()); |
| 108 | EXPECT_EQ(props[flimflam::kPriorityProperty].reader().get_int32(), |
| 109 | expected); |
| 110 | } |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 111 | { |
| 112 | ::DBus::Error dbus_error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 113 | DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error); |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 114 | ASSERT_FALSE(props.find(flimflam::kDeviceProperty) == props.end()); |
| 115 | EXPECT_EQ(props[flimflam::kDeviceProperty].reader().get_string(), |
| 116 | string(ServiceTest::kMockDeviceRpcId)); |
| 117 | } |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 118 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 119 | |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 120 | TEST_F(ServiceTest, Dispatch) { |
| 121 | { |
| 122 | ::DBus::Error error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 123 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(service_->store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 124 | flimflam::kSaveCredentialsProperty, |
| 125 | PropertyStoreTest::kBoolV, |
| 126 | &error)); |
| 127 | } |
| 128 | { |
| 129 | ::DBus::Error error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 130 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(service_->store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 131 | flimflam::kPriorityProperty, |
| 132 | PropertyStoreTest::kInt32V, |
| 133 | &error)); |
| 134 | } |
| 135 | { |
| 136 | ::DBus::Error error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 137 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(service_->store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 138 | flimflam::kEAPEAPProperty, |
| 139 | PropertyStoreTest::kStringV, |
| 140 | &error)); |
| 141 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 142 | // 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] | 143 | { |
| 144 | ::DBus::Error error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 145 | EXPECT_FALSE(DBusAdaptor::DispatchOnType(service_->store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 146 | flimflam::kFavoriteProperty, |
| 147 | PropertyStoreTest::kBoolV, |
| 148 | &error)); |
| 149 | EXPECT_EQ(invalid_args_, error.name()); |
| 150 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 153 | TEST_F(ServiceTest, GetStorageIdentifier) { |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 154 | EXPECT_EQ(service_name_, service_->GetStorageIdentifier()); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | TEST_F(ServiceTest, Load) { |
| 158 | NiceMock<MockStore> storage; |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 159 | const string id = service_name_; |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 160 | EXPECT_CALL(storage, ContainsGroup(id)).WillOnce(Return(true)); |
| 161 | EXPECT_CALL(storage, GetString(id, _, _)) |
| 162 | .Times(AtLeast(1)) |
| 163 | .WillRepeatedly(Return(true)); |
| 164 | EXPECT_TRUE(service_->Load(&storage)); |
| 165 | } |
| 166 | |
| 167 | TEST_F(ServiceTest, LoadFail) { |
| 168 | StrictMock<MockStore> storage; |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 169 | EXPECT_CALL(storage, ContainsGroup(service_name_)).WillOnce(Return(false)); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 170 | EXPECT_FALSE(service_->Load(&storage)); |
| 171 | } |
| 172 | |
| 173 | TEST_F(ServiceTest, SaveString) { |
| 174 | MockStore storage; |
| 175 | static const char kKey[] = "test-key"; |
| 176 | static const char kData[] = "test-data"; |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 177 | EXPECT_CALL(storage, SetString(service_name_, kKey, kData)) |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 178 | .WillOnce(Return(true)); |
| 179 | service_->SaveString(&storage, kKey, kData, false, true); |
| 180 | } |
| 181 | |
| 182 | TEST_F(ServiceTest, SaveStringCrypted) { |
| 183 | MockStore storage; |
| 184 | static const char kKey[] = "test-key"; |
| 185 | static const char kData[] = "test-data"; |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 186 | EXPECT_CALL(storage, SetCryptedString(service_name_, kKey, kData)) |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 187 | .WillOnce(Return(true)); |
| 188 | service_->SaveString(&storage, kKey, kData, true, true); |
| 189 | } |
| 190 | |
| 191 | TEST_F(ServiceTest, SaveStringDontSave) { |
| 192 | MockStore storage; |
| 193 | static const char kKey[] = "test-key"; |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 194 | EXPECT_CALL(storage, DeleteKey(service_name_, kKey)) |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 195 | .WillOnce(Return(true)); |
| 196 | service_->SaveString(&storage, kKey, "data", false, false); |
| 197 | } |
| 198 | |
| 199 | TEST_F(ServiceTest, SaveStringEmpty) { |
| 200 | MockStore storage; |
| 201 | static const char kKey[] = "test-key"; |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 202 | EXPECT_CALL(storage, DeleteKey(service_name_, kKey)) |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 203 | .WillOnce(Return(true)); |
| 204 | service_->SaveString(&storage, kKey, "", true, true); |
| 205 | } |
| 206 | |
| 207 | TEST_F(ServiceTest, Save) { |
| 208 | NiceMock<MockStore> storage; |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 209 | const string id = service_name_; |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 210 | EXPECT_CALL(storage, SetString(id, _, _)) |
| 211 | .Times(AtLeast(1)) |
| 212 | .WillRepeatedly(Return(true)); |
| 213 | EXPECT_CALL(storage, DeleteKey(id, _)) |
| 214 | .Times(AtLeast(1)) |
| 215 | .WillRepeatedly(Return(true)); |
| 216 | EXPECT_TRUE(service_->Save(&storage)); |
| 217 | } |
| 218 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 219 | } // namespace shill |