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" |
Chris Masone | aa48237 | 2011-09-14 16:40:37 -0700 | [diff] [blame] | 21 | #include "shill/mock_store.h" |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame^] | 22 | #include "shill/portal_detector.h" |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 23 | #include "shill/property_store_unittest.h" |
| 24 | |
| 25 | using std::map; |
| 26 | using std::string; |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 27 | using std::vector; |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 28 | using ::testing::_; |
Paul Stewart | 870523b | 2012-01-11 17:00:42 -0800 | [diff] [blame] | 29 | using ::testing::DoAll; |
Chris Masone | aa48237 | 2011-09-14 16:40:37 -0700 | [diff] [blame] | 30 | using ::testing::Return; |
Paul Stewart | 870523b | 2012-01-11 17:00:42 -0800 | [diff] [blame] | 31 | using ::testing::SetArgumentPointee; |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 32 | |
| 33 | namespace shill { |
| 34 | |
| 35 | class DefaultProfileTest : public PropertyStoreTest { |
| 36 | public: |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 37 | DefaultProfileTest() |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 38 | : profile_(new DefaultProfile(control_interface(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 39 | manager(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 40 | FilePath(storage_path()), |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 41 | properties_)), |
| 42 | device_(new MockDevice(control_interface(), |
| 43 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 44 | metrics(), |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 45 | manager(), |
| 46 | "null0", |
| 47 | "addr0", |
| 48 | 0)) { |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 49 | } |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 50 | |
| 51 | virtual ~DefaultProfileTest() {} |
| 52 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 53 | virtual void SetUp() { |
| 54 | PropertyStoreTest::SetUp(); |
| 55 | FilePath final_path; |
| 56 | ASSERT_TRUE(profile_->GetStoragePath(&final_path)); |
| 57 | scoped_ptr<KeyFileStore> storage(new KeyFileStore(&real_glib_)); |
| 58 | storage->set_path(final_path); |
| 59 | ASSERT_TRUE(storage->Open()); |
| 60 | profile_->set_storage(storage.release()); // Passes ownership. |
| 61 | } |
| 62 | |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 63 | protected: |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 64 | static const char kTestStoragePath[]; |
| 65 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 66 | GLib real_glib_; |
Paul Stewart | 870523b | 2012-01-11 17:00:42 -0800 | [diff] [blame] | 67 | scoped_refptr<DefaultProfile> profile_; |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 68 | scoped_refptr<MockDevice> device_; |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 69 | Manager::Properties properties_; |
| 70 | }; |
| 71 | |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 72 | const char DefaultProfileTest::kTestStoragePath[] = "/no/where"; |
| 73 | |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 74 | TEST_F(DefaultProfileTest, GetProperties) { |
| 75 | Error error(Error::kInvalidProperty, ""); |
| 76 | { |
| 77 | map<string, ::DBus::Variant> props; |
| 78 | ::DBus::Error dbus_error; |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 79 | DBusAdaptor::GetProperties(profile_->store(), &props, &dbus_error); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 80 | ASSERT_FALSE(props.find(flimflam::kOfflineModeProperty) == props.end()); |
| 81 | EXPECT_FALSE(props[flimflam::kOfflineModeProperty].reader().get_bool()); |
| 82 | } |
| 83 | properties_.offline_mode = true; |
| 84 | { |
| 85 | map<string, ::DBus::Variant> props; |
| 86 | ::DBus::Error dbus_error; |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 87 | DBusAdaptor::GetProperties(profile_->store(), &props, &dbus_error); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 88 | ASSERT_FALSE(props.find(flimflam::kOfflineModeProperty) == props.end()); |
| 89 | EXPECT_TRUE(props[flimflam::kOfflineModeProperty].reader().get_bool()); |
| 90 | } |
| 91 | { |
| 92 | Error error(Error::kInvalidProperty, ""); |
| 93 | EXPECT_FALSE( |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 94 | profile_->mutable_store()->SetBoolProperty( |
| 95 | flimflam::kOfflineModeProperty, |
| 96 | true, |
| 97 | &error)); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
Chris Masone | aa48237 | 2011-09-14 16:40:37 -0700 | [diff] [blame] | 101 | TEST_F(DefaultProfileTest, Save) { |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 102 | scoped_ptr<MockStore> storage(new MockStore); |
| 103 | EXPECT_CALL(*storage.get(), SetString(DefaultProfile::kStorageId, |
| 104 | DefaultProfile::kStorageName, |
| 105 | DefaultProfile::kDefaultId)) |
Chris Masone | aa48237 | 2011-09-14 16:40:37 -0700 | [diff] [blame] | 106 | .WillOnce(Return(true)); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 107 | EXPECT_CALL(*storage.get(), SetString(DefaultProfile::kStorageId, |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 108 | DefaultProfile::kStorageHostName, |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 109 | "")) |
Chris Masone | aa48237 | 2011-09-14 16:40:37 -0700 | [diff] [blame] | 110 | .WillOnce(Return(true)); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 111 | EXPECT_CALL(*storage.get(), SetBool(DefaultProfile::kStorageId, |
| 112 | DefaultProfile::kStorageOfflineMode, |
| 113 | false)) |
Chris Masone | aa48237 | 2011-09-14 16:40:37 -0700 | [diff] [blame] | 114 | .WillOnce(Return(true)); |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 115 | EXPECT_CALL(*storage.get(), SetString(DefaultProfile::kStorageId, |
| 116 | DefaultProfile::kStorageCheckPortalList, |
| 117 | "")) |
| 118 | .WillOnce(Return(true)); |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame^] | 119 | EXPECT_CALL(*storage.get(), SetString(DefaultProfile::kStorageId, |
| 120 | DefaultProfile::kStoragePortalURL, |
| 121 | "")) |
| 122 | .WillOnce(Return(true)); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 123 | EXPECT_CALL(*storage.get(), Flush()).WillOnce(Return(true)); |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 124 | |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 125 | EXPECT_CALL(*device_.get(), Save(storage.get())).WillOnce(Return(true)); |
| 126 | profile_->set_storage(storage.release()); |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 127 | |
| 128 | manager()->RegisterDevice(device_); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 129 | ASSERT_TRUE(profile_->Save()); |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 130 | manager()->DeregisterDevice(device_); |
Chris Masone | aa48237 | 2011-09-14 16:40:37 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame^] | 133 | TEST_F(DefaultProfileTest, LoadManagerDefaultProperties) { |
| 134 | scoped_ptr<MockStore> storage(new MockStore); |
| 135 | EXPECT_CALL(*storage.get(), GetString(DefaultProfile::kStorageId, |
| 136 | DefaultProfile::kStorageHostName, |
| 137 | _)) |
| 138 | .WillOnce(Return(false)); |
| 139 | EXPECT_CALL(*storage.get(), GetBool(DefaultProfile::kStorageId, |
| 140 | DefaultProfile::kStorageOfflineMode, |
| 141 | _)) |
| 142 | .WillOnce(Return(false)); |
| 143 | EXPECT_CALL(*storage.get(), GetString(DefaultProfile::kStorageId, |
| 144 | DefaultProfile::kStorageCheckPortalList, |
| 145 | _)) |
| 146 | .WillOnce(Return(false)); |
| 147 | EXPECT_CALL(*storage.get(), GetString(DefaultProfile::kStorageId, |
| 148 | DefaultProfile::kStoragePortalURL, |
| 149 | _)) |
| 150 | .WillOnce(Return(false)); |
| 151 | profile_->set_storage(storage.release()); |
| 152 | |
| 153 | Manager::Properties manager_props; |
| 154 | ASSERT_TRUE(profile_->LoadManagerProperties(&manager_props)); |
| 155 | EXPECT_EQ("", manager_props.host_name); |
| 156 | EXPECT_FALSE(manager_props.offline_mode); |
| 157 | EXPECT_EQ("", manager_props.check_portal_list); |
| 158 | EXPECT_EQ(PortalDetector::kDefaultURL, manager_props.portal_url); |
| 159 | } |
| 160 | |
Paul Stewart | 870523b | 2012-01-11 17:00:42 -0800 | [diff] [blame] | 161 | TEST_F(DefaultProfileTest, LoadManagerProperties) { |
| 162 | scoped_ptr<MockStore> storage(new MockStore); |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 163 | const string host_name("hostname"); |
| 164 | EXPECT_CALL(*storage.get(), GetString(DefaultProfile::kStorageId, |
| 165 | DefaultProfile::kStorageHostName, |
| 166 | _)) |
| 167 | .WillOnce(DoAll(SetArgumentPointee<2>(host_name), Return(true))); |
Paul Stewart | 870523b | 2012-01-11 17:00:42 -0800 | [diff] [blame] | 168 | EXPECT_CALL(*storage.get(), GetBool(DefaultProfile::kStorageId, |
| 169 | DefaultProfile::kStorageOfflineMode, |
| 170 | _)) |
| 171 | .WillOnce(DoAll(SetArgumentPointee<2>(true), Return(true))); |
| 172 | const string portal_list("technology1,technology2"); |
| 173 | EXPECT_CALL(*storage.get(), GetString(DefaultProfile::kStorageId, |
| 174 | DefaultProfile::kStorageCheckPortalList, |
| 175 | _)) |
| 176 | .WillOnce(DoAll(SetArgumentPointee<2>(portal_list), Return(true))); |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame^] | 177 | const string portal_url("http://www.chromium.org"); |
| 178 | EXPECT_CALL(*storage.get(), GetString(DefaultProfile::kStorageId, |
| 179 | DefaultProfile::kStoragePortalURL, |
| 180 | _)) |
| 181 | .WillOnce(DoAll(SetArgumentPointee<2>(portal_url), Return(true))); |
Paul Stewart | 870523b | 2012-01-11 17:00:42 -0800 | [diff] [blame] | 182 | profile_->set_storage(storage.release()); |
| 183 | |
| 184 | Manager::Properties manager_props; |
| 185 | ASSERT_TRUE(profile_->LoadManagerProperties(&manager_props)); |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 186 | EXPECT_EQ(host_name, manager_props.host_name); |
Paul Stewart | 870523b | 2012-01-11 17:00:42 -0800 | [diff] [blame] | 187 | EXPECT_TRUE(manager_props.offline_mode); |
| 188 | EXPECT_EQ(portal_list, manager_props.check_portal_list); |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame^] | 189 | EXPECT_EQ(portal_url, manager_props.portal_url); |
Paul Stewart | 870523b | 2012-01-11 17:00:42 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 192 | TEST_F(DefaultProfileTest, GetStoragePath) { |
| 193 | FilePath path; |
| 194 | EXPECT_TRUE(profile_->GetStoragePath(&path)); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 195 | EXPECT_EQ(storage_path() + "/default.profile", path.value()); |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 198 | } // namespace shill |