chromium/components/policy/core/common/generate_policy_source_unittest.cc

// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include <cstring>
#include <memory>
#include <string>

#include "base/values.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "components/policy/core/common/policy_details.h"
#include "components/policy/core/common/proxy_settings_constants.h"
#include "components/policy/core/common/schema.h"
#include "components/policy/policy_constants.h"
#include "testing/gtest/include/gtest/gtest.h"

// This unittest tests the code generated by
// chrome/tools/build/generate_policy_source.py.

namespace policy {

namespace {

#if BUILDFLAG(IS_CHROMEOS_ASH)
// Checks if two schemas are the same or not. Note that this function doesn't
// consider restrictions on integers and strings nor pattern properties.
bool IsSameSchema(Schema a, Schema b) {
  if (a.valid() != b.valid())
    return false;
  if (!a.valid())
    return true;
  if (a.type() != b.type())
    return false;
  if (a.type() == base::Value::Type::LIST)
    return IsSameSchema(a.GetItems(), b.GetItems());
  if (a.type() != base::Value::Type::DICT) {
    return true;
  }
  Schema::Iterator a_it = a.GetPropertiesIterator();
  Schema::Iterator b_it = b.GetPropertiesIterator();
  while (!a_it.IsAtEnd()) {
    if (b_it.IsAtEnd())
      return false;
    if (strcmp(a_it.key(), b_it.key()) != 0)
      return false;
    if (!IsSameSchema(a_it.schema(), b_it.schema()))
      return false;
    a_it.Advance();
    b_it.Advance();
  }
  if (!b_it.IsAtEnd())
    return false;
  return IsSameSchema(a.GetAdditionalProperties(), b.GetAdditionalProperties());
}
#endif

}  // namespace

TEST(GeneratePolicySource, ChromeSchemaData) {}

TEST(GeneratePolicySource, PolicyScope) {}

TEST(GeneratePolicySource, PolicyDetails) {}

#if BUILDFLAG(IS_CHROMEOS)
TEST(GeneratePolicySource, SetEnterpriseDefaults) {
  PolicyMap policy_map;

  // If policy not configured yet, set the enterprise default.
  SetEnterpriseUsersDefaults(&policy_map);

  const base::Value* multiprof_behavior = policy_map.GetValue(
      key::kChromeOsMultiProfileUserBehavior, base::Value::Type::STRING);
  base::Value expected("primary-only");
  EXPECT_EQ(expected, *multiprof_behavior);

  // If policy already configured, it's not changed to enterprise defaults.
  policy_map.Set(key::kChromeOsMultiProfileUserBehavior, POLICY_LEVEL_MANDATORY,
                 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD,
                 base::Value("test_value"), nullptr);
  SetEnterpriseUsersDefaults(&policy_map);
  multiprof_behavior = policy_map.GetValue(
      key::kChromeOsMultiProfileUserBehavior, base::Value::Type::STRING);
  expected = base::Value("test_value");
  EXPECT_EQ(expected, *multiprof_behavior);
}

TEST(GeneratePolicySource, SetEnterpriseSystemWideDefaults) {
  PolicyMap policy_map;

  // If policy not configured yet, set the enterprise system-wide default.
  SetEnterpriseUsersSystemWideDefaults(&policy_map);

  const base::Value* pin_unlock_autosubmit_enabled = policy_map.GetValue(
      key::kPinUnlockAutosubmitEnabled, base::Value::Type::BOOLEAN);
  ASSERT_TRUE(pin_unlock_autosubmit_enabled);
  EXPECT_FALSE(pin_unlock_autosubmit_enabled->GetBool());
  const base::Value* allow_dinosaur_easter_egg = policy_map.GetValue(
      key::kAllowDinosaurEasterEgg, base::Value::Type::BOOLEAN);
  EXPECT_EQ(nullptr, allow_dinosaur_easter_egg);

  // If policy already configured, it's not changed to enterprise defaults.
  policy_map.Set(key::kPinUnlockAutosubmitEnabled, POLICY_LEVEL_MANDATORY,
                 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(true),
                 nullptr);
  SetEnterpriseUsersSystemWideDefaults(&policy_map);
  pin_unlock_autosubmit_enabled = policy_map.GetValue(
      key::kPinUnlockAutosubmitEnabled, base::Value::Type::BOOLEAN);
  ASSERT_TRUE(pin_unlock_autosubmit_enabled);
  EXPECT_TRUE(pin_unlock_autosubmit_enabled->GetBool());
  allow_dinosaur_easter_egg = policy_map.GetValue(key::kAllowDinosaurEasterEgg,
                                                  base::Value::Type::BOOLEAN);
  EXPECT_EQ(nullptr, allow_dinosaur_easter_egg);
}

TEST(GeneratePolicySource, SetEnterpriseProfileDefaults) {
  PolicyMap policy_map;

  // If policy not configured yet, set the enterprise profile default.
  SetEnterpriseUsersProfileDefaults(&policy_map);

  const base::Value* allow_dinosaur_easter_egg = policy_map.GetValue(
      key::kAllowDinosaurEasterEgg, base::Value::Type::BOOLEAN);
  ASSERT_TRUE(allow_dinosaur_easter_egg);
  EXPECT_FALSE(allow_dinosaur_easter_egg->GetBool());
  const base::Value* pin_unlock_autosubmit_enabled = policy_map.GetValue(
      key::kPinUnlockAutosubmitEnabled, base::Value::Type::BOOLEAN);
  EXPECT_EQ(nullptr, pin_unlock_autosubmit_enabled);

  // If policy already configured, it's not changed to enterprise defaults.
  policy_map.Set(key::kAllowDinosaurEasterEgg, POLICY_LEVEL_MANDATORY,
                 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(true),
                 nullptr);
  SetEnterpriseUsersProfileDefaults(&policy_map);
  allow_dinosaur_easter_egg = policy_map.GetValue(key::kAllowDinosaurEasterEgg,
                                                  base::Value::Type::BOOLEAN);
  ASSERT_TRUE(allow_dinosaur_easter_egg);
  EXPECT_TRUE(allow_dinosaur_easter_egg->GetBool());
  pin_unlock_autosubmit_enabled = policy_map.GetValue(
      key::kPinUnlockAutosubmitEnabled, base::Value::Type::BOOLEAN);
  EXPECT_EQ(nullptr, pin_unlock_autosubmit_enabled);
}
#endif

}  // namespace policy