chromium/components/policy/core/common/policy_test_utils.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.

#include "components/policy/core/common/policy_test_utils.h"

#include <string>

#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/strings/sys_string_conversions.h"
#include "base/values.h"
#include "build/build_config.h"
#include "components/policy/core/common/policy_bundle.h"
#include "components/policy/policy_constants.h"

#if BUILDFLAG(IS_APPLE)
#include <CoreFoundation/CoreFoundation.h>

#include "base/apple/scoped_cftyperef.h"
#include "base/memory/scoped_policy.h"
#endif

namespace policy {

PolicyDetailsMap::PolicyDetailsMap() = default;

PolicyDetailsMap::~PolicyDetailsMap() = default;

GetChromePolicyDetailsCallback PolicyDetailsMap::GetCallback() const {}

void PolicyDetailsMap::SetDetails(const std::string& policy,
                                  const PolicyDetails* details) {}

const PolicyDetails* PolicyDetailsMap::Lookup(const std::string& policy) const {}

bool PolicyServiceIsEmpty(const PolicyService* service) {}

#if BUILDFLAG(IS_APPLE)
base::apple::ScopedCFTypeRef<CFPropertyListRef> ValueToProperty(
    const base::Value& value) {
  base::apple::ScopedCFTypeRef<CFPropertyListRef> result;

  switch (value.type()) {
    case base::Value::Type::NONE: {
      result.reset(kCFNull, base::scoped_policy::RETAIN);
      break;
    }

    case base::Value::Type::BOOLEAN: {
      result.reset(value.GetBool() ? kCFBooleanTrue : kCFBooleanFalse,
                   base::scoped_policy::RETAIN);
      break;
    }

    case base::Value::Type::INTEGER: {
      const int int_value = value.GetInt();
      result.reset(
          CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &int_value));
      break;
    }

    case base::Value::Type::DOUBLE: {
      const double double_value = value.GetDouble();
      result.reset(CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType,
                                  &double_value));
      break;
    }

    case base::Value::Type::STRING: {
      const std::string& string_value = value.GetString();
      result = base::SysUTF8ToCFStringRef(string_value);
      break;
    }

    case base::Value::Type::DICT: {
      const base::Value::Dict& value_dict = value.GetDict();
      base::apple::ScopedCFTypeRef<CFMutableDictionaryRef> cf_dict(
          CFDictionaryCreateMutable(kCFAllocatorDefault, value_dict.size(),
                                    &kCFTypeDictionaryKeyCallBacks,
                                    &kCFTypeDictionaryValueCallBacks));
      for (const auto [dict_key, dict_value] : value_dict) {
        base::apple::ScopedCFTypeRef<CFStringRef> cf_key =
            base::SysUTF8ToCFStringRef(dict_key);
        base::apple::ScopedCFTypeRef<CFPropertyListRef> cf_value =
            ValueToProperty(dict_value);
        if (cf_value) {
          CFDictionaryAddValue(cf_dict.get(), cf_key.get(), cf_value.get());
        }
      }
      result = cf_dict;
      break;
    }

    case base::Value::Type::LIST: {
      const base::Value::List& list = value.GetList();
      base::apple::ScopedCFTypeRef<CFMutableArrayRef> cf_array(
          CFArrayCreateMutable(kCFAllocatorDefault, list.size(),
                               &kCFTypeArrayCallBacks));
      for (const base::Value& entry : list) {
        base::apple::ScopedCFTypeRef<CFPropertyListRef> cf_value =
            ValueToProperty(entry);
        if (cf_value) {
          CFArrayAppendValue(cf_array.get(), cf_value.get());
        }
      }
      result = cf_array;
      break;
    }

    case base::Value::Type::BINARY:
      // This type isn't converted (though it can be represented as CFData)
      // because there's no equivalent JSON type, and policy values can only
      // take valid JSON values.
      break;
  }

  return result;
}
#endif  // BUILDFLAG(IS_APPLE)

std::ostream& operator<<(std::ostream& os, const PolicyBundle& bundle) {}

std::ostream& operator<<(std::ostream& os, PolicyScope scope) {}

std::ostream& operator<<(std::ostream& os, PolicyLevel level) {}

std::ostream& operator<<(std::ostream& os, PolicyDomain domain) {}

std::ostream& operator<<(std::ostream& os, const PolicyMap& policies) {}

std::ostream& operator<<(std::ostream& os, const PolicyMap::Entry& e) {}

std::ostream& operator<<(std::ostream& os, const PolicyNamespace& ns) {}

}  // namespace policy