File: os_registrations_table_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 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 (238 lines) | stat: -rw-r--r-- 8,533 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
// Copyright 2025 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/attribution_reporting/os_registrations_table.h"

#include <set>
#include <string>
#include <vector>

#include "base/containers/flat_set.h"
#include "base/functional/callback_helpers.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "content/browser/attribution_reporting/test/configurable_storage_delegate.h"
#include "content/public/browser/attribution_data_model.h"
#include "sql/database.h"
#include "sql/statement.h"
#include "sql/statement_id.h"
#include "sql/test/test_helpers.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "url/gurl.h"
#include "url/origin.h"

namespace content {
namespace {

using ::testing::IsEmpty;
using ::testing::SizeIs;
using ::testing::UnorderedElementsAre;

struct OsRegistrationData {
  std::string registration_origin;
  base::Time time;

  friend bool operator==(const OsRegistrationData&,
                         const OsRegistrationData&) = default;
};

class OsRegistrationsTableTest : public testing::Test {
 public:
  void SetUp() override {
    ASSERT_TRUE(db_.OpenInMemory());
    ASSERT_TRUE(table_.CreateTable(&db_));

    // Prevent any rows from being deleted during the test by default.
    delegate_.set_delete_expired_os_registrations_frequency(
        base::TimeDelta::Max());
  }

 protected:
  std::vector<OsRegistrationData> GetOsRegistrationRows() {
    std::vector<OsRegistrationData> rows;

    static constexpr char kSelectSql[] =
        "SELECT registration_origin,time "
        "FROM os_registrations";
    sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, kSelectSql));

    while (statement.Step()) {
      rows.emplace_back(/*registration_origin=*/statement.ColumnString(0),
                        /*time=*/statement.ColumnTime(1));
    }

    EXPECT_TRUE(statement.Succeeded());
    return rows;
  }

  base::test::SingleThreadTaskEnvironment task_environment_{
      base::test::TaskEnvironment::TimeSource::MOCK_TIME};

  sql::Database db_{sql::test::kTestTag};
  ConfigurableStorageDelegate delegate_;
  OsRegistrationsTable table_{&delegate_};
};

TEST_F(OsRegistrationsTableTest, AddOsRegistrations) {
  base::Time now = base::Time::Now();

  table_.AddOsRegistrations(&db_,
                            {url::Origin::Create(GURL("https://a.test")),
                             url::Origin::Create(GURL("https://b.test"))});

  EXPECT_THAT(
      GetOsRegistrationRows(),
      UnorderedElementsAre(
          OsRegistrationData(
              /*registration_origin=*/"https://a.test", now),
          OsRegistrationData(/*registration_origin=*/"https://b.test", now)));
}

TEST_F(OsRegistrationsTableTest, ClearAllDataAllTime) {
  table_.AddOsRegistrations(&db_,
                            {url::Origin::Create(GURL("https://a.test"))});

  task_environment_.FastForwardBy(base::Days(1));

  table_.AddOsRegistrations(&db_,
                            {url::Origin::Create(GURL("https://b.test"))});

  ASSERT_THAT(GetOsRegistrationRows(), SizeIs(2));

  table_.ClearAllDataAllTime(&db_);
  EXPECT_THAT(GetOsRegistrationRows(), IsEmpty());
}

TEST_F(OsRegistrationsTableTest, ClearDataForOriginsInRange) {
  base::Time now = base::Time::Now();

  table_.AddOsRegistrations(&db_,
                            {url::Origin::Create(GURL("https://a.test")),
                             url::Origin::Create(GURL("https://b.test"))});

  task_environment_.FastForwardBy(base::Days(1));

  table_.AddOsRegistrations(&db_,
                            {url::Origin::Create(GURL("https://a.test")),
                             url::Origin::Create(GURL("https://b.test"))});

  ASSERT_THAT(GetOsRegistrationRows(), SizeIs(4));

  table_.ClearDataForOriginsInRange(
      &db_, /*delete_begin=*/now + base::Hours(1),
      /*delete_end=*/base::Time::Max(),
      /*filter=*/base::BindRepeating([](const blink::StorageKey& storage_key) {
        return storage_key ==
               blink::StorageKey::CreateFromStringForTesting("https://a.test");
      }));

  ASSERT_THAT(
      GetOsRegistrationRows(),
      UnorderedElementsAre(
          OsRegistrationData(/*registration_origin=*/"https://a.test", now),
          OsRegistrationData(/*registration_origin=*/"https://b.test", now),
          OsRegistrationData(/*registration_origin=*/"https://b.test",
                             now + base::Days(1))));

  table_.ClearDataForOriginsInRange(&db_, /*delete_begin=*/now - base::Hours(1),
                                    /*delete_end=*/now,
                                    /*filter=*/base::NullCallback());

  EXPECT_THAT(
      GetOsRegistrationRows(),
      UnorderedElementsAre(OsRegistrationData(
          /*registration_origin=*/"https://b.test", now + base::Days(1))));
}

TEST_F(OsRegistrationsTableTest, DeleteExpiredRows) {
  delegate_.set_delete_expired_os_registrations_frequency(base::Hours(12));

  base::Time now = base::Time::Now();

  table_.AddOsRegistrations(&db_,
                            {url::Origin::Create(GURL("https://a.test"))});

  task_environment_.FastForwardBy(base::Hours(12));

  // No record has expired yet.
  table_.AddOsRegistrations(&db_,
                            {url::Origin::Create(GURL("https://b.test"))});

  ASSERT_THAT(GetOsRegistrationRows(), SizeIs(2));

  task_environment_.FastForwardBy(base::Days(45) - base::Hours(12));

  // This should delete the first record.
  table_.AddOsRegistrations(&db_,
                            {url::Origin::Create(GURL("https://c.test"))});

  EXPECT_THAT(GetOsRegistrationRows(),
              UnorderedElementsAre(
                  OsRegistrationData(/*registration_origin=*/"https://b.test",
                                     now + base::Hours(12)),
                  OsRegistrationData(/*registration_origin=*/"https://c.test",
                                     now + base::Days(45))));
}

TEST_F(OsRegistrationsTableTest, ClearDataForRegistrationOrigin) {
  base::Time now = base::Time::Now();

  auto origin_a = url::Origin::Create(GURL("https://a.test"));
  auto origin_b = url::Origin::Create(GURL("https://b.test"));

  table_.AddOsRegistrations(&db_,
                            {url::Origin::Create(GURL("https://a.test")),
                             url::Origin::Create(GURL("https://b.test"))});

  task_environment_.FastForwardBy(base::Days(1));

  table_.AddOsRegistrations(&db_,
                            {url::Origin::Create(GURL("https://a.test")),
                             url::Origin::Create(GURL("https://b.test"))});

  ASSERT_THAT(GetOsRegistrationRows(), SizeIs(4));

  table_.ClearDataForRegistrationOrigin(
      &db_, /*delete_begin=*/now + base::Hours(1),
      /*delete_end=*/base::Time::Max(),
      url::Origin::Create(GURL("https://a.test")));

  ASSERT_THAT(
      GetOsRegistrationRows(),
      UnorderedElementsAre(
          OsRegistrationData(/*registration_origin=*/"https://a.test", now),
          OsRegistrationData(/*registration_origin=*/"https://b.test", now),
          OsRegistrationData(/*registration_origin=*/"https://b.test",
                             now + base::Days(1))));

  table_.ClearDataForRegistrationOrigin(
      &db_, /*delete_begin=*/now - base::Hours(1),
      /*delete_end=*/now, url::Origin::Create(GURL("https://b.test")));
  ASSERT_THAT(
      GetOsRegistrationRows(),
      UnorderedElementsAre(
          OsRegistrationData(/*registration_origin=*/"https://a.test", now),
          OsRegistrationData(/*registration_origin=*/"https://b.test",
                             now + base::Days(1))));
}

TEST_F(OsRegistrationsTableTest, GetOsRegistrationDataKeys) {
  url::Origin origin_1 = url::Origin::Create(GURL("https://a.r.test"));
  url::Origin origin_2 = url::Origin::Create(GURL("https://b.r.test"));
  url::Origin origin_3 = url::Origin::Create(GURL("https://c.test"));
  table_.AddOsRegistrations(&db_, {origin_1, origin_2, origin_3});

  std::set<AttributionDataModel::DataKey> keys;
  table_.AppendOsRegistrationDataKeys(&db_, keys);

  EXPECT_THAT(keys,
              UnorderedElementsAre(AttributionDataModel::DataKey(origin_1),
                                   AttributionDataModel::DataKey(origin_2),
                                   AttributionDataModel::DataKey(origin_3)));
}

}  // namespace
}  // namespace content