Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / chrome / browser / chromeos / ownership / owner_settings_service_chromeos_unittest.cc
blobaa4d68909d788b970ec2f9686fbda4d0d03de51f
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <queue>
6 #include <utility>
8 #include "base/macros.h"
9 #include "base/memory/linked_ptr.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/run_loop.h"
12 #include "base/test/scoped_path_override.h"
13 #include "base/values.h"
14 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos.h"
15 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos_factory.h"
16 #include "chrome/browser/chromeos/settings/device_settings_provider.h"
17 #include "chrome/browser/chromeos/settings/device_settings_test_helper.h"
18 #include "chrome/common/chrome_paths.h"
19 #include "chrome/test/base/scoped_testing_local_state.h"
20 #include "chrome/test/base/testing_browser_process.h"
21 #include "chrome/test/base/testing_profile.h"
22 #include "chromeos/settings/cros_settings_names.h"
23 #include "testing/gtest/include/gtest/gtest.h"
25 namespace em = enterprise_management;
27 namespace chromeos {
29 namespace {
31 void OnPrefChanged(const std::string& /* setting */) {
34 class PrefsChecker : public ownership::OwnerSettingsService::Observer {
35 public:
36 PrefsChecker(OwnerSettingsServiceChromeOS* service,
37 DeviceSettingsProvider* provider)
38 : service_(service), provider_(provider) {
39 CHECK(service_);
40 CHECK(provider_);
41 service_->AddObserver(this);
44 ~PrefsChecker() override { service_->RemoveObserver(this); }
46 // OwnerSettingsService::Observer implementation:
47 void OnSignedPolicyStored(bool success) override {
48 if (service_->HasPendingChanges())
49 return;
51 while (!set_requests_.empty()) {
52 SetRequest request = set_requests_.front();
53 set_requests_.pop();
54 const base::Value* value = provider_->Get(request.first);
55 ASSERT_TRUE(request.second->Equals(value));
57 loop_.Quit();
60 bool Set(const std::string& setting, const base::Value& value) {
61 if (!service_->Set(setting, value))
62 return false;
63 set_requests_.push(
64 SetRequest(setting, linked_ptr<base::Value>(value.DeepCopy())));
65 return true;
68 void Wait() { loop_.Run(); }
70 private:
71 OwnerSettingsServiceChromeOS* service_;
72 DeviceSettingsProvider* provider_;
73 base::RunLoop loop_;
75 typedef std::pair<std::string, linked_ptr<base::Value>> SetRequest;
76 std::queue<SetRequest> set_requests_;
78 DISALLOW_COPY_AND_ASSIGN(PrefsChecker);
81 bool FindInListValue(const std::string& needle, const base::Value* haystack) {
82 const base::ListValue* list;
83 if (!haystack->GetAsList(&list))
84 return false;
85 return list->end() != list->Find(base::StringValue(needle));
88 } // namespace
90 class OwnerSettingsServiceChromeOSTest : public DeviceSettingsTestBase {
91 public:
92 OwnerSettingsServiceChromeOSTest()
93 : service_(nullptr),
94 local_state_(TestingBrowserProcess::GetGlobal()),
95 user_data_dir_override_(chrome::DIR_USER_DATA),
96 management_settings_set_(false) {}
98 void SetUp() override {
99 DeviceSettingsTestBase::SetUp();
100 provider_.reset(new DeviceSettingsProvider(base::Bind(&OnPrefChanged),
101 &device_settings_service_));
102 owner_key_util_->SetPrivateKey(device_policy_.GetSigningKey());
103 InitOwner(device_policy_.policy_data().username(), true);
104 FlushDeviceSettings();
106 service_ = OwnerSettingsServiceChromeOSFactory::GetForBrowserContext(
107 profile_.get());
108 ASSERT_TRUE(service_);
109 ASSERT_TRUE(service_->IsOwner());
111 device_policy_.policy_data().set_management_mode(
112 em::PolicyData::LOCAL_OWNER);
113 device_policy_.Build();
114 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
115 ReloadDeviceSettings();
118 void TearDown() override { DeviceSettingsTestBase::TearDown(); }
120 void TestSingleSet(OwnerSettingsServiceChromeOS* service,
121 const std::string& setting,
122 const base::Value& in_value) {
123 PrefsChecker checker(service, provider_.get());
124 checker.Set(setting, in_value);
125 FlushDeviceSettings();
126 checker.Wait();
129 void OnManagementSettingsSet(bool success) {
130 management_settings_set_ = success;
133 protected:
134 OwnerSettingsServiceChromeOS* service_;
135 ScopedTestingLocalState local_state_;
136 scoped_ptr<DeviceSettingsProvider> provider_;
137 base::ScopedPathOverride user_data_dir_override_;
138 bool management_settings_set_;
140 private:
141 DISALLOW_COPY_AND_ASSIGN(OwnerSettingsServiceChromeOSTest);
144 TEST_F(OwnerSettingsServiceChromeOSTest, SingleSetTest) {
145 TestSingleSet(service_, kReleaseChannel, base::StringValue("dev-channel"));
146 TestSingleSet(service_, kReleaseChannel, base::StringValue("beta-channel"));
147 TestSingleSet(service_, kReleaseChannel, base::StringValue("stable-channel"));
150 TEST_F(OwnerSettingsServiceChromeOSTest, MultipleSetTest) {
151 base::FundamentalValue allow_guest(false);
152 base::StringValue release_channel("stable-channel");
153 base::FundamentalValue show_user_names(true);
155 PrefsChecker checker(service_, provider_.get());
157 checker.Set(kAccountsPrefAllowGuest, allow_guest);
158 checker.Set(kReleaseChannel, release_channel);
159 checker.Set(kAccountsPrefShowUserNamesOnSignIn, show_user_names);
161 FlushDeviceSettings();
162 checker.Wait();
165 TEST_F(OwnerSettingsServiceChromeOSTest, FailedSetRequest) {
166 device_settings_test_helper_.set_store_result(false);
167 std::string current_channel;
168 ASSERT_TRUE(provider_->Get(kReleaseChannel)->GetAsString(&current_channel));
169 ASSERT_NE(current_channel, "stable-channel");
171 // Check that DeviceSettingsProvider's cache is updated.
172 PrefsChecker checker(service_, provider_.get());
173 checker.Set(kReleaseChannel, base::StringValue("stable-channel"));
174 FlushDeviceSettings();
175 checker.Wait();
177 // Check that DeviceSettingsService's policy isn't updated.
178 ASSERT_EQ(current_channel, device_settings_service_.device_settings()
179 ->release_channel()
180 .release_channel());
183 TEST_F(OwnerSettingsServiceChromeOSTest, SetManagementSettingsModeTransition) {
184 ReloadDeviceSettings();
185 EXPECT_EQ(DeviceSettingsService::STORE_SUCCESS,
186 device_settings_service_.status());
188 // The initial management mode should be LOCAL_OWNER.
189 EXPECT_TRUE(device_settings_service_.policy_data()->has_management_mode());
190 EXPECT_EQ(em::PolicyData::LOCAL_OWNER,
191 device_settings_service_.policy_data()->management_mode());
193 OwnerSettingsServiceChromeOS::ManagementSettings management_settings;
194 management_settings.management_mode =
195 policy::MANAGEMENT_MODE_CONSUMER_MANAGED;
196 management_settings.request_token = "fake_request_token";
197 management_settings.device_id = "fake_device_id";
198 OwnerSettingsServiceChromeOS::OnManagementSettingsSetCallback
199 on_management_settings_set_callback =
200 base::Bind(&OwnerSettingsServiceChromeOSTest::OnManagementSettingsSet,
201 base::Unretained(this));
203 // LOCAL_OWNER -> CONSUMER_MANAGED: Okay.
204 service_->SetManagementSettings(management_settings,
205 on_management_settings_set_callback);
206 FlushDeviceSettings();
208 EXPECT_TRUE(management_settings_set_);
209 EXPECT_EQ(em::PolicyData::CONSUMER_MANAGED,
210 device_settings_service_.policy_data()->management_mode());
212 // CONSUMER_MANAGED -> ENTERPRISE_MANAGED: Invalid.
213 management_settings.management_mode =
214 policy::MANAGEMENT_MODE_ENTERPRISE_MANAGED;
215 service_->SetManagementSettings(management_settings,
216 on_management_settings_set_callback);
217 FlushDeviceSettings();
219 EXPECT_FALSE(management_settings_set_);
220 EXPECT_EQ(em::PolicyData::CONSUMER_MANAGED,
221 device_settings_service_.policy_data()->management_mode());
223 // CONSUMER_MANAGED -> LOCAL_OWNER: Okay.
224 management_settings.management_mode = policy::MANAGEMENT_MODE_LOCAL_OWNER;
225 service_->SetManagementSettings(management_settings,
226 on_management_settings_set_callback);
227 FlushDeviceSettings();
229 EXPECT_TRUE(management_settings_set_);
230 EXPECT_EQ(em::PolicyData::LOCAL_OWNER,
231 device_settings_service_.policy_data()->management_mode());
233 // LOCAL_OWNER -> ENTERPRISE_MANAGED: Invalid.
234 management_settings.management_mode =
235 policy::MANAGEMENT_MODE_ENTERPRISE_MANAGED;
236 service_->SetManagementSettings(management_settings,
237 on_management_settings_set_callback);
238 FlushDeviceSettings();
240 EXPECT_FALSE(management_settings_set_);
241 EXPECT_EQ(em::PolicyData::LOCAL_OWNER,
242 device_settings_service_.policy_data()->management_mode());
244 // Inject a policy data with management mode set to ENTERPRISE_MANAGED.
245 device_policy_.policy_data().set_management_mode(
246 em::PolicyData::ENTERPRISE_MANAGED);
247 device_policy_.Build();
248 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
249 ReloadDeviceSettings();
250 EXPECT_EQ(em::PolicyData::ENTERPRISE_MANAGED,
251 device_settings_service_.policy_data()->management_mode());
253 // ENTERPRISE_MANAGED -> LOCAL_OWNER: Invalid.
254 management_settings.management_mode = policy::MANAGEMENT_MODE_LOCAL_OWNER;
255 service_->SetManagementSettings(management_settings,
256 on_management_settings_set_callback);
257 FlushDeviceSettings();
259 EXPECT_FALSE(management_settings_set_);
260 EXPECT_EQ(em::PolicyData::ENTERPRISE_MANAGED,
261 device_settings_service_.policy_data()->management_mode());
263 // ENTERPRISE_MANAGED -> CONSUMER_MANAGED: Invalid.
264 management_settings.management_mode =
265 policy::MANAGEMENT_MODE_CONSUMER_MANAGED;
266 service_->SetManagementSettings(management_settings,
267 on_management_settings_set_callback);
268 FlushDeviceSettings();
270 EXPECT_FALSE(management_settings_set_);
271 EXPECT_EQ(em::PolicyData::ENTERPRISE_MANAGED,
272 device_settings_service_.policy_data()->management_mode());
275 TEST_F(OwnerSettingsServiceChromeOSTest, SetManagementSettingsSuccess) {
276 ReloadDeviceSettings();
277 EXPECT_EQ(DeviceSettingsService::STORE_SUCCESS,
278 device_settings_service_.status());
280 OwnerSettingsServiceChromeOS::ManagementSettings management_settings;
281 management_settings.management_mode =
282 policy::MANAGEMENT_MODE_CONSUMER_MANAGED;
283 management_settings.request_token = "fake_request_token";
284 management_settings.device_id = "fake_device_id";
285 service_->SetManagementSettings(
286 management_settings,
287 base::Bind(&OwnerSettingsServiceChromeOSTest::OnManagementSettingsSet,
288 base::Unretained(this)));
289 FlushDeviceSettings();
291 EXPECT_EQ(DeviceSettingsService::STORE_SUCCESS,
292 device_settings_service_.status());
293 ASSERT_TRUE(device_settings_service_.device_settings());
295 // Check that the loaded policy_data contains the expected values.
296 const em::PolicyData* policy_data = device_settings_service_.policy_data();
297 EXPECT_EQ(policy::dm_protocol::kChromeDevicePolicyType,
298 policy_data->policy_type());
299 EXPECT_EQ(device_settings_service_.GetUsername(), policy_data->username());
300 EXPECT_EQ(em::PolicyData::CONSUMER_MANAGED, policy_data->management_mode());
301 EXPECT_EQ("fake_request_token", policy_data->request_token());
302 EXPECT_EQ("fake_device_id", policy_data->device_id());
305 TEST_F(OwnerSettingsServiceChromeOSTest, ForceWhitelist) {
306 EXPECT_FALSE(FindInListValue(device_policy_.policy_data().username(),
307 provider_->Get(kAccountsPrefUsers)));
308 // Force a settings write.
309 TestSingleSet(service_, kReleaseChannel, base::StringValue("dev-channel"));
310 EXPECT_TRUE(FindInListValue(device_policy_.policy_data().username(),
311 provider_->Get(kAccountsPrefUsers)));
314 class OwnerSettingsServiceChromeOSNoOwnerTest
315 : public OwnerSettingsServiceChromeOSTest {
316 public:
317 OwnerSettingsServiceChromeOSNoOwnerTest() {}
318 ~OwnerSettingsServiceChromeOSNoOwnerTest() override {}
320 void SetUp() override {
321 DeviceSettingsTestBase::SetUp();
322 provider_.reset(new DeviceSettingsProvider(base::Bind(&OnPrefChanged),
323 &device_settings_service_));
324 FlushDeviceSettings();
325 service_ = OwnerSettingsServiceChromeOSFactory::GetForBrowserContext(
326 profile_.get());
327 ASSERT_TRUE(service_);
328 ASSERT_FALSE(service_->IsOwner());
331 void TearDown() override { DeviceSettingsTestBase::TearDown(); }
333 private:
334 DISALLOW_COPY_AND_ASSIGN(OwnerSettingsServiceChromeOSNoOwnerTest);
337 TEST_F(OwnerSettingsServiceChromeOSNoOwnerTest, SingleSetTest) {
338 ASSERT_FALSE(service_->SetBoolean(kAccountsPrefAllowGuest, false));
341 TEST_F(OwnerSettingsServiceChromeOSNoOwnerTest, TakeOwnershipForceWhitelist) {
342 EXPECT_FALSE(FindInListValue(device_policy_.policy_data().username(),
343 provider_->Get(kAccountsPrefUsers)));
344 owner_key_util_->SetPrivateKey(device_policy_.GetSigningKey());
345 InitOwner(device_policy_.policy_data().username(), true);
346 ReloadDeviceSettings();
347 ASSERT_TRUE(service_->IsOwner());
349 EXPECT_TRUE(FindInListValue(device_policy_.policy_data().username(),
350 provider_->Get(kAccountsPrefUsers)));
353 } // namespace chromeos