device_settings_cache_unittest.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
1cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com// Use of this source code is governed by a BSD-style license that can be
3cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com// found in the LICENSE file.
4cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com
5cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com#include "chrome/browser/chromeos/settings/device_settings_cache.h"
6cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com
7cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com#include "chrome/browser/policy/proto/chrome_device_policy.pb.h"
8cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com#include "chrome/browser/policy/proto/device_management_backend.pb.h"
9cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com#include "chrome/common/pref_names.h"
108cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "chrome/test/base/testing_pref_service.h"
118cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "testing/gtest/include/gtest/gtest.h"
128cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
138cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comnamespace em = enterprise_management;
148cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
158cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comnamespace chromeos {
168cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
178cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comclass DeviceSettingsCacheTest : public testing::Test {
188cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com protected:
198cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  virtual void SetUp() {
208cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    // prepare some data.
218cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    policy_.set_policy_type("google/chromeos/device");
228cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    em::ChromeDeviceSettingsProto pol;
238cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    pol.mutable_allow_new_users()->set_allow_new_users(false);
248cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    policy_.set_policy_value(pol.SerializeAsString());
258cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
268cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    device_settings_cache::RegisterPrefs(&local_state_);
278cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  }
288cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
298cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  TestingPrefService local_state_;
308cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  em::PolicyData policy_;
318cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com};
328cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
338cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comTEST_F(DeviceSettingsCacheTest, Basic) {
348cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  EXPECT_TRUE(device_settings_cache::Store(policy_, &local_state_));
358cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
368cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  em::PolicyData policy_out;
378cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  EXPECT_TRUE(device_settings_cache::Retrieve(&policy_out, &local_state_));
388cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
398cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  EXPECT_TRUE(policy_out.has_policy_type());
408cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  EXPECT_TRUE(policy_out.has_policy_value());
418cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
428cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  em::ChromeDeviceSettingsProto pol;
438cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  pol.ParseFromString(policy_out.policy_value());
448cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  EXPECT_TRUE(pol.has_allow_new_users());
458cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  EXPECT_FALSE(pol.allow_new_users().allow_new_users());
468cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com}
478cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
488cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comTEST_F(DeviceSettingsCacheTest, CorruptData) {
498cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  EXPECT_TRUE(device_settings_cache::Store(policy_, &local_state_));
508cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
518cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  local_state_.SetString(prefs::kDeviceSettingsCache, "blaaa");
528cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
538cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  em::PolicyData policy_out;
548cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  EXPECT_FALSE(device_settings_cache::Retrieve(&policy_out, &local_state_));
558cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com}
568cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
578cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com}  // namespace chromeos
588cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com