Home | History | Annotate | Download | only in common
      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
      3 // found in the LICENSE file.
      4 
      5 #include <string>
      6 
      7 #include "base/file_util.h"
      8 #include "base/files/scoped_temp_dir.h"
      9 #include "base/guid.h"
     10 #include "base/message_loop/message_loop.h"
     11 #include "base/path_service.h"
     12 #include "base/stl_util.h"
     13 #include "base/strings/string16.h"
     14 #include "base/strings/string_number_conversions.h"
     15 #include "base/strings/utf_string_conversions.h"
     16 #include "base/time/time.h"
     17 #include "base/values.h"
     18 #include "chrome/browser/webdata/keyword_table.h"
     19 #include "chrome/browser/webdata/logins_table.h"
     20 #include "chrome/browser/webdata/token_service_table.h"
     21 #include "chrome/browser/webdata/web_apps_table.h"
     22 #include "chrome/browser/webdata/web_intents_table.h"
     23 #include "components/autofill/core/browser/autofill_country.h"
     24 #include "components/autofill/core/browser/autofill_profile.h"
     25 #include "components/autofill/core/browser/autofill_type.h"
     26 #include "components/autofill/core/browser/credit_card.h"
     27 #include "components/autofill/core/browser/webdata/autofill_change.h"
     28 #include "components/autofill/core/browser/webdata/autofill_entry.h"
     29 #include "components/autofill/core/browser/webdata/autofill_table.h"
     30 #include "components/webdata/common/web_database.h"
     31 #include "sql/statement.h"
     32 #include "testing/gtest/include/gtest/gtest.h"
     33 
     34 using autofill::AutofillProfile;
     35 using autofill::AutofillTable;
     36 using autofill::CreditCard;
     37 using base::Time;
     38 
     39 namespace {
     40 
     41 void AutofillProfile31FromStatement(const sql::Statement& s,
     42                                     AutofillProfile* profile,
     43                                     base::string16* label,
     44                                     int* unique_id,
     45                                     int64* date_modified) {
     46   DCHECK(profile);
     47   DCHECK(label);
     48   DCHECK(unique_id);
     49   DCHECK(date_modified);
     50   *label = s.ColumnString16(0);
     51   *unique_id = s.ColumnInt(1);
     52   profile->SetRawInfo(autofill::NAME_FIRST, s.ColumnString16(2));
     53   profile->SetRawInfo(autofill::NAME_MIDDLE, s.ColumnString16(3));
     54   profile->SetRawInfo(autofill::NAME_LAST, s.ColumnString16(4));
     55   profile->SetRawInfo(autofill::EMAIL_ADDRESS, s.ColumnString16(5));
     56   profile->SetRawInfo(autofill::COMPANY_NAME, s.ColumnString16(6));
     57   profile->SetRawInfo(autofill::ADDRESS_HOME_LINE1, s.ColumnString16(7));
     58   profile->SetRawInfo(autofill::ADDRESS_HOME_LINE2, s.ColumnString16(8));
     59   profile->SetRawInfo(autofill::ADDRESS_HOME_CITY, s.ColumnString16(9));
     60   profile->SetRawInfo(autofill::ADDRESS_HOME_STATE, s.ColumnString16(10));
     61   profile->SetRawInfo(autofill::ADDRESS_HOME_ZIP, s.ColumnString16(11));
     62   profile->SetInfo(
     63       autofill::AutofillType(autofill::ADDRESS_HOME_COUNTRY),
     64       s.ColumnString16(12), "en-US");
     65   profile->SetRawInfo(autofill::PHONE_HOME_WHOLE_NUMBER, s.ColumnString16(13));
     66   *date_modified = s.ColumnInt64(15);
     67   profile->set_guid(s.ColumnString(16));
     68   EXPECT_TRUE(base::IsValidGUID(profile->guid()));
     69 }
     70 
     71 void AutofillProfile33FromStatement(const sql::Statement& s,
     72                                     AutofillProfile* profile,
     73                                     int64* date_modified) {
     74   DCHECK(profile);
     75   DCHECK(date_modified);
     76   profile->set_guid(s.ColumnString(0));
     77   EXPECT_TRUE(base::IsValidGUID(profile->guid()));
     78   profile->SetRawInfo(autofill::COMPANY_NAME, s.ColumnString16(1));
     79   profile->SetRawInfo(autofill::ADDRESS_HOME_LINE1, s.ColumnString16(2));
     80   profile->SetRawInfo(autofill::ADDRESS_HOME_LINE2, s.ColumnString16(3));
     81   profile->SetRawInfo(autofill::ADDRESS_HOME_CITY, s.ColumnString16(4));
     82   profile->SetRawInfo(autofill::ADDRESS_HOME_STATE, s.ColumnString16(5));
     83   profile->SetRawInfo(autofill::ADDRESS_HOME_ZIP, s.ColumnString16(6));
     84   profile->SetInfo(
     85       autofill::AutofillType(autofill::ADDRESS_HOME_COUNTRY),
     86       s.ColumnString16(7), "en-US");
     87   *date_modified = s.ColumnInt64(8);
     88 }
     89 
     90 void CreditCard31FromStatement(const sql::Statement& s,
     91                               CreditCard* credit_card,
     92                               base::string16* label,
     93                               int* unique_id,
     94                               std::string* encrypted_number,
     95                               int64* date_modified) {
     96   DCHECK(credit_card);
     97   DCHECK(label);
     98   DCHECK(unique_id);
     99   DCHECK(encrypted_number);
    100   DCHECK(date_modified);
    101   *label = s.ColumnString16(0);
    102   *unique_id = s.ColumnInt(1);
    103   credit_card->SetRawInfo(autofill::CREDIT_CARD_NAME, s.ColumnString16(2));
    104   credit_card->SetRawInfo(autofill::CREDIT_CARD_TYPE, s.ColumnString16(3));
    105   credit_card->SetRawInfo(autofill::CREDIT_CARD_EXP_MONTH, s.ColumnString16(5));
    106   credit_card->SetRawInfo(
    107       autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR, s.ColumnString16(6));
    108   int encrypted_number_len = s.ColumnByteLength(10);
    109   if (encrypted_number_len) {
    110     encrypted_number->resize(encrypted_number_len);
    111     memcpy(&(*encrypted_number)[0], s.ColumnBlob(10), encrypted_number_len);
    112   }
    113   *date_modified = s.ColumnInt64(12);
    114   credit_card->set_guid(s.ColumnString(13));
    115   EXPECT_TRUE(base::IsValidGUID(credit_card->guid()));
    116 }
    117 
    118 void CreditCard32FromStatement(const sql::Statement& s,
    119                                CreditCard* credit_card,
    120                                std::string* encrypted_number,
    121                                int64* date_modified) {
    122   DCHECK(credit_card);
    123   DCHECK(encrypted_number);
    124   DCHECK(date_modified);
    125   credit_card->set_guid(s.ColumnString(0));
    126   EXPECT_TRUE(base::IsValidGUID(credit_card->guid()));
    127   credit_card->SetRawInfo(autofill::CREDIT_CARD_NAME, s.ColumnString16(1));
    128   credit_card->SetRawInfo(autofill::CREDIT_CARD_EXP_MONTH, s.ColumnString16(2));
    129   credit_card->SetRawInfo(
    130       autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR, s.ColumnString16(3));
    131   int encrypted_number_len = s.ColumnByteLength(4);
    132   if (encrypted_number_len) {
    133     encrypted_number->resize(encrypted_number_len);
    134     memcpy(&(*encrypted_number)[0], s.ColumnBlob(4), encrypted_number_len);
    135   }
    136   *date_modified = s.ColumnInt64(5);
    137 }
    138 
    139 void CheckHasBackupData(sql::MetaTable* meta_table) {
    140   std::string value;
    141   EXPECT_TRUE(meta_table->GetValue(
    142       "Default Search Provider ID Backup", &value));
    143   EXPECT_TRUE(meta_table->GetValue(
    144       "Default Search Provider ID Backup Signature", &value));
    145 }
    146 
    147 void CheckNoBackupData(const sql::Connection& connection,
    148                        sql::MetaTable* meta_table) {
    149   std::string value;
    150   EXPECT_FALSE(meta_table->GetValue(
    151       "Default Search Provider ID Backup", &value));
    152   EXPECT_FALSE(meta_table->GetValue(
    153       "Default Search Provider ID Backup Signature", &value));
    154   EXPECT_FALSE(connection.DoesTableExist("keywords_backup"));
    155 }
    156 
    157 }  // anonymous namespace
    158 
    159 // The WebDatabaseMigrationTest encapsulates testing of database migrations.
    160 // Specifically, these tests are intended to exercise any schema changes in
    161 // the WebDatabase and data migrations that occur in
    162 // |WebDatabase::MigrateOldVersionsAsNeeded()|.
    163 class WebDatabaseMigrationTest : public testing::Test {
    164  public:
    165   WebDatabaseMigrationTest() {}
    166   virtual ~WebDatabaseMigrationTest() {}
    167 
    168   virtual void SetUp() {
    169     ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
    170   }
    171 
    172   // Load the database via the WebDatabase class and migrate the database to
    173   // the current version.
    174   void DoMigration() {
    175     // TODO(joi): This whole unit test file needs to stay in //chrome
    176     // for now, as it needs to know about all the different table
    177     // types. Once all webdata datatypes have been componentized, this
    178     // could move to components_unittests.
    179     AutofillTable autofill_table("en-US");
    180     KeywordTable keyword_table;
    181     LoginsTable logins_table;
    182     TokenServiceTable token_service_table;
    183     WebAppsTable web_apps_table;
    184     WebIntentsTable web_intents_table;
    185 
    186     WebDatabase db;
    187     db.AddTable(&autofill_table);
    188     db.AddTable(&keyword_table);
    189     db.AddTable(&logins_table);
    190     db.AddTable(&token_service_table);
    191     db.AddTable(&web_apps_table);
    192     db.AddTable(&web_intents_table);
    193 
    194     // This causes the migration to occur.
    195     ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath()));
    196   }
    197 
    198  protected:
    199   // Current tested version number.  When adding a migration in
    200   // |WebDatabase::MigrateOldVersionsAsNeeded()| and changing the version number
    201   // |kCurrentVersionNumber| this value should change to reflect the new version
    202   // number and a new migration test added below.
    203   static const int kCurrentTestedVersionNumber;
    204 
    205   base::FilePath GetDatabasePath() {
    206     const base::FilePath::CharType kWebDatabaseFilename[] =
    207         FILE_PATH_LITERAL("TestWebDatabase.sqlite3");
    208     return temp_dir_.path().Append(base::FilePath(kWebDatabaseFilename));
    209   }
    210 
    211   // The textual contents of |file| are read from
    212   // "components/test/data/web_database" and returned in the string |contents|.
    213   // Returns true if the file exists and is read successfully, false otherwise.
    214   bool GetWebDatabaseData(const base::FilePath& file, std::string* contents) {
    215     base::FilePath source_path;
    216     PathService::Get(base::DIR_SOURCE_ROOT, &source_path);
    217     source_path = source_path.AppendASCII("components");
    218     source_path = source_path.AppendASCII("test");
    219     source_path = source_path.AppendASCII("data");
    220     source_path = source_path.AppendASCII("web_database");
    221     source_path = source_path.Append(file);
    222     return base::PathExists(source_path) &&
    223         file_util::ReadFileToString(source_path, contents);
    224   }
    225 
    226   static int VersionFromConnection(sql::Connection* connection) {
    227     // Get version.
    228     sql::Statement s(connection->GetUniqueStatement(
    229         "SELECT value FROM meta WHERE key='version'"));
    230     if (!s.Step())
    231       return 0;
    232     return s.ColumnInt(0);
    233   }
    234 
    235   // The sql files located in "chrome/test/data/web_database" were generated by
    236   // launching the Chromium application prior to schema change, then using the
    237   // sqlite3 command-line application to dump the contents of the "Web Data"
    238   // database.
    239   // Like this:
    240   //   > .output version_nn.sql
    241   //   > .dump
    242   void LoadDatabase(const base::FilePath::StringType& file);
    243 
    244  private:
    245   base::ScopedTempDir temp_dir_;
    246 
    247   DISALLOW_COPY_AND_ASSIGN(WebDatabaseMigrationTest);
    248 };
    249 
    250 const int WebDatabaseMigrationTest::kCurrentTestedVersionNumber = 52;
    251 
    252 void WebDatabaseMigrationTest::LoadDatabase(
    253     const base::FilePath::StringType& file) {
    254   std::string contents;
    255   ASSERT_TRUE(GetWebDatabaseData(base::FilePath(file), &contents));
    256 
    257   sql::Connection connection;
    258   ASSERT_TRUE(connection.Open(GetDatabasePath()));
    259   ASSERT_TRUE(connection.Execute(contents.data()));
    260 }
    261 
    262 // Tests that the all migrations from an empty database succeed.
    263 TEST_F(WebDatabaseMigrationTest, MigrateEmptyToCurrent) {
    264   DoMigration();
    265 
    266   // Verify post-conditions.  These are expectations for current version of the
    267   // database.
    268   {
    269     sql::Connection connection;
    270     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    271 
    272     // Check version.
    273     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
    274 
    275     // Check that expected tables are present.
    276     EXPECT_TRUE(connection.DoesTableExist("autofill"));
    277     EXPECT_TRUE(connection.DoesTableExist("autofill_dates"));
    278     EXPECT_TRUE(connection.DoesTableExist("autofill_profiles"));
    279     EXPECT_TRUE(connection.DoesTableExist("credit_cards"));
    280     EXPECT_TRUE(connection.DoesTableExist("keywords"));
    281     // The logins table is obsolete. (We used to store saved passwords here.)
    282     EXPECT_FALSE(connection.DoesTableExist("logins"));
    283     EXPECT_TRUE(connection.DoesTableExist("meta"));
    284     EXPECT_TRUE(connection.DoesTableExist("token_service"));
    285     EXPECT_TRUE(connection.DoesTableExist("web_app_icons"));
    286     EXPECT_TRUE(connection.DoesTableExist("web_apps"));
    287     EXPECT_TRUE(connection.DoesTableExist("web_intents"));
    288     EXPECT_TRUE(connection.DoesTableExist("web_intents_defaults"));
    289   }
    290 }
    291 
    292 // Tests that the |credit_card| table gets added to the schema for a version 22
    293 // database.
    294 TEST_F(WebDatabaseMigrationTest, MigrateVersion22ToCurrent) {
    295   // This schema is taken from a build prior to the addition of the
    296   // |credit_card| table.  Version 22 of the schema.  Contrast this with the
    297   // corrupt version below.
    298   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_22.sql")));
    299 
    300   // Verify pre-conditions.
    301   {
    302     sql::Connection connection;
    303     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    304 
    305     // No |credit_card| table prior to version 23.
    306     ASSERT_FALSE(connection.DoesColumnExist("credit_cards", "guid"));
    307     ASSERT_FALSE(
    308         connection.DoesColumnExist("credit_cards", "card_number_encrypted"));
    309   }
    310 
    311   DoMigration();
    312 
    313   // Verify post-conditions.  These are expectations for current version of the
    314   // database.
    315   {
    316     sql::Connection connection;
    317     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    318 
    319     // Check version.
    320     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
    321 
    322     // |credit_card| table now exists.
    323     EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "guid"));
    324     EXPECT_TRUE(
    325         connection.DoesColumnExist("credit_cards", "card_number_encrypted"));
    326   }
    327 }
    328 
    329 // Tests that the |credit_card| table gets added to the schema for a corrupt
    330 // version 22 database.  The corruption is that the |credit_cards| table exists
    331 // but the schema version number was not set correctly to 23 or later.  This
    332 // test exercises code introduced to fix bug http://crbug.com/50699 that
    333 // resulted from the corruption.
    334 TEST_F(WebDatabaseMigrationTest, MigrateVersion22CorruptedToCurrent) {
    335   // This schema is taken from a build after the addition of the |credit_card|
    336   // table.  Due to a bug in the migration logic the version is set incorrectly
    337   // to 22 (it should have been updated to 23 at least).
    338   ASSERT_NO_FATAL_FAILURE(
    339       LoadDatabase(FILE_PATH_LITERAL("version_22_corrupt.sql")));
    340 
    341   // Verify pre-conditions.  These are expectations for corrupt version 22 of
    342   // the database.
    343   {
    344     sql::Connection connection;
    345     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    346 
    347     // Columns existing and not existing before current version.
    348     ASSERT_TRUE(connection.DoesColumnExist("credit_cards", "unique_id"));
    349     ASSERT_TRUE(
    350         connection.DoesColumnExist("credit_cards", "card_number_encrypted"));
    351     ASSERT_TRUE(connection.DoesColumnExist("keywords", "id"));
    352   }
    353 
    354   DoMigration();
    355 
    356   // Verify post-conditions.  These are expectations for current version of the
    357   // database.
    358   {
    359     sql::Connection connection;
    360     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    361 
    362     // Check version.
    363     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
    364 
    365 
    366     // Columns existing and not existing before version 25.
    367     EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "unique_id"));
    368     EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "guid"));
    369     EXPECT_TRUE(
    370         connection.DoesColumnExist("credit_cards", "card_number_encrypted"));
    371     EXPECT_TRUE(connection.DoesColumnExist("keywords", "id"));
    372   }
    373 }
    374 
    375 // Tests that the |keywords| |created_by_policy| column gets added to the schema
    376 // for a version 25 database.
    377 TEST_F(WebDatabaseMigrationTest, MigrateVersion25ToCurrent) {
    378   // This schema is taken from a build prior to the addition of the |keywords|
    379   // |created_by_policy| column.
    380   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_25.sql")));
    381 
    382   // Verify pre-conditions.  These are expectations for version 25 of the
    383   // database.
    384   {
    385     sql::Connection connection;
    386     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    387   }
    388 
    389   DoMigration();
    390 
    391   // Verify post-conditions.  These are expectations for current version of the
    392   // database.
    393   {
    394     sql::Connection connection;
    395     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    396 
    397     // Check version.
    398     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
    399 
    400     // |keywords| |created_by_policy| column should have been added.
    401     EXPECT_TRUE(connection.DoesColumnExist("keywords", "id"));
    402     EXPECT_TRUE(connection.DoesColumnExist("keywords", "created_by_policy"));
    403   }
    404 }
    405 
    406 // Tests that the credit_cards.billing_address column is changed from a string
    407 // to an int whilst preserving the associated billing address. This version of
    408 // the test makes sure a stored label is converted to an ID.
    409 TEST_F(WebDatabaseMigrationTest, MigrateVersion26ToCurrentStringLabels) {
    410   // This schema is taken from a build prior to the change of column type for
    411   // credit_cards.billing_address from string to int.
    412   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_26.sql")));
    413 
    414   // Verify pre-conditions. These are expectations for version 26 of the
    415   // database.
    416   {
    417     sql::Connection connection;
    418     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    419 
    420     // Columns existing and not existing before current version.
    421     EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "billing_address"));
    422 
    423     std::string stmt = "INSERT INTO autofill_profiles"
    424       "(label, unique_id, first_name, middle_name, last_name, email,"
    425       " company_name, address_line_1, address_line_2, city, state, zipcode,"
    426       " country, phone, fax)"
    427       "VALUES ('Home',1,'','','','','','','','','','','','','')";
    428     sql::Statement s(connection.GetUniqueStatement(stmt.c_str()));
    429     ASSERT_TRUE(s.Run());
    430 
    431     // Insert a CC linked to an existing address.
    432     std::string stmt2 = "INSERT INTO credit_cards"
    433       "(label, unique_id, name_on_card, type, card_number,"
    434       " expiration_month, expiration_year, verification_code, billing_address,"
    435       " shipping_address, card_number_encrypted, verification_code_encrypted)"
    436       "VALUES ('label',2,'Jack','Visa','1234',2,2012,'','Home','','','')";
    437     sql::Statement s2(connection.GetUniqueStatement(stmt2.c_str()));
    438     ASSERT_TRUE(s2.Run());
    439 
    440     // |billing_address| is a string.
    441     std::string stmt3 = "SELECT billing_address FROM credit_cards";
    442     sql::Statement s3(connection.GetUniqueStatement(stmt3.c_str()));
    443     ASSERT_TRUE(s3.Step());
    444     EXPECT_EQ(s3.ColumnType(0), sql::COLUMN_TYPE_TEXT);
    445   }
    446 
    447   DoMigration();
    448 
    449   // Verify post-conditions.  These are expectations for current version of the
    450   // database.
    451   {
    452     sql::Connection connection;
    453     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    454 
    455     // Check version.
    456     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
    457     EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "billing_address"));
    458 
    459     // Verify the credit card data is converted.
    460     sql::Statement s(connection.GetUniqueStatement(
    461         "SELECT guid, name_on_card, expiration_month, expiration_year, "
    462         "card_number_encrypted, date_modified "
    463         "FROM credit_cards"));
    464     ASSERT_TRUE(s.Step());
    465     EXPECT_EQ("Jack", s.ColumnString(1));
    466     EXPECT_EQ(2, s.ColumnInt(2));
    467     EXPECT_EQ(2012, s.ColumnInt(3));
    468     // Column 5 is encrypted number blob.
    469     // Column 6 is date_modified.
    470   }
    471 }
    472 
    473 // Tests that the credit_cards.billing_address column is changed from a string
    474 // to an int whilst preserving the associated billing address. This version of
    475 // the test makes sure a stored string ID is converted to an integer ID.
    476 TEST_F(WebDatabaseMigrationTest, MigrateVersion26ToCurrentStringIDs) {
    477   // This schema is taken from a build prior to the change of column type for
    478   // credit_cards.billing_address from string to int.
    479   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_26.sql")));
    480 
    481   // Verify pre-conditions. These are expectations for version 26 of the
    482   // database.
    483   {
    484     sql::Connection connection;
    485     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    486     EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "billing_address"));
    487 
    488     std::string stmt = "INSERT INTO autofill_profiles"
    489       "(label, unique_id, first_name, middle_name, last_name, email,"
    490       " company_name, address_line_1, address_line_2, city, state, zipcode,"
    491       " country, phone, fax)"
    492       "VALUES ('Home',1,'','','','','','','','','','','','','')";
    493     sql::Statement s(connection.GetUniqueStatement(stmt.c_str()));
    494     ASSERT_TRUE(s.Run());
    495 
    496     // Insert a CC linked to an existing address.
    497     std::string stmt2 = "INSERT INTO credit_cards"
    498       "(label, unique_id, name_on_card, type, card_number,"
    499       " expiration_month, expiration_year, verification_code, billing_address,"
    500       " shipping_address, card_number_encrypted, verification_code_encrypted)"
    501       "VALUES ('label',2,'Jack','Visa','1234',2,2012,'','1','','','')";
    502     sql::Statement s2(connection.GetUniqueStatement(stmt2.c_str()));
    503     ASSERT_TRUE(s2.Run());
    504 
    505     // |billing_address| is a string.
    506     std::string stmt3 = "SELECT billing_address FROM credit_cards";
    507     sql::Statement s3(connection.GetUniqueStatement(stmt3.c_str()));
    508     ASSERT_TRUE(s3.Step());
    509     EXPECT_EQ(s3.ColumnType(0), sql::COLUMN_TYPE_TEXT);
    510   }
    511 
    512   DoMigration();
    513 
    514   // Verify post-conditions.  These are expectations for current version of the
    515   // database.
    516   {
    517     sql::Connection connection;
    518     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    519 
    520     // Check version.
    521     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
    522 
    523     // |keywords| |created_by_policy| column should have been added.
    524     EXPECT_TRUE(connection.DoesColumnExist("keywords", "id"));
    525     EXPECT_TRUE(connection.DoesColumnExist("keywords", "created_by_policy"));
    526     EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "billing_address"));
    527 
    528     // Verify the credit card data is converted.
    529     sql::Statement s(connection.GetUniqueStatement(
    530         "SELECT guid, name_on_card, expiration_month, expiration_year, "
    531         "card_number_encrypted, date_modified "
    532         "FROM credit_cards"));
    533     ASSERT_TRUE(s.Step());
    534     EXPECT_EQ("Jack", s.ColumnString(1));
    535     EXPECT_EQ(2, s.ColumnInt(2));
    536     EXPECT_EQ(2012, s.ColumnInt(3));
    537     // Column 5 is encrypted credit card number blo b.
    538     // Column 6 is date_modified.
    539   }
    540 }
    541 
    542 // Makes sure instant_url is added correctly to keywords.
    543 TEST_F(WebDatabaseMigrationTest, MigrateVersion27ToCurrent) {
    544   // Initialize the database.
    545   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_27.sql")));
    546 
    547   // Verify pre-conditions. These are expectations for version 27 of the
    548   // database.
    549   {
    550     sql::Connection connection;
    551     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    552 
    553     ASSERT_FALSE(connection.DoesColumnExist("keywords", "instant_url"));
    554   }
    555 
    556   DoMigration();
    557 
    558   // Verify post-conditions.  These are expectations for current version of the
    559   // database.
    560   {
    561     sql::Connection connection;
    562     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    563 
    564     // Check version.
    565     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
    566 
    567     // Make sure supports_instant (added in Version 28) was ultimately dropped
    568     // again and instant_url was added.
    569     EXPECT_FALSE(connection.DoesColumnExist("keywords", "supports_instant"));
    570     EXPECT_TRUE(connection.DoesColumnExist("keywords", "instant_url"));
    571 
    572     // Check that instant_url is empty.
    573     std::string stmt = "SELECT instant_url FROM keywords";
    574     sql::Statement s(connection.GetUniqueStatement(stmt.c_str()));
    575     ASSERT_TRUE(s.Step());
    576     EXPECT_EQ(std::string(), s.ColumnString(0));
    577 
    578     // Verify the data made it over.
    579     stmt = "SELECT " + KeywordTable::GetKeywordColumns() + " FROM keywords";
    580     sql::Statement s2(connection.GetUniqueStatement(stmt.c_str()));
    581     ASSERT_TRUE(s2.Step());
    582     EXPECT_EQ(2, s2.ColumnInt(0));
    583     EXPECT_EQ("Google", s2.ColumnString(1));
    584     EXPECT_EQ("google.com", s2.ColumnString(2));
    585     EXPECT_EQ("http://www.google.com/favicon.ico", s2.ColumnString(3));
    586     EXPECT_EQ("{google:baseURL}search?{google:RLZ}{google:acceptedSuggestion}"\
    587         "{google:originalQueryForSuggestion}sourceid=chrome&ie={inputEncoding}"\
    588         "&q={searchTerms}",
    589         s2.ColumnString(4));
    590     EXPECT_TRUE(s2.ColumnBool(5));
    591     EXPECT_EQ(std::string(), s2.ColumnString(6));
    592     EXPECT_EQ(0, s2.ColumnInt(7));
    593     EXPECT_EQ(0, s2.ColumnInt(8));
    594     EXPECT_EQ(std::string("UTF-8"), s2.ColumnString(9));
    595     EXPECT_TRUE(s2.ColumnBool(10));
    596     EXPECT_EQ(std::string("{google:baseSuggestURL}search?client=chrome&hl="
    597                           "{language}&q={searchTerms}"), s2.ColumnString(11));
    598     EXPECT_EQ(1, s2.ColumnInt(12));
    599     EXPECT_FALSE(s2.ColumnBool(13));
    600     EXPECT_EQ(std::string(), s2.ColumnString(14));
    601     EXPECT_EQ(0, s2.ColumnInt(15));
    602     EXPECT_EQ(std::string(), s2.ColumnString(16));
    603   }
    604 }
    605 
    606 // Makes sure date_modified is added correctly to autofill_profiles and
    607 // credit_cards.
    608 TEST_F(WebDatabaseMigrationTest, MigrateVersion29ToCurrent) {
    609   // Initialize the database.
    610   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_29.sql")));
    611 
    612   // Verify pre-conditions.  These are expectations for version 29 of the
    613   // database.
    614   {
    615     sql::Connection connection;
    616     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    617 
    618     EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles",
    619                                             "date_modified"));
    620     EXPECT_FALSE(connection.DoesColumnExist("credit_cards",
    621                                             "date_modified"));
    622   }
    623 
    624   Time pre_creation_time = Time::Now();
    625   DoMigration();
    626   Time post_creation_time = Time::Now();
    627 
    628   // Verify post-conditions.  These are expectations for current version of the
    629   // database.
    630   {
    631     sql::Connection connection;
    632     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    633 
    634     // Check version.
    635     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
    636 
    637     // Check that the columns were created.
    638     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
    639                                            "date_modified"));
    640     EXPECT_TRUE(connection.DoesColumnExist("credit_cards",
    641                                            "date_modified"));
    642 
    643     sql::Statement s_profiles(connection.GetUniqueStatement(
    644         "SELECT date_modified FROM autofill_profiles "));
    645     ASSERT_TRUE(s_profiles.is_valid());
    646     while (s_profiles.Step()) {
    647       EXPECT_GE(s_profiles.ColumnInt64(0),
    648                 pre_creation_time.ToTimeT());
    649       EXPECT_LE(s_profiles.ColumnInt64(0),
    650                 post_creation_time.ToTimeT());
    651     }
    652     EXPECT_TRUE(s_profiles.Succeeded());
    653 
    654     sql::Statement s_credit_cards(connection.GetUniqueStatement(
    655         "SELECT date_modified FROM credit_cards "));
    656     ASSERT_TRUE(s_credit_cards.is_valid());
    657     while (s_credit_cards.Step()) {
    658       EXPECT_GE(s_credit_cards.ColumnInt64(0),
    659                 pre_creation_time.ToTimeT());
    660       EXPECT_LE(s_credit_cards.ColumnInt64(0),
    661                 post_creation_time.ToTimeT());
    662     }
    663     EXPECT_TRUE(s_credit_cards.Succeeded());
    664   }
    665 }
    666 
    667 // Makes sure guids are added to autofill_profiles and credit_cards tables.
    668 TEST_F(WebDatabaseMigrationTest, MigrateVersion30ToCurrent) {
    669   // Initialize the database.
    670   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_30.sql")));
    671 
    672   // Verify pre-conditions. These are expectations for version 29 of the
    673   // database.
    674   {
    675     sql::Connection connection;
    676     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    677 
    678     EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "guid"));
    679     EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "guid"));
    680   }
    681 
    682   DoMigration();
    683 
    684   // Verify post-conditions.  These are expectations for current version of the
    685   // database.
    686   {
    687     sql::Connection connection;
    688     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    689 
    690     // Check version.
    691     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
    692 
    693     ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid"));
    694     ASSERT_TRUE(connection.DoesColumnExist("credit_cards", "guid"));
    695 
    696     // Check that guids are non-null, non-empty, conforms to guid format, and
    697     // are different.
    698     sql::Statement s(
    699         connection.GetUniqueStatement("SELECT guid FROM autofill_profiles"));
    700 
    701     ASSERT_TRUE(s.Step());
    702     std::string guid1 = s.ColumnString(0);
    703     EXPECT_TRUE(base::IsValidGUID(guid1));
    704 
    705     ASSERT_TRUE(s.Step());
    706     std::string guid2 = s.ColumnString(0);
    707     EXPECT_TRUE(base::IsValidGUID(guid2));
    708 
    709     EXPECT_NE(guid1, guid2);
    710   }
    711 }
    712 
    713 // Removes unique IDs and make GUIDs the primary key.  Also removes unused
    714 // columns.
    715 TEST_F(WebDatabaseMigrationTest, MigrateVersion31ToCurrent) {
    716   // Initialize the database.
    717   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_31.sql")));
    718 
    719   // Verify pre-conditions. These are expectations for version 30 of the
    720   // database.
    721   AutofillProfile profile;
    722   base::string16 profile_label;
    723   int profile_unique_id = 0;
    724   int64 profile_date_modified = 0;
    725   CreditCard credit_card;
    726   base::string16 cc_label;
    727   int cc_unique_id = 0;
    728   std::string cc_number_encrypted;
    729   int64 cc_date_modified = 0;
    730   {
    731     sql::Connection connection;
    732     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    733 
    734     // Verify existence of columns we'll be changing.
    735     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid"));
    736     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "unique_id"));
    737     EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "guid"));
    738     EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "unique_id"));
    739     EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "type"));
    740     EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "card_number"));
    741     EXPECT_TRUE(connection.DoesColumnExist("credit_cards",
    742                                            "verification_code"));
    743     EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "billing_address"));
    744     EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "shipping_address"));
    745     EXPECT_TRUE(connection.DoesColumnExist("credit_cards",
    746                                            "verification_code_encrypted"));
    747 
    748     // Fetch data in the database prior to migration.
    749     sql::Statement s1(
    750         connection.GetUniqueStatement(
    751             "SELECT label, unique_id, first_name, middle_name, last_name, "
    752             "email, company_name, address_line_1, address_line_2, city, state, "
    753             "zipcode, country, phone, fax, date_modified, guid "
    754             "FROM autofill_profiles"));
    755     ASSERT_TRUE(s1.Step());
    756     EXPECT_NO_FATAL_FAILURE(AutofillProfile31FromStatement(
    757         s1, &profile, &profile_label, &profile_unique_id,
    758         &profile_date_modified));
    759 
    760     sql::Statement s2(
    761         connection.GetUniqueStatement(
    762             "SELECT label, unique_id, name_on_card, type, card_number, "
    763             "expiration_month, expiration_year, verification_code, "
    764             "billing_address, shipping_address, card_number_encrypted, "
    765             "verification_code_encrypted, date_modified, guid "
    766             "FROM credit_cards"));
    767     ASSERT_TRUE(s2.Step());
    768     EXPECT_NO_FATAL_FAILURE(CreditCard31FromStatement(s2,
    769                                                       &credit_card,
    770                                                       &cc_label,
    771                                                       &cc_unique_id,
    772                                                       &cc_number_encrypted,
    773                                                       &cc_date_modified));
    774 
    775     EXPECT_NE(profile_unique_id, cc_unique_id);
    776     EXPECT_NE(profile.guid(), credit_card.guid());
    777   }
    778 
    779   DoMigration();
    780 
    781   // Verify post-conditions.  These are expectations for current version of the
    782   // database.
    783   {
    784     sql::Connection connection;
    785     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    786 
    787     // Check version.
    788     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
    789 
    790     // Verify existence of columns we'll be changing.
    791     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid"));
    792     EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "unique_id"));
    793     EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "guid"));
    794     EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "unique_id"));
    795     EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "type"));
    796     EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "card_number"));
    797     EXPECT_FALSE(connection.DoesColumnExist("credit_cards",
    798                                             "verification_code"));
    799     EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "billing_address"));
    800     EXPECT_FALSE(connection.DoesColumnExist("credit_cards",
    801                                             "shipping_address"));
    802     EXPECT_FALSE(connection.DoesColumnExist("credit_cards",
    803                                             "verification_code_encrypted"));
    804 
    805     // Verify data in the database after the migration.
    806     sql::Statement s1(
    807         connection.GetUniqueStatement(
    808             "SELECT guid, company_name, address_line_1, address_line_2, "
    809             "city, state, zipcode, country, date_modified "
    810             "FROM autofill_profiles"));
    811     ASSERT_TRUE(s1.Step());
    812 
    813     AutofillProfile profile_a;
    814     int64 profile_date_modified_a = 0;
    815     EXPECT_NO_FATAL_FAILURE(AutofillProfile33FromStatement(
    816         s1, &profile_a, &profile_date_modified_a));
    817     EXPECT_EQ(profile.guid(), profile_a.guid());
    818     EXPECT_EQ(profile.GetRawInfo(autofill::COMPANY_NAME),
    819               profile_a.GetRawInfo(autofill::COMPANY_NAME));
    820     EXPECT_EQ(profile.GetRawInfo(autofill::ADDRESS_HOME_LINE1),
    821               profile_a.GetRawInfo(autofill::ADDRESS_HOME_LINE1));
    822     EXPECT_EQ(profile.GetRawInfo(autofill::ADDRESS_HOME_LINE2),
    823               profile_a.GetRawInfo(autofill::ADDRESS_HOME_LINE2));
    824     EXPECT_EQ(profile.GetRawInfo(autofill::ADDRESS_HOME_CITY),
    825               profile_a.GetRawInfo(autofill::ADDRESS_HOME_CITY));
    826     EXPECT_EQ(profile.GetRawInfo(autofill::ADDRESS_HOME_STATE),
    827               profile_a.GetRawInfo(autofill::ADDRESS_HOME_STATE));
    828     EXPECT_EQ(profile.GetRawInfo(autofill::ADDRESS_HOME_ZIP),
    829               profile_a.GetRawInfo(autofill::ADDRESS_HOME_ZIP));
    830     EXPECT_EQ(profile.GetRawInfo(autofill::ADDRESS_HOME_COUNTRY),
    831               profile_a.GetRawInfo(autofill::ADDRESS_HOME_COUNTRY));
    832     EXPECT_EQ(profile_date_modified, profile_date_modified_a);
    833 
    834     sql::Statement s2(
    835         connection.GetUniqueStatement(
    836             "SELECT guid, name_on_card, expiration_month, "
    837             "expiration_year, card_number_encrypted, date_modified "
    838             "FROM credit_cards"));
    839     ASSERT_TRUE(s2.Step());
    840 
    841     CreditCard credit_card_a;
    842     base::string16 cc_label_a;
    843     std::string cc_number_encrypted_a;
    844     int64 cc_date_modified_a = 0;
    845     EXPECT_NO_FATAL_FAILURE(CreditCard32FromStatement(s2,
    846                                                       &credit_card_a,
    847                                                       &cc_number_encrypted_a,
    848                                                       &cc_date_modified_a));
    849     EXPECT_EQ(credit_card, credit_card_a);
    850     EXPECT_EQ(cc_label, cc_label_a);
    851     EXPECT_EQ(cc_number_encrypted, cc_number_encrypted_a);
    852     EXPECT_EQ(cc_date_modified, cc_date_modified_a);
    853   }
    854 }
    855 
    856 // Factor |autofill_profiles| address information separately from name, email,
    857 // and phone.
    858 TEST_F(WebDatabaseMigrationTest, MigrateVersion32ToCurrent) {
    859   // Initialize the database.
    860   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_32.sql")));
    861 
    862   // Verify pre-conditions. These are expectations for version 32 of the
    863   // database.
    864   {
    865     sql::Connection connection;
    866     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    867 
    868     // Verify existence of columns we'll be changing.
    869     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid"));
    870     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "label"));
    871     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "first_name"));
    872     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "middle_name"));
    873     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "last_name"));
    874     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "email"));
    875     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
    876                                            "company_name"));
    877     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
    878                                            "address_line_1"));
    879     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
    880                                            "address_line_2"));
    881     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "city"));
    882     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "state"));
    883     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "zipcode"));
    884     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "country"));
    885     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "phone"));
    886     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "fax"));
    887     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
    888                                            "date_modified"));
    889 
    890     EXPECT_FALSE(connection.DoesTableExist("autofill_profile_names"));
    891     EXPECT_FALSE(connection.DoesTableExist("autofill_profile_emails"));
    892     EXPECT_FALSE(connection.DoesTableExist("autofill_profile_phones"));
    893 
    894     EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "label"));
    895   }
    896 
    897   DoMigration();
    898 
    899   // Verify post-conditions.  These are expectations for current version of the
    900   // database.
    901   {
    902     sql::Connection connection;
    903     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    904 
    905     // Check version.
    906     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
    907 
    908     // Verify changes to columns.
    909     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid"));
    910     EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "label"));
    911     EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "first_name"));
    912     EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles",
    913                                             "middle_name"));
    914     EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "last_name"));
    915     EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "email"));
    916     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
    917                                            "company_name"));
    918     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
    919                                            "address_line_1"));
    920     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
    921                                            "address_line_2"));
    922     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "city"));
    923     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "state"));
    924     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "zipcode"));
    925     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "country"));
    926     EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "phone"));
    927     EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "fax"));
    928     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
    929                                            "date_modified"));
    930 
    931     // New "names" table.
    932     EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_names", "guid"));
    933     EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_names",
    934                                            "first_name"));
    935     EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_names",
    936                                            "middle_name"));
    937     EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_names",
    938                                            "last_name"));
    939 
    940     // New "emails" table.
    941     EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_emails", "guid"));
    942     EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_emails", "email"));
    943 
    944     // New "phones" table.
    945     EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_phones", "guid"));
    946     EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_phones", "type"));
    947     EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_phones",
    948                                            "number"));
    949 
    950     EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "label"));
    951 
    952     // Verify data in the database after the migration.
    953     sql::Statement s1(
    954         connection.GetUniqueStatement(
    955             "SELECT guid, company_name, address_line_1, address_line_2, "
    956             "city, state, zipcode, country, date_modified "
    957             "FROM autofill_profiles"));
    958 
    959     // John Doe.
    960     ASSERT_TRUE(s1.Step());
    961     EXPECT_EQ("00580526-FF81-EE2A-0546-1AC593A32E2F", s1.ColumnString(0));
    962     EXPECT_EQ(ASCIIToUTF16("Doe Enterprises"), s1.ColumnString16(1));
    963     EXPECT_EQ(ASCIIToUTF16("1 Main St"), s1.ColumnString16(2));
    964     EXPECT_EQ(ASCIIToUTF16("Apt 1"), s1.ColumnString16(3));
    965     EXPECT_EQ(ASCIIToUTF16("Los Altos"), s1.ColumnString16(4));
    966     EXPECT_EQ(ASCIIToUTF16("CA"), s1.ColumnString16(5));
    967     EXPECT_EQ(ASCIIToUTF16("94022"), s1.ColumnString16(6));
    968     EXPECT_EQ(ASCIIToUTF16("United States"), s1.ColumnString16(7));
    969     EXPECT_EQ(1297882100L, s1.ColumnInt64(8));
    970 
    971     // John P. Doe.
    972     // Gets merged during migration from 35 to 37 due to multi-valued fields.
    973 
    974     // Dave Smith.
    975     ASSERT_TRUE(s1.Step());
    976     EXPECT_EQ("4C74A9D8-7EEE-423E-F9C2-E7FA70ED1396", s1.ColumnString(0));
    977     EXPECT_EQ(base::string16(), s1.ColumnString16(1));
    978     EXPECT_EQ(ASCIIToUTF16("2 Main Street"), s1.ColumnString16(2));
    979     EXPECT_EQ(base::string16(), s1.ColumnString16(3));
    980     EXPECT_EQ(ASCIIToUTF16("Los Altos"), s1.ColumnString16(4));
    981     EXPECT_EQ(ASCIIToUTF16("CA"), s1.ColumnString16(5));
    982     EXPECT_EQ(ASCIIToUTF16("94022"), s1.ColumnString16(6));
    983     EXPECT_EQ(ASCIIToUTF16("United States"), s1.ColumnString16(7));
    984     EXPECT_EQ(1297882100L, s1.ColumnInt64(8));
    985 
    986     // Dave Smith (Part 2).
    987     ASSERT_TRUE(s1.Step());
    988     EXPECT_EQ("722DF5C4-F74A-294A-46F0-31FFDED0D635", s1.ColumnString(0));
    989     EXPECT_EQ(base::string16(), s1.ColumnString16(1));
    990     EXPECT_EQ(ASCIIToUTF16("2 Main St"), s1.ColumnString16(2));
    991     EXPECT_EQ(base::string16(), s1.ColumnString16(3));
    992     EXPECT_EQ(ASCIIToUTF16("Los Altos"), s1.ColumnString16(4));
    993     EXPECT_EQ(ASCIIToUTF16("CA"), s1.ColumnString16(5));
    994     EXPECT_EQ(ASCIIToUTF16("94022"), s1.ColumnString16(6));
    995     EXPECT_EQ(ASCIIToUTF16("United States"), s1.ColumnString16(7));
    996     EXPECT_EQ(1297882100L, s1.ColumnInt64(8));
    997 
    998     // Alfred E Newman.
    999     // Gets culled during migration from 35 to 36 due to incomplete address.
   1000 
   1001     // 3 Main St.
   1002     ASSERT_TRUE(s1.Step());
   1003     EXPECT_EQ("9E5FE298-62C7-83DF-6293-381BC589183F", s1.ColumnString(0));
   1004     EXPECT_EQ(base::string16(), s1.ColumnString16(1));
   1005     EXPECT_EQ(ASCIIToUTF16("3 Main St"), s1.ColumnString16(2));
   1006     EXPECT_EQ(base::string16(), s1.ColumnString16(3));
   1007     EXPECT_EQ(ASCIIToUTF16("Los Altos"), s1.ColumnString16(4));
   1008     EXPECT_EQ(ASCIIToUTF16("CA"), s1.ColumnString16(5));
   1009     EXPECT_EQ(ASCIIToUTF16("94022"), s1.ColumnString16(6));
   1010     EXPECT_EQ(ASCIIToUTF16("United States"), s1.ColumnString16(7));
   1011     EXPECT_EQ(1297882100L, s1.ColumnInt64(8));
   1012 
   1013     // That should be all.
   1014     EXPECT_FALSE(s1.Step());
   1015 
   1016     sql::Statement s2(
   1017         connection.GetUniqueStatement(
   1018             "SELECT guid, first_name, middle_name, last_name "
   1019             "FROM autofill_profile_names"));
   1020 
   1021     // John Doe.
   1022     ASSERT_TRUE(s2.Step());
   1023     EXPECT_EQ("00580526-FF81-EE2A-0546-1AC593A32E2F", s2.ColumnString(0));
   1024     EXPECT_EQ(ASCIIToUTF16("John"), s2.ColumnString16(1));
   1025     EXPECT_EQ(base::string16(), s2.ColumnString16(2));
   1026     EXPECT_EQ(ASCIIToUTF16("Doe"), s2.ColumnString16(3));
   1027 
   1028     // John P. Doe.  Note same guid as above due to merging of multi-valued
   1029     // fields.
   1030     ASSERT_TRUE(s2.Step());
   1031     EXPECT_EQ("00580526-FF81-EE2A-0546-1AC593A32E2F", s2.ColumnString(0));
   1032     EXPECT_EQ(ASCIIToUTF16("John"), s2.ColumnString16(1));
   1033     EXPECT_EQ(ASCIIToUTF16("P."), s2.ColumnString16(2));
   1034     EXPECT_EQ(ASCIIToUTF16("Doe"), s2.ColumnString16(3));
   1035 
   1036     // Dave Smith.
   1037     ASSERT_TRUE(s2.Step());
   1038     EXPECT_EQ("4C74A9D8-7EEE-423E-F9C2-E7FA70ED1396", s2.ColumnString(0));
   1039     EXPECT_EQ(ASCIIToUTF16("Dave"), s2.ColumnString16(1));
   1040     EXPECT_EQ(base::string16(), s2.ColumnString16(2));
   1041     EXPECT_EQ(ASCIIToUTF16("Smith"), s2.ColumnString16(3));
   1042 
   1043     // Dave Smith (Part 2).
   1044     ASSERT_TRUE(s2.Step());
   1045     EXPECT_EQ("722DF5C4-F74A-294A-46F0-31FFDED0D635", s2.ColumnString(0));
   1046     EXPECT_EQ(ASCIIToUTF16("Dave"), s2.ColumnString16(1));
   1047     EXPECT_EQ(base::string16(), s2.ColumnString16(2));
   1048     EXPECT_EQ(ASCIIToUTF16("Smith"), s2.ColumnString16(3));
   1049 
   1050     // Alfred E Newman.
   1051     // Gets culled during migration from 35 to 36 due to incomplete address.
   1052 
   1053     // 3 Main St.
   1054     ASSERT_TRUE(s2.Step());
   1055     EXPECT_EQ("9E5FE298-62C7-83DF-6293-381BC589183F", s2.ColumnString(0));
   1056     EXPECT_EQ(base::string16(), s2.ColumnString16(1));
   1057     EXPECT_EQ(base::string16(), s2.ColumnString16(2));
   1058     EXPECT_EQ(base::string16(), s2.ColumnString16(3));
   1059 
   1060     // Should be all.
   1061     EXPECT_FALSE(s2.Step());
   1062 
   1063     sql::Statement s3(
   1064         connection.GetUniqueStatement(
   1065             "SELECT guid, email "
   1066             "FROM autofill_profile_emails"));
   1067 
   1068     // John Doe.
   1069     ASSERT_TRUE(s3.Step());
   1070     EXPECT_EQ("00580526-FF81-EE2A-0546-1AC593A32E2F", s3.ColumnString(0));
   1071     EXPECT_EQ(ASCIIToUTF16("john (at) doe.com"), s3.ColumnString16(1));
   1072 
   1073     // John P. Doe.
   1074     // Gets culled during migration from 35 to 37 due to merging of John Doe and
   1075     // John P. Doe addresses.
   1076 
   1077     // 2 Main Street.
   1078     ASSERT_TRUE(s3.Step());
   1079     EXPECT_EQ("4C74A9D8-7EEE-423E-F9C2-E7FA70ED1396", s3.ColumnString(0));
   1080     EXPECT_EQ(base::string16(), s3.ColumnString16(1));
   1081 
   1082     // 2 Main St.
   1083     ASSERT_TRUE(s3.Step());
   1084     EXPECT_EQ("722DF5C4-F74A-294A-46F0-31FFDED0D635", s3.ColumnString(0));
   1085     EXPECT_EQ(base::string16(), s3.ColumnString16(1));
   1086 
   1087     // Alfred E Newman.
   1088     // Gets culled during migration from 35 to 36 due to incomplete address.
   1089 
   1090     // 3 Main St.
   1091     ASSERT_TRUE(s3.Step());
   1092     EXPECT_EQ("9E5FE298-62C7-83DF-6293-381BC589183F", s3.ColumnString(0));
   1093     EXPECT_EQ(base::string16(), s3.ColumnString16(1));
   1094 
   1095     // Should be all.
   1096     EXPECT_FALSE(s3.Step());
   1097 
   1098     sql::Statement s4(
   1099         connection.GetUniqueStatement(
   1100             "SELECT guid, type, number "
   1101             "FROM autofill_profile_phones"));
   1102 
   1103     // John Doe phone.
   1104     ASSERT_TRUE(s4.Step());
   1105     EXPECT_EQ("00580526-FF81-EE2A-0546-1AC593A32E2F", s4.ColumnString(0));
   1106     EXPECT_EQ(0, s4.ColumnInt(1));  // 0 means phone.
   1107     EXPECT_EQ(ASCIIToUTF16("4151112222"), s4.ColumnString16(2));
   1108 
   1109     // John Doe fax.
   1110     // Gets culled after fax type removed.
   1111 
   1112     // John P. Doe phone.
   1113     // Gets culled during migration from 35 to 37 due to merging of John Doe and
   1114     // John P. Doe addresses.
   1115 
   1116     // John P. Doe fax.
   1117     // Gets culled during migration from 35 to 37 due to merging of John Doe and
   1118     // John P. Doe addresses.
   1119 
   1120     // 2 Main Street phone.
   1121     ASSERT_TRUE(s4.Step());
   1122     EXPECT_EQ("4C74A9D8-7EEE-423E-F9C2-E7FA70ED1396", s4.ColumnString(0));
   1123     EXPECT_EQ(0, s4.ColumnInt(1));  // 0 means phone.
   1124     EXPECT_EQ(base::string16(), s4.ColumnString16(2));
   1125 
   1126     // 2 Main Street fax.
   1127     // Gets culled after fax type removed.
   1128 
   1129     // 2 Main St phone.
   1130     ASSERT_TRUE(s4.Step());
   1131     EXPECT_EQ("722DF5C4-F74A-294A-46F0-31FFDED0D635", s4.ColumnString(0));
   1132     EXPECT_EQ(0, s4.ColumnInt(1));  // 0 means phone.
   1133     EXPECT_EQ(base::string16(), s4.ColumnString16(2));
   1134 
   1135     // 2 Main St fax.
   1136     // Gets culled after fax type removed.
   1137 
   1138     // Note no phone or fax for Alfred E Newman.
   1139 
   1140     // 3 Main St phone.
   1141     ASSERT_TRUE(s4.Step());
   1142     EXPECT_EQ("9E5FE298-62C7-83DF-6293-381BC589183F", s4.ColumnString(0));
   1143     EXPECT_EQ(0, s4.ColumnInt(1));  // 0 means phone.
   1144     EXPECT_EQ(base::string16(), s4.ColumnString16(2));
   1145 
   1146     // 2 Main St fax.
   1147     // Gets culled after fax type removed.
   1148 
   1149     // Should be all.
   1150     EXPECT_FALSE(s4.Step());
   1151   }
   1152 }
   1153 
   1154 // Adds a column for the autofill profile's country code.
   1155 TEST_F(WebDatabaseMigrationTest, MigrateVersion33ToCurrent) {
   1156   // Initialize the database.
   1157   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_33.sql")));
   1158 
   1159   // Verify pre-conditions. These are expectations for version 33 of the
   1160   // database.
   1161   {
   1162     sql::Connection connection;
   1163     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1164 
   1165     EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles",
   1166                                             "country_code"));
   1167 
   1168     // Check that the country value is the one we expect.
   1169     sql::Statement s(
   1170         connection.GetUniqueStatement("SELECT country FROM autofill_profiles"));
   1171 
   1172     ASSERT_TRUE(s.Step());
   1173     std::string country = s.ColumnString(0);
   1174     EXPECT_EQ("United States", country);
   1175   }
   1176 
   1177   DoMigration();
   1178 
   1179   // Verify post-conditions.  These are expectations for current version of the
   1180   // database.
   1181   {
   1182     sql::Connection connection;
   1183     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1184 
   1185     // Check version.
   1186     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1187 
   1188     ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles",
   1189                                            "country_code"));
   1190 
   1191     // Check that the country code is properly converted.
   1192     sql::Statement s(connection.GetUniqueStatement(
   1193         "SELECT country_code FROM autofill_profiles"));
   1194 
   1195     ASSERT_TRUE(s.Step());
   1196     std::string country_code = s.ColumnString(0);
   1197     EXPECT_EQ("US", country_code);
   1198   }
   1199 }
   1200 
   1201 // Cleans up bad country code "UK" in favor of good country code "GB".
   1202 TEST_F(WebDatabaseMigrationTest, MigrateVersion34ToCurrent) {
   1203   // Initialize the database.
   1204   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_34.sql")));
   1205 
   1206   // Verify pre-conditions. These are expectations for version 34 of the
   1207   // database.
   1208   {
   1209     sql::Connection connection;
   1210     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1211 
   1212     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
   1213                                            "country_code"));
   1214 
   1215     // Check that the country_code value is the one we expect.
   1216     sql::Statement s(
   1217         connection.GetUniqueStatement("SELECT country_code "
   1218                                       "FROM autofill_profiles"));
   1219 
   1220     ASSERT_TRUE(s.Step());
   1221     std::string country_code = s.ColumnString(0);
   1222     EXPECT_EQ("UK", country_code);
   1223 
   1224     // Should have only one.
   1225     ASSERT_FALSE(s.Step());
   1226   }
   1227 
   1228   DoMigration();
   1229 
   1230   // Verify post-conditions.  These are expectations for current version of the
   1231   // database.
   1232   {
   1233     sql::Connection connection;
   1234     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1235 
   1236     // Check version.
   1237     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1238 
   1239     ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles",
   1240                                            "country_code"));
   1241 
   1242     // Check that the country_code code is properly converted.
   1243     sql::Statement s(connection.GetUniqueStatement(
   1244         "SELECT country_code FROM autofill_profiles"));
   1245 
   1246     ASSERT_TRUE(s.Step());
   1247     std::string country_code = s.ColumnString(0);
   1248     EXPECT_EQ("GB", country_code);
   1249 
   1250     // Should have only one.
   1251     ASSERT_FALSE(s.Step());
   1252   }
   1253 }
   1254 
   1255 // Cleans up invalid profiles based on more agressive merging.  Filters out
   1256 // profiles that are subsets of other profiles, and profiles with invalid email,
   1257 // state, and incomplete address.
   1258 TEST_F(WebDatabaseMigrationTest, MigrateVersion35ToCurrent) {
   1259   // Initialize the database.
   1260   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_35.sql")));
   1261 
   1262   // Verify pre-conditions. These are expectations for version 34 of the
   1263   // database.
   1264   {
   1265     sql::Connection connection;
   1266     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1267 
   1268     EXPECT_FALSE(connection.DoesTableExist("autofill_profiles_trash"));
   1269     ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid"));
   1270 
   1271     // Check that there are 6 profiles prior to merge.
   1272     sql::Statement s(
   1273         connection.GetUniqueStatement("SELECT guid FROM autofill_profiles"));
   1274     int i = 0;
   1275     while (s.Step())
   1276       ++i;
   1277     EXPECT_EQ(6, i);
   1278   }
   1279 
   1280   DoMigration();
   1281 
   1282   // Verify post-conditions.  These are expectations for current version of the
   1283   // database.
   1284   {
   1285     sql::Connection connection;
   1286     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1287 
   1288     // Check version.
   1289     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1290 
   1291     ASSERT_TRUE(connection.DoesTableExist("autofill_profiles_trash"));
   1292     ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles_trash", "guid"));
   1293     ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid"));
   1294 
   1295     // Verify data in the database after the migration.
   1296     sql::Statement s1(
   1297         connection.GetUniqueStatement(
   1298             "SELECT guid, company_name, address_line_1, address_line_2, "
   1299             "city, state, zipcode, country, date_modified "
   1300             "FROM autofill_profiles"));
   1301 
   1302     // John Doe.
   1303     ASSERT_TRUE(s1.Step());
   1304     EXPECT_EQ("00000000-0000-0000-0000-000000000001", s1.ColumnString(0));
   1305     EXPECT_EQ(ASCIIToUTF16("Acme Inc."), s1.ColumnString16(1));
   1306     EXPECT_EQ(ASCIIToUTF16("1 Main Street"), s1.ColumnString16(2));
   1307     EXPECT_EQ(ASCIIToUTF16("Apt 2"), s1.ColumnString16(3));
   1308     EXPECT_EQ(ASCIIToUTF16("San Francisco"), s1.ColumnString16(4));
   1309     EXPECT_EQ(ASCIIToUTF16("CA"), s1.ColumnString16(5));
   1310     EXPECT_EQ(ASCIIToUTF16("94102"), s1.ColumnString16(6));
   1311     EXPECT_EQ(ASCIIToUTF16("United States"), s1.ColumnString16(7));
   1312     EXPECT_EQ(1300131704, s1.ColumnInt64(8));
   1313 
   1314     // That should be it.
   1315     ASSERT_FALSE(s1.Step());
   1316 
   1317     // Check that there 5 trashed profile after the merge.
   1318     sql::Statement s2(
   1319         connection.GetUniqueStatement("SELECT guid "
   1320                                       "FROM autofill_profiles_trash"));
   1321     ASSERT_TRUE(s2.Step());
   1322     EXPECT_EQ("00000000-0000-0000-0000-000000000002", s2.ColumnString(0));
   1323 
   1324     ASSERT_TRUE(s2.Step());
   1325     EXPECT_EQ("00000000-0000-0000-0000-000000000003", s2.ColumnString(0));
   1326 
   1327     ASSERT_TRUE(s2.Step());
   1328     EXPECT_EQ("00000000-0000-0000-0000-000000000004", s2.ColumnString(0));
   1329 
   1330     ASSERT_TRUE(s2.Step());
   1331     EXPECT_EQ("00000000-0000-0000-0000-000000000005", s2.ColumnString(0));
   1332 
   1333     ASSERT_TRUE(s2.Step());
   1334     EXPECT_EQ("00000000-0000-0000-0000-000000000006", s2.ColumnString(0));
   1335 
   1336     // That should be it.
   1337     ASSERT_FALSE(s2.Step());
   1338   }
   1339 }
   1340 
   1341 // Tests that the |keywords| |last_modified| column gets added to the schema for
   1342 // a version 37 database.
   1343 TEST_F(WebDatabaseMigrationTest, MigrateVersion37ToCurrent) {
   1344   // This schema is taken from a build prior to the addition of the |keywords|
   1345   // |last_modified| column.
   1346   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_37.sql")));
   1347 
   1348   // Verify pre-conditions.  These are expectations for version 37 of the
   1349   // database.
   1350   {
   1351     sql::Connection connection;
   1352     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1353 
   1354     // Columns existing and not existing before current version.
   1355     ASSERT_TRUE(connection.DoesColumnExist("keywords", "id"));
   1356     ASSERT_FALSE(connection.DoesColumnExist("keywords", "last_modified"));
   1357   }
   1358 
   1359   DoMigration();
   1360 
   1361   // Verify post-conditions.  These are expectations for current version of the
   1362   // database.
   1363   {
   1364     sql::Connection connection;
   1365     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1366 
   1367     // Check version.
   1368     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1369 
   1370     // |keywords| |last_modified| column should have been added.
   1371     EXPECT_TRUE(connection.DoesColumnExist("keywords", "id"));
   1372     EXPECT_TRUE(connection.DoesColumnExist("keywords", "last_modified"));
   1373   }
   1374 }
   1375 
   1376 // Tests that the |keywords| |sync_guid| column gets added to the schema for
   1377 // a version 38 database.
   1378 TEST_F(WebDatabaseMigrationTest, MigrateVersion38ToCurrent) {
   1379   // This schema is taken from a build prior to the addition of the |keywords|
   1380   // |sync_guid| column.
   1381   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_38.sql")));
   1382 
   1383   // Verify pre-conditions.  These are expectations for version 38 of the
   1384   // database.
   1385   {
   1386     sql::Connection connection;
   1387     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1388 
   1389     // Columns existing and not existing before current version.
   1390     ASSERT_TRUE(connection.DoesColumnExist("keywords", "id"));
   1391     ASSERT_FALSE(connection.DoesColumnExist("keywords", "sync_guid"));
   1392   }
   1393 
   1394   DoMigration();
   1395 
   1396   // Verify post-conditions.  These are expectations for current version of the
   1397   // database.
   1398   {
   1399     sql::Connection connection;
   1400     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1401 
   1402     // Check version.
   1403     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1404 
   1405     // |keywords| |sync_guid| column should have been added.
   1406     EXPECT_TRUE(connection.DoesColumnExist("keywords", "id"));
   1407     EXPECT_TRUE(connection.DoesColumnExist("keywords", "sync_guid"));
   1408   }
   1409 }
   1410 
   1411 // Tests that no backup data is added to a version 39 database.
   1412 TEST_F(WebDatabaseMigrationTest, MigrateVersion39ToCurrent) {
   1413   // This schema is taken from a build prior to the addition of the default
   1414   // search provider backup field to the meta table.
   1415   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_39.sql")));
   1416 
   1417   // Verify pre-conditions.  These are expectations for version 39 of the
   1418   // database.
   1419   {
   1420     sql::Connection connection;
   1421     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1422     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1423 
   1424     sql::MetaTable meta_table;
   1425     ASSERT_TRUE(meta_table.Init(&connection, 39, 39));
   1426 
   1427     int64 default_search_provider_id = 0;
   1428     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   1429                                     &default_search_provider_id));
   1430 
   1431     EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
   1432   }
   1433 
   1434   DoMigration();
   1435 
   1436   // Verify post-conditions.  These are expectations for current version of the
   1437   // database.
   1438   {
   1439     sql::Connection connection;
   1440     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1441     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1442 
   1443     // Check version.
   1444     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1445 
   1446     sql::MetaTable meta_table;
   1447     ASSERT_TRUE(meta_table.Init(&connection, kCurrentTestedVersionNumber,
   1448                                 kCurrentTestedVersionNumber));
   1449 
   1450     int64 default_search_provider_id = 0;
   1451     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   1452                                     &default_search_provider_id));
   1453     EXPECT_NE(0, default_search_provider_id);
   1454 
   1455     EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
   1456   }
   1457 }
   1458 
   1459 // Tests that the backup data is removed from the database.
   1460 TEST_F(WebDatabaseMigrationTest, MigrateVersion40ToCurrent) {
   1461   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_40.sql")));
   1462 
   1463   // Verify pre-conditions.  These are expectations for version 40 of the
   1464   // database.
   1465   {
   1466     sql::Connection connection;
   1467     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1468     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1469 
   1470     sql::MetaTable meta_table;
   1471     ASSERT_TRUE(meta_table.Init(&connection, 40, 40));
   1472 
   1473     int64 default_search_provider_id = 0;
   1474     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   1475                                     &default_search_provider_id));
   1476 
   1477     EXPECT_NO_FATAL_FAILURE(CheckHasBackupData(&meta_table));
   1478   }
   1479 
   1480   DoMigration();
   1481 
   1482   // Verify post-conditions.  These are expectations for current version of the
   1483   // database.
   1484   {
   1485     sql::Connection connection;
   1486     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1487     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1488 
   1489     // Check version.
   1490     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1491 
   1492     sql::MetaTable meta_table;
   1493     ASSERT_TRUE(meta_table.Init(&connection, kCurrentTestedVersionNumber,
   1494                                 kCurrentTestedVersionNumber));
   1495 
   1496     int64 default_search_provider_id = 0;
   1497     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   1498                                     &default_search_provider_id));
   1499     EXPECT_NE(0, default_search_provider_id);
   1500 
   1501     EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
   1502   }
   1503 }
   1504 
   1505 // Tests that the backup data is removed from the database.
   1506 TEST_F(WebDatabaseMigrationTest, MigrateVersion41ToCurrent) {
   1507   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_41.sql")));
   1508 
   1509   // Verify pre-conditions.  These are expectations for version 41 of the
   1510   // database.
   1511   {
   1512     sql::Connection connection;
   1513     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1514     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1515 
   1516     sql::MetaTable meta_table;
   1517     ASSERT_TRUE(meta_table.Init(&connection, 41, 41));
   1518 
   1519     int64 default_search_provider_id = 0;
   1520     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   1521                                     &default_search_provider_id));
   1522 
   1523     EXPECT_NO_FATAL_FAILURE(CheckHasBackupData(&meta_table));
   1524   }
   1525 
   1526   DoMigration();
   1527 
   1528   // Verify post-conditions.  These are expectations for current version of the
   1529   // database.
   1530   {
   1531     sql::Connection connection;
   1532     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1533     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1534 
   1535     // Check version.
   1536     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1537 
   1538     sql::MetaTable meta_table;
   1539     ASSERT_TRUE(meta_table.Init(&connection, kCurrentTestedVersionNumber,
   1540                                 kCurrentTestedVersionNumber));
   1541 
   1542     int64 default_search_provider_id = 0;
   1543     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   1544                                     &default_search_provider_id));
   1545     EXPECT_NE(0, default_search_provider_id);
   1546 
   1547     EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
   1548   }
   1549 }
   1550 
   1551 // Tests that the backup data is removed from the database.
   1552 TEST_F(WebDatabaseMigrationTest, MigrateVersion42ToCurrent) {
   1553   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_42.sql")));
   1554 
   1555   // Verify pre-conditions.  These are expectations for version 42 of the
   1556   // database.
   1557   {
   1558     sql::Connection connection;
   1559     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1560     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1561 
   1562     sql::MetaTable meta_table;
   1563     ASSERT_TRUE(meta_table.Init(&connection, 42, 42));
   1564 
   1565     int64 default_search_provider_id = 0;
   1566     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   1567                                     &default_search_provider_id));
   1568 
   1569     EXPECT_NO_FATAL_FAILURE(CheckHasBackupData(&meta_table));
   1570 
   1571     EXPECT_FALSE(connection.DoesTableExist("keywords_backup"));
   1572   }
   1573 
   1574   DoMigration();
   1575 
   1576   // Verify post-conditions.  These are expectations for current version of the
   1577   // database.
   1578   {
   1579     sql::Connection connection;
   1580     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1581     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1582 
   1583     // Check version.
   1584     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1585 
   1586     sql::MetaTable meta_table;
   1587     ASSERT_TRUE(meta_table.Init(&connection, kCurrentTestedVersionNumber,
   1588                                 kCurrentTestedVersionNumber));
   1589 
   1590     int64 default_search_provider_id = 0;
   1591     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   1592                                     &default_search_provider_id));
   1593     EXPECT_NE(0, default_search_provider_id);
   1594 
   1595     EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
   1596   }
   1597 }
   1598 
   1599 // Tests that the backup data is removed from the database.
   1600 TEST_F(WebDatabaseMigrationTest, MigrateVersion43ToCurrent) {
   1601   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_43.sql")));
   1602 
   1603   int64 previous_default_search_provider_id;
   1604 
   1605   // Verify pre-conditions.  These are expectations for version 43 of the
   1606   // database.
   1607   {
   1608     sql::Connection connection;
   1609     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1610     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1611 
   1612     sql::MetaTable meta_table;
   1613     ASSERT_TRUE(meta_table.Init(&connection, 43, 43));
   1614 
   1615     int64 default_search_provider_id = 0;
   1616     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   1617                                     &default_search_provider_id));
   1618     EXPECT_NE(default_search_provider_id, 0);
   1619     previous_default_search_provider_id = default_search_provider_id;
   1620 
   1621     EXPECT_NO_FATAL_FAILURE(CheckHasBackupData(&meta_table));
   1622     EXPECT_TRUE(connection.DoesTableExist("keywords_backup"));
   1623   }
   1624 
   1625   DoMigration();
   1626 
   1627   // Verify post-conditions.  These are expectations for current version of the
   1628   // database.
   1629   {
   1630     sql::Connection connection;
   1631     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1632     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1633 
   1634     // Check version.
   1635     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1636 
   1637     sql::MetaTable meta_table;
   1638     ASSERT_TRUE(meta_table.Init(
   1639         &connection,
   1640         kCurrentTestedVersionNumber,
   1641         kCurrentTestedVersionNumber));
   1642 
   1643     int64 default_search_provider_id = 0;
   1644     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   1645                                     &default_search_provider_id));
   1646     // Default search provider ID should not change.
   1647     EXPECT_EQ(previous_default_search_provider_id, default_search_provider_id);
   1648 
   1649     EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
   1650   }
   1651 }
   1652 
   1653 // Tests that the |autogenerate_keyword| and |logo_id| columns get removed from
   1654 // the keyword table schema for a version 45 database.
   1655 TEST_F(WebDatabaseMigrationTest, MigrateVersion44ToCurrent) {
   1656   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_44.sql")));
   1657 
   1658   // Verify pre-conditions.  These are expectations for version 44 of the
   1659   // database.
   1660   {
   1661     sql::Connection connection;
   1662     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1663     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1664 
   1665     sql::MetaTable meta_table;
   1666     ASSERT_TRUE(meta_table.Init(&connection, 44, 44));
   1667 
   1668     ASSERT_TRUE(connection.DoesColumnExist("keywords", "autogenerate_keyword"));
   1669     ASSERT_TRUE(connection.DoesColumnExist("keywords", "logo_id"));
   1670   }
   1671 
   1672   DoMigration();
   1673 
   1674   // Verify post-conditions.  These are expectations for current version of the
   1675   // database.
   1676   {
   1677     sql::Connection connection;
   1678     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1679     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1680 
   1681     // Check version.
   1682     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1683 
   1684     sql::MetaTable meta_table;
   1685     ASSERT_TRUE(meta_table.Init(&connection, kCurrentTestedVersionNumber,
   1686                                 kCurrentTestedVersionNumber));
   1687 
   1688     // We should have removed this obsolete key.
   1689     std::string default_search_provider_backup;
   1690     EXPECT_FALSE(meta_table.GetValue("Default Search Provider Backup",
   1691                                      &default_search_provider_backup));
   1692 
   1693     // Two columns should have been removed.
   1694     EXPECT_FALSE(connection.DoesColumnExist("keywords",
   1695                                             "autogenerate_keyword"));
   1696     EXPECT_FALSE(connection.DoesColumnExist("keywords", "logo_id"));
   1697 
   1698     // Backup data should have been removed.
   1699     EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
   1700   }
   1701 }
   1702 
   1703 // Tests that the web_intents and web_intents_defaults tables are
   1704 // modified to include "scheme" columns.
   1705 TEST_F(WebDatabaseMigrationTest, MigrateVersion45ToCurrent) {
   1706   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_45.sql")));
   1707 
   1708   // Verify pre-conditions.  These are expectations for version 45 of the
   1709   // database.
   1710   {
   1711     sql::Connection connection;
   1712     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1713     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1714 
   1715     sql::MetaTable meta_table;
   1716     ASSERT_TRUE(meta_table.Init(&connection, 45, 45));
   1717 
   1718     ASSERT_FALSE(connection.DoesColumnExist("scheme", "web_intents"));
   1719     ASSERT_FALSE(connection.DoesColumnExist(
   1720         "scheme", "web_intents_defaults"));
   1721   }
   1722 
   1723   DoMigration();
   1724 
   1725   // Verify post-conditions.  These are expectations for current version of the
   1726   // database.
   1727   {
   1728     sql::Connection connection;
   1729     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1730     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1731 
   1732     // Check version.
   1733     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1734 
   1735     sql::MetaTable meta_table;
   1736     ASSERT_TRUE(meta_table.Init(
   1737         &connection,
   1738         kCurrentTestedVersionNumber,
   1739         kCurrentTestedVersionNumber));
   1740 
   1741     // A new "scheme" column should have been added to each web_intents table.
   1742     EXPECT_TRUE(connection.DoesColumnExist("web_intents", "scheme"));
   1743     EXPECT_TRUE(connection.DoesColumnExist("web_intents_defaults", "scheme"));
   1744 
   1745     // Verify existing user data was copied.
   1746     sql::Statement s1(
   1747         connection.GetUniqueStatement("SELECT * FROM web_intents"));
   1748 
   1749     ASSERT_TRUE(s1.Step());
   1750     EXPECT_EQ("http://poodles.com/fuzzer", s1.ColumnString(0));
   1751     EXPECT_EQ(ASCIIToUTF16("fuzz"), s1.ColumnString16(1));
   1752     EXPECT_EQ(ASCIIToUTF16("poodle/*"), s1.ColumnString16(2));
   1753     EXPECT_EQ(ASCIIToUTF16("Poodle Fuzzer"), s1.ColumnString16(3));
   1754     EXPECT_EQ(ASCIIToUTF16("window"), s1.ColumnString16(4));
   1755     EXPECT_EQ(ASCIIToUTF16(""), s1.ColumnString16(5));
   1756     ASSERT_FALSE(s1.Step());
   1757 
   1758     // Now we want to verify existing user data was copied
   1759     sql::Statement s2(
   1760         connection.GetUniqueStatement("SELECT * FROM web_intents_defaults"));
   1761 
   1762     ASSERT_TRUE(s2.Step());
   1763     EXPECT_EQ("fuzz", s2.ColumnString(0));
   1764     EXPECT_EQ(ASCIIToUTF16("poodle/*"), s2.ColumnString16(1));
   1765     EXPECT_EQ(ASCIIToUTF16(""), s2.ColumnString16(2));
   1766     EXPECT_EQ(0, s2.ColumnInt(3));
   1767     EXPECT_EQ(0, s2.ColumnInt(4));
   1768     EXPECT_EQ(ASCIIToUTF16("http://poodles.com/fuzzer"), s2.ColumnString16(5));
   1769     EXPECT_EQ(ASCIIToUTF16(""), s2.ColumnString16(6));
   1770     ASSERT_FALSE(s2.Step());
   1771 
   1772     // finally ensure the migration code cleaned up after itself
   1773     EXPECT_FALSE(connection.DoesTableExist("old_web_intents"));
   1774     EXPECT_FALSE(connection.DoesTableExist("old_web_intents_defaults"));
   1775   }
   1776 }
   1777 
   1778 // Tests that the web_intents and web_intents_defaults tables are
   1779 // modified to include "scheme" columns.
   1780 TEST_F(WebDatabaseMigrationTest, MigrateVersion45InvalidToCurrent) {
   1781   ASSERT_NO_FATAL_FAILURE(
   1782       LoadDatabase(FILE_PATH_LITERAL("version_45_invalid.sql")));
   1783 
   1784   // Verify pre-conditions.  These are expectations for version 45 of the
   1785   // database.
   1786   {
   1787     sql::Connection connection;
   1788     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1789     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1790 
   1791     sql::MetaTable meta_table;
   1792     ASSERT_TRUE(meta_table.Init(&connection, 45, 45));
   1793 
   1794     ASSERT_FALSE(connection.DoesColumnExist("scheme", "web_intents"));
   1795     ASSERT_FALSE(connection.DoesColumnExist(
   1796         "scheme", "web_intents_defaults"));
   1797   }
   1798 
   1799   DoMigration();
   1800 
   1801   // Verify post-conditions.  These are expectations for current version of the
   1802   // database.
   1803   {
   1804     sql::Connection connection;
   1805     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1806     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1807 
   1808     // Check version.
   1809     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1810 
   1811     sql::MetaTable meta_table;
   1812     ASSERT_TRUE(meta_table.Init(
   1813         &connection,
   1814         kCurrentTestedVersionNumber,
   1815         kCurrentTestedVersionNumber));
   1816 
   1817     // A new "scheme" column should have been added to each web_intents table.
   1818     EXPECT_TRUE(connection.DoesColumnExist("web_intents", "scheme"));
   1819     EXPECT_TRUE(connection.DoesColumnExist("web_intents_defaults", "scheme"));
   1820 
   1821     // Verify existing user data was copied.
   1822     sql::Statement s1(
   1823         connection.GetUniqueStatement("SELECT * FROM web_intents"));
   1824 
   1825     ASSERT_FALSE(s1.Step());  // Basically should be empty at this point.
   1826 
   1827     // Now we want to verify existing user data was copied
   1828     sql::Statement s2(
   1829         connection.GetUniqueStatement("SELECT * FROM web_intents_defaults"));
   1830 
   1831     // We were able to create the new tables, but unable to copy any data
   1832     // Given the initial bad state of the tables.
   1833     ASSERT_FALSE(s2.Step());
   1834 
   1835     // Finally ensure the migration code cleaned up after itself.
   1836     EXPECT_FALSE(connection.DoesTableExist("old_web_intents"));
   1837     EXPECT_FALSE(connection.DoesTableExist("old_web_intents_defaults"));
   1838   }
   1839 }
   1840 
   1841 // Check that current version is forced to compatible version before migration,
   1842 // if the former is smaller.
   1843 TEST_F(WebDatabaseMigrationTest, MigrateVersion45CompatibleToCurrent) {
   1844   ASSERT_NO_FATAL_FAILURE(
   1845       LoadDatabase(FILE_PATH_LITERAL("version_45_compatible.sql")));
   1846 
   1847   // Verify pre-conditions.  These are expectations for version 45 of the
   1848   // database.
   1849   {
   1850     sql::Connection connection;
   1851     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1852     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1853 
   1854     sql::MetaTable meta_table;
   1855     // Database is actually version 45 but the version field states 40.
   1856     ASSERT_TRUE(meta_table.Init(&connection, 40, 45));
   1857   }
   1858 
   1859   DoMigration();
   1860 
   1861   // Verify post-conditions.  These are expectations for current version of the
   1862   // database.
   1863   {
   1864     sql::Connection connection;
   1865     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1866     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1867 
   1868     // Check version.
   1869     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1870     EXPECT_LE(45, VersionFromConnection(&connection));
   1871   }
   1872 }
   1873 
   1874 // Tests that the |alternate_urls| column is added to the keyword table schema
   1875 // for a version 47 database.
   1876 TEST_F(WebDatabaseMigrationTest, MigrateVersion46ToCurrent) {
   1877   ASSERT_NO_FATAL_FAILURE(
   1878       LoadDatabase(FILE_PATH_LITERAL("version_46.sql")));
   1879 
   1880   // Verify pre-conditions.  These are expectations for version 46 of the
   1881   // database.
   1882   {
   1883     sql::Connection connection;
   1884     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1885     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1886 
   1887     sql::MetaTable meta_table;
   1888     ASSERT_TRUE(meta_table.Init(&connection, 46, 46));
   1889 
   1890     ASSERT_FALSE(connection.DoesColumnExist("keywords", "alternate_urls"));
   1891     ASSERT_FALSE(connection.DoesColumnExist("keywords_backup",
   1892                                             "alternate_urls"));
   1893   }
   1894 
   1895   DoMigration();
   1896 
   1897   // Verify post-conditions.  These are expectations for current version of the
   1898   // database.
   1899   {
   1900     sql::Connection connection;
   1901     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1902     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1903 
   1904     // Check version.
   1905     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1906 
   1907     // A new column should have been created.
   1908     EXPECT_TRUE(connection.DoesColumnExist("keywords", "alternate_urls"));
   1909   }
   1910 }
   1911 
   1912 // Tests that the backup data is removed from the database.
   1913 TEST_F(WebDatabaseMigrationTest, MigrateVersion47ToCurrent) {
   1914   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_47.sql")));
   1915 
   1916   // Verify pre-conditions.  These are expectations for version 47 of the
   1917   // database.
   1918   {
   1919     sql::Connection connection;
   1920     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1921     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1922 
   1923     sql::MetaTable meta_table;
   1924     ASSERT_TRUE(meta_table.Init(&connection, 47, 47));
   1925 
   1926     int64 default_search_provider_id = 0;
   1927     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   1928                                     &default_search_provider_id));
   1929     EXPECT_NE(0, default_search_provider_id);
   1930 
   1931     EXPECT_NO_FATAL_FAILURE(CheckHasBackupData(&meta_table));
   1932     EXPECT_TRUE(connection.DoesTableExist("keywords_backup"));
   1933   }
   1934 
   1935   DoMigration();
   1936 
   1937   // Verify post-conditions.  These are expectations for current version of the
   1938   // database.
   1939   {
   1940     sql::Connection connection;
   1941     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1942     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1943 
   1944     // Check version.
   1945     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1946 
   1947     sql::MetaTable meta_table;
   1948     ASSERT_TRUE(meta_table.Init(&connection, kCurrentTestedVersionNumber,
   1949                                 kCurrentTestedVersionNumber));
   1950 
   1951     int64 default_search_provider_id = 0;
   1952     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   1953                                     &default_search_provider_id));
   1954     EXPECT_NE(0, default_search_provider_id);
   1955 
   1956     EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
   1957   }
   1958 }
   1959 
   1960 // Tests that the |search_terms_replacement_key| column is added to the keyword
   1961 // table schema for a version 49 database.
   1962 TEST_F(WebDatabaseMigrationTest, MigrateVersion48ToCurrent) {
   1963   ASSERT_NO_FATAL_FAILURE(
   1964       LoadDatabase(FILE_PATH_LITERAL("version_48.sql")));
   1965 
   1966   // Verify pre-conditions.  These are expectations for version 48 of the
   1967   // database.
   1968   {
   1969     sql::Connection connection;
   1970     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1971     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1972 
   1973     sql::MetaTable meta_table;
   1974     ASSERT_TRUE(meta_table.Init(&connection, 48, 48));
   1975 
   1976     ASSERT_FALSE(connection.DoesColumnExist("keywords",
   1977                                             "search_terms_replacement_key"));
   1978   }
   1979 
   1980   DoMigration();
   1981 
   1982   // Verify post-conditions.  These are expectations for current version of the
   1983   // database.
   1984   {
   1985     sql::Connection connection;
   1986     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1987     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1988 
   1989     // Check version.
   1990     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1991 
   1992     // A new column should have been created.
   1993     EXPECT_TRUE(connection.DoesColumnExist("keywords",
   1994                                            "search_terms_replacement_key"));
   1995   }
   1996 }
   1997 
   1998 // Tests that the |origin| column is added to the autofill_profiles and
   1999 // credit_cards table schemas for a version 50 database.
   2000 TEST_F(WebDatabaseMigrationTest, MigrateVersion49ToCurrent) {
   2001   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_49.sql")));
   2002 
   2003   // Verify pre-conditions.  These are expectations for version 49 of the
   2004   // database.
   2005   {
   2006     sql::Connection connection;
   2007     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   2008 
   2009     ASSERT_FALSE(connection.DoesColumnExist("autofill_profiles", "origin"));
   2010     ASSERT_FALSE(connection.DoesColumnExist("credit_cards", "origin"));
   2011   }
   2012 
   2013   DoMigration();
   2014 
   2015   // Verify post-conditions.  These are expectations for current version of the
   2016   // database.
   2017   {
   2018     sql::Connection connection;
   2019     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   2020     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   2021 
   2022     // Check version.
   2023     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   2024 
   2025     // A new column should have been created in both tables.
   2026     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "origin"));
   2027     EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "origin"));
   2028   }
   2029 }
   2030 
   2031 // Tests that the columns |image_url|, |search_url_post_params|,
   2032 // |suggest_url_post_params|, |instant_url_post_params|, |image_url_post_params|
   2033 // are added to the keyword table schema for a version 52 database.
   2034 TEST_F(WebDatabaseMigrationTest, MigrateVersion50ToCurrent) {
   2035   ASSERT_NO_FATAL_FAILURE(
   2036       LoadDatabase(FILE_PATH_LITERAL("version_50.sql")));
   2037 
   2038   // Verify pre-conditions.  These are expectations for version 50 of the
   2039   // database.
   2040   {
   2041     sql::Connection connection;
   2042     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   2043     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   2044 
   2045     sql::MetaTable meta_table;
   2046     ASSERT_TRUE(meta_table.Init(&connection, 50, 50));
   2047 
   2048     ASSERT_FALSE(connection.DoesColumnExist("keywords", "image_url"));
   2049     ASSERT_FALSE(connection.DoesColumnExist("keywords",
   2050                                             "search_url_post_params"));
   2051     ASSERT_FALSE(connection.DoesColumnExist("keywords",
   2052                                             "suggest_url_post_params"));
   2053     ASSERT_FALSE(connection.DoesColumnExist("keywords",
   2054                                             "instant_url_post_params"));
   2055     ASSERT_FALSE(connection.DoesColumnExist("keywords",
   2056                                             "image_url_post_params"));
   2057   }
   2058 
   2059   DoMigration();
   2060 
   2061   // Verify post-conditions.  These are expectations for current version of the
   2062   // database.
   2063   {
   2064     sql::Connection connection;
   2065     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   2066     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   2067 
   2068     // Check version.
   2069     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   2070 
   2071     // New columns should have been created.
   2072     EXPECT_TRUE(connection.DoesColumnExist("keywords", "image_url"));
   2073     EXPECT_TRUE(connection.DoesColumnExist("keywords",
   2074                                            "search_url_post_params"));
   2075     EXPECT_TRUE(connection.DoesColumnExist("keywords",
   2076                                            "suggest_url_post_params"));
   2077     EXPECT_TRUE(connection.DoesColumnExist("keywords",
   2078                                            "instant_url_post_params"));
   2079     EXPECT_TRUE(connection.DoesColumnExist("keywords",
   2080                                            "image_url_post_params"));
   2081   }
   2082 }
   2083