File: epoch_topics_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (445 lines) | stat: -rw-r--r-- 17,051 bytes parent folder | download | duplicates (5)
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
// 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 "components/browsing_topics/epoch_topics.h"

#include "base/json/values_util.h"
#include "base/logging.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "components/browsing_topics/util.h"
#include "services/network/public/cpp/features.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"

namespace browsing_topics {

namespace {

constexpr base::Time kCalculationTime =
    base::Time::FromDeltaSinceWindowsEpoch(base::Days(1));
constexpr browsing_topics::HmacKey kTestKey = {1};
constexpr size_t kTaxonomySize = 349;
constexpr int kConfigVersion = 1;
constexpr int kTaxonomyVersion = 1;
constexpr int64_t kModelVersion = 2;
constexpr size_t kPaddedTopTopicsStartIndex = 2;

std::vector<TopicAndDomains> CreateTestTopTopics() {
  std::vector<TopicAndDomains> top_topics_and_observing_domains;
  top_topics_and_observing_domains.emplace_back(
      TopicAndDomains(Topic(1), {HashedDomain(1)}));
  top_topics_and_observing_domains.emplace_back(
      TopicAndDomains(Topic(2), {HashedDomain(1), HashedDomain(2)}));
  top_topics_and_observing_domains.emplace_back(
      TopicAndDomains(Topic(3), {HashedDomain(1), HashedDomain(3)}));
  top_topics_and_observing_domains.emplace_back(
      TopicAndDomains(Topic(4), {HashedDomain(2), HashedDomain(3)}));
  top_topics_and_observing_domains.emplace_back(
      TopicAndDomains(Topic(100), {HashedDomain(1)}));
  return top_topics_and_observing_domains;
}

EpochTopics CreateTestEpochTopics(
    base::Time calculation_time = kCalculationTime) {
  EpochTopics epoch_topics(CreateTestTopTopics(), kPaddedTopTopicsStartIndex,
                           kConfigVersion, kTaxonomyVersion, kModelVersion,
                           calculation_time,
                           /*from_manually_triggered_calculation=*/true);

  return epoch_topics;
}

}  // namespace

class EpochTopicsTest : public testing::Test {
 public:
  EpochTopicsTest()
      : task_environment_(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}

 protected:
  base::test::ScopedFeatureList feature_list_;
  base::test::TaskEnvironment task_environment_;
};

TEST_F(EpochTopicsTest, CandidateTopicForSite_InvalidIndividualTopics) {
  std::vector<TopicAndDomains> top_topics_and_observing_domains;
  for (int i = 0; i < 5; ++i) {
    top_topics_and_observing_domains.emplace_back(TopicAndDomains());
  }

  EpochTopics epoch_topics(std::move(top_topics_and_observing_domains),
                           kPaddedTopTopicsStartIndex, kConfigVersion,
                           kTaxonomyVersion, kModelVersion, kCalculationTime,
                           /*from_manually_triggered_calculation=*/false);
  EXPECT_FALSE(epoch_topics.empty());

  CandidateTopic candidate_topic = epoch_topics.CandidateTopicForSite(
      /*top_domain=*/"foo.com", /*hashed_context_domain=*/HashedDomain(2),
      kTestKey);
  EXPECT_FALSE(candidate_topic.IsValid());
}

TEST_F(EpochTopicsTest, CandidateTopicForSite) {
  EpochTopics epoch_topics = CreateTestEpochTopics();

  EXPECT_FALSE(epoch_topics.empty());
  EXPECT_EQ(epoch_topics.config_version(), kConfigVersion);
  EXPECT_EQ(epoch_topics.taxonomy_version(), kTaxonomyVersion);
  EXPECT_EQ(epoch_topics.model_version(), kModelVersion);
  EXPECT_EQ(epoch_topics.calculation_time(), kCalculationTime);

  {
    std::string top_site = "foo.com";
    uint64_t random_or_top_topic_decision_hash =
        HashTopDomainForRandomOrTopTopicDecision(kTestKey, kCalculationTime,
                                                 top_site);

    // `random_or_top_topic_decision_hash` mod 100 is not less than 5. Thus one
    // of the top 5 topics will be the candidate topic.
    ASSERT_GE(random_or_top_topic_decision_hash % 100, 5ULL);

    uint64_t top_topics_index_decision_hash =
        HashTopDomainForTopTopicIndexDecision(kTestKey, kCalculationTime,
                                              top_site);

    // The topic index is 1, thus the candidate topic is Topic(2). Only the
    // context with HashedDomain(1) or HashedDomain(2) is allowed to see it.
    ASSERT_EQ(top_topics_index_decision_hash % 5, 1ULL);
    {
      CandidateTopic candidate_topic = epoch_topics.CandidateTopicForSite(
          top_site, HashedDomain(1), kTestKey);

      EXPECT_EQ(candidate_topic.topic(), Topic(2));
      EXPECT_TRUE(candidate_topic.is_true_topic());
      EXPECT_FALSE(candidate_topic.should_be_filtered());
    }

    {
      CandidateTopic candidate_topic = epoch_topics.CandidateTopicForSite(
          top_site, HashedDomain(2), kTestKey);

      EXPECT_EQ(candidate_topic.topic(), Topic(2));
      EXPECT_TRUE(candidate_topic.is_true_topic());
      EXPECT_FALSE(candidate_topic.should_be_filtered());
    }

    {
      CandidateTopic candidate_topic = epoch_topics.CandidateTopicForSite(
          top_site, HashedDomain(3), kTestKey);

      EXPECT_EQ(candidate_topic.topic(), Topic(2));
      EXPECT_TRUE(candidate_topic.is_true_topic());
      EXPECT_TRUE(candidate_topic.should_be_filtered());
    }
  }

  {
    std::string top_site = "foo1.com";
    uint64_t random_or_top_topic_decision_hash =
        HashTopDomainForRandomOrTopTopicDecision(kTestKey, kCalculationTime,
                                                 top_site);

    // `random_or_top_topic_decision_hash` mod 100 is not less than 5. Thus one
    // of the top 5 topics will be the candidate topic.
    ASSERT_GE(random_or_top_topic_decision_hash % 100, 5ULL);

    uint64_t top_topics_index_decision_hash =
        HashTopDomainForTopTopicIndexDecision(kTestKey, kCalculationTime,
                                              top_site);

    // The topic index is 2, thus the candidate topic is Topic(3). Only the
    // context with HashedDomain(1) or HashedDomain(3) is allowed to see it.
    ASSERT_EQ(top_topics_index_decision_hash % 5, 2ULL);
    {
      CandidateTopic candidate_topic = epoch_topics.CandidateTopicForSite(
          top_site, HashedDomain(1), kTestKey);

      EXPECT_EQ(candidate_topic.topic(), Topic(3));
      EXPECT_FALSE(candidate_topic.is_true_topic());
      EXPECT_FALSE(candidate_topic.should_be_filtered());
    }

    {
      CandidateTopic candidate_topic = epoch_topics.CandidateTopicForSite(
          top_site, HashedDomain(2), kTestKey);

      EXPECT_EQ(candidate_topic.topic(), Topic(3));
      EXPECT_FALSE(candidate_topic.is_true_topic());
      EXPECT_TRUE(candidate_topic.should_be_filtered());
    }

    {
      CandidateTopic candidate_topic = epoch_topics.CandidateTopicForSite(
          top_site, HashedDomain(3), kTestKey);

      EXPECT_EQ(candidate_topic.topic(), Topic(3));
      EXPECT_FALSE(candidate_topic.is_true_topic());
      EXPECT_FALSE(candidate_topic.should_be_filtered());
    }
  }

  {
    std::string top_site = "foo5.com";
    uint64_t random_or_top_topic_decision_hash =
        HashTopDomainForRandomOrTopTopicDecision(kTestKey, kCalculationTime,
                                                 top_site);

    // `random_or_top_topic_decision_hash` mod 100 is less than 5. Thus the
    // random topic will be returned.
    ASSERT_LT(random_or_top_topic_decision_hash % 100, 5ULL);

    uint64_t random_topic_index_decision =
        HashTopDomainForRandomTopicIndexDecision(kTestKey, kCalculationTime,
                                                 top_site);

    // The real topic would have been 4, but a random topic (186) is returned
    // instead. Only callers that are able to receive 4 (domains 2 and 3) should
    // receive the random topic.
    ASSERT_EQ(random_topic_index_decision % kTaxonomySize, 185ULL);

    {
      CandidateTopic candidate_topic = epoch_topics.CandidateTopicForSite(
          top_site, HashedDomain(1), kTestKey);

      EXPECT_EQ(candidate_topic.topic(), Topic(186));
      EXPECT_FALSE(candidate_topic.is_true_topic());
      EXPECT_TRUE(candidate_topic.should_be_filtered());
    }

    {
      CandidateTopic candidate_topic = epoch_topics.CandidateTopicForSite(
          top_site, HashedDomain(2), kTestKey);

      EXPECT_EQ(candidate_topic.topic(), Topic(186));
      EXPECT_FALSE(candidate_topic.is_true_topic());
      EXPECT_FALSE(candidate_topic.should_be_filtered());
    }

    {
      CandidateTopic candidate_topic = epoch_topics.CandidateTopicForSite(
          top_site, HashedDomain(3), kTestKey);

      EXPECT_EQ(candidate_topic.topic(), Topic(186));
      EXPECT_FALSE(candidate_topic.is_true_topic());
      EXPECT_FALSE(candidate_topic.should_be_filtered());
    }
  }
}

TEST_F(EpochTopicsTest, ClearTopics) {
  EpochTopics epoch_topics = CreateTestEpochTopics();

  EXPECT_FALSE(epoch_topics.empty());

  epoch_topics.ClearTopics();

  EXPECT_TRUE(epoch_topics.empty());

  CandidateTopic candidate_topic = epoch_topics.CandidateTopicForSite(
      /*top_domain=*/"foo.com", HashedDomain(1), kTestKey);

  EXPECT_FALSE(candidate_topic.IsValid());
}

TEST_F(EpochTopicsTest, ClearTopic_NoDescendants) {
  EpochTopics epoch_topics = CreateTestEpochTopics();

  EXPECT_FALSE(epoch_topics.empty());

  epoch_topics.ClearTopic(Topic(3));

  EXPECT_FALSE(epoch_topics.empty());

  EXPECT_TRUE(epoch_topics.top_topics_and_observing_domains()[0].IsValid());
  EXPECT_TRUE(epoch_topics.top_topics_and_observing_domains()[1].IsValid());
  EXPECT_FALSE(epoch_topics.top_topics_and_observing_domains()[2].IsValid());
  EXPECT_TRUE(epoch_topics.top_topics_and_observing_domains()[3].IsValid());
  EXPECT_TRUE(epoch_topics.top_topics_and_observing_domains()[4].IsValid());
}

TEST_F(EpochTopicsTest, ClearTopic_WithDescendants) {
  EpochTopics epoch_topics = CreateTestEpochTopics();

  EXPECT_FALSE(epoch_topics.empty());

  epoch_topics.ClearTopic(Topic(1));

  EXPECT_FALSE(epoch_topics.empty());

  EXPECT_FALSE(epoch_topics.top_topics_and_observing_domains()[0].IsValid());
  EXPECT_FALSE(epoch_topics.top_topics_and_observing_domains()[1].IsValid());
  EXPECT_FALSE(epoch_topics.top_topics_and_observing_domains()[2].IsValid());
  EXPECT_FALSE(epoch_topics.top_topics_and_observing_domains()[3].IsValid());
  EXPECT_TRUE(epoch_topics.top_topics_and_observing_domains()[4].IsValid());
}

TEST_F(EpochTopicsTest, ClearContextDomain) {
  EpochTopics epoch_topics = CreateTestEpochTopics();

  EXPECT_FALSE(epoch_topics.empty());

  epoch_topics.ClearContextDomain(HashedDomain(1));

  EXPECT_FALSE(epoch_topics.empty());

  EXPECT_EQ(epoch_topics.top_topics_and_observing_domains()[0].hashed_domains(),
            std::set<HashedDomain>{});
  EXPECT_EQ(epoch_topics.top_topics_and_observing_domains()[1].hashed_domains(),
            std::set<HashedDomain>({HashedDomain(2)}));
  EXPECT_EQ(epoch_topics.top_topics_and_observing_domains()[2].hashed_domains(),
            std::set<HashedDomain>({HashedDomain(3)}));
  EXPECT_EQ(epoch_topics.top_topics_and_observing_domains()[3].hashed_domains(),
            std::set<HashedDomain>({HashedDomain(2), HashedDomain(3)}));
  EXPECT_EQ(epoch_topics.top_topics_and_observing_domains()[4].hashed_domains(),
            std::set<HashedDomain>{});
}

TEST_F(EpochTopicsTest, FromEmptyDictionaryValue) {
  EpochTopics read_epoch_topics =
      EpochTopics::FromDictValue(base::Value::Dict());

  EXPECT_TRUE(read_epoch_topics.empty());
  EXPECT_EQ(read_epoch_topics.config_version(), 0);
  EXPECT_EQ(read_epoch_topics.taxonomy_version(), 0);
  EXPECT_EQ(read_epoch_topics.model_version(), 0);
  EXPECT_EQ(read_epoch_topics.calculation_time(), base::Time());

  CandidateTopic candidate_topic = read_epoch_topics.CandidateTopicForSite(
      /*top_domain=*/"foo.com", HashedDomain(1), kTestKey);

  EXPECT_FALSE(candidate_topic.IsValid());
}

TEST_F(EpochTopicsTest, FromDictValueInvalidCalculationTime) {
  EpochTopics epoch_topics(kCalculationTime);

  base::Value::Dict dict_value = epoch_topics.ToDictValue();
  dict_value.Set("calculation_time", "nonsense");
  EpochTopics read_epoch_topics = EpochTopics::FromDictValue(dict_value);
  EXPECT_EQ(read_epoch_topics.calculation_time(), base::Time());
}

TEST_F(EpochTopicsTest,
       FromDictionaryValueWithoutConfigVersion_UseConfigVersion1) {
  base::Value::Dict dict;

  base::Value::List top_topics_and_observing_domains_list;
  std::vector<TopicAndDomains> top_topics_and_domains = CreateTestTopTopics();
  for (const TopicAndDomains& topic_and_domains : top_topics_and_domains) {
    top_topics_and_observing_domains_list.Append(
        topic_and_domains.ToDictValue());
  }

  dict.Set("top_topics_and_observing_domains",
           std::move(top_topics_and_observing_domains_list));
  dict.Set("padded_top_topics_start_index", 0);
  dict.Set("taxonomy_version", 2);
  dict.Set("model_version", base::Int64ToValue(3));
  dict.Set("calculation_time", base::TimeToValue(kCalculationTime));

  EpochTopics read_epoch_topics = EpochTopics::FromDictValue(std::move(dict));

  EXPECT_FALSE(read_epoch_topics.empty());
  EXPECT_EQ(read_epoch_topics.config_version(), 1);
  EXPECT_EQ(read_epoch_topics.taxonomy_version(), 2);
  EXPECT_EQ(read_epoch_topics.model_version(), 3);
  EXPECT_EQ(read_epoch_topics.calculation_time(), kCalculationTime);
}

TEST_F(EpochTopicsTest, EmptyEpochTopics_ToAndFromDictValue) {
  EpochTopics epoch_topics(kCalculationTime);

  base::Value::Dict dict_value = epoch_topics.ToDictValue();
  EpochTopics read_epoch_topics = EpochTopics::FromDictValue(dict_value);

  EXPECT_TRUE(read_epoch_topics.empty());
  EXPECT_EQ(read_epoch_topics.config_version(), 0);
  EXPECT_EQ(read_epoch_topics.taxonomy_version(), 0);
  EXPECT_EQ(read_epoch_topics.model_version(), 0);
  EXPECT_EQ(read_epoch_topics.calculation_time(), kCalculationTime);
  EXPECT_FALSE(read_epoch_topics.calculator_result_status());

  CandidateTopic candidate_topic = epoch_topics.CandidateTopicForSite(
      /*top_domain=*/"foo.com", HashedDomain(1), kTestKey);

  EXPECT_FALSE(candidate_topic.IsValid());
}

TEST_F(EpochTopicsTest, PopulatedEpochTopics_ToAndFromValue) {
  EpochTopics epoch_topics = CreateTestEpochTopics();

  base::Value::Dict dict_value = epoch_topics.ToDictValue();
  EpochTopics read_epoch_topics = EpochTopics::FromDictValue(dict_value);

  EXPECT_FALSE(read_epoch_topics.empty());
  EXPECT_EQ(read_epoch_topics.config_version(), 1);
  EXPECT_EQ(read_epoch_topics.taxonomy_version(), 1);
  EXPECT_EQ(read_epoch_topics.model_version(), 2);
  EXPECT_EQ(read_epoch_topics.calculation_time(), kCalculationTime);

  // `from_manually_triggered_calculation` should not persist after being
  // written.
  EXPECT_TRUE(epoch_topics.from_manually_triggered_calculation());
  EXPECT_FALSE(read_epoch_topics.from_manually_triggered_calculation());

  // The kSuccess `calculator_result_status` should persist after being written.
  EXPECT_EQ(epoch_topics.calculator_result_status(),
            CalculatorResultStatus::kSuccess);
  EXPECT_EQ(read_epoch_topics.calculator_result_status(),
            CalculatorResultStatus::kSuccess);

  CandidateTopic candidate_topic = epoch_topics.CandidateTopicForSite(
      /*top_domain=*/"foo.com", HashedDomain(1), kTestKey);

  EXPECT_EQ(candidate_topic.topic(), Topic(2));
}

TEST_F(EpochTopicsTest,
       EmptyEpochTopicsWithCalculatorResultStatus_ToAndFromDictValue) {
  EpochTopics epoch_topics(
      kCalculationTime,
      CalculatorResultStatus::kFailureAnnotationExecutionError);

  base::Value::Dict dict_value = epoch_topics.ToDictValue();
  EpochTopics read_epoch_topics = EpochTopics::FromDictValue(dict_value);

  EXPECT_TRUE(read_epoch_topics.empty());

  // The failure `calculator_result_status` should persist after being written.
  EXPECT_EQ(epoch_topics.calculator_result_status(),
            CalculatorResultStatus::kFailureAnnotationExecutionError);
  EXPECT_FALSE(read_epoch_topics.calculator_result_status());
}

TEST_F(EpochTopicsTest, ScheduleExpiration) {
  feature_list_.InitWithFeaturesAndParameters(
      /*enabled_features=*/
      {{network::features::kBrowsingTopics, {}},
       {blink::features::kBrowsingTopicsParameters,
        {{"epoch_retention_duration", "28s"}}}},
      /*disabled_features=*/{});

  base::Time start_time = base::Time::Now();
  EpochTopics epoch_topics = CreateTestEpochTopics(start_time);

  bool expiration_callback_invoked = false;
  base::OnceClosure expiration_callback =
      base::BindLambdaForTesting([&]() { expiration_callback_invoked = true; });

  // Schedule expiration 1 second after the calculation time.
  task_environment_.FastForwardBy(base::Seconds(1));
  epoch_topics.ScheduleExpiration(std::move(expiration_callback));

  // Verify the callback isn't invoked prematurely.
  task_environment_.FastForwardBy(base::Seconds(26));
  EXPECT_FALSE(expiration_callback_invoked);

  // Verify the callback is invoked at the expected expiration time.
  task_environment_.FastForwardBy(base::Seconds(1));
  EXPECT_TRUE(expiration_callback_invoked);
}

}  // namespace browsing_topics