blob: 301dfd4ad5f77a5a297bbb0a1bd7806caa0fac04 [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()),
Paul Stewartd0a3b812012-03-28 22:48:22 -070041 DefaultProfile::kDefaultId,
Chris Masone877ff982011-09-21 16:18:24 -070042 properties_)),
43 device_(new MockDevice(control_interface(),
44 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080045 metrics(),
Chris Masone877ff982011-09-21 16:18:24 -070046 manager(),
47 "null0",
48 "addr0",
49 0)) {
Chris Masone7aa5f902011-07-11 11:13:35 -070050 }
Chris Masone88cbd5f2011-07-03 14:30:04 -070051
52 virtual ~DefaultProfileTest() {}
53
Chris Masone6515aab2011-10-12 16:19:09 -070054 virtual void SetUp() {
55 PropertyStoreTest::SetUp();
56 FilePath final_path;
57 ASSERT_TRUE(profile_->GetStoragePath(&final_path));
58 scoped_ptr<KeyFileStore> storage(new KeyFileStore(&real_glib_));
59 storage->set_path(final_path);
60 ASSERT_TRUE(storage->Open());
61 profile_->set_storage(storage.release()); // Passes ownership.
62 }
63
Chris Masone88cbd5f2011-07-03 14:30:04 -070064 protected:
Chris Masone2ae797d2011-08-23 20:41:00 -070065 static const char kTestStoragePath[];
66
Chris Masone6515aab2011-10-12 16:19:09 -070067 GLib real_glib_;
Paul Stewart870523b2012-01-11 17:00:42 -080068 scoped_refptr<DefaultProfile> profile_;
Chris Masone877ff982011-09-21 16:18:24 -070069 scoped_refptr<MockDevice> device_;
Chris Masone88cbd5f2011-07-03 14:30:04 -070070 Manager::Properties properties_;
71};
72
Chris Masone2ae797d2011-08-23 20:41:00 -070073const char DefaultProfileTest::kTestStoragePath[] = "/no/where";
74
Chris Masone88cbd5f2011-07-03 14:30:04 -070075TEST_F(DefaultProfileTest, GetProperties) {
76 Error error(Error::kInvalidProperty, "");
77 {
78 map<string, ::DBus::Variant> props;
79 ::DBus::Error dbus_error;
Chris Masone7aa5f902011-07-11 11:13:35 -070080 DBusAdaptor::GetProperties(profile_->store(), &props, &dbus_error);
Chris Masone88cbd5f2011-07-03 14:30:04 -070081 ASSERT_FALSE(props.find(flimflam::kOfflineModeProperty) == props.end());
82 EXPECT_FALSE(props[flimflam::kOfflineModeProperty].reader().get_bool());
83 }
84 properties_.offline_mode = true;
85 {
86 map<string, ::DBus::Variant> props;
87 ::DBus::Error dbus_error;
Chris Masone7aa5f902011-07-11 11:13:35 -070088 DBusAdaptor::GetProperties(profile_->store(), &props, &dbus_error);
Chris Masone88cbd5f2011-07-03 14:30:04 -070089 ASSERT_FALSE(props.find(flimflam::kOfflineModeProperty) == props.end());
90 EXPECT_TRUE(props[flimflam::kOfflineModeProperty].reader().get_bool());
91 }
92 {
93 Error error(Error::kInvalidProperty, "");
94 EXPECT_FALSE(
mukesh agrawalde29fa82011-09-16 16:16:36 -070095 profile_->mutable_store()->SetBoolProperty(
96 flimflam::kOfflineModeProperty,
97 true,
98 &error));
Chris Masone88cbd5f2011-07-03 14:30:04 -070099 }
100}
101
Chris Masoneaa482372011-09-14 16:40:37 -0700102TEST_F(DefaultProfileTest, Save) {
Chris Masoneb9c00592011-10-06 13:10:39 -0700103 scoped_ptr<MockStore> storage(new MockStore);
104 EXPECT_CALL(*storage.get(), SetString(DefaultProfile::kStorageId,
105 DefaultProfile::kStorageName,
106 DefaultProfile::kDefaultId))
Chris Masoneaa482372011-09-14 16:40:37 -0700107 .WillOnce(Return(true));
Chris Masoneb9c00592011-10-06 13:10:39 -0700108 EXPECT_CALL(*storage.get(), SetString(DefaultProfile::kStorageId,
Paul Stewartd32f4842012-01-11 16:08:13 -0800109 DefaultProfile::kStorageHostName,
Chris Masoneb9c00592011-10-06 13:10:39 -0700110 ""))
Chris Masoneaa482372011-09-14 16:40:37 -0700111 .WillOnce(Return(true));
Chris Masoneb9c00592011-10-06 13:10:39 -0700112 EXPECT_CALL(*storage.get(), SetBool(DefaultProfile::kStorageId,
113 DefaultProfile::kStorageOfflineMode,
114 false))
Chris Masoneaa482372011-09-14 16:40:37 -0700115 .WillOnce(Return(true));
Paul Stewartd32f4842012-01-11 16:08:13 -0800116 EXPECT_CALL(*storage.get(), SetString(DefaultProfile::kStorageId,
117 DefaultProfile::kStorageCheckPortalList,
118 ""))
119 .WillOnce(Return(true));
Paul Stewarte6927402012-01-23 16:11:30 -0800120 EXPECT_CALL(*storage.get(), SetString(DefaultProfile::kStorageId,
121 DefaultProfile::kStoragePortalURL,
122 ""))
123 .WillOnce(Return(true));
Paul Stewartc681fa02012-03-02 19:40:04 -0800124 EXPECT_CALL(*storage.get(),
125 SetString(DefaultProfile::kStorageId,
126 DefaultProfile::kStoragePortalCheckInterval,
127 "0"))
128 .WillOnce(Return(true));
Chris Masoneb9c00592011-10-06 13:10:39 -0700129 EXPECT_CALL(*storage.get(), Flush()).WillOnce(Return(true));
Chris Masone877ff982011-09-21 16:18:24 -0700130
Chris Masoneb9c00592011-10-06 13:10:39 -0700131 EXPECT_CALL(*device_.get(), Save(storage.get())).WillOnce(Return(true));
132 profile_->set_storage(storage.release());
Chris Masone877ff982011-09-21 16:18:24 -0700133
134 manager()->RegisterDevice(device_);
Chris Masoneb9c00592011-10-06 13:10:39 -0700135 ASSERT_TRUE(profile_->Save());
Chris Masone877ff982011-09-21 16:18:24 -0700136 manager()->DeregisterDevice(device_);
Chris Masoneaa482372011-09-14 16:40:37 -0700137}
138
Paul Stewarte6927402012-01-23 16:11:30 -0800139TEST_F(DefaultProfileTest, LoadManagerDefaultProperties) {
140 scoped_ptr<MockStore> storage(new MockStore);
141 EXPECT_CALL(*storage.get(), GetString(DefaultProfile::kStorageId,
142 DefaultProfile::kStorageHostName,
143 _))
144 .WillOnce(Return(false));
145 EXPECT_CALL(*storage.get(), GetBool(DefaultProfile::kStorageId,
146 DefaultProfile::kStorageOfflineMode,
147 _))
148 .WillOnce(Return(false));
149 EXPECT_CALL(*storage.get(), GetString(DefaultProfile::kStorageId,
150 DefaultProfile::kStorageCheckPortalList,
151 _))
152 .WillOnce(Return(false));
153 EXPECT_CALL(*storage.get(), GetString(DefaultProfile::kStorageId,
154 DefaultProfile::kStoragePortalURL,
155 _))
156 .WillOnce(Return(false));
Paul Stewartc681fa02012-03-02 19:40:04 -0800157 EXPECT_CALL(*storage.get(),
158 GetString(DefaultProfile::kStorageId,
159 DefaultProfile::kStoragePortalCheckInterval,
160 _))
161 .WillOnce(Return(false));
Paul Stewarte6927402012-01-23 16:11:30 -0800162 profile_->set_storage(storage.release());
163
164 Manager::Properties manager_props;
165 ASSERT_TRUE(profile_->LoadManagerProperties(&manager_props));
166 EXPECT_EQ("", manager_props.host_name);
167 EXPECT_FALSE(manager_props.offline_mode);
Paul Stewartf555cf82012-03-15 14:42:43 -0700168 EXPECT_EQ(PortalDetector::kDefaultCheckPortalList,
169 manager_props.check_portal_list);
Paul Stewarte6927402012-01-23 16:11:30 -0800170 EXPECT_EQ(PortalDetector::kDefaultURL, manager_props.portal_url);
Paul Stewartc681fa02012-03-02 19:40:04 -0800171 EXPECT_EQ(PortalDetector::kDefaultCheckIntervalSeconds,
172 manager_props.portal_check_interval_seconds);
Paul Stewarte6927402012-01-23 16:11:30 -0800173}
174
Paul Stewart870523b2012-01-11 17:00:42 -0800175TEST_F(DefaultProfileTest, LoadManagerProperties) {
176 scoped_ptr<MockStore> storage(new MockStore);
Paul Stewartd32f4842012-01-11 16:08:13 -0800177 const string host_name("hostname");
178 EXPECT_CALL(*storage.get(), GetString(DefaultProfile::kStorageId,
179 DefaultProfile::kStorageHostName,
180 _))
181 .WillOnce(DoAll(SetArgumentPointee<2>(host_name), Return(true)));
Paul Stewart870523b2012-01-11 17:00:42 -0800182 EXPECT_CALL(*storage.get(), GetBool(DefaultProfile::kStorageId,
183 DefaultProfile::kStorageOfflineMode,
184 _))
185 .WillOnce(DoAll(SetArgumentPointee<2>(true), Return(true)));
186 const string portal_list("technology1,technology2");
187 EXPECT_CALL(*storage.get(), GetString(DefaultProfile::kStorageId,
188 DefaultProfile::kStorageCheckPortalList,
189 _))
190 .WillOnce(DoAll(SetArgumentPointee<2>(portal_list), Return(true)));
Paul Stewarte6927402012-01-23 16:11:30 -0800191 const string portal_url("http://www.chromium.org");
192 EXPECT_CALL(*storage.get(), GetString(DefaultProfile::kStorageId,
193 DefaultProfile::kStoragePortalURL,
194 _))
195 .WillOnce(DoAll(SetArgumentPointee<2>(portal_url), Return(true)));
Paul Stewartc681fa02012-03-02 19:40:04 -0800196 const string portal_check_interval_string("10");
197 const int portal_check_interval_int = 10;
198 EXPECT_CALL(*storage.get(),
199 GetString(DefaultProfile::kStorageId,
200 DefaultProfile::kStoragePortalCheckInterval,
201 _))
202 .WillOnce(DoAll(SetArgumentPointee<2>(portal_check_interval_string),
203 Return(true)));
Paul Stewart870523b2012-01-11 17:00:42 -0800204 profile_->set_storage(storage.release());
205
206 Manager::Properties manager_props;
207 ASSERT_TRUE(profile_->LoadManagerProperties(&manager_props));
Paul Stewartd32f4842012-01-11 16:08:13 -0800208 EXPECT_EQ(host_name, manager_props.host_name);
Paul Stewart870523b2012-01-11 17:00:42 -0800209 EXPECT_TRUE(manager_props.offline_mode);
210 EXPECT_EQ(portal_list, manager_props.check_portal_list);
Paul Stewarte6927402012-01-23 16:11:30 -0800211 EXPECT_EQ(portal_url, manager_props.portal_url);
Paul Stewartc681fa02012-03-02 19:40:04 -0800212 EXPECT_EQ(portal_check_interval_int,
213 manager_props.portal_check_interval_seconds);
Paul Stewart870523b2012-01-11 17:00:42 -0800214}
215
Chris Masone2ae797d2011-08-23 20:41:00 -0700216TEST_F(DefaultProfileTest, GetStoragePath) {
217 FilePath path;
218 EXPECT_TRUE(profile_->GetStoragePath(&path));
Chris Masone6515aab2011-10-12 16:19:09 -0700219 EXPECT_EQ(storage_path() + "/default.profile", path.value());
Chris Masone2ae797d2011-08-23 20:41:00 -0700220}
221
Chris Masone88cbd5f2011-07-03 14:30:04 -0700222} // namespace shill