File: k_anonymity_service_storage_unittest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (365 lines) | stat: -rw-r--r-- 12,698 bytes parent folder | download | duplicates (4)
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
// 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 "chrome/browser/k_anonymity_service/k_anonymity_service_storage.h"

#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace {

const char kTestOrigin[] = "https://foo.test";
const char kTestOhttpKey[] = "FooOhttpKey";
const char kTestKeyCommitment[] = "FooKeyCommitment";

const char kTestOrigin2[] = "https://bar.test";
const char kTestOhttpKey2[] = "BarOhttpKey";

const char kTestOrigin3[] = "https://baz.test";
const char kTestOhttpKey3[] = "BazOhttpKey";

class KAnonymityServiceStorageTest : public testing::TestWithParam<bool> {
 public:
  base::test::TaskEnvironment& task_environment() { return task_environment_; }

  void SetUp() override { ASSERT_TRUE(temp_directory_.CreateUniqueTempDir()); }

  base::FilePath db_path() {
    return temp_directory_.GetPath().Append(
        FILE_PATH_LITERAL("KAnonymityService"));
  }

  std::unique_ptr<KAnonymityServiceStorage> CreateStorage() {
    std::unique_ptr<KAnonymityServiceStorage> storage;
    if (StorageIsPersistent()) {
      storage = CreateKAnonymitySqlStorageForPath(db_path());
    } else {
      storage = std::make_unique<KAnonymityServiceMemoryStorage>();
    }

    base::RunLoop run_loop;
    storage->WaitUntilReady(base::BindLambdaForTesting(
        [&run_loop](KAnonymityServiceStorage::InitStatus status) {
          EXPECT_EQ(status, KAnonymityServiceStorage::InitStatus::kInitOk);
          run_loop.Quit();
        }));
    run_loop.Run();

    return storage;
  }

  bool StorageIsPersistent() { return GetParam(); }

 private:
  base::test::TaskEnvironment task_environment_;
  base::ScopedTempDir temp_directory_;
};

TEST_P(KAnonymityServiceStorageTest, InitializeDatabase) {
  std::unique_ptr<KAnonymityServiceStorage> storage = CreateStorage();
}

TEST_P(KAnonymityServiceStorageTest, MultipleWaitUntilReady) {
  std::unique_ptr<KAnonymityServiceStorage> storage;
  if (StorageIsPersistent()) {
    storage = CreateKAnonymitySqlStorageForPath(db_path());
  } else {
    storage = std::make_unique<KAnonymityServiceMemoryStorage>();
  }
  int seq_num = 0;
  base::RunLoop run_loop;
  storage->WaitUntilReady(base::BindLambdaForTesting(
      [&](KAnonymityServiceStorage::InitStatus status) {
        EXPECT_EQ(status, KAnonymityServiceStorage::InitStatus::kInitOk);
        EXPECT_EQ(1, ++seq_num);
      }));
  storage->WaitUntilReady(base::BindLambdaForTesting(
      [&](KAnonymityServiceStorage::InitStatus status) {
        EXPECT_EQ(status, KAnonymityServiceStorage::InitStatus::kInitOk);
        EXPECT_EQ(2, ++seq_num);
        run_loop.Quit();
      }));
  run_loop.Run();
}

TEST_P(KAnonymityServiceStorageTest, SaveAndLoadOHTTPKeys) {
  url::Origin test_origin = url::Origin::Create(GURL(kTestOrigin));
  std::string test_ohttp_key(kTestOhttpKey);
  base::Time expiration = base::Time::Now();

  url::Origin test_origin2 = url::Origin::Create(GURL(kTestOrigin2));
  std::string test_ohttp_key2(kTestOhttpKey2);
  base::Time expiration2 = expiration + base::Seconds(1);

  {
    std::unique_ptr<KAnonymityServiceStorage> storage = CreateStorage();

    EXPECT_FALSE(storage->GetOHTTPKeyFor(test_origin));
    EXPECT_FALSE(storage->GetOHTTPKeyFor(test_origin2));

    storage->UpdateOHTTPKeyFor(test_origin, {test_ohttp_key, expiration});
    storage->UpdateOHTTPKeyFor(test_origin2, {test_ohttp_key2, expiration2});

    std::optional<OHTTPKeyAndExpiration> result;
    result = storage->GetOHTTPKeyFor(test_origin);
    ASSERT_TRUE(result);
    EXPECT_EQ(test_ohttp_key, result->key);
    EXPECT_EQ(expiration, result->expiration);

    result = storage->GetOHTTPKeyFor(test_origin2);
    ASSERT_TRUE(result);
    EXPECT_EQ(test_ohttp_key2, result->key);
    EXPECT_EQ(expiration2, result->expiration);
  }

  task_environment().RunUntilIdle();

  {
    std::unique_ptr<KAnonymityServiceStorage> storage = CreateStorage();
    std::optional<OHTTPKeyAndExpiration> result;

    if (StorageIsPersistent()) {
      // Should be persisted after the storage is closed and re-opened.

      result = storage->GetOHTTPKeyFor(test_origin);
      ASSERT_TRUE(result);
      EXPECT_EQ(test_ohttp_key, result->key);
      EXPECT_EQ(expiration, result->expiration);

      result = storage->GetOHTTPKeyFor(test_origin2);
      ASSERT_TRUE(result);
      EXPECT_EQ(test_ohttp_key2, result->key);
      EXPECT_EQ(expiration2, result->expiration);
    } else {
      // Storage should have been cleared.
      EXPECT_FALSE(storage->GetOHTTPKeyFor(test_origin));
      EXPECT_FALSE(storage->GetOHTTPKeyFor(test_origin2));

      // Set them to the expected values.
      storage->UpdateOHTTPKeyFor(test_origin, {test_ohttp_key, expiration});
      storage->UpdateOHTTPKeyFor(test_origin2, {test_ohttp_key2, expiration2});
    }

    // Modify an existing key.
    expiration += base::Seconds(4);
    storage->UpdateOHTTPKeyFor(test_origin, {test_ohttp_key, expiration});

    result = storage->GetOHTTPKeyFor(test_origin);
    ASSERT_TRUE(result);
    EXPECT_EQ(test_ohttp_key, result->key);
    EXPECT_EQ(expiration, result->expiration);
  }

  task_environment().RunUntilIdle();

  {
    std::unique_ptr<KAnonymityServiceStorage> storage = CreateStorage();
    std::optional<OHTTPKeyAndExpiration> result;

    if (StorageIsPersistent()) {
      // Modifications should be persisted after the storage is closed and
      // re-opened.

      result = storage->GetOHTTPKeyFor(test_origin);
      ASSERT_TRUE(result);
      EXPECT_EQ(test_ohttp_key, result->key);
      EXPECT_EQ(expiration, result->expiration);

      result = storage->GetOHTTPKeyFor(test_origin2);
      ASSERT_TRUE(result);
      EXPECT_EQ(test_ohttp_key2, result->key);
      EXPECT_EQ(expiration2, result->expiration);
    } else {
      // Storage should have been cleared.
      EXPECT_FALSE(storage->GetOHTTPKeyFor(test_origin));
      EXPECT_FALSE(storage->GetOHTTPKeyFor(test_origin2));
    }
  }
}

TEST_P(KAnonymityServiceStorageTest, SaveAndLoadTooManyOHTTPKeys) {
  url::Origin test_origin = url::Origin::Create(GURL(kTestOrigin));
  std::string test_ohttp_key(kTestOhttpKey);
  base::Time expiration = base::Time::Now();

  url::Origin test_origin2 = url::Origin::Create(GURL(kTestOrigin2));
  std::string test_ohttp_key2(kTestOhttpKey2);
  base::Time expiration2 = expiration + base::Seconds(1);

  url::Origin test_origin3 = url::Origin::Create(GURL(kTestOrigin3));
  std::string test_ohttp_key3(kTestOhttpKey3);
  base::Time expiration3 = expiration2 + base::Seconds(2);

  {
    std::unique_ptr<KAnonymityServiceStorage> storage = CreateStorage();

    EXPECT_FALSE(storage->GetOHTTPKeyFor(test_origin));
    EXPECT_FALSE(storage->GetOHTTPKeyFor(test_origin2));
    EXPECT_FALSE(storage->GetOHTTPKeyFor(test_origin3));

    storage->UpdateOHTTPKeyFor(test_origin, {test_ohttp_key, expiration});
    storage->UpdateOHTTPKeyFor(test_origin2, {test_ohttp_key2, expiration2});
    storage->UpdateOHTTPKeyFor(test_origin3, {test_ohttp_key3, expiration3});

    std::optional<OHTTPKeyAndExpiration> result;
    result = storage->GetOHTTPKeyFor(test_origin);
    if (StorageIsPersistent()) {
      // The oldest should be forgotten.
      result = storage->GetOHTTPKeyFor(test_origin);
      ASSERT_FALSE(result);
    } else {
      ASSERT_TRUE(result);
      EXPECT_EQ(test_ohttp_key, result->key);
      EXPECT_EQ(expiration, result->expiration);
    }

    result = storage->GetOHTTPKeyFor(test_origin2);
    ASSERT_TRUE(result);
    EXPECT_EQ(test_ohttp_key2, result->key);
    EXPECT_EQ(expiration2, result->expiration);

    result = storage->GetOHTTPKeyFor(test_origin3);
    ASSERT_TRUE(result);
    EXPECT_EQ(test_ohttp_key3, result->key);
    EXPECT_EQ(expiration3, result->expiration);
  }

  task_environment().RunUntilIdle();

  {
    std::unique_ptr<KAnonymityServiceStorage> storage = CreateStorage();
    std::optional<OHTTPKeyAndExpiration> result;

    if (StorageIsPersistent()) {
      // Modifications should be persisted after the storage is closed and
      // re-opened.

      // The oldest should be forgotten.
      result = storage->GetOHTTPKeyFor(test_origin);
      ASSERT_FALSE(result);

      result = storage->GetOHTTPKeyFor(test_origin2);
      ASSERT_TRUE(result);
      EXPECT_EQ(test_ohttp_key2, result->key);
      EXPECT_EQ(expiration2, result->expiration);

      result = storage->GetOHTTPKeyFor(test_origin3);
      ASSERT_TRUE(result);
      EXPECT_EQ(test_ohttp_key3, result->key);
      EXPECT_EQ(expiration3, result->expiration);
    } else {
      // Storage should have been cleared.
      EXPECT_FALSE(storage->GetOHTTPKeyFor(test_origin));
      EXPECT_FALSE(storage->GetOHTTPKeyFor(test_origin2));
      EXPECT_FALSE(storage->GetOHTTPKeyFor(test_origin3));
    }
  }
}

TEST_P(KAnonymityServiceStorageTest, SaveAndLoadKeyCommitment) {
  std::string test_key_commitment(kTestKeyCommitment);
  int non_unique_user_id = 1;
  base::Time expiration = base::Time::Now();

  {
    std::unique_ptr<KAnonymityServiceStorage> storage = CreateStorage();

    EXPECT_FALSE(storage->GetKeyAndNonUniqueUserId());

    storage->UpdateKeyAndNonUniqueUserId(
        {{test_key_commitment, non_unique_user_id}, expiration});

    std::optional<KeyAndNonUniqueUserIdWithExpiration> result;
    result = storage->GetKeyAndNonUniqueUserId();
    ASSERT_TRUE(result);
    EXPECT_EQ(test_key_commitment, result->key_and_id.key_commitment);
    EXPECT_EQ(non_unique_user_id, result->key_and_id.non_unique_user_id);
    EXPECT_EQ(expiration, result->expiration);
  }

  task_environment().RunUntilIdle();

  {
    std::unique_ptr<KAnonymityServiceStorage> storage = CreateStorage();
    std::optional<KeyAndNonUniqueUserIdWithExpiration> result;
    if (StorageIsPersistent()) {
      // Should be persisted after the storage is closed and re-opened.

      result = storage->GetKeyAndNonUniqueUserId();
      ASSERT_TRUE(result);
      EXPECT_EQ(test_key_commitment, result->key_and_id.key_commitment);
      EXPECT_EQ(non_unique_user_id, result->key_and_id.non_unique_user_id);
      EXPECT_EQ(expiration, result->expiration);
    } else {
      // Should have been cleared.
      EXPECT_FALSE(storage->GetKeyAndNonUniqueUserId());
      storage->UpdateKeyAndNonUniqueUserId(
          {{test_key_commitment, non_unique_user_id}, expiration});
    }

    non_unique_user_id += 1;
    storage->UpdateKeyAndNonUniqueUserId(
        {{test_key_commitment, non_unique_user_id}, expiration});
    result = storage->GetKeyAndNonUniqueUserId();
    ASSERT_TRUE(result);
    EXPECT_EQ(test_key_commitment, result->key_and_id.key_commitment);
    EXPECT_EQ(non_unique_user_id, result->key_and_id.non_unique_user_id);
    EXPECT_EQ(expiration, result->expiration);
  }

  task_environment().RunUntilIdle();

  {
    std::unique_ptr<KAnonymityServiceStorage> storage = CreateStorage();
    std::optional<KeyAndNonUniqueUserIdWithExpiration> result;
    if (StorageIsPersistent()) {
      // Modifications should be persisted after the storage is closed and
      // re-opened.
      result = storage->GetKeyAndNonUniqueUserId();
      ASSERT_TRUE(result);
      EXPECT_EQ(test_key_commitment, result->key_and_id.key_commitment);
      EXPECT_EQ(non_unique_user_id, result->key_and_id.non_unique_user_id);
      EXPECT_EQ(expiration, result->expiration);
    } else {
      // Should have been cleared.
      EXPECT_FALSE(storage->GetKeyAndNonUniqueUserId());
    }
  }
}

TEST_P(KAnonymityServiceStorageTest, HandlesDestructionBeforeReady) {
  std::unique_ptr<KAnonymityServiceStorage> storage;
  if (StorageIsPersistent()) {
    storage = CreateKAnonymitySqlStorageForPath(db_path());
  } else {
    storage = std::make_unique<KAnonymityServiceMemoryStorage>();
  }
  storage->WaitUntilReady(base::BindLambdaForTesting(
      [&](KAnonymityServiceStorage::InitStatus status) {
        if (StorageIsPersistent()) {
          // Persistent storage doesn't return synchronously, so it should have
          // already been released.
          EXPECT_FALSE(storage);
        } else {
          // Memory storage calls synchronously so we haven't released the
          // storage.
          EXPECT_TRUE(storage);
        }
      }));
  storage.reset();
  task_environment().RunUntilIdle();
}

INSTANTIATE_TEST_SUITE_P(
    /* no label */,
    KAnonymityServiceStorageTest,
    ::testing::Values(false, true));

}  // namespace