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 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
|
// 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 "content/browser/interest_group/interest_group_caching_storage.h"
#include <cstddef>
#include <optional>
#include "base/files/scoped_temp_dir.h"
#include "base/functional/callback_helpers.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "content/browser/interest_group/interest_group_features.h"
#include "content/browser/interest_group/interest_group_update.h"
#include "content/common/features.h"
#include "content/public/common/content_features.h"
#include "net/test/embedded_test_server/request_handler_util.h"
#include "services/network/public/cpp/ad_auction/event_record.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/interest_group/ad_auction_constants.h"
#include "third_party/blink/public/common/interest_group/interest_group.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "url/origin.h"
namespace content {
constexpr char kAdURL[] = "https://www.foo.com/ad1.html";
constexpr char kBiddingURL[] = "https://www.example.test/bidding_logic";
constexpr char kUpdateURL[] = "https://www.example.test/update";
constexpr char kJoiningURL[] = "https://www.test.com";
blink::InterestGroup MakeInterestGroup(url::Origin owner, std::string name) {
blink::InterestGroup group;
group.expiry = base::Time::Now() + base::Days(1);
group.owner = owner;
group.name = name;
group.bidding_url = GURL(kBiddingURL);
group.update_url = GURL(kUpdateURL);
group.ads.emplace();
group.ads->emplace_back(GURL(kAdURL), /*metadata=*/"");
EXPECT_TRUE(group.IsValid());
return group;
}
class InterestGroupCachingStorageTest : public testing::Test {
public:
void SetUp() override { ASSERT_TRUE(temp_directory_.CreateUniqueTempDir()); }
std::optional<scoped_refptr<StorageInterestGroups>> GetInterestGroupsForOwner(
InterestGroupCachingStorage* caching_storage,
const url::Origin& owner) {
std::optional<scoped_refptr<StorageInterestGroups>> result;
base::RunLoop run_loop;
caching_storage->GetInterestGroupsForOwner(
owner,
base::BindLambdaForTesting(
[&result, &run_loop](scoped_refptr<StorageInterestGroups> groups) {
result = std::move(groups);
run_loop.Quit();
}));
run_loop.Run();
return result;
}
void UpdateCachedOrigins(InterestGroupCachingStorage* caching_storage,
const url::Origin& owner) {
GetInterestGroupsForOwner(caching_storage, owner);
caching_storage->UpdateCachedOriginsIfEnabled(owner);
}
std::optional<SingleStorageInterestGroup> GetInterestGroup(
InterestGroupCachingStorage* caching_storage,
const blink::InterestGroupKey& group_key) {
std::optional<SingleStorageInterestGroup> result;
base::RunLoop run_loop;
caching_storage->GetInterestGroup(
group_key, base::BindLambdaForTesting(
[&result, &run_loop](
std::optional<SingleStorageInterestGroup> group) {
result = std::move(group);
run_loop.Quit();
}));
run_loop.Run();
return result;
}
std::vector<url::Origin> GetAllInterestGroupOwners(
InterestGroupCachingStorage* caching_storage) {
std::vector<url::Origin> result;
base::RunLoop run_loop;
caching_storage->GetAllInterestGroupOwners(base::BindLambdaForTesting(
[&result, &run_loop](std::vector<url::Origin> res) {
result = std::move(res);
run_loop.Quit();
}));
run_loop.Run();
return result;
}
std::vector<InterestGroupUpdateParameter> GetInterestGroupsForUpdate(
InterestGroupCachingStorage* caching_storage,
const url::Origin& owner,
int groups_limit) {
std::vector<InterestGroupUpdateParameter> result;
base::RunLoop run_loop;
caching_storage->GetInterestGroupsForUpdate(
owner, groups_limit,
base::BindLambdaForTesting(
[&result,
&run_loop](std::vector<InterestGroupUpdateParameter> res) {
result = std::move(res);
run_loop.Quit();
}));
run_loop.Run();
return result;
}
std::vector<url::Origin> GetAllInterestGroupJoiningOrigins(
InterestGroupCachingStorage* caching_storage) {
std::vector<url::Origin> result;
base::RunLoop run_loop;
caching_storage->GetAllInterestGroupJoiningOrigins(
base::BindLambdaForTesting(
[&result, &run_loop](std::vector<url::Origin> res) {
result = std::move(res);
run_loop.Quit();
}));
run_loop.Run();
return result;
}
std::vector<std::pair<url::Origin, url::Origin>>
GetAllInterestGroupOwnerJoinerPairs(
InterestGroupCachingStorage* caching_storage) {
std::vector<std::pair<url::Origin, url::Origin>> result;
base::RunLoop run_loop;
caching_storage->GetAllInterestGroupOwnerJoinerPairs(
base::BindLambdaForTesting(
[&result,
&run_loop](std::vector<std::pair<url::Origin, url::Origin>> res) {
result = std::move(res);
run_loop.Quit();
}));
run_loop.Run();
return result;
}
std::optional<base::Time> GetLastKAnonymityReported(
InterestGroupCachingStorage* caching_storage,
const std::string& key) {
std::optional<base::Time> result;
base::RunLoop run_loop;
caching_storage->GetLastKAnonymityReported(
key, base::BindLambdaForTesting(
[&result, &run_loop](std::optional<base::Time> res) {
result = std::move(res);
run_loop.Quit();
}));
run_loop.Run();
return result;
}
void JoinInterestGroup(InterestGroupCachingStorage* caching_storage,
const blink::InterestGroup& group,
const GURL& main_frame_joining_url) {
base::RunLoop run_loop;
caching_storage->JoinInterestGroup(
group, main_frame_joining_url,
base::BindLambdaForTesting(
[&run_loop](
std::optional<InterestGroupKanonUpdateParameter> input) {
run_loop.Quit();
}));
run_loop.Run();
}
void LeaveInterestGroup(InterestGroupCachingStorage* caching_storage,
const blink::InterestGroupKey& group_key,
const url::Origin& main_frame) {
base::RunLoop run_loop;
caching_storage->LeaveInterestGroup(
group_key, main_frame,
base::BindLambdaForTesting([&run_loop]() { run_loop.Quit(); }));
run_loop.Run();
}
void UpdateInterestGroup(InterestGroupCachingStorage* caching_storage,
const blink::InterestGroupKey& group_key,
InterestGroupUpdate update) {
base::RunLoop run_loop;
caching_storage->UpdateInterestGroup(
group_key, update,
base::BindLambdaForTesting(
[&run_loop](
std::optional<InterestGroupKanonUpdateParameter> input) {
run_loop.Quit();
}));
run_loop.Run();
}
std::unique_ptr<content::InterestGroupCachingStorage> CreateCachingStorage() {
return std::make_unique<content::InterestGroupCachingStorage>(
temp_directory_.GetPath(), false);
}
base::test::TaskEnvironment& task_environment() { return task_environment_; }
protected:
base::ScopedTempDir temp_directory_;
base::test::TaskEnvironment task_environment_{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
};
TEST_F(InterestGroupCachingStorageTest, DBUpdatesShouldModifyCache) {
std::unique_ptr<content::InterestGroupCachingStorage> caching_storage =
CreateCachingStorage();
GURL joining_url(kJoiningURL);
url::Origin joining_origin(url::Origin::Create(joining_url));
url::Origin owner = url::Origin::Create(GURL("https://www.example.test/"));
auto ig1 = MakeInterestGroup(owner, "name1");
auto ig2 = MakeInterestGroup(owner, "name2");
blink::InterestGroup ig_different_owner;
ig_different_owner.owner = url::Origin::Create(GURL("https://www.other.com"));
ig_different_owner.name = "other";
ig_different_owner.expiry =
base::Time::Now() + blink::MaxInterestGroupLifetime();
std::optional<scoped_refptr<StorageInterestGroups>> loaded_igs =
GetInterestGroupsForOwner(caching_storage.get(), owner);
ASSERT_EQ(loaded_igs->get()->size(), 0u);
// We can get an interest group after joining it.
JoinInterestGroup(caching_storage.get(), ig1, joining_url);
loaded_igs = GetInterestGroupsForOwner(caching_storage.get(), owner);
ASSERT_EQ(loaded_igs->get()->size(), 1u);
std::optional<scoped_refptr<StorageInterestGroups>> previously_loaded_igs =
loaded_igs;
// Getting an interest group a second time works.
loaded_igs = GetInterestGroupsForOwner(caching_storage.get(), owner);
ASSERT_EQ(loaded_igs->get()->size(), 1u);
ASSERT_EQ(loaded_igs->get(), previously_loaded_igs->get());
// Joining a second interest group (for the same owner) works.
JoinInterestGroup(caching_storage.get(), ig2, joining_url);
loaded_igs = GetInterestGroupsForOwner(caching_storage.get(), owner);
ASSERT_EQ(loaded_igs->get()->size(), 2u);
ASSERT_NE(loaded_igs->get(), previously_loaded_igs->get());
previously_loaded_igs = loaded_igs;
// Leaving an interest group updates the cached value.
LeaveInterestGroup(caching_storage.get(),
blink::InterestGroupKey(owner, "name1"), owner);
loaded_igs = GetInterestGroupsForOwner(caching_storage.get(), owner);
ASSERT_EQ(loaded_igs->get()->size(), 1u);
ASSERT_NE(loaded_igs->get(), previously_loaded_igs->get());
previously_loaded_igs = loaded_igs;
caching_storage->ClearOriginJoinedInterestGroups(
owner, {"name2"}, joining_origin,
base::BindLambdaForTesting([](std::vector<std::string>) {}));
task_environment().FastForwardBy(base::Minutes(1));
loaded_igs = GetInterestGroupsForOwner(caching_storage.get(), owner);
ASSERT_EQ(loaded_igs->get()->size(), 1u);
ASSERT_NE(loaded_igs->get(), previously_loaded_igs->get());
previously_loaded_igs = loaded_igs;
InterestGroupUpdate ig_update;
ig_update.priority = 1;
ASSERT_NE(ig2.priority, 1);
UpdateInterestGroup(caching_storage.get(),
blink::InterestGroupKey(owner, "name2"), ig_update);
loaded_igs = GetInterestGroupsForOwner(caching_storage.get(), owner);
ASSERT_EQ(loaded_igs->get()->size(), 1u);
ASSERT_EQ(loaded_igs->get()->GetInterestGroups()[0]->interest_group.priority,
1);
ASSERT_NE(loaded_igs->get(), previously_loaded_igs->get());
previously_loaded_igs = loaded_igs;
// ReportUpdateFailed should _not_ affect our cached values.
caching_storage->ReportUpdateFailed(blink::InterestGroupKey(owner, "name2"),
false);
task_environment().FastForwardBy(base::Minutes(1));
loaded_igs = GetInterestGroupsForOwner(caching_storage.get(), owner);
ASSERT_EQ(loaded_igs->get(), previously_loaded_igs->get());
// RecordInterestGroupBids updates the in-memory object instead of
// invalidating the cache.
caching_storage->RecordInterestGroupBids(
blink::InterestGroupSet({blink::InterestGroupKey(owner, "name2")}));
task_environment().FastForwardBy(base::Minutes(1));
loaded_igs = GetInterestGroupsForOwner(caching_storage.get(), owner);
ASSERT_EQ(loaded_igs->get()->size(), 1u);
ASSERT_EQ(previously_loaded_igs->get()
->GetInterestGroups()[0]
->bidding_browser_signals->bid_count,
1);
ASSERT_EQ(loaded_igs->get()
->GetInterestGroups()[0]
->bidding_browser_signals->bid_count,
1);
previously_loaded_igs = loaded_igs;
caching_storage->RecordInterestGroupWin(
blink::InterestGroupKey(owner, "name2"), "");
task_environment().FastForwardBy(base::Minutes(1));
loaded_igs = GetInterestGroupsForOwner(caching_storage.get(), owner);
ASSERT_EQ(loaded_igs->get()->size(), 1u);
ASSERT_NE(loaded_igs->get(), previously_loaded_igs->get());
ASSERT_EQ(previously_loaded_igs->get()
->GetInterestGroups()[0]
->bidding_browser_signals->prev_wins.size(),
0u);
ASSERT_EQ(loaded_igs->get()
->GetInterestGroups()[0]
->bidding_browser_signals->prev_wins.size(),
1u);
previously_loaded_igs = loaded_igs;
std::string k_anon_key = blink::HashedKAnonKeyForAdBid(ig1, kAdURL);
caching_storage->UpdateKAnonymity(
blink::InterestGroupKey(ig2.owner, ig2.name), {k_anon_key},
base::Time::Now(),
/*replace_existing_values*/ true);
task_environment().FastForwardBy(base::Minutes(1));
loaded_igs = GetInterestGroupsForOwner(caching_storage.get(), owner);
ASSERT_EQ(loaded_igs->get()->size(), 1u);
ASSERT_NE(loaded_igs->get(), previously_loaded_igs->get());
ASSERT_EQ(previously_loaded_igs->get()
->GetInterestGroups()[0]
->hashed_kanon_keys.size(),
0u);
ASSERT_THAT(loaded_igs->get()->GetInterestGroups()[0]->hashed_kanon_keys,
testing::UnorderedElementsAre(k_anon_key));
previously_loaded_igs = loaded_igs;
// UpdateLastKAnonymityReported does not alter any cached values but does
// update the reported time.
base::Time update_time = base::Time::Now();
caching_storage->UpdateLastKAnonymityReported(k_anon_key);
task_environment().FastForwardBy(base::Minutes(1));
loaded_igs = GetInterestGroupsForOwner(caching_storage.get(), owner);
ASSERT_EQ(loaded_igs->get(), previously_loaded_igs->get());
std::optional<base::Time> loaded_time =
GetLastKAnonymityReported(caching_storage.get(), k_anon_key);
ASSERT_EQ(loaded_time, update_time);
loaded_igs = GetInterestGroupsForOwner(caching_storage.get(), owner);
ASSERT_EQ(loaded_igs->get(), previously_loaded_igs->get());
caching_storage->RemoveInterestGroupsMatchingOwnerAndJoiner(
owner, joining_origin, base::BindLambdaForTesting([]() {}));
task_environment().FastForwardBy(base::Minutes(1));
loaded_igs = GetInterestGroupsForOwner(caching_storage.get(), owner);
ASSERT_EQ(loaded_igs->get()->size(), 0u);
ASSERT_NE(loaded_igs->get(), previously_loaded_igs->get());
previously_loaded_igs = loaded_igs;
JoinInterestGroup(caching_storage.get(), ig2, joining_url);
JoinInterestGroup(caching_storage.get(), ig_different_owner, joining_url);
loaded_igs = GetInterestGroupsForOwner(caching_storage.get(),
ig_different_owner.owner);
ASSERT_EQ(loaded_igs->get()->size(), 1u);
loaded_igs = GetInterestGroupsForOwner(caching_storage.get(), owner);
ASSERT_EQ(loaded_igs->get()->size(), 1u);
previously_loaded_igs = loaded_igs;
caching_storage->DeleteInterestGroupData(
base::BindLambdaForTesting([&owner](const blink::StorageKey& candidate) {
return candidate == blink::StorageKey::CreateFirstParty(owner);
}),
/*user_initiated_deletion=*/true, base::BindLambdaForTesting([]() {}));
task_environment().FastForwardBy(base::Minutes(1));
loaded_igs = GetInterestGroupsForOwner(caching_storage.get(),
ig_different_owner.owner);
ASSERT_EQ(loaded_igs->get()->size(), 1u);
loaded_igs = GetInterestGroupsForOwner(caching_storage.get(), owner);
ASSERT_EQ(loaded_igs->get()->size(), 0u);
ASSERT_NE(loaded_igs->get(), previously_loaded_igs->get());
previously_loaded_igs = loaded_igs;
JoinInterestGroup(caching_storage.get(), ig2, joining_url);
JoinInterestGroup(caching_storage.get(), ig_different_owner, joining_url);
caching_storage->DeleteAllInterestGroupData(
base::BindLambdaForTesting([]() {}));
task_environment().FastForwardBy(base::Minutes(1));
loaded_igs = GetInterestGroupsForOwner(caching_storage.get(),
ig_different_owner.owner);
ASSERT_EQ(loaded_igs->get()->size(), 0u);
loaded_igs = GetInterestGroupsForOwner(caching_storage.get(), owner);
ASSERT_EQ(loaded_igs->get()->size(), 0u);
ASSERT_NE(loaded_igs->get(), previously_loaded_igs->get());
previously_loaded_igs = loaded_igs;
JoinInterestGroup(caching_storage.get(), ig2, joining_url);
caching_storage->SetInterestGroupPriority(
blink::InterestGroupKey(ig2.owner, ig2.name), 2);
loaded_igs = GetInterestGroupsForOwner(caching_storage.get(), owner);
ASSERT_NE(loaded_igs->get(), previously_loaded_igs->get());
ASSERT_EQ(loaded_igs->get()->GetInterestGroups()[0]->interest_group.priority,
2);
previously_loaded_igs = loaded_igs;
base::flat_map<std::string, auction_worklet::mojom::PrioritySignalsDoublePtr>
update_priority_signals_overrides;
update_priority_signals_overrides.emplace(
"key", auction_worklet::mojom::PrioritySignalsDouble::New(0));
caching_storage->UpdateInterestGroupPriorityOverrides(
blink::InterestGroupKey(ig2.owner, ig2.name),
std::move(update_priority_signals_overrides));
task_environment().FastForwardBy(base::Minutes(1));
loaded_igs = GetInterestGroupsForOwner(caching_storage.get(), owner);
ASSERT_NE(loaded_igs->get(), previously_loaded_igs->get());
ASSERT_TRUE(loaded_igs->get()
->GetInterestGroups()[0]
->interest_group.priority_signals_overrides->contains("key"));
}
TEST_F(InterestGroupCachingStorageTest, GettersShouldNotModifyCache) {
std::unique_ptr<content::InterestGroupCachingStorage> caching_storage =
CreateCachingStorage();
url::Origin owner = url::Origin::Create(GURL("https://www.example.test/"));
auto ig1 = MakeInterestGroup(owner, "name1");
auto ig2 = MakeInterestGroup(owner, "name2");
JoinInterestGroup(caching_storage.get(), ig1, GURL("https://www.test.com"));
JoinInterestGroup(caching_storage.get(), ig2, GURL("https://www.test.com"));
std::optional<scoped_refptr<StorageInterestGroups>> loaded_igs =
GetInterestGroupsForOwner(caching_storage.get(), owner);
ASSERT_EQ(loaded_igs->get()->size(), 2u);
std::optional<scoped_refptr<StorageInterestGroups>> previously_loaded_igs =
loaded_igs;
GetInterestGroup(caching_storage.get(),
blink::InterestGroupKey(ig1.owner, ig1.name));
loaded_igs = GetInterestGroupsForOwner(caching_storage.get(), owner);
ASSERT_EQ(loaded_igs->get(), previously_loaded_igs->get());
GetAllInterestGroupOwners(caching_storage.get());
loaded_igs = GetInterestGroupsForOwner(caching_storage.get(), owner);
ASSERT_EQ(loaded_igs->get(), previously_loaded_igs->get());
GetInterestGroupsForUpdate(caching_storage.get(), owner, 1);
loaded_igs = GetInterestGroupsForOwner(caching_storage.get(), owner);
ASSERT_EQ(loaded_igs->get(), previously_loaded_igs->get());
GetAllInterestGroupJoiningOrigins(caching_storage.get());
loaded_igs = GetInterestGroupsForOwner(caching_storage.get(), owner);
ASSERT_EQ(loaded_igs->get(), previously_loaded_igs->get());
GetAllInterestGroupOwnerJoinerPairs(caching_storage.get());
loaded_igs = GetInterestGroupsForOwner(caching_storage.get(), owner);
ASSERT_EQ(loaded_igs->get(), previously_loaded_igs->get());
}
TEST_F(InterestGroupCachingStorageTest, GetInterestGroupUsesCache) {
std::unique_ptr<content::InterestGroupCachingStorage> caching_storage =
CreateCachingStorage();
url::Origin owner = url::Origin::Create(GURL("https://www.example.test/"));
auto ig = MakeInterestGroup(owner, "name1");
JoinInterestGroup(caching_storage.get(), ig, GURL("https://www.test.com"));
base::HistogramTester histogram_tester;
// GetInterestGroupsForOwner was not called yet -- there is no cached result
// to use.
std::optional<SingleStorageInterestGroup> pre_cache_loaded_group =
GetInterestGroup(caching_storage.get(),
blink::InterestGroupKey(ig.owner, ig.name));
std::optional<scoped_refptr<StorageInterestGroups>> loaded_igs =
GetInterestGroupsForOwner(caching_storage.get(), owner);
ASSERT_EQ(loaded_igs->get()->size(), 1u);
ASSERT_NE(&(loaded_igs->get()->GetInterestGroups()[0]->interest_group),
&(pre_cache_loaded_group.value()->interest_group));
histogram_tester.ExpectUniqueSample(
"Ads.InterestGroup.GetInterestGroupCacheHit", false, 1);
// There should be a cached value to use now.
std::optional<SingleStorageInterestGroup> post_cache_loaded_group =
GetInterestGroup(caching_storage.get(),
blink::InterestGroupKey(ig.owner, ig.name));
ASSERT_EQ(&(loaded_igs->get()->GetInterestGroups()[0]->interest_group),
&(post_cache_loaded_group.value()->interest_group));
histogram_tester.ExpectBucketCount(
"Ads.InterestGroup.GetInterestGroupCacheHit", true, 1);
// After the cached value expires there should be no cached value.
task_environment().FastForwardBy(base::Days(2));
std::optional<SingleStorageInterestGroup> post_expiration_loaded_group =
GetInterestGroup(caching_storage.get(),
blink::InterestGroupKey(ig.owner, ig.name));
EXPECT_FALSE(post_expiration_loaded_group.has_value());
histogram_tester.ExpectBucketCount(
"Ads.InterestGroup.GetInterestGroupCacheHit", true, 2);
}
TEST_F(InterestGroupCachingStorageTest, CacheWorksWhenPointerReleased) {
std::unique_ptr<content::InterestGroupCachingStorage> caching_storage =
CreateCachingStorage();
GURL joining_url(kJoiningURL);
url::Origin joining_origin(url::Origin::Create(joining_url));
url::Origin owner1 = url::Origin::Create(GURL("https://www.example.test/"));
url::Origin owner2 = url::Origin::Create(GURL("https://www.other.com/"));
auto ig1 = MakeInterestGroup(owner1, "name1");
auto ig2 = MakeInterestGroup(owner1, "name2");
blink::InterestGroup ig_different_owner;
ig_different_owner.owner = owner2;
ig_different_owner.name = "other";
ig_different_owner.expiry =
base::Time::Now() + blink::MaxInterestGroupLifetime();
JoinInterestGroup(caching_storage.get(), ig1, joining_url);
JoinInterestGroup(caching_storage.get(), ig2, joining_url);
JoinInterestGroup(caching_storage.get(), ig_different_owner, joining_url);
std::optional<scoped_refptr<StorageInterestGroups>> loaded_igs_1 =
GetInterestGroupsForOwner(caching_storage.get(), owner1);
std::optional<scoped_refptr<StorageInterestGroups>> loaded_igs_2 =
GetInterestGroupsForOwner(caching_storage.get(), owner2);
// Releasing loaded_igs_1 shouldn't affect loaded_igs_2.
loaded_igs_1.reset();
std::optional<scoped_refptr<StorageInterestGroups>> newly_loaded_igs_1 =
GetInterestGroupsForOwner(caching_storage.get(), owner1);
std::optional<scoped_refptr<StorageInterestGroups>> newly_loaded_igs_2 =
GetInterestGroupsForOwner(caching_storage.get(), owner2);
ASSERT_EQ(loaded_igs_2->get(), newly_loaded_igs_2->get());
ASSERT_EQ(newly_loaded_igs_1->get()->size(), 2u);
ASSERT_EQ(newly_loaded_igs_2->get()->size(), 1u);
}
TEST_F(InterestGroupCachingStorageTest,
CacheCollatesCallsToGetInterestGroupsByOwner) {
std::unique_ptr<content::InterestGroupCachingStorage> caching_storage =
CreateCachingStorage();
url::Origin owner = url::Origin::Create(GURL("https://www.example.test/"));
auto ig = MakeInterestGroup(owner, "name");
JoinInterestGroup(caching_storage.get(), ig, GURL("https://www.test.com"));
base::HistogramTester histogram_tester;
std::optional<scoped_refptr<StorageInterestGroups>> loaded_igs1;
std::optional<scoped_refptr<StorageInterestGroups>> loaded_igs2;
std::optional<scoped_refptr<StorageInterestGroups>> loaded_igs3;
base::RunLoop run_loop;
caching_storage->GetInterestGroupsForOwner(
owner, base::BindLambdaForTesting(
[&loaded_igs1](scoped_refptr<StorageInterestGroups> groups) {
loaded_igs1 = std::move(groups);
}));
caching_storage->GetInterestGroupsForOwner(
owner, base::BindLambdaForTesting(
[&loaded_igs2](scoped_refptr<StorageInterestGroups> groups) {
loaded_igs2 = std::move(groups);
}));
caching_storage->GetInterestGroupsForOwner(
owner, base::BindLambdaForTesting(
[&loaded_igs3,
&run_loop](scoped_refptr<StorageInterestGroups> groups) {
loaded_igs3 = std::move(groups);
run_loop.Quit();
}));
run_loop.Run();
histogram_tester.ExpectBucketCount(
"Ads.InterestGroup.Auction.LoadGroupsUseInProgressLoad", true, 2);
histogram_tester.ExpectBucketCount(
"Ads.InterestGroup.Auction.LoadGroupsUseInProgressLoad", false, 1);
ASSERT_EQ(loaded_igs1->get(), loaded_igs2->get());
ASSERT_EQ(loaded_igs1->get(), loaded_igs3->get());
}
TEST_F(InterestGroupCachingStorageTest,
CacheDoesNotCollateCallsIfInvalidatedSinceOutstandingCall) {
std::unique_ptr<content::InterestGroupCachingStorage> caching_storage =
CreateCachingStorage();
url::Origin owner = url::Origin::Create(GURL("https://www.example.test/"));
auto ig = MakeInterestGroup(owner, "name");
JoinInterestGroup(caching_storage.get(), ig, GURL("https://www.test.com"));
std::optional<scoped_refptr<StorageInterestGroups>> loaded_igs1;
std::optional<scoped_refptr<StorageInterestGroups>> loaded_igs2;
std::optional<scoped_refptr<StorageInterestGroups>> loaded_igs3;
std::optional<scoped_refptr<StorageInterestGroups>> loaded_igs4;
base::RunLoop run_loop;
caching_storage->GetInterestGroupsForOwner(
owner, base::BindLambdaForTesting(
[&loaded_igs1](scoped_refptr<StorageInterestGroups> groups) {
loaded_igs1 = std::move(groups);
}));
caching_storage->GetInterestGroupsForOwner(
owner, base::BindLambdaForTesting(
[&loaded_igs2](scoped_refptr<StorageInterestGroups> groups) {
loaded_igs2 = std::move(groups);
}));
caching_storage->LeaveInterestGroup(blink::InterestGroupKey(owner, "name"),
owner,
base::BindLambdaForTesting([]() {}));
caching_storage->GetInterestGroupsForOwner(
owner, base::BindLambdaForTesting(
[&loaded_igs3](scoped_refptr<StorageInterestGroups> groups) {
loaded_igs3 = std::move(groups);
}));
caching_storage->GetInterestGroupsForOwner(
owner, base::BindLambdaForTesting(
[&loaded_igs4,
&run_loop](scoped_refptr<StorageInterestGroups> groups) {
loaded_igs4 = std::move(groups);
run_loop.Quit();
}));
run_loop.Run();
// The first two results should reflect the state prior to LeaveInterestGroup.
ASSERT_EQ(loaded_igs1->get(), loaded_igs2->get());
ASSERT_EQ(loaded_igs1->get()->size(), 1u);
// The third result should reflect the state after LeaveInterestGroup.
ASSERT_NE(loaded_igs1->get(), loaded_igs3->get());
ASSERT_EQ(loaded_igs3->get()->size(), 0u);
// The fourth result *should* be the same as the third, because those two
// calls should be collated since the invalidation occurred prior to
// the third load.
ASSERT_EQ(loaded_igs3->get(), loaded_igs4->get());
// Another call to GetInterestGroupsForOwner should get the newly cached
// value.
std::optional<scoped_refptr<StorageInterestGroups>> loaded_igs5 =
GetInterestGroupsForOwner(caching_storage.get(), owner);
ASSERT_EQ(loaded_igs3->get(), loaded_igs5->get());
}
TEST_F(InterestGroupCachingStorageTest,
CacheCollatesLoadsCorrectlyWithMultipleInvalidationsInARow) {
std::unique_ptr<content::InterestGroupCachingStorage> caching_storage =
CreateCachingStorage();
url::Origin owner = url::Origin::Create(GURL("https://www.example.test/"));
auto ig = MakeInterestGroup(owner, "name");
std::optional<scoped_refptr<StorageInterestGroups>> loaded_igs1;
std::optional<scoped_refptr<StorageInterestGroups>> loaded_igs2;
std::optional<scoped_refptr<StorageInterestGroups>> loaded_igs3;
std::optional<scoped_refptr<StorageInterestGroups>> loaded_igs4;
// The join and leave and join calls are three invalidations in a row.
base::RunLoop run_loop;
caching_storage->GetInterestGroupsForOwner(
owner, base::BindLambdaForTesting(
[&loaded_igs1](scoped_refptr<StorageInterestGroups> groups) {
loaded_igs1 = std::move(groups);
}));
caching_storage->GetInterestGroupsForOwner(
owner, base::BindLambdaForTesting(
[&loaded_igs2](scoped_refptr<StorageInterestGroups> groups) {
loaded_igs2 = std::move(groups);
}));
caching_storage->JoinInterestGroup(ig, GURL("https://www.test.com"),
base::DoNothing());
caching_storage->LeaveInterestGroup(blink::InterestGroupKey(owner, "name"),
owner,
base::BindLambdaForTesting([]() {}));
caching_storage->JoinInterestGroup(ig, GURL("https://www.test.com"),
base::DoNothing());
caching_storage->GetInterestGroupsForOwner(
owner, base::BindLambdaForTesting(
[&loaded_igs3](scoped_refptr<StorageInterestGroups> groups) {
loaded_igs3 = std::move(groups);
}));
caching_storage->GetInterestGroupsForOwner(
owner, base::BindLambdaForTesting(
[&loaded_igs4,
&run_loop](scoped_refptr<StorageInterestGroups> groups) {
loaded_igs4 = std::move(groups);
run_loop.Quit();
}));
run_loop.Run();
// The first two results should reflect the state prior to Join/Leave/Join.
ASSERT_EQ(loaded_igs1->get(), loaded_igs2->get());
ASSERT_EQ(loaded_igs1->get()->size(), 0u);
// The third result should reflect the state after Join/Leave/Join.
ASSERT_NE(loaded_igs1->get(), loaded_igs3->get());
ASSERT_EQ(loaded_igs3->get()->size(), 1u);
// The fourth result *should* be the same as the third, because those two
// calls should be collated since the invalidation occurred prior to
// the third load.
ASSERT_EQ(loaded_igs3->get(), loaded_igs4->get());
}
TEST_F(InterestGroupCachingStorageTest,
CacheCollatesLoadsCorrectlyWithInvalidationsInterwovenWithLoads) {
std::unique_ptr<content::InterestGroupCachingStorage> caching_storage =
CreateCachingStorage();
url::Origin owner = url::Origin::Create(GURL("https://www.example.test/"));
auto ig = MakeInterestGroup(owner, "name");
std::optional<scoped_refptr<StorageInterestGroups>> loaded_igs1;
std::optional<scoped_refptr<StorageInterestGroups>> loaded_igs2;
std::optional<scoped_refptr<StorageInterestGroups>> loaded_igs3;
std::optional<scoped_refptr<StorageInterestGroups>> loaded_igs4;
std::optional<scoped_refptr<StorageInterestGroups>> loaded_igs5;
// The join and leave are interwoven with outstanding loads.
base::RunLoop run_loop;
caching_storage->GetInterestGroupsForOwner(
owner, base::BindLambdaForTesting(
[&loaded_igs1](scoped_refptr<StorageInterestGroups> groups) {
loaded_igs1 = std::move(groups);
}));
caching_storage->GetInterestGroupsForOwner(
owner, base::BindLambdaForTesting(
[&loaded_igs2](scoped_refptr<StorageInterestGroups> groups) {
loaded_igs2 = std::move(groups);
}));
caching_storage->JoinInterestGroup(ig, GURL("https://www.test.com"),
base::DoNothing());
caching_storage->GetInterestGroupsForOwner(
owner, base::BindLambdaForTesting(
[&loaded_igs3](scoped_refptr<StorageInterestGroups> groups) {
loaded_igs3 = std::move(groups);
}));
caching_storage->GetInterestGroupsForOwner(
owner, base::BindLambdaForTesting(
[&loaded_igs4](scoped_refptr<StorageInterestGroups> groups) {
loaded_igs4 = std::move(groups);
}));
caching_storage->LeaveInterestGroup(blink::InterestGroupKey(owner, "name"),
owner,
base::BindLambdaForTesting([]() {}));
caching_storage->GetInterestGroupsForOwner(
owner, base::BindLambdaForTesting(
[&loaded_igs5,
&run_loop](scoped_refptr<StorageInterestGroups> groups) {
loaded_igs5 = std::move(groups);
run_loop.Quit();
}));
run_loop.Run();
// The first two results should reflect the state prior to JoinInterestGroup.
ASSERT_EQ(loaded_igs1->get(), loaded_igs2->get());
ASSERT_EQ(loaded_igs1->get()->size(), 0u);
// The third & fourth results should reflect the state after
// JoinInterestGroup.
ASSERT_NE(loaded_igs1->get(), loaded_igs3->get());
ASSERT_EQ(loaded_igs3->get(), loaded_igs4->get());
ASSERT_EQ(loaded_igs3->get()->size(), 1u);
// The fifth result should reflect the state after LeaveInterestGroup.
ASSERT_NE(loaded_igs4->get(), loaded_igs5->get());
ASSERT_EQ(loaded_igs5->get()->size(), 0u);
}
TEST_F(InterestGroupCachingStorageTest, LoadGroupsCacheHitHistogram) {
std::unique_ptr<content::InterestGroupCachingStorage> caching_storage =
CreateCachingStorage();
url::Origin owner = url::Origin::Create(GURL("https://www.example.test/"));
auto ig = MakeInterestGroup(owner, "name");
JoinInterestGroup(caching_storage.get(), ig, GURL("https://www.test.com"));
base::HistogramTester histogram_tester;
GetInterestGroupsForOwner(caching_storage.get(), owner);
histogram_tester.ExpectUniqueSample(
"Ads.InterestGroup.Auction.LoadGroupsCacheHit", false, 1);
task_environment_.FastForwardBy(base::Seconds(2));
GetInterestGroupsForOwner(caching_storage.get(), owner);
// Cache hit because the cache holds a reference to the object for
// kMinimumCacheHoldTime.
histogram_tester.ExpectBucketCount(
"Ads.InterestGroup.Auction.LoadGroupsCacheHit", true, 1);
task_environment_.FastForwardBy(
InterestGroupCachingStorage::kMinimumCacheHoldTime - base::Seconds(1));
GetInterestGroupsForOwner(caching_storage.get(), owner);
// More than kMinimumCacheHoldTime has passed since the database load but less
// than kMinimumCacheHoldTime has passed since the last access of the groups.
// The reference should still be in memory.
histogram_tester.ExpectBucketCount(
"Ads.InterestGroup.Auction.LoadGroupsCacheHit", true, 2);
task_environment_.FastForwardBy(
InterestGroupCachingStorage::kMinimumCacheHoldTime);
std::optional<scoped_refptr<StorageInterestGroups>> loaded_igs =
GetInterestGroupsForOwner(caching_storage.get(), owner);
// Not a cache hit because the previous result of GetInterestGroupsForOwner is
// not in memory.
histogram_tester.ExpectBucketCount(
"Ads.InterestGroup.Auction.LoadGroupsCacheHit", false, 2);
task_environment_.FastForwardBy(
InterestGroupCachingStorage::kMinimumCacheHoldTime);
GetInterestGroupsForOwner(caching_storage.get(), owner);
// Cache hit because we have a reference to the object in `loaded_igs`.
histogram_tester.ExpectBucketCount(
"Ads.InterestGroup.Auction.LoadGroupsCacheHit", true, 3);
task_environment_.FastForwardBy(
InterestGroupCachingStorage::kMinimumCacheHoldTime);
GetInterestGroupsForOwner(caching_storage.get(), owner);
histogram_tester.ExpectBucketCount(
"Ads.InterestGroup.Auction.LoadGroupsCacheHit", true, 4);
caching_storage->RecordInterestGroupWin(
blink::InterestGroupKey(owner, "name"), "");
loaded_igs = GetInterestGroupsForOwner(caching_storage.get(), owner);
// Not a cache hit because RecordInterestGroupWin wipes out the cached value.
histogram_tester.ExpectBucketCount(
"Ads.InterestGroup.Auction.LoadGroupsCacheHit", false, 3);
task_environment_.FastForwardBy(
InterestGroupCachingStorage::kMinimumCacheHoldTime);
loaded_igs = GetInterestGroupsForOwner(caching_storage.get(), owner);
histogram_tester.ExpectBucketCount(
"Ads.InterestGroup.Auction.LoadGroupsCacheHit", true, 5);
}
TEST_F(InterestGroupCachingStorageTest, DontLoadCachedInterestGroupsIfExpired) {
std::unique_ptr<content::InterestGroupCachingStorage> caching_storage =
CreateCachingStorage();
url::Origin owner = url::Origin::Create(GURL("https://www.example.test/"));
// Make a group with expiration 1 day from now.
auto ig = MakeInterestGroup(owner, "name");
JoinInterestGroup(caching_storage.get(), ig, GURL("https://www.test.com"));
std::optional<scoped_refptr<StorageInterestGroups>> loaded_igs =
GetInterestGroupsForOwner(caching_storage.get(), owner);
task_environment().FastForwardBy(base::Days(2));
std::optional<scoped_refptr<StorageInterestGroups>> loaded_igs_again =
GetInterestGroupsForOwner(caching_storage.get(), owner);
ASSERT_NE(loaded_igs->get(), loaded_igs_again->get());
}
// See crbug.com/395087859 for why this test is necessary.
TEST_F(InterestGroupCachingStorageTest,
UpdateCachedOriginsWithNullPriorResult) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kFledgeUsePreconnectCache);
std::unique_ptr<content::InterestGroupCachingStorage> caching_storage =
CreateCachingStorage();
GURL test_url("https://www.test.test");
url::Origin owner = url::Origin::Create(GURL("https://www.example.test/"));
blink::InterestGroup ig = MakeInterestGroup(owner, "1");
JoinInterestGroup(caching_storage.get(), ig, test_url);
// Get a previous cached result, but let it expire + go out of scope.
{
std::optional<scoped_refptr<StorageInterestGroups>> loaded_igs =
GetInterestGroupsForOwner(caching_storage.get(), owner);
ASSERT_TRUE(loaded_igs.value());
task_environment_.FastForwardBy(
InterestGroupCachingStorage::kMinimumCacheHoldTime + base::Seconds(1));
}
// Make sure we don't crash when we try UpdateCachedOrigins.
caching_storage->UpdateCachedOriginsIfEnabled(owner);
}
TEST_F(InterestGroupCachingStorageTest, GetCachedOwnerAndSignalsOrigins) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kFledgeUsePreconnectCache);
std::unique_ptr<content::InterestGroupCachingStorage> caching_storage =
CreateCachingStorage();
url::Origin owner1 = url::Origin::Create(GURL("https://www.example.test/"));
blink::InterestGroup ig1 = MakeInterestGroup(owner1, "1");
std::optional<url::Origin> loaded_signals_origin;
ASSERT_FALSE(caching_storage->GetCachedOwnerAndSignalsOrigins(
owner1, loaded_signals_origin));
ASSERT_FALSE(loaded_signals_origin);
GURL test_url("https://www.test.test");
// We shouldn't be able to cache owners which aren't in the
// database.
UpdateCachedOrigins(caching_storage.get(), owner1);
ASSERT_FALSE(caching_storage->GetCachedOwnerAndSignalsOrigins(
owner1, loaded_signals_origin));
ASSERT_FALSE(loaded_signals_origin);
// Join an interest group without a bidding signals url. We should be able to
// cache the owner.
JoinInterestGroup(caching_storage.get(), ig1, test_url);
UpdateCachedOrigins(caching_storage.get(), owner1);
ASSERT_TRUE(caching_storage->GetCachedOwnerAndSignalsOrigins(
owner1, loaded_signals_origin));
ASSERT_FALSE(loaded_signals_origin);
// Join an interest group with a bidding signals url (different origin as the
// owner).
task_environment_.FastForwardBy(base::Minutes(1));
blink::InterestGroup ig2 = MakeInterestGroup(owner1, "2");
ig2.trusted_bidding_signals_url = GURL("https://www.other.test");
JoinInterestGroup(caching_storage.get(), ig2, test_url);
UpdateCachedOrigins(caching_storage.get(), owner1);
ASSERT_TRUE(caching_storage->GetCachedOwnerAndSignalsOrigins(
owner1, loaded_signals_origin));
ASSERT_EQ(loaded_signals_origin,
url::Origin::Create(*ig2.trusted_bidding_signals_url));
// Join an interest group with a bidding signals url (same origin as the
// owner).
task_environment_.FastForwardBy(base::Minutes(1));
blink::InterestGroup ig3 = MakeInterestGroup(owner1, "3");
ig3.trusted_bidding_signals_url = owner1.GetURL();
JoinInterestGroup(caching_storage.get(), ig3, test_url);
UpdateCachedOrigins(caching_storage.get(), owner1);
ASSERT_TRUE(caching_storage->GetCachedOwnerAndSignalsOrigins(
owner1, loaded_signals_origin));
ASSERT_FALSE(loaded_signals_origin);
// Join an interest group from a different owner. Both owners should
// be in the cache.
task_environment_.FastForwardBy(base::Minutes(1));
url::Origin owner2 = url::Origin::Create(GURL("https://www.example2.test/"));
blink::InterestGroup ig4;
ig4.owner = owner2;
ig4.name = "4";
ig4.expiry = ig3.expiry + base::Minutes(3);
ig4.trusted_bidding_signals_url = GURL("https://www.other.test");
JoinInterestGroup(caching_storage.get(), ig4, test_url);
UpdateCachedOrigins(caching_storage.get(), owner2);
InterestGroupCachingStorage::CachedOriginsInfo expected_owner2_cached_info(
ig4);
ASSERT_TRUE(caching_storage->GetCachedOwnerAndSignalsOrigins(
owner1, loaded_signals_origin));
ASSERT_FALSE(loaded_signals_origin);
ASSERT_TRUE(caching_storage->GetCachedOwnerAndSignalsOrigins(
owner2, loaded_signals_origin));
ASSERT_EQ(loaded_signals_origin,
url::Origin::Create(*ig4.trusted_bidding_signals_url));
// Join an interest group that has an earlier expiry than the one cached. That
// should not affect the cache.
blink::InterestGroup ig5;
ig5.owner = owner2;
ig5.name = "5";
ig5.expiry = ig3.expiry;
JoinInterestGroup(caching_storage.get(), ig5, test_url);
UpdateCachedOrigins(caching_storage.get(), owner2);
ASSERT_TRUE(caching_storage->GetCachedOwnerAndSignalsOrigins(
owner1, loaded_signals_origin));
ASSERT_FALSE(loaded_signals_origin);
ASSERT_TRUE(caching_storage->GetCachedOwnerAndSignalsOrigins(
owner2, loaded_signals_origin));
ASSERT_EQ(loaded_signals_origin,
url::Origin::Create(*ig4.trusted_bidding_signals_url));
// Loading the first owner's interest groups should not affect
// the cache because they're not yet expired.
std::optional<scoped_refptr<StorageInterestGroups>> groups =
GetInterestGroupsForOwner(caching_storage.get(), owner1);
ASSERT_TRUE(groups.has_value());
ASSERT_GT(groups.value()->size(), 0u);
ASSERT_TRUE(caching_storage->GetCachedOwnerAndSignalsOrigins(
owner1, loaded_signals_origin));
ASSERT_FALSE(loaded_signals_origin);
ASSERT_TRUE(caching_storage->GetCachedOwnerAndSignalsOrigins(
owner2, loaded_signals_origin));
ASSERT_EQ(loaded_signals_origin,
url::Origin::Create(*ig4.trusted_bidding_signals_url));
// Let the first owner's interest groups expire. Then try loading them.
// The first owner should no longer appear in the cache, but the second
// should.
task_environment_.FastForwardBy(base::Days(1) + base::Minutes(1));
task_environment_.RunUntilIdle();
groups = GetInterestGroupsForOwner(caching_storage.get(), owner1);
ASSERT_TRUE(groups.has_value());
ASSERT_EQ(groups.value()->size(), 0u);
ASSERT_FALSE(caching_storage->GetCachedOwnerAndSignalsOrigins(
owner1, loaded_signals_origin));
ASSERT_TRUE(caching_storage->GetCachedOwnerAndSignalsOrigins(
owner2, loaded_signals_origin));
ASSERT_EQ(loaded_signals_origin,
url::Origin::Create(*ig4.trusted_bidding_signals_url));
// Loading the second owner's interest groups should not affect
// the cache because the last IG is still not expired.
groups = GetInterestGroupsForOwner(caching_storage.get(), owner2);
ASSERT_TRUE(groups.has_value());
ASSERT_GT(groups.value()->size(), 0u);
ASSERT_TRUE(caching_storage->GetCachedOwnerAndSignalsOrigins(
owner2, loaded_signals_origin));
ASSERT_EQ(loaded_signals_origin,
url::Origin::Create(*ig4.trusted_bidding_signals_url));
// DeleteAllInterestGroupData should clear the cache.
caching_storage->DeleteAllInterestGroupData(
base::BindLambdaForTesting([]() {}));
ASSERT_FALSE(caching_storage->GetCachedOwnerAndSignalsOrigins(
owner2, loaded_signals_origin));
// Now join a few more interest groups so that we can test functions that
// update the cache.
blink::InterestGroup ig6 = MakeInterestGroup(owner1, "6");
JoinInterestGroup(caching_storage.get(), ig6, test_url);
task_environment_.FastForwardBy(base::Seconds(1));
blink::InterestGroup ig7 = MakeInterestGroup(owner1, "7");
JoinInterestGroup(caching_storage.get(), ig7, test_url);
task_environment_.FastForwardBy(base::Seconds(1));
blink::InterestGroup ig8 = MakeInterestGroup(owner1, "8");
JoinInterestGroup(caching_storage.get(), ig8, test_url);
UpdateCachedOrigins(caching_storage.get(), owner1);
ASSERT_TRUE(caching_storage->GetCachedOwnerAndSignalsOrigins(
owner1, loaded_signals_origin));
ASSERT_FALSE(loaded_signals_origin);
// An update to an earlier expiring interest group does not affect the cached
// bidding signals URL.
InterestGroupUpdate update;
ig7.trusted_bidding_signals_url = GURL("https://www.another.test");
update.trusted_bidding_signals_url = ig7.trusted_bidding_signals_url;
UpdateInterestGroup(caching_storage.get(),
blink::InterestGroupKey(ig7.owner, ig7.name), update);
ASSERT_TRUE(caching_storage->GetCachedOwnerAndSignalsOrigins(
owner1, loaded_signals_origin));
ASSERT_FALSE(loaded_signals_origin);
// If the update doesn't contain an update to the bidding signals URL -- no
// update is made.
update.trusted_bidding_signals_url.reset();
UpdateInterestGroup(caching_storage.get(),
blink::InterestGroupKey(ig8.owner, ig8.name), update);
ASSERT_TRUE(caching_storage->GetCachedOwnerAndSignalsOrigins(
owner1, loaded_signals_origin));
ASSERT_FALSE(loaded_signals_origin);
// This update would clear the cache because it's for the latest
// expiring IG with an update to the bidding signals URL.
update.trusted_bidding_signals_url = GURL("https://www.other.test");
UpdateInterestGroup(caching_storage.get(),
blink::InterestGroupKey(ig8.owner, ig8.name), update);
ASSERT_FALSE(caching_storage->GetCachedOwnerAndSignalsOrigins(
owner1, loaded_signals_origin));
// We can get this cache again from an IG load. The signals URL reflects
// the update to ig8.
groups = GetInterestGroupsForOwner(caching_storage.get(), owner1);
UpdateCachedOrigins(caching_storage.get(), owner1);
ASSERT_TRUE(caching_storage->GetCachedOwnerAndSignalsOrigins(
owner1, loaded_signals_origin));
ASSERT_EQ(loaded_signals_origin,
url::Origin::Create(*update.trusted_bidding_signals_url));
// Leaving an interest group that isn't the latest joined or loaded should not
// affect the cache.
LeaveInterestGroup(caching_storage.get(),
blink::InterestGroupKey(ig6.owner, ig6.name),
url::Origin::Create(test_url));
ASSERT_TRUE(caching_storage->GetCachedOwnerAndSignalsOrigins(
owner1, loaded_signals_origin));
ASSERT_EQ(loaded_signals_origin,
url::Origin::Create(*update.trusted_bidding_signals_url));
// Leaving an interest group that does match the name will clear the cache.
LeaveInterestGroup(caching_storage.get(),
blink::InterestGroupKey(ig8.owner, ig8.name),
url::Origin::Create(test_url));
ASSERT_FALSE(caching_storage->GetCachedOwnerAndSignalsOrigins(
owner1, loaded_signals_origin));
// We can get the cache again by loading IGs + running
// UpdateCachedOriginsIfEnabled because there were other interest groups with
// the same owner. Now the latest expiring IG will be ig7.
groups = GetInterestGroupsForOwner(caching_storage.get(), owner1);
UpdateCachedOrigins(caching_storage.get(), owner1);
ASSERT_TRUE(groups.has_value());
ASSERT_GT(groups.value()->size(), 0u);
ASSERT_TRUE(caching_storage->GetCachedOwnerAndSignalsOrigins(
owner1, loaded_signals_origin));
EXPECT_EQ(loaded_signals_origin,
url::Origin::Create(*ig7.trusted_bidding_signals_url));
// ClearOriginJoinedInterestGroups won't affect the cache if the latest joined
// IG (ig7) is in `interest_groups_to_keep`.
caching_storage->ClearOriginJoinedInterestGroups(
owner1, {ig7.name}, url::Origin::Create(test_url),
base::BindLambdaForTesting([](std::vector<std::string>) {}));
ASSERT_TRUE(caching_storage->GetCachedOwnerAndSignalsOrigins(
owner1, loaded_signals_origin));
EXPECT_EQ(loaded_signals_origin,
url::Origin::Create(*ig7.trusted_bidding_signals_url));
// ClearOriginJoinedInterestGroups *will* clear the cache for the
// corresponding owner if the IG is not in interest_groups_to_keep.
caching_storage->ClearOriginJoinedInterestGroups(
owner1, {ig6.name}, url::Origin::Create(test_url),
base::BindLambdaForTesting([](std::vector<std::string>) {}));
ASSERT_FALSE(caching_storage->GetCachedOwnerAndSignalsOrigins(
owner1, loaded_signals_origin));
// When the latest expiring interest group expires, the cache does too.
blink::InterestGroup ig9 = MakeInterestGroup(owner1, "9");
JoinInterestGroup(caching_storage.get(), ig9, test_url);
UpdateCachedOrigins(caching_storage.get(), owner1);
ASSERT_TRUE(caching_storage->GetCachedOwnerAndSignalsOrigins(
owner1, loaded_signals_origin));
task_environment_.FastForwardBy(base::Days(1) + base::Minutes(2));
ASSERT_FALSE(caching_storage->GetCachedOwnerAndSignalsOrigins(
owner1, loaded_signals_origin));
// When we join the same interest group twice, the earlier expiry is
// respected.
blink::InterestGroup ig10 = MakeInterestGroup(owner1, "10");
JoinInterestGroup(caching_storage.get(), ig10, test_url);
UpdateCachedOrigins(caching_storage.get(), owner1);
ig10.expiry = ig10.expiry - base::Minutes(1);
ASSERT_TRUE(caching_storage->GetCachedOwnerAndSignalsOrigins(
owner1, loaded_signals_origin));
JoinInterestGroup(caching_storage.get(), ig10, test_url);
task_environment_.FastForwardBy(base::Days(1) - base::Minutes(1) +
base::Milliseconds(1));
ASSERT_FALSE(caching_storage->GetCachedOwnerAndSignalsOrigins(
owner1, loaded_signals_origin));
}
TEST_F(InterestGroupCachingStorageTest, LimitLifetimeForClickiness) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(blink::features::kFledgeClickiness);
std::unique_ptr<content::InterestGroupCachingStorage> caching_storage =
CreateCachingStorage();
// Join a group. It shouldn't have any clicks yet.
url::Origin owner = url::Origin::Create(GURL(kBiddingURL));
GURL joining_url(kJoiningURL);
auto ig = MakeInterestGroup(owner, "name");
// TODO(crbug.com/394108643): The next couple lines will be unneeded when
// the default provider change lands.
ig.view_and_click_counts_providers.emplace();
ig.view_and_click_counts_providers->push_back(owner);
JoinInterestGroup(caching_storage.get(), ig, joining_url);
std::optional<scoped_refptr<StorageInterestGroups>> loaded_igs =
GetInterestGroupsForOwner(caching_storage.get(), owner);
ASSERT_EQ(loaded_igs->get()->size(), 1u);
EXPECT_EQ(loaded_igs->get()
->GetInterestGroups()[0]
->bidding_browser_signals->view_and_click_counts->click_counts
->past_hour,
0);
// Added a click.
network::AdAuctionEventRecord click;
click.type = network::AdAuctionEventRecord::Type::kClick;
click.providing_origin = owner;
click.eligible_origins.push_back(owner);
caching_storage->RecordViewClick(std::move(click));
// Try to look up the IG again, change should be hidden by cache.
// (Note that we're still hanging on to result of previous read to keep it
// around based on that).
auto loaded_igs2 = GetInterestGroupsForOwner(caching_storage.get(), owner);
ASSERT_EQ(loaded_igs2->get()->size(), 1u);
EXPECT_EQ(loaded_igs2->get()
->GetInterestGroups()[0]
->bidding_browser_signals->view_and_click_counts->click_counts
->past_hour,
0);
// After the timeout expires, the new read returns new values, even though
// old value is still held.
task_environment().FastForwardBy(
InterestGroupCachingStorage::kMaximumCacheHoldTime +
base::Milliseconds(1));
auto loaded_igs3 = GetInterestGroupsForOwner(caching_storage.get(), owner);
ASSERT_EQ(loaded_igs3->get()->size(), 1u);
EXPECT_EQ(loaded_igs3->get()
->GetInterestGroups()[0]
->bidding_browser_signals->view_and_click_counts->click_counts
->past_hour,
1);
}
} // namespace content
|