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
|
// 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 "base/functional/bind.h"
#include "chrome/browser/apps/app_service/metrics/website_metrics_browser_test_mixin.h"
#include "chrome/browser/ash/login/test/cryptohome_mixin.h"
#include "chrome/browser/ash/login/test/user_auth_config.h"
#include "chrome/browser/ash/policy/affiliation/affiliation_mixin.h"
#include "chrome/browser/ash/policy/affiliation/affiliation_test_helper.h"
#include "chrome/browser/ash/policy/core/device_policy_cros_browser_test.h"
#include "chrome/browser/ash/policy/core/device_policy_cros_test_helper.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/chromeos/reporting/metric_reporting_prefs.h"
#include "chrome/browser/policy/dm_token_utils.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chromeos/dbus/missive/missive_client_test_observer.h"
#include "components/content_settings/core/common/content_settings_pattern.h"
#include "components/policy/core/common/cloud/dm_token.h"
#include "components/prefs/pref_service.h"
#include "components/reporting/proto/synced/metric_data.pb.h"
#include "components/reporting/proto/synced/record.pb.h"
#include "components/reporting/proto/synced/record_constants.pb.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::Eq;
using ::testing::StrEq;
namespace reporting {
namespace {
constexpr char kTestDMToken[] = "token";
constexpr char kTestUrl[] = "https://a.example.org/";
void AssertRecordData(Priority priority, const Record& record) {
EXPECT_THAT(priority, Eq(Priority::SLOW_BATCH));
ASSERT_TRUE(record.has_destination());
EXPECT_THAT(record.destination(), Eq(Destination::EVENT_METRIC));
ASSERT_TRUE(record.has_dm_token());
EXPECT_THAT(record.dm_token(), StrEq(kTestDMToken));
ASSERT_TRUE(record.has_source_info());
EXPECT_THAT(record.source_info().source(), Eq(SourceInfo::ASH));
}
void AssertMetricData(const MetricData& metric_data) {
EXPECT_TRUE(metric_data.has_timestamp_ms());
EXPECT_TRUE(metric_data.has_event_data());
ASSERT_TRUE(metric_data.has_telemetry_data());
EXPECT_TRUE(metric_data.telemetry_data().has_website_telemetry());
}
// Returns true if the record includes the specified metric event type. False
// otherwise.
bool IsMetricEventOfType(MetricEventType metric_event_type,
const Record& record) {
MetricData record_data;
return record_data.ParseFromString(record.data()) &&
record_data.has_event_data() &&
(record_data.event_data().type() == metric_event_type);
}
class WebsiteEventsObserverBrowserTest
: public ::policy::DevicePolicyCrosBrowserTest {
protected:
WebsiteEventsObserverBrowserTest() {
crypto_home_mixin_.MarkUserAsExisting(affiliation_mixin_.account_id());
crypto_home_mixin_.ApplyAuthConfig(
affiliation_mixin_.account_id(),
ash::test::UserAuthConfig::Create(ash::test::kDefaultAuthSetup));
::policy::SetDMTokenForTesting(
::policy::DMToken::CreateValidToken(kTestDMToken));
}
void SetUpCommandLine(base::CommandLine* command_line) override {
::policy::AffiliationTestHelper::AppendCommandLineSwitchesForLoginManager(
command_line);
::policy::DevicePolicyCrosBrowserTest::SetUpCommandLine(command_line);
}
void SetAllowlistedUrls(const std::vector<std::string>& allowlisted_urls) {
base::Value::List allowed_urls;
for (const auto& url : allowlisted_urls) {
allowed_urls.Append(url);
}
profile()->GetPrefs()->SetList(kReportWebsiteActivityAllowlist,
std::move(allowed_urls));
}
Profile* profile() {
return ::ash::ProfileHelper::Get()->GetProfileByAccountId(
affiliation_mixin_.account_id());
}
::policy::DevicePolicyCrosTestHelper test_helper_;
::policy::AffiliationMixin affiliation_mixin_{&mixin_host_, &test_helper_};
::ash::CryptohomeMixin crypto_home_mixin_{&mixin_host_};
::apps::WebsiteMetricsBrowserTestMixin website_metrics_browser_test_mixin_{
&mixin_host_};
};
IN_PROC_BROWSER_TEST_F(WebsiteEventsObserverBrowserTest,
PRE_ReportAllUrlOpened) {
// Set up affiliated user.
::policy::AffiliationTestHelper::PreLoginUser(
affiliation_mixin_.account_id());
}
IN_PROC_BROWSER_TEST_F(WebsiteEventsObserverBrowserTest, ReportAllUrlOpened) {
// Login as affiliated user and set policy.
::policy::AffiliationTestHelper::LoginUser(affiliation_mixin_.account_id());
SetAllowlistedUrls({ContentSettingsPattern::Wildcard().ToString()});
// Simulate URL open.
auto* const browser = website_metrics_browser_test_mixin_.CreateBrowser();
::chromeos::MissiveClientTestObserver missive_observer(
base::BindRepeating(&IsMetricEventOfType, MetricEventType::URL_OPENED));
website_metrics_browser_test_mixin_.InsertForegroundTab(browser, kTestUrl);
// Verify data being enqueued.
const auto [priority, record] = missive_observer.GetNextEnqueuedRecord();
AssertRecordData(priority, record);
MetricData metric_data;
ASSERT_TRUE(metric_data.ParseFromString(record.data()));
AssertMetricData(metric_data);
ASSERT_TRUE(metric_data.telemetry_data()
.website_telemetry()
.has_website_opened_data());
const auto& website_opened_data =
metric_data.telemetry_data().website_telemetry().website_opened_data();
EXPECT_THAT(website_opened_data.url(), StrEq(kTestUrl));
EXPECT_TRUE(website_opened_data.has_render_process_host_id());
EXPECT_TRUE(website_opened_data.has_render_frame_routing_id());
}
IN_PROC_BROWSER_TEST_F(WebsiteEventsObserverBrowserTest,
PRE_ReportFilteredUrlOpened) {
// Set up affiliated user.
::policy::AffiliationTestHelper::PreLoginUser(
affiliation_mixin_.account_id());
}
IN_PROC_BROWSER_TEST_F(WebsiteEventsObserverBrowserTest,
ReportFilteredUrlOpened) {
// Login as affiliated user and set policy.
::policy::AffiliationTestHelper::LoginUser(affiliation_mixin_.account_id());
SetAllowlistedUrls({kTestUrl});
// Simulate URL open.
static constexpr char kDisallowedUrl[] = "https://b.example.org/";
auto* const browser = website_metrics_browser_test_mixin_.CreateBrowser();
::chromeos::MissiveClientTestObserver missive_observer(
base::BindRepeating(&IsMetricEventOfType, MetricEventType::URL_OPENED));
website_metrics_browser_test_mixin_.NavigateActiveTab(browser,
kDisallowedUrl);
website_metrics_browser_test_mixin_.InsertForegroundTab(browser, kTestUrl);
// Verify only event with allowlisted URL is being enqueued.
const auto [priority, record] = missive_observer.GetNextEnqueuedRecord();
AssertRecordData(priority, record);
MetricData metric_data;
ASSERT_TRUE(metric_data.ParseFromString(record.data()));
AssertMetricData(metric_data);
ASSERT_TRUE(metric_data.telemetry_data()
.website_telemetry()
.has_website_opened_data());
const auto& website_opened_data =
metric_data.telemetry_data().website_telemetry().website_opened_data();
EXPECT_THAT(website_opened_data.url(), StrEq(kTestUrl));
EXPECT_TRUE(website_opened_data.has_render_process_host_id());
EXPECT_TRUE(website_opened_data.has_render_frame_routing_id());
EXPECT_FALSE(missive_observer.HasNewEnqueuedRecord());
}
IN_PROC_BROWSER_TEST_F(WebsiteEventsObserverBrowserTest,
PRE_DisallowAllUrlOpened) {
// Set up affiliated user.
::policy::AffiliationTestHelper::PreLoginUser(
affiliation_mixin_.account_id());
}
IN_PROC_BROWSER_TEST_F(WebsiteEventsObserverBrowserTest, DisallowAllUrlOpened) {
// Login as affiliated user and set policy to disallow all URLs.
::policy::AffiliationTestHelper::LoginUser(affiliation_mixin_.account_id());
SetAllowlistedUrls({});
// Simulate URL open.
auto* const browser = website_metrics_browser_test_mixin_.CreateBrowser();
::chromeos::MissiveClientTestObserver missive_observer(
base::BindRepeating(&IsMetricEventOfType, MetricEventType::URL_OPENED));
website_metrics_browser_test_mixin_.InsertForegroundTab(browser, kTestUrl);
// Verify data is not enqueued.
EXPECT_FALSE(missive_observer.HasNewEnqueuedRecord());
}
IN_PROC_BROWSER_TEST_F(WebsiteEventsObserverBrowserTest,
PRE_ReportAllUrlClosed) {
// Set up affiliated user.
::policy::AffiliationTestHelper::PreLoginUser(
affiliation_mixin_.account_id());
}
IN_PROC_BROWSER_TEST_F(WebsiteEventsObserverBrowserTest, ReportAllUrlClosed) {
// Login as affiliated user and set policy.
::policy::AffiliationTestHelper::LoginUser(affiliation_mixin_.account_id());
SetAllowlistedUrls({ContentSettingsPattern::Wildcard().ToString()});
// Simulate URL close.
auto* const browser = website_metrics_browser_test_mixin_.CreateBrowser();
website_metrics_browser_test_mixin_.NavigateActiveTab(browser, kTestUrl);
::chromeos::MissiveClientTestObserver missive_observer(
base::BindRepeating(&IsMetricEventOfType, MetricEventType::URL_CLOSED));
browser->tab_strip_model()->CloseAllTabs();
// Verify data being enqueued.
const auto [priority, record] = missive_observer.GetNextEnqueuedRecord();
AssertRecordData(priority, record);
MetricData metric_data;
ASSERT_TRUE(metric_data.ParseFromString(record.data()));
AssertMetricData(metric_data);
ASSERT_TRUE(metric_data.telemetry_data()
.website_telemetry()
.has_website_closed_data());
const auto& website_closed_data =
metric_data.telemetry_data().website_telemetry().website_closed_data();
EXPECT_THAT(website_closed_data.url(), StrEq(kTestUrl));
EXPECT_TRUE(website_closed_data.has_render_process_host_id());
EXPECT_TRUE(website_closed_data.has_render_frame_routing_id());
}
IN_PROC_BROWSER_TEST_F(WebsiteEventsObserverBrowserTest,
PRE_ReportFilteredUrlClosed) {
// Set up affiliated user.
::policy::AffiliationTestHelper::PreLoginUser(
affiliation_mixin_.account_id());
}
IN_PROC_BROWSER_TEST_F(WebsiteEventsObserverBrowserTest,
ReportFilteredUrlClosed) {
// Login as affiliated user and set policy.
::policy::AffiliationTestHelper::LoginUser(affiliation_mixin_.account_id());
SetAllowlistedUrls({kTestUrl});
// Simulate URL close.
static constexpr char kDisallowedUrl[] = "https://b.example.org/";
auto* const browser = website_metrics_browser_test_mixin_.CreateBrowser();
website_metrics_browser_test_mixin_.NavigateActiveTab(browser,
kDisallowedUrl);
website_metrics_browser_test_mixin_.InsertForegroundTab(browser, kTestUrl);
::chromeos::MissiveClientTestObserver missive_observer(
base::BindRepeating(&IsMetricEventOfType, MetricEventType::URL_CLOSED));
browser->tab_strip_model()->CloseAllTabs();
// Verify only event with allowlisted URL is being enqueued.
const auto [priority, record] = missive_observer.GetNextEnqueuedRecord();
AssertRecordData(priority, record);
MetricData metric_data;
ASSERT_TRUE(metric_data.ParseFromString(record.data()));
AssertMetricData(metric_data);
ASSERT_TRUE(metric_data.telemetry_data()
.website_telemetry()
.has_website_closed_data());
const auto& website_closed_data =
metric_data.telemetry_data().website_telemetry().website_closed_data();
EXPECT_THAT(website_closed_data.url(), StrEq(kTestUrl));
EXPECT_TRUE(website_closed_data.has_render_process_host_id());
EXPECT_TRUE(website_closed_data.has_render_frame_routing_id());
EXPECT_FALSE(missive_observer.HasNewEnqueuedRecord());
}
IN_PROC_BROWSER_TEST_F(WebsiteEventsObserverBrowserTest,
PRE_DisallowAllUrlClosed) {
// Set up affiliated user.
::policy::AffiliationTestHelper::PreLoginUser(
affiliation_mixin_.account_id());
}
IN_PROC_BROWSER_TEST_F(WebsiteEventsObserverBrowserTest, DisallowAllUrlClosed) {
// Login as affiliated user and set policy to disallow all URLs.
::policy::AffiliationTestHelper::LoginUser(affiliation_mixin_.account_id());
SetAllowlistedUrls({});
// Simulate URL close.
auto* const browser = website_metrics_browser_test_mixin_.CreateBrowser();
website_metrics_browser_test_mixin_.NavigateActiveTab(browser, kTestUrl);
::chromeos::MissiveClientTestObserver missive_observer(
base::BindRepeating(&IsMetricEventOfType, MetricEventType::URL_CLOSED));
browser->tab_strip_model()->CloseAllTabs();
// Verify data is not enqueued.
EXPECT_FALSE(missive_observer.HasNewEnqueuedRecord());
}
} // namespace
} // namespace reporting
|