File: leveldb_scopes_test_utils.h

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 (128 lines) | stat: -rw-r--r-- 5,022 bytes parent folder | download | duplicates (9)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_SERVICES_STORAGE_INDEXED_DB_SCOPES_LEVELDB_SCOPES_TEST_UTILS_H_
#define COMPONENTS_SERVICES_STORAGE_INDEXED_DB_SCOPES_LEVELDB_SCOPES_TEST_UTILS_H_

#include "base/files/scoped_temp_dir.h"
#include "base/functional/callback.h"
#include "base/test/task_environment.h"
#include "base/threading/thread_restrictions.h"
#include "components/services/storage/indexed_db/leveldb/fake_leveldb_factory.h"
#include "components/services/storage/indexed_db/leveldb/leveldb_state.h"
#include "components/services/storage/indexed_db/locks/partitioned_lock_manager.h"
#include "components/services/storage/indexed_db/scopes/leveldb_scopes_coding.h"
#include "components/services/storage/indexed_db/scopes/scopes_metadata.pb.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/leveldatabase/src/include/leveldb/slice.h"
#include "third_party/leveldatabase/src/include/leveldb/status.h"

namespace content::indexed_db {

class LevelDBScopesTestBase : public testing::Test {
 public:
  static constexpr const size_t kWriteBatchSizeForTesting = 1024;

  LevelDBScopesTestBase();
  ~LevelDBScopesTestBase() override;

  void SetUp() override;

  // Ensures that |leveldb_| is destroyed correctly, and all contents are
  // deleted off disk.
  void TearDown() override;

  // Destroys the leveldb files.
  leveldb::Status DestroyDB();

  // Ensures that |leveldb_| is destroyed correctly, but doesn't delete the
  // database on disk.
  void CloseScopesAndDestroyLevelDBState();

  // Initializes |leveldb_| to be a read database backed on disk.
  void SetUpRealDatabase();

  // Initializes |leveldb_| to be a database that will break when |callback| is
  // called, with the given status.
  void SetUpBreakableDB(base::OnceCallback<void(leveldb::Status)>* callback);

  // Initializes |leveldb_| to be a flaky database that will
  void SetUpFlakyDB(std::queue<FakeLevelDBFactory::FlakePoint> flake_points);

  // Writes |metadata_buffer_| to disk.
  void WriteScopesMetadata(int64_t scope_number, bool ignore_cleanup_tasks);

  // Writes |undo_task_buffer_| to disk.
  void WriteUndoTask(int64_t scope_number, int64_t sequence_number);

  // Writes |cleanup_task_buffer_| to disk.
  void WriteCleanupTask(int64_t scope_number, int64_t sequence_number);

  // Writes a large value to the given key (|large_string_|).
  void WriteLargeValue(const std::string& key);

  // Loads the given key into |value_buffer_|.
  leveldb::Status LoadAt(const std::string& key);

  // Loads the scope metadata into |value_buffer_|.
  leveldb::Status LoadScopeMetadata(int64_t scope_number);

  // Loads the undo task into |value_buffer_|.
  leveldb::Status LoadUndoTask(int64_t scope_number, int64_t sequence_number);

  // Loads the cleanup task into |value_buffer_|.
  leveldb::Status LoadCleanupTask(int64_t scope_number,
                                  int64_t sequence_number);

  // Returns if the database doesn't have any entries with the given key prefix.
  bool IsPrefixedRangeEmptyInDB(leveldb::Slice key);

  // Returns if all of the scope log data and metadata is gone.
  bool IsScopeCleanedUp(int64_t scope_number);

  // Returns if any scope data exists, including metadata or log data (other
  // than global metadata).
  bool ScopeDataExistsOnDisk();

  // Creates a shared lock request from |simple_lock_begin_| to
  // |simple_lock_end_|.
  PartitionedLockManager::PartitionedLockRequest CreateSimpleSharedLock();
  // Creates a exclusive lock request from |simple_lock_begin_| to
  // |simple_lock_end_|.
  PartitionedLockManager::PartitionedLockRequest CreateSimpleExclusiveLock();

  PartitionedLockManager::PartitionedLockRequest CreateSharedLock(int i);
  PartitionedLockManager::PartitionedLockRequest CreateExclusiveLock(int i);

  const base::FilePath& DatabaseDirFilePath();

  leveldb::Status CreateAndSaveLevelDBState();

 protected:
  base::ScopedAllowBaseSyncPrimitivesForTesting allow_;
  // Ensure that the ScopedTempDir outlives the TaskEnvironment since the latter
  // may need to run cleanup tasks that close files residing in the former.
  base::ScopedTempDir temp_directory_;
  base::test::TaskEnvironment task_env_;
  // For use with calling leveldb_->RequestDestruction(...);
  base::WaitableEvent leveldb_close_event_;

  const std::string simple_lock_begin_ = "0000000001";
  const std::string simple_lock_end_ = "0000000010";
  const std::vector<uint8_t> metadata_prefix_ = {'a'};
  const std::vector<uint8_t> db_prefix_ = {'b'};

  scoped_refptr<LevelDBState> leveldb_;
  std::string large_string_;
  LevelDBScopesUndoTask undo_task_buffer_;
  LevelDBScopesCleanupTask cleanup_task_buffer_;
  LevelDBScopesScopeMetadata metadata_buffer_;

  ScopesEncoder scopes_encoder_;
  std::string value_buffer_;
};

}  // namespace content::indexed_db

#endif  // COMPONENTS_SERVICES_STORAGE_INDEXED_DB_SCOPES_LEVELDB_SCOPES_TEST_UTILS_H_