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 EXPECT_TRUE(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.
79 MetaTable::RazeIfIncompatible(&db_, kWrittenVersion + 1,
80 /*current_version=*/kWrittenVersion + 1));
81 EXPECT_FALSE(db_.DoesTableExist("data"));
84 TEST_F(SQLMetaTableTest, RazeIfIncompatibleRazesIncompatiblyNewTables) {
85 constexpr int kCompatibleVersion = 2;
86 constexpr int kWrittenVersion = 3;
88 // Setup a current database.
91 EXPECT_TRUE(meta_table.Init(&db_, kWrittenVersion, kCompatibleVersion));
92 EXPECT_TRUE(db_.Execute("CREATE TABLE data(id INTEGER PRIMARY KEY)"));
93 ASSERT_TRUE(db_.DoesTableExist("data"));
96 // The table should have been cleared, since the least version compatible with
97 // the written database is greater than the current version.
98 EXPECT_TRUE(MetaTable::RazeIfIncompatible(
99 &db_, MetaTable::kNoLowestSupportedVersion,
100 /*current_version=*/kCompatibleVersion - 1));
101 EXPECT_FALSE(db_.DoesTableExist("data"));
104 TEST_F(SQLMetaTableTest, RazeIfIncompatibleDoesntRazeWhenItShouldnt) {
105 constexpr int kVersion = 2;
108 MetaTable meta_table;
110 meta_table.Init(&db_, kVersion, /*compatible_version=*/kVersion - 1));
111 EXPECT_TRUE(db_.Execute("CREATE TABLE data(id INTEGER PRIMARY KEY)"));
112 EXPECT_TRUE(db_.DoesTableExist("data"));
115 EXPECT_TRUE(MetaTable::RazeIfIncompatible(&db_, kVersion,
116 /*current_version=*/kVersion));
117 EXPECT_TRUE(db_.DoesTableExist("data"))
118 << "Table should still exist if the database version is exactly right.";
120 EXPECT_TRUE(MetaTable::RazeIfIncompatible(&db_, kVersion - 1,
121 /*current_version=*/kVersion));
122 EXPECT_TRUE(db_.DoesTableExist("data"))
123 << "... or if the lower bound is less than the actual version";
126 MetaTable::RazeIfIncompatible(&db_, MetaTable::kNoLowestSupportedVersion,
127 /*current_version=*/kVersion));
128 EXPECT_TRUE(db_.DoesTableExist("data"))
129 << "... or if the lower bound is not set";
132 MetaTable::RazeIfIncompatible(&db_, MetaTable::kNoLowestSupportedVersion,
133 /*current_version=*/kVersion - 1));
134 EXPECT_TRUE(db_.DoesTableExist("data"))
135 << "... even if the current version exactly matches the written "
136 "database's least compatible version.";
139 TEST_F(SQLMetaTableTest, VersionNumber) {
140 // Compatibility versions one less than the main versions to make
141 // sure the values aren't being crossed with each other.
142 constexpr int kVersionFirst = 2;
143 constexpr int kCompatVersionFirst = kVersionFirst - 1;
144 constexpr int kVersionSecond = 4;
145 constexpr int kCompatVersionSecond = kVersionSecond - 1;
146 constexpr int kVersionThird = 6;
147 constexpr int kCompatVersionThird = kVersionThird - 1;
149 // First Init() sets the version info as expected.
151 MetaTable meta_table;
152 EXPECT_TRUE(meta_table.Init(&db_, kVersionFirst, kCompatVersionFirst));
153 EXPECT_EQ(kVersionFirst, meta_table.GetVersionNumber());
154 EXPECT_EQ(kCompatVersionFirst, meta_table.GetCompatibleVersionNumber());
157 // Second Init() does not change the version info.
159 MetaTable meta_table;
160 EXPECT_TRUE(meta_table.Init(&db_, kVersionSecond, kCompatVersionSecond));
161 EXPECT_EQ(kVersionFirst, meta_table.GetVersionNumber());
162 EXPECT_EQ(kCompatVersionFirst, meta_table.GetCompatibleVersionNumber());
164 EXPECT_TRUE(meta_table.SetVersionNumber(kVersionSecond));
165 EXPECT_TRUE(meta_table.SetCompatibleVersionNumber(kCompatVersionSecond));
168 // Version info from Set*() calls is seen.
170 MetaTable meta_table;
171 EXPECT_TRUE(meta_table.Init(&db_, kVersionThird, kCompatVersionThird));
172 EXPECT_EQ(kVersionSecond, meta_table.GetVersionNumber());
173 EXPECT_EQ(kCompatVersionSecond, meta_table.GetCompatibleVersionNumber());
177 TEST_F(SQLMetaTableTest, StringValue) {
178 static const char kKey[] = "String Key";
179 const std::string kFirstValue("First Value");
180 const std::string kSecondValue("Second Value");
182 // Initially, the value isn't there until set.
184 MetaTable meta_table;
185 EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
188 EXPECT_FALSE(meta_table.GetValue(kKey, &value));
190 EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue));
191 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
192 EXPECT_EQ(kFirstValue, value);
195 // Value is persistent across different instances.
197 MetaTable meta_table;
198 EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
201 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
202 EXPECT_EQ(kFirstValue, value);
204 EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue));
207 // Existing value was successfully changed.
209 MetaTable meta_table;
210 EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
213 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
214 EXPECT_EQ(kSecondValue, value);
218 TEST_F(SQLMetaTableTest, IntValue) {
219 static const char kKey[] = "Int Key";
220 constexpr int kFirstValue = 17;
221 constexpr int kSecondValue = 23;
223 // Initially, the value isn't there until set.
225 MetaTable meta_table;
226 EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
229 EXPECT_FALSE(meta_table.GetValue(kKey, &value));
231 EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue));
232 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
233 EXPECT_EQ(kFirstValue, value);
236 // Value is persistent across different instances.
238 MetaTable meta_table;
239 EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
242 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
243 EXPECT_EQ(kFirstValue, value);
245 EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue));
248 // Existing value was successfully changed.
250 MetaTable meta_table;
251 EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
254 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
255 EXPECT_EQ(kSecondValue, value);
259 TEST_F(SQLMetaTableTest, Int64Value) {
260 static const char kKey[] = "Int Key";
261 const int64_t kFirstValue = 5000000017LL;
262 const int64_t kSecondValue = 5000000023LL;
264 // Initially, the value isn't there until set.
266 MetaTable meta_table;
267 EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
270 EXPECT_FALSE(meta_table.GetValue(kKey, &value));
272 EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue));
273 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
274 EXPECT_EQ(kFirstValue, value);
277 // Value is persistent across different instances.
279 MetaTable meta_table;
280 EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
283 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
284 EXPECT_EQ(kFirstValue, value);
286 EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue));
289 // Existing value was successfully changed.
291 MetaTable meta_table;
292 EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
295 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
296 EXPECT_EQ(kSecondValue, value);
300 TEST_F(SQLMetaTableTest, DeleteKey) {
301 static const char kKey[] = "String Key";
302 const std::string kValue("String Value");
304 MetaTable meta_table;
305 EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
307 // Value isn't present.
309 EXPECT_FALSE(meta_table.GetValue(kKey, &value));
311 // Now value is present.
312 EXPECT_TRUE(meta_table.SetValue(kKey, kValue));
313 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
314 EXPECT_EQ(kValue, value);
316 // After delete value isn't present.
317 EXPECT_TRUE(meta_table.DeleteKey(kKey));
318 EXPECT_FALSE(meta_table.GetValue(kKey, &value));