meta_table_unittest.cc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "sql/meta_table.h"
  5. #include <stdint.h>
  6. #include "base/files/file_path.h"
  7. #include "base/files/scoped_temp_dir.h"
  8. #include "sql/database.h"
  9. #include "sql/statement.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. namespace sql {
  12. namespace {
  13. class SQLMetaTableTest : public testing::Test {
  14. public:
  15. ~SQLMetaTableTest() override = default;
  16. void SetUp() override {
  17. ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
  18. ASSERT_TRUE(
  19. db_.Open(temp_dir_.GetPath().AppendASCII("meta_table_test.sqlite")));
  20. }
  21. protected:
  22. base::ScopedTempDir temp_dir_;
  23. Database db_;
  24. };
  25. TEST_F(SQLMetaTableTest, DoesTableExist) {
  26. EXPECT_FALSE(MetaTable::DoesTableExist(&db_));
  27. {
  28. MetaTable meta_table;
  29. EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
  30. }
  31. EXPECT_TRUE(MetaTable::DoesTableExist(&db_));
  32. }
  33. TEST_F(SQLMetaTableTest, DeleteTableForTesting) {
  34. MetaTable meta_table;
  35. EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
  36. EXPECT_TRUE(MetaTable::DeleteTableForTesting(&db_));
  37. EXPECT_FALSE(MetaTable::DoesTableExist(&db_));
  38. }
  39. TEST_F(SQLMetaTableTest, RazeIfIncompatiblePreservesDatabasesWithoutMetadata) {
  40. EXPECT_TRUE(db_.Execute("CREATE TABLE data(id INTEGER PRIMARY KEY)"));
  41. ASSERT_TRUE(db_.DoesTableExist("data"));
  42. // The table should not have been cleared, since the database does not have a
  43. // metadata table.
  44. MetaTable::RazeIfIncompatible(&db_, 1,
  45. /*current_version=*/1);
  46. EXPECT_TRUE(db_.DoesTableExist("data"));
  47. }
  48. TEST_F(SQLMetaTableTest, RazeIfIncompatibleRazesIncompatiblyOldTables) {
  49. constexpr int kWrittenVersion = 1;
  50. constexpr int kCompatibleVersion = 1;
  51. // Setup a current database.
  52. {
  53. MetaTable meta_table;
  54. EXPECT_TRUE(meta_table.Init(&db_, kWrittenVersion, kCompatibleVersion));
  55. EXPECT_TRUE(db_.Execute("CREATE TABLE data(id INTEGER PRIMARY KEY)"));
  56. ASSERT_TRUE(db_.DoesTableExist("data"));
  57. }
  58. // The table should have been cleared, since the least version compatible with
  59. // the written database is greater than the current version.
  60. MetaTable::RazeIfIncompatible(&db_, kWrittenVersion + 1,
  61. /*current_version=*/kWrittenVersion + 1);
  62. EXPECT_FALSE(db_.DoesTableExist("data"));
  63. }
  64. TEST_F(SQLMetaTableTest, RazeIfIncompatibleRazesIncompatiblyNewTables) {
  65. constexpr int kCompatibleVersion = 2;
  66. constexpr int kWrittenVersion = 3;
  67. // Setup a current database.
  68. {
  69. MetaTable meta_table;
  70. EXPECT_TRUE(meta_table.Init(&db_, kWrittenVersion, kCompatibleVersion));
  71. EXPECT_TRUE(db_.Execute("CREATE TABLE data(id INTEGER PRIMARY KEY)"));
  72. ASSERT_TRUE(db_.DoesTableExist("data"));
  73. }
  74. // The table should have been cleared, since the least version compatible with
  75. // the written database is greater than the current version.
  76. MetaTable::RazeIfIncompatible(&db_, MetaTable::kNoLowestSupportedVersion,
  77. /*current_version=*/kCompatibleVersion - 1);
  78. EXPECT_FALSE(db_.DoesTableExist("data"));
  79. }
  80. TEST_F(SQLMetaTableTest, RazeIfIncompatibleDoesntRazeWhenItShouldnt) {
  81. constexpr int kVersion = 2;
  82. {
  83. MetaTable meta_table;
  84. EXPECT_TRUE(
  85. meta_table.Init(&db_, kVersion, /*compatible_version=*/kVersion - 1));
  86. EXPECT_TRUE(db_.Execute("CREATE TABLE data(id INTEGER PRIMARY KEY)"));
  87. EXPECT_TRUE(db_.DoesTableExist("data"));
  88. }
  89. MetaTable::RazeIfIncompatible(&db_, kVersion,
  90. /*current_version=*/kVersion);
  91. EXPECT_TRUE(db_.DoesTableExist("data"))
  92. << "Table should still exist if the database version is exactly right.";
  93. MetaTable::RazeIfIncompatible(&db_, kVersion - 1,
  94. /*current_version=*/kVersion);
  95. EXPECT_TRUE(db_.DoesTableExist("data"))
  96. << "... or if the lower bound is less than the actual version";
  97. MetaTable::RazeIfIncompatible(&db_, MetaTable::kNoLowestSupportedVersion,
  98. /*current_version=*/kVersion);
  99. EXPECT_TRUE(db_.DoesTableExist("data"))
  100. << "... or if the lower bound is not set";
  101. MetaTable::RazeIfIncompatible(&db_, MetaTable::kNoLowestSupportedVersion,
  102. /*current_version=*/kVersion - 1);
  103. EXPECT_TRUE(db_.DoesTableExist("data"))
  104. << "... even if the current version exactly matches the written "
  105. "database's least compatible version.";
  106. }
  107. TEST_F(SQLMetaTableTest, VersionNumber) {
  108. // Compatibility versions one less than the main versions to make
  109. // sure the values aren't being crossed with each other.
  110. constexpr int kVersionFirst = 2;
  111. constexpr int kCompatVersionFirst = kVersionFirst - 1;
  112. constexpr int kVersionSecond = 4;
  113. constexpr int kCompatVersionSecond = kVersionSecond - 1;
  114. constexpr int kVersionThird = 6;
  115. constexpr int kCompatVersionThird = kVersionThird - 1;
  116. // First Init() sets the version info as expected.
  117. {
  118. MetaTable meta_table;
  119. EXPECT_TRUE(meta_table.Init(&db_, kVersionFirst, kCompatVersionFirst));
  120. EXPECT_EQ(kVersionFirst, meta_table.GetVersionNumber());
  121. EXPECT_EQ(kCompatVersionFirst, meta_table.GetCompatibleVersionNumber());
  122. }
  123. // Second Init() does not change the version info.
  124. {
  125. MetaTable meta_table;
  126. EXPECT_TRUE(meta_table.Init(&db_, kVersionSecond, kCompatVersionSecond));
  127. EXPECT_EQ(kVersionFirst, meta_table.GetVersionNumber());
  128. EXPECT_EQ(kCompatVersionFirst, meta_table.GetCompatibleVersionNumber());
  129. meta_table.SetVersionNumber(kVersionSecond);
  130. meta_table.SetCompatibleVersionNumber(kCompatVersionSecond);
  131. }
  132. // Version info from Set*() calls is seen.
  133. {
  134. MetaTable meta_table;
  135. EXPECT_TRUE(meta_table.Init(&db_, kVersionThird, kCompatVersionThird));
  136. EXPECT_EQ(kVersionSecond, meta_table.GetVersionNumber());
  137. EXPECT_EQ(kCompatVersionSecond, meta_table.GetCompatibleVersionNumber());
  138. }
  139. }
  140. TEST_F(SQLMetaTableTest, StringValue) {
  141. static const char kKey[] = "String Key";
  142. const std::string kFirstValue("First Value");
  143. const std::string kSecondValue("Second Value");
  144. // Initially, the value isn't there until set.
  145. {
  146. MetaTable meta_table;
  147. EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
  148. std::string value;
  149. EXPECT_FALSE(meta_table.GetValue(kKey, &value));
  150. EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue));
  151. EXPECT_TRUE(meta_table.GetValue(kKey, &value));
  152. EXPECT_EQ(kFirstValue, value);
  153. }
  154. // Value is persistent across different instances.
  155. {
  156. MetaTable meta_table;
  157. EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
  158. std::string value;
  159. EXPECT_TRUE(meta_table.GetValue(kKey, &value));
  160. EXPECT_EQ(kFirstValue, value);
  161. EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue));
  162. }
  163. // Existing value was successfully changed.
  164. {
  165. MetaTable meta_table;
  166. EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
  167. std::string value;
  168. EXPECT_TRUE(meta_table.GetValue(kKey, &value));
  169. EXPECT_EQ(kSecondValue, value);
  170. }
  171. }
  172. TEST_F(SQLMetaTableTest, IntValue) {
  173. static const char kKey[] = "Int Key";
  174. constexpr int kFirstValue = 17;
  175. constexpr int kSecondValue = 23;
  176. // Initially, the value isn't there until set.
  177. {
  178. MetaTable meta_table;
  179. EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
  180. int value;
  181. EXPECT_FALSE(meta_table.GetValue(kKey, &value));
  182. EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue));
  183. EXPECT_TRUE(meta_table.GetValue(kKey, &value));
  184. EXPECT_EQ(kFirstValue, value);
  185. }
  186. // Value is persistent across different instances.
  187. {
  188. MetaTable meta_table;
  189. EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
  190. int value;
  191. EXPECT_TRUE(meta_table.GetValue(kKey, &value));
  192. EXPECT_EQ(kFirstValue, value);
  193. EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue));
  194. }
  195. // Existing value was successfully changed.
  196. {
  197. MetaTable meta_table;
  198. EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
  199. int value;
  200. EXPECT_TRUE(meta_table.GetValue(kKey, &value));
  201. EXPECT_EQ(kSecondValue, value);
  202. }
  203. }
  204. TEST_F(SQLMetaTableTest, Int64Value) {
  205. static const char kKey[] = "Int Key";
  206. const int64_t kFirstValue = 5000000017LL;
  207. const int64_t kSecondValue = 5000000023LL;
  208. // Initially, the value isn't there until set.
  209. {
  210. MetaTable meta_table;
  211. EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
  212. int64_t value;
  213. EXPECT_FALSE(meta_table.GetValue(kKey, &value));
  214. EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue));
  215. EXPECT_TRUE(meta_table.GetValue(kKey, &value));
  216. EXPECT_EQ(kFirstValue, value);
  217. }
  218. // Value is persistent across different instances.
  219. {
  220. MetaTable meta_table;
  221. EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
  222. int64_t value;
  223. EXPECT_TRUE(meta_table.GetValue(kKey, &value));
  224. EXPECT_EQ(kFirstValue, value);
  225. EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue));
  226. }
  227. // Existing value was successfully changed.
  228. {
  229. MetaTable meta_table;
  230. EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
  231. int64_t value;
  232. EXPECT_TRUE(meta_table.GetValue(kKey, &value));
  233. EXPECT_EQ(kSecondValue, value);
  234. }
  235. }
  236. TEST_F(SQLMetaTableTest, DeleteKey) {
  237. static const char kKey[] = "String Key";
  238. const std::string kValue("String Value");
  239. MetaTable meta_table;
  240. EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
  241. // Value isn't present.
  242. std::string value;
  243. EXPECT_FALSE(meta_table.GetValue(kKey, &value));
  244. // Now value is present.
  245. EXPECT_TRUE(meta_table.SetValue(kKey, kValue));
  246. EXPECT_TRUE(meta_table.GetValue(kKey, &value));
  247. EXPECT_EQ(kValue, value);
  248. // After delete value isn't present.
  249. EXPECT_TRUE(meta_table.DeleteKey(kKey));
  250. EXPECT_FALSE(meta_table.GetValue(kKey, &value));
  251. }
  252. } // namespace
  253. } // namespace sql