File: paste_allowed_request_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 (620 lines) | stat: -rw-r--r-- 23,651 bytes parent folder | download | duplicates (3)
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
// Copyright 2024 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/enterprise/data_protection/paste_allowed_request.h"

#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "base/test/bind.h"
#include "base/test/test_future.h"
#include "chrome/browser/enterprise/connectors/analysis/clipboard_request_handler.h"
#include "chrome/browser/enterprise/connectors/analysis/content_analysis_delegate.h"
#include "chrome/browser/enterprise/connectors/analysis/content_analysis_delegate_base.h"
#include "chrome/browser/enterprise/connectors/test/deep_scanning_test_utils.h"
#include "chrome/browser/enterprise/connectors/test/fake_clipboard_request_handler.h"
#include "chrome/browser/enterprise/connectors/test/fake_content_analysis_delegate.h"
#include "chrome/browser/enterprise/data_protection/data_protection_clipboard_utils.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/enterprise/data_controls/core/browser/test_utils.h"
#include "content/public/browser/clipboard_types.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/navigation_simulator.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/clipboard/clipboard_monitor.h"
#include "ui/base/clipboard/scoped_clipboard_writer.h"
#include "ui/base/clipboard/test/test_clipboard.h"

namespace enterprise_data_protection {

namespace {

constexpr char kScanId[] = "scan_id";

enterprise_connectors::ContentAnalysisResponse::Result CreateResult(
    enterprise_connectors::ContentAnalysisResponse::Result::TriggeredRule::
        Action action) {
  enterprise_connectors::ContentAnalysisResponse::Result result;
  result.set_tag("dlp");
  result.set_status(
      enterprise_connectors::ContentAnalysisResponse::Result::SUCCESS);

  if (action != enterprise_connectors::ContentAnalysisResponse::Result::
                    TriggeredRule::ACTION_UNSPECIFIED) {
    auto* rule = result.add_triggered_rules();
    rule->set_rule_name("paste_rule_name");
    rule->set_action(action);
  }
  return result;
}

enterprise_connectors::ContentAnalysisResponse CreateResponse(
    enterprise_connectors::ContentAnalysisResponse::Result::TriggeredRule::
        Action action) {
  enterprise_connectors::ContentAnalysisResponse response;
  response.set_request_token(kScanId);

  auto* result = response.add_results();
  *result = CreateResult(action);
  return response;
}

class TestClipboardRequestHandler
    : public enterprise_connectors::test::FakeClipboardRequestHandler {
 public:
  static std::unique_ptr<ClipboardRequestHandler> Create(
      enterprise_connectors::ContentAnalysisInfo* content_analysis_info,
      safe_browsing::BinaryUploadService* upload_service,
      Profile* profile,
      GURL url,
      Type type,
      safe_browsing::DeepScanAccessPoint access_point,
      enterprise_connectors::ContentMetaData::CopiedTextSource clipboard_source,
      std::string source_content_area_email,
      std::string content_transfer_method,
      std::string data,
      CompletionCallback callback) {
    return base::WrapUnique(new TestClipboardRequestHandler(
        content_analysis_info, upload_service, profile, std::move(url), type,
        access_point, std::move(clipboard_source),
        std::move(source_content_area_email),
        std::move(content_transfer_method), std::move(data),
        std::move(callback)));
  }

 protected:
  using FakeClipboardRequestHandler::FakeClipboardRequestHandler;

 private:
  void UploadForDeepScanning(
      std::unique_ptr<enterprise_connectors::ClipboardAnalysisRequest> request)
      override {
    base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE,
        base::BindOnce(
            &TestClipboardRequestHandler::OnContentAnalysisResponse,
            base::Unretained(this),
            safe_browsing::BinaryUploadService::Result::SUCCESS,
            CreateResponse(enterprise_connectors::ContentAnalysisResponse::
                               Result::TriggeredRule::BLOCK)));
  }
};

class PasteAllowedRequestTest : public testing::Test {
 public:
  PasteAllowedRequestTest()
      : profile_manager_(TestingBrowserProcess::GetGlobal()) {
    EXPECT_TRUE(profile_manager_.SetUp());
    profile_ = profile_manager_.CreateTestingProfile("test-user-1");
  }

  void SetUp() override {
    PasteAllowedRequest::CleanupRequestsForTesting();
    ui::TestClipboard::CreateForCurrentThread();
  }

  content::WebContents* main_web_contents() {
    if (!main_web_contents_) {
      content::WebContents::CreateParams params(profile_);
      main_web_contents_ = content::WebContents::Create(params);
    }
    return main_web_contents_.get();
  }

  content::RenderFrameHost& main_rfh() {
    return *main_web_contents()->GetPrimaryMainFrame();
  }

  content::ClipboardEndpoint main_endpoint() {
    return content::ClipboardEndpoint(
        ui::DataTransferEndpoint(GURL("https://google.com")),
        base::BindLambdaForTesting([this]() -> content::BrowserContext* {
          return static_cast<content::BrowserContext*>(profile_);
        }),
        main_rfh());
  }

  content::WebContents* secondary_web_contents() {
    if (!secondary_web_contents_) {
      content::WebContents::CreateParams params(profile_);
      secondary_web_contents_ = content::WebContents::Create(params);
    }
    return secondary_web_contents_.get();
  }

  content::RenderFrameHost& secondary_rfh() {
    return *secondary_web_contents()->GetPrimaryMainFrame();
  }

  content::ClipboardEndpoint secondary_endpoint() {
    return content::ClipboardEndpoint(
        ui::DataTransferEndpoint(GURL("https://google.com")),
        base::BindLambdaForTesting([this]() -> content::BrowserContext* {
          return static_cast<content::BrowserContext*>(profile_);
        }),
        secondary_rfh());
  }

 protected:
  content::BrowserTaskEnvironment task_environment_{
      base::test::TaskEnvironment::TimeSource::MOCK_TIME};
  TestingProfileManager profile_manager_;
  raw_ptr<TestingProfile> profile_;
  std::unique_ptr<content::WebContents> main_web_contents_;
  std::unique_ptr<content::WebContents> secondary_web_contents_;
};

class PasteAllowedRequestScanningTest : public PasteAllowedRequestTest {
 public:
  PasteAllowedRequestScanningTest() {
    enterprise_connectors::ContentAnalysisDelegate::DisableUIForTesting();
  }

  void SetUp() override {
    PasteAllowedRequestTest::SetUp();

    helper_ = std::make_unique<
        enterprise_connectors::test::EventReportValidatorHelper>(profile_);

    enterprise_connectors::test::SetAnalysisConnector(
        profile_->GetPrefs(), enterprise_connectors::BULK_DATA_ENTRY,
        R"({
          "service_provider": "google",
          "block_until_verdict": 1,
          "minimum_data_size": 1,
          "enable": [
            {
              "url_list": ["*"],
              "tags": ["dlp"]
            }
          ]
        })");
  }

 protected:
  std::unique_ptr<enterprise_connectors::test::EventReportValidatorHelper>
      helper_;
};

}  // namespace

TEST_F(PasteAllowedRequestTest, AddCallbacksAndComplete) {
  PasteAllowedRequest request;
  content::ClipboardPasteData clipboard_paste_data_1;
  clipboard_paste_data_1.text = u"text";
  clipboard_paste_data_1.png = {1, 2, 3, 4, 5};
  content::ClipboardPasteData clipboard_paste_data_2;
  clipboard_paste_data_2.text = u"other text";
  clipboard_paste_data_2.png = {6, 7, 8, 9, 10};

  int count = 0;

  // Add a callback.  It should not fire right away.
  request.AddCallback(base::BindLambdaForTesting(
      [&count, clipboard_paste_data_1](
          std::optional<content::ClipboardPasteData> clipboard_paste_data) {
        ++count;
        ASSERT_EQ(clipboard_paste_data->text, clipboard_paste_data_1.text);
        ASSERT_EQ(clipboard_paste_data->png, clipboard_paste_data_1.png);
      }));
  EXPECT_EQ(0, count);

  // Complete the request.  Callback should fire.  Whether paste is allowed
  // or not is not important.
  request.Complete(clipboard_paste_data_1);
  EXPECT_EQ(1, count);

  // Add a second callback.  It should not fire right away.
  request.AddCallback(base::BindLambdaForTesting(
      [&count, clipboard_paste_data_2](
          std::optional<content::ClipboardPasteData> clipboard_paste_data) {
        ++count;
        ASSERT_EQ(clipboard_paste_data->text, clipboard_paste_data_2.text);
        ASSERT_EQ(clipboard_paste_data->png, clipboard_paste_data_2.png);
      }));
  EXPECT_EQ(1, count);

  // Calling `Complete()` again will call the second callback.
  request.Complete(clipboard_paste_data_2);
  EXPECT_EQ(2, count);
}

TEST_F(PasteAllowedRequestTest, IsObsolete) {
  PasteAllowedRequest request;
  content::ClipboardPasteData clipboard_paste_data;
  clipboard_paste_data.text = u"data";

  // A request is obsolete once it is too old and completed.
  // Whether paste is allowed or not is not important.
  request.Complete(clipboard_paste_data);
  EXPECT_TRUE(
      request.IsObsolete(request.completed_time() +
                         PasteAllowedRequest::kIsPasteAllowedRequestTooOld +
                         base::Microseconds(1)));
}

TEST_F(PasteAllowedRequestTest, SameDestinationSource) {
  auto seqno = ui::Clipboard::GetForCurrentThread()->GetSequenceNumber(
      ui::ClipboardBuffer::kCopyPaste);

  const std::u16string kText = u"text";
  content::ClipboardPasteData clipboard_paste_data;
  clipboard_paste_data.text = kText;

  base::test::TestFuture<std::optional<content::ClipboardPasteData>> future;
  PasteAllowedRequest::StartPasteAllowedRequest(
      /*source*/ main_endpoint(), /*destination*/ main_endpoint(),
      {.seqno = seqno}, clipboard_paste_data, future.GetCallback());

  ASSERT_TRUE(future.Get());
  ASSERT_EQ(future.Get()->text, kText);

  EXPECT_EQ(1u, PasteAllowedRequest::requests_count_for_testing());
}

TEST_F(PasteAllowedRequestTest, SameDestinationSource_AfterReplacement) {
  data_controls::SetDataControls(profile_->GetPrefs(), {
                                                           R"({
                    "sources": {
                      "urls": ["google.com"]
                    },
                    "destinations": {
                      "os_clipboard": true
                    },
                    "restrictions": [
                      {"class": "CLIPBOARD", "level": "BLOCK"}
                    ]
                  })"});

  const std::u16string kText = u"text";
  content::ClipboardPasteData copied_data;
  copied_data.text = kText;
  base::test::TestFuture<const ui::ClipboardFormatType&,
                         const content::ClipboardPasteData&,
                         std::optional<std::u16string>>
      copy_future;

  IsClipboardCopyAllowedByPolicy(main_endpoint(), {}, copied_data,
                                 copy_future.GetCallback());
  auto replacement = copy_future.Get<std::optional<std::u16string>>();
  EXPECT_TRUE(replacement);
  EXPECT_EQ(*replacement,
            u"Pasting this content here is blocked by your administrator.");

  // This triggers the clipboard observer started by the
  // `IsClipboardCopyAllowedByPolicy` calls so that they're aware of the new
  // seqno.
  ui::ClipboardMonitor::GetInstance()->NotifyClipboardDataChanged();

  auto seqno = ui::Clipboard::GetForCurrentThread()->GetSequenceNumber(
      ui::ClipboardBuffer::kCopyPaste);

  // After the data was replaced when initially copied, it should be put back
  // when pasting in the same tab.
  content::ClipboardPasteData pasted_data;
  pasted_data.html = u"to be replaced";
  base::test::TestFuture<std::optional<content::ClipboardPasteData>>
      paste_future;
  PasteAllowedRequest::StartPasteAllowedRequest(
      /*source*/ main_endpoint(), /*destination*/ main_endpoint(),
      {.seqno = seqno}, pasted_data, paste_future.GetCallback());

  ASSERT_TRUE(paste_future.Get());
  ASSERT_EQ(paste_future.Get()->text, kText);
  ASSERT_TRUE(paste_future.Get()->html.empty());

  EXPECT_EQ(1u, PasteAllowedRequest::requests_count_for_testing());
}

TEST_F(PasteAllowedRequestTest, DifferentDestinationSource) {
  auto seqno = ui::Clipboard::GetForCurrentThread()->GetSequenceNumber(
      ui::ClipboardBuffer::kCopyPaste);

  const std::u16string kText = u"text";
  content::ClipboardPasteData clipboard_paste_data;
  clipboard_paste_data.text = kText;

  base::test::TestFuture<std::optional<content::ClipboardPasteData>> future;
  PasteAllowedRequest::StartPasteAllowedRequest(
      /*source*/ secondary_endpoint(), /*destination*/ main_endpoint(),
      {.seqno = seqno}, clipboard_paste_data, future.GetCallback());

  ASSERT_TRUE(future.Get());
  ASSERT_EQ(future.Get()->text, kText);

  EXPECT_EQ(1u, PasteAllowedRequest::requests_count_for_testing());
}

TEST_F(PasteAllowedRequestTest,
       DifferentDestinationSource_AllowedWithCachedRequest) {
  const std::u16string kText = u"text";
  auto seqno = ui::Clipboard::GetForCurrentThread()->GetSequenceNumber(
      ui::ClipboardBuffer::kCopyPaste);

  content::ClipboardPasteData clipboard_paste_data;
  clipboard_paste_data.text = kText;
  PasteAllowedRequest request;
  request.Complete(clipboard_paste_data);
  PasteAllowedRequest::AddRequestToCacheForTesting(main_rfh().GetGlobalId(),
                                                   seqno, std::move(request));
  EXPECT_EQ(1u, PasteAllowedRequest::requests_count_for_testing());

  // Attempting to start a new request when there is an allowed cached request
  // with the same seqno should allow again, but not by making a new request.
  base::test::TestFuture<std::optional<content::ClipboardPasteData>> future;
  PasteAllowedRequest::StartPasteAllowedRequest(
      /*source*/ secondary_endpoint(), /*destination*/ main_endpoint(),
      {.seqno = seqno}, clipboard_paste_data, future.GetCallback());

  ASSERT_TRUE(future.Wait());
  ASSERT_TRUE(future.Get());
  ASSERT_EQ(future.Get()->text, kText);

  EXPECT_EQ(1u, PasteAllowedRequest::requests_count_for_testing());
}

TEST_F(PasteAllowedRequestTest,
       DifferentDestinationSource_BlockedWithCachedRequest) {
  auto seqno = ui::Clipboard::GetForCurrentThread()->GetSequenceNumber(
      ui::ClipboardBuffer::kCopyPaste);

  PasteAllowedRequest request;
  request.Complete(std::nullopt);
  PasteAllowedRequest::AddRequestToCacheForTesting(main_rfh().GetGlobalId(),
                                                   seqno, std::move(request));
  EXPECT_EQ(1u, PasteAllowedRequest::requests_count_for_testing());

  const std::u16string kText = u"text";
  content::ClipboardPasteData clipboard_paste_data;
  clipboard_paste_data.text = kText;

  // Attempting to start a new request when there is a blocked cached request
  // with the same seqno should block again, but not by making a new request.
  base::test::TestFuture<std::optional<content::ClipboardPasteData>> future;
  PasteAllowedRequest::StartPasteAllowedRequest(
      /*source*/ secondary_endpoint(), /*destination*/ main_endpoint(),
      {.seqno = seqno}, clipboard_paste_data, future.GetCallback());

  ASSERT_TRUE(future.Wait());
  ASSERT_FALSE(future.Get());

  EXPECT_EQ(1u, PasteAllowedRequest::requests_count_for_testing());
}

TEST_F(PasteAllowedRequestTest, UnknownSource) {
  auto seqno = ui::Clipboard::GetForCurrentThread()->GetSequenceNumber(
      ui::ClipboardBuffer::kCopyPaste);

  const std::u16string kText = u"text";
  content::ClipboardPasteData clipboard_paste_data;
  clipboard_paste_data.text = kText;

  base::test::TestFuture<std::optional<content::ClipboardPasteData>> future;
  PasteAllowedRequest::StartPasteAllowedRequest(
      /*source*/ content::ClipboardEndpoint(std::nullopt),
      /*destination*/ main_endpoint(), {.seqno = seqno}, clipboard_paste_data,
      future.GetCallback());

  ASSERT_TRUE(future.Get());
  ASSERT_EQ(future.Get()->text, kText);

  // When a document reads from the clipboard, but the clipboard was written
  // from an unknown source, content checks should not be skipped.
  EXPECT_EQ(1u, PasteAllowedRequest::requests_count_for_testing());
}

TEST_F(PasteAllowedRequestTest, EmptyData) {
  base::test::TestFuture<std::optional<content::ClipboardPasteData>> future;
  PasteAllowedRequest::StartPasteAllowedRequest(
      /*source*/ secondary_endpoint(),
      /*destination*/ main_endpoint(),
      {
          .seqno = ui::Clipboard::GetForCurrentThread()->GetSequenceNumber(
              ui::ClipboardBuffer::kCopyPaste),
      },
      content::ClipboardPasteData(), future.GetCallback());

  ASSERT_TRUE(future.Get());
  ASSERT_TRUE(future.Get()->empty());

  // When data is empty, a request is still made in case the data needs to be
  // replaced later.
  EXPECT_EQ(1u, PasteAllowedRequest::requests_count_for_testing());
}

TEST_F(PasteAllowedRequestTest, EmptyData_SameSourceReplaced) {
  data_controls::SetDataControls(profile_->GetPrefs(), {
                                                           R"({
                    "destinations": {
                      "os_clipboard": true
                    },
                    "restrictions": [
                      {"class": "CLIPBOARD", "level": "BLOCK"}
                    ]
                  })"});

  const std::u16string kText = u"text";
  content::ClipboardPasteData copied_data;
  copied_data.text = kText;
  base::test::TestFuture<const ui::ClipboardFormatType&,
                         const content::ClipboardPasteData&,
                         std::optional<std::u16string>>
      copy_future;

  IsClipboardCopyAllowedByPolicy(main_endpoint(), {}, copied_data,
                                 copy_future.GetCallback());
  auto replacement = copy_future.Get<std::optional<std::u16string>>();
  EXPECT_TRUE(replacement);
  EXPECT_EQ(*replacement,
            u"Pasting this content here is blocked by your administrator.");

  // This triggers the clipboard observer started by the
  // `IsClipboardCopyAllowedByPolicy` calls so that they're aware of the new
  // seqno.
  ui::ClipboardMonitor::GetInstance()->NotifyClipboardDataChanged();

  base::test::TestFuture<std::optional<content::ClipboardPasteData>> future;
  PasteAllowedRequest::StartPasteAllowedRequest(
      /*source*/ secondary_endpoint(),
      /*destination*/ main_endpoint(),
      {
          .seqno = ui::Clipboard::GetForCurrentThread()->GetSequenceNumber(
              ui::ClipboardBuffer::kCopyPaste),
      },
      content::ClipboardPasteData(), future.GetCallback());

  ASSERT_TRUE(future.Get());
  ASSERT_EQ(future.Get()->text, kText);

  EXPECT_EQ(1u, PasteAllowedRequest::requests_count_for_testing());
}

TEST_F(PasteAllowedRequestTest, EmptyData_DifferentSourceReplaced) {
  data_controls::SetDataControls(profile_->GetPrefs(), {
                                                           R"({
                    "destinations": {
                      "os_clipboard": true
                    },
                    "restrictions": [
                      {"class": "CLIPBOARD", "level": "BLOCK"}
                    ]
                  })"});

  const std::u16string kText = u"text";
  content::ClipboardPasteData copied_data;
  copied_data.text = kText;
  base::test::TestFuture<const ui::ClipboardFormatType&,
                         const content::ClipboardPasteData&,
                         std::optional<std::u16string>>
      copy_future;

  IsClipboardCopyAllowedByPolicy(main_endpoint(), {}, copied_data,
                                 copy_future.GetCallback());
  auto replacement = copy_future.Get<std::optional<std::u16string>>();
  EXPECT_TRUE(replacement);
  EXPECT_EQ(*replacement,
            u"Pasting this content here is blocked by your administrator.");

  // This triggers the clipboard observer started by the
  // `IsClipboardCopyAllowedByPolicy` calls so that they're aware of the new
  // seqno.
  ui::ClipboardMonitor::GetInstance()->NotifyClipboardDataChanged();

  base::test::TestFuture<std::optional<content::ClipboardPasteData>> future;
  PasteAllowedRequest::StartPasteAllowedRequest(
      /*source*/ main_endpoint(),
      /*destination*/ secondary_endpoint(),
      {
          .seqno = ui::Clipboard::GetForCurrentThread()->GetSequenceNumber(
              ui::ClipboardBuffer::kCopyPaste),
      },
      content::ClipboardPasteData(), future.GetCallback());

  ASSERT_TRUE(future.Get());
  ASSERT_EQ(future.Get()->text, kText);

  EXPECT_EQ(1u, PasteAllowedRequest::requests_count_for_testing());
}

TEST_F(PasteAllowedRequestTest, CleanupObsoleteScanRequests) {
  auto seqno = ui::Clipboard::GetForCurrentThread()->GetSequenceNumber(
      ui::ClipboardBuffer::kCopyPaste);

  const std::u16string kText = u"text";
  content::ClipboardPasteData clipboard_paste_data;
  clipboard_paste_data.text = kText;

  base::test::TestFuture<std::optional<content::ClipboardPasteData>> future;
  PasteAllowedRequest::StartPasteAllowedRequest(
      /*source*/ secondary_endpoint(), /*destination*/ main_endpoint(),
      {.seqno = seqno}, clipboard_paste_data, future.GetCallback());

  ASSERT_TRUE(future.Get());
  ASSERT_EQ(future.Get()->text, kText);
  EXPECT_EQ(1u, PasteAllowedRequest::requests_count_for_testing());

  // Make sure an appropriate amount of time passes to make the request old.
  // It should be cleaned up.
  task_environment_.FastForwardBy(
      PasteAllowedRequest::kIsPasteAllowedRequestTooOld +
      base::Microseconds(1));
  PasteAllowedRequest::CleanupObsoleteRequests();
  EXPECT_EQ(0u, PasteAllowedRequest::requests_count_for_testing());
}

TEST_F(PasteAllowedRequestScanningTest, DifferentDestinationSource) {
  enterprise_connectors::ClipboardRequestHandler::SetFactoryForTesting(
      base::BindRepeating(TestClipboardRequestHandler::Create));

  auto validator = helper_->CreateValidator();
  validator.ExpectSensitiveDataEvent(
      /*url*/
      "",
      /*tab_url*/ "",
      /*source*/ "https://google.com/",
      /*destination*/ "",
      /*filename*/ "Text data",
      /*sha*/ "",
      /*trigger*/ "WEB_CONTENT_UPLOAD",
      /*dlp_verdict*/
      CreateResult(enterprise_connectors::ContentAnalysisResponse::Result::
                       TriggeredRule::BLOCK),
      /*mimetype*/
      []() {
        static std::set<std::string> set = {"text/plain"};
        return &set;
      }(),
      /*size*/ 4,
      /*result*/
      enterprise_connectors::EventResultToString(
          enterprise_connectors::EventResult::BLOCKED),
      /*username*/ "test-user@chromium.org",
      /*profile_identifier*/ profile_->GetPath().AsUTF8Unsafe(),
      /*scan_id*/ kScanId,
      /*content_transfer_method*/ std::nullopt,
      /*user_justification*/ std::nullopt);

  auto seqno = ui::Clipboard::GetForCurrentThread()->GetSequenceNumber(
      ui::ClipboardBuffer::kCopyPaste);

  const std::u16string kText = u"text";
  content::ClipboardPasteData clipboard_paste_data;
  clipboard_paste_data.text = kText;

  base::test::TestFuture<std::optional<content::ClipboardPasteData>> future;
  PasteAllowedRequest::StartPasteAllowedRequest(
      /*source*/ secondary_endpoint(), /*destination*/ main_endpoint(),
      {.seqno = seqno}, clipboard_paste_data, future.GetCallback());

  ASSERT_TRUE(future.Wait());
  ASSERT_FALSE(future.Get().has_value());

  EXPECT_EQ(1u, PasteAllowedRequest::requests_count_for_testing());
}

}  // namespace enterprise_data_protection