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