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
sql / meta_table_unittest.cc [blame]
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "sql/meta_table.h"
#include <stdint.h>
#include <string>
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "sql/database.h"
#include "sql/test/test_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace sql {
namespace {
class SQLMetaTableTest : public testing::Test {
public:
void SetUp() override {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
ASSERT_TRUE(
db_.Open(temp_dir_.GetPath().AppendASCII("meta_table_test.sqlite")));
}
protected:
base::ScopedTempDir temp_dir_;
Database db_{test::kTestTag};
};
TEST_F(SQLMetaTableTest, DoesTableExist) {
EXPECT_FALSE(MetaTable::DoesTableExist(&db_));
{
MetaTable meta_table;
EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
}
EXPECT_TRUE(MetaTable::DoesTableExist(&db_));
}
TEST_F(SQLMetaTableTest, DeleteTableForTesting) {
MetaTable meta_table;
EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
EXPECT_TRUE(MetaTable::DeleteTableForTesting(&db_));
EXPECT_FALSE(MetaTable::DoesTableExist(&db_));
}
TEST_F(SQLMetaTableTest, RazeIfIncompatiblePreservesDatabasesWithoutMetadata) {
EXPECT_TRUE(db_.Execute("CREATE TABLE data(id INTEGER PRIMARY KEY)"));
ASSERT_TRUE(db_.DoesTableExist("data"));
// The table should not have been cleared, since the database does not have a
// metadata table.
EXPECT_EQ(RazeIfIncompatibleResult::kCompatible,
MetaTable::RazeIfIncompatible(&db_, 1,
/*current_version=*/1));
EXPECT_TRUE(db_.DoesTableExist("data"));
}
TEST_F(SQLMetaTableTest, RazeIfIncompatibleRazesIncompatiblyOldTables) {
constexpr int kWrittenVersion = 1;
constexpr int kCompatibleVersion = 1;
// Setup a current database.
{
MetaTable meta_table;
EXPECT_TRUE(meta_table.Init(&db_, kWrittenVersion, kCompatibleVersion));
EXPECT_TRUE(db_.Execute("CREATE TABLE data(id INTEGER PRIMARY KEY)"));
ASSERT_TRUE(db_.DoesTableExist("data"));
}
// The table should have been cleared, since the least version compatible with
// the written database is greater than the current version.
EXPECT_EQ(
RazeIfIncompatibleResult::kRazedSuccessfully,
MetaTable::RazeIfIncompatible(&db_, kWrittenVersion + 1,
/*current_version=*/kWrittenVersion + 1));
EXPECT_FALSE(db_.DoesTableExist("data"));
}
TEST_F(SQLMetaTableTest, RazeIfIncompatibleRazesIncompatiblyNewTables) {
constexpr int kCompatibleVersion = 2;
constexpr int kWrittenVersion = 3;
// Setup a current database.
{
MetaTable meta_table;
EXPECT_TRUE(meta_table.Init(&db_, kWrittenVersion, kCompatibleVersion));
EXPECT_TRUE(db_.Execute("CREATE TABLE data(id INTEGER PRIMARY KEY)"));
ASSERT_TRUE(db_.DoesTableExist("data"));
}
// The table should have been cleared, since the least version compatible with
// the written database is greater than the current version.
EXPECT_EQ(RazeIfIncompatibleResult::kRazedSuccessfully,
MetaTable::RazeIfIncompatible(
&db_, MetaTable::kNoLowestSupportedVersion,
/*current_version=*/kCompatibleVersion - 1));
EXPECT_FALSE(db_.DoesTableExist("data"));
}
TEST_F(SQLMetaTableTest, RazeIfIncompatibleDoesntRazeWhenItShouldnt) {
constexpr int kVersion = 2;
{
MetaTable meta_table;
EXPECT_TRUE(
meta_table.Init(&db_, kVersion, /*compatible_version=*/kVersion - 1));
EXPECT_TRUE(db_.Execute("CREATE TABLE data(id INTEGER PRIMARY KEY)"));
EXPECT_TRUE(db_.DoesTableExist("data"));
}
EXPECT_EQ(RazeIfIncompatibleResult::kCompatible,
MetaTable::RazeIfIncompatible(&db_, kVersion,
/*current_version=*/kVersion));
EXPECT_TRUE(db_.DoesTableExist("data"))
<< "Table should still exist if the database version is exactly right.";
EXPECT_EQ(RazeIfIncompatibleResult::kCompatible,
MetaTable::RazeIfIncompatible(&db_, kVersion - 1,
/*current_version=*/kVersion));
EXPECT_TRUE(db_.DoesTableExist("data"))
<< "... or if the lower bound is less than the actual version";
EXPECT_EQ(
RazeIfIncompatibleResult::kCompatible,
MetaTable::RazeIfIncompatible(&db_, MetaTable::kNoLowestSupportedVersion,
/*current_version=*/kVersion));
EXPECT_TRUE(db_.DoesTableExist("data"))
<< "... or if the lower bound is not set";
EXPECT_EQ(
RazeIfIncompatibleResult::kCompatible,
MetaTable::RazeIfIncompatible(&db_, MetaTable::kNoLowestSupportedVersion,
/*current_version=*/kVersion - 1));
EXPECT_TRUE(db_.DoesTableExist("data"))
<< "... even if the current version exactly matches the written "
"database's least compatible version.";
}
TEST_F(SQLMetaTableTest, VersionNumber) {
// Compatibility versions one less than the main versions to make
// sure the values aren't being crossed with each other.
constexpr int kVersionFirst = 2;
constexpr int kCompatVersionFirst = kVersionFirst - 1;
constexpr int kVersionSecond = 4;
constexpr int kCompatVersionSecond = kVersionSecond - 1;
constexpr int kVersionThird = 6;
constexpr int kCompatVersionThird = kVersionThird - 1;
// First Init() sets the version info as expected.
{
MetaTable meta_table;
EXPECT_TRUE(meta_table.Init(&db_, kVersionFirst, kCompatVersionFirst));
EXPECT_EQ(kVersionFirst, meta_table.GetVersionNumber());
EXPECT_EQ(kCompatVersionFirst, meta_table.GetCompatibleVersionNumber());
}
// Second Init() does not change the version info.
{
MetaTable meta_table;
EXPECT_TRUE(meta_table.Init(&db_, kVersionSecond, kCompatVersionSecond));
EXPECT_EQ(kVersionFirst, meta_table.GetVersionNumber());
EXPECT_EQ(kCompatVersionFirst, meta_table.GetCompatibleVersionNumber());
EXPECT_TRUE(meta_table.SetVersionNumber(kVersionSecond));
EXPECT_TRUE(meta_table.SetCompatibleVersionNumber(kCompatVersionSecond));
}
// Version info from Set*() calls is seen.
{
MetaTable meta_table;
EXPECT_TRUE(meta_table.Init(&db_, kVersionThird, kCompatVersionThird));
EXPECT_EQ(kVersionSecond, meta_table.GetVersionNumber());
EXPECT_EQ(kCompatVersionSecond, meta_table.GetCompatibleVersionNumber());
}
}
TEST_F(SQLMetaTableTest, StringValue) {
static const char kKey[] = "String Key";
const std::string kFirstValue("First Value");
const std::string kSecondValue("Second Value");
// Initially, the value isn't there until set.
{
MetaTable meta_table;
EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
std::string value;
EXPECT_FALSE(meta_table.GetValue(kKey, &value));
EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue));
EXPECT_TRUE(meta_table.GetValue(kKey, &value));
EXPECT_EQ(kFirstValue, value);
}
// Value is persistent across different instances.
{
MetaTable meta_table;
EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
std::string value;
EXPECT_TRUE(meta_table.GetValue(kKey, &value));
EXPECT_EQ(kFirstValue, value);
EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue));
}
// Existing value was successfully changed.
{
MetaTable meta_table;
EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
std::string value;
EXPECT_TRUE(meta_table.GetValue(kKey, &value));
EXPECT_EQ(kSecondValue, value);
}
}
TEST_F(SQLMetaTableTest, IntValue) {
static const char kKey[] = "Int Key";
constexpr int kFirstValue = 17;
constexpr int kSecondValue = 23;
// Initially, the value isn't there until set.
{
MetaTable meta_table;
EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
int value;
EXPECT_FALSE(meta_table.GetValue(kKey, &value));
EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue));
EXPECT_TRUE(meta_table.GetValue(kKey, &value));
EXPECT_EQ(kFirstValue, value);
}
// Value is persistent across different instances.
{
MetaTable meta_table;
EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
int value;
EXPECT_TRUE(meta_table.GetValue(kKey, &value));
EXPECT_EQ(kFirstValue, value);
EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue));
}
// Existing value was successfully changed.
{
MetaTable meta_table;
EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
int value;
EXPECT_TRUE(meta_table.GetValue(kKey, &value));
EXPECT_EQ(kSecondValue, value);
}
}
TEST_F(SQLMetaTableTest, Int64Value) {
static const char kKey[] = "Int Key";
const int64_t kFirstValue = 5000000017LL;
const int64_t kSecondValue = 5000000023LL;
// Initially, the value isn't there until set.
{
MetaTable meta_table;
EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
int64_t value;
EXPECT_FALSE(meta_table.GetValue(kKey, &value));
EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue));
EXPECT_TRUE(meta_table.GetValue(kKey, &value));
EXPECT_EQ(kFirstValue, value);
}
// Value is persistent across different instances.
{
MetaTable meta_table;
EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
int64_t value;
EXPECT_TRUE(meta_table.GetValue(kKey, &value));
EXPECT_EQ(kFirstValue, value);
EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue));
}
// Existing value was successfully changed.
{
MetaTable meta_table;
EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
int64_t value;
EXPECT_TRUE(meta_table.GetValue(kKey, &value));
EXPECT_EQ(kSecondValue, value);
}
}
TEST_F(SQLMetaTableTest, DeleteKey) {
static const char kKey[] = "String Key";
const std::string kValue("String Value");
MetaTable meta_table;
EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
// Value isn't present.
std::string value;
EXPECT_FALSE(meta_table.GetValue(kKey, &value));
// Now value is present.
EXPECT_TRUE(meta_table.SetValue(kKey, kValue));
EXPECT_TRUE(meta_table.GetValue(kKey, &value));
EXPECT_EQ(kValue, value);
// After delete value isn't present.
EXPECT_TRUE(meta_table.DeleteKey(kKey));
EXPECT_FALSE(meta_table.GetValue(kKey, &value));
}
} // namespace
} // namespace sql