Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -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/default_profile.h" |
| 6 | |
| 7 | #include <map> |
| 8 | #include <string> |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 9 | #include <vector> |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 10 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 11 | #include <base/file_path.h> |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 12 | #include <chromeos/dbus/service_constants.h> |
| 13 | #include <gtest/gtest.h> |
| 14 | #include <gmock/gmock.h> |
| 15 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 16 | #include "shill/key_file_store.h" |
| 17 | #include "shill/glib.h" |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 18 | #include "shill/manager.h" |
| 19 | #include "shill/mock_control.h" |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 20 | #include "shill/mock_device.h" |
Paul Stewart | f284a23 | 2012-05-01 10:24:37 -0700 | [diff] [blame^] | 21 | #include "shill/mock_service.h" |
Chris Masone | aa48237 | 2011-09-14 16:40:37 -0700 | [diff] [blame] | 22 | #include "shill/mock_store.h" |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 23 | #include "shill/portal_detector.h" |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 24 | #include "shill/property_store_unittest.h" |
| 25 | |
| 26 | using std::map; |
| 27 | using std::string; |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 28 | using std::vector; |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 29 | using ::testing::_; |
Paul Stewart | 870523b | 2012-01-11 17:00:42 -0800 | [diff] [blame] | 30 | using ::testing::DoAll; |
Chris Masone | aa48237 | 2011-09-14 16:40:37 -0700 | [diff] [blame] | 31 | using ::testing::Return; |
Paul Stewart | 870523b | 2012-01-11 17:00:42 -0800 | [diff] [blame] | 32 | using ::testing::SetArgumentPointee; |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 33 | |
| 34 | namespace shill { |
| 35 | |
| 36 | class DefaultProfileTest : public PropertyStoreTest { |
| 37 | public: |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 38 | DefaultProfileTest() |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 39 | : profile_(new DefaultProfile(control_interface(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 40 | manager(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 41 | FilePath(storage_path()), |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 42 | DefaultProfile::kDefaultId, |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 43 | properties_)), |
| 44 | device_(new MockDevice(control_interface(), |
| 45 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 46 | metrics(), |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 47 | manager(), |
| 48 | "null0", |
| 49 | "addr0", |
| 50 | 0)) { |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 51 | } |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 52 | |
| 53 | virtual ~DefaultProfileTest() {} |
| 54 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 55 | virtual void SetUp() { |
| 56 | PropertyStoreTest::SetUp(); |
| 57 | FilePath final_path; |
| 58 | ASSERT_TRUE(profile_->GetStoragePath(&final_path)); |
| 59 | scoped_ptr<KeyFileStore> storage(new KeyFileStore(&real_glib_)); |
| 60 | storage->set_path(final_path); |
| 61 | ASSERT_TRUE(storage->Open()); |
| 62 | profile_->set_storage(storage.release()); // Passes ownership. |
| 63 | } |
| 64 | |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 65 | protected: |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 66 | static const char kTestStoragePath[]; |
| 67 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 68 | GLib real_glib_; |
Paul Stewart | 870523b | 2012-01-11 17:00:42 -0800 | [diff] [blame] | 69 | scoped_refptr<DefaultProfile> profile_; |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 70 | scoped_refptr<MockDevice> device_; |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 71 | Manager::Properties properties_; |
| 72 | }; |
| 73 | |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 74 | const char DefaultProfileTest::kTestStoragePath[] = "/no/where"; |
| 75 | |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 76 | TEST_F(DefaultProfileTest, GetProperties) { |
| 77 | Error error(Error::kInvalidProperty, ""); |
| 78 | { |
| 79 | map<string, ::DBus::Variant> props; |
| 80 | ::DBus::Error dbus_error; |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 81 | DBusAdaptor::GetProperties(profile_->store(), &props, &dbus_error); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 82 | ASSERT_FALSE(props.find(flimflam::kOfflineModeProperty) == props.end()); |
| 83 | EXPECT_FALSE(props[flimflam::kOfflineModeProperty].reader().get_bool()); |
| 84 | } |
| 85 | properties_.offline_mode = true; |
| 86 | { |
| 87 | map<string, ::DBus::Variant> props; |
| 88 | ::DBus::Error dbus_error; |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 89 | DBusAdaptor::GetProperties(profile_->store(), &props, &dbus_error); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 90 | ASSERT_FALSE(props.find(flimflam::kOfflineModeProperty) == props.end()); |
| 91 | EXPECT_TRUE(props[flimflam::kOfflineModeProperty].reader().get_bool()); |
| 92 | } |
| 93 | { |
| 94 | Error error(Error::kInvalidProperty, ""); |
| 95 | EXPECT_FALSE( |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 96 | profile_->mutable_store()->SetBoolProperty( |
| 97 | flimflam::kOfflineModeProperty, |
| 98 | true, |
| 99 | &error)); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
Chris Masone | aa48237 | 2011-09-14 16:40:37 -0700 | [diff] [blame] | 103 | TEST_F(DefaultProfileTest, Save) { |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 104 | scoped_ptr<MockStore> storage(new MockStore); |
| 105 | EXPECT_CALL(*storage.get(), SetString(DefaultProfile::kStorageId, |
| 106 | DefaultProfile::kStorageName, |
| 107 | DefaultProfile::kDefaultId)) |
Chris Masone | aa48237 | 2011-09-14 16:40:37 -0700 | [diff] [blame] | 108 | .WillOnce(Return(true)); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 109 | EXPECT_CALL(*storage.get(), SetString(DefaultProfile::kStorageId, |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 110 | DefaultProfile::kStorageHostName, |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 111 | "")) |
Chris Masone | aa48237 | 2011-09-14 16:40:37 -0700 | [diff] [blame] | 112 | .WillOnce(Return(true)); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 113 | EXPECT_CALL(*storage.get(), SetBool(DefaultProfile::kStorageId, |
| 114 | DefaultProfile::kStorageOfflineMode, |
| 115 | false)) |
Chris Masone | aa48237 | 2011-09-14 16:40:37 -0700 | [diff] [blame] | 116 | .WillOnce(Return(true)); |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 117 | EXPECT_CALL(*storage.get(), SetString(DefaultProfile::kStorageId, |
| 118 | DefaultProfile::kStorageCheckPortalList, |
| 119 | "")) |
| 120 | .WillOnce(Return(true)); |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 121 | EXPECT_CALL(*storage.get(), SetString(DefaultProfile::kStorageId, |
| 122 | DefaultProfile::kStoragePortalURL, |
| 123 | "")) |
| 124 | .WillOnce(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 125 | EXPECT_CALL(*storage.get(), |
| 126 | SetString(DefaultProfile::kStorageId, |
| 127 | DefaultProfile::kStoragePortalCheckInterval, |
| 128 | "0")) |
| 129 | .WillOnce(Return(true)); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 130 | EXPECT_CALL(*storage.get(), Flush()).WillOnce(Return(true)); |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 131 | |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 132 | EXPECT_CALL(*device_.get(), Save(storage.get())).WillOnce(Return(true)); |
| 133 | profile_->set_storage(storage.release()); |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 134 | |
| 135 | manager()->RegisterDevice(device_); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 136 | ASSERT_TRUE(profile_->Save()); |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 137 | manager()->DeregisterDevice(device_); |
Chris Masone | aa48237 | 2011-09-14 16:40:37 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 140 | TEST_F(DefaultProfileTest, LoadManagerDefaultProperties) { |
| 141 | scoped_ptr<MockStore> storage(new MockStore); |
| 142 | EXPECT_CALL(*storage.get(), GetString(DefaultProfile::kStorageId, |
| 143 | DefaultProfile::kStorageHostName, |
| 144 | _)) |
| 145 | .WillOnce(Return(false)); |
| 146 | EXPECT_CALL(*storage.get(), GetBool(DefaultProfile::kStorageId, |
| 147 | DefaultProfile::kStorageOfflineMode, |
| 148 | _)) |
| 149 | .WillOnce(Return(false)); |
| 150 | EXPECT_CALL(*storage.get(), GetString(DefaultProfile::kStorageId, |
| 151 | DefaultProfile::kStorageCheckPortalList, |
| 152 | _)) |
| 153 | .WillOnce(Return(false)); |
| 154 | EXPECT_CALL(*storage.get(), GetString(DefaultProfile::kStorageId, |
| 155 | DefaultProfile::kStoragePortalURL, |
| 156 | _)) |
| 157 | .WillOnce(Return(false)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 158 | EXPECT_CALL(*storage.get(), |
| 159 | GetString(DefaultProfile::kStorageId, |
| 160 | DefaultProfile::kStoragePortalCheckInterval, |
| 161 | _)) |
| 162 | .WillOnce(Return(false)); |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 163 | profile_->set_storage(storage.release()); |
| 164 | |
| 165 | Manager::Properties manager_props; |
| 166 | ASSERT_TRUE(profile_->LoadManagerProperties(&manager_props)); |
| 167 | EXPECT_EQ("", manager_props.host_name); |
| 168 | EXPECT_FALSE(manager_props.offline_mode); |
Paul Stewart | f555cf8 | 2012-03-15 14:42:43 -0700 | [diff] [blame] | 169 | EXPECT_EQ(PortalDetector::kDefaultCheckPortalList, |
| 170 | manager_props.check_portal_list); |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 171 | EXPECT_EQ(PortalDetector::kDefaultURL, manager_props.portal_url); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 172 | EXPECT_EQ(PortalDetector::kDefaultCheckIntervalSeconds, |
| 173 | manager_props.portal_check_interval_seconds); |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Paul Stewart | 870523b | 2012-01-11 17:00:42 -0800 | [diff] [blame] | 176 | TEST_F(DefaultProfileTest, LoadManagerProperties) { |
| 177 | scoped_ptr<MockStore> storage(new MockStore); |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 178 | const string host_name("hostname"); |
| 179 | EXPECT_CALL(*storage.get(), GetString(DefaultProfile::kStorageId, |
| 180 | DefaultProfile::kStorageHostName, |
| 181 | _)) |
| 182 | .WillOnce(DoAll(SetArgumentPointee<2>(host_name), Return(true))); |
Paul Stewart | 870523b | 2012-01-11 17:00:42 -0800 | [diff] [blame] | 183 | EXPECT_CALL(*storage.get(), GetBool(DefaultProfile::kStorageId, |
| 184 | DefaultProfile::kStorageOfflineMode, |
| 185 | _)) |
| 186 | .WillOnce(DoAll(SetArgumentPointee<2>(true), Return(true))); |
| 187 | const string portal_list("technology1,technology2"); |
| 188 | EXPECT_CALL(*storage.get(), GetString(DefaultProfile::kStorageId, |
| 189 | DefaultProfile::kStorageCheckPortalList, |
| 190 | _)) |
| 191 | .WillOnce(DoAll(SetArgumentPointee<2>(portal_list), Return(true))); |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 192 | const string portal_url("http://www.chromium.org"); |
| 193 | EXPECT_CALL(*storage.get(), GetString(DefaultProfile::kStorageId, |
| 194 | DefaultProfile::kStoragePortalURL, |
| 195 | _)) |
| 196 | .WillOnce(DoAll(SetArgumentPointee<2>(portal_url), Return(true))); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 197 | const string portal_check_interval_string("10"); |
| 198 | const int portal_check_interval_int = 10; |
| 199 | EXPECT_CALL(*storage.get(), |
| 200 | GetString(DefaultProfile::kStorageId, |
| 201 | DefaultProfile::kStoragePortalCheckInterval, |
| 202 | _)) |
| 203 | .WillOnce(DoAll(SetArgumentPointee<2>(portal_check_interval_string), |
| 204 | Return(true))); |
Paul Stewart | 870523b | 2012-01-11 17:00:42 -0800 | [diff] [blame] | 205 | profile_->set_storage(storage.release()); |
| 206 | |
| 207 | Manager::Properties manager_props; |
| 208 | ASSERT_TRUE(profile_->LoadManagerProperties(&manager_props)); |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 209 | EXPECT_EQ(host_name, manager_props.host_name); |
Paul Stewart | 870523b | 2012-01-11 17:00:42 -0800 | [diff] [blame] | 210 | EXPECT_TRUE(manager_props.offline_mode); |
| 211 | EXPECT_EQ(portal_list, manager_props.check_portal_list); |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 212 | EXPECT_EQ(portal_url, manager_props.portal_url); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 213 | EXPECT_EQ(portal_check_interval_int, |
| 214 | manager_props.portal_check_interval_seconds); |
Paul Stewart | 870523b | 2012-01-11 17:00:42 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 217 | TEST_F(DefaultProfileTest, GetStoragePath) { |
| 218 | FilePath path; |
| 219 | EXPECT_TRUE(profile_->GetStoragePath(&path)); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 220 | EXPECT_EQ(storage_path() + "/default.profile", path.value()); |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Paul Stewart | f284a23 | 2012-05-01 10:24:37 -0700 | [diff] [blame^] | 223 | TEST_F(DefaultProfileTest, ConfigureService) { |
| 224 | scoped_ptr<MockStore> storage(new MockStore); |
| 225 | EXPECT_CALL(*storage, ContainsGroup(_)) |
| 226 | .WillRepeatedly(Return(false)); |
| 227 | EXPECT_CALL(*storage, Flush()) |
| 228 | .WillOnce(Return(true)); |
| 229 | |
| 230 | scoped_refptr<MockService> unknown_service(new MockService( |
| 231 | control_interface(), |
| 232 | dispatcher(), |
| 233 | metrics(), |
| 234 | manager())); |
| 235 | EXPECT_CALL(*unknown_service, technology()) |
| 236 | .WillOnce(Return(Technology::kUnknown)); |
| 237 | EXPECT_CALL(*unknown_service, Save(_)) .Times(0); |
| 238 | |
| 239 | scoped_refptr<MockService> ethernet_service(new MockService( |
| 240 | control_interface(), |
| 241 | dispatcher(), |
| 242 | metrics(), |
| 243 | manager())); |
| 244 | EXPECT_CALL(*ethernet_service, technology()) |
| 245 | .WillOnce(Return(Technology::kEthernet)); |
| 246 | EXPECT_CALL(*ethernet_service, Save(storage.get())) |
| 247 | .WillOnce(Return(true)); |
| 248 | |
| 249 | profile_->set_storage(storage.release()); |
| 250 | EXPECT_FALSE(profile_->ConfigureService(unknown_service)); |
| 251 | EXPECT_TRUE(profile_->ConfigureService(ethernet_service)); |
| 252 | } |
| 253 | |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 254 | } // namespace shill |