File: k12_age_classification_metrics_provider_browsertest.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 (173 lines) | stat: -rw-r--r-- 6,537 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
// Copyright 2025 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/metrics/k12_age_classification_metrics_provider.h"

#include <optional>

#include "ash/constants/ash_switches.h"
#include "base/test/metrics/histogram_tester.h"
#include "chrome/browser/ash/login/existing_user_controller.h"
#include "chrome/browser/ash/login/test/logged_in_user_mixin.h"
#include "chrome/browser/ash/login/test/session_manager_state_waiter.h"
#include "chrome/browser/ash/login/wizard_controller.h"
#include "chrome/browser/ash/ownership/fake_owner_settings_service.h"
#include "chrome/browser/ash/policy/core/device_local_account.h"
#include "chrome/browser/ash/policy/core/device_policy_cros_browser_test.h"
#include "chrome/browser/ash/policy/test_support/embedded_policy_test_server_mixin.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_process_platform_part_ash.h"
#include "chrome/test/base/fake_gaia_mixin.h"
#include "chromeos/ash/components/dbus/session_manager/fake_session_manager_client.h"
#include "chromeos/ash/components/policy/device_local_account/device_local_account_type.h"
#include "components/metrics/metrics_service.h"
#include "components/policy/core/common/cloud/cloud_policy_constants.h"
#include "components/policy/core/common/cloud/mock_cloud_policy_store.h"
#include "components/policy/core/common/cloud/test/policy_builder.h"
#include "components/policy/proto/device_management_backend.pb.h"
#include "content/public/test/browser_test.h"

namespace {

namespace em = enterprise_management;
using K12AgeClassificationSegment =
    K12AgeClassificationMetricsProvider::K12AgeClassificationSegment;
using testing::InvokeWithoutArgs;

constexpr char kAccountId1[] = "dla1@example.com";

std::optional<em::PolicyData::K12AgeClassificationMetricsLogSegment>
GetK12AgeClassificationMetricsLogSegment(K12AgeClassificationSegment segment) {
  switch (segment) {
    case K12AgeClassificationSegment::kAgeUnder18:
      return em::PolicyData::AGE_UNDER18;
    case K12AgeClassificationSegment::kAgeEqualOrOver18:
      return em::PolicyData::AGE_EQUAL_OR_OVER18;
    case K12AgeClassificationSegment::kAgeUnspecified:
      [[fallthrough]];
    default:
      return em::PolicyData::AGE_UNSPECIFIED;
  }
}

void ProvideHistograms() {
  // The purpose of the below call is to avoid a DCHECK failure in an unrelated
  // metrics provider, in |FieldTrialsProvider::ProvideCurrentSessionData()|.
  metrics::SystemProfileProto system_profile_proto;
  // Downstream functions do not use system_profile_proto so there is no risk of
  // UAF.
  g_browser_process->metrics_service()
      ->GetDelegatingProviderForTesting()
      ->ProvideSystemProfileMetricsWithLogCreationTime(base::TimeTicks::Now(),
                                                       &system_profile_proto);
  g_browser_process->metrics_service()
      ->GetDelegatingProviderForTesting()
      ->OnDidCreateMetricsLog();
}

class TestCase {
 public:
  explicit TestCase(K12AgeClassificationSegment segment) : segment_(segment) {}

  std::string GetTestName() const {
    switch (segment_) {
      case K12AgeClassificationSegment::kAgeUnder18:
        return "AgeUnder18";
      case K12AgeClassificationSegment::kAgeEqualOrOver18:
        return "AgeEqualOrOver18";
      case K12AgeClassificationSegment::kAgeUnspecified:
        return "AgeUnspecified";
    }
  }

  K12AgeClassificationSegment GetSegment() const { return segment_; }

  std::optional<em::PolicyData::K12AgeClassificationMetricsLogSegment>
  GetK12AgeClassificationMetricsLogSegment() const {
    return ::GetK12AgeClassificationMetricsLogSegment(segment_);
  }

 private:
  const K12AgeClassificationSegment segment_;
};

class K12AgeClassificationMetricsProviderTest
    : public policy::DevicePolicyCrosBrowserTest,
      public testing::WithParamInterface<TestCase> {
 protected:
  void SetUpInProcessBrowserTestFixture() override {
    policy::DevicePolicyCrosBrowserTest::SetUpInProcessBrowserTestFixture();
    InitializePolicy();
  }

  void SetUpCommandLine(base::CommandLine* command_line) override {
    command_line->AppendSwitch(ash::switches::kOobeSkipPostLogin);
    DevicePolicyCrosBrowserTest::SetUpCommandLine(command_line);
  }

  void InitializePolicy() {
    device_policy()->policy_data().set_public_key_version(1);
  }

  void SetDevicePolicy() {
    device_local_account_policy_.SetDefaultSigningKey();
    device_local_account_policy_.Build();
    logged_in_user_mixin_.GetEmbeddedPolicyTestServerMixin()
        ->UpdateExternalPolicy(
            policy::dm_protocol::kChromePublicAccountPolicyType, kAccountId1,
            device_local_account_policy_.payload().SerializeAsString());
    session_manager_client()->set_device_local_account_policy(
        kAccountId1, device_local_account_policy_.GetBlob());
  }

  void LogInUser() {
    std::optional<em::PolicyData::K12AgeClassificationMetricsLogSegment>
        log_segment = GetParam().GetK12AgeClassificationMetricsLogSegment();
    if (log_segment) {
      logged_in_user_mixin_.GetEmbeddedPolicyTestServerMixin()
          ->SetK12AgeClassificationMetricsLogSegment(log_segment.value());
    }
    logged_in_user_mixin_.LogInUser();
  }

  int GetExpectedUmaValue() {
    return static_cast<int>(GetParam().GetSegment());
  }

 private:
  ash::LoggedInUserMixin logged_in_user_mixin_{
      &mixin_host_, /*test_base=*/this, embedded_test_server(),
      ash::LoggedInUserMixin::LogInType::kManaged};
  policy::UserPolicyBuilder device_local_account_policy_;
};

IN_PROC_BROWSER_TEST_P(K12AgeClassificationMetricsProviderTest, Uma) {
  base::HistogramTester histogram_tester;

  SetDevicePolicy();

  // Simulate calling ProvideHistograms() prior to logging in.
  ProvideHistograms();

  // No metrics were recorded.
  histogram_tester.ExpectTotalCount(
      K12AgeClassificationMetricsProvider::kHistogramName, 0);

  LogInUser();

  // Simulate calling ProvideHistograms() after logging in.
  ProvideHistograms();

  histogram_tester.ExpectUniqueSample(
      K12AgeClassificationMetricsProvider::kHistogramName,
      GetExpectedUmaValue(), 1);
}

INSTANTIATE_TEST_SUITE_P(
    All,
    K12AgeClassificationMetricsProviderTest,
    testing::Values(TestCase(K12AgeClassificationSegment::kAgeUnder18),
                    TestCase(K12AgeClassificationSegment::kAgeEqualOrOver18),
                    TestCase(K12AgeClassificationSegment::kAgeUnspecified)));
}  // namespace