chromium/components/enterprise/data_controls/core/browser/conditions/attributes_condition_unittest.cc

// Copyright 2023 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/enterprise/data_controls/core/browser/conditions/attributes_condition.h"

#include <vector>

#include "base/json/json_reader.h"
#include "base/strings/stringprintf.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace data_controls {

namespace {

constexpr char kGoogleUrl[] =;
constexpr char kChromiumUrl[] =;

base::Value CreateDict(const std::string& value) {}

}  // namespace

TEST(AttributesConditionTest, InvalidSourceInputs) {}

TEST(AttributesConditionTest, InvalidDestinationInputs) {}

TEST(AttributesConditionTest, AnyURL) {}

TEST(AttributesConditionTest, SpecificSourceURL) {}

TEST(AttributesConditionTest, SpecificDestinationURL) {}

#if BUILDFLAG(IS_CHROMEOS)
TEST(AttributesConditionTest, AllComponents) {
  auto any_component = DestinationAttributesCondition::Create(CreateDict(R"(
    {
      "components": ["ARC", "CROSTINI", "PLUGIN_VM", "USB", "DRIVE", "ONEDRIVE"]
    })"));
  ASSERT_TRUE(any_component);
  for (Component component : kAllComponents) {
    ActionContext context = {.destination = {.component = component}};
    ASSERT_TRUE(any_component->IsTriggered(context));
  }
}

TEST(AttributesConditionTest, OneComponent) {
  for (Component condition_component : kAllComponents) {
    constexpr char kTemplate[] = R"({"components": ["%s"]})";
    auto one_component =
        DestinationAttributesCondition::Create(CreateDict(base::StringPrintf(
            kTemplate, GetComponentMapping(condition_component).c_str())));

    for (Component context_component : kAllComponents) {
      ActionContext context = {.destination = {.component = context_component}};
      if (context_component == condition_component) {
        ASSERT_TRUE(one_component->IsTriggered(context));
      } else {
        ASSERT_FALSE(one_component->IsTriggered(context));
      }
    }
  }
}

TEST(AttributesConditionTest, URLAndAllComponents) {
  auto any_component_or_url =
      DestinationAttributesCondition::Create(CreateDict(R"(
      {
        "urls": ["*"],
        "components": ["ARC", "CROSTINI", "PLUGIN_VM", "USB", "DRIVE",
                       "ONEDRIVE"]
      })"));
  ASSERT_TRUE(any_component_or_url);
  for (Component component : kAllComponents) {
    for (const char* url : {kGoogleUrl, kChromiumUrl}) {
      ActionContext context = {
          .destination = {.url = GURL(url), .component = component}};
      ASSERT_TRUE(any_component_or_url->IsTriggered(context));
    }
  }
}

TEST(AttributesConditionTest, URLAndOneComponent) {
  for (Component condition_component : kAllComponents) {
    constexpr char kTemplate[] =
        R"({"urls": ["google.com"], "components": ["%s"]})";
    auto google_and_one_component =
        DestinationAttributesCondition::Create(CreateDict(base::StringPrintf(
            kTemplate, GetComponentMapping(condition_component).c_str())));

    ASSERT_TRUE(google_and_one_component);
    for (Component context_component : kAllComponents) {
      for (const char* url : {kGoogleUrl, kChromiumUrl}) {
        ActionContext context = {
            .destination = {.url = GURL(url), .component = context_component}};
        if (context_component == condition_component && url == kGoogleUrl) {
          ASSERT_TRUE(google_and_one_component->IsTriggered(context))
              << "Expected " << GetComponentMapping(context_component)
              << " to trigger for " << url;
        } else {
          ASSERT_FALSE(google_and_one_component->IsTriggered(context))
              << "Expected " << GetComponentMapping(context_component)
              << " to not trigger for " << url;
        }
      }
    }
  }
}
#endif  // BUILDFLAG(IS_CHROMEOS)

TEST(AttributesConditionTest, IncognitoDestination) {}

TEST(AttributesConditionTest, IncognitoSource) {}

TEST(AttributesConditionTest, URLAndIncognitoDestination) {}

TEST(AttributesConditionTest, URLAndIncognitoSource) {}

TEST(AttributesConditionTest, URLAndNoIncognitoDestination) {}

TEST(AttributesConditionTest, URLAndNoIncognitoSource) {}

TEST(AttributesConditionTest, OtherProfileDestination) {}

TEST(AttributesConditionTest, OtherProfileSource) {}

TEST(AttributesConditionTest, URLAndOtherProfileDestination) {}

TEST(AttributesConditionTest, URLAndOtherProfileSource) {}

TEST(AttributesConditionTest, URLAndNoOtherProfileDestination) {}

TEST(AttributesConditionTest, URLAndNoOtherProfileSource) {}

TEST(AttributesConditionTest, URLOtherProfileIncognitoSource) {}

TEST(AttributesConditionTest, URLOtherProfileIncognitoDestination) {}

TEST(AttributesConditionTest, OsClipboardDestination) {}

TEST(AttributesConditionTest, OsClipboardSource) {}

}  // namespace data_controls