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 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/reporting/reporting_service.h"
#include <memory>
#include <optional>
#include <string>
#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/test/scoped_feature_list.h"
#include "base/time/tick_clock.h"
#include "base/values.h"
#include "net/base/features.h"
#include "net/base/isolation_info.h"
#include "net/base/network_anonymization_key.h"
#include "net/base/schemeful_site.h"
#include "net/reporting/mock_persistent_reporting_store.h"
#include "net/reporting/reporting_browsing_data_remover.h"
#include "net/reporting/reporting_cache.h"
#include "net/reporting/reporting_context.h"
#include "net/reporting/reporting_endpoint.h"
#include "net/reporting/reporting_policy.h"
#include "net/reporting/reporting_report.h"
#include "net/reporting/reporting_service.h"
#include "net/reporting/reporting_target_type.h"
#include "net/reporting/reporting_test_util.h"
#include "net/test/test_with_task_environment.h"
#include "net/url_request/url_request_context_builder.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
#include "url/origin.h"
namespace net {
namespace {
using CommandType = MockPersistentReportingStore::Command::Type;
// The tests are parametrized on a boolean value which represents whether to use
// a MockPersistentReportingStore (if false, no store is used).
class ReportingServiceTest : public ::testing::TestWithParam<bool>,
public WithTaskEnvironment {
protected:
const GURL kUrl_ = GURL("https://origin/path");
const GURL kUrl2_ = GURL("https://origin2/path");
const url::Origin kOrigin_ = url::Origin::Create(kUrl_);
const url::Origin kOrigin2_ = url::Origin::Create(kUrl2_);
const GURL kEndpoint_ = GURL("https://endpoint/");
const GURL kEndpoint2_ = GURL("https://endpoint2/");
const std::string kUserAgent_ = "Mozilla/1.0";
const std::string kGroup_ = "group";
const std::string kGroup2_ = "group2";
const std::string kType_ = "type";
const std::optional<base::UnguessableToken> kReportingSource_ =
base::UnguessableToken::Create();
const NetworkAnonymizationKey kNak_ =
NetworkAnonymizationKey::CreateSameSite(SchemefulSite(kOrigin_));
const NetworkAnonymizationKey kNak2_ =
NetworkAnonymizationKey::CreateSameSite(SchemefulSite(kOrigin2_));
const ReportingEndpointGroupKey kGroupKey_ =
ReportingEndpointGroupKey(kNak_,
kOrigin_,
kGroup_,
ReportingTargetType::kDeveloper);
const ReportingEndpointGroupKey kGroupKey2_ =
ReportingEndpointGroupKey(kNak2_,
kOrigin2_,
kGroup_,
ReportingTargetType::kDeveloper);
const IsolationInfo kIsolationInfo_ =
IsolationInfo::Create(IsolationInfo::RequestType::kOther,
kOrigin_,
kOrigin_,
SiteForCookies::FromOrigin(kOrigin_));
ReportingServiceTest() {
feature_list_.InitAndEnableFeature(
features::kPartitionConnectionsByNetworkIsolationKey);
Init();
}
// Initializes, or re-initializes, |service_| and its dependencies.
void Init() {
// Must destroy old service, if there is one, before destroying old store.
// Need to clear `context_` first, since it points to an object owned by the
// service.
context_ = nullptr;
service_.reset();
if (GetParam()) {
store_ = std::make_unique<MockPersistentReportingStore>();
} else {
store_ = nullptr;
}
auto test_context = std::make_unique<TestReportingContext>(
&clock_, &tick_clock_, ReportingPolicy(), store_.get());
context_ = test_context.get();
service_ = ReportingService::CreateForTesting(std::move(test_context));
}
// If the store exists, simulate finishing loading the store, which should
// make the rest of the test run synchronously.
void FinishLoading(bool load_success) {
if (store_) {
store_->FinishLoading(load_success);
}
}
MockPersistentReportingStore* store() { return store_.get(); }
TestReportingContext* context() { return context_; }
ReportingService* service() { return service_.get(); }
private:
base::test::ScopedFeatureList feature_list_;
base::SimpleTestClock clock_;
base::SimpleTestTickClock tick_clock_;
std::unique_ptr<MockPersistentReportingStore> store_;
std::unique_ptr<ReportingService> service_;
raw_ptr<TestReportingContext> context_ = nullptr;
};
TEST_P(ReportingServiceTest, QueueReport) {
service()->QueueReport(kUrl_, kReportingSource_, kNak_, kUserAgent_, kGroup_,
kType_, base::Value::Dict(), 0,
ReportingTargetType::kDeveloper);
FinishLoading(true /* load_success */);
std::vector<raw_ptr<const ReportingReport, VectorExperimental>> reports;
context()->cache()->GetReports(&reports);
ASSERT_EQ(1u, reports.size());
EXPECT_EQ(kUrl_, reports[0]->url);
EXPECT_EQ(kNak_, reports[0]->network_anonymization_key);
EXPECT_EQ(kUserAgent_, reports[0]->user_agent);
EXPECT_EQ(kGroup_, reports[0]->group);
EXPECT_EQ(kType_, reports[0]->type);
EXPECT_EQ(ReportingTargetType::kDeveloper, reports[0]->target_type);
}
TEST_P(ReportingServiceTest, QueueEnterpriseReport) {
service()->QueueReport(kUrl_, kReportingSource_, kNak_, kUserAgent_, kGroup_,
kType_, base::Value::Dict(), 0,
ReportingTargetType::kEnterprise);
FinishLoading(true /* load_success */);
std::vector<raw_ptr<const ReportingReport, VectorExperimental>> reports;
context()->cache()->GetReports(&reports);
ASSERT_EQ(1u, reports.size());
EXPECT_EQ(kUrl_, reports[0]->url);
EXPECT_EQ(kNak_, reports[0]->network_anonymization_key);
EXPECT_EQ(kUserAgent_, reports[0]->user_agent);
EXPECT_EQ(kGroup_, reports[0]->group);
EXPECT_EQ(kType_, reports[0]->type);
EXPECT_EQ(ReportingTargetType::kEnterprise, reports[0]->target_type);
}
TEST_P(ReportingServiceTest, QueueReportSanitizeUrl) {
// Same as kUrl_ but with username, password, and fragment.
GURL url = GURL("https://username:password@origin/path#fragment");
service()->QueueReport(url, kReportingSource_, kNak_, kUserAgent_, kGroup_,
kType_, base::Value::Dict(), 0,
ReportingTargetType::kDeveloper);
FinishLoading(true /* load_success */);
std::vector<raw_ptr<const ReportingReport, VectorExperimental>> reports;
context()->cache()->GetReports(&reports);
ASSERT_EQ(1u, reports.size());
EXPECT_EQ(kUrl_, reports[0]->url);
EXPECT_EQ(kNak_, reports[0]->network_anonymization_key);
EXPECT_EQ(kUserAgent_, reports[0]->user_agent);
EXPECT_EQ(kGroup_, reports[0]->group);
EXPECT_EQ(kType_, reports[0]->type);
}
TEST_P(ReportingServiceTest, DontQueueReportInvalidUrl) {
GURL url = GURL("https://");
// This does not trigger an attempt to load from the store because the url
// is immediately rejected as invalid.
service()->QueueReport(url, kReportingSource_, kNak_, kUserAgent_, kGroup_,
kType_, base::Value::Dict(), 0,
ReportingTargetType::kDeveloper);
std::vector<raw_ptr<const ReportingReport, VectorExperimental>> reports;
context()->cache()->GetReports(&reports);
ASSERT_EQ(0u, reports.size());
}
TEST_P(ReportingServiceTest, QueueReportNetworkIsolationKeyDisabled) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndDisableFeature(
features::kPartitionConnectionsByNetworkIsolationKey);
// Re-create the store, so it reads the new feature value.
Init();
service()->QueueReport(kUrl_, kReportingSource_, kNak_, kUserAgent_, kGroup_,
kType_, base::Value::Dict(), 0,
ReportingTargetType::kDeveloper);
FinishLoading(true /* load_success */);
std::vector<raw_ptr<const ReportingReport, VectorExperimental>> reports;
context()->cache()->GetReports(&reports);
ASSERT_EQ(1u, reports.size());
// NetworkAnonymizationKey should be empty, instead of kNak_;
EXPECT_EQ(NetworkAnonymizationKey(), reports[0]->network_anonymization_key);
EXPECT_NE(kNak_, reports[0]->network_anonymization_key);
EXPECT_EQ(kUrl_, reports[0]->url);
EXPECT_EQ(kUserAgent_, reports[0]->user_agent);
EXPECT_EQ(kGroup_, reports[0]->group);
EXPECT_EQ(kType_, reports[0]->type);
}
TEST_P(ReportingServiceTest, ProcessReportToHeader) {
service()->ProcessReportToHeader(kOrigin_, kNak_,
"{\"endpoints\":[{\"url\":\"" +
kEndpoint_.spec() +
"\"}],"
"\"group\":\"" +
kGroup_ +
"\","
"\"max_age\":86400}");
FinishLoading(true /* load_success */);
EXPECT_EQ(1u, context()->cache()->GetEndpointCount());
EXPECT_TRUE(context()->cache()->GetEndpointForTesting(
ReportingEndpointGroupKey(kNak_, kOrigin_, kGroup_,
ReportingTargetType::kDeveloper),
kEndpoint_));
}
TEST_P(ReportingServiceTest, ProcessReportingEndpointsHeader) {
auto parsed_header =
ParseReportingEndpoints(kGroup_ + "=\"" + kEndpoint_.spec() + "\"");
ASSERT_TRUE(parsed_header.has_value());
service()->SetDocumentReportingEndpoints(*kReportingSource_, kOrigin_,
kIsolationInfo_, *parsed_header);
FinishLoading(true /* load_success */);
// Endpoint should not be part of the persistent store.
EXPECT_EQ(0u, context()->cache()->GetEndpointCount());
// Endpoint should be associated with the reporting source.
ReportingEndpoint cached_endpoint =
context()->cache()->GetV1EndpointForTesting(*kReportingSource_, kGroup_);
EXPECT_TRUE(cached_endpoint);
// Ensure that the NAK is stored properly with the endpoint group.
EXPECT_FALSE(cached_endpoint.group_key.network_anonymization_key.IsEmpty());
}
TEST_P(ReportingServiceTest,
ProcessReportingEndpointsHeaderNetworkIsolationKeyDisabled) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndDisableFeature(
features::kPartitionConnectionsByNetworkIsolationKey);
// Re-create the store, so it reads the new feature value.
Init();
auto parsed_header =
ParseReportingEndpoints(kGroup_ + "=\"" + kEndpoint_.spec() + "\"");
ASSERT_TRUE(parsed_header.has_value());
service()->SetDocumentReportingEndpoints(*kReportingSource_, kOrigin_,
kIsolationInfo_, *parsed_header);
FinishLoading(true /* load_success */);
// Endpoint should not be part of the persistent store.
EXPECT_EQ(0u, context()->cache()->GetEndpointCount());
// Endpoint should be associated with the reporting source.
ReportingEndpoint cached_endpoint =
context()->cache()->GetV1EndpointForTesting(*kReportingSource_, kGroup_);
EXPECT_TRUE(cached_endpoint);
// When isolation is disabled, cached endpoints should have a null NAK.
EXPECT_TRUE(cached_endpoint.group_key.network_anonymization_key.IsEmpty());
}
TEST_P(ReportingServiceTest, SendReportsAndRemoveSource) {
auto parsed_header =
ParseReportingEndpoints(kGroup_ + "=\"" + kEndpoint_.spec() + "\", " +
kGroup2_ + "=\"" + kEndpoint2_.spec() + "\"");
ASSERT_TRUE(parsed_header.has_value());
service()->SetDocumentReportingEndpoints(*kReportingSource_, kOrigin_,
kIsolationInfo_, *parsed_header);
// This report should be sent immediately, starting the delivery agent timer.
service()->QueueReport(kUrl_, kReportingSource_, kNak_, kUserAgent_, kGroup_,
kType_, base::Value::Dict(), 0,
ReportingTargetType::kDeveloper);
FinishLoading(true /* load_success */);
std::vector<raw_ptr<const ReportingReport, VectorExperimental>> reports;
context()->cache()->GetReports(&reports);
ASSERT_EQ(1u, reports.size());
EXPECT_EQ(0u, context()->cache()->GetReportCountWithStatusForTesting(
ReportingReport::Status::QUEUED));
// Now simulate the source being destroyed.
service()->SendReportsAndRemoveSource(*kReportingSource_);
// There should be no queued reports, but the previously sent report should
// still be pending.
EXPECT_EQ(0u, context()->cache()->GetReportCountWithStatusForTesting(
ReportingReport::Status::QUEUED));
EXPECT_EQ(1u, context()->cache()->GetReportCountWithStatusForTesting(
ReportingReport::Status::PENDING));
// Source should be marked as expired.
ASSERT_TRUE(
context()->cache()->GetExpiredSources().contains(*kReportingSource_));
}
TEST_P(ReportingServiceTest,
ProcessReportsBeforeSourceExpirationWhenUninitialized) {
// Test only relevant when using a persistent store. The store requires async
// loading, creating the opportunity for backlog tasks to accumulate before
// initialization completes.
if (!store()) {
GTEST_SKIP();
}
auto parsed_header =
ParseReportingEndpoints(kGroup_ + "=\"" + kEndpoint_.spec() + "\"");
ASSERT_TRUE(parsed_header.has_value());
service()->SetDocumentReportingEndpoints(*kReportingSource_, kOrigin_,
kIsolationInfo_, *parsed_header);
// Add a "create report" task to the backlog (service not initialized yet).
service()->QueueReport(kUrl_, kReportingSource_, kNak_, kUserAgent_, kGroup_,
kType_, base::Value::Dict(), 0,
ReportingTargetType::kDeveloper);
// Now simulate the source being destroyed, adding a "mark source as
// expired" task to the backlog.
service()->SendReportsAndRemoveSource(*kReportingSource_);
// Verify neither operation has been processed yet.
std::vector<raw_ptr<const ReportingReport, VectorExperimental>> reports;
context()->cache()->GetReports(&reports);
EXPECT_EQ(0u, reports.size());
EXPECT_FALSE(
context()->cache()->GetExpiredSources().contains(*kReportingSource_));
// Now finish loading, which should process the backlog.
FinishLoading(true /* load_success */);
// 1. First the "create report" task was processed
context()->cache()->GetReports(&reports);
EXPECT_EQ(1u, reports.size());
// 2. Then the "mark source as expired" task was processed
EXPECT_TRUE(
context()->cache()->GetExpiredSources().contains(*kReportingSource_));
}
// Flaky in ChromeOS: crbug.com/1356127
#if BUILDFLAG(IS_CHROMEOS)
#define MAYBE_SendReportsAndRemoveSourceWithPendingReports \
DISABLED_SendReportsAndRemoveSourceWithPendingReports
#else
#define MAYBE_SendReportsAndRemoveSourceWithPendingReports \
SendReportsAndRemoveSourceWithPendingReports
#endif
TEST_P(ReportingServiceTest,
MAYBE_SendReportsAndRemoveSourceWithPendingReports) {
auto parsed_header =
ParseReportingEndpoints(kGroup_ + "=\"" + kEndpoint_.spec() + "\", " +
kGroup2_ + "=\"" + kEndpoint2_.spec() + "\"");
ASSERT_TRUE(parsed_header.has_value());
service()->SetDocumentReportingEndpoints(*kReportingSource_, kOrigin_,
kIsolationInfo_, *parsed_header);
// This report should be sent immediately, starting the delivery agent timer.
service()->QueueReport(kUrl_, kReportingSource_, kNak_, kUserAgent_, kGroup_,
kType_, base::Value::Dict(), 0,
ReportingTargetType::kDeveloper);
FinishLoading(true /* load_success */);
std::vector<raw_ptr<const ReportingReport, VectorExperimental>> reports;
context()->cache()->GetReports(&reports);
ASSERT_EQ(1u, reports.size());
EXPECT_EQ(0u, context()->cache()->GetReportCountWithStatusForTesting(
ReportingReport::Status::QUEUED));
EXPECT_EQ(1u, context()->cache()->GetReportCountWithStatusForTesting(
ReportingReport::Status::PENDING));
// Queue another report, which should remain queued.
service()->QueueReport(kUrl_, kReportingSource_, kNak_, kUserAgent_, kGroup_,
kType_, base::Value::Dict(), 0,
ReportingTargetType::kDeveloper);
EXPECT_EQ(1u, context()->cache()->GetReportCountWithStatusForTesting(
ReportingReport::Status::QUEUED));
EXPECT_EQ(1u, context()->cache()->GetReportCountWithStatusForTesting(
ReportingReport::Status::PENDING));
// Now simulate the source being destroyed.
service()->SendReportsAndRemoveSource(*kReportingSource_);
// The report should still be queued, while the source should be marked as
// expired. (The original report is still pending.)
EXPECT_EQ(1u, context()->cache()->GetReportCountWithStatusForTesting(
ReportingReport::Status::QUEUED));
EXPECT_EQ(1u, context()->cache()->GetReportCountWithStatusForTesting(
ReportingReport::Status::PENDING));
ASSERT_TRUE(
context()->cache()->GetExpiredSources().contains(kReportingSource_));
}
TEST_P(ReportingServiceTest, ProcessReportingEndpointsHeaderPathAbsolute) {
auto parsed_header = ParseReportingEndpoints(kGroup_ + "=\"/path-absolute\"");
ASSERT_TRUE(parsed_header.has_value());
service()->SetDocumentReportingEndpoints(*kReportingSource_, kOrigin_,
kIsolationInfo_, *parsed_header);
FinishLoading(true /* load_success */);
// Endpoint should not be part of the persistent store.
EXPECT_EQ(0u, context()->cache()->GetEndpointCount());
// Endpoint should be associated with the reporting source.
ReportingEndpoint endpoint =
context()->cache()->GetV1EndpointForTesting(*kReportingSource_, kGroup_);
EXPECT_TRUE(endpoint);
// Endpoint should have the correct path.
EXPECT_EQ(kUrl_.Resolve("/path-absolute"), endpoint.info.url);
}
TEST_P(ReportingServiceTest, ProcessReportToHeaderPathAbsolute) {
service()->ProcessReportToHeader(
kOrigin_, kNak_,
"{\"endpoints\":[{\"url\":\"/path-absolute\"}],"
"\"group\":\"" +
kGroup_ +
"\","
"\"max_age\":86400}");
FinishLoading(true /* load_success */);
EXPECT_EQ(1u, context()->cache()->GetEndpointCount());
}
TEST_P(ReportingServiceTest, ProcessReportToHeader_TooLong) {
const std::string header_too_long =
"{\"endpoints\":[{\"url\":\"" + kEndpoint_.spec() +
"\"}],"
"\"group\":\"" +
kGroup_ +
"\","
"\"max_age\":86400," +
"\"junk\":\"" + std::string(32 * 1024, 'a') + "\"}";
// This does not trigger an attempt to load from the store because the header
// is immediately rejected as invalid.
service()->ProcessReportToHeader(kOrigin_, kNak_, header_too_long);
EXPECT_EQ(0u, context()->cache()->GetEndpointCount());
}
TEST_P(ReportingServiceTest, ProcessReportToHeader_TooDeep) {
const std::string header_too_deep = "{\"endpoints\":[{\"url\":\"" +
kEndpoint_.spec() +
"\"}],"
"\"group\":\"" +
kGroup_ +
"\","
"\"max_age\":86400," +
"\"junk\":[[[[[[[[[[]]]]]]]]]]}";
// This does not trigger an attempt to load from the store because the header
// is immediately rejected as invalid.
service()->ProcessReportToHeader(kOrigin_, kNak_, header_too_deep);
EXPECT_EQ(0u, context()->cache()->GetEndpointCount());
}
TEST_P(ReportingServiceTest, ProcessReportToHeaderNetworkIsolationKeyDisabled) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndDisableFeature(
features::kPartitionConnectionsByNetworkIsolationKey);
// Re-create the store, so it reads the new feature value.
Init();
service()->ProcessReportToHeader(kOrigin_, kNak_,
"{\"endpoints\":[{\"url\":\"" +
kEndpoint_.spec() +
"\"}],"
"\"group\":\"" +
kGroup_ +
"\","
"\"max_age\":86400}");
FinishLoading(true /* load_success */);
EXPECT_EQ(1u, context()->cache()->GetEndpointCount());
EXPECT_FALSE(context()->cache()->GetEndpointForTesting(
ReportingEndpointGroupKey(kNak_, kOrigin_, kGroup_,
ReportingTargetType::kDeveloper),
kEndpoint_));
EXPECT_TRUE(context()->cache()->GetEndpointForTesting(
ReportingEndpointGroupKey(NetworkAnonymizationKey(), kOrigin_, kGroup_,
ReportingTargetType::kDeveloper),
kEndpoint_));
}
TEST_P(ReportingServiceTest, WriteToStore) {
if (!store()) {
return;
}
MockPersistentReportingStore::CommandList expected_commands;
// This first call to any public method triggers a load. The load will block
// until we call FinishLoading.
service()->ProcessReportToHeader(kOrigin_, kNak_,
"{\"endpoints\":[{\"url\":\"" +
kEndpoint_.spec() +
"\"}],"
"\"group\":\"" +
kGroup_ +
"\","
"\"max_age\":86400}");
expected_commands.emplace_back(CommandType::LOAD_REPORTING_CLIENTS);
EXPECT_THAT(store()->GetAllCommands(),
testing::UnorderedElementsAreArray(expected_commands));
// Unblock the load. The will let the remaining calls to the service complete
// without blocking.
FinishLoading(true /* load_success */);
expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
kGroupKey_, kEndpoint_);
expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP,
kGroupKey_);
EXPECT_THAT(store()->GetAllCommands(),
testing::UnorderedElementsAreArray(expected_commands));
service()->ProcessReportToHeader(kOrigin2_, kNak2_,
"{\"endpoints\":[{\"url\":\"" +
kEndpoint_.spec() +
"\"}],"
"\"group\":\"" +
kGroup_ +
"\","
"\"max_age\":86400}");
expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
kGroupKey2_, kEndpoint_);
expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP,
kGroupKey2_);
EXPECT_THAT(store()->GetAllCommands(),
testing::UnorderedElementsAreArray(expected_commands));
service()->QueueReport(kUrl_, kReportingSource_, kNak_, kUserAgent_, kGroup_,
kType_, base::Value::Dict(), 0,
ReportingTargetType::kDeveloper);
expected_commands.emplace_back(
CommandType::UPDATE_REPORTING_ENDPOINT_GROUP_ACCESS_TIME, kGroupKey_);
EXPECT_THAT(store()->GetAllCommands(),
testing::UnorderedElementsAreArray(expected_commands));
service()->RemoveBrowsingData(
ReportingBrowsingDataRemover::DATA_TYPE_CLIENTS,
base::BindRepeating(
[](const url::Origin& origin) { return origin.host() == "origin"; }));
expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT,
kGroupKey_, kEndpoint_);
expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT_GROUP,
kGroupKey_);
expected_commands.emplace_back(CommandType::FLUSH);
EXPECT_THAT(store()->GetAllCommands(),
testing::UnorderedElementsAreArray(expected_commands));
service()->RemoveAllBrowsingData(
ReportingBrowsingDataRemover::DATA_TYPE_CLIENTS);
expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT,
kGroupKey2_, kEndpoint_);
expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT_GROUP,
kGroupKey2_);
expected_commands.emplace_back(CommandType::FLUSH);
EXPECT_THAT(store()->GetAllCommands(),
testing::UnorderedElementsAreArray(expected_commands));
}
TEST_P(ReportingServiceTest, WaitUntilLoadFinishesBeforeWritingToStore) {
if (!store()) {
return;
}
MockPersistentReportingStore::CommandList expected_commands;
// This first call to any public method triggers a load. The load will block
// until we call FinishLoading.
service()->ProcessReportToHeader(kOrigin_, kNak_,
"{\"endpoints\":[{\"url\":\"" +
kEndpoint_.spec() +
"\"}],"
"\"group\":\"" +
kGroup_ +
"\","
"\"max_age\":86400}");
expected_commands.emplace_back(CommandType::LOAD_REPORTING_CLIENTS);
EXPECT_THAT(store()->GetAllCommands(),
testing::UnorderedElementsAreArray(expected_commands));
service()->ProcessReportToHeader(kOrigin2_, kNak2_,
"{\"endpoints\":[{\"url\":\"" +
kEndpoint_.spec() +
"\"}],"
"\"group\":\"" +
kGroup_ +
"\","
"\"max_age\":86400}");
EXPECT_THAT(store()->GetAllCommands(),
testing::UnorderedElementsAreArray(expected_commands));
service()->QueueReport(kUrl_, kReportingSource_, kNak_, kUserAgent_, kGroup_,
kType_, base::Value::Dict(), 0,
ReportingTargetType::kDeveloper);
EXPECT_THAT(store()->GetAllCommands(),
testing::UnorderedElementsAreArray(expected_commands));
service()->RemoveBrowsingData(
ReportingBrowsingDataRemover::DATA_TYPE_CLIENTS,
base::BindRepeating(
[](const url::Origin& origin) { return origin.host() == "origin"; }));
EXPECT_THAT(store()->GetAllCommands(),
testing::UnorderedElementsAreArray(expected_commands));
service()->RemoveAllBrowsingData(
ReportingBrowsingDataRemover::DATA_TYPE_CLIENTS);
EXPECT_THAT(store()->GetAllCommands(),
testing::UnorderedElementsAreArray(expected_commands));
// Unblock the load. The will let the remaining calls to the service complete
// without blocking.
FinishLoading(true /* load_success */);
expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
kGroupKey_, kEndpoint_);
expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
kGroupKey2_, kEndpoint_);
expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP,
kGroupKey_);
expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP,
kGroupKey2_);
expected_commands.emplace_back(
CommandType::UPDATE_REPORTING_ENDPOINT_GROUP_ACCESS_TIME, kGroupKey_);
expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT,
kGroupKey_, kEndpoint_);
expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT_GROUP,
kGroupKey_);
expected_commands.emplace_back(CommandType::FLUSH);
expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT,
kGroupKey2_, kEndpoint_);
expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT_GROUP,
kGroupKey2_);
expected_commands.emplace_back(CommandType::FLUSH);
EXPECT_THAT(store()->GetAllCommands(),
testing::UnorderedElementsAreArray(expected_commands));
}
TEST_P(ReportingServiceTest,
SetEnterpriseReportingEndpointsWithFeatureEnabled) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(
net::features::kReportingApiEnableEnterpriseCookieIssues);
EXPECT_EQ(0u, context()->cache()->GetEnterpriseEndpointsForTesting().size());
base::flat_map<std::string, GURL> test_enterprise_endpoints{
{"endpoint-1", GURL("https://example.com/reports")},
{"endpoint-2", GURL("https://reporting.example/cookie-issues")},
{"endpoint-3", GURL("https://report-collector.example")},
};
std::vector<ReportingEndpoint> expected_enterprise_endpoints = {
{ReportingEndpointGroupKey(NetworkAnonymizationKey(),
/*reporting_source=*/std::nullopt,
/*origin=*/std::nullopt, "endpoint-1",
ReportingTargetType::kEnterprise),
{.url = GURL("https://example.com/reports")}},
{ReportingEndpointGroupKey(NetworkAnonymizationKey(),
/*reporting_source=*/std::nullopt,
/*origin=*/std::nullopt, "endpoint-2",
ReportingTargetType::kEnterprise),
{.url = GURL("https://reporting.example/cookie-issues")}},
{ReportingEndpointGroupKey(NetworkAnonymizationKey(),
/*reporting_source=*/std::nullopt,
/*origin=*/std::nullopt, "endpoint-3",
ReportingTargetType::kEnterprise),
{.url = GURL("https://report-collector.example")}}};
service()->SetEnterpriseReportingEndpoints(test_enterprise_endpoints);
EXPECT_EQ(expected_enterprise_endpoints,
context()->cache()->GetEnterpriseEndpointsForTesting());
}
TEST_P(ReportingServiceTest,
SetEnterpriseReportingEndpointsWithFeatureDisabled) {
EXPECT_EQ(0u, context()->cache()->GetEnterpriseEndpointsForTesting().size());
base::flat_map<std::string, GURL> test_enterprise_endpoints{
{"endpoint-1", GURL("https://example.com/reports")},
{"endpoint-2", GURL("https://reporting.example/cookie-issues")},
{"endpoint-3", GURL("https://report-collector.example")},
};
service()->SetEnterpriseReportingEndpoints(test_enterprise_endpoints);
EXPECT_EQ(0u, context()->cache()->GetEnterpriseEndpointsForTesting().size());
}
TEST_P(ReportingServiceTest, ReportingServiceConstructionWithFeatureEnabled) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(
net::features::kReportingApiEnableEnterpriseCookieIssues);
base::flat_map<std::string, GURL> test_enterprise_endpoints{
{"endpoint-1", GURL("https://example.com/reports")},
{"endpoint-2", GURL("https://reporting.example/cookie-issues")},
{"endpoint-3", GURL("https://report-collector.example")},
};
EXPECT_EQ(0u, service()
->GetContextForTesting()
->cache()
->GetEnterpriseEndpointsForTesting()
.size());
std::unique_ptr<URLRequestContext> url_request_context =
CreateTestURLRequestContextBuilder()->Build();
std::unique_ptr<ReportingService> reporting_service_ptr =
ReportingService::Create(ReportingPolicy(), url_request_context.get(),
store(), test_enterprise_endpoints);
std::vector<ReportingEndpoint> expected_enterprise_endpoints = {
{ReportingEndpointGroupKey(NetworkAnonymizationKey(),
/*reporting_source=*/std::nullopt,
/*origin=*/std::nullopt, "endpoint-1",
ReportingTargetType::kEnterprise),
{.url = GURL("https://example.com/reports")}},
{ReportingEndpointGroupKey(NetworkAnonymizationKey(),
/*reporting_source=*/std::nullopt,
/*origin=*/std::nullopt, "endpoint-2",
ReportingTargetType::kEnterprise),
{.url = GURL("https://reporting.example/cookie-issues")}},
{ReportingEndpointGroupKey(NetworkAnonymizationKey(),
/*reporting_source=*/std::nullopt,
/*origin=*/std::nullopt, "endpoint-3",
ReportingTargetType::kEnterprise),
{.url = GURL("https://report-collector.example")}}};
EXPECT_EQ(expected_enterprise_endpoints,
reporting_service_ptr->GetContextForTesting()
->cache()
->GetEnterpriseEndpointsForTesting());
}
TEST_P(ReportingServiceTest, ReportingServiceConstructionWithFeatureDisabled) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndDisableFeature(
net::features::kReportingApiEnableEnterpriseCookieIssues);
base::flat_map<std::string, GURL> test_enterprise_endpoints{
{"endpoint-1", GURL("https://example.com/reports")},
{"endpoint-2", GURL("https://reporting.example/cookie-issues")},
{"endpoint-3", GURL("https://report-collector.example")},
};
EXPECT_EQ(0u, service()
->GetContextForTesting()
->cache()
->GetEnterpriseEndpointsForTesting()
.size());
std::unique_ptr<URLRequestContext> url_request_context =
CreateTestURLRequestContextBuilder()->Build();
std::unique_ptr<ReportingService> reporting_service_ptr =
ReportingService::Create(ReportingPolicy(), url_request_context.get(),
store(), test_enterprise_endpoints);
EXPECT_EQ(0u, reporting_service_ptr->GetContextForTesting()
->cache()
->GetEnterpriseEndpointsForTesting()
.size());
}
INSTANTIATE_TEST_SUITE_P(ReportingServiceStoreTest,
ReportingServiceTest,
::testing::Bool());
} // namespace
} // namespace net
|