File: dlp_rules_manager_test_utils.cc

package info (click to toggle)
chromium 139.0.7258.127-1~deb13u1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,096 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (123 lines) | stat: -rw-r--r-- 3,933 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/policy/dlp/test/dlp_rules_manager_test_utils.h"

#include <utility>

#include "base/check.h"

namespace policy::dlp_test_util {

namespace {

const char kName[] = "name";
const char kDescription[] = "description";
const char kRuleId[] = "rule_id";
const char kSources[] = "sources";
const char kUrls[] = "urls";
const char kDestinations[] = "destinations";
const char kComponents[] = "components";
const char kRestrictions[] = "restrictions";
const char kClass[] = "class";
const char kLevel[] = "level";

}  // namespace

base::Value::Dict CreateSources(base::Value::List urls) {
  return base::Value::Dict().Set(kUrls, std::move(urls));
}

base::Value::Dict CreateDestinations(
    std::optional<base::Value::List> urls,
    std::optional<base::Value::List> components) {
  base::Value::Dict dsts;
  if (urls.has_value()) {
    dsts.Set(kUrls, std::move(urls.value()));
  }
  if (components.has_value()) {
    dsts.Set(kComponents, std::move(components.value()));
  }
  return dsts;
}

base::Value::Dict CreateRestrictionWithLevel(const std::string& restriction,
                                             const std::string& level) {
  return base::Value::Dict().Set(kClass, restriction).Set(kLevel, level);
}

base::Value::Dict CreateRule(const std::string& name,
                             const std::string& desc,
                             const std::string& rule_id,
                             base::Value::List src_urls,
                             std::optional<base::Value::List> dst_urls,
                             std::optional<base::Value::List> dst_components,
                             base::Value::List restrictions) {
  auto rule =
      base::Value::Dict()
          .Set(kName, name)
          .Set(kDescription, desc)
          .Set(kSources, CreateSources(std::move(src_urls)))
          .Set(kDestinations, CreateDestinations(std::move(dst_urls),
                                                 std::move(dst_components)))
          .Set(kRestrictions, std::move(restrictions));
  if (!rule_id.empty()) {
    rule.Set(kRuleId, rule_id);
  }
  return rule;
}

DlpRule::DlpRule(const std::string& name,
                 const std::string& description,
                 const std::string& id)
    : name(name), description(description), id(id) {}
DlpRule::DlpRule() = default;
DlpRule::~DlpRule() = default;
DlpRule::DlpRule(const DlpRule& other) = default;
DlpRule& DlpRule::AddSrcUrl(const std::string& url) {
  src_urls.emplace_back(url);
  return *this;
}
DlpRule& DlpRule::AddDstUrl(const std::string& url) {
  dst_urls.emplace_back(url);
  return *this;
}
DlpRule& DlpRule::AddDstComponent(const std::string& component) {
  dst_components.emplace_back(component);
  return *this;
}
DlpRule& DlpRule::AddRestriction(const std::string& type,
                                 const std::string& level) {
  restrictions.emplace_back(type, level);
  return *this;
}

base::Value::Dict DlpRule::Create() const {
  base::Value::List src_urls_list;
  for (const std::string& src : src_urls) {
    src_urls_list.Append(src);
  }

  base::Value::List dst_urls_list;
  for (const std::string& dst : dst_urls) {
    dst_urls_list.Append(dst);
  }

  base::Value::List dst_components_list;
  for (const std::string& component : dst_components) {
    dst_components_list.Append(component);
  }

  base::Value::List restrictions_list;
  for (const auto& [type, level] : restrictions) {
    restrictions_list.Append(
        base::Value::Dict().Set("class", type).Set("level", level));
  }

  return CreateRule(name, description, id, std::move(src_urls_list),
                    std::move(dst_urls_list), std::move(dst_components_list),
                    std::move(restrictions_list));
}

}  // namespace policy::dlp_test_util