blob: a60d37b57bbd2372535907149129f27ee764b261 [file] [log] [blame]
Thieu Le3426c8f2012-01-11 17:35:11 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Masone88cbd5f2011-07-03 14:30:04 -07002// 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 Masone877ff982011-09-21 16:18:24 -07009#include <vector>
Chris Masone88cbd5f2011-07-03 14:30:04 -070010
Chris Masone6515aab2011-10-12 16:19:09 -070011#include <base/file_path.h>
Chris Masone88cbd5f2011-07-03 14:30:04 -070012#include <chromeos/dbus/service_constants.h>
13#include <gtest/gtest.h>
14#include <gmock/gmock.h>
15
Chris Masone6515aab2011-10-12 16:19:09 -070016#include "shill/key_file_store.h"
17#include "shill/glib.h"
Chris Masone88cbd5f2011-07-03 14:30:04 -070018#include "shill/manager.h"
19#include "shill/mock_control.h"
Chris Masone877ff982011-09-21 16:18:24 -070020#include "shill/mock_device.h"
Chris Masoneaa482372011-09-14 16:40:37 -070021#include "shill/mock_store.h"
Paul Stewarte6927402012-01-23 16:11:30 -080022#include "shill/portal_detector.h"
Chris Masone88cbd5f2011-07-03 14:30:04 -070023#include "shill/property_store_unittest.h"
24
25using std::map;
26using std::string;
Chris Masone877ff982011-09-21 16:18:24 -070027using std::vector;
Chris Masone88cbd5f2011-07-03 14:30:04 -070028using ::testing::_;
Paul Stewart870523b2012-01-11 17:00:42 -080029using ::testing::DoAll;
Chris Masoneaa482372011-09-14 16:40:37 -070030using ::testing::Return;
Paul Stewart870523b2012-01-11 17:00:42 -080031using ::testing::SetArgumentPointee;
Chris Masone88cbd5f2011-07-03 14:30:04 -070032
33namespace shill {
34
35class DefaultProfileTest : public PropertyStoreTest {
36 public:
Chris Masone7aa5f902011-07-11 11:13:35 -070037 DefaultProfileTest()
Chris Masone2176a882011-09-14 22:29:15 -070038 : profile_(new DefaultProfile(control_interface(),
Chris Masone9d779932011-08-25 16:33:41 -070039 manager(),
Chris Masone6515aab2011-10-12 16:19:09 -070040 FilePath(storage_path()),
Chris Masone877ff982011-09-21 16:18:24 -070041 properties_)),
42 device_(new MockDevice(control_interface(),
43 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080044 metrics(),
Chris Masone877ff982011-09-21 16:18:24 -070045 manager(),
46 "null0",
47 "addr0",
48 0)) {
Chris Masone7aa5f902011-07-11 11:13:35 -070049 }
Chris Masone88cbd5f2011-07-03 14:30:04 -070050
51 virtual ~DefaultProfileTest() {}
52
Chris Masone6515aab2011-10-12 16:19:09 -070053 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 Masone88cbd5f2011-07-03 14:30:04 -070063 protected:
Chris Masone2ae797d2011-08-23 20:41:00 -070064 static const char kTestStoragePath[];
65
Chris Masone6515aab2011-10-12 16:19:09 -070066 GLib real_glib_;
Paul Stewart870523b2012-01-11 17:00:42 -080067 scoped_refptr<DefaultProfile> profile_;
Chris Masone877ff982011-09-21 16:18:24 -070068 scoped_refptr<MockDevice> device_;
Chris Masone88cbd5f2011-07-03 14:30:04 -070069 Manager::Properties properties_;
70};
71
Chris Masone2ae797d2011-08-23 20:41:00 -070072const char DefaultProfileTest::kTestStoragePath[] = "/no/where";
73
Chris Masone88cbd5f2011-07-03 14:30:04 -070074TEST_F(DefaultProfileTest, GetProperties) {
75 Error error(Error::kInvalidProperty, "");
76 {
77 map<string, ::DBus::Variant> props;
78 ::DBus::Error dbus_error;
Chris Masone7aa5f902011-07-11 11:13:35 -070079 DBusAdaptor::GetProperties(profile_->store(), &props, &dbus_error);
Chris Masone88cbd5f2011-07-03 14:30:04 -070080 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 Masone7aa5f902011-07-11 11:13:35 -070087 DBusAdaptor::GetProperties(profile_->store(), &props, &dbus_error);
Chris Masone88cbd5f2011-07-03 14:30:04 -070088 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 agrawalde29fa82011-09-16 16:16:36 -070094 profile_->mutable_store()->SetBoolProperty(
95 flimflam::kOfflineModeProperty,
96 true,
97 &error));
Chris Masone88cbd5f2011-07-03 14:30:04 -070098 }
99}
100
Chris Masoneaa482372011-09-14 16:40:37 -0700101TEST_F(DefaultProfileTest, Save) {
Chris Masoneb9c00592011-10-06 13:10:39 -0700102 scoped_ptr<MockStore> storage(new MockStore);
103 EXPECT_CALL(*storage.get(), SetString(DefaultProfile::kStorageId,
104 DefaultProfile::kStorageName,
105 DefaultProfile::kDefaultId))
Chris Masoneaa482372011-09-14 16:40:37 -0700106 .WillOnce(Return(true));
Chris Masoneb9c00592011-10-06 13:10:39 -0700107 EXPECT_CALL(*storage.get(), SetString(DefaultProfile::kStorageId,
Paul Stewartd32f4842012-01-11 16:08:13 -0800108 DefaultProfile::kStorageHostName,
Chris Masoneb9c00592011-10-06 13:10:39 -0700109 ""))
Chris Masoneaa482372011-09-14 16:40:37 -0700110 .WillOnce(Return(true));
Chris Masoneb9c00592011-10-06 13:10:39 -0700111 EXPECT_CALL(*storage.get(), SetBool(DefaultProfile::kStorageId,
112 DefaultProfile::kStorageOfflineMode,
113 false))
Chris Masoneaa482372011-09-14 16:40:37 -0700114 .WillOnce(Return(true));
Paul Stewartd32f4842012-01-11 16:08:13 -0800115 EXPECT_CALL(*storage.get(), SetString(DefaultProfile::kStorageId,
116 DefaultProfile::kStorageCheckPortalList,
117 ""))
118 .WillOnce(Return(true));
Paul Stewarte6927402012-01-23 16:11:30 -0800119 EXPECT_CALL(*storage.get(), SetString(DefaultProfile::kStorageId,
120 DefaultProfile::kStoragePortalURL,
121 ""))
122 .WillOnce(Return(true));
Paul Stewartc681fa02012-03-02 19:40:04 -0800123 EXPECT_CALL(*storage.get(),
124 SetString(DefaultProfile::kStorageId,
125 DefaultProfile::kStoragePortalCheckInterval,
126 "0"))
127 .WillOnce(Return(true));
Chris Masoneb9c00592011-10-06 13:10:39 -0700128 EXPECT_CALL(*storage.get(), Flush()).WillOnce(Return(true));
Chris Masone877ff982011-09-21 16:18:24 -0700129
Chris Masoneb9c00592011-10-06 13:10:39 -0700130 EXPECT_CALL(*device_.get(), Save(storage.get())).WillOnce(Return(true));
131 profile_->set_storage(storage.release());
Chris Masone877ff982011-09-21 16:18:24 -0700132
133 manager()->RegisterDevice(device_);
Chris Masoneb9c00592011-10-06 13:10:39 -0700134 ASSERT_TRUE(profile_->Save());
Chris Masone877ff982011-09-21 16:18:24 -0700135 manager()->DeregisterDevice(device_);
Chris Masoneaa482372011-09-14 16:40:37 -0700136}
137
Paul Stewarte6927402012-01-23 16:11:30 -0800138TEST_F(DefaultProfileTest, LoadManagerDefaultProperties) {
139 scoped_ptr<MockStore> storage(new MockStore);
140 EXPECT_CALL(*storage.get(), GetString(DefaultProfile::kStorageId,
141 DefaultProfile::kStorageHostName,
142 _))
143 .WillOnce(Return(false));
144 EXPECT_CALL(*storage.get(), GetBool(DefaultProfile::kStorageId,
145 DefaultProfile::kStorageOfflineMode,
146 _))
147 .WillOnce(Return(false));
148 EXPECT_CALL(*storage.get(), GetString(DefaultProfile::kStorageId,
149 DefaultProfile::kStorageCheckPortalList,
150 _))
151 .WillOnce(Return(false));
152 EXPECT_CALL(*storage.get(), GetString(DefaultProfile::kStorageId,
153 DefaultProfile::kStoragePortalURL,
154 _))
155 .WillOnce(Return(false));
Paul Stewartc681fa02012-03-02 19:40:04 -0800156 EXPECT_CALL(*storage.get(),
157 GetString(DefaultProfile::kStorageId,
158 DefaultProfile::kStoragePortalCheckInterval,
159 _))
160 .WillOnce(Return(false));
Paul Stewarte6927402012-01-23 16:11:30 -0800161 profile_->set_storage(storage.release());
162
163 Manager::Properties manager_props;
164 ASSERT_TRUE(profile_->LoadManagerProperties(&manager_props));
165 EXPECT_EQ("", manager_props.host_name);
166 EXPECT_FALSE(manager_props.offline_mode);
167 EXPECT_EQ("", manager_props.check_portal_list);
168 EXPECT_EQ(PortalDetector::kDefaultURL, manager_props.portal_url);
Paul Stewartc681fa02012-03-02 19:40:04 -0800169 EXPECT_EQ(PortalDetector::kDefaultCheckIntervalSeconds,
170 manager_props.portal_check_interval_seconds);
Paul Stewarte6927402012-01-23 16:11:30 -0800171}
172
Paul Stewart870523b2012-01-11 17:00:42 -0800173TEST_F(DefaultProfileTest, LoadManagerProperties) {
174 scoped_ptr<MockStore> storage(new MockStore);
Paul Stewartd32f4842012-01-11 16:08:13 -0800175 const string host_name("hostname");
176 EXPECT_CALL(*storage.get(), GetString(DefaultProfile::kStorageId,
177 DefaultProfile::kStorageHostName,
178 _))
179 .WillOnce(DoAll(SetArgumentPointee<2>(host_name), Return(true)));
Paul Stewart870523b2012-01-11 17:00:42 -0800180 EXPECT_CALL(*storage.get(), GetBool(DefaultProfile::kStorageId,
181 DefaultProfile::kStorageOfflineMode,
182 _))
183 .WillOnce(DoAll(SetArgumentPointee<2>(true), Return(true)));
184 const string portal_list("technology1,technology2");
185 EXPECT_CALL(*storage.get(), GetString(DefaultProfile::kStorageId,
186 DefaultProfile::kStorageCheckPortalList,
187 _))
188 .WillOnce(DoAll(SetArgumentPointee<2>(portal_list), Return(true)));
Paul Stewarte6927402012-01-23 16:11:30 -0800189 const string portal_url("http://www.chromium.org");
190 EXPECT_CALL(*storage.get(), GetString(DefaultProfile::kStorageId,
191 DefaultProfile::kStoragePortalURL,
192 _))
193 .WillOnce(DoAll(SetArgumentPointee<2>(portal_url), Return(true)));
Paul Stewartc681fa02012-03-02 19:40:04 -0800194 const string portal_check_interval_string("10");
195 const int portal_check_interval_int = 10;
196 EXPECT_CALL(*storage.get(),
197 GetString(DefaultProfile::kStorageId,
198 DefaultProfile::kStoragePortalCheckInterval,
199 _))
200 .WillOnce(DoAll(SetArgumentPointee<2>(portal_check_interval_string),
201 Return(true)));
Paul Stewart870523b2012-01-11 17:00:42 -0800202 profile_->set_storage(storage.release());
203
204 Manager::Properties manager_props;
205 ASSERT_TRUE(profile_->LoadManagerProperties(&manager_props));
Paul Stewartd32f4842012-01-11 16:08:13 -0800206 EXPECT_EQ(host_name, manager_props.host_name);
Paul Stewart870523b2012-01-11 17:00:42 -0800207 EXPECT_TRUE(manager_props.offline_mode);
208 EXPECT_EQ(portal_list, manager_props.check_portal_list);
Paul Stewarte6927402012-01-23 16:11:30 -0800209 EXPECT_EQ(portal_url, manager_props.portal_url);
Paul Stewartc681fa02012-03-02 19:40:04 -0800210 EXPECT_EQ(portal_check_interval_int,
211 manager_props.portal_check_interval_seconds);
Paul Stewart870523b2012-01-11 17:00:42 -0800212}
213
Chris Masone2ae797d2011-08-23 20:41:00 -0700214TEST_F(DefaultProfileTest, GetStoragePath) {
215 FilePath path;
216 EXPECT_TRUE(profile_->GetStoragePath(&path));
Chris Masone6515aab2011-10-12 16:19:09 -0700217 EXPECT_EQ(storage_path() + "/default.profile", path.value());
Chris Masone2ae797d2011-08-23 20:41:00 -0700218}
219
Chris Masone88cbd5f2011-07-03 14:30:04 -0700220} // namespace shill