Chris Masone | 7156c92 | 2011-08-23 20:36:21 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 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" |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 22 | #include "shill/property_store_unittest.h" |
| 23 | |
| 24 | using std::map; |
| 25 | using std::string; |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 26 | using std::vector; |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 27 | using ::testing::_; |
Paul Stewart | 870523b | 2012-01-11 17:00:42 -0800 | [diff] [blame^] | 28 | using ::testing::DoAll; |
Chris Masone | aa48237 | 2011-09-14 16:40:37 -0700 | [diff] [blame] | 29 | using ::testing::Return; |
Paul Stewart | 870523b | 2012-01-11 17:00:42 -0800 | [diff] [blame^] | 30 | using ::testing::SetArgumentPointee; |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 31 | |
| 32 | namespace shill { |
| 33 | |
| 34 | class DefaultProfileTest : public PropertyStoreTest { |
| 35 | public: |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 36 | DefaultProfileTest() |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 37 | : profile_(new DefaultProfile(control_interface(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 38 | manager(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 39 | FilePath(storage_path()), |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 40 | properties_)), |
| 41 | device_(new MockDevice(control_interface(), |
| 42 | dispatcher(), |
| 43 | manager(), |
| 44 | "null0", |
| 45 | "addr0", |
| 46 | 0)) { |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 47 | } |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 48 | |
| 49 | virtual ~DefaultProfileTest() {} |
| 50 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 51 | virtual void SetUp() { |
| 52 | PropertyStoreTest::SetUp(); |
| 53 | FilePath final_path; |
| 54 | ASSERT_TRUE(profile_->GetStoragePath(&final_path)); |
| 55 | scoped_ptr<KeyFileStore> storage(new KeyFileStore(&real_glib_)); |
| 56 | storage->set_path(final_path); |
| 57 | ASSERT_TRUE(storage->Open()); |
| 58 | profile_->set_storage(storage.release()); // Passes ownership. |
| 59 | } |
| 60 | |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 61 | protected: |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 62 | static const char kTestStoragePath[]; |
| 63 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 64 | GLib real_glib_; |
Paul Stewart | 870523b | 2012-01-11 17:00:42 -0800 | [diff] [blame^] | 65 | scoped_refptr<DefaultProfile> profile_; |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 66 | scoped_refptr<MockDevice> device_; |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 67 | Manager::Properties properties_; |
| 68 | }; |
| 69 | |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 70 | const char DefaultProfileTest::kTestStoragePath[] = "/no/where"; |
| 71 | |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 72 | TEST_F(DefaultProfileTest, GetProperties) { |
| 73 | Error error(Error::kInvalidProperty, ""); |
| 74 | { |
| 75 | map<string, ::DBus::Variant> props; |
| 76 | ::DBus::Error dbus_error; |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 77 | DBusAdaptor::GetProperties(profile_->store(), &props, &dbus_error); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 78 | ASSERT_FALSE(props.find(flimflam::kOfflineModeProperty) == props.end()); |
| 79 | EXPECT_FALSE(props[flimflam::kOfflineModeProperty].reader().get_bool()); |
| 80 | } |
| 81 | properties_.offline_mode = true; |
| 82 | { |
| 83 | map<string, ::DBus::Variant> props; |
| 84 | ::DBus::Error dbus_error; |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 85 | DBusAdaptor::GetProperties(profile_->store(), &props, &dbus_error); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 86 | ASSERT_FALSE(props.find(flimflam::kOfflineModeProperty) == props.end()); |
| 87 | EXPECT_TRUE(props[flimflam::kOfflineModeProperty].reader().get_bool()); |
| 88 | } |
| 89 | { |
| 90 | Error error(Error::kInvalidProperty, ""); |
| 91 | EXPECT_FALSE( |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 92 | profile_->mutable_store()->SetBoolProperty( |
| 93 | flimflam::kOfflineModeProperty, |
| 94 | true, |
| 95 | &error)); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
Chris Masone | aa48237 | 2011-09-14 16:40:37 -0700 | [diff] [blame] | 99 | TEST_F(DefaultProfileTest, Save) { |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 100 | scoped_ptr<MockStore> storage(new MockStore); |
| 101 | EXPECT_CALL(*storage.get(), SetString(DefaultProfile::kStorageId, |
| 102 | DefaultProfile::kStorageName, |
| 103 | DefaultProfile::kDefaultId)) |
Chris Masone | aa48237 | 2011-09-14 16:40:37 -0700 | [diff] [blame] | 104 | .WillOnce(Return(true)); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 105 | EXPECT_CALL(*storage.get(), SetString(DefaultProfile::kStorageId, |
| 106 | DefaultProfile::kStorageCheckPortalList, |
| 107 | "")) |
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(), SetBool(DefaultProfile::kStorageId, |
| 110 | DefaultProfile::kStorageOfflineMode, |
| 111 | false)) |
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(), Flush()).WillOnce(Return(true)); |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 114 | |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 115 | EXPECT_CALL(*device_.get(), Save(storage.get())).WillOnce(Return(true)); |
| 116 | profile_->set_storage(storage.release()); |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 117 | |
| 118 | manager()->RegisterDevice(device_); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 119 | ASSERT_TRUE(profile_->Save()); |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 120 | manager()->DeregisterDevice(device_); |
Chris Masone | aa48237 | 2011-09-14 16:40:37 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Paul Stewart | 870523b | 2012-01-11 17:00:42 -0800 | [diff] [blame^] | 123 | TEST_F(DefaultProfileTest, LoadManagerProperties) { |
| 124 | scoped_ptr<MockStore> storage(new MockStore); |
| 125 | EXPECT_CALL(*storage.get(), GetBool(DefaultProfile::kStorageId, |
| 126 | DefaultProfile::kStorageOfflineMode, |
| 127 | _)) |
| 128 | .WillOnce(DoAll(SetArgumentPointee<2>(true), Return(true))); |
| 129 | const string portal_list("technology1,technology2"); |
| 130 | EXPECT_CALL(*storage.get(), GetString(DefaultProfile::kStorageId, |
| 131 | DefaultProfile::kStorageCheckPortalList, |
| 132 | _)) |
| 133 | .WillOnce(DoAll(SetArgumentPointee<2>(portal_list), Return(true))); |
| 134 | profile_->set_storage(storage.release()); |
| 135 | |
| 136 | Manager::Properties manager_props; |
| 137 | ASSERT_TRUE(profile_->LoadManagerProperties(&manager_props)); |
| 138 | EXPECT_TRUE(manager_props.offline_mode); |
| 139 | EXPECT_EQ(portal_list, manager_props.check_portal_list); |
| 140 | } |
| 141 | |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 142 | TEST_F(DefaultProfileTest, GetStoragePath) { |
| 143 | FilePath path; |
| 144 | EXPECT_TRUE(profile_->GetStoragePath(&path)); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 145 | EXPECT_EQ(storage_path() + "/default.profile", path.value()); |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 148 | } // namespace shill |