File: simple_autofill_strike_database_unittest.cc

package info (click to toggle)
chromium 141.0.7390.107-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,246,132 kB
  • sloc: cpp: 35,264,965; ansic: 7,169,920; javascript: 4,250,185; python: 1,460,635; asm: 950,788; xml: 751,751; pascal: 187,972; sh: 89,459; perl: 88,691; objc: 79,953; sql: 53,924; cs: 44,622; fortran: 24,137; makefile: 22,313; tcl: 15,277; php: 14,018; yacc: 8,995; ruby: 7,553; awk: 3,720; lisp: 3,096; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (97 lines) | stat: -rw-r--r-- 3,691 bytes parent folder | download | duplicates (3)
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
// 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 "components/strike_database/simple_autofill_strike_database.h"

#include <memory>
#include <string>

#include "base/files/scoped_temp_dir.h"
#include "base/strings/to_string.h"
#include "base/test/task_environment.h"
#include "components/autofill/core/browser/proto/strike_data.pb.h"
#include "components/leveldb_proto/public/proto_database_provider.h"
#include "components/strike_database/strike_database_integrator_test_strike_database.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace autofill {

namespace {

struct TestStrikeDatabaseTraits {
  static constexpr std::string_view kName = "TestStrikeDatabase";
  static constexpr size_t kMaxStrikeEntities = 100;
  static constexpr size_t kMaxStrikeEntitiesAfterCleanup = 50;
  static constexpr size_t kMaxStrikeLimit = 3;
  static constexpr std::optional<base::TimeDelta> kExpiryTimeDelta =
      std::nullopt;
  static constexpr bool kUniqueIdRequired = true;
};

using TestStrikeDatabase =
    SimpleAutofillStrikeDatabase<TestStrikeDatabaseTraits>;

class SimpleAutofillStrikeDatabaseTest : public ::testing::Test {
 public:
  void SetUp() override {
    EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
    db_provider_ = std::make_unique<leveldb_proto::ProtoDatabaseProvider>(
        temp_dir_.GetPath());
    strike_database_service_ = std::make_unique<StrikeDatabase>(
        db_provider_.get(), temp_dir_.GetPath());
    strike_database_ =
        std::make_unique<TestStrikeDatabase>(strike_database_service_.get());
  }

  void TearDown() override {
    // The destruction of `strike_database_service_`'s components is posted
    // to a task runner. Wait for the task's completion.
    strike_database_.reset();
    strike_database_service_.reset();
    db_provider_.reset();
    task_environment_.RunUntilIdle();
  }

 protected:
  base::ScopedTempDir temp_dir_;
  base::test::TaskEnvironment task_environment_;
  std::unique_ptr<leveldb_proto::ProtoDatabaseProvider> db_provider_;
  std::unique_ptr<StrikeDatabase> strike_database_service_;
  std::unique_ptr<TestStrikeDatabase> strike_database_;
};

// Tests that strikes can be added and removed, and that depending on the
// `kMaxStrikeLimit`, the feature is considered blocked.
TEST_F(SimpleAutofillStrikeDatabaseTest, AddAndRemoveStrikes) {
  const std::string test_key = "123";
  strike_database_->AddStrike(test_key);
  EXPECT_EQ(strike_database_->GetStrikes(test_key), 1);
  EXPECT_FALSE(strike_database_->ShouldBlockFeature(test_key));

  strike_database_->AddStrikes(TestStrikeDatabaseTraits::kMaxStrikeLimit - 1,
                               test_key);
  EXPECT_EQ(strike_database_->GetStrikes(test_key),
            static_cast<int>(TestStrikeDatabaseTraits::kMaxStrikeLimit));
  EXPECT_TRUE(strike_database_->ShouldBlockFeature(test_key));

  strike_database_->RemoveStrike(test_key);
  EXPECT_EQ(strike_database_->GetStrikes(test_key),
            static_cast<int>(TestStrikeDatabaseTraits::kMaxStrikeLimit - 1));
  EXPECT_FALSE(strike_database_->ShouldBlockFeature(test_key));
}

// Tests that when too many strikes are added, the oldest strikes are cleared.
TEST_F(SimpleAutofillStrikeDatabaseTest, MaxEntries) {
  for (size_t i = 0; i < TestStrikeDatabaseTraits::kMaxStrikeEntities; i++) {
    strike_database_->AddStrike(base::ToString(i));
  }
  EXPECT_EQ(strike_database_->GetStrikes("0"), 1);
  strike_database_->AddStrike(
      base::ToString(TestStrikeDatabaseTraits::kMaxStrikeEntities));
  EXPECT_EQ(strike_database_->GetStrikes("0"), 0);
}

}  // namespace

}  // namespace autofill