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_STREET_ADDRESS,
     80                       s.ColumnString16(2));
     81   profile->SetRawInfo(autofill::ADDRESS_HOME_CITY, s.ColumnString16(3));
     82   profile->SetRawInfo(autofill::ADDRESS_HOME_STATE, s.ColumnString16(4));
     83   profile->SetRawInfo(autofill::ADDRESS_HOME_ZIP, s.ColumnString16(5));
     84   profile->SetInfo(
     85       autofill::AutofillType(autofill::ADDRESS_HOME_COUNTRY),
     86       s.ColumnString16(6), "en-US");
     87   *date_modified = s.ColumnInt64(7);
     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         base::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 = 54;
    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, street_address, city, state, zipcode,"
    809             " country_code, 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                                            "street_address"));
    920     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "city"));
    921     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "state"));
    922     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "zipcode"));
    923     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
    924                                            "country_code"));
    925     EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "phone"));
    926     EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "fax"));
    927     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
    928                                            "date_modified"));
    929 
    930     // New "names" table.
    931     EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_names", "guid"));
    932     EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_names",
    933                                            "first_name"));
    934     EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_names",
    935                                            "middle_name"));
    936     EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_names",
    937                                            "last_name"));
    938 
    939     // New "emails" table.
    940     EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_emails", "guid"));
    941     EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_emails", "email"));
    942 
    943     // New "phones" table.
    944     EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_phones", "guid"));
    945     EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_phones",
    946                                            "number"));
    947 
    948     EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "label"));
    949 
    950     // Verify data in the database after the migration.
    951     sql::Statement s1(
    952         connection.GetUniqueStatement(
    953             "SELECT guid, company_name, street_address, city, state, zipcode, "
    954             " country_code, date_modified "
    955             "FROM autofill_profiles"));
    956 
    957     // John Doe.
    958     ASSERT_TRUE(s1.Step());
    959     EXPECT_EQ("00580526-FF81-EE2A-0546-1AC593A32E2F", s1.ColumnString(0));
    960     EXPECT_EQ(ASCIIToUTF16("Doe Enterprises"), s1.ColumnString16(1));
    961     EXPECT_EQ(ASCIIToUTF16("1 Main St\n"
    962                            "Apt 1"),
    963               s1.ColumnString16(2));
    964     EXPECT_EQ(ASCIIToUTF16("Los Altos"), s1.ColumnString16(3));
    965     EXPECT_EQ(ASCIIToUTF16("CA"), s1.ColumnString16(4));
    966     EXPECT_EQ(ASCIIToUTF16("94022"), s1.ColumnString16(5));
    967     EXPECT_EQ(ASCIIToUTF16("US"), s1.ColumnString16(6));
    968     EXPECT_EQ(1297882100L, s1.ColumnInt64(7));
    969 
    970     // John P. Doe.
    971     // Gets merged during migration from 35 to 37 due to multi-valued fields.
    972 
    973     // Dave Smith.
    974     ASSERT_TRUE(s1.Step());
    975     EXPECT_EQ("4C74A9D8-7EEE-423E-F9C2-E7FA70ED1396", s1.ColumnString(0));
    976     EXPECT_EQ(base::string16(), s1.ColumnString16(1));
    977     EXPECT_EQ(ASCIIToUTF16("2 Main Street"), s1.ColumnString16(2));
    978     EXPECT_EQ(ASCIIToUTF16("Los Altos"), s1.ColumnString16(3));
    979     EXPECT_EQ(ASCIIToUTF16("CA"), s1.ColumnString16(4));
    980     EXPECT_EQ(ASCIIToUTF16("94022"), s1.ColumnString16(5));
    981     EXPECT_EQ(ASCIIToUTF16("US"), s1.ColumnString16(6));
    982     EXPECT_EQ(1297882100L, s1.ColumnInt64(7));
    983 
    984     // Dave Smith (Part 2).
    985     ASSERT_TRUE(s1.Step());
    986     EXPECT_EQ("722DF5C4-F74A-294A-46F0-31FFDED0D635", s1.ColumnString(0));
    987     EXPECT_EQ(base::string16(), s1.ColumnString16(1));
    988     EXPECT_EQ(ASCIIToUTF16("2 Main St"), s1.ColumnString16(2));
    989     EXPECT_EQ(ASCIIToUTF16("Los Altos"), s1.ColumnString16(3));
    990     EXPECT_EQ(ASCIIToUTF16("CA"), s1.ColumnString16(4));
    991     EXPECT_EQ(ASCIIToUTF16("94022"), s1.ColumnString16(5));
    992     EXPECT_EQ(ASCIIToUTF16("US"), s1.ColumnString16(6));
    993     EXPECT_EQ(1297882100L, s1.ColumnInt64(7));
    994 
    995     // Alfred E Newman.
    996     // Gets culled during migration from 35 to 36 due to incomplete address.
    997 
    998     // 3 Main St.
    999     ASSERT_TRUE(s1.Step());
   1000     EXPECT_EQ("9E5FE298-62C7-83DF-6293-381BC589183F", s1.ColumnString(0));
   1001     EXPECT_EQ(base::string16(), s1.ColumnString16(1));
   1002     EXPECT_EQ(ASCIIToUTF16("3 Main St"), s1.ColumnString16(2));
   1003     EXPECT_EQ(ASCIIToUTF16("Los Altos"), s1.ColumnString16(3));
   1004     EXPECT_EQ(ASCIIToUTF16("CA"), s1.ColumnString16(4));
   1005     EXPECT_EQ(ASCIIToUTF16("94022"), s1.ColumnString16(5));
   1006     EXPECT_EQ(ASCIIToUTF16("US"), s1.ColumnString16(6));
   1007     EXPECT_EQ(1297882100L, s1.ColumnInt64(7));
   1008 
   1009     // That should be all.
   1010     EXPECT_FALSE(s1.Step());
   1011 
   1012     sql::Statement s2(
   1013         connection.GetUniqueStatement(
   1014             "SELECT guid, first_name, middle_name, last_name "
   1015             "FROM autofill_profile_names"));
   1016 
   1017     // John Doe.
   1018     ASSERT_TRUE(s2.Step());
   1019     EXPECT_EQ("00580526-FF81-EE2A-0546-1AC593A32E2F", s2.ColumnString(0));
   1020     EXPECT_EQ(ASCIIToUTF16("John"), s2.ColumnString16(1));
   1021     EXPECT_EQ(base::string16(), s2.ColumnString16(2));
   1022     EXPECT_EQ(ASCIIToUTF16("Doe"), s2.ColumnString16(3));
   1023 
   1024     // John P. Doe.  Note same guid as above due to merging of multi-valued
   1025     // fields.
   1026     ASSERT_TRUE(s2.Step());
   1027     EXPECT_EQ("00580526-FF81-EE2A-0546-1AC593A32E2F", s2.ColumnString(0));
   1028     EXPECT_EQ(ASCIIToUTF16("John"), s2.ColumnString16(1));
   1029     EXPECT_EQ(ASCIIToUTF16("P."), s2.ColumnString16(2));
   1030     EXPECT_EQ(ASCIIToUTF16("Doe"), s2.ColumnString16(3));
   1031 
   1032     // Dave Smith.
   1033     ASSERT_TRUE(s2.Step());
   1034     EXPECT_EQ("4C74A9D8-7EEE-423E-F9C2-E7FA70ED1396", s2.ColumnString(0));
   1035     EXPECT_EQ(ASCIIToUTF16("Dave"), s2.ColumnString16(1));
   1036     EXPECT_EQ(base::string16(), s2.ColumnString16(2));
   1037     EXPECT_EQ(ASCIIToUTF16("Smith"), s2.ColumnString16(3));
   1038 
   1039     // Dave Smith (Part 2).
   1040     ASSERT_TRUE(s2.Step());
   1041     EXPECT_EQ("722DF5C4-F74A-294A-46F0-31FFDED0D635", s2.ColumnString(0));
   1042     EXPECT_EQ(ASCIIToUTF16("Dave"), s2.ColumnString16(1));
   1043     EXPECT_EQ(base::string16(), s2.ColumnString16(2));
   1044     EXPECT_EQ(ASCIIToUTF16("Smith"), s2.ColumnString16(3));
   1045 
   1046     // Alfred E Newman.
   1047     // Gets culled during migration from 35 to 36 due to incomplete address.
   1048 
   1049     // 3 Main St.
   1050     ASSERT_TRUE(s2.Step());
   1051     EXPECT_EQ("9E5FE298-62C7-83DF-6293-381BC589183F", s2.ColumnString(0));
   1052     EXPECT_EQ(base::string16(), s2.ColumnString16(1));
   1053     EXPECT_EQ(base::string16(), s2.ColumnString16(2));
   1054     EXPECT_EQ(base::string16(), s2.ColumnString16(3));
   1055 
   1056     // Should be all.
   1057     EXPECT_FALSE(s2.Step());
   1058 
   1059     sql::Statement s3(
   1060         connection.GetUniqueStatement(
   1061             "SELECT guid, email "
   1062             "FROM autofill_profile_emails"));
   1063 
   1064     // John Doe.
   1065     ASSERT_TRUE(s3.Step());
   1066     EXPECT_EQ("00580526-FF81-EE2A-0546-1AC593A32E2F", s3.ColumnString(0));
   1067     EXPECT_EQ(ASCIIToUTF16("john (at) doe.com"), s3.ColumnString16(1));
   1068 
   1069     // John P. Doe.
   1070     // Gets culled during migration from 35 to 37 due to merging of John Doe and
   1071     // John P. Doe addresses.
   1072 
   1073     // 2 Main Street.
   1074     ASSERT_TRUE(s3.Step());
   1075     EXPECT_EQ("4C74A9D8-7EEE-423E-F9C2-E7FA70ED1396", s3.ColumnString(0));
   1076     EXPECT_EQ(base::string16(), s3.ColumnString16(1));
   1077 
   1078     // 2 Main St.
   1079     ASSERT_TRUE(s3.Step());
   1080     EXPECT_EQ("722DF5C4-F74A-294A-46F0-31FFDED0D635", s3.ColumnString(0));
   1081     EXPECT_EQ(base::string16(), s3.ColumnString16(1));
   1082 
   1083     // Alfred E Newman.
   1084     // Gets culled during migration from 35 to 36 due to incomplete address.
   1085 
   1086     // 3 Main St.
   1087     ASSERT_TRUE(s3.Step());
   1088     EXPECT_EQ("9E5FE298-62C7-83DF-6293-381BC589183F", s3.ColumnString(0));
   1089     EXPECT_EQ(base::string16(), s3.ColumnString16(1));
   1090 
   1091     // Should be all.
   1092     EXPECT_FALSE(s3.Step());
   1093 
   1094     sql::Statement s4(
   1095         connection.GetUniqueStatement(
   1096             "SELECT guid, number "
   1097             "FROM autofill_profile_phones"));
   1098 
   1099     // John Doe phone.
   1100     ASSERT_TRUE(s4.Step());
   1101     EXPECT_EQ("00580526-FF81-EE2A-0546-1AC593A32E2F", s4.ColumnString(0));
   1102     EXPECT_EQ(ASCIIToUTF16("4151112222"), s4.ColumnString16(1));
   1103 
   1104     // John Doe fax.
   1105     // Gets culled after fax type removed.
   1106 
   1107     // John P. Doe phone.
   1108     // Gets culled during migration from 35 to 37 due to merging of John Doe and
   1109     // John P. Doe addresses.
   1110 
   1111     // John P. Doe fax.
   1112     // Gets culled during migration from 35 to 37 due to merging of John Doe and
   1113     // John P. Doe addresses.
   1114 
   1115     // 2 Main Street phone.
   1116     ASSERT_TRUE(s4.Step());
   1117     EXPECT_EQ("4C74A9D8-7EEE-423E-F9C2-E7FA70ED1396", s4.ColumnString(0));
   1118     EXPECT_EQ(base::string16(), s4.ColumnString16(1));
   1119 
   1120     // 2 Main Street fax.
   1121     // Gets culled after fax type removed.
   1122 
   1123     // 2 Main St phone.
   1124     ASSERT_TRUE(s4.Step());
   1125     EXPECT_EQ("722DF5C4-F74A-294A-46F0-31FFDED0D635", s4.ColumnString(0));
   1126     EXPECT_EQ(0, s4.ColumnInt(1));  // 0 means phone.
   1127     EXPECT_EQ(base::string16(), s4.ColumnString16(2));
   1128 
   1129     // 2 Main St fax.
   1130     // Gets culled after fax type removed.
   1131 
   1132     // Note no phone or fax for Alfred E Newman.
   1133 
   1134     // 3 Main St phone.
   1135     ASSERT_TRUE(s4.Step());
   1136     EXPECT_EQ("9E5FE298-62C7-83DF-6293-381BC589183F", s4.ColumnString(0));
   1137     EXPECT_EQ(base::string16(), s4.ColumnString16(1));
   1138 
   1139     // 2 Main St fax.
   1140     // Gets culled after fax type removed.
   1141 
   1142     // Should be all.
   1143     EXPECT_FALSE(s4.Step());
   1144   }
   1145 }
   1146 
   1147 // Adds a column for the autofill profile's country code.
   1148 TEST_F(WebDatabaseMigrationTest, MigrateVersion33ToCurrent) {
   1149   // Initialize the database.
   1150   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_33.sql")));
   1151 
   1152   // Verify pre-conditions. These are expectations for version 33 of the
   1153   // database.
   1154   {
   1155     sql::Connection connection;
   1156     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1157 
   1158     EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles",
   1159                                             "country_code"));
   1160 
   1161     // Check that the country value is the one we expect.
   1162     sql::Statement s(
   1163         connection.GetUniqueStatement("SELECT country FROM autofill_profiles"));
   1164 
   1165     ASSERT_TRUE(s.Step());
   1166     std::string country = s.ColumnString(0);
   1167     EXPECT_EQ("United States", country);
   1168   }
   1169 
   1170   DoMigration();
   1171 
   1172   // Verify post-conditions.  These are expectations for current version of the
   1173   // database.
   1174   {
   1175     sql::Connection connection;
   1176     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1177 
   1178     // Check version.
   1179     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1180 
   1181     ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles",
   1182                                            "country_code"));
   1183 
   1184     // Check that the country code is properly converted.
   1185     sql::Statement s(connection.GetUniqueStatement(
   1186         "SELECT country_code FROM autofill_profiles"));
   1187 
   1188     ASSERT_TRUE(s.Step());
   1189     std::string country_code = s.ColumnString(0);
   1190     EXPECT_EQ("US", country_code);
   1191   }
   1192 }
   1193 
   1194 // Cleans up bad country code "UK" in favor of good country code "GB".
   1195 TEST_F(WebDatabaseMigrationTest, MigrateVersion34ToCurrent) {
   1196   // Initialize the database.
   1197   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_34.sql")));
   1198 
   1199   // Verify pre-conditions. These are expectations for version 34 of the
   1200   // database.
   1201   {
   1202     sql::Connection connection;
   1203     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1204 
   1205     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
   1206                                            "country_code"));
   1207 
   1208     // Check that the country_code value is the one we expect.
   1209     sql::Statement s(
   1210         connection.GetUniqueStatement("SELECT country_code "
   1211                                       "FROM autofill_profiles"));
   1212 
   1213     ASSERT_TRUE(s.Step());
   1214     std::string country_code = s.ColumnString(0);
   1215     EXPECT_EQ("UK", country_code);
   1216 
   1217     // Should have only one.
   1218     ASSERT_FALSE(s.Step());
   1219   }
   1220 
   1221   DoMigration();
   1222 
   1223   // Verify post-conditions.  These are expectations for current version of the
   1224   // database.
   1225   {
   1226     sql::Connection connection;
   1227     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1228 
   1229     // Check version.
   1230     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1231 
   1232     ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles",
   1233                                            "country_code"));
   1234 
   1235     // Check that the country_code code is properly converted.
   1236     sql::Statement s(connection.GetUniqueStatement(
   1237         "SELECT country_code FROM autofill_profiles"));
   1238 
   1239     ASSERT_TRUE(s.Step());
   1240     std::string country_code = s.ColumnString(0);
   1241     EXPECT_EQ("GB", country_code);
   1242 
   1243     // Should have only one.
   1244     ASSERT_FALSE(s.Step());
   1245   }
   1246 }
   1247 
   1248 // Cleans up invalid profiles based on more agressive merging.  Filters out
   1249 // profiles that are subsets of other profiles, and profiles with invalid email,
   1250 // state, and incomplete address.
   1251 TEST_F(WebDatabaseMigrationTest, MigrateVersion35ToCurrent) {
   1252   // Initialize the database.
   1253   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_35.sql")));
   1254 
   1255   // Verify pre-conditions. These are expectations for version 34 of the
   1256   // database.
   1257   {
   1258     sql::Connection connection;
   1259     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1260 
   1261     EXPECT_FALSE(connection.DoesTableExist("autofill_profiles_trash"));
   1262     ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid"));
   1263 
   1264     // Check that there are 6 profiles prior to merge.
   1265     sql::Statement s(
   1266         connection.GetUniqueStatement("SELECT guid FROM autofill_profiles"));
   1267     int i = 0;
   1268     while (s.Step())
   1269       ++i;
   1270     EXPECT_EQ(6, i);
   1271   }
   1272 
   1273   DoMigration();
   1274 
   1275   // Verify post-conditions.  These are expectations for current version of the
   1276   // database.
   1277   {
   1278     sql::Connection connection;
   1279     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1280 
   1281     // Check version.
   1282     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1283 
   1284     ASSERT_TRUE(connection.DoesTableExist("autofill_profiles_trash"));
   1285     ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles_trash", "guid"));
   1286     ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid"));
   1287 
   1288     // Verify data in the database after the migration.
   1289     sql::Statement s1(
   1290         connection.GetUniqueStatement(
   1291             "SELECT guid, company_name, street_address, city, state, zipcode,"
   1292             " country_code, date_modified "
   1293             "FROM autofill_profiles"));
   1294 
   1295     // John Doe.
   1296     ASSERT_TRUE(s1.Step());
   1297     EXPECT_EQ("00000000-0000-0000-0000-000000000001", s1.ColumnString(0));
   1298     EXPECT_EQ(ASCIIToUTF16("Acme Inc."), s1.ColumnString16(1));
   1299     EXPECT_EQ(ASCIIToUTF16("1 Main Street\n"
   1300                            "Apt 2"),
   1301               s1.ColumnString16(2));
   1302     EXPECT_EQ(ASCIIToUTF16("San Francisco"), s1.ColumnString16(3));
   1303     EXPECT_EQ(ASCIIToUTF16("CA"), s1.ColumnString16(4));
   1304     EXPECT_EQ(ASCIIToUTF16("94102"), s1.ColumnString16(5));
   1305     EXPECT_EQ(ASCIIToUTF16("US"), s1.ColumnString16(6));
   1306     EXPECT_EQ(1300131704, s1.ColumnInt64(7));
   1307 
   1308     // That should be it.
   1309     ASSERT_FALSE(s1.Step());
   1310 
   1311     // Check that there 5 trashed profile after the merge.
   1312     sql::Statement s2(
   1313         connection.GetUniqueStatement("SELECT guid "
   1314                                       "FROM autofill_profiles_trash"));
   1315     ASSERT_TRUE(s2.Step());
   1316     EXPECT_EQ("00000000-0000-0000-0000-000000000002", s2.ColumnString(0));
   1317 
   1318     ASSERT_TRUE(s2.Step());
   1319     EXPECT_EQ("00000000-0000-0000-0000-000000000003", s2.ColumnString(0));
   1320 
   1321     ASSERT_TRUE(s2.Step());
   1322     EXPECT_EQ("00000000-0000-0000-0000-000000000004", s2.ColumnString(0));
   1323 
   1324     ASSERT_TRUE(s2.Step());
   1325     EXPECT_EQ("00000000-0000-0000-0000-000000000005", s2.ColumnString(0));
   1326 
   1327     ASSERT_TRUE(s2.Step());
   1328     EXPECT_EQ("00000000-0000-0000-0000-000000000006", s2.ColumnString(0));
   1329 
   1330     // That should be it.
   1331     ASSERT_FALSE(s2.Step());
   1332   }
   1333 }
   1334 
   1335 // Tests that the |keywords| |last_modified| column gets added to the schema for
   1336 // a version 37 database.
   1337 TEST_F(WebDatabaseMigrationTest, MigrateVersion37ToCurrent) {
   1338   // This schema is taken from a build prior to the addition of the |keywords|
   1339   // |last_modified| column.
   1340   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_37.sql")));
   1341 
   1342   // Verify pre-conditions.  These are expectations for version 37 of the
   1343   // database.
   1344   {
   1345     sql::Connection connection;
   1346     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1347 
   1348     // Columns existing and not existing before current version.
   1349     ASSERT_TRUE(connection.DoesColumnExist("keywords", "id"));
   1350     ASSERT_FALSE(connection.DoesColumnExist("keywords", "last_modified"));
   1351   }
   1352 
   1353   DoMigration();
   1354 
   1355   // Verify post-conditions.  These are expectations for current version of the
   1356   // database.
   1357   {
   1358     sql::Connection connection;
   1359     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1360 
   1361     // Check version.
   1362     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1363 
   1364     // |keywords| |last_modified| column should have been added.
   1365     EXPECT_TRUE(connection.DoesColumnExist("keywords", "id"));
   1366     EXPECT_TRUE(connection.DoesColumnExist("keywords", "last_modified"));
   1367   }
   1368 }
   1369 
   1370 // Tests that the |keywords| |sync_guid| column gets added to the schema for
   1371 // a version 38 database.
   1372 TEST_F(WebDatabaseMigrationTest, MigrateVersion38ToCurrent) {
   1373   // This schema is taken from a build prior to the addition of the |keywords|
   1374   // |sync_guid| column.
   1375   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_38.sql")));
   1376 
   1377   // Verify pre-conditions.  These are expectations for version 38 of the
   1378   // database.
   1379   {
   1380     sql::Connection connection;
   1381     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1382 
   1383     // Columns existing and not existing before current version.
   1384     ASSERT_TRUE(connection.DoesColumnExist("keywords", "id"));
   1385     ASSERT_FALSE(connection.DoesColumnExist("keywords", "sync_guid"));
   1386   }
   1387 
   1388   DoMigration();
   1389 
   1390   // Verify post-conditions.  These are expectations for current version of the
   1391   // database.
   1392   {
   1393     sql::Connection connection;
   1394     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1395 
   1396     // Check version.
   1397     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1398 
   1399     // |keywords| |sync_guid| column should have been added.
   1400     EXPECT_TRUE(connection.DoesColumnExist("keywords", "id"));
   1401     EXPECT_TRUE(connection.DoesColumnExist("keywords", "sync_guid"));
   1402   }
   1403 }
   1404 
   1405 // Tests that no backup data is added to a version 39 database.
   1406 TEST_F(WebDatabaseMigrationTest, MigrateVersion39ToCurrent) {
   1407   // This schema is taken from a build prior to the addition of the default
   1408   // search provider backup field to the meta table.
   1409   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_39.sql")));
   1410 
   1411   // Verify pre-conditions.  These are expectations for version 39 of the
   1412   // database.
   1413   {
   1414     sql::Connection connection;
   1415     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1416     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1417 
   1418     sql::MetaTable meta_table;
   1419     ASSERT_TRUE(meta_table.Init(&connection, 39, 39));
   1420 
   1421     int64 default_search_provider_id = 0;
   1422     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   1423                                     &default_search_provider_id));
   1424 
   1425     EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
   1426   }
   1427 
   1428   DoMigration();
   1429 
   1430   // Verify post-conditions.  These are expectations for current version of the
   1431   // database.
   1432   {
   1433     sql::Connection connection;
   1434     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1435     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1436 
   1437     // Check version.
   1438     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1439 
   1440     sql::MetaTable meta_table;
   1441     ASSERT_TRUE(meta_table.Init(&connection, kCurrentTestedVersionNumber,
   1442                                 kCurrentTestedVersionNumber));
   1443 
   1444     int64 default_search_provider_id = 0;
   1445     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   1446                                     &default_search_provider_id));
   1447     EXPECT_NE(0, default_search_provider_id);
   1448 
   1449     EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
   1450   }
   1451 }
   1452 
   1453 // Tests that the backup data is removed from the database.
   1454 TEST_F(WebDatabaseMigrationTest, MigrateVersion40ToCurrent) {
   1455   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_40.sql")));
   1456 
   1457   // Verify pre-conditions.  These are expectations for version 40 of the
   1458   // database.
   1459   {
   1460     sql::Connection connection;
   1461     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1462     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1463 
   1464     sql::MetaTable meta_table;
   1465     ASSERT_TRUE(meta_table.Init(&connection, 40, 40));
   1466 
   1467     int64 default_search_provider_id = 0;
   1468     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   1469                                     &default_search_provider_id));
   1470 
   1471     EXPECT_NO_FATAL_FAILURE(CheckHasBackupData(&meta_table));
   1472   }
   1473 
   1474   DoMigration();
   1475 
   1476   // Verify post-conditions.  These are expectations for current version of the
   1477   // database.
   1478   {
   1479     sql::Connection connection;
   1480     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1481     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1482 
   1483     // Check version.
   1484     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1485 
   1486     sql::MetaTable meta_table;
   1487     ASSERT_TRUE(meta_table.Init(&connection, kCurrentTestedVersionNumber,
   1488                                 kCurrentTestedVersionNumber));
   1489 
   1490     int64 default_search_provider_id = 0;
   1491     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   1492                                     &default_search_provider_id));
   1493     EXPECT_NE(0, default_search_provider_id);
   1494 
   1495     EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
   1496   }
   1497 }
   1498 
   1499 // Tests that the backup data is removed from the database.
   1500 TEST_F(WebDatabaseMigrationTest, MigrateVersion41ToCurrent) {
   1501   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_41.sql")));
   1502 
   1503   // Verify pre-conditions.  These are expectations for version 41 of the
   1504   // database.
   1505   {
   1506     sql::Connection connection;
   1507     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1508     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1509 
   1510     sql::MetaTable meta_table;
   1511     ASSERT_TRUE(meta_table.Init(&connection, 41, 41));
   1512 
   1513     int64 default_search_provider_id = 0;
   1514     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   1515                                     &default_search_provider_id));
   1516 
   1517     EXPECT_NO_FATAL_FAILURE(CheckHasBackupData(&meta_table));
   1518   }
   1519 
   1520   DoMigration();
   1521 
   1522   // Verify post-conditions.  These are expectations for current version of the
   1523   // database.
   1524   {
   1525     sql::Connection connection;
   1526     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1527     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1528 
   1529     // Check version.
   1530     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1531 
   1532     sql::MetaTable meta_table;
   1533     ASSERT_TRUE(meta_table.Init(&connection, kCurrentTestedVersionNumber,
   1534                                 kCurrentTestedVersionNumber));
   1535 
   1536     int64 default_search_provider_id = 0;
   1537     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   1538                                     &default_search_provider_id));
   1539     EXPECT_NE(0, default_search_provider_id);
   1540 
   1541     EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
   1542   }
   1543 }
   1544 
   1545 // Tests that the backup data is removed from the database.
   1546 TEST_F(WebDatabaseMigrationTest, MigrateVersion42ToCurrent) {
   1547   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_42.sql")));
   1548 
   1549   // Verify pre-conditions.  These are expectations for version 42 of the
   1550   // database.
   1551   {
   1552     sql::Connection connection;
   1553     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1554     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1555 
   1556     sql::MetaTable meta_table;
   1557     ASSERT_TRUE(meta_table.Init(&connection, 42, 42));
   1558 
   1559     int64 default_search_provider_id = 0;
   1560     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   1561                                     &default_search_provider_id));
   1562 
   1563     EXPECT_NO_FATAL_FAILURE(CheckHasBackupData(&meta_table));
   1564 
   1565     EXPECT_FALSE(connection.DoesTableExist("keywords_backup"));
   1566   }
   1567 
   1568   DoMigration();
   1569 
   1570   // Verify post-conditions.  These are expectations for current version of the
   1571   // database.
   1572   {
   1573     sql::Connection connection;
   1574     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1575     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1576 
   1577     // Check version.
   1578     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1579 
   1580     sql::MetaTable meta_table;
   1581     ASSERT_TRUE(meta_table.Init(&connection, kCurrentTestedVersionNumber,
   1582                                 kCurrentTestedVersionNumber));
   1583 
   1584     int64 default_search_provider_id = 0;
   1585     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   1586                                     &default_search_provider_id));
   1587     EXPECT_NE(0, default_search_provider_id);
   1588 
   1589     EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
   1590   }
   1591 }
   1592 
   1593 // Tests that the backup data is removed from the database.
   1594 TEST_F(WebDatabaseMigrationTest, MigrateVersion43ToCurrent) {
   1595   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_43.sql")));
   1596 
   1597   int64 previous_default_search_provider_id;
   1598 
   1599   // Verify pre-conditions.  These are expectations for version 43 of the
   1600   // database.
   1601   {
   1602     sql::Connection connection;
   1603     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1604     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1605 
   1606     sql::MetaTable meta_table;
   1607     ASSERT_TRUE(meta_table.Init(&connection, 43, 43));
   1608 
   1609     int64 default_search_provider_id = 0;
   1610     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   1611                                     &default_search_provider_id));
   1612     EXPECT_NE(default_search_provider_id, 0);
   1613     previous_default_search_provider_id = default_search_provider_id;
   1614 
   1615     EXPECT_NO_FATAL_FAILURE(CheckHasBackupData(&meta_table));
   1616     EXPECT_TRUE(connection.DoesTableExist("keywords_backup"));
   1617   }
   1618 
   1619   DoMigration();
   1620 
   1621   // Verify post-conditions.  These are expectations for current version of the
   1622   // database.
   1623   {
   1624     sql::Connection connection;
   1625     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1626     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1627 
   1628     // Check version.
   1629     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1630 
   1631     sql::MetaTable meta_table;
   1632     ASSERT_TRUE(meta_table.Init(
   1633         &connection,
   1634         kCurrentTestedVersionNumber,
   1635         kCurrentTestedVersionNumber));
   1636 
   1637     int64 default_search_provider_id = 0;
   1638     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   1639                                     &default_search_provider_id));
   1640     // Default search provider ID should not change.
   1641     EXPECT_EQ(previous_default_search_provider_id, default_search_provider_id);
   1642 
   1643     EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
   1644   }
   1645 }
   1646 
   1647 // Tests that the |autogenerate_keyword| and |logo_id| columns get removed from
   1648 // the keyword table schema for a version 45 database.
   1649 TEST_F(WebDatabaseMigrationTest, MigrateVersion44ToCurrent) {
   1650   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_44.sql")));
   1651 
   1652   // Verify pre-conditions.  These are expectations for version 44 of the
   1653   // database.
   1654   {
   1655     sql::Connection connection;
   1656     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1657     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1658 
   1659     sql::MetaTable meta_table;
   1660     ASSERT_TRUE(meta_table.Init(&connection, 44, 44));
   1661 
   1662     ASSERT_TRUE(connection.DoesColumnExist("keywords", "autogenerate_keyword"));
   1663     ASSERT_TRUE(connection.DoesColumnExist("keywords", "logo_id"));
   1664   }
   1665 
   1666   DoMigration();
   1667 
   1668   // Verify post-conditions.  These are expectations for current version of the
   1669   // database.
   1670   {
   1671     sql::Connection connection;
   1672     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1673     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1674 
   1675     // Check version.
   1676     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1677 
   1678     sql::MetaTable meta_table;
   1679     ASSERT_TRUE(meta_table.Init(&connection, kCurrentTestedVersionNumber,
   1680                                 kCurrentTestedVersionNumber));
   1681 
   1682     // We should have removed this obsolete key.
   1683     std::string default_search_provider_backup;
   1684     EXPECT_FALSE(meta_table.GetValue("Default Search Provider Backup",
   1685                                      &default_search_provider_backup));
   1686 
   1687     // Two columns should have been removed.
   1688     EXPECT_FALSE(connection.DoesColumnExist("keywords",
   1689                                             "autogenerate_keyword"));
   1690     EXPECT_FALSE(connection.DoesColumnExist("keywords", "logo_id"));
   1691 
   1692     // Backup data should have been removed.
   1693     EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
   1694   }
   1695 }
   1696 
   1697 // Tests that the web_intents and web_intents_defaults tables are
   1698 // modified to include "scheme" columns.
   1699 TEST_F(WebDatabaseMigrationTest, MigrateVersion45ToCurrent) {
   1700   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_45.sql")));
   1701 
   1702   // Verify pre-conditions.  These are expectations for version 45 of the
   1703   // database.
   1704   {
   1705     sql::Connection connection;
   1706     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1707     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1708 
   1709     sql::MetaTable meta_table;
   1710     ASSERT_TRUE(meta_table.Init(&connection, 45, 45));
   1711 
   1712     ASSERT_FALSE(connection.DoesColumnExist("scheme", "web_intents"));
   1713     ASSERT_FALSE(connection.DoesColumnExist(
   1714         "scheme", "web_intents_defaults"));
   1715   }
   1716 
   1717   DoMigration();
   1718 
   1719   // Verify post-conditions.  These are expectations for current version of the
   1720   // database.
   1721   {
   1722     sql::Connection connection;
   1723     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1724     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1725 
   1726     // Check version.
   1727     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1728 
   1729     sql::MetaTable meta_table;
   1730     ASSERT_TRUE(meta_table.Init(
   1731         &connection,
   1732         kCurrentTestedVersionNumber,
   1733         kCurrentTestedVersionNumber));
   1734 
   1735     // A new "scheme" column should have been added to each web_intents table.
   1736     EXPECT_TRUE(connection.DoesColumnExist("web_intents", "scheme"));
   1737     EXPECT_TRUE(connection.DoesColumnExist("web_intents_defaults", "scheme"));
   1738 
   1739     // Verify existing user data was copied.
   1740     sql::Statement s1(
   1741         connection.GetUniqueStatement("SELECT * FROM web_intents"));
   1742 
   1743     ASSERT_TRUE(s1.Step());
   1744     EXPECT_EQ("http://poodles.com/fuzzer", s1.ColumnString(0));
   1745     EXPECT_EQ(ASCIIToUTF16("fuzz"), s1.ColumnString16(1));
   1746     EXPECT_EQ(ASCIIToUTF16("poodle/*"), s1.ColumnString16(2));
   1747     EXPECT_EQ(ASCIIToUTF16("Poodle Fuzzer"), s1.ColumnString16(3));
   1748     EXPECT_EQ(ASCIIToUTF16("window"), s1.ColumnString16(4));
   1749     EXPECT_EQ(ASCIIToUTF16(""), s1.ColumnString16(5));
   1750     ASSERT_FALSE(s1.Step());
   1751 
   1752     // Now we want to verify existing user data was copied
   1753     sql::Statement s2(
   1754         connection.GetUniqueStatement("SELECT * FROM web_intents_defaults"));
   1755 
   1756     ASSERT_TRUE(s2.Step());
   1757     EXPECT_EQ("fuzz", s2.ColumnString(0));
   1758     EXPECT_EQ(ASCIIToUTF16("poodle/*"), s2.ColumnString16(1));
   1759     EXPECT_EQ(ASCIIToUTF16(""), s2.ColumnString16(2));
   1760     EXPECT_EQ(0, s2.ColumnInt(3));
   1761     EXPECT_EQ(0, s2.ColumnInt(4));
   1762     EXPECT_EQ(ASCIIToUTF16("http://poodles.com/fuzzer"), s2.ColumnString16(5));
   1763     EXPECT_EQ(ASCIIToUTF16(""), s2.ColumnString16(6));
   1764     ASSERT_FALSE(s2.Step());
   1765 
   1766     // finally ensure the migration code cleaned up after itself
   1767     EXPECT_FALSE(connection.DoesTableExist("old_web_intents"));
   1768     EXPECT_FALSE(connection.DoesTableExist("old_web_intents_defaults"));
   1769   }
   1770 }
   1771 
   1772 // Tests that the web_intents and web_intents_defaults tables are
   1773 // modified to include "scheme" columns.
   1774 TEST_F(WebDatabaseMigrationTest, MigrateVersion45InvalidToCurrent) {
   1775   ASSERT_NO_FATAL_FAILURE(
   1776       LoadDatabase(FILE_PATH_LITERAL("version_45_invalid.sql")));
   1777 
   1778   // Verify pre-conditions.  These are expectations for version 45 of the
   1779   // database.
   1780   {
   1781     sql::Connection connection;
   1782     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1783     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1784 
   1785     sql::MetaTable meta_table;
   1786     ASSERT_TRUE(meta_table.Init(&connection, 45, 45));
   1787 
   1788     ASSERT_FALSE(connection.DoesColumnExist("scheme", "web_intents"));
   1789     ASSERT_FALSE(connection.DoesColumnExist(
   1790         "scheme", "web_intents_defaults"));
   1791   }
   1792 
   1793   DoMigration();
   1794 
   1795   // Verify post-conditions.  These are expectations for current version of the
   1796   // database.
   1797   {
   1798     sql::Connection connection;
   1799     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1800     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1801 
   1802     // Check version.
   1803     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1804 
   1805     sql::MetaTable meta_table;
   1806     ASSERT_TRUE(meta_table.Init(
   1807         &connection,
   1808         kCurrentTestedVersionNumber,
   1809         kCurrentTestedVersionNumber));
   1810 
   1811     // A new "scheme" column should have been added to each web_intents table.
   1812     EXPECT_TRUE(connection.DoesColumnExist("web_intents", "scheme"));
   1813     EXPECT_TRUE(connection.DoesColumnExist("web_intents_defaults", "scheme"));
   1814 
   1815     // Verify existing user data was copied.
   1816     sql::Statement s1(
   1817         connection.GetUniqueStatement("SELECT * FROM web_intents"));
   1818 
   1819     ASSERT_FALSE(s1.Step());  // Basically should be empty at this point.
   1820 
   1821     // Now we want to verify existing user data was copied
   1822     sql::Statement s2(
   1823         connection.GetUniqueStatement("SELECT * FROM web_intents_defaults"));
   1824 
   1825     // We were able to create the new tables, but unable to copy any data
   1826     // Given the initial bad state of the tables.
   1827     ASSERT_FALSE(s2.Step());
   1828 
   1829     // Finally ensure the migration code cleaned up after itself.
   1830     EXPECT_FALSE(connection.DoesTableExist("old_web_intents"));
   1831     EXPECT_FALSE(connection.DoesTableExist("old_web_intents_defaults"));
   1832   }
   1833 }
   1834 
   1835 // Check that current version is forced to compatible version before migration,
   1836 // if the former is smaller.
   1837 TEST_F(WebDatabaseMigrationTest, MigrateVersion45CompatibleToCurrent) {
   1838   ASSERT_NO_FATAL_FAILURE(
   1839       LoadDatabase(FILE_PATH_LITERAL("version_45_compatible.sql")));
   1840 
   1841   // Verify pre-conditions.  These are expectations for version 45 of the
   1842   // database.
   1843   {
   1844     sql::Connection connection;
   1845     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1846     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1847 
   1848     sql::MetaTable meta_table;
   1849     // Database is actually version 45 but the version field states 40.
   1850     ASSERT_TRUE(meta_table.Init(&connection, 40, 45));
   1851   }
   1852 
   1853   DoMigration();
   1854 
   1855   // Verify post-conditions.  These are expectations for current version of the
   1856   // database.
   1857   {
   1858     sql::Connection connection;
   1859     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1860     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1861 
   1862     // Check version.
   1863     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1864     EXPECT_LE(45, VersionFromConnection(&connection));
   1865   }
   1866 }
   1867 
   1868 // Tests that the |alternate_urls| column is added to the keyword table schema
   1869 // for a version 47 database.
   1870 TEST_F(WebDatabaseMigrationTest, MigrateVersion46ToCurrent) {
   1871   ASSERT_NO_FATAL_FAILURE(
   1872       LoadDatabase(FILE_PATH_LITERAL("version_46.sql")));
   1873 
   1874   // Verify pre-conditions.  These are expectations for version 46 of the
   1875   // database.
   1876   {
   1877     sql::Connection connection;
   1878     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1879     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1880 
   1881     sql::MetaTable meta_table;
   1882     ASSERT_TRUE(meta_table.Init(&connection, 46, 46));
   1883 
   1884     ASSERT_FALSE(connection.DoesColumnExist("keywords", "alternate_urls"));
   1885     ASSERT_FALSE(connection.DoesColumnExist("keywords_backup",
   1886                                             "alternate_urls"));
   1887   }
   1888 
   1889   DoMigration();
   1890 
   1891   // Verify post-conditions.  These are expectations for current version of the
   1892   // database.
   1893   {
   1894     sql::Connection connection;
   1895     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1896     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1897 
   1898     // Check version.
   1899     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1900 
   1901     // A new column should have been created.
   1902     EXPECT_TRUE(connection.DoesColumnExist("keywords", "alternate_urls"));
   1903   }
   1904 }
   1905 
   1906 // Tests that the backup data is removed from the database.
   1907 TEST_F(WebDatabaseMigrationTest, MigrateVersion47ToCurrent) {
   1908   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_47.sql")));
   1909 
   1910   // Verify pre-conditions.  These are expectations for version 47 of the
   1911   // database.
   1912   {
   1913     sql::Connection connection;
   1914     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1915     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1916 
   1917     sql::MetaTable meta_table;
   1918     ASSERT_TRUE(meta_table.Init(&connection, 47, 47));
   1919 
   1920     int64 default_search_provider_id = 0;
   1921     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   1922                                     &default_search_provider_id));
   1923     EXPECT_NE(0, default_search_provider_id);
   1924 
   1925     EXPECT_NO_FATAL_FAILURE(CheckHasBackupData(&meta_table));
   1926     EXPECT_TRUE(connection.DoesTableExist("keywords_backup"));
   1927   }
   1928 
   1929   DoMigration();
   1930 
   1931   // Verify post-conditions.  These are expectations for current version of the
   1932   // database.
   1933   {
   1934     sql::Connection connection;
   1935     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1936     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1937 
   1938     // Check version.
   1939     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1940 
   1941     sql::MetaTable meta_table;
   1942     ASSERT_TRUE(meta_table.Init(&connection, kCurrentTestedVersionNumber,
   1943                                 kCurrentTestedVersionNumber));
   1944 
   1945     int64 default_search_provider_id = 0;
   1946     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   1947                                     &default_search_provider_id));
   1948     EXPECT_NE(0, default_search_provider_id);
   1949 
   1950     EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
   1951   }
   1952 }
   1953 
   1954 // Tests that the |search_terms_replacement_key| column is added to the keyword
   1955 // table schema for a version 49 database.
   1956 TEST_F(WebDatabaseMigrationTest, MigrateVersion48ToCurrent) {
   1957   ASSERT_NO_FATAL_FAILURE(
   1958       LoadDatabase(FILE_PATH_LITERAL("version_48.sql")));
   1959 
   1960   // Verify pre-conditions.  These are expectations for version 48 of the
   1961   // database.
   1962   {
   1963     sql::Connection connection;
   1964     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1965     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1966 
   1967     sql::MetaTable meta_table;
   1968     ASSERT_TRUE(meta_table.Init(&connection, 48, 48));
   1969 
   1970     ASSERT_FALSE(connection.DoesColumnExist("keywords",
   1971                                             "search_terms_replacement_key"));
   1972   }
   1973 
   1974   DoMigration();
   1975 
   1976   // Verify post-conditions.  These are expectations for current version of the
   1977   // database.
   1978   {
   1979     sql::Connection connection;
   1980     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1981     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1982 
   1983     // Check version.
   1984     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1985 
   1986     // A new column should have been created.
   1987     EXPECT_TRUE(connection.DoesColumnExist("keywords",
   1988                                            "search_terms_replacement_key"));
   1989   }
   1990 }
   1991 
   1992 // Tests that the |origin| column is added to the autofill_profiles and
   1993 // credit_cards table schemas for a version 50 database.
   1994 TEST_F(WebDatabaseMigrationTest, MigrateVersion49ToCurrent) {
   1995   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_49.sql")));
   1996 
   1997   // Verify pre-conditions.  These are expectations for version 49 of the
   1998   // database.
   1999   {
   2000     sql::Connection connection;
   2001     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   2002 
   2003     ASSERT_FALSE(connection.DoesColumnExist("autofill_profiles", "origin"));
   2004     ASSERT_FALSE(connection.DoesColumnExist("credit_cards", "origin"));
   2005   }
   2006 
   2007   DoMigration();
   2008 
   2009   // Verify post-conditions.  These are expectations for current version of the
   2010   // database.
   2011   {
   2012     sql::Connection connection;
   2013     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   2014     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   2015 
   2016     // Check version.
   2017     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   2018 
   2019     // A new column should have been created in both tables.
   2020     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "origin"));
   2021     EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "origin"));
   2022   }
   2023 }
   2024 
   2025 // Tests that the columns |image_url|, |search_url_post_params|,
   2026 // |suggest_url_post_params|, |instant_url_post_params|, and
   2027 // |image_url_post_params| are added to the keyword table schema for a version
   2028 // 50 database.
   2029 TEST_F(WebDatabaseMigrationTest, MigrateVersion50ToCurrent) {
   2030   ASSERT_NO_FATAL_FAILURE(
   2031       LoadDatabase(FILE_PATH_LITERAL("version_50.sql")));
   2032 
   2033   // Verify pre-conditions.  These are expectations for version 50 of the
   2034   // database.
   2035   {
   2036     sql::Connection connection;
   2037     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   2038     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   2039 
   2040     sql::MetaTable meta_table;
   2041     ASSERT_TRUE(meta_table.Init(&connection, 50, 50));
   2042 
   2043     ASSERT_FALSE(connection.DoesColumnExist("keywords", "image_url"));
   2044     ASSERT_FALSE(connection.DoesColumnExist("keywords",
   2045                                             "search_url_post_params"));
   2046     ASSERT_FALSE(connection.DoesColumnExist("keywords",
   2047                                             "suggest_url_post_params"));
   2048     ASSERT_FALSE(connection.DoesColumnExist("keywords",
   2049                                             "instant_url_post_params"));
   2050     ASSERT_FALSE(connection.DoesColumnExist("keywords",
   2051                                             "image_url_post_params"));
   2052   }
   2053 
   2054   DoMigration();
   2055 
   2056   // Verify post-conditions.  These are expectations for current version of the
   2057   // database.
   2058   {
   2059     sql::Connection connection;
   2060     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   2061     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   2062 
   2063     // Check version.
   2064     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   2065 
   2066     // New columns should have been created.
   2067     EXPECT_TRUE(connection.DoesColumnExist("keywords", "image_url"));
   2068     EXPECT_TRUE(connection.DoesColumnExist("keywords",
   2069                                            "search_url_post_params"));
   2070     EXPECT_TRUE(connection.DoesColumnExist("keywords",
   2071                                            "suggest_url_post_params"));
   2072     EXPECT_TRUE(connection.DoesColumnExist("keywords",
   2073                                            "instant_url_post_params"));
   2074     EXPECT_TRUE(connection.DoesColumnExist("keywords",
   2075                                            "image_url_post_params"));
   2076   }
   2077 }
   2078 
   2079 // Tests that the column |new_tab_url| is added to the keyword table schema for
   2080 // a version 52 database.
   2081 TEST_F(WebDatabaseMigrationTest, MigrateVersion52ToCurrent) {
   2082   ASSERT_NO_FATAL_FAILURE(
   2083       LoadDatabase(FILE_PATH_LITERAL("version_52.sql")));
   2084 
   2085   // Verify pre-conditions.  These are expectations for version 52 of the
   2086   // database.
   2087   {
   2088     sql::Connection connection;
   2089     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   2090     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   2091 
   2092     sql::MetaTable meta_table;
   2093     ASSERT_TRUE(meta_table.Init(&connection, 52, 52));
   2094 
   2095     ASSERT_FALSE(connection.DoesColumnExist("keywords", "new_tab_url"));
   2096   }
   2097 
   2098   DoMigration();
   2099 
   2100   // Verify post-conditions.  These are expectations for current version of the
   2101   // database.
   2102   {
   2103     sql::Connection connection;
   2104     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   2105     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   2106 
   2107     // Check version.
   2108     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   2109 
   2110     // New columns should have been created.
   2111     EXPECT_TRUE(connection.DoesColumnExist("keywords", "new_tab_url"));
   2112   }
   2113 }
   2114 
   2115 // Tests that for a version 54 database,
   2116 //   (a) The street_address, dependent_locality, and sorting_code columns are
   2117 //       added to the autofill_profiles table schema.
   2118 //   (b) The address_line1, address_line2, and country columns are dropped from
   2119 //       the autofill_profiles table schema.
   2120 //   (c) The type column is dropped from the autofill_profile_phones schema.
   2121 TEST_F(WebDatabaseMigrationTest, MigrateVersion53ToCurrent) {
   2122   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_53.sql")));
   2123 
   2124   // Verify pre-conditions.  These are expectations for version 53 of the
   2125   // database.
   2126   {
   2127     sql::Connection connection;
   2128     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   2129 
   2130     EXPECT_TRUE(
   2131         connection.DoesColumnExist("autofill_profiles", "address_line_1"));
   2132     EXPECT_TRUE(
   2133         connection.DoesColumnExist("autofill_profiles", "address_line_2"));
   2134     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "country"));
   2135     EXPECT_FALSE(
   2136         connection.DoesColumnExist("autofill_profiles", "street_address"));
   2137     EXPECT_FALSE(
   2138         connection.DoesColumnExist("autofill_profiles", "dependent_locality"));
   2139     EXPECT_FALSE(
   2140         connection.DoesColumnExist("autofill_profiles", "sorting_code"));
   2141     EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_phones", "type"));
   2142   }
   2143 
   2144   DoMigration();
   2145 
   2146   // Verify post-conditions.  These are expectations for current version of the
   2147   // database.
   2148   {
   2149     sql::Connection connection;
   2150     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   2151     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   2152 
   2153     // Check version.
   2154     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   2155 
   2156     // Columns should have been added and removed appropriately.
   2157     EXPECT_FALSE(
   2158         connection.DoesColumnExist("autofill_profiles", "address_line1"));
   2159     EXPECT_FALSE(
   2160         connection.DoesColumnExist("autofill_profiles", "address_line2"));
   2161     EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "country"));
   2162     EXPECT_TRUE(
   2163         connection.DoesColumnExist("autofill_profiles", "street_address"));
   2164     EXPECT_TRUE(
   2165         connection.DoesColumnExist("autofill_profiles", "dependent_locality"));
   2166     EXPECT_TRUE(
   2167         connection.DoesColumnExist("autofill_profiles", "sorting_code"));
   2168     EXPECT_FALSE(connection.DoesColumnExist("autofill_profile_phones", "type"));
   2169 
   2170     // Data should have been preserved.
   2171     sql::Statement s_profiles(
   2172         connection.GetUniqueStatement(
   2173             "SELECT guid, company_name, street_address, dependent_locality,"
   2174             " city, state, zipcode, sorting_code, country_code, date_modified,"
   2175             " origin "
   2176             "FROM autofill_profiles"));
   2177 
   2178     // Address lines 1 and 2.
   2179     ASSERT_TRUE(s_profiles.Step());
   2180     EXPECT_EQ("00000000-0000-0000-0000-000000000001",
   2181               s_profiles.ColumnString(0));
   2182     EXPECT_EQ(ASCIIToUTF16("Google, Inc."), s_profiles.ColumnString16(1));
   2183     EXPECT_EQ(ASCIIToUTF16("1950 Charleston Rd.\n"
   2184                            "(2nd floor)"),
   2185               s_profiles.ColumnString16(2));
   2186     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(3));
   2187     EXPECT_EQ(ASCIIToUTF16("Mountain View"), s_profiles.ColumnString16(4));
   2188     EXPECT_EQ(ASCIIToUTF16("CA"), s_profiles.ColumnString16(5));
   2189     EXPECT_EQ(ASCIIToUTF16("94043"), s_profiles.ColumnString16(6));
   2190     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(7));
   2191     EXPECT_EQ(ASCIIToUTF16("US"), s_profiles.ColumnString16(8));
   2192     EXPECT_EQ(1386046731, s_profiles.ColumnInt(9));
   2193     EXPECT_EQ(ASCIIToUTF16("Chrome settings"), s_profiles.ColumnString16(10));
   2194 
   2195     // Only address line 1.
   2196     ASSERT_TRUE(s_profiles.Step());
   2197     EXPECT_EQ("00000000-0000-0000-0000-000000000002",
   2198               s_profiles.ColumnString(0));
   2199     EXPECT_EQ(ASCIIToUTF16("Google!"), s_profiles.ColumnString16(1));
   2200     EXPECT_EQ(ASCIIToUTF16("1600 Amphitheatre Pkwy."),
   2201               s_profiles.ColumnString16(2));
   2202     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(3));
   2203     EXPECT_EQ(ASCIIToUTF16("Mtn. View"), s_profiles.ColumnString16(4));
   2204     EXPECT_EQ(ASCIIToUTF16("California"), s_profiles.ColumnString16(5));
   2205     EXPECT_EQ(ASCIIToUTF16("94043-1234"), s_profiles.ColumnString16(6));
   2206     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(7));
   2207     EXPECT_EQ(ASCIIToUTF16("US"), s_profiles.ColumnString16(8));
   2208     EXPECT_EQ(1386046800, s_profiles.ColumnInt(9));
   2209     EXPECT_EQ(ASCIIToUTF16("Chrome settings"), s_profiles.ColumnString16(10));
   2210 
   2211     // Only address line 2.
   2212     ASSERT_TRUE(s_profiles.Step());
   2213     EXPECT_EQ("00000000-0000-0000-0000-000000000003",
   2214               s_profiles.ColumnString(0));
   2215     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(1));
   2216     EXPECT_EQ(ASCIIToUTF16("\nOnly line 2???"), s_profiles.ColumnString16(2));
   2217     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(3));
   2218     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(4));
   2219     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(5));
   2220     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(6));
   2221     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(7));
   2222     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(8));
   2223     EXPECT_EQ(1386046834, s_profiles.ColumnInt(9));
   2224     EXPECT_EQ(ASCIIToUTF16("Chrome settings"), s_profiles.ColumnString16(10));
   2225 
   2226     // No address lines.
   2227     ASSERT_TRUE(s_profiles.Step());
   2228     EXPECT_EQ("00000000-0000-0000-0000-000000000004",
   2229               s_profiles.ColumnString(0));
   2230     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(1));
   2231     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(2));
   2232     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(3));
   2233     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(4));
   2234     EXPECT_EQ(ASCIIToUTF16("Texas"), s_profiles.ColumnString16(5));
   2235     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(6));
   2236     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(7));
   2237     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(8));
   2238     EXPECT_EQ(1386046847, s_profiles.ColumnInt(9));
   2239     EXPECT_EQ(ASCIIToUTF16("Chrome settings"), s_profiles.ColumnString16(10));
   2240 
   2241     // That should be it.
   2242     EXPECT_FALSE(s_profiles.Step());
   2243 
   2244     // Verify the phone number data as well.
   2245     sql::Statement s_phones(
   2246         connection.GetUniqueStatement(
   2247             "SELECT guid, number FROM autofill_profile_phones"));
   2248 
   2249     ASSERT_TRUE(s_phones.Step());
   2250     EXPECT_EQ("00000000-0000-0000-0000-000000000001", s_phones.ColumnString(0));
   2251     EXPECT_EQ(ASCIIToUTF16("1.800.555.1234"), s_phones.ColumnString16(1));
   2252 
   2253     ASSERT_TRUE(s_phones.Step());
   2254     EXPECT_EQ("00000000-0000-0000-0000-000000000001", s_phones.ColumnString(0));
   2255     EXPECT_EQ(ASCIIToUTF16("+1 (800) 555-4321"), s_phones.ColumnString16(1));
   2256 
   2257     ASSERT_TRUE(s_phones.Step());
   2258     EXPECT_EQ("00000000-0000-0000-0000-000000000002", s_phones.ColumnString(0));
   2259     EXPECT_EQ(base::string16(), s_phones.ColumnString16(1));
   2260 
   2261     ASSERT_TRUE(s_phones.Step());
   2262     EXPECT_EQ("00000000-0000-0000-0000-000000000003", s_phones.ColumnString(0));
   2263     EXPECT_EQ(ASCIIToUTF16("6505557890"), s_phones.ColumnString16(1));
   2264 
   2265     ASSERT_TRUE(s_phones.Step());
   2266     EXPECT_EQ("00000000-0000-0000-0000-000000000004", s_phones.ColumnString(0));
   2267     EXPECT_EQ(base::string16(), s_phones.ColumnString16(1));
   2268 
   2269     EXPECT_FALSE(s_phones.Step());
   2270   }
   2271 }
   2272