#ifdef UNSAFE_BUFFERS_BUILD
#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"
namespace policy {
namespace {
#if BUILDFLAG(IS_CHROMEOS_ASH)
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
}
TEST(GeneratePolicySource, ChromeSchemaData) { … }
TEST(GeneratePolicySource, PolicyScope) { … }
TEST(GeneratePolicySource, PolicyDetails) { … }
#if BUILDFLAG(IS_CHROMEOS)
TEST(GeneratePolicySource, SetEnterpriseDefaults) {
PolicyMap policy_map;
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);
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;
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);
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;
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);
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
}