1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "sql/meta_table.h"
9 #include "base/files/file_path.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "sql/database.h"
12 #include "sql/statement.h"
13 #include "testing/gtest/include/gtest/gtest.h"
19 class SQLMetaTableTest : public testing::Test {
21 ~SQLMetaTableTest() override = default;
23 void SetUp() override {
24 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
26 db_.Open(temp_dir_.GetPath().AppendASCII("meta_table_test.sqlite")));
30 base::ScopedTempDir temp_dir_;
34 TEST_F(SQLMetaTableTest, DoesTableExist) {
35 EXPECT_FALSE(MetaTable::DoesTableExist(&db_));
39 EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
42 EXPECT_TRUE(MetaTable::DoesTableExist(&db_));
45 TEST_F(SQLMetaTableTest, DeleteTableForTesting) {
47 EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
49 EXPECT_TRUE(MetaTable::DeleteTableForTesting(&db_));
50 EXPECT_FALSE(MetaTable::DoesTableExist(&db_));
53 TEST_F(SQLMetaTableTest, RazeIfIncompatiblePreservesDatabasesWithoutMetadata) {
54 EXPECT_TRUE(db_.Execute("CREATE TABLE data(id INTEGER PRIMARY KEY)"));
55 ASSERT_TRUE(db_.DoesTableExist("data"));
57 // The table should not have been cleared, since the database does not have a
59 MetaTable::RazeIfIncompatible(&db_, 1,
60 /*current_version=*/1);
61 EXPECT_TRUE(db_.DoesTableExist("data"));
64 TEST_F(SQLMetaTableTest, RazeIfIncompatibleRazesIncompatiblyOldTables) {
65 constexpr int kWrittenVersion = 1;
66 constexpr int kCompatibleVersion = 1;
68 // Setup a current database.
71 EXPECT_TRUE(meta_table.Init(&db_, kWrittenVersion, kCompatibleVersion));
72 EXPECT_TRUE(db_.Execute("CREATE TABLE data(id INTEGER PRIMARY KEY)"));
73 ASSERT_TRUE(db_.DoesTableExist("data"));
76 // The table should have been cleared, since the least version compatible with
77 // the written database is greater than the current version.
78 MetaTable::RazeIfIncompatible(&db_, kWrittenVersion + 1,
79 /*current_version=*/kWrittenVersion + 1);
80 EXPECT_FALSE(db_.DoesTableExist("data"));
83 TEST_F(SQLMetaTableTest, RazeIfIncompatibleRazesIncompatiblyNewTables) {
84 constexpr int kCompatibleVersion = 2;
85 constexpr int kWrittenVersion = 3;
87 // Setup a current database.
90 EXPECT_TRUE(meta_table.Init(&db_, kWrittenVersion, kCompatibleVersion));
91 EXPECT_TRUE(db_.Execute("CREATE TABLE data(id INTEGER PRIMARY KEY)"));
92 ASSERT_TRUE(db_.DoesTableExist("data"));
95 // The table should have been cleared, since the least version compatible with
96 // the written database is greater than the current version.
97 MetaTable::RazeIfIncompatible(&db_, MetaTable::kNoLowestSupportedVersion,
98 /*current_version=*/kCompatibleVersion - 1);
99 EXPECT_FALSE(db_.DoesTableExist("data"));
102 TEST_F(SQLMetaTableTest, RazeIfIncompatibleDoesntRazeWhenItShouldnt) {
103 constexpr int kVersion = 2;
106 MetaTable meta_table;
108 meta_table.Init(&db_, kVersion, /*compatible_version=*/kVersion - 1));
109 EXPECT_TRUE(db_.Execute("CREATE TABLE data(id INTEGER PRIMARY KEY)"));
110 EXPECT_TRUE(db_.DoesTableExist("data"));
113 MetaTable::RazeIfIncompatible(&db_, kVersion,
114 /*current_version=*/kVersion);
115 EXPECT_TRUE(db_.DoesTableExist("data"))
116 << "Table should still exist if the database version is exactly right.";
118 MetaTable::RazeIfIncompatible(&db_, kVersion - 1,
119 /*current_version=*/kVersion);
120 EXPECT_TRUE(db_.DoesTableExist("data"))
121 << "... or if the lower bound is less than the actual version";
123 MetaTable::RazeIfIncompatible(&db_, MetaTable::kNoLowestSupportedVersion,
124 /*current_version=*/kVersion);
125 EXPECT_TRUE(db_.DoesTableExist("data"))
126 << "... or if the lower bound is not set";
128 MetaTable::RazeIfIncompatible(&db_, MetaTable::kNoLowestSupportedVersion,
129 /*current_version=*/kVersion - 1);
130 EXPECT_TRUE(db_.DoesTableExist("data"))
131 << "... even if the current version exactly matches the written "
132 "database's least compatible version.";
135 TEST_F(SQLMetaTableTest, VersionNumber) {
136 // Compatibility versions one less than the main versions to make
137 // sure the values aren't being crossed with each other.
138 constexpr int kVersionFirst = 2;
139 constexpr int kCompatVersionFirst = kVersionFirst - 1;
140 constexpr int kVersionSecond = 4;
141 constexpr int kCompatVersionSecond = kVersionSecond - 1;
142 constexpr int kVersionThird = 6;
143 constexpr int kCompatVersionThird = kVersionThird - 1;
145 // First Init() sets the version info as expected.
147 MetaTable meta_table;
148 EXPECT_TRUE(meta_table.Init(&db_, kVersionFirst, kCompatVersionFirst));
149 EXPECT_EQ(kVersionFirst, meta_table.GetVersionNumber());
150 EXPECT_EQ(kCompatVersionFirst, meta_table.GetCompatibleVersionNumber());
153 // Second Init() does not change the version info.
155 MetaTable meta_table;
156 EXPECT_TRUE(meta_table.Init(&db_, kVersionSecond, kCompatVersionSecond));
157 EXPECT_EQ(kVersionFirst, meta_table.GetVersionNumber());
158 EXPECT_EQ(kCompatVersionFirst, meta_table.GetCompatibleVersionNumber());
160 meta_table.SetVersionNumber(kVersionSecond);
161 meta_table.SetCompatibleVersionNumber(kCompatVersionSecond);
164 // Version info from Set*() calls is seen.
166 MetaTable meta_table;
167 EXPECT_TRUE(meta_table.Init(&db_, kVersionThird, kCompatVersionThird));
168 EXPECT_EQ(kVersionSecond, meta_table.GetVersionNumber());
169 EXPECT_EQ(kCompatVersionSecond, meta_table.GetCompatibleVersionNumber());
173 TEST_F(SQLMetaTableTest, StringValue) {
174 static const char kKey[] = "String Key";
175 const std::string kFirstValue("First Value");
176 const std::string kSecondValue("Second Value");
178 // Initially, the value isn't there until set.
180 MetaTable meta_table;
181 EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
184 EXPECT_FALSE(meta_table.GetValue(kKey, &value));
186 EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue));
187 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
188 EXPECT_EQ(kFirstValue, value);
191 // Value is persistent across different instances.
193 MetaTable meta_table;
194 EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
197 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
198 EXPECT_EQ(kFirstValue, value);
200 EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue));
203 // Existing value was successfully changed.
205 MetaTable meta_table;
206 EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
209 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
210 EXPECT_EQ(kSecondValue, value);
214 TEST_F(SQLMetaTableTest, IntValue) {
215 static const char kKey[] = "Int Key";
216 constexpr int kFirstValue = 17;
217 constexpr int kSecondValue = 23;
219 // Initially, the value isn't there until set.
221 MetaTable meta_table;
222 EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
225 EXPECT_FALSE(meta_table.GetValue(kKey, &value));
227 EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue));
228 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
229 EXPECT_EQ(kFirstValue, value);
232 // Value is persistent across different instances.
234 MetaTable meta_table;
235 EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
238 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
239 EXPECT_EQ(kFirstValue, value);
241 EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue));
244 // Existing value was successfully changed.
246 MetaTable meta_table;
247 EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
250 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
251 EXPECT_EQ(kSecondValue, value);
255 TEST_F(SQLMetaTableTest, Int64Value) {
256 static const char kKey[] = "Int Key";
257 const int64_t kFirstValue = 5000000017LL;
258 const int64_t kSecondValue = 5000000023LL;
260 // Initially, the value isn't there until set.
262 MetaTable meta_table;
263 EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
266 EXPECT_FALSE(meta_table.GetValue(kKey, &value));
268 EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue));
269 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
270 EXPECT_EQ(kFirstValue, value);
273 // Value is persistent across different instances.
275 MetaTable meta_table;
276 EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
279 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
280 EXPECT_EQ(kFirstValue, value);
282 EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue));
285 // Existing value was successfully changed.
287 MetaTable meta_table;
288 EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
291 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
292 EXPECT_EQ(kSecondValue, value);
296 TEST_F(SQLMetaTableTest, DeleteKey) {
297 static const char kKey[] = "String Key";
298 const std::string kValue("String Value");
300 MetaTable meta_table;
301 EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
303 // Value isn't present.
305 EXPECT_FALSE(meta_table.GetValue(kKey, &value));
307 // Now value is present.
308 EXPECT_TRUE(meta_table.SetValue(kKey, kValue));
309 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
310 EXPECT_EQ(kValue, value);
312 // After delete value isn't present.
313 EXPECT_TRUE(meta_table.DeleteKey(kKey));
314 EXPECT_FALSE(meta_table.GetValue(kKey, &value));