File: dlp_confidential_contents_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 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 (325 lines) | stat: -rw-r--r-- 11,930 bytes parent folder | download | duplicates (6)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright 2021 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/dlp_confidential_contents.h"

#include <algorithm>
#include <sstream>
#include <string>

#include "base/test/metrics/histogram_tester.h"
#include "base/test/test_mock_time_task_runner.h"
#include "chrome/browser/chromeos/policy/dlp/dlp_rules_manager.h"
#include "chrome/test/base/testing_profile.h"
#include "components/enterprise/data_controls/core/browser/dlp_histogram_helper.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/web_contents_tester.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace policy {

constexpr DlpRulesManager::Restriction kRestriction =
    DlpRulesManager::Restriction::kPrinting;

class DlpConfidentialContentsTest : public testing::Test {
 public:
  const std::u16string title1 = u"Example1";
  const std::u16string title2 = u"Example2";
  const std::u16string title3 = u"Example3";
  const GURL url1 = GURL("https://example1.com");
  const GURL url2 = GURL("https://example2.com");
  const GURL url3 = GURL("https://example3.com");

  DlpConfidentialContentsTest()
      : profile_(std::make_unique<TestingProfile>()) {}
  DlpConfidentialContentsTest(const DlpConfidentialContentsTest&) = delete;
  DlpConfidentialContentsTest& operator=(const DlpConfidentialContentsTest&) =
      delete;
  ~DlpConfidentialContentsTest() override = default;

  std::unique_ptr<content::WebContents> CreateWebContents(
      const std::u16string& title,
      const GURL& url) {
    std::unique_ptr<content::WebContents> web_contents =
        content::WebContentsTester::CreateTestWebContents(profile_.get(),
                                                          nullptr);
    content::WebContentsTester* tester =
        content::WebContentsTester::For(web_contents.get());
    tester->SetTitle(title);
    tester->SetLastCommittedURL(url);
    return web_contents;
  }

  DlpConfidentialContent CreateConfidentialContent(const std::u16string& title,
                                                   const GURL& url) {
    return DlpConfidentialContent(CreateWebContents(title, url).get());
  }

  // Helper to check whether |contents| has an element corresponding to
  // |web_contents|.
  bool Contains(const DlpConfidentialContents& contents,
                content::WebContents* web_contents) {
    return std::ranges::any_of(contents.GetContents(),
                               [&](const DlpConfidentialContent& content) {
                                 return content.url.EqualsIgnoringRef(
                                     web_contents->GetLastCommittedURL());
                               });
  }

 protected:
  base::HistogramTester histogram_tester_;

 private:
  content::BrowserTaskEnvironment task_environment_;
  content::RenderViewHostTestEnabler render_view_host_test_enabler_;
  const std::unique_ptr<TestingProfile> profile_;
};

TEST_F(DlpConfidentialContentsTest, ComparisonWithDifferentUrls) {
  DlpConfidentialContent content1 = CreateConfidentialContent(title1, url1);
  DlpConfidentialContent content2 = CreateConfidentialContent(title2, url2);

  EXPECT_TRUE(content1 != content2);
  EXPECT_TRUE(content1 < content2);
  EXPECT_TRUE(content1 <= content2);
  EXPECT_TRUE(content2 > content1);
  EXPECT_TRUE(content2 >= content1);
}

TEST_F(DlpConfidentialContentsTest, ComparisonWithSameUrls) {
  DlpConfidentialContent content1 = CreateConfidentialContent(title1, url1);
  DlpConfidentialContent content2 = CreateConfidentialContent(title2, url1);

  EXPECT_TRUE(content1 == content2);
  EXPECT_FALSE(content1 != content2);
  EXPECT_FALSE(content1 < content2);
  EXPECT_TRUE(content1 <= content2);
  EXPECT_FALSE(content2 > content1);
  EXPECT_TRUE(content2 >= content1);
}

TEST_F(DlpConfidentialContentsTest, ComparisonAfterAssignement) {
  DlpConfidentialContent content1 = CreateConfidentialContent(title1, url1);
  DlpConfidentialContent content2 = CreateConfidentialContent(title2, url2);

  EXPECT_FALSE(content1 == content2);

  content2 = content1;
  EXPECT_TRUE(content1 == content2);
}

TEST_F(DlpConfidentialContentsTest, EqualityIgnoresTheRef) {
  const GURL url1 = GURL("https://example.com#first_ref");
  const GURL url2 = GURL("https://example.com#second_ref");
  EXPECT_NE(url1, url2);
  EXPECT_TRUE(url1.EqualsIgnoringRef(url2));

  DlpConfidentialContent content1 = CreateConfidentialContent(title1, url1);
  DlpConfidentialContent content2 = CreateConfidentialContent(title2, url2);
  EXPECT_EQ(content1, content2);
}

TEST_F(DlpConfidentialContentsTest, EmptyContents) {
  DlpConfidentialContents contents;
  EXPECT_TRUE(contents.IsEmpty());
}

TEST_F(DlpConfidentialContentsTest, DuplicateConfidentialDataAdded) {
  DlpConfidentialContents contents;
  auto web_contents = CreateWebContents(title1, url1);
  contents.Add(web_contents.get());
  contents.Add(web_contents.get());
  EXPECT_EQ(contents.GetContents().size(), 1u);
  EXPECT_TRUE(Contains(contents, web_contents.get()));
}

TEST_F(DlpConfidentialContentsTest, ClearAndAdd) {
  DlpConfidentialContents contents;

  auto web_contents1 = CreateWebContents(title1, url1);
  auto web_contents2 = CreateWebContents(title2, url2);
  auto web_contents3 = CreateWebContents(title3, url3);

  contents.Add(web_contents1.get());
  contents.Add(web_contents2.get());
  EXPECT_EQ(contents.GetContents().size(), 2u);

  contents.ClearAndAdd(CreateWebContents(title3, url3).get());
  EXPECT_EQ(contents.GetContents().size(), 1u);
  EXPECT_FALSE(Contains(contents, web_contents1.get()));
  EXPECT_FALSE(Contains(contents, web_contents2.get()));
  EXPECT_TRUE(Contains(contents, web_contents3.get()));
}

TEST_F(DlpConfidentialContentsTest, InsertOrUpdateDropsDuplicates) {
  DlpConfidentialContents contents1;
  DlpConfidentialContents contents2;

  auto web_contents1 = CreateWebContents(title1, url1);
  auto web_contents2 = CreateWebContents(title2, url2);
  auto web_contents3 = CreateWebContents(title3, url3);

  contents1.Add(web_contents1.get());
  contents1.Add(web_contents2.get());

  contents2.Add(web_contents1.get());
  contents2.Add(web_contents3.get());

  EXPECT_EQ(contents1.GetContents().size(), 2u);
  EXPECT_EQ(contents2.GetContents().size(), 2u);
  EXPECT_FALSE(Contains(contents1, web_contents3.get()));

  contents1.InsertOrUpdate(contents2);

  EXPECT_EQ(contents1.GetContents().size(), 3u);
  EXPECT_EQ(contents2.GetContents().size(), 2u);
  EXPECT_TRUE(Contains(contents1, web_contents3.get()));
}

TEST_F(DlpConfidentialContentsTest, InsertOrUpdateUpdatesTitles) {
  const GURL url1 = GURL("https://example.com#first_ref");
  const GURL url2 = GURL("https://example.com#second_ref");
  EXPECT_NE(url1, url2);
  EXPECT_TRUE(url1.EqualsIgnoringRef(url2));

  DlpConfidentialContents contents1;
  DlpConfidentialContents contents2;

  auto web_contents1 = CreateWebContents(title1, url1);
  auto web_contents2 = CreateWebContents(title2, url2);
  auto web_contents3 = CreateWebContents(title3, url3);

  contents1.Add(web_contents1.get());
  contents2.Add(web_contents2.get());
  contents2.Add(web_contents3.get());

  EXPECT_EQ(contents1.GetContents().begin()->title, title1);

  contents1.InsertOrUpdate(contents2);

  EXPECT_EQ(contents2.GetContents().size(), 2u);
  EXPECT_EQ(contents1.GetContents().begin()->title, title2);
}

TEST_F(DlpConfidentialContentsTest, EqualityDoesNotDependOnOrder) {
  DlpConfidentialContents contents1;
  DlpConfidentialContents contents2;

  auto web_contents1 = CreateWebContents(title1, url1);
  auto web_contents2 = CreateWebContents(title2, url2);
  auto web_contents3 = CreateWebContents(title3, url3);

  contents1.Add(web_contents1.get());
  contents1.Add(web_contents2.get());
  contents2.Add(web_contents2.get());
  contents2.Add(web_contents1.get());
  EXPECT_TRUE(contents1 == contents2);
  EXPECT_TRUE(EqualWithTitles(contents1, contents2));

  contents1.Add(web_contents3.get());
  EXPECT_TRUE(contents1 != contents2);
  EXPECT_FALSE(EqualWithTitles(contents1, contents2));
}

TEST_F(DlpConfidentialContentsTest, EqualityDoesNotDependOnTitle) {
  DlpConfidentialContents contents1;
  DlpConfidentialContents contents2;
  DlpConfidentialContents contents3;

  auto web_contents1 = CreateWebContents(title1, url1);
  auto web_contents2 = CreateWebContents(title2, url1);

  contents1.Add(web_contents1.get());
  contents2.Add(web_contents1.get());
  contents3.Add(web_contents2.get());
  EXPECT_TRUE(contents1 == contents2);
  EXPECT_TRUE(EqualWithTitles(contents1, contents2));

  EXPECT_TRUE(contents1 == contents3);
  EXPECT_FALSE(EqualWithTitles(contents1, contents3));
}

TEST_F(DlpConfidentialContentsTest, CacheEvictsAfterTimeout) {
  scoped_refptr<base::TestMockTimeTaskRunner> task_runner =
      base::MakeRefCounted<base::TestMockTimeTaskRunner>();
  DlpConfidentialContentsCache cache;
  cache.SetTaskRunnerForTesting(task_runner);

  DlpConfidentialContent content = CreateConfidentialContent(title1, url1);

  cache.Cache(content, kRestriction);
  EXPECT_TRUE(cache.Contains(content, kRestriction));
  histogram_tester_.ExpectBucketCount(
      data_controls::GetDlpHistogramPrefix() +
          data_controls::dlp::kConfidentialContentsCount,
      1, 1);
  task_runner->FastForwardBy(DlpConfidentialContentsCache::GetCacheTimeout());
  EXPECT_FALSE(cache.Contains(content, kRestriction));
}

TEST_F(DlpConfidentialContentsTest, CacheEvictsWhenFull) {
  DlpConfidentialContentsCache cache;
  DlpConfidentialContent content1 = CreateConfidentialContent(title1, url1);
  cache.Cache(content1, kRestriction);
  histogram_tester_.ExpectBucketCount(
      data_controls::GetDlpHistogramPrefix() +
          data_controls::dlp::kConfidentialContentsCount,
      1, 1);
  for (int i = 2; i <= 100; i++) {
    std::stringstream url;
    url << "https://example";
    url << i;
    url << ".com";
    cache.Cache(CreateConfidentialContent(u"title", GURL(url.str())),
                kRestriction);
  }
  EXPECT_EQ(cache.GetSizeForTesting(), 100u);
  histogram_tester_.ExpectBucketCount(
      data_controls::GetDlpHistogramPrefix() +
          data_controls::dlp::kConfidentialContentsCount,
      100, 1);
  EXPECT_EQ(histogram_tester_
                .GetHistogramSamplesSinceCreation(
                    data_controls::GetDlpHistogramPrefix() +
                    data_controls::dlp::kConfidentialContentsCount)
                ->TotalCount(),
            100);

  // Add an additional item which should lead to the first one being evicted.
  DlpConfidentialContent content101 =
      CreateConfidentialContent(u"Example101", GURL("https://example101.com"));
  cache.Cache(content101, kRestriction);
  EXPECT_EQ(cache.GetSizeForTesting(), 100u);
  EXPECT_FALSE(cache.Contains(content1, kRestriction));
  EXPECT_TRUE(cache.Contains(content101, kRestriction));
  EXPECT_EQ(histogram_tester_
                .GetHistogramSamplesSinceCreation(
                    data_controls::GetDlpHistogramPrefix() +
                    data_controls::dlp::kConfidentialContentsCount)
                ->TotalCount(),
            101);
  histogram_tester_.ExpectBucketCount(
      data_controls::GetDlpHistogramPrefix() +
          data_controls::dlp::kConfidentialContentsCount,
      100, 2);
}

TEST_F(DlpConfidentialContentsTest, CacheRemovesDuplicates) {
  DlpConfidentialContentsCache cache;

  DlpConfidentialContent content = CreateConfidentialContent(title1, url1);

  cache.Cache(content, kRestriction);
  cache.Cache(content, kRestriction);
  EXPECT_EQ(cache.GetSizeForTesting(), 1u);
  histogram_tester_.ExpectBucketCount(
      data_controls::GetDlpHistogramPrefix() +
          data_controls::dlp::kConfidentialContentsCount,
      1, 1);
}

}  // namespace policy