File: client_report_util_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 (126 lines) | stat: -rw-r--r-- 5,554 bytes parent folder | download | duplicates (9)
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
// 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/safe_browsing/content/browser/client_report_util.h"
#include "components/safe_browsing/core/common/proto/csd.pb.h"
#include "testing/gtest/include/gtest/gtest.h"

const char kPhishingUrl[] = "https://phishing.com/";

namespace safe_browsing {

class ClientReportUtilTest : public testing::Test {
 protected:
  ClientReportUtilTest() = default;

  security_interstitials::UnsafeResource GetPhishingResource() {
    security_interstitials::UnsafeResource resource =
        security_interstitials::UnsafeResource();
    resource.url = GURL(kPhishingUrl);
    resource.threat_type = SBThreatType::SB_THREAT_TYPE_URL_PHISHING;
    return resource;
  }

  std::unique_ptr<security_interstitials::InterstitialInteractionMap>
  SetUpInterstitialInteractionMap(const int kExpectedShowMoreEvents,
                                  const int kExpectedOpenHelpCenter,
                                  const int kExpectedProceed) {
    auto interactions_map =
        std::make_unique<security_interstitials::InterstitialInteractionMap>();
    interactions_map->insert_or_assign(
        security_interstitials::SecurityInterstitialCommand::
            CMD_SHOW_MORE_SECTION,
        security_interstitials::InterstitialInteractionDetails(
            kExpectedShowMoreEvents,
            base::Time::Now().InMillisecondsSinceUnixEpoch() - 2000,
            base::Time::Now().InMillisecondsSinceUnixEpoch()));
    interactions_map->insert_or_assign(
        security_interstitials::SecurityInterstitialCommand::
            CMD_OPEN_HELP_CENTER,
        security_interstitials::InterstitialInteractionDetails(
            kExpectedOpenHelpCenter,
            base::Time::Now().InMillisecondsSinceUnixEpoch() - 1000,
            base::Time::Now().InMillisecondsSinceUnixEpoch()));
    int64_t time_now = base::Time::Now().InMillisecondsSinceUnixEpoch();
    interactions_map->insert_or_assign(
        security_interstitials::SecurityInterstitialCommand::CMD_PROCEED,
        security_interstitials::InterstitialInteractionDetails(
            kExpectedProceed, time_now, time_now));
    return interactions_map;
  }

  void VerifyInterstitialInteraction(
      const safe_browsing::ClientSafeBrowsingReportRequest& report,
      ClientSafeBrowsingReportRequest::InterstitialInteraction::
          SecurityInterstitialInteraction expected_interaction,
      int expected_count) {
    for (auto interaction : report.interstitial_interactions()) {
      if (interaction.security_interstitial_interaction() ==
          expected_interaction) {
        EXPECT_EQ(interaction.occurrence_count(), expected_count);
        EXPECT_EQ(interaction.first_interaction_timestamp_msec() ==
                      interaction.last_interaction_timestamp_msec(),
                  expected_count == 1);
      }
    }
  }

  void VerifyInterstitialInteractionsReport(
      const safe_browsing::ClientSafeBrowsingReportRequest& report,
      const int kExpectedShowMoreEvents,
      const int kExpectedOpenHelpCenter,
      const int kExpectedProceed) {
    EXPECT_EQ(report.interstitial_interactions_size(), 3);
    VerifyInterstitialInteraction(
        report,
        ClientSafeBrowsingReportRequest::InterstitialInteraction::
            CMD_SHOW_MORE_SECTION,
        kExpectedShowMoreEvents);
    VerifyInterstitialInteraction(
        report,
        ClientSafeBrowsingReportRequest::InterstitialInteraction::
            CMD_OPEN_HELP_CENTER,
        kExpectedOpenHelpCenter);
    VerifyInterstitialInteraction(
        report,
        ClientSafeBrowsingReportRequest::InterstitialInteraction::CMD_PROCEED,
        kExpectedProceed);
  }
};

TEST_F(ClientReportUtilTest, FillBasicReport) {
  auto report = std::make_unique<ClientSafeBrowsingReportRequest>();
  client_report_utils::FillReportBasicResourceDetails(report.get(),
                                                      GetPhishingResource());
  ASSERT_EQ(report->url(), kPhishingUrl);
  ASSERT_EQ(report->type(),
            safe_browsing::ClientSafeBrowsingReportRequest::URL_PHISHING);
  ASSERT_EQ(report->url_request_destination(),
            ClientSafeBrowsingReportRequest::DOCUMENT);
  ASSERT_TRUE(report->has_client_properties());
  ASSERT_TRUE(report->client_properties().has_url_api_type());
  ASSERT_TRUE(report->client_properties().has_is_async_check());
  ASSERT_EQ(
      report->client_properties().url_api_type(),
      ClientSafeBrowsingReportRequest::SAFE_BROWSING_URL_API_TYPE_UNSPECIFIED);
  ASSERT_FALSE(report->client_properties().is_async_check());
}

TEST_F(ClientReportUtilTest, FillInterstitialInteractionsOfReport) {
  const int kExpectedShowMoreEvents = 3;
  const int kExpectedOpenHelpCenter = 2;
  const int kExpectedProceed = 1;
  auto report = std::make_unique<ClientSafeBrowsingReportRequest>();
  client_report_utils::FillReportBasicResourceDetails(report.get(),
                                                      GetPhishingResource());
  auto interactions_map = SetUpInterstitialInteractionMap(
      kExpectedShowMoreEvents, kExpectedOpenHelpCenter, kExpectedProceed);
  client_report_utils::FillInterstitialInteractionsHelper(
      report.get(), interactions_map.get());
  VerifyInterstitialInteractionsReport(*report.get(), kExpectedShowMoreEvents,
                                       kExpectedOpenHelpCenter,
                                       kExpectedProceed);
}

}  // namespace safe_browsing