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
|
// 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 "content/browser/interest_group/interest_group_priority_util.h"
#include <string>
#include "base/containers/flat_map.h"
#include "base/time/time.h"
#include "content/browser/interest_group/storage_interest_group.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/interest_group/ad_auction_constants.h"
#include "third_party/blink/public/common/interest_group/auction_config.h"
#include "third_party/blink/public/common/interest_group/interest_group.h"
#include "url/gurl.h"
#include "url/origin.h"
namespace content {
class InterestGroupPriorityUtilTest : public testing::Test {
public:
InterestGroupPriorityUtilTest() {
storage_interest_group_.interest_group.priority = 2.5;
storage_interest_group_.interest_group.owner = kOrigin;
}
~InterestGroupPriorityUtilTest() override = default;
blink::AuctionConfig auction_config_;
StorageInterestGroup storage_interest_group_;
const url::Origin kOrigin = url::Origin::Create(GURL("https://origin.test"));
const url::Origin kOtherOrigin =
url::Origin::Create(GURL("https://other-origin.test"));
};
// All priority signals maps are null.
TEST_F(InterestGroupPriorityUtilTest, NullSignals) {
EXPECT_EQ(0, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base::Time(),
/*priority_vector=*/{{"foo", 2}}));
EXPECT_EQ(2, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base::Time(),
/*priority_vector=*/{{"browserSignals.one", 2}}));
EXPECT_EQ(5, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base::Time(),
/*priority_vector=*/{{"browserSignals.basePriority", 2}}));
EXPECT_EQ(
0,
CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base::Time(),
/*priority_vector=*/{{"browserSignals.firstDotProductPriority", 2}}));
EXPECT_EQ(
6,
CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base::Time(),
/*priority_vector=*/{{"browserSignals.firstDotProductPriority", 2}},
/*first_dot_product_priority=*/3));
}
// All priority signals maps exist but are empty.
TEST_F(InterestGroupPriorityUtilTest, EmptySignals) {
auction_config_.non_shared_params.per_buyer_priority_signals.emplace();
auction_config_.non_shared_params.per_buyer_priority_signals->emplace(
kOrigin, base::flat_map<std::string, double>{});
auction_config_.non_shared_params.all_buyers_priority_signals.emplace();
storage_interest_group_.interest_group.priority_signals_overrides.emplace();
EXPECT_EQ(0, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base::Time(),
/*priority_vector=*/{{"foo", 2}}));
EXPECT_EQ(2, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base::Time(),
/*priority_vector=*/{{"browserSignals.one", 2}}));
EXPECT_EQ(5, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base::Time(),
/*priority_vector=*/{{"browserSignals.basePriority", 2}}));
}
TEST_F(InterestGroupPriorityUtilTest, PerBuyerSignals) {
auction_config_.non_shared_params.per_buyer_priority_signals.emplace();
auction_config_.non_shared_params.per_buyer_priority_signals->emplace(
kOrigin, base::flat_map<std::string, double>{{"foo", 2}, {"bar", -1.5}});
// <missing>*2
EXPECT_EQ(0, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base::Time(),
/*priority_vector=*/{{"one", 2}}));
// 2*-1.5
EXPECT_EQ(-3, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base::Time(),
/*priority_vector=*/{{"foo", -1.5}}));
// 2*4 + -1.5*3
EXPECT_EQ(3.5, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base::Time(),
/*priority_vector=*/{{"foo", 4}, {"bar", 3}}));
// 1*3
EXPECT_EQ(3, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base::Time(),
/*priority_vector=*/{{"browserSignals.one", 3}}));
// 2*4 + 1*3
EXPECT_EQ(11, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base::Time(),
/*priority_vector=*/
{{"foo", 4}, {"browserSignals.one", 3}}));
}
TEST_F(InterestGroupPriorityUtilTest, PerBuyerSignalsOtherOrigin) {
auction_config_.non_shared_params.per_buyer_priority_signals.emplace();
// Values for another origin should have no effect.
auction_config_.non_shared_params.per_buyer_priority_signals->emplace(
kOtherOrigin,
base::flat_map<std::string, double>{{"foo", 2}, {"bar", -1.5}});
EXPECT_EQ(0, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base::Time(),
/*priority_vector=*/{{"foo", 2}}));
EXPECT_EQ(0, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base::Time(),
/*priority_vector=*/{{"foo", 3}, {"bar", 4}}));
// Add an entry for kOrigin with the same key as one of the entries for
// kOtherOrigin, but a different value.
auction_config_.non_shared_params.per_buyer_priority_signals->emplace(
kOrigin, base::flat_map<std::string, double>{{"foo", 5}});
EXPECT_EQ(15, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base::Time(),
/*priority_vector=*/{{"foo", 3}, {"bar", -30}}));
}
TEST_F(InterestGroupPriorityUtilTest, AllBuyerSignals) {
auction_config_.non_shared_params.all_buyers_priority_signals = {
{"foo", 2}, {"bar", -1.5}};
// <missing>*2
EXPECT_EQ(0, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base::Time(),
/*priority_vector=*/{{"one", 2}}));
// 2*-1.5
EXPECT_EQ(-3, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base::Time(),
/*priority_vector=*/{{"foo", -1.5}}));
// 2*4 + -1.5*3
EXPECT_EQ(3.5, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base::Time(),
/*priority_vector=*/{{"foo", 4}, {"bar", 3}}));
// 1*3
EXPECT_EQ(3, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base::Time(),
/*priority_vector=*/{{"browserSignals.one", 3}}));
// 2*4 + 1*3
EXPECT_EQ(11, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base::Time(),
/*priority_vector=*/
{{"foo", 4}, {"browserSignals.one", 3}}));
}
TEST_F(InterestGroupPriorityUtilTest, PrioritySignalsOverrides) {
storage_interest_group_.interest_group.priority_signals_overrides = {
{"foo", 2},
{"bar", -1.5},
// This should override the `browserSignals` values added by default.
{"browserSignals.one", -4}};
// <missing>*2
EXPECT_EQ(0, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base::Time(),
/*priority_vector=*/{{"one", 2}}));
// 2*-1.5
EXPECT_EQ(-3, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base::Time(),
/*priority_vector=*/{{"foo", -1.5}}));
// 2*4 + -1.5*3
EXPECT_EQ(3.5, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base::Time(),
/*priority_vector=*/{{"foo", 4}, {"bar", 3}}));
// -4 * 3
EXPECT_EQ(-12, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base::Time(),
/*priority_vector=*/{{"browserSignals.one", 3}}));
// 2*4 + -4*3
EXPECT_EQ(-4, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base::Time(),
/*priority_vector=*/
{{"foo", 4}, {"browserSignals.one", 3}}));
// browserSignals.priority should not be masked.
// 2.5 * 5
EXPECT_EQ(5, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base::Time(),
/*priority_vector=*/{{"browserSignals.basePriority", 2}}));
}
// Test relative priority of perBuyerSignals, allBuyersSignals, and
// prioritySignalsOverrides (browserSignals are tested in the overrides test
// case).
TEST_F(InterestGroupPriorityUtilTest, PrioritySignalsMasking) {
storage_interest_group_.interest_group.priority_signals_overrides = {
{"foo", 1}};
auction_config_.non_shared_params.per_buyer_priority_signals.emplace();
auction_config_.non_shared_params.per_buyer_priority_signals->emplace(
kOrigin, base::flat_map<std::string, double>{{"foo", 10}, {"bar", 2}});
auction_config_.non_shared_params.all_buyers_priority_signals = {
{"foo", 10}, {"bar", 10}, {"baz", 3}};
// "foo" should come from `priority_signals_overrides`, masking the values in
// the other two maps.
EXPECT_EQ(1, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base::Time(),
/*priority_vector=*/{{"foo", 1}}));
// "bar" should come from `per_buyer_priority_signals`, masking the value in
// `all_buyers_priority_signals`.
EXPECT_EQ(2, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base::Time(),
/*priority_vector=*/{{"bar", 1}}));
// "baz" should come from `all_buyers_priority_signals`, since no other maps
// have an entry for it.
EXPECT_EQ(3, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base::Time(),
/*priority_vector=*/{{"baz", 1}}));
// Combine one value from each map.
//
// 1*1 + 2*2 + 3*3 + 4*<null>
EXPECT_EQ(14, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base::Time(),
/*priority_vector=*/
{{"foo", 1}, {"bar", 2}, {"baz", 3}, {"qux", 4}}));
}
TEST_F(InterestGroupPriorityUtilTest, BrowserSignalsAge) {
base::Time base_time = base::Time::Now();
storage_interest_group_.join_time = base_time;
EXPECT_EQ(0, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base_time,
/*priority_vector=*/{{"browserSignals.ageInMinutes", 2}}));
EXPECT_EQ(0,
CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base_time,
/*priority_vector=*/{{"browserSignals.ageInMinutesMax60", 2}}));
EXPECT_EQ(0,
CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base_time,
/*priority_vector=*/{{"browserSignals.ageInHoursMax24", 2}}));
EXPECT_EQ(0, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base_time,
/*priority_vector=*/{{"browserSignals.ageInDaysMax30", 2}}));
EXPECT_EQ(0, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, base_time,
/*priority_vector=*/{{"browserSignals.ageInDaysMax90", 2}}));
// Add 59 seconds to make sure minutes are not rounded up. Don't need to do
// this for hours or years because the 59 minutes case test hours aren't
// rounded up, and the 23 hours test makes sure days aren't rounded up.
base::Time fifty_nine_minutes_from_base =
base_time + base::Minutes(59) + base::Seconds(59);
EXPECT_EQ(118, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_,
fifty_nine_minutes_from_base,
/*priority_vector=*/{{"browserSignals.ageInMinutes", 2}}));
EXPECT_EQ(118,
CalculateInterestGroupPriority(
auction_config_, storage_interest_group_,
fifty_nine_minutes_from_base,
/*priority_vector=*/{{"browserSignals.ageInMinutesMax60", 2}}));
EXPECT_EQ(0,
CalculateInterestGroupPriority(
auction_config_, storage_interest_group_,
fifty_nine_minutes_from_base,
/*priority_vector=*/{{"browserSignals.ageInHoursMax24", 2}}));
EXPECT_EQ(0, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_,
fifty_nine_minutes_from_base,
/*priority_vector=*/{{"browserSignals.ageInDaysMax30", 2}}));
EXPECT_EQ(0, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_,
fifty_nine_minutes_from_base,
/*priority_vector=*/{{"browserSignals.ageInDaysMax90", 2}}));
base::Time twenty_three_hours_frome_base = base_time + base::Hours(23);
EXPECT_EQ(2 * 23 * 60,
CalculateInterestGroupPriority(
auction_config_, storage_interest_group_,
twenty_three_hours_frome_base,
/*priority_vector=*/{{"browserSignals.ageInMinutes", 2}}));
EXPECT_EQ(2 * 60,
CalculateInterestGroupPriority(
auction_config_, storage_interest_group_,
twenty_three_hours_frome_base,
/*priority_vector=*/{{"browserSignals.ageInMinutesMax60", 2}}));
EXPECT_EQ(2 * 23,
CalculateInterestGroupPriority(
auction_config_, storage_interest_group_,
twenty_three_hours_frome_base,
/*priority_vector=*/{{"browserSignals.ageInHoursMax24", 2}}));
EXPECT_EQ(0, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_,
twenty_three_hours_frome_base,
/*priority_vector=*/{{"browserSignals.ageInDaysMax30", 2}}));
EXPECT_EQ(0, CalculateInterestGroupPriority(
auction_config_, storage_interest_group_,
twenty_three_hours_frome_base,
/*priority_vector=*/{{"browserSignals.ageInDaysMax90", 2}}));
base::Time twenty_nine_days_frome_base = base_time + base::Days(29);
EXPECT_EQ(
2 * 29 * 24 * 60,
CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, twenty_nine_days_frome_base,
/*priority_vector=*/{{"browserSignals.ageInMinutes", 2}}));
EXPECT_EQ(
2 * 60,
CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, twenty_nine_days_frome_base,
/*priority_vector=*/{{"browserSignals.ageInMinutesMax60", 2}}));
EXPECT_EQ(
2 * 24,
CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, twenty_nine_days_frome_base,
/*priority_vector=*/{{"browserSignals.ageInHoursMax24", 2}}));
EXPECT_EQ(
2 * 29,
CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, twenty_nine_days_frome_base,
/*priority_vector=*/{{"browserSignals.ageInDaysMax30", 2}}));
EXPECT_EQ(
2 * 29,
CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, twenty_nine_days_frome_base,
/*priority_vector=*/{{"browserSignals.ageInDaysMax90", 2}}));
base::Time one_year_from_base = base_time + base::Days(365);
EXPECT_EQ(2 * blink::MaxInterestGroupLifetime().InDays() * 24 * 60,
CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, one_year_from_base,
/*priority_vector=*/{{"browserSignals.ageInMinutes", 2}}));
EXPECT_EQ(2 * 60,
CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, one_year_from_base,
/*priority_vector=*/{{"browserSignals.ageInMinutesMax60", 2}}));
EXPECT_EQ(2 * 24,
CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, one_year_from_base,
/*priority_vector=*/{{"browserSignals.ageInHoursMax24", 2}}));
EXPECT_EQ(2 * 30,
CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, one_year_from_base,
/*priority_vector=*/{{"browserSignals.ageInDaysMax30", 2}}));
EXPECT_EQ(2 * blink::MaxInterestGroupLifetime().InDays(),
CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, one_year_from_base,
/*priority_vector=*/{{"browserSignals.ageInDaysMax90", 2}}));
base::Time one_year_before_base = base_time - base::Days(365);
EXPECT_EQ(0,
CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, one_year_before_base,
/*priority_vector=*/{{"browserSignals.ageInMinutes", 2}}));
EXPECT_EQ(0,
CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, one_year_before_base,
/*priority_vector=*/{{"browserSignals.ageInMinutesMax60", 2}}));
EXPECT_EQ(0,
CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, one_year_before_base,
/*priority_vector=*/{{"browserSignals.ageInHoursMax24", 2}}));
EXPECT_EQ(0,
CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, one_year_before_base,
/*priority_vector=*/{{"browserSignals.ageInDaysMax30", 2}}));
EXPECT_EQ(0,
CalculateInterestGroupPriority(
auction_config_, storage_interest_group_, one_year_before_base,
/*priority_vector=*/{{"browserSignals.ageInDaysMax90", 2}}));
}
} // namespace content
|