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 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
|
// 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 <stdint.h>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <variant>
#include <vector>
#include "base/base64.h"
#include "base/base_paths.h"
#include "base/check.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/function_ref.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/strings/string_util.h"
#include "base/test/bind.h"
#include "base/test/gmock_expected_support.h"
#include "base/test/values_test_util.h"
#include "base/time/time.h"
#include "components/aggregation_service/aggregation_coordinator_utils.h"
#include "components/attribution_reporting/aggregatable_filtering_id_max_bytes.h"
#include "components/attribution_reporting/source_registration_time_config.mojom.h"
#include "components/attribution_reporting/suitable_origin.h"
#include "content/browser/aggregation_service/aggregatable_report.h"
#include "content/browser/aggregation_service/aggregation_service.h"
#include "content/browser/aggregation_service/aggregation_service_impl.h"
#include "content/browser/aggregation_service/aggregation_service_test_utils.h"
#include "content/browser/aggregation_service/public_key.h"
#include "content/browser/attribution_reporting/aggregatable_attribution_utils.h"
#include "content/browser/attribution_reporting/aggregatable_debug_report.h"
#include "content/browser/attribution_reporting/attribution_test_utils.h"
#include "content/browser/storage_partition_impl.h"
#include "content/public/common/content_paths.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_browser_context.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/abseil-cpp/absl/numeric/int128.h"
#include "third_party/blink/public/mojom/aggregation_service/aggregatable_report.mojom.h"
#include "third_party/boringssl/src/include/openssl/hpke.h"
#include "url/gurl.h"
#include "url/origin.h"
namespace content {
namespace {
using ::attribution_reporting::SuitableOrigin;
using ::blink::mojom::AggregatableReportHistogramContribution;
constexpr char kKeyAggregationServicePayloads[] =
"aggregation_service_payloads";
std::string ReadStringFromFile(const base::FilePath& file, bool trim = false) {
std::string str;
EXPECT_TRUE(base::ReadFileToString(file, &str));
if (trim) {
str = std::string(base::TrimString(str, base::kWhitespaceASCII,
base::TrimPositions::TRIM_ALL));
}
return str;
}
base::Value ParseJsonFromFile(const base::FilePath& file) {
return base::test::ParseJson(ReadStringFromFile(file));
}
// See
// //content/test/data/attribution_reporting/aggregatable_report_goldens/README.md.
class AggregatableReportGoldenLatestVersionTest : public testing::Test {
public:
void SetUp() override {
ASSERT_OK_AND_ASSIGN(
PublicKeyset keyset,
aggregation_service::ReadAndParsePublicKeys(
input_dir_.AppendASCII("public_key.json"), base::Time::Now()));
ASSERT_EQ(keyset.keys.size(), 1u);
aggregation_service().SetPublicKeysForTesting(
GetAggregationServiceProcessingUrl(url::Origin::Create(GURL(
::aggregation_service::kDefaultAggregationCoordinatorAwsCloud))),
keyset);
aggregation_service().SetPublicKeysForTesting(
GetAggregationServiceProcessingUrl(url::Origin::Create(GURL(
::aggregation_service::kDefaultAggregationCoordinatorGcpCloud))),
keyset);
std::optional<std::vector<uint8_t>> private_key =
base::Base64Decode(ReadStringFromFile(
input_dir_.AppendASCII("private_key.txt"), /*trim=*/true));
ASSERT_TRUE(private_key);
ASSERT_EQ(static_cast<int>(private_key->size()), X25519_PRIVATE_KEY_LEN);
ASSERT_TRUE(EVP_HPKE_KEY_init(full_hpke_key_.get(),
EVP_hpke_x25519_hkdf_sha256(),
private_key->data(), private_key->size()));
}
protected:
// Assembles the report for `request`, and verifies that the assembled report
// matches the expected report specified in `report_file` and
//`cleartext_payloads_file`.
// `get_report_body` is a function ref to create report body from the
// assembled report.
void AssembleAndVerifyReport(
AggregatableReportRequest request,
base::FunctionRef<base::Value::Dict(AggregatableReport)> get_report_body,
std::string_view report_file,
std::string_view cleartext_payloads_file) {
base::Value expected_report =
ParseJsonFromFile(input_dir_.AppendASCII(report_file));
ASSERT_TRUE(expected_report.is_dict());
base::Value expected_cleartext_payloads =
ParseJsonFromFile(input_dir_.AppendASCII(cleartext_payloads_file));
ASSERT_TRUE(expected_cleartext_payloads.is_list());
ASSERT_EQ(expected_cleartext_payloads.GetList().size(), 1u);
const std::string* base64_encoded_expected_cleartext_payload =
expected_cleartext_payloads.GetList().front().GetIfString();
ASSERT_TRUE(base64_encoded_expected_cleartext_payload);
base::RunLoop run_loop;
aggregation_service().AssembleReport(
std::move(request),
base::BindLambdaForTesting(
[&](AggregatableReportRequest,
std::optional<AggregatableReport> assembled_report,
AggregationService::AssemblyStatus status) {
EXPECT_EQ(status, AggregationService::AssemblyStatus::kOk);
ASSERT_TRUE(assembled_report);
base::Value::Dict report_body =
get_report_body(*std::move(assembled_report));
EXPECT_TRUE(VerifyReport(
report_body.Clone(), std::move(expected_report).TakeDict(),
*base64_encoded_expected_cleartext_payload))
<< "There was an error, consider bumping report version, "
" actual output for "
<< report_file << " is:\n"
<< report_body;
run_loop.Quit();
}));
run_loop.Run();
}
base::FilePath input_dir_;
private:
AggregationServiceImpl& aggregation_service() {
return *static_cast<AggregationServiceImpl*>(
static_cast<StoragePartitionImpl*>(
browser_context_.GetDefaultStoragePartition())
->GetAggregationService());
}
testing::AssertionResult VerifyReport(
base::Value::Dict actual_report,
base::Value::Dict expected_report,
const std::string& base64_encoded_expected_cleartext_payload) {
std::optional<base::Value> actual_payloads =
actual_report.Extract(kKeyAggregationServicePayloads);
if (!actual_payloads) {
return testing::AssertionFailure() << kKeyAggregationServicePayloads
<< " not present in the actual report";
}
std::optional<base::Value> expected_payloads =
expected_report.Extract(kKeyAggregationServicePayloads);
if (!expected_payloads) {
return testing::AssertionFailure()
<< kKeyAggregationServicePayloads
<< " not present in the expected report";
}
// All other fields are deterministic.
if (actual_report != expected_report) {
return testing::AssertionFailure()
<< "The actual report and expected reports do not match, ignoring "
"the aggregation service payloads";
}
static constexpr char kKeySharedInfo[] = "shared_info";
const std::string* shared_info = expected_report.FindString(kKeySharedInfo);
if (!shared_info) {
return testing::AssertionFailure()
<< kKeySharedInfo << " not present in the report";
}
base::Value::List* actual_payloads_list = actual_payloads->GetIfList();
if (!actual_payloads_list) {
return testing::AssertionFailure() << kKeyAggregationServicePayloads
<< " not a list in the actual report";
}
base::Value::List* expected_payloads_list = expected_payloads->GetIfList();
if (!expected_payloads_list) {
return testing::AssertionFailure()
<< kKeyAggregationServicePayloads
<< " not a list in the expected report";
}
return VerifyAggregationServicePayloads(
std::move(*actual_payloads_list), std::move(*expected_payloads_list),
base64_encoded_expected_cleartext_payload, *shared_info);
}
testing::AssertionResult VerifyAggregationServicePayloads(
base::Value::List actual_payloads,
base::Value::List expected_payloads,
const std::string& base64_encoded_expected_cleartext_payload,
const std::string& shared_info) {
if (actual_payloads.size() != 1u) {
return testing::AssertionFailure()
<< kKeyAggregationServicePayloads
<< " not a list of size 1 in the actual report";
}
base::Value::Dict* actual_payload = actual_payloads.front().GetIfDict();
if (!actual_payload) {
return testing::AssertionFailure()
<< kKeyAggregationServicePayloads
<< "[0] not a dictionary in the actual report";
}
if (expected_payloads.size() != 1u) {
return testing::AssertionFailure()
<< kKeyAggregationServicePayloads
<< " not a list of size 1 in the expected report";
}
base::Value::Dict* expected_payload = expected_payloads.front().GetIfDict();
if (!expected_payload) {
return testing::AssertionFailure()
<< kKeyAggregationServicePayloads
<< "[0] not a dictionary in the expected report";
}
static constexpr char kKeyPayload[] = "payload";
std::optional<base::Value> actual_encrypted_payload =
actual_payload->Extract(kKeyPayload);
if (!actual_encrypted_payload) {
return testing::AssertionFailure()
<< kKeyPayload << " not present in the actual report";
}
std::optional<base::Value> expected_encrypted_payload =
expected_payload->Extract(kKeyPayload);
if (!expected_encrypted_payload) {
return testing::AssertionFailure()
<< kKeyPayload << " not present in the expected report";
}
// All other fields are deterministic.
if (*actual_payload != *expected_payload) {
return testing::AssertionFailure()
<< "The actual and expected aggregation service payloads do not "
"match, ignoring the encrypted payloads";
}
std::vector<uint8_t> actual_decrypted_payload =
DecryptPayload(actual_encrypted_payload->GetString(), shared_info);
if (actual_decrypted_payload.empty()) {
return testing::AssertionFailure()
<< "Failed to decrypt payload in the actual report";
}
std::vector<uint8_t> expected_decrypted_payload =
DecryptPayload(expected_encrypted_payload->GetString(), shared_info);
if (expected_decrypted_payload.empty()) {
return testing::AssertionFailure()
<< "Failed to decrypt payload in the expected payload";
}
if (actual_decrypted_payload != expected_decrypted_payload) {
return testing::AssertionFailure()
<< "The actual and expected decrypted payloads do not match";
}
if (std::string base64_encoded_decrypted_payload =
base::Base64Encode(actual_decrypted_payload);
base64_encoded_decrypted_payload !=
base64_encoded_expected_cleartext_payload) {
return testing::AssertionFailure()
<< "The expected cleartext payload does not match actual "
"decrypted payload, actual output is "
<< base64_encoded_decrypted_payload;
}
return testing::AssertionSuccess();
}
// Returns empty vector in case of an error.
std::vector<uint8_t> DecryptPayload(
const std::string& base64_encoded_encrypted_payload,
const std::string& shared_info) {
std::optional<std::vector<uint8_t>> encrypted_payload =
base::Base64Decode(base64_encoded_encrypted_payload);
if (!encrypted_payload) {
return {};
}
return aggregation_service::DecryptPayloadWithHpke(
*encrypted_payload, *full_hpke_key_.get(), shared_info);
}
BrowserTaskEnvironment task_environment_;
TestBrowserContext browser_context_;
bssl::ScopedEVP_HPKE_KEY full_hpke_key_;
};
class AttributionAggregatableReportGoldenLatestVersionTest
: public AggregatableReportGoldenLatestVersionTest {
public:
void SetUp() override {
base::PathService::Get(content::DIR_TEST_DATA, &input_dir_);
input_dir_ = input_dir_.AppendASCII(
"attribution_reporting/aggregatable_report_goldens/latest");
AggregatableReportGoldenLatestVersionTest::SetUp();
}
protected:
void VerifyAttributionReport(AttributionReport report,
std::string_view report_file,
std::string_view cleartext_payloads_file) {
std::optional<AggregatableReportRequest> request =
CreateAggregatableReportRequest(report);
ASSERT_TRUE(request);
const auto get_report_body = [&](AggregatableReport assembled_report) {
auto* data =
std::get_if<AttributionReport::AggregatableData>(&report.data());
CHECK(data);
data->SetAssembledReport(std::move(assembled_report));
return report.ReportBody();
};
AssembleAndVerifyReport(*std::move(request), get_report_body, report_file,
cleartext_payloads_file);
}
};
TEST_F(AttributionAggregatableReportGoldenLatestVersionTest,
VerifyGoldenReport) {
const auto kGcpCoordinatorOrigin = *SuitableOrigin::Deserialize(
::aggregation_service::kDefaultAggregationCoordinatorGcpCloud);
struct {
AttributionReport report;
std::string_view report_file;
std::string_view cleartext_payloads_file;
} kTestCases[] = {
{.report = ReportBuilder(
AttributionInfoBuilder().SetDebugKey(456).Build(),
SourceBuilder(base::Time::FromMillisecondsSinceUnixEpoch(
1234483200000))
.SetDebugKey(123)
.SetCookieBasedDebugAllowed(true)
.BuildStored())
.SetAggregatableHistogramContributions(
{AggregatableReportHistogramContribution(
/*bucket=*/1,
/*value=*/2, /*filtering_id=*/std::nullopt)})
.SetReportTime(base::Time::FromMillisecondsSinceUnixEpoch(
1234486400000))
.BuildAggregatableAttribution(),
.report_file = "report_1.json",
.cleartext_payloads_file = "report_1_cleartext_payloads.json"},
{.report = ReportBuilder(
AttributionInfoBuilder().Build(),
SourceBuilder(base::Time::FromMillisecondsSinceUnixEpoch(
1234483200000))
.BuildStored())
.SetAggregatableHistogramContributions(
{AggregatableReportHistogramContribution(
/*bucket=*/1,
/*value=*/2, /*filtering_id=*/std::nullopt)})
.SetReportTime(base::Time::FromMillisecondsSinceUnixEpoch(
1234486400000))
.BuildAggregatableAttribution(),
.report_file = "report_2.json",
.cleartext_payloads_file = "report_2_cleartext_payloads.json"},
{.report = ReportBuilder(
AttributionInfoBuilder().SetDebugKey(456).Build(),
SourceBuilder(base::Time::FromMillisecondsSinceUnixEpoch(
1234483300000))
.SetDebugKey(123)
.SetCookieBasedDebugAllowed(true)
.BuildStored())
.SetAggregatableHistogramContributions(
{AggregatableReportHistogramContribution(
/*bucket=*/1,
/*value=*/2, /*filtering_id=*/std::nullopt),
AggregatableReportHistogramContribution(
/*bucket=*/3,
/*value=*/4, /*filtering_id=*/std::nullopt)})
.SetReportTime(base::Time::FromMillisecondsSinceUnixEpoch(
1234486500000))
.BuildAggregatableAttribution(),
.report_file = "report_3.json",
.cleartext_payloads_file = "report_3_cleartext_payloads.json"},
{.report = ReportBuilder(
AttributionInfoBuilder().Build(),
SourceBuilder(base::Time::FromMillisecondsSinceUnixEpoch(
1234483300000))
.BuildStored())
.SetAggregatableHistogramContributions(
{AggregatableReportHistogramContribution(
/*bucket=*/1,
/*value=*/2, /*filtering_id=*/std::nullopt),
AggregatableReportHistogramContribution(
/*bucket=*/3,
/*value=*/4, /*filtering_id=*/std::nullopt)})
.SetReportTime(base::Time::FromMillisecondsSinceUnixEpoch(
1234486500000))
.BuildAggregatableAttribution(),
.report_file = "report_4.json",
.cleartext_payloads_file = "report_4_cleartext_payloads.json"},
{.report = ReportBuilder(
AttributionInfoBuilder().SetDebugKey(456).Build(),
SourceBuilder(base::Time::FromMillisecondsSinceUnixEpoch(
1234483400000))
.SetDebugKey(123)
.SetCookieBasedDebugAllowed(true)
.BuildStored())
.SetAggregatableHistogramContributions(
{AggregatableReportHistogramContribution(
/*bucket=*/absl::Uint128Max(), /*value=*/1000,
/*filtering_id=*/std::nullopt)})
.SetReportTime(base::Time::FromMillisecondsSinceUnixEpoch(
1234486600000))
.BuildAggregatableAttribution(),
.report_file = "report_5.json",
.cleartext_payloads_file = "report_5_cleartext_payloads.json"},
{.report = ReportBuilder(
AttributionInfoBuilder().Build(),
SourceBuilder(base::Time::FromMillisecondsSinceUnixEpoch(
1234483400000))
.BuildStored())
.SetAggregatableHistogramContributions(
{AggregatableReportHistogramContribution(
/*bucket=*/absl::Uint128Max(), /*value=*/1000,
/*filtering_id=*/std::nullopt)})
.SetReportTime(base::Time::FromMillisecondsSinceUnixEpoch(
1234486600000))
.BuildAggregatableAttribution(),
.report_file = "report_6.json",
.cleartext_payloads_file = "report_6_cleartext_payloads.json"},
{.report = ReportBuilder(
AttributionInfoBuilder().Build(),
SourceBuilder(base::Time::FromMillisecondsSinceUnixEpoch(
1234483200000))
.BuildStored())
.SetAggregatableHistogramContributions(
{AggregatableReportHistogramContribution(
/*bucket=*/0,
/*value=*/1, /*filtering_id=*/std::nullopt)})
.SetReportTime(base::Time::FromMillisecondsSinceUnixEpoch(
1234486400000))
.SetSourceRegistrationTimeConfig(
attribution_reporting::mojom::
SourceRegistrationTimeConfig::kExclude)
.BuildAggregatableAttribution(),
.report_file = "report_7.json",
.cleartext_payloads_file = "report_7_cleartext_payloads.json"},
{.report = ReportBuilder(
AttributionInfoBuilder().Build(),
SourceBuilder(base::Time::FromMillisecondsSinceUnixEpoch(
1234483200000))
.BuildStored())
.SetReportTime(base::Time::FromMillisecondsSinceUnixEpoch(
1234486400000))
.SetSourceRegistrationTimeConfig(
attribution_reporting::mojom::
SourceRegistrationTimeConfig::kExclude)
.BuildNullAggregatable(),
.report_file = "report_8.json",
.cleartext_payloads_file = "report_8_cleartext_payloads.json"},
{.report = ReportBuilder(
AttributionInfoBuilder().Build(),
SourceBuilder(base::Time::FromMillisecondsSinceUnixEpoch(
1234483200000))
.BuildStored())
.SetAggregatableHistogramContributions(
{AggregatableReportHistogramContribution(
/*bucket=*/0,
/*value=*/1, /*filtering_id=*/std::nullopt)})
.SetReportTime(base::Time::FromMillisecondsSinceUnixEpoch(
1234486400000))
.SetSourceRegistrationTimeConfig(
attribution_reporting::mojom::
SourceRegistrationTimeConfig::kExclude)
.SetTriggerContextId("example")
.BuildAggregatableAttribution(),
.report_file = "report_9.json",
.cleartext_payloads_file = "report_9_cleartext_payloads.json"},
{.report = ReportBuilder(
AttributionInfoBuilder().Build(),
SourceBuilder(base::Time::FromMillisecondsSinceUnixEpoch(
1234483200000))
.BuildStored())
.SetAggregatableHistogramContributions(
{AggregatableReportHistogramContribution(
/*bucket=*/0,
/*value=*/33, /*filtering_id=*/257)})
.SetReportTime(base::Time::FromMillisecondsSinceUnixEpoch(
1234486400000))
.SetSourceRegistrationTimeConfig(
attribution_reporting::mojom::
SourceRegistrationTimeConfig::kExclude)
.SetTriggerContextId("example")
.SetAggregatableFilteringIdsMaxBytes(
*attribution_reporting::
AggregatableFilteringIdsMaxBytes::Create(2))
.BuildAggregatableAttribution(),
.report_file = "report_10.json",
.cleartext_payloads_file = "report_10_cleartext_payloads.json"},
{.report = ReportBuilder(
AttributionInfoBuilder().SetDebugKey(456).Build(),
SourceBuilder(base::Time::FromMillisecondsSinceUnixEpoch(
1234483200000))
.SetDebugKey(123)
.SetCookieBasedDebugAllowed(true)
.BuildStored())
.SetAggregatableHistogramContributions(
{AggregatableReportHistogramContribution(
/*bucket=*/1,
/*value=*/2, /*filtering_id=*/std::nullopt)})
.SetReportTime(base::Time::FromMillisecondsSinceUnixEpoch(
1234486400000))
.SetAggregationCoordinatorOrigin(kGcpCoordinatorOrigin)
.BuildAggregatableAttribution(),
.report_file = "report_gcp_1.json",
.cleartext_payloads_file = "report_gcp_1_cleartext_payloads.json"},
{.report = ReportBuilder(
AttributionInfoBuilder().Build(),
SourceBuilder(base::Time::FromMillisecondsSinceUnixEpoch(
1234483200000))
.BuildStored())
.SetAggregatableHistogramContributions(
{AggregatableReportHistogramContribution(
/*bucket=*/1,
/*value=*/2, /*filtering_id=*/std::nullopt)})
.SetReportTime(base::Time::FromMillisecondsSinceUnixEpoch(
1234486400000))
.SetAggregationCoordinatorOrigin(kGcpCoordinatorOrigin)
.BuildAggregatableAttribution(),
.report_file = "report_gcp_2.json",
.cleartext_payloads_file = "report_gcp_2_cleartext_payloads.json"},
{.report = ReportBuilder(
AttributionInfoBuilder().SetDebugKey(456).Build(),
SourceBuilder(base::Time::FromMillisecondsSinceUnixEpoch(
1234483300000))
.SetDebugKey(123)
.SetCookieBasedDebugAllowed(true)
.BuildStored())
.SetAggregatableHistogramContributions(
{AggregatableReportHistogramContribution(
/*bucket=*/1,
/*value=*/2, /*filtering_id=*/std::nullopt),
AggregatableReportHistogramContribution(
/*bucket=*/3,
/*value=*/4, /*filtering_id=*/std::nullopt)})
.SetReportTime(base::Time::FromMillisecondsSinceUnixEpoch(
1234486500000))
.SetAggregationCoordinatorOrigin(kGcpCoordinatorOrigin)
.BuildAggregatableAttribution(),
.report_file = "report_gcp_3.json",
.cleartext_payloads_file = "report_gcp_3_cleartext_payloads.json"},
{.report = ReportBuilder(
AttributionInfoBuilder().Build(),
SourceBuilder(base::Time::FromMillisecondsSinceUnixEpoch(
1234483300000))
.BuildStored())
.SetAggregatableHistogramContributions(
{AggregatableReportHistogramContribution(
/*bucket=*/1,
/*value=*/2, /*filtering_id=*/std::nullopt),
AggregatableReportHistogramContribution(
/*bucket=*/3,
/*value=*/4, /*filtering_id=*/std::nullopt)})
.SetReportTime(base::Time::FromMillisecondsSinceUnixEpoch(
1234486500000))
.SetAggregationCoordinatorOrigin(kGcpCoordinatorOrigin)
.BuildAggregatableAttribution(),
.report_file = "report_gcp_4.json",
.cleartext_payloads_file = "report_gcp_4_cleartext_payloads.json"},
{.report = ReportBuilder(
AttributionInfoBuilder().SetDebugKey(456).Build(),
SourceBuilder(base::Time::FromMillisecondsSinceUnixEpoch(
1234483400000))
.SetDebugKey(123)
.SetCookieBasedDebugAllowed(true)
.BuildStored())
.SetAggregatableHistogramContributions(
{AggregatableReportHistogramContribution(
/*bucket=*/absl::Uint128Max(), /*value=*/1000,
/*filtering_id=*/std::nullopt)})
.SetReportTime(base::Time::FromMillisecondsSinceUnixEpoch(
1234486600000))
.SetAggregationCoordinatorOrigin(kGcpCoordinatorOrigin)
.BuildAggregatableAttribution(),
.report_file = "report_gcp_5.json",
.cleartext_payloads_file = "report_gcp_5_cleartext_payloads.json"},
{.report = ReportBuilder(
AttributionInfoBuilder().Build(),
SourceBuilder(base::Time::FromMillisecondsSinceUnixEpoch(
1234483400000))
.BuildStored())
.SetAggregatableHistogramContributions(
{AggregatableReportHistogramContribution(
/*bucket=*/absl::Uint128Max(), /*value=*/1000,
/*filtering_id=*/std::nullopt)})
.SetReportTime(base::Time::FromMillisecondsSinceUnixEpoch(
1234486600000))
.SetAggregationCoordinatorOrigin(kGcpCoordinatorOrigin)
.BuildAggregatableAttribution(),
.report_file = "report_gcp_6.json",
.cleartext_payloads_file = "report_gcp_6_cleartext_payloads.json"},
{.report = ReportBuilder(
AttributionInfoBuilder().Build(),
SourceBuilder(base::Time::FromMillisecondsSinceUnixEpoch(
1234483200000))
.BuildStored())
.SetAggregatableHistogramContributions(
{AggregatableReportHistogramContribution(
/*bucket=*/0,
/*value=*/1, /*filtering_id=*/std::nullopt)})
.SetReportTime(base::Time::FromMillisecondsSinceUnixEpoch(
1234486400000))
.SetSourceRegistrationTimeConfig(
attribution_reporting::mojom::
SourceRegistrationTimeConfig::kExclude)
.SetAggregationCoordinatorOrigin(kGcpCoordinatorOrigin)
.BuildAggregatableAttribution(),
.report_file = "report_gcp_7.json",
.cleartext_payloads_file = "report_gcp_7_cleartext_payloads.json"},
{.report = ReportBuilder(
AttributionInfoBuilder().Build(),
SourceBuilder(base::Time::FromMillisecondsSinceUnixEpoch(
1234483200000))
.BuildStored())
.SetReportTime(base::Time::FromMillisecondsSinceUnixEpoch(
1234486400000))
.SetSourceRegistrationTimeConfig(
attribution_reporting::mojom::
SourceRegistrationTimeConfig::kExclude)
.SetAggregationCoordinatorOrigin(kGcpCoordinatorOrigin)
.BuildNullAggregatable(),
.report_file = "report_gcp_8.json",
.cleartext_payloads_file = "report_gcp_8_cleartext_payloads.json"},
{.report = ReportBuilder(
AttributionInfoBuilder().Build(),
SourceBuilder(base::Time::FromMillisecondsSinceUnixEpoch(
1234483200000))
.BuildStored())
.SetAggregatableHistogramContributions(
{AggregatableReportHistogramContribution(
/*bucket=*/0,
/*value=*/1, /*filtering_id=*/std::nullopt)})
.SetReportTime(base::Time::FromMillisecondsSinceUnixEpoch(
1234486400000))
.SetSourceRegistrationTimeConfig(
attribution_reporting::mojom::
SourceRegistrationTimeConfig::kExclude)
.SetTriggerContextId("example")
.SetAggregationCoordinatorOrigin(kGcpCoordinatorOrigin)
.BuildAggregatableAttribution(),
.report_file = "report_gcp_9.json",
.cleartext_payloads_file = "report_gcp_9_cleartext_payloads.json"},
};
for (auto& test_case : kTestCases) {
VerifyAttributionReport(std::move(test_case.report), test_case.report_file,
test_case.cleartext_payloads_file);
}
}
class AggregatableDebugReportGoldenLatestVersionTest
: public AggregatableReportGoldenLatestVersionTest {
public:
void SetUp() override {
base::PathService::Get(content::DIR_TEST_DATA, &input_dir_);
input_dir_ = input_dir_.AppendASCII(
"attribution_reporting/aggregatable_debug_report_goldens/latest");
AggregatableReportGoldenLatestVersionTest::SetUp();
}
protected:
void VerifyAggregatableDebugReport(AggregatableDebugReport report,
std::string_view report_file,
std::string_view cleartext_payloads_file) {
std::optional<AggregatableReportRequest> request =
report.CreateAggregatableReportRequest();
ASSERT_TRUE(request);
const auto get_report_body = [](AggregatableReport assembled_report) {
return assembled_report.GetAsJson();
};
AssembleAndVerifyReport(*std::move(request), get_report_body, report_file,
cleartext_payloads_file);
}
};
TEST_F(AggregatableDebugReportGoldenLatestVersionTest, VerifyGoldenReport) {
const auto kGcpCoordinatorOrigin = *SuitableOrigin::Deserialize(
::aggregation_service::kDefaultAggregationCoordinatorGcpCloud);
const auto context_site =
net::SchemefulSite::Deserialize("https://context.test");
const auto reporting_origin =
*SuitableOrigin::Deserialize("https://report.test");
const auto effective_destination =
net::SchemefulSite::Deserialize("https://conversion.test");
const auto time = base::Time::FromMillisecondsSinceUnixEpoch(1234486400000);
struct {
std::vector<AggregatableReportHistogramContribution> contributions;
std::optional<SuitableOrigin> aggregation_coordinator_origin;
std::string_view report_file;
std::string_view cleartext_payloads_file;
} kTestCases[] = {
{
.contributions = {AggregatableReportHistogramContribution(
/*bucket=*/123, /*value=*/456,
/*filtering_id=*/std::nullopt)},
.report_file = "report_1.json",
.cleartext_payloads_file = "report_1_cleartext_payloads.json",
},
{
.contributions = {AggregatableReportHistogramContribution(
/*bucket=*/123, /*value=*/456,
/*filtering_id=*/std::nullopt),
AggregatableReportHistogramContribution(
/*bucket=*/321, /*value=*/654,
/*filtering_id=*/std::nullopt)},
.report_file = "report_2.json",
.cleartext_payloads_file = "report_2_cleartext_payloads.json",
},
{
// null report
.report_file = "report_3.json",
.cleartext_payloads_file = "report_3_cleartext_payloads.json",
},
{
.contributions = {AggregatableReportHistogramContribution(
/*bucket=*/123, /*value=*/456,
/*filtering_id=*/std::nullopt)},
.aggregation_coordinator_origin = kGcpCoordinatorOrigin,
.report_file = "report_gcp_1.json",
.cleartext_payloads_file = "report_gcp_1_cleartext_payloads.json",
},
{
.contributions = {AggregatableReportHistogramContribution(
/*bucket=*/123, /*value=*/456,
/*filtering_id=*/std::nullopt),
AggregatableReportHistogramContribution(
/*bucket=*/321, /*value=*/654,
/*filtering_id=*/std::nullopt)},
.aggregation_coordinator_origin = kGcpCoordinatorOrigin,
.report_file = "report_gcp_2.json",
.cleartext_payloads_file = "report_gcp_2_cleartext_payloads.json",
},
{
// null report
.aggregation_coordinator_origin = kGcpCoordinatorOrigin,
.report_file = "report_gcp_3.json",
.cleartext_payloads_file = "report_gcp_3_cleartext_payloads.json",
},
};
for (auto& test_case : kTestCases) {
auto report = AggregatableDebugReport::CreateForTesting(
test_case.contributions, context_site, reporting_origin,
effective_destination, test_case.aggregation_coordinator_origin, time);
report.set_report_id(DefaultExternalReportID());
VerifyAggregatableDebugReport(std::move(report), test_case.report_file,
test_case.cleartext_payloads_file);
}
}
// Returns the legacy versions of attribution and debug aggregatable reports.
std::vector<base::FilePath> GetLegacyVersions() {
std::vector<base::FilePath> input_paths;
for (const std::string& relative_path :
{"content/test/data/attribution_reporting/aggregatable_report_goldens",
"content/test/data/attribution_reporting/"
"aggregatable_debug_report_goldens"}) {
base::FilePath input_dir;
base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &input_dir);
input_dir = input_dir.AppendASCII(relative_path);
base::FileEnumerator e(input_dir, /*recursive=*/false,
base::FileEnumerator::DIRECTORIES);
for (base::FilePath name = e.Next(); !name.empty(); name = e.Next()) {
if (name.BaseName() == base::FilePath(FILE_PATH_LITERAL("latest"))) {
continue;
}
input_paths.push_back(std::move(name));
}
}
return input_paths;
}
// Verifies that legacy versions are properly labeled/stored. Note that there
// is an implicit requirement that "version" is located in the "shared_info"
// field in the report.
class AttributionAndDebugAggregatableReportGoldenLegacyVersionTest
: public ::testing::TestWithParam<base::FilePath> {};
TEST_P(AttributionAndDebugAggregatableReportGoldenLegacyVersionTest,
HasExpectedVersion) {
static constexpr std::string_view prefix = "version_";
base::FilePath dir = GetParam();
std::string base_name = dir.BaseName().MaybeAsASCII();
ASSERT_TRUE(base::StartsWith(base_name, prefix));
std::string expected_version = base_name.substr(prefix.size());
base::FileEnumerator e(dir, /*recursive=*/false, base::FileEnumerator::FILES,
FILE_PATH_LITERAL("*.json"));
for (base::FilePath name = e.Next(); !name.empty(); name = e.Next()) {
base::Value value = ParseJsonFromFile(name);
if (!value.is_dict()) {
continue;
}
const base::Value::Dict& dict = value.GetDict();
if (const std::string* shared_info = dict.FindString("shared_info")) {
base::Value shared_info_value = base::test::ParseJson(*shared_info);
EXPECT_TRUE(shared_info_value.is_dict()) << name;
if (!shared_info_value.is_dict()) {
continue;
}
const std::string* version =
shared_info_value.GetDict().FindString("version");
EXPECT_TRUE(version) << name;
if (!version) {
continue;
}
EXPECT_EQ(*version, expected_version) << name;
}
}
}
INSTANTIATE_TEST_SUITE_P(
,
AttributionAndDebugAggregatableReportGoldenLegacyVersionTest,
::testing::ValuesIn(GetLegacyVersions()));
} // namespace
} // namespace content
|