blob: 6ff59ff1d5773c1c758d868ae28848e8c059e7ac [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"
Paul Stewart036dba02012-08-07 12:34:41 -070018#include "shill/link_monitor.h"
Chris Masone88cbd5f2011-07-03 14:30:04 -070019#include "shill/manager.h"
20#include "shill/mock_control.h"
Chris Masone877ff982011-09-21 16:18:24 -070021#include "shill/mock_device.h"
Paul Stewartf284a232012-05-01 10:24:37 -070022#include "shill/mock_service.h"
Chris Masoneaa482372011-09-14 16:40:37 -070023#include "shill/mock_store.h"
Wade Guthrie60a37062013-04-02 11:39:09 -070024#include "shill/mock_wifi_provider.h"
Paul Stewarte6927402012-01-23 16:11:30 -080025#include "shill/portal_detector.h"
Chris Masone88cbd5f2011-07-03 14:30:04 -070026#include "shill/property_store_unittest.h"
Paul Stewartbf667612012-06-29 14:49:54 -070027#include "shill/resolver.h"
Wade Guthrie60a37062013-04-02 11:39:09 -070028#include "shill/wifi_service.h"
Chris Masone88cbd5f2011-07-03 14:30:04 -070029
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080030using base::FilePath;
Chris Masone88cbd5f2011-07-03 14:30:04 -070031using std::map;
32using std::string;
Chris Masone877ff982011-09-21 16:18:24 -070033using std::vector;
Chris Masone88cbd5f2011-07-03 14:30:04 -070034using ::testing::_;
Paul Stewart870523b2012-01-11 17:00:42 -080035using ::testing::DoAll;
Chris Masoneaa482372011-09-14 16:40:37 -070036using ::testing::Return;
Paul Stewart870523b2012-01-11 17:00:42 -080037using ::testing::SetArgumentPointee;
Chris Masone88cbd5f2011-07-03 14:30:04 -070038
39namespace shill {
40
41class DefaultProfileTest : public PropertyStoreTest {
42 public:
Chris Masone7aa5f902011-07-11 11:13:35 -070043 DefaultProfileTest()
Chris Masone2176a882011-09-14 22:29:15 -070044 : profile_(new DefaultProfile(control_interface(),
Thieu Le5133b712013-02-19 14:47:21 -080045 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -070046 manager(),
Chris Masone6515aab2011-10-12 16:19:09 -070047 FilePath(storage_path()),
Paul Stewartd0a3b812012-03-28 22:48:22 -070048 DefaultProfile::kDefaultId,
Chris Masone877ff982011-09-21 16:18:24 -070049 properties_)),
50 device_(new MockDevice(control_interface(),
51 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080052 metrics(),
Chris Masone877ff982011-09-21 16:18:24 -070053 manager(),
54 "null0",
55 "addr0",
56 0)) {
Chris Masone7aa5f902011-07-11 11:13:35 -070057 }
Chris Masone88cbd5f2011-07-03 14:30:04 -070058
59 virtual ~DefaultProfileTest() {}
60
Chris Masone6515aab2011-10-12 16:19:09 -070061 virtual void SetUp() {
62 PropertyStoreTest::SetUp();
63 FilePath final_path;
64 ASSERT_TRUE(profile_->GetStoragePath(&final_path));
65 scoped_ptr<KeyFileStore> storage(new KeyFileStore(&real_glib_));
66 storage->set_path(final_path);
67 ASSERT_TRUE(storage->Open());
68 profile_->set_storage(storage.release()); // Passes ownership.
69 }
70
Chris Masone88cbd5f2011-07-03 14:30:04 -070071 protected:
Chris Masone2ae797d2011-08-23 20:41:00 -070072 static const char kTestStoragePath[];
73
Chris Masone6515aab2011-10-12 16:19:09 -070074 GLib real_glib_;
Paul Stewart870523b2012-01-11 17:00:42 -080075 scoped_refptr<DefaultProfile> profile_;
Chris Masone877ff982011-09-21 16:18:24 -070076 scoped_refptr<MockDevice> device_;
Chris Masone88cbd5f2011-07-03 14:30:04 -070077 Manager::Properties properties_;
78};
79
Chris Masone2ae797d2011-08-23 20:41:00 -070080const char DefaultProfileTest::kTestStoragePath[] = "/no/where";
81
Chris Masone88cbd5f2011-07-03 14:30:04 -070082TEST_F(DefaultProfileTest, GetProperties) {
83 Error error(Error::kInvalidProperty, "");
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_FALSE(props[flimflam::kOfflineModeProperty].reader().get_bool());
90 }
91 properties_.offline_mode = true;
92 {
93 map<string, ::DBus::Variant> props;
94 ::DBus::Error dbus_error;
Chris Masone7aa5f902011-07-11 11:13:35 -070095 DBusAdaptor::GetProperties(profile_->store(), &props, &dbus_error);
Chris Masone88cbd5f2011-07-03 14:30:04 -070096 ASSERT_FALSE(props.find(flimflam::kOfflineModeProperty) == props.end());
97 EXPECT_TRUE(props[flimflam::kOfflineModeProperty].reader().get_bool());
98 }
99 {
100 Error error(Error::kInvalidProperty, "");
101 EXPECT_FALSE(
mukesh agrawalde29fa82011-09-16 16:16:36 -0700102 profile_->mutable_store()->SetBoolProperty(
103 flimflam::kOfflineModeProperty,
104 true,
105 &error));
Chris Masone88cbd5f2011-07-03 14:30:04 -0700106 }
107}
108
Chris Masoneaa482372011-09-14 16:40:37 -0700109TEST_F(DefaultProfileTest, Save) {
Chris Masoneb9c00592011-10-06 13:10:39 -0700110 scoped_ptr<MockStore> storage(new MockStore);
Paul Stewartd408fdf2012-05-07 17:15:57 -0700111 EXPECT_CALL(*storage.get(), SetBool(DefaultProfile::kStorageId,
112 DefaultProfile::kStorageArpGateway,
113 true))
114 .WillOnce(Return(true));
Chris Masoneb9c00592011-10-06 13:10:39 -0700115 EXPECT_CALL(*storage.get(), SetString(DefaultProfile::kStorageId,
116 DefaultProfile::kStorageName,
117 DefaultProfile::kDefaultId))
Chris Masoneaa482372011-09-14 16:40:37 -0700118 .WillOnce(Return(true));
Chris Masoneb9c00592011-10-06 13:10:39 -0700119 EXPECT_CALL(*storage.get(), SetString(DefaultProfile::kStorageId,
Paul Stewartd32f4842012-01-11 16:08:13 -0800120 DefaultProfile::kStorageHostName,
Chris Masoneb9c00592011-10-06 13:10:39 -0700121 ""))
Chris Masoneaa482372011-09-14 16:40:37 -0700122 .WillOnce(Return(true));
Chris Masoneb9c00592011-10-06 13:10:39 -0700123 EXPECT_CALL(*storage.get(), SetBool(DefaultProfile::kStorageId,
124 DefaultProfile::kStorageOfflineMode,
125 false))
Chris Masoneaa482372011-09-14 16:40:37 -0700126 .WillOnce(Return(true));
Paul Stewartd32f4842012-01-11 16:08:13 -0800127 EXPECT_CALL(*storage.get(), SetString(DefaultProfile::kStorageId,
128 DefaultProfile::kStorageCheckPortalList,
129 ""))
130 .WillOnce(Return(true));
Paul Stewart036dba02012-08-07 12:34:41 -0700131 EXPECT_CALL(*storage.get(),
132 SetString(DefaultProfile::kStorageId,
Paul Stewart4d5efb72012-09-17 12:24:34 -0700133 DefaultProfile::kStorageIgnoredDNSSearchPaths,
134 ""))
135 .WillOnce(Return(true));
136 EXPECT_CALL(*storage.get(),
137 SetString(DefaultProfile::kStorageId,
Paul Stewart036dba02012-08-07 12:34:41 -0700138 DefaultProfile::kStorageLinkMonitorTechnologies,
139 ""))
140 .WillOnce(Return(true));
Paul Stewarte6927402012-01-23 16:11:30 -0800141 EXPECT_CALL(*storage.get(), SetString(DefaultProfile::kStorageId,
142 DefaultProfile::kStoragePortalURL,
143 ""))
144 .WillOnce(Return(true));
Paul Stewartc681fa02012-03-02 19:40:04 -0800145 EXPECT_CALL(*storage.get(),
146 SetString(DefaultProfile::kStorageId,
147 DefaultProfile::kStoragePortalCheckInterval,
148 "0"))
149 .WillOnce(Return(true));
Chris Masoneb9c00592011-10-06 13:10:39 -0700150 EXPECT_CALL(*storage.get(), Flush()).WillOnce(Return(true));
Chris Masone877ff982011-09-21 16:18:24 -0700151
Paul Stewart212d60f2012-07-12 10:59:13 -0700152 EXPECT_CALL(*device_.get(), Save(storage.get())).Times(0);
Chris Masoneb9c00592011-10-06 13:10:39 -0700153 profile_->set_storage(storage.release());
Chris Masone877ff982011-09-21 16:18:24 -0700154
155 manager()->RegisterDevice(device_);
Chris Masoneb9c00592011-10-06 13:10:39 -0700156 ASSERT_TRUE(profile_->Save());
Chris Masone877ff982011-09-21 16:18:24 -0700157 manager()->DeregisterDevice(device_);
Chris Masoneaa482372011-09-14 16:40:37 -0700158}
159
Paul Stewarte6927402012-01-23 16:11:30 -0800160TEST_F(DefaultProfileTest, LoadManagerDefaultProperties) {
161 scoped_ptr<MockStore> storage(new MockStore);
Paul Stewartd408fdf2012-05-07 17:15:57 -0700162 Manager::Properties manager_props;
163 EXPECT_CALL(*storage.get(), GetBool(DefaultProfile::kStorageId,
164 DefaultProfile::kStorageArpGateway,
165 &manager_props.arp_gateway))
166 .WillOnce(Return(false));
Paul Stewarte6927402012-01-23 16:11:30 -0800167 EXPECT_CALL(*storage.get(), GetString(DefaultProfile::kStorageId,
168 DefaultProfile::kStorageHostName,
Paul Stewartd408fdf2012-05-07 17:15:57 -0700169 &manager_props.host_name))
Paul Stewarte6927402012-01-23 16:11:30 -0800170 .WillOnce(Return(false));
171 EXPECT_CALL(*storage.get(), GetBool(DefaultProfile::kStorageId,
172 DefaultProfile::kStorageOfflineMode,
Paul Stewartd408fdf2012-05-07 17:15:57 -0700173 &manager_props.offline_mode))
Paul Stewarte6927402012-01-23 16:11:30 -0800174 .WillOnce(Return(false));
175 EXPECT_CALL(*storage.get(), GetString(DefaultProfile::kStorageId,
176 DefaultProfile::kStorageCheckPortalList,
Paul Stewartd408fdf2012-05-07 17:15:57 -0700177 &manager_props.check_portal_list))
Paul Stewarte6927402012-01-23 16:11:30 -0800178 .WillOnce(Return(false));
Paul Stewart036dba02012-08-07 12:34:41 -0700179 EXPECT_CALL(*storage.get(),
180 GetString(DefaultProfile::kStorageId,
Paul Stewart4d5efb72012-09-17 12:24:34 -0700181 DefaultProfile::kStorageIgnoredDNSSearchPaths,
182 &manager_props.ignored_dns_search_paths))
183 .WillOnce(Return(false));
184 EXPECT_CALL(*storage.get(),
185 GetString(DefaultProfile::kStorageId,
Paul Stewart036dba02012-08-07 12:34:41 -0700186 DefaultProfile::kStorageLinkMonitorTechnologies,
187 _))
188 .WillOnce(Return(false));
Paul Stewarte6927402012-01-23 16:11:30 -0800189 EXPECT_CALL(*storage.get(), GetString(DefaultProfile::kStorageId,
190 DefaultProfile::kStoragePortalURL,
Paul Stewartd408fdf2012-05-07 17:15:57 -0700191 &manager_props.portal_url))
Paul Stewarte6927402012-01-23 16:11:30 -0800192 .WillOnce(Return(false));
Paul Stewartc681fa02012-03-02 19:40:04 -0800193 EXPECT_CALL(*storage.get(),
194 GetString(DefaultProfile::kStorageId,
195 DefaultProfile::kStoragePortalCheckInterval,
196 _))
197 .WillOnce(Return(false));
Paul Stewart036dba02012-08-07 12:34:41 -0700198
Paul Stewarte6927402012-01-23 16:11:30 -0800199 profile_->set_storage(storage.release());
200
Paul Stewarte6927402012-01-23 16:11:30 -0800201 ASSERT_TRUE(profile_->LoadManagerProperties(&manager_props));
Paul Stewartd408fdf2012-05-07 17:15:57 -0700202 EXPECT_TRUE(manager_props.arp_gateway);
Paul Stewarte6927402012-01-23 16:11:30 -0800203 EXPECT_EQ("", manager_props.host_name);
204 EXPECT_FALSE(manager_props.offline_mode);
Paul Stewartf555cf82012-03-15 14:42:43 -0700205 EXPECT_EQ(PortalDetector::kDefaultCheckPortalList,
206 manager_props.check_portal_list);
Paul Stewart4d5efb72012-09-17 12:24:34 -0700207 EXPECT_EQ(Resolver::kDefaultIgnoredSearchList,
208 manager_props.ignored_dns_search_paths);
Paul Stewart036dba02012-08-07 12:34:41 -0700209 EXPECT_EQ(LinkMonitor::kDefaultLinkMonitorTechnologies,
210 manager_props.link_monitor_technologies);
Paul Stewarte6927402012-01-23 16:11:30 -0800211 EXPECT_EQ(PortalDetector::kDefaultURL, manager_props.portal_url);
Paul Stewartc681fa02012-03-02 19:40:04 -0800212 EXPECT_EQ(PortalDetector::kDefaultCheckIntervalSeconds,
213 manager_props.portal_check_interval_seconds);
Paul Stewarte6927402012-01-23 16:11:30 -0800214}
215
Paul Stewart870523b2012-01-11 17:00:42 -0800216TEST_F(DefaultProfileTest, LoadManagerProperties) {
217 scoped_ptr<MockStore> storage(new MockStore);
Paul Stewartd32f4842012-01-11 16:08:13 -0800218 const string host_name("hostname");
Paul Stewartd408fdf2012-05-07 17:15:57 -0700219 EXPECT_CALL(*storage.get(), GetBool(DefaultProfile::kStorageId,
220 DefaultProfile::kStorageArpGateway,
221 _))
222 .WillOnce(DoAll(SetArgumentPointee<2>(false), Return(true)));
Paul Stewartd32f4842012-01-11 16:08:13 -0800223 EXPECT_CALL(*storage.get(), GetString(DefaultProfile::kStorageId,
224 DefaultProfile::kStorageHostName,
225 _))
226 .WillOnce(DoAll(SetArgumentPointee<2>(host_name), Return(true)));
Paul Stewart870523b2012-01-11 17:00:42 -0800227 EXPECT_CALL(*storage.get(), GetBool(DefaultProfile::kStorageId,
228 DefaultProfile::kStorageOfflineMode,
229 _))
230 .WillOnce(DoAll(SetArgumentPointee<2>(true), Return(true)));
231 const string portal_list("technology1,technology2");
232 EXPECT_CALL(*storage.get(), GetString(DefaultProfile::kStorageId,
233 DefaultProfile::kStorageCheckPortalList,
234 _))
235 .WillOnce(DoAll(SetArgumentPointee<2>(portal_list), Return(true)));
Paul Stewart4d5efb72012-09-17 12:24:34 -0700236 const string ignored_paths("chromium.org,google.com");
237 EXPECT_CALL(*storage.get(),
238 GetString(DefaultProfile::kStorageId,
239 DefaultProfile::kStorageIgnoredDNSSearchPaths,
240 _))
241 .WillOnce(DoAll(SetArgumentPointee<2>(ignored_paths), Return(true)));
Paul Stewart036dba02012-08-07 12:34:41 -0700242 const string link_monitor_technologies("ethernet,wimax");
243 EXPECT_CALL(*storage.get(),
244 GetString(DefaultProfile::kStorageId,
245 DefaultProfile::kStorageLinkMonitorTechnologies,
246 _))
247 .WillOnce(DoAll(SetArgumentPointee<2>(link_monitor_technologies),
248 Return(true)));
Paul Stewarte6927402012-01-23 16:11:30 -0800249 const string portal_url("http://www.chromium.org");
250 EXPECT_CALL(*storage.get(), GetString(DefaultProfile::kStorageId,
251 DefaultProfile::kStoragePortalURL,
252 _))
253 .WillOnce(DoAll(SetArgumentPointee<2>(portal_url), Return(true)));
Paul Stewartc681fa02012-03-02 19:40:04 -0800254 const string portal_check_interval_string("10");
255 const int portal_check_interval_int = 10;
256 EXPECT_CALL(*storage.get(),
257 GetString(DefaultProfile::kStorageId,
258 DefaultProfile::kStoragePortalCheckInterval,
259 _))
260 .WillOnce(DoAll(SetArgumentPointee<2>(portal_check_interval_string),
261 Return(true)));
Paul Stewart870523b2012-01-11 17:00:42 -0800262 profile_->set_storage(storage.release());
263
264 Manager::Properties manager_props;
265 ASSERT_TRUE(profile_->LoadManagerProperties(&manager_props));
Paul Stewartd408fdf2012-05-07 17:15:57 -0700266 EXPECT_FALSE(manager_props.arp_gateway);
Paul Stewartd32f4842012-01-11 16:08:13 -0800267 EXPECT_EQ(host_name, manager_props.host_name);
Paul Stewart870523b2012-01-11 17:00:42 -0800268 EXPECT_TRUE(manager_props.offline_mode);
269 EXPECT_EQ(portal_list, manager_props.check_portal_list);
Paul Stewart4d5efb72012-09-17 12:24:34 -0700270 EXPECT_EQ(ignored_paths, manager_props.ignored_dns_search_paths);
Paul Stewart036dba02012-08-07 12:34:41 -0700271 EXPECT_EQ(link_monitor_technologies,
272 manager_props.link_monitor_technologies);
Paul Stewarte6927402012-01-23 16:11:30 -0800273 EXPECT_EQ(portal_url, manager_props.portal_url);
Paul Stewartc681fa02012-03-02 19:40:04 -0800274 EXPECT_EQ(portal_check_interval_int,
275 manager_props.portal_check_interval_seconds);
Paul Stewart870523b2012-01-11 17:00:42 -0800276}
277
Chris Masone2ae797d2011-08-23 20:41:00 -0700278TEST_F(DefaultProfileTest, GetStoragePath) {
279 FilePath path;
280 EXPECT_TRUE(profile_->GetStoragePath(&path));
Chris Masone6515aab2011-10-12 16:19:09 -0700281 EXPECT_EQ(storage_path() + "/default.profile", path.value());
Chris Masone2ae797d2011-08-23 20:41:00 -0700282}
283
Paul Stewartf284a232012-05-01 10:24:37 -0700284TEST_F(DefaultProfileTest, ConfigureService) {
285 scoped_ptr<MockStore> storage(new MockStore);
286 EXPECT_CALL(*storage, ContainsGroup(_))
287 .WillRepeatedly(Return(false));
288 EXPECT_CALL(*storage, Flush())
289 .WillOnce(Return(true));
290
291 scoped_refptr<MockService> unknown_service(new MockService(
292 control_interface(),
293 dispatcher(),
294 metrics(),
295 manager()));
296 EXPECT_CALL(*unknown_service, technology())
297 .WillOnce(Return(Technology::kUnknown));
298 EXPECT_CALL(*unknown_service, Save(_)) .Times(0);
299
300 scoped_refptr<MockService> ethernet_service(new MockService(
301 control_interface(),
302 dispatcher(),
303 metrics(),
304 manager()));
305 EXPECT_CALL(*ethernet_service, technology())
306 .WillOnce(Return(Technology::kEthernet));
307 EXPECT_CALL(*ethernet_service, Save(storage.get()))
308 .WillOnce(Return(true));
309
310 profile_->set_storage(storage.release());
311 EXPECT_FALSE(profile_->ConfigureService(unknown_service));
312 EXPECT_TRUE(profile_->ConfigureService(ethernet_service));
313}
314
Darin Petkove7c6ad32012-06-29 10:22:09 +0200315TEST_F(DefaultProfileTest, UpdateDevice) {
316 scoped_ptr<MockStore> storage(new MockStore());
317 EXPECT_CALL(*storage, Flush()).WillOnce(Return(true));
318 EXPECT_CALL(*device_, Save(storage.get()))
319 .WillOnce(Return(true))
320 .WillOnce(Return(false));
321 profile_->set_storage(storage.release());
322 EXPECT_TRUE(profile_->UpdateDevice(device_));
323 EXPECT_FALSE(profile_->UpdateDevice(device_));
324}
325
Wade Guthrie60a37062013-04-02 11:39:09 -0700326TEST_F(DefaultProfileTest, UpdateWiFiProvider) {
327 MockWiFiProvider wifi_provider;
328
329 {
330 scoped_ptr<MockStore> storage(new MockStore());
331 EXPECT_CALL(*storage, Flush()).Times(0);
332 EXPECT_CALL(wifi_provider, Save(storage.get())).WillOnce(Return(false));
333 profile_->set_storage(storage.release());
334 EXPECT_FALSE(profile_->UpdateWiFiProvider(wifi_provider));
335 }
336
337 {
338 scoped_ptr<MockStore> storage(new MockStore());
339 EXPECT_CALL(*storage, Flush()).WillOnce(Return(false));
340 EXPECT_CALL(wifi_provider, Save(storage.get())).WillOnce(Return(true));
341 profile_->set_storage(storage.release());
342 EXPECT_FALSE(profile_->UpdateWiFiProvider(wifi_provider));
343 }
344
345 {
346 scoped_ptr<MockStore> storage(new MockStore());
347 EXPECT_CALL(*storage, Flush()).WillOnce(Return(true));
348 EXPECT_CALL(wifi_provider, Save(storage.get())).WillOnce(Return(true));
349 profile_->set_storage(storage.release());
350 EXPECT_TRUE(profile_->UpdateWiFiProvider(wifi_provider));
351 }
352}
353
Chris Masone88cbd5f2011-07-03 14:30:04 -0700354} // namespace shill