File: chrome_profile_request_generator_unittest.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; 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 (348 lines) | stat: -rw-r--r-- 15,013 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
// Copyright 2022 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/browser/reporting/chrome_profile_request_generator.h"

#include <vector>

#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/test/test_future.h"
#include "components/device_signals/core/browser/mock_signals_aggregator.h"
#include "components/enterprise/browser/reporting/fake_browser_report_generator_delegate.h"
#include "components/enterprise/browser/reporting/report_generation_config.h"
#include "components/enterprise/browser/reporting/report_util.h"
#include "components/enterprise/browser/reporting/reporting_delegate_factory.h"
#include "components/policy/core/common/cloud/cloud_policy_util.h"
#include "components/policy/proto/device_management_backend.pb.h"
#include "components/version_info/channel.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::_;
using testing::Invoke;
using testing::StrictMock;

namespace enterprise_reporting {

namespace em = enterprise_management;

const char kFakeSignalOsName[] = "os_name_from_signals";
const char kFakeSignalOsVersion[] = "100.0.from_signals";
const char kFakeSignalDisplayName[] = "user_from_signals";
const char kFakeSignalHostname[] = "host_name_from_signals";
const char kFakeProfileId[] = "profile_id_from_signals";

const char kFakeSignalMacAddr1[] = "00-11-22-33-44-55-66";
const char kFakeSignalMacAddr2[] = "AA-BB-CC-DD-EE-FF";
const char kFakeSignalMacAddr3[] = "A0-B1-C2-D3-E4-F5";

#if BUILDFLAG(IS_WIN)
const char kFakeSignalAvName[] = "AV_name_from_signals";
const char kFakeSignalAvId[] = "AV_id_from_signals";

const char kFakeFirstHotfix[] = "hotfix_1";
const char kFakeSecondHotfix[] = "hotfix_2";
#endif  // BUILDFLAG(IS_WIN)

namespace {

const base::FilePath::CharType kProfilePath[] =
    FILE_PATH_LITERAL("profile-path");
constexpr char kBrowserExePath[] = "browser-path";

void VerifyReportContent(
    const ReportRequestQueue& requests,
    em::ChromeProfileReportRequest::ReportType expected_report_type,
    bool is_profile_id_null = false) {
  // True if a status report-exclusive field is expected to be filled correctly,
  // status reports with signals also count.
  bool expect_status_report_only_value =
      expected_report_type !=
      em::ChromeProfileReportRequest::PROFILE_SECURITY_SIGNALS;
  bool expect_signals_override_value =
      expected_report_type != em::ChromeProfileReportRequest::PROFILE_REPORT;

  ASSERT_EQ(1u, requests.size());
  ReportRequest* request = requests.front().get();
  ASSERT_TRUE(request);
  EXPECT_EQ(request->GetChromeProfileReportRequest().report_type(),
            expected_report_type);

  EXPECT_EQ(
      request->GetChromeProfileReportRequest().has_browser_device_identifier(),
      expect_signals_override_value);

  if (expect_signals_override_value) {
    auto browser_device_identifier =
        request->GetChromeProfileReportRequest().browser_device_identifier();
    EXPECT_EQ(browser_device_identifier.computer_name(),
              kFakeSignalDisplayName);
    // Test the case when a field value cannot be collected
    EXPECT_EQ(browser_device_identifier.serial_number(), std::string());
    EXPECT_EQ(browser_device_identifier.host_name(), kFakeSignalHostname);
  }

  ASSERT_TRUE(request->GetChromeProfileReportRequest().has_os_report());
  auto os_report = request->GetChromeProfileReportRequest().os_report();
  if (expect_signals_override_value) {
    EXPECT_EQ(os_report.name(), kFakeSignalOsName);
    // If a field does not get overwritten by value collected from device
    // signals, it should use the default OS report provided value.
    EXPECT_EQ(os_report.arch(), policy::GetOSArchitecture());
    EXPECT_EQ(os_report.version(), kFakeSignalOsVersion);
    EXPECT_EQ(os_report.screen_lock_secured(), em::SettingValue::ENABLED);

    EXPECT_EQ(3, os_report.mac_addresses_size());
    EXPECT_EQ(os_report.mac_addresses(0), kFakeSignalMacAddr1);
    EXPECT_EQ(os_report.mac_addresses(1), kFakeSignalMacAddr2);
    EXPECT_EQ(os_report.mac_addresses(2), kFakeSignalMacAddr3);
#if BUILDFLAG(IS_WIN)
    auto av_info = os_report.antivirus_info(0);
    EXPECT_EQ(av_info.display_name(), kFakeSignalAvName);
    EXPECT_EQ(av_info.product_id(), kFakeSignalAvId);
    EXPECT_EQ(av_info.state(), em::AntiVirusProduct::EXPIRED);

    auto first_hotfix = os_report.hotfixes(0);
    EXPECT_EQ(first_hotfix, kFakeFirstHotfix);
    auto second_hotfix = os_report.hotfixes(1);
    EXPECT_EQ(second_hotfix, kFakeSecondHotfix);
#endif  // BUILDFLAG(IS_WIN)
  } else {
    EXPECT_EQ(os_report.name(), policy::GetOSPlatform());
    EXPECT_EQ(os_report.arch(), policy::GetOSArchitecture());
    EXPECT_EQ(os_report.version(), policy::GetOSVersion());

    // Signals report only fields should not be written
    ASSERT_FALSE(os_report.has_device_enrollment_domain());
    ASSERT_FALSE(os_report.has_screen_lock_secured());

    EXPECT_EQ(0, os_report.mac_addresses_size());
#if BUILDFLAG(IS_WIN)
    EXPECT_EQ(0, os_report.antivirus_info_size());
    EXPECT_EQ(0, os_report.hotfixes_size());
#endif  // BUILDFLAG(IS_WIN)
  }

  ASSERT_TRUE(request->GetChromeProfileReportRequest().has_browser_report());
  auto browser_report =
      request->GetChromeProfileReportRequest().browser_report();

  // These fields are only filled if status report is enabled.
  EXPECT_EQ(
      ObfuscateFilePath(kBrowserExePath) == browser_report.executable_path(),
      expect_status_report_only_value);
  EXPECT_EQ(browser_report.is_extended_stable_channel(),
            expect_status_report_only_value);

  EXPECT_EQ(1, browser_report.chrome_user_profile_infos_size());
  auto chrome_user_profile_info = browser_report.chrome_user_profile_infos(0);

  // These fields are only filled if status report is enabled.
  EXPECT_EQ(ObfuscateFilePath(base::FilePath(kProfilePath).AsUTF8Unsafe()) ==
                chrome_user_profile_info.id(),
            expect_status_report_only_value);
  EXPECT_EQ(chrome_user_profile_info.is_detail_available(),
            expect_status_report_only_value);

  // `profile_signals_report` is a signals report only sub-proto.
  EXPECT_EQ(chrome_user_profile_info.has_profile_signals_report(),
            expect_signals_override_value);

  if (expect_signals_override_value) {
    auto profile_signals_report =
        chrome_user_profile_info.profile_signals_report();

    EXPECT_EQ(profile_signals_report.built_in_dns_client_enabled(), true);
    EXPECT_EQ(profile_signals_report.chrome_remote_desktop_app_blocked(),
              false);
    EXPECT_EQ(profile_signals_report.password_protection_warning_trigger(),
              em::ProfileSignalsReport::PHISHING_REUSE);
    // If a value cannot be collected, this report field will be empty.
    EXPECT_EQ(profile_signals_report.profile_enrollment_domain(),
              std::string());
    EXPECT_EQ(profile_signals_report.safe_browsing_protection_level(),
              em::ProfileSignalsReport::STANDARD_PROTECTION);
    EXPECT_EQ(profile_signals_report.site_isolation_enabled(), true);
    EXPECT_EQ(chrome_user_profile_info.profile_id(),
              is_profile_id_null ? std::string() : kFakeProfileId);
    EXPECT_EQ(profile_signals_report.realtime_url_check_mode(),
              em::ProfileSignalsReport::ENABLED_MAIN_FRAME);
  }
}

device_signals::SignalsAggregationRequest CreateExpectedRequest() {
  device_signals::SignalsAggregationRequest request;
  request.signal_names.emplace(device_signals::SignalName::kOsSignals);
  request.signal_names.emplace(
      device_signals::SignalName::kBrowserContextSignals);
#if BUILDFLAG(IS_WIN)
  request.signal_names.emplace(device_signals::SignalName::kAntiVirus);
  request.signal_names.emplace(device_signals::SignalName::kHotfixes);
#endif  // BUILDFLAG(IS_WIN)
  request.trigger = device_signals::Trigger::kSignalsReport;

  return request;
}

device_signals::SignalsAggregationResponse CreateFilledResponse(
    bool nullify_profile_id = false) {
  device_signals::SignalsAggregationResponse response;

  device_signals::OsSignalsResponse os_signals;
  os_signals.operating_system = kFakeSignalOsName;
  os_signals.os_version = kFakeSignalOsVersion;
  os_signals.display_name = kFakeSignalDisplayName;
  os_signals.hostname = kFakeSignalHostname;
  os_signals.screen_lock_secured = device_signals::SettingValue::ENABLED;
  // Test vector field
  os_signals.mac_addresses = {kFakeSignalMacAddr1, kFakeSignalMacAddr2,
                              kFakeSignalMacAddr3};

  response.os_signals_response = os_signals;

#if BUILDFLAG(IS_WIN)
  device_signals::AvProduct av_product;
  av_product.display_name = kFakeSignalAvName;
  av_product.product_id = kFakeSignalAvId;
  av_product.state = device_signals::AvProductState::kExpired;
  device_signals::AntiVirusSignalResponse av_response;
  av_response.av_products.push_back(av_product);
  response.av_signal_response = av_response;

  device_signals::HotfixSignalResponse hotfix_response;
  hotfix_response.hotfixes.push_back({kFakeFirstHotfix});
  hotfix_response.hotfixes.push_back({kFakeSecondHotfix});
  response.hotfix_signal_response = hotfix_response;
#endif  // BUILDFLAG(IS_WIN)

  device_signals::ProfileSignalsResponse profile_signals;
  profile_signals.built_in_dns_client_enabled = true;
  profile_signals.chrome_remote_desktop_app_blocked = false;
  profile_signals.password_protection_warning_trigger =
      safe_browsing::PasswordProtectionTrigger::PHISHING_REUSE;
  // Test that there is no issue if domain can't be collected.
  profile_signals.profile_enrollment_domain = std::nullopt;
  profile_signals.safe_browsing_protection_level =
      safe_browsing::SafeBrowsingState::STANDARD_PROTECTION;
  profile_signals.site_isolation_enabled = true;

  profile_signals.profile_id = kFakeProfileId;
  if (nullify_profile_id) {
    profile_signals.profile_id = std::nullopt;
  }

  profile_signals.realtime_url_check_mode = enterprise_connectors::
      EnterpriseRealTimeUrlCheckMode::REAL_TIME_CHECK_FOR_MAINFRAME_ENABLED;

  response.profile_signals_response = profile_signals;

  return response;
}

}  // namespace

class ChromeProfileRequestGeneratorTest : public ::testing::Test {
 protected:
  ChromeProfileRequestGeneratorTest()
      : generator_(base::FilePath(kProfilePath),
                   &delegate_factory_,
                   &mock_aggregator_) {}

  test::FakeReportingDelegateFactory delegate_factory_{kBrowserExePath};
  ChromeProfileRequestGenerator generator_;
  StrictMock<device_signals::MockSignalsAggregator> mock_aggregator_;
};

TEST_F(ChromeProfileRequestGeneratorTest, GenerateFullReportNoSecuritySignals) {
  EXPECT_CALL(mock_aggregator_, GetSignals(_, _)).Times(0);
  base::test::TestFuture<ReportRequestQueue> test_future;
  generator_.Generate(ReportGenerationConfig(ReportTrigger::kTriggerTimer,
                                             ReportType::kProfileReport,
                                             SecuritySignalsMode::kNoSignals,
                                             /*use_cookies=*/false),
                      test_future.GetCallback());

  VerifyReportContent(test_future.Get(),
                      em::ChromeProfileReportRequest::PROFILE_REPORT);
}

TEST_F(ChromeProfileRequestGeneratorTest,
       GenerateFullReportWithSecuritySignals) {
  EXPECT_CALL(mock_aggregator_, GetSignals(CreateExpectedRequest(), _))
      .WillOnce(
          Invoke([](const device_signals::SignalsAggregationRequest& request,
                    base::OnceCallback<void(
                        device_signals::SignalsAggregationResponse)> callback) {
            std::move(callback).Run(CreateFilledResponse());
          }));

  base::test::TestFuture<ReportRequestQueue> test_future;
  generator_.Generate(
      ReportGenerationConfig(ReportTrigger::kTriggerTimer,
                             ReportType::kProfileReport,
                             SecuritySignalsMode::kSignalsAttached,
                             /*use_cookies=*/false),
      test_future.GetCallback());

  VerifyReportContent(
      test_future.Get(),
      em::ChromeProfileReportRequest::PROFILE_REPORT_WITH_SECURITY_SIGNALS);
}

TEST_F(ChromeProfileRequestGeneratorTest, GenerateSecuritySignalsOnlyReport) {
  EXPECT_CALL(mock_aggregator_, GetSignals(CreateExpectedRequest(), _))
      .WillOnce(
          Invoke([](const device_signals::SignalsAggregationRequest& request,
                    base::OnceCallback<void(
                        device_signals::SignalsAggregationResponse)> callback) {
            std::move(callback).Run(CreateFilledResponse());
          }));
  base::test::TestFuture<ReportRequestQueue> test_future;
  generator_.Generate(ReportGenerationConfig(ReportTrigger::kTriggerNone,
                                             ReportType::kProfileReport,
                                             SecuritySignalsMode::kSignalsOnly,
                                             /*use_cookies=*/false),
                      test_future.GetCallback());
  VerifyReportContent(test_future.Get(),
                      em::ChromeProfileReportRequest::PROFILE_SECURITY_SIGNALS);
}

// Test that no issue is encountered when a nullopt value is collected, on an
// optional field
TEST_F(ChromeProfileRequestGeneratorTest, NoProfileId) {
  EXPECT_CALL(mock_aggregator_, GetSignals(CreateExpectedRequest(), _))
      .WillOnce(
          Invoke([](const device_signals::SignalsAggregationRequest& request,
                    base::OnceCallback<void(
                        device_signals::SignalsAggregationResponse)> callback) {
            std::move(callback).Run(
                CreateFilledResponse(/*nullify_profile_id=*/true));
          }));
  base::test::TestFuture<ReportRequestQueue> test_future;
  generator_.Generate(ReportGenerationConfig(ReportTrigger::kTriggerNone,
                                             ReportType::kProfileReport,
                                             SecuritySignalsMode::kSignalsOnly,
                                             /*use_cookies=*/false),
                      test_future.GetCallback());
  VerifyReportContent(test_future.Get(),
                      em::ChromeProfileReportRequest::PROFILE_SECURITY_SIGNALS,
                      /*is_profile_id_null=*/true);
}

TEST_F(ChromeProfileRequestGeneratorTest, IncorrectReportType) {
  EXPECT_CALL(mock_aggregator_, GetSignals(_, _)).Times(0);
  base::test::TestFuture<ReportRequestQueue> test_future;
  generator_.Generate(ReportGenerationConfig(), test_future.GetCallback());

  const ReportRequestQueue& requests = test_future.Get();

  // When the wrong report type is provided, generator should still return the
  // correct request, but with empty content.
  ASSERT_EQ(1u, requests.size());
  ReportRequest* request = requests.front().get();
  ASSERT_TRUE(request);
  ASSERT_FALSE(request->GetDeviceReportRequest().has_browser_report());
}

}  // namespace enterprise_reporting