blob: 695e0921c89f257ff66d09649d890ecc32b52cc5 [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"
Chris Masone88cbd5f2011-07-03 14:30:04 -070022#include "shill/property_store_unittest.h"
23
24using std::map;
25using std::string;
Chris Masone877ff982011-09-21 16:18:24 -070026using std::vector;
Chris Masone88cbd5f2011-07-03 14:30:04 -070027using ::testing::_;
Paul Stewart870523b2012-01-11 17:00:42 -080028using ::testing::DoAll;
Chris Masoneaa482372011-09-14 16:40:37 -070029using ::testing::Return;
Paul Stewart870523b2012-01-11 17:00:42 -080030using ::testing::SetArgumentPointee;
Chris Masone88cbd5f2011-07-03 14:30:04 -070031
32namespace shill {
33
34class DefaultProfileTest : public PropertyStoreTest {
35 public:
Chris Masone7aa5f902011-07-11 11:13:35 -070036 DefaultProfileTest()
Chris Masone2176a882011-09-14 22:29:15 -070037 : profile_(new DefaultProfile(control_interface(),
Chris Masone9d779932011-08-25 16:33:41 -070038 manager(),
Chris Masone6515aab2011-10-12 16:19:09 -070039 FilePath(storage_path()),
Chris Masone877ff982011-09-21 16:18:24 -070040 properties_)),
41 device_(new MockDevice(control_interface(),
42 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080043 metrics(),
Chris Masone877ff982011-09-21 16:18:24 -070044 manager(),
45 "null0",
46 "addr0",
47 0)) {
Chris Masone7aa5f902011-07-11 11:13:35 -070048 }
Chris Masone88cbd5f2011-07-03 14:30:04 -070049
50 virtual ~DefaultProfileTest() {}
51
Chris Masone6515aab2011-10-12 16:19:09 -070052 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 Masone88cbd5f2011-07-03 14:30:04 -070062 protected:
Chris Masone2ae797d2011-08-23 20:41:00 -070063 static const char kTestStoragePath[];
64
Chris Masone6515aab2011-10-12 16:19:09 -070065 GLib real_glib_;
Paul Stewart870523b2012-01-11 17:00:42 -080066 scoped_refptr<DefaultProfile> profile_;
Chris Masone877ff982011-09-21 16:18:24 -070067 scoped_refptr<MockDevice> device_;
Chris Masone88cbd5f2011-07-03 14:30:04 -070068 Manager::Properties properties_;
69};
70
Chris Masone2ae797d2011-08-23 20:41:00 -070071const char DefaultProfileTest::kTestStoragePath[] = "/no/where";
72
Chris Masone88cbd5f2011-07-03 14:30:04 -070073TEST_F(DefaultProfileTest, GetProperties) {
74 Error error(Error::kInvalidProperty, "");
75 {
76 map<string, ::DBus::Variant> props;
77 ::DBus::Error dbus_error;
Chris Masone7aa5f902011-07-11 11:13:35 -070078 DBusAdaptor::GetProperties(profile_->store(), &props, &dbus_error);
Chris Masone88cbd5f2011-07-03 14:30:04 -070079 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 Masone7aa5f902011-07-11 11:13:35 -070086 DBusAdaptor::GetProperties(profile_->store(), &props, &dbus_error);
Chris Masone88cbd5f2011-07-03 14:30:04 -070087 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 agrawalde29fa82011-09-16 16:16:36 -070093 profile_->mutable_store()->SetBoolProperty(
94 flimflam::kOfflineModeProperty,
95 true,
96 &error));
Chris Masone88cbd5f2011-07-03 14:30:04 -070097 }
98}
99
Chris Masoneaa482372011-09-14 16:40:37 -0700100TEST_F(DefaultProfileTest, Save) {
Chris Masoneb9c00592011-10-06 13:10:39 -0700101 scoped_ptr<MockStore> storage(new MockStore);
102 EXPECT_CALL(*storage.get(), SetString(DefaultProfile::kStorageId,
103 DefaultProfile::kStorageName,
104 DefaultProfile::kDefaultId))
Chris Masoneaa482372011-09-14 16:40:37 -0700105 .WillOnce(Return(true));
Chris Masoneb9c00592011-10-06 13:10:39 -0700106 EXPECT_CALL(*storage.get(), SetString(DefaultProfile::kStorageId,
Paul Stewartd32f4842012-01-11 16:08:13 -0800107 DefaultProfile::kStorageHostName,
Chris Masoneb9c00592011-10-06 13:10:39 -0700108 ""))
Chris Masoneaa482372011-09-14 16:40:37 -0700109 .WillOnce(Return(true));
Chris Masoneb9c00592011-10-06 13:10:39 -0700110 EXPECT_CALL(*storage.get(), SetBool(DefaultProfile::kStorageId,
111 DefaultProfile::kStorageOfflineMode,
112 false))
Chris Masoneaa482372011-09-14 16:40:37 -0700113 .WillOnce(Return(true));
Paul Stewartd32f4842012-01-11 16:08:13 -0800114 EXPECT_CALL(*storage.get(), SetString(DefaultProfile::kStorageId,
115 DefaultProfile::kStorageCheckPortalList,
116 ""))
117 .WillOnce(Return(true));
Chris Masoneb9c00592011-10-06 13:10:39 -0700118 EXPECT_CALL(*storage.get(), Flush()).WillOnce(Return(true));
Chris Masone877ff982011-09-21 16:18:24 -0700119
Chris Masoneb9c00592011-10-06 13:10:39 -0700120 EXPECT_CALL(*device_.get(), Save(storage.get())).WillOnce(Return(true));
121 profile_->set_storage(storage.release());
Chris Masone877ff982011-09-21 16:18:24 -0700122
123 manager()->RegisterDevice(device_);
Chris Masoneb9c00592011-10-06 13:10:39 -0700124 ASSERT_TRUE(profile_->Save());
Chris Masone877ff982011-09-21 16:18:24 -0700125 manager()->DeregisterDevice(device_);
Chris Masoneaa482372011-09-14 16:40:37 -0700126}
127
Paul Stewart870523b2012-01-11 17:00:42 -0800128TEST_F(DefaultProfileTest, LoadManagerProperties) {
129 scoped_ptr<MockStore> storage(new MockStore);
Paul Stewartd32f4842012-01-11 16:08:13 -0800130 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 Stewart870523b2012-01-11 17:00:42 -0800135 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 Stewartd32f4842012-01-11 16:08:13 -0800148 EXPECT_EQ(host_name, manager_props.host_name);
Paul Stewart870523b2012-01-11 17:00:42 -0800149 EXPECT_TRUE(manager_props.offline_mode);
150 EXPECT_EQ(portal_list, manager_props.check_portal_list);
151}
152
Chris Masone2ae797d2011-08-23 20:41:00 -0700153TEST_F(DefaultProfileTest, GetStoragePath) {
154 FilePath path;
155 EXPECT_TRUE(profile_->GetStoragePath(&path));
Chris Masone6515aab2011-10-12 16:19:09 -0700156 EXPECT_EQ(storage_path() + "/default.profile", path.value());
Chris Masone2ae797d2011-08-23 20:41:00 -0700157}
158
Chris Masone88cbd5f2011-07-03 14:30:04 -0700159} // namespace shill