forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_options_unittest.cc
334 lines (277 loc) · 9.9 KB
/
database_options_unittest.cc
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
// Copyright 2022 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/callback_helpers.h"
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "sql/database.h"
#include "sql/sqlite_result_code.h"
#include "sql/statement.h"
#include "sql/test/scoped_error_expecter.h"
#include "sql/test/test_helpers.h"
#include "sql/transaction.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "third_party/sqlite/sqlite3.h"
namespace sql {
namespace {
enum class OpenVariant {
kInMemory = 1,
kOnDiskExclusiveJournal = 2,
kOnDiskNonExclusiveJournal = 3,
kOnDiskExclusiveWal = 4,
};
// We use the parameter to run all tests with WAL mode on and off.
class DatabaseOptionsTest : public testing::TestWithParam<OpenVariant> {
public:
DatabaseOptionsTest() = default;
~DatabaseOptionsTest() override = default;
void SetUp() override {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
db_path_ = temp_dir_.GetPath().AppendASCII("database_test.sqlite");
}
OpenVariant open_variant() const { return GetParam(); }
// The options below interact with all other options. These tests ensure that
// all combinations work.
bool exclusive_locking() const {
return GetParam() != OpenVariant::kOnDiskNonExclusiveJournal;
}
bool wal_mode() const {
return GetParam() == OpenVariant::kOnDiskExclusiveWal;
}
void OpenDatabase(Database& db) {
switch (open_variant()) {
case OpenVariant::kOnDiskExclusiveJournal:
ASSERT_TRUE(db.Open(db_path_));
break;
case OpenVariant::kOnDiskNonExclusiveJournal:
ASSERT_TRUE(db.Open(db_path_));
break;
case OpenVariant::kOnDiskExclusiveWal:
ASSERT_TRUE(db.Open(db_path_));
break;
case OpenVariant::kInMemory:
ASSERT_TRUE(db.OpenInMemory());
break;
}
}
// Runs a rolled back transaction, followed by a committed transaction.
void RunTransactions(Database& db) {
{
Transaction rolled_back(&db);
ASSERT_TRUE(rolled_back.Begin());
ASSERT_TRUE(db.Execute("CREATE TABLE rows(id PRIMARY KEY NOT NULL)"));
rolled_back.Rollback();
}
{
Transaction committed(&db);
ASSERT_TRUE(committed.Begin());
ASSERT_TRUE(db.Execute("CREATE TABLE rows(id PRIMARY KEY NOT NULL)"));
ASSERT_TRUE(committed.Commit());
}
}
protected:
base::ScopedTempDir temp_dir_;
base::FilePath db_path_;
};
TEST_P(DatabaseOptionsTest, FlushToDisk_FalseByDefault) {
DatabaseOptions options = {
.exclusive_locking = exclusive_locking(),
.wal_mode = wal_mode(),
};
EXPECT_FALSE(options.flush_to_media) << "Invalid test assumption";
Database db(options);
OpenDatabase(db);
EXPECT_EQ("0", sql::test::ExecuteWithResult(&db, "PRAGMA fullfsync"));
}
TEST_P(DatabaseOptionsTest, FlushToDisk_True) {
Database db(DatabaseOptions{
.exclusive_locking = exclusive_locking(),
.wal_mode = wal_mode(),
.flush_to_media = true,
});
OpenDatabase(db);
EXPECT_EQ("1", sql::test::ExecuteWithResult(&db, "PRAGMA fullfsync"));
}
TEST_P(DatabaseOptionsTest, FlushToDisk_False_DoesNotCrash) {
Database db(DatabaseOptions{
.exclusive_locking = exclusive_locking(),
.wal_mode = wal_mode(),
.flush_to_media = false,
});
OpenDatabase(db);
EXPECT_EQ("0", sql::test::ExecuteWithResult(&db, "PRAGMA fullfsync"))
<< "Invalid test setup";
RunTransactions(db);
}
TEST_P(DatabaseOptionsTest, FlushToDisk_True_DoesNotCrash) {
Database db(DatabaseOptions{
.exclusive_locking = exclusive_locking(),
.wal_mode = wal_mode(),
.flush_to_media = true,
});
OpenDatabase(db);
EXPECT_EQ("1", sql::test::ExecuteWithResult(&db, "PRAGMA fullfsync"))
<< "Invalid test setup";
RunTransactions(db);
}
TEST_P(DatabaseOptionsTest, PageSize_Default) {
static_assert(DatabaseOptions::kDefaultPageSize == 4096,
"The page size numbers in this test file need to change");
Database db(DatabaseOptions{
.exclusive_locking = exclusive_locking(),
.wal_mode = wal_mode(),
.page_size = 4096,
});
OpenDatabase(db);
EXPECT_EQ("4096", sql::test::ExecuteWithResult(&db, "PRAGMA page_size"));
RunTransactions(db);
if (open_variant() != OpenVariant::kInMemory) {
db.Close();
EXPECT_EQ(4096, sql::test::ReadDatabasePageSize(db_path_).value_or(-1));
}
}
TEST_P(DatabaseOptionsTest, PageSize_Large) {
static_assert(DatabaseOptions::kDefaultPageSize < 16384,
"The page size numbers in this test file need to change");
Database db(DatabaseOptions{
.exclusive_locking = exclusive_locking(),
.wal_mode = wal_mode(),
.page_size = 16384,
});
OpenDatabase(db);
EXPECT_EQ("16384", sql::test::ExecuteWithResult(&db, "PRAGMA page_size"));
RunTransactions(db);
if (open_variant() != OpenVariant::kInMemory) {
db.Close();
EXPECT_EQ(16384, sql::test::ReadDatabasePageSize(db_path_).value_or(-1));
}
}
TEST_P(DatabaseOptionsTest, PageSize_Small) {
static_assert(DatabaseOptions::kDefaultPageSize > 1024,
"The page size numbers in this test file need to change");
Database db(DatabaseOptions{
.exclusive_locking = exclusive_locking(),
.wal_mode = wal_mode(),
.page_size = 1024,
});
OpenDatabase(db);
EXPECT_EQ("1024", sql::test::ExecuteWithResult(&db, "PRAGMA page_size"));
RunTransactions(db);
if (open_variant() != OpenVariant::kInMemory) {
db.Close();
EXPECT_EQ(1024, sql::test::ReadDatabasePageSize(db_path_).value_or(-1));
}
}
TEST_P(DatabaseOptionsTest, CacheSize_Legacy) {
Database db(DatabaseOptions{
.exclusive_locking = exclusive_locking(),
.wal_mode = wal_mode(),
.cache_size = 0,
});
OpenDatabase(db);
EXPECT_EQ("-2000", sql::test::ExecuteWithResult(&db, "PRAGMA cache_size"));
}
TEST_P(DatabaseOptionsTest, CacheSize_Small) {
Database db(DatabaseOptions{
.exclusive_locking = exclusive_locking(),
.wal_mode = wal_mode(),
.cache_size = 16,
});
OpenDatabase(db);
EXPECT_EQ("16", sql::test::ExecuteWithResult(&db, "PRAGMA cache_size"));
}
TEST_P(DatabaseOptionsTest, CacheSize_Large) {
Database db(DatabaseOptions{
.exclusive_locking = exclusive_locking(),
.wal_mode = wal_mode(),
.cache_size = 1000,
});
OpenDatabase(db);
EXPECT_EQ("1000", sql::test::ExecuteWithResult(&db, "PRAGMA cache_size"));
}
TEST_P(DatabaseOptionsTest, EnableViewsDiscouraged_FalseByDefault) {
DatabaseOptions options = {
.exclusive_locking = exclusive_locking(),
.wal_mode = wal_mode(),
};
EXPECT_FALSE(options.enable_views_discouraged) << "Invalid test assumption";
Database db(options);
OpenDatabase(db);
// sqlite3_db_config() currently only disables querying views. Schema
// operations on views are still allowed.
ASSERT_TRUE(db.Execute("CREATE VIEW view(id) AS SELECT 1"));
{
sql::test::ScopedErrorExpecter expecter;
expecter.ExpectError(SQLITE_ERROR);
Statement select_from_view(db.GetUniqueStatement("SELECT id FROM view"));
EXPECT_FALSE(select_from_view.is_valid());
EXPECT_TRUE(expecter.SawExpectedErrors());
}
// sqlite3_db_config() currently only disables querying views. Schema
// operations on views are still allowed.
EXPECT_TRUE(db.Execute("DROP VIEW IF EXISTS view"));
}
TEST_P(DatabaseOptionsTest, EnableViewsDiscouraged_True) {
Database db(DatabaseOptions{
.exclusive_locking = exclusive_locking(),
.wal_mode = wal_mode(),
.enable_views_discouraged = true,
});
OpenDatabase(db);
ASSERT_TRUE(db.Execute("CREATE VIEW view(id) AS SELECT 1"));
Statement select_from_view(db.GetUniqueStatement("SELECT id FROM view"));
ASSERT_TRUE(select_from_view.is_valid());
EXPECT_TRUE(select_from_view.Step());
EXPECT_EQ(1, select_from_view.ColumnInt64(0));
EXPECT_TRUE(db.Execute("DROP VIEW IF EXISTS view"));
}
TEST_P(DatabaseOptionsTest, EnableVirtualTablesDiscouraged_FalseByDefault) {
DatabaseOptions options = {
.exclusive_locking = exclusive_locking(),
.wal_mode = wal_mode(),
};
EXPECT_FALSE(options.enable_virtual_tables_discouraged)
<< "Invalid test assumption";
Database db(options);
OpenDatabase(db);
// sqlite3_prepare_v3() currently only disables accessing virtual tables.
// Schema operations on virtual tables are still allowed.
ASSERT_TRUE(db.Execute(
"CREATE VIRTUAL TABLE fts_table USING fts3(data_table, content TEXT)"));
{
sql::test::ScopedErrorExpecter expecter;
expecter.ExpectError(SQLITE_ERROR);
Statement select_from_vtable(db.GetUniqueStatement(
"SELECT content FROM fts_table WHERE content MATCH 'pattern'"));
EXPECT_FALSE(select_from_vtable.is_valid());
EXPECT_TRUE(expecter.SawExpectedErrors());
}
// sqlite3_prepare_v3() currently only disables accessing virtual tables.
// Schema operations on virtual tables are still allowed.
EXPECT_TRUE(db.Execute("DROP TABLE IF EXISTS fts_table"));
}
TEST_P(DatabaseOptionsTest, EnableVirtualTablesDiscouraged_True) {
Database db(DatabaseOptions{
.exclusive_locking = exclusive_locking(),
.wal_mode = wal_mode(),
.enable_virtual_tables_discouraged = true,
});
OpenDatabase(db);
ASSERT_TRUE(db.Execute(
"CREATE VIRTUAL TABLE fts_table USING fts3(data_table, content TEXT)"));
Statement select_from_vtable(db.GetUniqueStatement(
"SELECT content FROM fts_table WHERE content MATCH 'pattern'"));
ASSERT_TRUE(select_from_vtable.is_valid());
EXPECT_FALSE(select_from_vtable.Step());
EXPECT_TRUE(db.Execute("DROP TABLE IF EXISTS fts_table"));
}
INSTANTIATE_TEST_SUITE_P(
,
DatabaseOptionsTest,
testing::Values(OpenVariant::kInMemory,
OpenVariant::kOnDiskExclusiveJournal,
OpenVariant::kOnDiskNonExclusiveJournal,
OpenVariant::kOnDiskExclusiveWal));
} // namespace
} // namespace sql