| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/scoped_temp_dir.h" | 9 #include "base/scoped_temp_dir.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 void LoadDatabase(const FilePath::StringType& file); | 187 void LoadDatabase(const FilePath::StringType& file); |
| 188 | 188 |
| 189 private: | 189 private: |
| 190 MessageLoopForUI message_loop_for_ui_; | 190 MessageLoopForUI message_loop_for_ui_; |
| 191 content::TestBrowserThread ui_thread_; | 191 content::TestBrowserThread ui_thread_; |
| 192 ScopedTempDir temp_dir_; | 192 ScopedTempDir temp_dir_; |
| 193 | 193 |
| 194 DISALLOW_COPY_AND_ASSIGN(WebDatabaseMigrationTest); | 194 DISALLOW_COPY_AND_ASSIGN(WebDatabaseMigrationTest); |
| 195 }; | 195 }; |
| 196 | 196 |
| 197 const int WebDatabaseMigrationTest::kCurrentTestedVersionNumber = 44; | 197 const int WebDatabaseMigrationTest::kCurrentTestedVersionNumber = 45; |
| 198 | 198 |
| 199 void WebDatabaseMigrationTest::LoadDatabase(const FilePath::StringType& file) { | 199 void WebDatabaseMigrationTest::LoadDatabase(const FilePath::StringType& file) { |
| 200 std::string contents; | 200 std::string contents; |
| 201 ASSERT_TRUE(GetWebDatabaseData(FilePath(file), &contents)); | 201 ASSERT_TRUE(GetWebDatabaseData(FilePath(file), &contents)); |
| 202 | 202 |
| 203 sql::Connection connection; | 203 sql::Connection connection; |
| 204 ASSERT_TRUE(connection.Open(GetDatabasePath())); | 204 ASSERT_TRUE(connection.Open(GetDatabasePath())); |
| 205 ASSERT_TRUE(connection.Execute(contents.data())); | 205 ASSERT_TRUE(connection.Execute(contents.data())); |
| 206 } | 206 } |
| 207 | 207 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 // the database. | 296 // the database. |
| 297 { | 297 { |
| 298 sql::Connection connection; | 298 sql::Connection connection; |
| 299 ASSERT_TRUE(connection.Open(GetDatabasePath())); | 299 ASSERT_TRUE(connection.Open(GetDatabasePath())); |
| 300 | 300 |
| 301 // Columns existing and not existing before current version. | 301 // Columns existing and not existing before current version. |
| 302 ASSERT_TRUE(connection.DoesColumnExist("credit_cards", "unique_id")); | 302 ASSERT_TRUE(connection.DoesColumnExist("credit_cards", "unique_id")); |
| 303 ASSERT_TRUE( | 303 ASSERT_TRUE( |
| 304 connection.DoesColumnExist("credit_cards", "card_number_encrypted")); | 304 connection.DoesColumnExist("credit_cards", "card_number_encrypted")); |
| 305 ASSERT_TRUE(connection.DoesColumnExist("keywords", "id")); | 305 ASSERT_TRUE(connection.DoesColumnExist("keywords", "id")); |
| 306 ASSERT_FALSE(connection.DoesColumnExist("keywords", "logo_id")); | |
| 307 } | 306 } |
| 308 | 307 |
| 309 // Load the database via the WebDatabase class and migrate the database to | 308 // Load the database via the WebDatabase class and migrate the database to |
| 310 // the current version. | 309 // the current version. |
| 311 { | 310 { |
| 312 WebDatabase db; | 311 WebDatabase db; |
| 313 ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath())); | 312 ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath())); |
| 314 } | 313 } |
| 315 | 314 |
| 316 // Verify post-conditions. These are expectations for current version of the | 315 // Verify post-conditions. These are expectations for current version of the |
| 317 // database. | 316 // database. |
| 318 { | 317 { |
| 319 sql::Connection connection; | 318 sql::Connection connection; |
| 320 ASSERT_TRUE(connection.Open(GetDatabasePath())); | 319 ASSERT_TRUE(connection.Open(GetDatabasePath())); |
| 321 | 320 |
| 322 // Check version. | 321 // Check version. |
| 323 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection)); | 322 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection)); |
| 324 | 323 |
| 325 | 324 |
| 326 // Columns existing and not existing before version 25. | 325 // Columns existing and not existing before version 25. |
| 327 EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "unique_id")); | 326 EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "unique_id")); |
| 328 EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "guid")); | 327 EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "guid")); |
| 329 EXPECT_TRUE( | 328 EXPECT_TRUE( |
| 330 connection.DoesColumnExist("credit_cards", "card_number_encrypted")); | 329 connection.DoesColumnExist("credit_cards", "card_number_encrypted")); |
| 331 EXPECT_TRUE(connection.DoesColumnExist("keywords", "id")); | 330 EXPECT_TRUE(connection.DoesColumnExist("keywords", "id")); |
| 332 EXPECT_TRUE(connection.DoesColumnExist("keywords", "logo_id")); | |
| 333 } | |
| 334 } | |
| 335 | |
| 336 // Tests that the |keywords| |logo_id| column gets added to the schema for a | |
| 337 // version 24 database. | |
| 338 TEST_F(WebDatabaseMigrationTest, MigrateVersion24ToCurrent) { | |
| 339 // This schema is taken from a build prior to the addition of the |keywords| | |
| 340 // |logo_id| column. | |
| 341 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_24.sql"))); | |
| 342 | |
| 343 // Verify pre-conditions. These are expectations for version 24 of the | |
| 344 // database. | |
| 345 { | |
| 346 sql::Connection connection; | |
| 347 ASSERT_TRUE(connection.Open(GetDatabasePath())); | |
| 348 | |
| 349 // Columns existing and not existing before current version. | |
| 350 ASSERT_TRUE(connection.DoesColumnExist("keywords", "id")); | |
| 351 ASSERT_FALSE(connection.DoesColumnExist("keywords", "logo_id")); | |
| 352 } | |
| 353 | |
| 354 // Load the database via the WebDatabase class and migrate the database to | |
| 355 // the current version. | |
| 356 { | |
| 357 WebDatabase db; | |
| 358 ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath())); | |
| 359 } | |
| 360 | |
| 361 // Verify post-conditions. These are expectations for current version of the | |
| 362 // database. | |
| 363 { | |
| 364 sql::Connection connection; | |
| 365 ASSERT_TRUE(connection.Open(GetDatabasePath())); | |
| 366 | |
| 367 // Check version. | |
| 368 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection)); | |
| 369 | |
| 370 // |keywords| |logo_id| column should have been added. | |
| 371 EXPECT_TRUE(connection.DoesColumnExist("keywords", "id")); | |
| 372 EXPECT_TRUE(connection.DoesColumnExist("keywords", "logo_id")); | |
| 373 } | 331 } |
| 374 } | 332 } |
| 375 | 333 |
| 376 // Tests that the |keywords| |created_by_policy| column gets added to the schema | 334 // Tests that the |keywords| |created_by_policy| column gets added to the schema |
| 377 // for a version 25 database. | 335 // for a version 25 database. |
| 378 TEST_F(WebDatabaseMigrationTest, MigrateVersion25ToCurrent) { | 336 TEST_F(WebDatabaseMigrationTest, MigrateVersion25ToCurrent) { |
| 379 // This schema is taken from a build prior to the addition of the |keywords| | 337 // This schema is taken from a build prior to the addition of the |keywords| |
| 380 // |created_by_policy| column. | 338 // |created_by_policy| column. |
| 381 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_25.sql"))); | 339 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_25.sql"))); |
| 382 | 340 |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 611 s2.ColumnString(4)); | 569 s2.ColumnString(4)); |
| 612 EXPECT_TRUE(s2.ColumnBool(5)); | 570 EXPECT_TRUE(s2.ColumnBool(5)); |
| 613 EXPECT_EQ(std::string(), s2.ColumnString(6)); | 571 EXPECT_EQ(std::string(), s2.ColumnString(6)); |
| 614 EXPECT_EQ(0, s2.ColumnInt(7)); | 572 EXPECT_EQ(0, s2.ColumnInt(7)); |
| 615 EXPECT_EQ(0, s2.ColumnInt(8)); | 573 EXPECT_EQ(0, s2.ColumnInt(8)); |
| 616 EXPECT_EQ(std::string("UTF-8"), s2.ColumnString(9)); | 574 EXPECT_EQ(std::string("UTF-8"), s2.ColumnString(9)); |
| 617 EXPECT_TRUE(s2.ColumnBool(10)); | 575 EXPECT_TRUE(s2.ColumnBool(10)); |
| 618 EXPECT_EQ(std::string("{google:baseSuggestURL}search?client=chrome&hl=" | 576 EXPECT_EQ(std::string("{google:baseSuggestURL}search?client=chrome&hl=" |
| 619 "{language}&q={searchTerms}"), s2.ColumnString(11)); | 577 "{language}&q={searchTerms}"), s2.ColumnString(11)); |
| 620 EXPECT_EQ(1, s2.ColumnInt(12)); | 578 EXPECT_EQ(1, s2.ColumnInt(12)); |
| 621 EXPECT_TRUE(s2.ColumnBool(13)); | 579 EXPECT_EQ(false, s2.ColumnBool(13)); |
| 622 EXPECT_EQ(6245, s2.ColumnInt(14)); | 580 EXPECT_EQ(std::string(), s2.ColumnString(14)); |
| 623 EXPECT_FALSE(s2.ColumnBool(15)); | 581 EXPECT_EQ(0, s2.ColumnInt(15)); |
| 624 EXPECT_EQ(std::string(), s2.ColumnString(16)); | 582 EXPECT_EQ(std::string(), s2.ColumnString(16)); |
| 625 EXPECT_EQ(0, s2.ColumnInt(17)); | |
| 626 EXPECT_EQ(std::string(), s2.ColumnString(18)); | |
| 627 } | 583 } |
| 628 } | 584 } |
| 629 | 585 |
| 630 // Makes sure date_modified is added correctly to autofill_profiles and | 586 // Makes sure date_modified is added correctly to autofill_profiles and |
| 631 // credit_cards. | 587 // credit_cards. |
| 632 TEST_F(WebDatabaseMigrationTest, MigrateVersion29ToCurrent) { | 588 TEST_F(WebDatabaseMigrationTest, MigrateVersion29ToCurrent) { |
| 633 // Initialize the database. | 589 // Initialize the database. |
| 634 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_29.sql"))); | 590 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_29.sql"))); |
| 635 | 591 |
| 636 // Verify pre-conditions. These are expectations for version 29 of the | 592 // Verify pre-conditions. These are expectations for version 29 of the |
| (...skipping 1201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1838 s.ColumnString(4)); | 1794 s.ColumnString(4)); |
| 1839 EXPECT_TRUE(s.ColumnBool(5)); | 1795 EXPECT_TRUE(s.ColumnBool(5)); |
| 1840 EXPECT_EQ(std::string(), s.ColumnString(6)); | 1796 EXPECT_EQ(std::string(), s.ColumnString(6)); |
| 1841 EXPECT_EQ(0, s.ColumnInt(7)); | 1797 EXPECT_EQ(0, s.ColumnInt(7)); |
| 1842 EXPECT_EQ(0, s.ColumnInt(8)); | 1798 EXPECT_EQ(0, s.ColumnInt(8)); |
| 1843 EXPECT_EQ("UTF-8", s.ColumnString(9)); | 1799 EXPECT_EQ("UTF-8", s.ColumnString(9)); |
| 1844 EXPECT_TRUE(s.ColumnBool(10)); | 1800 EXPECT_TRUE(s.ColumnBool(10)); |
| 1845 EXPECT_EQ("{google:baseSuggestURL}search?client=chrome&hl={language}&" | 1801 EXPECT_EQ("{google:baseSuggestURL}search?client=chrome&hl={language}&" |
| 1846 "q={searchTerms}", s.ColumnString(11)); | 1802 "q={searchTerms}", s.ColumnString(11)); |
| 1847 EXPECT_EQ(1, s.ColumnInt(12)); | 1803 EXPECT_EQ(1, s.ColumnInt(12)); |
| 1848 EXPECT_TRUE(s.ColumnBool(13)); | 1804 EXPECT_EQ(false, s.ColumnBool(13)); |
| 1849 EXPECT_EQ(6262, s.ColumnInt(14)); | |
| 1850 EXPECT_FALSE(s.ColumnBool(15)); | |
| 1851 EXPECT_EQ("{google:baseURL}webhp?{google:RLZ}sourceid=chrome-instant&" | 1805 EXPECT_EQ("{google:baseURL}webhp?{google:RLZ}sourceid=chrome-instant&" |
| 1852 "ie={inputEncoding}&ion=1{searchTerms}&nord=1", | 1806 "ie={inputEncoding}&ion=1{searchTerms}&nord=1", |
| 1853 s.ColumnString(16)); | 1807 s.ColumnString(14)); |
| 1854 EXPECT_EQ(0, s.ColumnInt(17)); | 1808 EXPECT_EQ(0, s.ColumnInt(15)); |
| 1855 EXPECT_EQ("{1234-5678-90AB-CDEF}", s.ColumnString(18)); | 1809 EXPECT_EQ("{1234-5678-90AB-CDEF}", s.ColumnString(16)); |
| 1856 | 1810 |
| 1857 EXPECT_FALSE(s.Step()); | 1811 EXPECT_FALSE(s.Step()); |
| 1858 } | 1812 } |
| 1859 } | 1813 } |
| 1860 | 1814 |
| 1861 // Tests that the default search provider is backed up and signed. | 1815 // Tests that the default search provider is backed up and signed. |
| 1862 TEST_F(WebDatabaseMigrationTest, MigrateVersion43ToCurrent) { | 1816 TEST_F(WebDatabaseMigrationTest, MigrateVersion43ToCurrent) { |
| 1863 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_43.sql"))); | 1817 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_43.sql"))); |
| 1864 | 1818 |
| 1865 int64 previous_default_search_provider_id; | 1819 int64 previous_default_search_provider_id; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1929 &default_search_provider_id_backup)); | 1883 &default_search_provider_id_backup)); |
| 1930 // Backup ID must be updated to match the old default search provider ID. | 1884 // Backup ID must be updated to match the old default search provider ID. |
| 1931 EXPECT_EQ(default_search_provider_id, default_search_provider_id_backup); | 1885 EXPECT_EQ(default_search_provider_id, default_search_provider_id_backup); |
| 1932 | 1886 |
| 1933 std::string default_search_provider_id_backup_signature; | 1887 std::string default_search_provider_id_backup_signature; |
| 1934 EXPECT_TRUE(meta_table.GetValue(KeywordTable::kBackupSignatureKey, | 1888 EXPECT_TRUE(meta_table.GetValue(KeywordTable::kBackupSignatureKey, |
| 1935 &default_search_provider_id_backup_signature)); | 1889 &default_search_provider_id_backup_signature)); |
| 1936 EXPECT_FALSE(default_search_provider_id_backup_signature.empty()); | 1890 EXPECT_FALSE(default_search_provider_id_backup_signature.empty()); |
| 1937 } | 1891 } |
| 1938 } | 1892 } |
| 1893 |
| 1894 #if !defined(GOOGLE_CHROME_BUILD) |
| 1895 // Tests that the |autogenerate_keyword| and |logo_id| columns get removed from |
| 1896 // the keyword table schema for a version 45 database. |
| 1897 // |
| 1898 // This is enabled on Chromium only because a valid signature is required for |
| 1899 // this test, which makes it key-dependent. |
| 1900 TEST_F(WebDatabaseMigrationTest, MigrateVersion44ToCurrent) { |
| 1901 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_44.sql"))); |
| 1902 |
| 1903 // Verify pre-conditions. These are expectations for version 44 of the |
| 1904 // database. |
| 1905 { |
| 1906 sql::Connection connection; |
| 1907 ASSERT_TRUE(connection.Open(GetDatabasePath())); |
| 1908 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection)); |
| 1909 |
| 1910 sql::MetaTable meta_table; |
| 1911 ASSERT_TRUE(meta_table.Init(&connection, 44, 44)); |
| 1912 |
| 1913 ASSERT_TRUE(connection.DoesColumnExist("keywords", "autogenerate_keyword")); |
| 1914 ASSERT_TRUE(connection.DoesColumnExist("keywords", "logo_id")); |
| 1915 } |
| 1916 |
| 1917 // Load the database via the WebDatabase class and migrate the database to |
| 1918 // the current version. |
| 1919 { |
| 1920 WebDatabase db; |
| 1921 ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath())); |
| 1922 ASSERT_FALSE(db.GetKeywordTable()->DidDefaultSearchProviderChange()); |
| 1923 |
| 1924 // The backup table should match the keyword table. |
| 1925 std::string keywords_contents; |
| 1926 EXPECT_TRUE(db.GetKeywordTable()->GetTableContents("keywords", |
| 1927 kCurrentTestedVersionNumber, &keywords_contents)); |
| 1928 std::string keywords_backup_contents; |
| 1929 EXPECT_TRUE(db.GetKeywordTable()->GetTableContents("keywords_backup", |
| 1930 kCurrentTestedVersionNumber, &keywords_backup_contents)); |
| 1931 EXPECT_EQ(keywords_contents, keywords_backup_contents); |
| 1932 } |
| 1933 |
| 1934 // Verify post-conditions. These are expectations for current version of the |
| 1935 // database. |
| 1936 { |
| 1937 sql::Connection connection; |
| 1938 ASSERT_TRUE(connection.Open(GetDatabasePath())); |
| 1939 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection)); |
| 1940 |
| 1941 // Check version. |
| 1942 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection)); |
| 1943 |
| 1944 sql::MetaTable meta_table; |
| 1945 ASSERT_TRUE(meta_table.Init(&connection, kCurrentTestedVersionNumber, |
| 1946 kCurrentTestedVersionNumber)); |
| 1947 |
| 1948 // We should have removed this obsolete key. |
| 1949 std::string default_search_provider_backup; |
| 1950 EXPECT_FALSE(meta_table.GetValue("Default Search Provider Backup", |
| 1951 &default_search_provider_backup)); |
| 1952 |
| 1953 // Two columns should have been removed. |
| 1954 EXPECT_FALSE(connection.DoesColumnExist("keywords", |
| 1955 "autogenerate_keyword")); |
| 1956 EXPECT_FALSE(connection.DoesColumnExist("keywords", "logo_id")); |
| 1957 } |
| 1958 } |
| 1959 #endif // !defined(GOOGLE_CHROME_BUILD) |
| 1960 |
| 1961 // Like MigrateVersion44ToCurrent above, but with a corrupt backup signature. |
| 1962 // This should result in us dropping the backup table but successfully migrating |
| 1963 // otherwise. |
| 1964 // |
| 1965 // Because this test doesn't rely on a valid signature, we can run it on |
| 1966 // official builds as well. |
| 1967 TEST_F(WebDatabaseMigrationTest, MigrateVersion44CorruptBackupToCurrent) { |
| 1968 ASSERT_NO_FATAL_FAILURE( |
| 1969 LoadDatabase(FILE_PATH_LITERAL("version_44_backup_corrupt.sql"))); |
| 1970 |
| 1971 // Verify pre-conditions. These are expectations for version 44 of the |
| 1972 // database. |
| 1973 { |
| 1974 sql::Connection connection; |
| 1975 ASSERT_TRUE(connection.Open(GetDatabasePath())); |
| 1976 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection)); |
| 1977 |
| 1978 sql::MetaTable meta_table; |
| 1979 ASSERT_TRUE(meta_table.Init(&connection, 44, 44)); |
| 1980 |
| 1981 ASSERT_TRUE(connection.DoesColumnExist("keywords", "autogenerate_keyword")); |
| 1982 ASSERT_TRUE(connection.DoesColumnExist("keywords", "logo_id")); |
| 1983 } |
| 1984 |
| 1985 // Load the database via the WebDatabase class and migrate the database to |
| 1986 // the current version. |
| 1987 { |
| 1988 WebDatabase db; |
| 1989 ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath())); |
| 1990 // We should detect a "search provider change" as a side effect of dropping |
| 1991 // the backup table. |
| 1992 ASSERT_TRUE(db.GetKeywordTable()->DidDefaultSearchProviderChange()); |
| 1993 } |
| 1994 |
| 1995 // Verify post-conditions. These are expectations for current version of the |
| 1996 // database. |
| 1997 { |
| 1998 sql::Connection connection; |
| 1999 ASSERT_TRUE(connection.Open(GetDatabasePath())); |
| 2000 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection)); |
| 2001 |
| 2002 // Check version. |
| 2003 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection)); |
| 2004 |
| 2005 sql::MetaTable meta_table; |
| 2006 ASSERT_TRUE(meta_table.Init(&connection, kCurrentTestedVersionNumber, |
| 2007 kCurrentTestedVersionNumber)); |
| 2008 |
| 2009 // We should have removed this obsolete key. |
| 2010 std::string default_search_provider_backup; |
| 2011 EXPECT_FALSE(meta_table.GetValue("Default Search Provider Backup", |
| 2012 &default_search_provider_backup)); |
| 2013 |
| 2014 // Two columns should have been removed. |
| 2015 EXPECT_FALSE(connection.DoesColumnExist("keywords", |
| 2016 "autogenerate_keyword")); |
| 2017 EXPECT_FALSE(connection.DoesColumnExist("keywords", "logo_id")); |
| 2018 |
| 2019 // The backup table should be gone. |
| 2020 EXPECT_FALSE(connection.DoesTableExist("keywords_backup")); |
| 2021 } |
| 2022 } |
| OLD | NEW |