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/string_util.h"
     16 #include "base/strings/utf_string_conversions.h"
     17 #include "base/time/time.h"
     18 #include "base/values.h"
     19 #include "chrome/browser/webdata/keyword_table.h"
     20 #include "chrome/browser/webdata/logins_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/signin/core/browser/webdata/token_service_table.h"
     31 #include "components/webdata/common/web_database.h"
     32 #include "sql/statement.h"
     33 #include "testing/gtest/include/gtest/gtest.h"
     34 
     35 using autofill::AutofillProfile;
     36 using autofill::AutofillTable;
     37 using autofill::CreditCard;
     38 using base::ASCIIToUTF16;
     39 using base::Time;
     40 
     41 namespace {
     42 
     43 void AutofillProfile31FromStatement(const sql::Statement& s,
     44                                     AutofillProfile* profile,
     45                                     base::string16* label,
     46                                     int* unique_id,
     47                                     int64* date_modified) {
     48   DCHECK(profile);
     49   DCHECK(label);
     50   DCHECK(unique_id);
     51   DCHECK(date_modified);
     52   *label = s.ColumnString16(0);
     53   *unique_id = s.ColumnInt(1);
     54   profile->SetRawInfo(autofill::NAME_FIRST, s.ColumnString16(2));
     55   profile->SetRawInfo(autofill::NAME_MIDDLE, s.ColumnString16(3));
     56   profile->SetRawInfo(autofill::NAME_LAST, s.ColumnString16(4));
     57   profile->SetRawInfo(autofill::EMAIL_ADDRESS, s.ColumnString16(5));
     58   profile->SetRawInfo(autofill::COMPANY_NAME, s.ColumnString16(6));
     59   profile->SetRawInfo(autofill::ADDRESS_HOME_LINE1, s.ColumnString16(7));
     60   profile->SetRawInfo(autofill::ADDRESS_HOME_LINE2, s.ColumnString16(8));
     61   profile->SetRawInfo(autofill::ADDRESS_HOME_CITY, s.ColumnString16(9));
     62   profile->SetRawInfo(autofill::ADDRESS_HOME_STATE, s.ColumnString16(10));
     63   profile->SetRawInfo(autofill::ADDRESS_HOME_ZIP, s.ColumnString16(11));
     64   profile->SetInfo(
     65       autofill::AutofillType(autofill::ADDRESS_HOME_COUNTRY),
     66       s.ColumnString16(12), "en-US");
     67   profile->SetRawInfo(autofill::PHONE_HOME_WHOLE_NUMBER, s.ColumnString16(13));
     68   *date_modified = s.ColumnInt64(15);
     69   profile->set_guid(s.ColumnString(16));
     70   EXPECT_TRUE(base::IsValidGUID(profile->guid()));
     71 }
     72 
     73 void AutofillProfile33FromStatement(const sql::Statement& s,
     74                                     AutofillProfile* profile,
     75                                     int64* date_modified) {
     76   DCHECK(profile);
     77   DCHECK(date_modified);
     78   profile->set_guid(s.ColumnString(0));
     79   EXPECT_TRUE(base::IsValidGUID(profile->guid()));
     80   profile->SetRawInfo(autofill::COMPANY_NAME, s.ColumnString16(1));
     81   profile->SetRawInfo(autofill::ADDRESS_HOME_STREET_ADDRESS,
     82                       s.ColumnString16(2));
     83   profile->SetRawInfo(autofill::ADDRESS_HOME_CITY, s.ColumnString16(3));
     84   profile->SetRawInfo(autofill::ADDRESS_HOME_STATE, s.ColumnString16(4));
     85   profile->SetRawInfo(autofill::ADDRESS_HOME_ZIP, s.ColumnString16(5));
     86   profile->SetInfo(
     87       autofill::AutofillType(autofill::ADDRESS_HOME_COUNTRY),
     88       s.ColumnString16(6), "en-US");
     89   *date_modified = s.ColumnInt64(7);
     90 }
     91 
     92 void CreditCard31FromStatement(const sql::Statement& s,
     93                               CreditCard* credit_card,
     94                               base::string16* label,
     95                               int* unique_id,
     96                               std::string* encrypted_number,
     97                               int64* date_modified) {
     98   DCHECK(credit_card);
     99   DCHECK(label);
    100   DCHECK(unique_id);
    101   DCHECK(encrypted_number);
    102   DCHECK(date_modified);
    103   *label = s.ColumnString16(0);
    104   *unique_id = s.ColumnInt(1);
    105   credit_card->SetRawInfo(autofill::CREDIT_CARD_NAME, s.ColumnString16(2));
    106   credit_card->SetRawInfo(autofill::CREDIT_CARD_TYPE, s.ColumnString16(3));
    107   credit_card->SetRawInfo(autofill::CREDIT_CARD_EXP_MONTH, s.ColumnString16(5));
    108   credit_card->SetRawInfo(
    109       autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR, s.ColumnString16(6));
    110   int encrypted_number_len = s.ColumnByteLength(10);
    111   if (encrypted_number_len) {
    112     encrypted_number->resize(encrypted_number_len);
    113     memcpy(&(*encrypted_number)[0], s.ColumnBlob(10), encrypted_number_len);
    114   }
    115   *date_modified = s.ColumnInt64(12);
    116   credit_card->set_guid(s.ColumnString(13));
    117   EXPECT_TRUE(base::IsValidGUID(credit_card->guid()));
    118 }
    119 
    120 void CreditCard32FromStatement(const sql::Statement& s,
    121                                CreditCard* credit_card,
    122                                std::string* encrypted_number,
    123                                int64* date_modified) {
    124   DCHECK(credit_card);
    125   DCHECK(encrypted_number);
    126   DCHECK(date_modified);
    127   credit_card->set_guid(s.ColumnString(0));
    128   EXPECT_TRUE(base::IsValidGUID(credit_card->guid()));
    129   credit_card->SetRawInfo(autofill::CREDIT_CARD_NAME, s.ColumnString16(1));
    130   credit_card->SetRawInfo(autofill::CREDIT_CARD_EXP_MONTH, s.ColumnString16(2));
    131   credit_card->SetRawInfo(
    132       autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR, s.ColumnString16(3));
    133   int encrypted_number_len = s.ColumnByteLength(4);
    134   if (encrypted_number_len) {
    135     encrypted_number->resize(encrypted_number_len);
    136     memcpy(&(*encrypted_number)[0], s.ColumnBlob(4), encrypted_number_len);
    137   }
    138   *date_modified = s.ColumnInt64(5);
    139 }
    140 
    141 void CheckHasBackupData(sql::MetaTable* meta_table) {
    142   std::string value;
    143   EXPECT_TRUE(meta_table->GetValue(
    144       "Default Search Provider ID Backup", &value));
    145   EXPECT_TRUE(meta_table->GetValue(
    146       "Default Search Provider ID Backup Signature", &value));
    147 }
    148 
    149 void CheckNoBackupData(const sql::Connection& connection,
    150                        sql::MetaTable* meta_table) {
    151   std::string value;
    152   EXPECT_FALSE(meta_table->GetValue(
    153       "Default Search Provider ID Backup", &value));
    154   EXPECT_FALSE(meta_table->GetValue(
    155       "Default Search Provider ID Backup Signature", &value));
    156   EXPECT_FALSE(connection.DoesTableExist("keywords_backup"));
    157 }
    158 
    159 std::string RemoveQuotes(const std::string& has_quotes) {
    160   std::string no_quotes;
    161   // SQLite quotes: http://www.sqlite.org/lang_keywords.html
    162   base::RemoveChars(has_quotes, "\"[]`", &no_quotes);
    163   return no_quotes;
    164 }
    165 
    166 }  // anonymous namespace
    167 
    168 // The WebDatabaseMigrationTest encapsulates testing of database migrations.
    169 // Specifically, these tests are intended to exercise any schema changes in
    170 // the WebDatabase and data migrations that occur in
    171 // |WebDatabase::MigrateOldVersionsAsNeeded()|.
    172 class WebDatabaseMigrationTest : public testing::Test {
    173  public:
    174   WebDatabaseMigrationTest() {}
    175   virtual ~WebDatabaseMigrationTest() {}
    176 
    177   virtual void SetUp() {
    178     ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
    179   }
    180 
    181   // Load the database via the WebDatabase class and migrate the database to
    182   // the current version.
    183   void DoMigration() {
    184     // TODO(joi): This whole unit test file needs to stay in //chrome
    185     // for now, as it needs to know about all the different table
    186     // types. Once all webdata datatypes have been componentized, this
    187     // could move to components_unittests.
    188     AutofillTable autofill_table("en-US");
    189     KeywordTable keyword_table;
    190     LoginsTable logins_table;
    191     TokenServiceTable token_service_table;
    192     WebAppsTable web_apps_table;
    193     WebIntentsTable web_intents_table;
    194 
    195     WebDatabase db;
    196     db.AddTable(&autofill_table);
    197     db.AddTable(&keyword_table);
    198     db.AddTable(&logins_table);
    199     db.AddTable(&token_service_table);
    200     db.AddTable(&web_apps_table);
    201     db.AddTable(&web_intents_table);
    202 
    203     // This causes the migration to occur.
    204     ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath()));
    205   }
    206 
    207  protected:
    208   // Current tested version number.  When adding a migration in
    209   // |WebDatabase::MigrateOldVersionsAsNeeded()| and changing the version number
    210   // |kCurrentVersionNumber| this value should change to reflect the new version
    211   // number and a new migration test added below.
    212   static const int kCurrentTestedVersionNumber;
    213 
    214   base::FilePath GetDatabasePath() {
    215     const base::FilePath::CharType kWebDatabaseFilename[] =
    216         FILE_PATH_LITERAL("TestWebDatabase.sqlite3");
    217     return temp_dir_.path().Append(base::FilePath(kWebDatabaseFilename));
    218   }
    219 
    220   // The textual contents of |file| are read from
    221   // "components/test/data/web_database" and returned in the string |contents|.
    222   // Returns true if the file exists and is read successfully, false otherwise.
    223   bool GetWebDatabaseData(const base::FilePath& file, std::string* contents) {
    224     base::FilePath source_path;
    225     PathService::Get(base::DIR_SOURCE_ROOT, &source_path);
    226     source_path = source_path.AppendASCII("components");
    227     source_path = source_path.AppendASCII("test");
    228     source_path = source_path.AppendASCII("data");
    229     source_path = source_path.AppendASCII("web_database");
    230     source_path = source_path.Append(file);
    231     return base::PathExists(source_path) &&
    232         base::ReadFileToString(source_path, contents);
    233   }
    234 
    235   static int VersionFromConnection(sql::Connection* connection) {
    236     // Get version.
    237     sql::Statement s(connection->GetUniqueStatement(
    238         "SELECT value FROM meta WHERE key='version'"));
    239     if (!s.Step())
    240       return 0;
    241     return s.ColumnInt(0);
    242   }
    243 
    244   // The sql files located in "chrome/test/data/web_database" were generated by
    245   // launching the Chromium application prior to schema change, then using the
    246   // sqlite3 command-line application to dump the contents of the "Web Data"
    247   // database.
    248   // Like this:
    249   //   > .output version_nn.sql
    250   //   > .dump
    251   void LoadDatabase(const base::FilePath::StringType& file);
    252 
    253  private:
    254   base::ScopedTempDir temp_dir_;
    255 
    256   DISALLOW_COPY_AND_ASSIGN(WebDatabaseMigrationTest);
    257 };
    258 
    259 const int WebDatabaseMigrationTest::kCurrentTestedVersionNumber = 57;
    260 
    261 void WebDatabaseMigrationTest::LoadDatabase(
    262     const base::FilePath::StringType& file) {
    263   std::string contents;
    264   ASSERT_TRUE(GetWebDatabaseData(base::FilePath(file), &contents));
    265 
    266   sql::Connection connection;
    267   ASSERT_TRUE(connection.Open(GetDatabasePath()));
    268   ASSERT_TRUE(connection.Execute(contents.data()));
    269 }
    270 
    271 // Tests that migrating from the golden files version_XX.sql results in the same
    272 // schema as migrating from an empty database.
    273 TEST_F(WebDatabaseMigrationTest, VersionXxSqlFilesAreGolden) {
    274   DoMigration();
    275   sql::Connection connection;
    276   ASSERT_TRUE(connection.Open(GetDatabasePath()));
    277   const std::string& expected_schema = RemoveQuotes(connection.GetSchema());
    278   static const int kFirstVersion = 53;
    279   for (int i = kFirstVersion; i < kCurrentTestedVersionNumber; ++i) {
    280     connection.Raze();
    281     const base::FilePath& file_name = base::FilePath::FromUTF8Unsafe(
    282         "version_" + base::IntToString(i) + ".sql");
    283     ASSERT_NO_FATAL_FAILURE(LoadDatabase(file_name.value()))
    284         << "Failed to load " << file_name.MaybeAsASCII();
    285     DoMigration();
    286     EXPECT_EQ(expected_schema, RemoveQuotes(connection.GetSchema()));
    287   }
    288 }
    289 
    290 // Tests that the all migrations from an empty database succeed.
    291 TEST_F(WebDatabaseMigrationTest, MigrateEmptyToCurrent) {
    292   DoMigration();
    293 
    294   // Verify post-conditions.  These are expectations for current version of the
    295   // database.
    296   {
    297     sql::Connection connection;
    298     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    299 
    300     // Check version.
    301     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
    302 
    303     // Check that expected tables are present.
    304     EXPECT_TRUE(connection.DoesTableExist("autofill"));
    305     // The autofill_dates table is obsolete. (It's been merged into the autofill
    306     // table.)
    307     EXPECT_FALSE(connection.DoesTableExist("autofill_dates"));
    308     EXPECT_TRUE(connection.DoesTableExist("autofill_profiles"));
    309     EXPECT_TRUE(connection.DoesTableExist("credit_cards"));
    310     EXPECT_TRUE(connection.DoesTableExist("keywords"));
    311     // The logins table is obsolete. (We used to store saved passwords here.)
    312     EXPECT_FALSE(connection.DoesTableExist("logins"));
    313     EXPECT_TRUE(connection.DoesTableExist("meta"));
    314     EXPECT_TRUE(connection.DoesTableExist("token_service"));
    315     EXPECT_TRUE(connection.DoesTableExist("web_app_icons"));
    316     EXPECT_TRUE(connection.DoesTableExist("web_apps"));
    317     EXPECT_TRUE(connection.DoesTableExist("web_intents"));
    318     EXPECT_TRUE(connection.DoesTableExist("web_intents_defaults"));
    319   }
    320 }
    321 
    322 // Tests that absent Autofill tables do not create any problems when migrating
    323 // from a DB written by the earliest publicly released version of Chrome.
    324 TEST_F(WebDatabaseMigrationTest, MigrateVersion20ToCurrent) {
    325   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_20.sql")));
    326 
    327   // Verify pre-conditions.
    328   {
    329     sql::Connection connection;
    330     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    331 
    332     EXPECT_FALSE(connection.DoesTableExist("autofill"));
    333     EXPECT_FALSE(connection.DoesTableExist("autofill_profiles"));
    334     EXPECT_FALSE(connection.DoesTableExist("credit_cards"));
    335   }
    336 
    337   DoMigration();
    338 
    339   // Verify post-conditions.  These are expectations for current version of the
    340   // database.
    341   {
    342     sql::Connection connection;
    343     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    344 
    345     // Check version.
    346     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
    347 
    348     // Mostly this test just verifies that no SQL errors occur during migration;
    349     // but might as well verify that the tables were created as well.
    350     EXPECT_TRUE(connection.DoesTableExist("autofill"));
    351     EXPECT_TRUE(connection.DoesTableExist("autofill_profiles"));
    352     EXPECT_TRUE(connection.DoesTableExist("credit_cards"));
    353   }
    354 }
    355 
    356 // Tests that rows with empty values get removed from the autofill tables.
    357 TEST_F(WebDatabaseMigrationTest, MigrateVersion21ToCurrent) {
    358   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_21.sql")));
    359 
    360   // Verify pre-conditions.
    361   {
    362     sql::Connection connection;
    363     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    364 
    365     // Both empty and non-empty values are allowed in a version 21 database.
    366     sql::Statement s_autofill(connection.GetUniqueStatement(
    367         "SELECT name, value, value_lower, pair_id, count FROM autofill"));
    368     sql::Statement s_dates(connection.GetUniqueStatement(
    369         "SELECT pair_id, date_created FROM autofill_dates"));
    370 
    371     // An entry with a non-empty value.
    372     ASSERT_TRUE(s_autofill.Step());
    373     EXPECT_EQ(ASCIIToUTF16("Name"), s_autofill.ColumnString16(0));
    374     EXPECT_EQ(ASCIIToUTF16("John Doe"), s_autofill.ColumnString16(1));
    375     EXPECT_EQ(ASCIIToUTF16("john doe"), s_autofill.ColumnString16(2));
    376     EXPECT_EQ(10, s_autofill.ColumnInt(3));
    377     EXPECT_EQ(1, s_autofill.ColumnInt(4));
    378     ASSERT_TRUE(s_dates.Step());
    379     EXPECT_EQ(10, s_dates.ColumnInt(0));
    380     EXPECT_EQ(1384299100, s_dates.ColumnInt64(1));
    381 
    382     // An entry with an empty value.
    383     ASSERT_TRUE(s_autofill.Step());
    384     EXPECT_EQ(ASCIIToUTF16("Name"), s_autofill.ColumnString16(0));
    385     EXPECT_EQ(base::string16(), s_autofill.ColumnString16(1));
    386     EXPECT_EQ(base::string16(), s_autofill.ColumnString16(2));
    387     EXPECT_EQ(11, s_autofill.ColumnInt(3));
    388     EXPECT_EQ(1, s_autofill.ColumnInt(4));
    389     ASSERT_TRUE(s_dates.Step());
    390     EXPECT_EQ(11, s_dates.ColumnInt(0));
    391     EXPECT_EQ(1384299200, s_dates.ColumnInt64(1));
    392 
    393     // Another entry with a non-empty value.
    394     ASSERT_TRUE(s_autofill.Step());
    395     EXPECT_EQ(ASCIIToUTF16("Email"), s_autofill.ColumnString16(0));
    396     EXPECT_EQ(ASCIIToUTF16("jane (at) example.com"), s_autofill.ColumnString16(1));
    397     EXPECT_EQ(ASCIIToUTF16("jane (at) example.com"), s_autofill.ColumnString16(2));
    398     EXPECT_EQ(20, s_autofill.ColumnInt(3));
    399     EXPECT_EQ(3, s_autofill.ColumnInt(4));
    400     ASSERT_TRUE(s_dates.Step());
    401     EXPECT_EQ(20, s_dates.ColumnInt(0));
    402     EXPECT_EQ(1384299300, s_dates.ColumnInt64(1));
    403     ASSERT_TRUE(s_dates.Step());
    404     EXPECT_EQ(20, s_dates.ColumnInt(0));
    405     EXPECT_EQ(1384299301, s_dates.ColumnInt64(1));
    406 
    407     // Another entry with an empty value.
    408     ASSERT_TRUE(s_autofill.Step());
    409     EXPECT_EQ(ASCIIToUTF16("Email"), s_autofill.ColumnString16(0));
    410     EXPECT_EQ(base::string16(), s_autofill.ColumnString16(1));
    411     EXPECT_EQ(base::string16(), s_autofill.ColumnString16(2));
    412     EXPECT_EQ(21, s_autofill.ColumnInt(3));
    413     EXPECT_EQ(4, s_autofill.ColumnInt(4));
    414     ASSERT_TRUE(s_dates.Step());
    415     EXPECT_EQ(21, s_dates.ColumnInt(0));
    416     EXPECT_EQ(1384299401, s_dates.ColumnInt64(1));
    417     ASSERT_TRUE(s_dates.Step());
    418     EXPECT_EQ(21, s_dates.ColumnInt(0));
    419     EXPECT_EQ(1384299400, s_dates.ColumnInt64(1));
    420     ASSERT_TRUE(s_dates.Step());
    421     EXPECT_EQ(21, s_dates.ColumnInt(0));
    422     EXPECT_EQ(1384299403, s_dates.ColumnInt64(1));
    423     ASSERT_TRUE(s_dates.Step());
    424     EXPECT_EQ(21, s_dates.ColumnInt(0));
    425     EXPECT_EQ(1384299402, s_dates.ColumnInt64(1));
    426 
    427     // No more entries expected.
    428     ASSERT_FALSE(s_autofill.Step());
    429     ASSERT_FALSE(s_dates.Step());
    430   }
    431 
    432   DoMigration();
    433 
    434   // Verify post-conditions.  These are expectations for current version of the
    435   // database.
    436   {
    437     sql::Connection connection;
    438     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    439 
    440     // Check version.
    441     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
    442 
    443     // Entries with empty values should have been dropped.  The remaining
    444     // entries should have been preserved.
    445     sql::Statement s(
    446         connection.GetUniqueStatement(
    447             "SELECT name, value, value_lower, date_created, date_last_used,"
    448             " count "
    449             "FROM autofill "
    450             "ORDER BY name, value ASC"));
    451 
    452     // "jane (at) example.com"
    453     ASSERT_TRUE(s.Step());
    454     EXPECT_EQ(ASCIIToUTF16("Email"), s.ColumnString16(0));
    455     EXPECT_EQ(ASCIIToUTF16("jane (at) example.com"), s.ColumnString16(1));
    456     EXPECT_EQ(ASCIIToUTF16("jane (at) example.com"), s.ColumnString16(2));
    457     EXPECT_EQ(1384299300, s.ColumnInt64(3));
    458     EXPECT_EQ(1384299301, s.ColumnInt64(4));
    459     EXPECT_EQ(3, s.ColumnInt(5));
    460 
    461     // "John Doe"
    462     ASSERT_TRUE(s.Step());
    463     EXPECT_EQ(ASCIIToUTF16("Name"), s.ColumnString16(0));
    464     EXPECT_EQ(ASCIIToUTF16("John Doe"), s.ColumnString16(1));
    465     EXPECT_EQ(ASCIIToUTF16("john doe"), s.ColumnString16(2));
    466     EXPECT_EQ(1384299100, s.ColumnInt64(3));
    467     EXPECT_EQ(1384299100, s.ColumnInt64(4));
    468     EXPECT_EQ(1, s.ColumnInt(5));
    469 
    470     // No more entries expected.
    471     ASSERT_FALSE(s.Step());
    472   }
    473 }
    474 
    475 // Tests that the |credit_card| table gets added to the schema for a version 22
    476 // database.
    477 TEST_F(WebDatabaseMigrationTest, MigrateVersion22ToCurrent) {
    478   // This schema is taken from a build prior to the addition of the
    479   // |credit_card| table.  Version 22 of the schema.  Contrast this with the
    480   // corrupt version below.
    481   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_22.sql")));
    482 
    483   // Verify pre-conditions.
    484   {
    485     sql::Connection connection;
    486     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    487 
    488     // No |credit_card| table prior to version 23.
    489     ASSERT_FALSE(connection.DoesColumnExist("credit_cards", "guid"));
    490     ASSERT_FALSE(
    491         connection.DoesColumnExist("credit_cards", "card_number_encrypted"));
    492   }
    493 
    494   DoMigration();
    495 
    496   // Verify post-conditions.  These are expectations for current version of the
    497   // database.
    498   {
    499     sql::Connection connection;
    500     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    501 
    502     // Check version.
    503     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
    504 
    505     // |credit_card| table now exists.
    506     EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "guid"));
    507     EXPECT_TRUE(
    508         connection.DoesColumnExist("credit_cards", "card_number_encrypted"));
    509   }
    510 }
    511 
    512 // Tests that the |credit_card| table gets added to the schema for a corrupt
    513 // version 22 database.  The corruption is that the |credit_cards| table exists
    514 // but the schema version number was not set correctly to 23 or later.  This
    515 // test exercises code introduced to fix bug http://crbug.com/50699 that
    516 // resulted from the corruption.
    517 TEST_F(WebDatabaseMigrationTest, MigrateVersion22CorruptedToCurrent) {
    518   // This schema is taken from a build after the addition of the |credit_card|
    519   // table.  Due to a bug in the migration logic the version is set incorrectly
    520   // to 22 (it should have been updated to 23 at least).
    521   ASSERT_NO_FATAL_FAILURE(
    522       LoadDatabase(FILE_PATH_LITERAL("version_22_corrupt.sql")));
    523 
    524   // Verify pre-conditions.  These are expectations for corrupt version 22 of
    525   // the database.
    526   {
    527     sql::Connection connection;
    528     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    529 
    530     // Columns existing and not existing before current version.
    531     ASSERT_TRUE(connection.DoesColumnExist("credit_cards", "unique_id"));
    532     ASSERT_TRUE(
    533         connection.DoesColumnExist("credit_cards", "card_number_encrypted"));
    534     ASSERT_TRUE(connection.DoesColumnExist("keywords", "id"));
    535   }
    536 
    537   DoMigration();
    538 
    539   // Verify post-conditions.  These are expectations for current version of the
    540   // database.
    541   {
    542     sql::Connection connection;
    543     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    544 
    545     // Check version.
    546     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
    547 
    548 
    549     // Columns existing and not existing before version 25.
    550     EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "unique_id"));
    551     EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "guid"));
    552     EXPECT_TRUE(
    553         connection.DoesColumnExist("credit_cards", "card_number_encrypted"));
    554     EXPECT_TRUE(connection.DoesColumnExist("keywords", "id"));
    555   }
    556 }
    557 
    558 // Tests that the |keywords| |created_by_policy| column gets added to the schema
    559 // for a version 25 database.
    560 TEST_F(WebDatabaseMigrationTest, MigrateVersion25ToCurrent) {
    561   // This schema is taken from a build prior to the addition of the |keywords|
    562   // |created_by_policy| column.
    563   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_25.sql")));
    564 
    565   // Verify pre-conditions.  These are expectations for version 25 of the
    566   // database.
    567   {
    568     sql::Connection connection;
    569     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    570   }
    571 
    572   DoMigration();
    573 
    574   // Verify post-conditions.  These are expectations for current version of the
    575   // database.
    576   {
    577     sql::Connection connection;
    578     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    579 
    580     // Check version.
    581     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
    582 
    583     // |keywords| |created_by_policy| column should have been added.
    584     EXPECT_TRUE(connection.DoesColumnExist("keywords", "id"));
    585     EXPECT_TRUE(connection.DoesColumnExist("keywords", "created_by_policy"));
    586   }
    587 }
    588 
    589 // Tests that the credit_cards.billing_address column is changed from a string
    590 // to an int whilst preserving the associated billing address. This version of
    591 // the test makes sure a stored label is converted to an ID.
    592 TEST_F(WebDatabaseMigrationTest, MigrateVersion26ToCurrentStringLabels) {
    593   // This schema is taken from a build prior to the change of column type for
    594   // credit_cards.billing_address from string to int.
    595   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_26.sql")));
    596 
    597   // Verify pre-conditions. These are expectations for version 26 of the
    598   // database.
    599   {
    600     sql::Connection connection;
    601     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    602 
    603     // Columns existing and not existing before current version.
    604     EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "billing_address"));
    605 
    606     std::string stmt = "INSERT INTO autofill_profiles"
    607       "(label, unique_id, first_name, middle_name, last_name, email,"
    608       " company_name, address_line_1, address_line_2, city, state, zipcode,"
    609       " country, phone, fax)"
    610       "VALUES ('Home',1,'','','','','','','','','','','','','')";
    611     sql::Statement s(connection.GetUniqueStatement(stmt.c_str()));
    612     ASSERT_TRUE(s.Run());
    613 
    614     // Insert a CC linked to an existing address.
    615     std::string stmt2 = "INSERT INTO credit_cards"
    616       "(label, unique_id, name_on_card, type, card_number,"
    617       " expiration_month, expiration_year, verification_code, billing_address,"
    618       " shipping_address, card_number_encrypted, verification_code_encrypted)"
    619       "VALUES ('label',2,'Jack','Visa','1234',2,2012,'','Home','','','')";
    620     sql::Statement s2(connection.GetUniqueStatement(stmt2.c_str()));
    621     ASSERT_TRUE(s2.Run());
    622 
    623     // |billing_address| is a string.
    624     std::string stmt3 = "SELECT billing_address FROM credit_cards";
    625     sql::Statement s3(connection.GetUniqueStatement(stmt3.c_str()));
    626     ASSERT_TRUE(s3.Step());
    627     EXPECT_EQ(s3.ColumnType(0), sql::COLUMN_TYPE_TEXT);
    628   }
    629 
    630   DoMigration();
    631 
    632   // Verify post-conditions.  These are expectations for current version of the
    633   // database.
    634   {
    635     sql::Connection connection;
    636     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    637 
    638     // Check version.
    639     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
    640     EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "billing_address"));
    641 
    642     // Verify the credit card data is converted.
    643     sql::Statement s(connection.GetUniqueStatement(
    644         "SELECT guid, name_on_card, expiration_month, expiration_year, "
    645         "card_number_encrypted, date_modified "
    646         "FROM credit_cards"));
    647     ASSERT_TRUE(s.Step());
    648     EXPECT_EQ("Jack", s.ColumnString(1));
    649     EXPECT_EQ(2, s.ColumnInt(2));
    650     EXPECT_EQ(2012, s.ColumnInt(3));
    651     // Column 5 is encrypted number blob.
    652     // Column 6 is date_modified.
    653   }
    654 }
    655 
    656 // Tests that the credit_cards.billing_address column is changed from a string
    657 // to an int whilst preserving the associated billing address. This version of
    658 // the test makes sure a stored string ID is converted to an integer ID.
    659 TEST_F(WebDatabaseMigrationTest, MigrateVersion26ToCurrentStringIDs) {
    660   // This schema is taken from a build prior to the change of column type for
    661   // credit_cards.billing_address from string to int.
    662   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_26.sql")));
    663 
    664   // Verify pre-conditions. These are expectations for version 26 of the
    665   // database.
    666   {
    667     sql::Connection connection;
    668     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    669     EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "billing_address"));
    670 
    671     std::string stmt = "INSERT INTO autofill_profiles"
    672       "(label, unique_id, first_name, middle_name, last_name, email,"
    673       " company_name, address_line_1, address_line_2, city, state, zipcode,"
    674       " country, phone, fax)"
    675       "VALUES ('Home',1,'','','','','','','','','','','','','')";
    676     sql::Statement s(connection.GetUniqueStatement(stmt.c_str()));
    677     ASSERT_TRUE(s.Run());
    678 
    679     // Insert a CC linked to an existing address.
    680     std::string stmt2 = "INSERT INTO credit_cards"
    681       "(label, unique_id, name_on_card, type, card_number,"
    682       " expiration_month, expiration_year, verification_code, billing_address,"
    683       " shipping_address, card_number_encrypted, verification_code_encrypted)"
    684       "VALUES ('label',2,'Jack','Visa','1234',2,2012,'','1','','','')";
    685     sql::Statement s2(connection.GetUniqueStatement(stmt2.c_str()));
    686     ASSERT_TRUE(s2.Run());
    687 
    688     // |billing_address| is a string.
    689     std::string stmt3 = "SELECT billing_address FROM credit_cards";
    690     sql::Statement s3(connection.GetUniqueStatement(stmt3.c_str()));
    691     ASSERT_TRUE(s3.Step());
    692     EXPECT_EQ(s3.ColumnType(0), sql::COLUMN_TYPE_TEXT);
    693   }
    694 
    695   DoMigration();
    696 
    697   // Verify post-conditions.  These are expectations for current version of the
    698   // database.
    699   {
    700     sql::Connection connection;
    701     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    702 
    703     // Check version.
    704     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
    705 
    706     // |keywords| |created_by_policy| column should have been added.
    707     EXPECT_TRUE(connection.DoesColumnExist("keywords", "id"));
    708     EXPECT_TRUE(connection.DoesColumnExist("keywords", "created_by_policy"));
    709     EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "billing_address"));
    710 
    711     // Verify the credit card data is converted.
    712     sql::Statement s(connection.GetUniqueStatement(
    713         "SELECT guid, name_on_card, expiration_month, expiration_year, "
    714         "card_number_encrypted, date_modified "
    715         "FROM credit_cards"));
    716     ASSERT_TRUE(s.Step());
    717     EXPECT_EQ("Jack", s.ColumnString(1));
    718     EXPECT_EQ(2, s.ColumnInt(2));
    719     EXPECT_EQ(2012, s.ColumnInt(3));
    720     // Column 5 is encrypted credit card number blo b.
    721     // Column 6 is date_modified.
    722   }
    723 }
    724 
    725 // Makes sure instant_url is added correctly to keywords.
    726 TEST_F(WebDatabaseMigrationTest, MigrateVersion27ToCurrent) {
    727   // Initialize the database.
    728   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_27.sql")));
    729 
    730   // Verify pre-conditions. These are expectations for version 27 of the
    731   // database.
    732   {
    733     sql::Connection connection;
    734     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    735 
    736     ASSERT_FALSE(connection.DoesColumnExist("keywords", "instant_url"));
    737   }
    738 
    739   DoMigration();
    740 
    741   // Verify post-conditions.  These are expectations for current version of the
    742   // database.
    743   {
    744     sql::Connection connection;
    745     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    746 
    747     // Check version.
    748     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
    749 
    750     // Make sure supports_instant (added in Version 28) was ultimately dropped
    751     // again and instant_url was added.
    752     EXPECT_FALSE(connection.DoesColumnExist("keywords", "supports_instant"));
    753     EXPECT_TRUE(connection.DoesColumnExist("keywords", "instant_url"));
    754 
    755     // Check that instant_url is empty.
    756     std::string stmt = "SELECT instant_url FROM keywords";
    757     sql::Statement s(connection.GetUniqueStatement(stmt.c_str()));
    758     ASSERT_TRUE(s.Step());
    759     EXPECT_EQ(std::string(), s.ColumnString(0));
    760 
    761     // Verify the data made it over.
    762     stmt = "SELECT " + KeywordTable::GetKeywordColumns() + " FROM keywords";
    763     sql::Statement s2(connection.GetUniqueStatement(stmt.c_str()));
    764     ASSERT_TRUE(s2.Step());
    765     EXPECT_EQ(2, s2.ColumnInt(0));
    766     EXPECT_EQ("Google", s2.ColumnString(1));
    767     EXPECT_EQ("google.com", s2.ColumnString(2));
    768     EXPECT_EQ("http://www.google.com/favicon.ico", s2.ColumnString(3));
    769     EXPECT_EQ("{google:baseURL}search?{google:RLZ}{google:acceptedSuggestion}"\
    770         "{google:originalQueryForSuggestion}sourceid=chrome&ie={inputEncoding}"\
    771         "&q={searchTerms}",
    772         s2.ColumnString(4));
    773     EXPECT_TRUE(s2.ColumnBool(5));
    774     EXPECT_EQ(std::string(), s2.ColumnString(6));
    775     EXPECT_EQ(0, s2.ColumnInt(7));
    776     EXPECT_EQ(0, s2.ColumnInt(8));
    777     EXPECT_EQ(std::string("UTF-8"), s2.ColumnString(9));
    778     EXPECT_TRUE(s2.ColumnBool(10));
    779     EXPECT_EQ(std::string("{google:baseSuggestURL}search?client=chrome&hl="
    780                           "{language}&q={searchTerms}"), s2.ColumnString(11));
    781     EXPECT_EQ(1, s2.ColumnInt(12));
    782     EXPECT_FALSE(s2.ColumnBool(13));
    783     EXPECT_EQ(std::string(), s2.ColumnString(14));
    784     EXPECT_EQ(0, s2.ColumnInt(15));
    785     EXPECT_EQ(std::string(), s2.ColumnString(16));
    786   }
    787 }
    788 
    789 // Makes sure date_modified is added correctly to autofill_profiles and
    790 // credit_cards.
    791 TEST_F(WebDatabaseMigrationTest, MigrateVersion29ToCurrent) {
    792   // Initialize the database.
    793   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_29.sql")));
    794 
    795   // Verify pre-conditions.  These are expectations for version 29 of the
    796   // database.
    797   {
    798     sql::Connection connection;
    799     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    800 
    801     EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles",
    802                                             "date_modified"));
    803     EXPECT_FALSE(connection.DoesColumnExist("credit_cards",
    804                                             "date_modified"));
    805   }
    806 
    807   Time pre_creation_time = Time::Now();
    808   DoMigration();
    809   Time post_creation_time = Time::Now();
    810 
    811   // Verify post-conditions.  These are expectations for current version of the
    812   // database.
    813   {
    814     sql::Connection connection;
    815     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    816 
    817     // Check version.
    818     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
    819 
    820     // Check that the columns were created.
    821     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
    822                                            "date_modified"));
    823     EXPECT_TRUE(connection.DoesColumnExist("credit_cards",
    824                                            "date_modified"));
    825 
    826     sql::Statement s_profiles(connection.GetUniqueStatement(
    827         "SELECT date_modified FROM autofill_profiles "));
    828     ASSERT_TRUE(s_profiles.is_valid());
    829     while (s_profiles.Step()) {
    830       EXPECT_GE(s_profiles.ColumnInt64(0),
    831                 pre_creation_time.ToTimeT());
    832       EXPECT_LE(s_profiles.ColumnInt64(0),
    833                 post_creation_time.ToTimeT());
    834     }
    835     EXPECT_TRUE(s_profiles.Succeeded());
    836 
    837     sql::Statement s_credit_cards(connection.GetUniqueStatement(
    838         "SELECT date_modified FROM credit_cards "));
    839     ASSERT_TRUE(s_credit_cards.is_valid());
    840     while (s_credit_cards.Step()) {
    841       EXPECT_GE(s_credit_cards.ColumnInt64(0),
    842                 pre_creation_time.ToTimeT());
    843       EXPECT_LE(s_credit_cards.ColumnInt64(0),
    844                 post_creation_time.ToTimeT());
    845     }
    846     EXPECT_TRUE(s_credit_cards.Succeeded());
    847   }
    848 }
    849 
    850 // Makes sure guids are added to autofill_profiles and credit_cards tables.
    851 TEST_F(WebDatabaseMigrationTest, MigrateVersion30ToCurrent) {
    852   // Initialize the database.
    853   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_30.sql")));
    854 
    855   // Verify pre-conditions. These are expectations for version 29 of the
    856   // database.
    857   {
    858     sql::Connection connection;
    859     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    860 
    861     EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "guid"));
    862     EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "guid"));
    863   }
    864 
    865   DoMigration();
    866 
    867   // Verify post-conditions.  These are expectations for current version of the
    868   // database.
    869   {
    870     sql::Connection connection;
    871     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    872 
    873     // Check version.
    874     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
    875 
    876     ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid"));
    877     ASSERT_TRUE(connection.DoesColumnExist("credit_cards", "guid"));
    878 
    879     // Check that guids are non-null, non-empty, conforms to guid format, and
    880     // are different.
    881     sql::Statement s(
    882         connection.GetUniqueStatement("SELECT guid FROM autofill_profiles"));
    883 
    884     ASSERT_TRUE(s.Step());
    885     std::string guid1 = s.ColumnString(0);
    886     EXPECT_TRUE(base::IsValidGUID(guid1));
    887 
    888     ASSERT_TRUE(s.Step());
    889     std::string guid2 = s.ColumnString(0);
    890     EXPECT_TRUE(base::IsValidGUID(guid2));
    891 
    892     EXPECT_NE(guid1, guid2);
    893   }
    894 }
    895 
    896 // Removes unique IDs and make GUIDs the primary key.  Also removes unused
    897 // columns.
    898 TEST_F(WebDatabaseMigrationTest, MigrateVersion31ToCurrent) {
    899   // Initialize the database.
    900   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_31.sql")));
    901 
    902   // Verify pre-conditions. These are expectations for version 30 of the
    903   // database.
    904   AutofillProfile profile;
    905   base::string16 profile_label;
    906   int profile_unique_id = 0;
    907   int64 profile_date_modified = 0;
    908   CreditCard credit_card;
    909   base::string16 cc_label;
    910   int cc_unique_id = 0;
    911   std::string cc_number_encrypted;
    912   int64 cc_date_modified = 0;
    913   {
    914     sql::Connection connection;
    915     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    916 
    917     // Verify existence of columns we'll be changing.
    918     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid"));
    919     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "unique_id"));
    920     EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "guid"));
    921     EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "unique_id"));
    922     EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "type"));
    923     EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "card_number"));
    924     EXPECT_TRUE(connection.DoesColumnExist("credit_cards",
    925                                            "verification_code"));
    926     EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "billing_address"));
    927     EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "shipping_address"));
    928     EXPECT_TRUE(connection.DoesColumnExist("credit_cards",
    929                                            "verification_code_encrypted"));
    930 
    931     // Fetch data in the database prior to migration.
    932     sql::Statement s1(
    933         connection.GetUniqueStatement(
    934             "SELECT label, unique_id, first_name, middle_name, last_name, "
    935             "email, company_name, address_line_1, address_line_2, city, state, "
    936             "zipcode, country, phone, fax, date_modified, guid "
    937             "FROM autofill_profiles"));
    938     ASSERT_TRUE(s1.Step());
    939     EXPECT_NO_FATAL_FAILURE(AutofillProfile31FromStatement(
    940         s1, &profile, &profile_label, &profile_unique_id,
    941         &profile_date_modified));
    942 
    943     sql::Statement s2(
    944         connection.GetUniqueStatement(
    945             "SELECT label, unique_id, name_on_card, type, card_number, "
    946             "expiration_month, expiration_year, verification_code, "
    947             "billing_address, shipping_address, card_number_encrypted, "
    948             "verification_code_encrypted, date_modified, guid "
    949             "FROM credit_cards"));
    950     ASSERT_TRUE(s2.Step());
    951     EXPECT_NO_FATAL_FAILURE(CreditCard31FromStatement(s2,
    952                                                       &credit_card,
    953                                                       &cc_label,
    954                                                       &cc_unique_id,
    955                                                       &cc_number_encrypted,
    956                                                       &cc_date_modified));
    957 
    958     EXPECT_NE(profile_unique_id, cc_unique_id);
    959     EXPECT_NE(profile.guid(), credit_card.guid());
    960   }
    961 
    962   DoMigration();
    963 
    964   // Verify post-conditions.  These are expectations for current version of the
    965   // database.
    966   {
    967     sql::Connection connection;
    968     ASSERT_TRUE(connection.Open(GetDatabasePath()));
    969 
    970     // Check version.
    971     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
    972 
    973     // Verify existence of columns we'll be changing.
    974     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid"));
    975     EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "unique_id"));
    976     EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "guid"));
    977     EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "unique_id"));
    978     EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "type"));
    979     EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "card_number"));
    980     EXPECT_FALSE(connection.DoesColumnExist("credit_cards",
    981                                             "verification_code"));
    982     EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "billing_address"));
    983     EXPECT_FALSE(connection.DoesColumnExist("credit_cards",
    984                                             "shipping_address"));
    985     EXPECT_FALSE(connection.DoesColumnExist("credit_cards",
    986                                             "verification_code_encrypted"));
    987 
    988     // Verify data in the database after the migration.
    989     sql::Statement s1(
    990         connection.GetUniqueStatement(
    991             "SELECT guid, company_name, street_address, city, state, zipcode,"
    992             " country_code, date_modified "
    993             "FROM autofill_profiles"));
    994     ASSERT_TRUE(s1.Step());
    995 
    996     AutofillProfile profile_a;
    997     int64 profile_date_modified_a = 0;
    998     EXPECT_NO_FATAL_FAILURE(AutofillProfile33FromStatement(
    999         s1, &profile_a, &profile_date_modified_a));
   1000     EXPECT_EQ(profile.guid(), profile_a.guid());
   1001     EXPECT_EQ(profile.GetRawInfo(autofill::COMPANY_NAME),
   1002               profile_a.GetRawInfo(autofill::COMPANY_NAME));
   1003     EXPECT_EQ(profile.GetRawInfo(autofill::ADDRESS_HOME_LINE1),
   1004               profile_a.GetRawInfo(autofill::ADDRESS_HOME_LINE1));
   1005     EXPECT_EQ(profile.GetRawInfo(autofill::ADDRESS_HOME_LINE2),
   1006               profile_a.GetRawInfo(autofill::ADDRESS_HOME_LINE2));
   1007     EXPECT_EQ(profile.GetRawInfo(autofill::ADDRESS_HOME_CITY),
   1008               profile_a.GetRawInfo(autofill::ADDRESS_HOME_CITY));
   1009     EXPECT_EQ(profile.GetRawInfo(autofill::ADDRESS_HOME_STATE),
   1010               profile_a.GetRawInfo(autofill::ADDRESS_HOME_STATE));
   1011     EXPECT_EQ(profile.GetRawInfo(autofill::ADDRESS_HOME_ZIP),
   1012               profile_a.GetRawInfo(autofill::ADDRESS_HOME_ZIP));
   1013     EXPECT_EQ(profile.GetRawInfo(autofill::ADDRESS_HOME_COUNTRY),
   1014               profile_a.GetRawInfo(autofill::ADDRESS_HOME_COUNTRY));
   1015     EXPECT_EQ(profile_date_modified, profile_date_modified_a);
   1016 
   1017     sql::Statement s2(
   1018         connection.GetUniqueStatement(
   1019             "SELECT guid, name_on_card, expiration_month, "
   1020             "expiration_year, card_number_encrypted, date_modified "
   1021             "FROM credit_cards"));
   1022     ASSERT_TRUE(s2.Step());
   1023 
   1024     CreditCard credit_card_a;
   1025     base::string16 cc_label_a;
   1026     std::string cc_number_encrypted_a;
   1027     int64 cc_date_modified_a = 0;
   1028     EXPECT_NO_FATAL_FAILURE(CreditCard32FromStatement(s2,
   1029                                                       &credit_card_a,
   1030                                                       &cc_number_encrypted_a,
   1031                                                       &cc_date_modified_a));
   1032     EXPECT_EQ(credit_card, credit_card_a);
   1033     EXPECT_EQ(cc_label, cc_label_a);
   1034     EXPECT_EQ(cc_number_encrypted, cc_number_encrypted_a);
   1035     EXPECT_EQ(cc_date_modified, cc_date_modified_a);
   1036   }
   1037 }
   1038 
   1039 // Factor |autofill_profiles| address information separately from name, email,
   1040 // and phone.
   1041 TEST_F(WebDatabaseMigrationTest, MigrateVersion32ToCurrent) {
   1042   // Initialize the database.
   1043   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_32.sql")));
   1044 
   1045   // Verify pre-conditions. These are expectations for version 32 of the
   1046   // database.
   1047   {
   1048     sql::Connection connection;
   1049     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1050 
   1051     // Verify existence of columns we'll be changing.
   1052     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid"));
   1053     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "label"));
   1054     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "first_name"));
   1055     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "middle_name"));
   1056     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "last_name"));
   1057     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "email"));
   1058     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
   1059                                            "company_name"));
   1060     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
   1061                                            "address_line_1"));
   1062     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
   1063                                            "address_line_2"));
   1064     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "city"));
   1065     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "state"));
   1066     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "zipcode"));
   1067     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "country"));
   1068     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "phone"));
   1069     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "fax"));
   1070     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
   1071                                            "date_modified"));
   1072 
   1073     EXPECT_FALSE(connection.DoesTableExist("autofill_profile_names"));
   1074     EXPECT_FALSE(connection.DoesTableExist("autofill_profile_emails"));
   1075     EXPECT_FALSE(connection.DoesTableExist("autofill_profile_phones"));
   1076 
   1077     EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "label"));
   1078   }
   1079 
   1080   DoMigration();
   1081 
   1082   // Verify post-conditions.  These are expectations for current version of the
   1083   // database.
   1084   {
   1085     sql::Connection connection;
   1086     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1087 
   1088     // Check version.
   1089     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1090 
   1091     // Verify changes to columns.
   1092     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid"));
   1093     EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "label"));
   1094     EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "first_name"));
   1095     EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles",
   1096                                             "middle_name"));
   1097     EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "last_name"));
   1098     EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "email"));
   1099     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
   1100                                            "company_name"));
   1101     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
   1102                                            "street_address"));
   1103     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "city"));
   1104     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "state"));
   1105     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "zipcode"));
   1106     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
   1107                                            "country_code"));
   1108     EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "phone"));
   1109     EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "fax"));
   1110     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
   1111                                            "date_modified"));
   1112 
   1113     // New "names" table.
   1114     EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_names", "guid"));
   1115     EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_names",
   1116                                            "first_name"));
   1117     EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_names",
   1118                                            "middle_name"));
   1119     EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_names",
   1120                                            "last_name"));
   1121 
   1122     // New "emails" table.
   1123     EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_emails", "guid"));
   1124     EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_emails", "email"));
   1125 
   1126     // New "phones" table.
   1127     EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_phones", "guid"));
   1128     EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_phones",
   1129                                            "number"));
   1130 
   1131     EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "label"));
   1132 
   1133     // Verify data in the database after the migration.
   1134     sql::Statement s1(
   1135         connection.GetUniqueStatement(
   1136             "SELECT guid, company_name, street_address, city, state, zipcode, "
   1137             " country_code, date_modified "
   1138             "FROM autofill_profiles"));
   1139 
   1140     // John Doe.
   1141     ASSERT_TRUE(s1.Step());
   1142     EXPECT_EQ("00580526-FF81-EE2A-0546-1AC593A32E2F", s1.ColumnString(0));
   1143     EXPECT_EQ(ASCIIToUTF16("Doe Enterprises"), s1.ColumnString16(1));
   1144     EXPECT_EQ(ASCIIToUTF16("1 Main St\n"
   1145                            "Apt 1"),
   1146               s1.ColumnString16(2));
   1147     EXPECT_EQ(ASCIIToUTF16("Los Altos"), s1.ColumnString16(3));
   1148     EXPECT_EQ(ASCIIToUTF16("CA"), s1.ColumnString16(4));
   1149     EXPECT_EQ(ASCIIToUTF16("94022"), s1.ColumnString16(5));
   1150     EXPECT_EQ(ASCIIToUTF16("US"), s1.ColumnString16(6));
   1151     EXPECT_EQ(1297882100L, s1.ColumnInt64(7));
   1152 
   1153     // John P. Doe.
   1154     // Gets merged during migration from 35 to 37 due to multi-valued fields.
   1155 
   1156     // Dave Smith.
   1157     ASSERT_TRUE(s1.Step());
   1158     EXPECT_EQ("4C74A9D8-7EEE-423E-F9C2-E7FA70ED1396", s1.ColumnString(0));
   1159     EXPECT_EQ(base::string16(), s1.ColumnString16(1));
   1160     EXPECT_EQ(ASCIIToUTF16("2 Main Street"), s1.ColumnString16(2));
   1161     EXPECT_EQ(ASCIIToUTF16("Los Altos"), s1.ColumnString16(3));
   1162     EXPECT_EQ(ASCIIToUTF16("CA"), s1.ColumnString16(4));
   1163     EXPECT_EQ(ASCIIToUTF16("94022"), s1.ColumnString16(5));
   1164     EXPECT_EQ(ASCIIToUTF16("US"), s1.ColumnString16(6));
   1165     EXPECT_EQ(1297882100L, s1.ColumnInt64(7));
   1166 
   1167     // Dave Smith (Part 2).
   1168     ASSERT_TRUE(s1.Step());
   1169     EXPECT_EQ("722DF5C4-F74A-294A-46F0-31FFDED0D635", s1.ColumnString(0));
   1170     EXPECT_EQ(base::string16(), s1.ColumnString16(1));
   1171     EXPECT_EQ(ASCIIToUTF16("2 Main St"), s1.ColumnString16(2));
   1172     EXPECT_EQ(ASCIIToUTF16("Los Altos"), s1.ColumnString16(3));
   1173     EXPECT_EQ(ASCIIToUTF16("CA"), s1.ColumnString16(4));
   1174     EXPECT_EQ(ASCIIToUTF16("94022"), s1.ColumnString16(5));
   1175     EXPECT_EQ(ASCIIToUTF16("US"), s1.ColumnString16(6));
   1176     EXPECT_EQ(1297882100L, s1.ColumnInt64(7));
   1177 
   1178     // Alfred E Newman.
   1179     // Gets culled during migration from 35 to 36 due to incomplete address.
   1180 
   1181     // 3 Main St.
   1182     ASSERT_TRUE(s1.Step());
   1183     EXPECT_EQ("9E5FE298-62C7-83DF-6293-381BC589183F", s1.ColumnString(0));
   1184     EXPECT_EQ(base::string16(), s1.ColumnString16(1));
   1185     EXPECT_EQ(ASCIIToUTF16("3 Main St"), s1.ColumnString16(2));
   1186     EXPECT_EQ(ASCIIToUTF16("Los Altos"), s1.ColumnString16(3));
   1187     EXPECT_EQ(ASCIIToUTF16("CA"), s1.ColumnString16(4));
   1188     EXPECT_EQ(ASCIIToUTF16("94022"), s1.ColumnString16(5));
   1189     EXPECT_EQ(ASCIIToUTF16("US"), s1.ColumnString16(6));
   1190     EXPECT_EQ(1297882100L, s1.ColumnInt64(7));
   1191 
   1192     // That should be all.
   1193     EXPECT_FALSE(s1.Step());
   1194 
   1195     sql::Statement s2(
   1196         connection.GetUniqueStatement(
   1197             "SELECT guid, first_name, middle_name, last_name "
   1198             "FROM autofill_profile_names"));
   1199 
   1200     // John Doe.
   1201     ASSERT_TRUE(s2.Step());
   1202     EXPECT_EQ("00580526-FF81-EE2A-0546-1AC593A32E2F", s2.ColumnString(0));
   1203     EXPECT_EQ(ASCIIToUTF16("John"), s2.ColumnString16(1));
   1204     EXPECT_EQ(base::string16(), s2.ColumnString16(2));
   1205     EXPECT_EQ(ASCIIToUTF16("Doe"), s2.ColumnString16(3));
   1206 
   1207     // John P. Doe.  Note same guid as above due to merging of multi-valued
   1208     // fields.
   1209     ASSERT_TRUE(s2.Step());
   1210     EXPECT_EQ("00580526-FF81-EE2A-0546-1AC593A32E2F", s2.ColumnString(0));
   1211     EXPECT_EQ(ASCIIToUTF16("John"), s2.ColumnString16(1));
   1212     EXPECT_EQ(ASCIIToUTF16("P."), s2.ColumnString16(2));
   1213     EXPECT_EQ(ASCIIToUTF16("Doe"), s2.ColumnString16(3));
   1214 
   1215     // Dave Smith.
   1216     ASSERT_TRUE(s2.Step());
   1217     EXPECT_EQ("4C74A9D8-7EEE-423E-F9C2-E7FA70ED1396", s2.ColumnString(0));
   1218     EXPECT_EQ(ASCIIToUTF16("Dave"), s2.ColumnString16(1));
   1219     EXPECT_EQ(base::string16(), s2.ColumnString16(2));
   1220     EXPECT_EQ(ASCIIToUTF16("Smith"), s2.ColumnString16(3));
   1221 
   1222     // Dave Smith (Part 2).
   1223     ASSERT_TRUE(s2.Step());
   1224     EXPECT_EQ("722DF5C4-F74A-294A-46F0-31FFDED0D635", s2.ColumnString(0));
   1225     EXPECT_EQ(ASCIIToUTF16("Dave"), s2.ColumnString16(1));
   1226     EXPECT_EQ(base::string16(), s2.ColumnString16(2));
   1227     EXPECT_EQ(ASCIIToUTF16("Smith"), s2.ColumnString16(3));
   1228 
   1229     // Alfred E Newman.
   1230     // Gets culled during migration from 35 to 36 due to incomplete address.
   1231 
   1232     // 3 Main St.
   1233     ASSERT_TRUE(s2.Step());
   1234     EXPECT_EQ("9E5FE298-62C7-83DF-6293-381BC589183F", s2.ColumnString(0));
   1235     EXPECT_EQ(base::string16(), s2.ColumnString16(1));
   1236     EXPECT_EQ(base::string16(), s2.ColumnString16(2));
   1237     EXPECT_EQ(base::string16(), s2.ColumnString16(3));
   1238 
   1239     // Should be all.
   1240     EXPECT_FALSE(s2.Step());
   1241 
   1242     sql::Statement s3(
   1243         connection.GetUniqueStatement(
   1244             "SELECT guid, email "
   1245             "FROM autofill_profile_emails"));
   1246 
   1247     // John Doe.
   1248     ASSERT_TRUE(s3.Step());
   1249     EXPECT_EQ("00580526-FF81-EE2A-0546-1AC593A32E2F", s3.ColumnString(0));
   1250     EXPECT_EQ(ASCIIToUTF16("john (at) doe.com"), s3.ColumnString16(1));
   1251 
   1252     // John P. Doe.
   1253     // Gets culled during migration from 35 to 37 due to merging of John Doe and
   1254     // John P. Doe addresses.
   1255 
   1256     // 2 Main Street.
   1257     ASSERT_TRUE(s3.Step());
   1258     EXPECT_EQ("4C74A9D8-7EEE-423E-F9C2-E7FA70ED1396", s3.ColumnString(0));
   1259     EXPECT_EQ(base::string16(), s3.ColumnString16(1));
   1260 
   1261     // 2 Main St.
   1262     ASSERT_TRUE(s3.Step());
   1263     EXPECT_EQ("722DF5C4-F74A-294A-46F0-31FFDED0D635", s3.ColumnString(0));
   1264     EXPECT_EQ(base::string16(), s3.ColumnString16(1));
   1265 
   1266     // Alfred E Newman.
   1267     // Gets culled during migration from 35 to 36 due to incomplete address.
   1268 
   1269     // 3 Main St.
   1270     ASSERT_TRUE(s3.Step());
   1271     EXPECT_EQ("9E5FE298-62C7-83DF-6293-381BC589183F", s3.ColumnString(0));
   1272     EXPECT_EQ(base::string16(), s3.ColumnString16(1));
   1273 
   1274     // Should be all.
   1275     EXPECT_FALSE(s3.Step());
   1276 
   1277     sql::Statement s4(
   1278         connection.GetUniqueStatement(
   1279             "SELECT guid, number "
   1280             "FROM autofill_profile_phones"));
   1281 
   1282     // John Doe phone.
   1283     ASSERT_TRUE(s4.Step());
   1284     EXPECT_EQ("00580526-FF81-EE2A-0546-1AC593A32E2F", s4.ColumnString(0));
   1285     EXPECT_EQ(ASCIIToUTF16("4151112222"), s4.ColumnString16(1));
   1286 
   1287     // John Doe fax.
   1288     // Gets culled after fax type removed.
   1289 
   1290     // John P. Doe phone.
   1291     // Gets culled during migration from 35 to 37 due to merging of John Doe and
   1292     // John P. Doe addresses.
   1293 
   1294     // John P. Doe fax.
   1295     // Gets culled during migration from 35 to 37 due to merging of John Doe and
   1296     // John P. Doe addresses.
   1297 
   1298     // 2 Main Street phone.
   1299     ASSERT_TRUE(s4.Step());
   1300     EXPECT_EQ("4C74A9D8-7EEE-423E-F9C2-E7FA70ED1396", s4.ColumnString(0));
   1301     EXPECT_EQ(base::string16(), s4.ColumnString16(1));
   1302 
   1303     // 2 Main Street fax.
   1304     // Gets culled after fax type removed.
   1305 
   1306     // 2 Main St phone.
   1307     ASSERT_TRUE(s4.Step());
   1308     EXPECT_EQ("722DF5C4-F74A-294A-46F0-31FFDED0D635", s4.ColumnString(0));
   1309     EXPECT_EQ(0, s4.ColumnInt(1));  // 0 means phone.
   1310     EXPECT_EQ(base::string16(), s4.ColumnString16(2));
   1311 
   1312     // 2 Main St fax.
   1313     // Gets culled after fax type removed.
   1314 
   1315     // Note no phone or fax for Alfred E Newman.
   1316 
   1317     // 3 Main St phone.
   1318     ASSERT_TRUE(s4.Step());
   1319     EXPECT_EQ("9E5FE298-62C7-83DF-6293-381BC589183F", s4.ColumnString(0));
   1320     EXPECT_EQ(base::string16(), s4.ColumnString16(1));
   1321 
   1322     // 2 Main St fax.
   1323     // Gets culled after fax type removed.
   1324 
   1325     // Should be all.
   1326     EXPECT_FALSE(s4.Step());
   1327   }
   1328 }
   1329 
   1330 // Adds a column for the autofill profile's country code.
   1331 TEST_F(WebDatabaseMigrationTest, MigrateVersion33ToCurrent) {
   1332   // Initialize the database.
   1333   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_33.sql")));
   1334 
   1335   // Verify pre-conditions. These are expectations for version 33 of the
   1336   // database.
   1337   {
   1338     sql::Connection connection;
   1339     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1340 
   1341     EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles",
   1342                                             "country_code"));
   1343 
   1344     // Check that the country value is the one we expect.
   1345     sql::Statement s(
   1346         connection.GetUniqueStatement("SELECT country FROM autofill_profiles"));
   1347 
   1348     ASSERT_TRUE(s.Step());
   1349     std::string country = s.ColumnString(0);
   1350     EXPECT_EQ("United States", country);
   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     ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles",
   1365                                            "country_code"));
   1366 
   1367     // Check that the country code is properly converted.
   1368     sql::Statement s(connection.GetUniqueStatement(
   1369         "SELECT country_code FROM autofill_profiles"));
   1370 
   1371     ASSERT_TRUE(s.Step());
   1372     std::string country_code = s.ColumnString(0);
   1373     EXPECT_EQ("US", country_code);
   1374   }
   1375 }
   1376 
   1377 // Cleans up bad country code "UK" in favor of good country code "GB".
   1378 TEST_F(WebDatabaseMigrationTest, MigrateVersion34ToCurrent) {
   1379   // Initialize the database.
   1380   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_34.sql")));
   1381 
   1382   // Verify pre-conditions. These are expectations for version 34 of the
   1383   // database.
   1384   {
   1385     sql::Connection connection;
   1386     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1387 
   1388     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
   1389                                            "country_code"));
   1390 
   1391     // Check that the country_code value is the one we expect.
   1392     sql::Statement s(
   1393         connection.GetUniqueStatement("SELECT country_code "
   1394                                       "FROM autofill_profiles"));
   1395 
   1396     ASSERT_TRUE(s.Step());
   1397     std::string country_code = s.ColumnString(0);
   1398     EXPECT_EQ("UK", country_code);
   1399 
   1400     // Should have only one.
   1401     ASSERT_FALSE(s.Step());
   1402   }
   1403 
   1404   DoMigration();
   1405 
   1406   // Verify post-conditions.  These are expectations for current version of the
   1407   // database.
   1408   {
   1409     sql::Connection connection;
   1410     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1411 
   1412     // Check version.
   1413     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1414 
   1415     ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles",
   1416                                            "country_code"));
   1417 
   1418     // Check that the country_code code is properly converted.
   1419     sql::Statement s(connection.GetUniqueStatement(
   1420         "SELECT country_code FROM autofill_profiles"));
   1421 
   1422     ASSERT_TRUE(s.Step());
   1423     std::string country_code = s.ColumnString(0);
   1424     EXPECT_EQ("GB", country_code);
   1425 
   1426     // Should have only one.
   1427     ASSERT_FALSE(s.Step());
   1428   }
   1429 }
   1430 
   1431 // Cleans up invalid profiles based on more agressive merging.  Filters out
   1432 // profiles that are subsets of other profiles, and profiles with invalid email,
   1433 // state, and incomplete address.
   1434 TEST_F(WebDatabaseMigrationTest, MigrateVersion35ToCurrent) {
   1435   // Initialize the database.
   1436   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_35.sql")));
   1437 
   1438   // Verify pre-conditions. These are expectations for version 34 of the
   1439   // database.
   1440   {
   1441     sql::Connection connection;
   1442     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1443 
   1444     EXPECT_FALSE(connection.DoesTableExist("autofill_profiles_trash"));
   1445     ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid"));
   1446 
   1447     // Check that there are 6 profiles prior to merge.
   1448     sql::Statement s(
   1449         connection.GetUniqueStatement("SELECT guid FROM autofill_profiles"));
   1450     int i = 0;
   1451     while (s.Step())
   1452       ++i;
   1453     EXPECT_EQ(6, i);
   1454   }
   1455 
   1456   DoMigration();
   1457 
   1458   // Verify post-conditions.  These are expectations for current version of the
   1459   // database.
   1460   {
   1461     sql::Connection connection;
   1462     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1463 
   1464     // Check version.
   1465     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1466 
   1467     ASSERT_TRUE(connection.DoesTableExist("autofill_profiles_trash"));
   1468     ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles_trash", "guid"));
   1469     ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid"));
   1470 
   1471     // Verify data in the database after the migration.
   1472     sql::Statement s1(
   1473         connection.GetUniqueStatement(
   1474             "SELECT guid, company_name, street_address, city, state, zipcode,"
   1475             " country_code, date_modified "
   1476             "FROM autofill_profiles"));
   1477 
   1478     // John Doe.
   1479     ASSERT_TRUE(s1.Step());
   1480     EXPECT_EQ("00000000-0000-0000-0000-000000000001", s1.ColumnString(0));
   1481     EXPECT_EQ(ASCIIToUTF16("Acme Inc."), s1.ColumnString16(1));
   1482     EXPECT_EQ(ASCIIToUTF16("1 Main Street\n"
   1483                            "Apt 2"),
   1484               s1.ColumnString16(2));
   1485     EXPECT_EQ(ASCIIToUTF16("San Francisco"), s1.ColumnString16(3));
   1486     EXPECT_EQ(ASCIIToUTF16("CA"), s1.ColumnString16(4));
   1487     EXPECT_EQ(ASCIIToUTF16("94102"), s1.ColumnString16(5));
   1488     EXPECT_EQ(ASCIIToUTF16("US"), s1.ColumnString16(6));
   1489     EXPECT_EQ(1300131704, s1.ColumnInt64(7));
   1490 
   1491     // That should be it.
   1492     ASSERT_FALSE(s1.Step());
   1493 
   1494     // Check that there 5 trashed profile after the merge.
   1495     sql::Statement s2(
   1496         connection.GetUniqueStatement("SELECT guid "
   1497                                       "FROM autofill_profiles_trash"));
   1498     ASSERT_TRUE(s2.Step());
   1499     EXPECT_EQ("00000000-0000-0000-0000-000000000002", s2.ColumnString(0));
   1500 
   1501     ASSERT_TRUE(s2.Step());
   1502     EXPECT_EQ("00000000-0000-0000-0000-000000000003", s2.ColumnString(0));
   1503 
   1504     ASSERT_TRUE(s2.Step());
   1505     EXPECT_EQ("00000000-0000-0000-0000-000000000004", s2.ColumnString(0));
   1506 
   1507     ASSERT_TRUE(s2.Step());
   1508     EXPECT_EQ("00000000-0000-0000-0000-000000000005", s2.ColumnString(0));
   1509 
   1510     ASSERT_TRUE(s2.Step());
   1511     EXPECT_EQ("00000000-0000-0000-0000-000000000006", s2.ColumnString(0));
   1512 
   1513     // That should be it.
   1514     ASSERT_FALSE(s2.Step());
   1515   }
   1516 }
   1517 
   1518 // Tests that the |keywords| |last_modified| column gets added to the schema for
   1519 // a version 37 database.
   1520 TEST_F(WebDatabaseMigrationTest, MigrateVersion37ToCurrent) {
   1521   // This schema is taken from a build prior to the addition of the |keywords|
   1522   // |last_modified| column.
   1523   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_37.sql")));
   1524 
   1525   // Verify pre-conditions.  These are expectations for version 37 of the
   1526   // database.
   1527   {
   1528     sql::Connection connection;
   1529     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1530 
   1531     // Columns existing and not existing before current version.
   1532     ASSERT_TRUE(connection.DoesColumnExist("keywords", "id"));
   1533     ASSERT_FALSE(connection.DoesColumnExist("keywords", "last_modified"));
   1534   }
   1535 
   1536   DoMigration();
   1537 
   1538   // Verify post-conditions.  These are expectations for current version of the
   1539   // database.
   1540   {
   1541     sql::Connection connection;
   1542     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1543 
   1544     // Check version.
   1545     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1546 
   1547     // |keywords| |last_modified| column should have been added.
   1548     EXPECT_TRUE(connection.DoesColumnExist("keywords", "id"));
   1549     EXPECT_TRUE(connection.DoesColumnExist("keywords", "last_modified"));
   1550   }
   1551 }
   1552 
   1553 // Tests that the |keywords| |sync_guid| column gets added to the schema for
   1554 // a version 38 database.
   1555 TEST_F(WebDatabaseMigrationTest, MigrateVersion38ToCurrent) {
   1556   // This schema is taken from a build prior to the addition of the |keywords|
   1557   // |sync_guid| column.
   1558   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_38.sql")));
   1559 
   1560   // Verify pre-conditions.  These are expectations for version 38 of the
   1561   // database.
   1562   {
   1563     sql::Connection connection;
   1564     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1565 
   1566     // Columns existing and not existing before current version.
   1567     ASSERT_TRUE(connection.DoesColumnExist("keywords", "id"));
   1568     ASSERT_FALSE(connection.DoesColumnExist("keywords", "sync_guid"));
   1569   }
   1570 
   1571   DoMigration();
   1572 
   1573   // Verify post-conditions.  These are expectations for current version of the
   1574   // database.
   1575   {
   1576     sql::Connection connection;
   1577     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1578 
   1579     // Check version.
   1580     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1581 
   1582     // |keywords| |sync_guid| column should have been added.
   1583     EXPECT_TRUE(connection.DoesColumnExist("keywords", "id"));
   1584     EXPECT_TRUE(connection.DoesColumnExist("keywords", "sync_guid"));
   1585   }
   1586 }
   1587 
   1588 // Tests that no backup data is added to a version 39 database.
   1589 TEST_F(WebDatabaseMigrationTest, MigrateVersion39ToCurrent) {
   1590   // This schema is taken from a build prior to the addition of the default
   1591   // search provider backup field to the meta table.
   1592   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_39.sql")));
   1593 
   1594   // Verify pre-conditions.  These are expectations for version 39 of the
   1595   // database.
   1596   {
   1597     sql::Connection connection;
   1598     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1599     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1600 
   1601     sql::MetaTable meta_table;
   1602     ASSERT_TRUE(meta_table.Init(&connection, 39, 39));
   1603 
   1604     int64 default_search_provider_id = 0;
   1605     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   1606                                     &default_search_provider_id));
   1607 
   1608     EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
   1609   }
   1610 
   1611   DoMigration();
   1612 
   1613   // Verify post-conditions.  These are expectations for current version of the
   1614   // database.
   1615   {
   1616     sql::Connection connection;
   1617     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1618     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1619 
   1620     // Check version.
   1621     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1622 
   1623     sql::MetaTable meta_table;
   1624     ASSERT_TRUE(meta_table.Init(&connection, kCurrentTestedVersionNumber,
   1625                                 kCurrentTestedVersionNumber));
   1626 
   1627     int64 default_search_provider_id = 0;
   1628     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   1629                                     &default_search_provider_id));
   1630     EXPECT_NE(0, default_search_provider_id);
   1631 
   1632     EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
   1633   }
   1634 }
   1635 
   1636 // Tests that the backup data is removed from the database.
   1637 TEST_F(WebDatabaseMigrationTest, MigrateVersion40ToCurrent) {
   1638   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_40.sql")));
   1639 
   1640   // Verify pre-conditions.  These are expectations for version 40 of the
   1641   // database.
   1642   {
   1643     sql::Connection connection;
   1644     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1645     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1646 
   1647     sql::MetaTable meta_table;
   1648     ASSERT_TRUE(meta_table.Init(&connection, 40, 40));
   1649 
   1650     int64 default_search_provider_id = 0;
   1651     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   1652                                     &default_search_provider_id));
   1653 
   1654     EXPECT_NO_FATAL_FAILURE(CheckHasBackupData(&meta_table));
   1655   }
   1656 
   1657   DoMigration();
   1658 
   1659   // Verify post-conditions.  These are expectations for current version of the
   1660   // database.
   1661   {
   1662     sql::Connection connection;
   1663     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1664     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1665 
   1666     // Check version.
   1667     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1668 
   1669     sql::MetaTable meta_table;
   1670     ASSERT_TRUE(meta_table.Init(&connection, kCurrentTestedVersionNumber,
   1671                                 kCurrentTestedVersionNumber));
   1672 
   1673     int64 default_search_provider_id = 0;
   1674     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   1675                                     &default_search_provider_id));
   1676     EXPECT_NE(0, default_search_provider_id);
   1677 
   1678     EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
   1679   }
   1680 }
   1681 
   1682 // Tests that the backup data is removed from the database.
   1683 TEST_F(WebDatabaseMigrationTest, MigrateVersion41ToCurrent) {
   1684   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_41.sql")));
   1685 
   1686   // Verify pre-conditions.  These are expectations for version 41 of the
   1687   // database.
   1688   {
   1689     sql::Connection connection;
   1690     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1691     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1692 
   1693     sql::MetaTable meta_table;
   1694     ASSERT_TRUE(meta_table.Init(&connection, 41, 41));
   1695 
   1696     int64 default_search_provider_id = 0;
   1697     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   1698                                     &default_search_provider_id));
   1699 
   1700     EXPECT_NO_FATAL_FAILURE(CheckHasBackupData(&meta_table));
   1701   }
   1702 
   1703   DoMigration();
   1704 
   1705   // Verify post-conditions.  These are expectations for current version of the
   1706   // database.
   1707   {
   1708     sql::Connection connection;
   1709     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1710     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1711 
   1712     // Check version.
   1713     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1714 
   1715     sql::MetaTable meta_table;
   1716     ASSERT_TRUE(meta_table.Init(&connection, kCurrentTestedVersionNumber,
   1717                                 kCurrentTestedVersionNumber));
   1718 
   1719     int64 default_search_provider_id = 0;
   1720     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   1721                                     &default_search_provider_id));
   1722     EXPECT_NE(0, default_search_provider_id);
   1723 
   1724     EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
   1725   }
   1726 }
   1727 
   1728 // Tests that the backup data is removed from the database.
   1729 TEST_F(WebDatabaseMigrationTest, MigrateVersion42ToCurrent) {
   1730   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_42.sql")));
   1731 
   1732   // Verify pre-conditions.  These are expectations for version 42 of the
   1733   // database.
   1734   {
   1735     sql::Connection connection;
   1736     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1737     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1738 
   1739     sql::MetaTable meta_table;
   1740     ASSERT_TRUE(meta_table.Init(&connection, 42, 42));
   1741 
   1742     int64 default_search_provider_id = 0;
   1743     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   1744                                     &default_search_provider_id));
   1745 
   1746     EXPECT_NO_FATAL_FAILURE(CheckHasBackupData(&meta_table));
   1747 
   1748     EXPECT_FALSE(connection.DoesTableExist("keywords_backup"));
   1749   }
   1750 
   1751   DoMigration();
   1752 
   1753   // Verify post-conditions.  These are expectations for current version of the
   1754   // database.
   1755   {
   1756     sql::Connection connection;
   1757     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1758     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1759 
   1760     // Check version.
   1761     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1762 
   1763     sql::MetaTable meta_table;
   1764     ASSERT_TRUE(meta_table.Init(&connection, kCurrentTestedVersionNumber,
   1765                                 kCurrentTestedVersionNumber));
   1766 
   1767     int64 default_search_provider_id = 0;
   1768     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   1769                                     &default_search_provider_id));
   1770     EXPECT_NE(0, default_search_provider_id);
   1771 
   1772     EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
   1773   }
   1774 }
   1775 
   1776 // Tests that the backup data is removed from the database.
   1777 TEST_F(WebDatabaseMigrationTest, MigrateVersion43ToCurrent) {
   1778   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_43.sql")));
   1779 
   1780   int64 previous_default_search_provider_id;
   1781 
   1782   // Verify pre-conditions.  These are expectations for version 43 of the
   1783   // database.
   1784   {
   1785     sql::Connection connection;
   1786     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1787     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1788 
   1789     sql::MetaTable meta_table;
   1790     ASSERT_TRUE(meta_table.Init(&connection, 43, 43));
   1791 
   1792     int64 default_search_provider_id = 0;
   1793     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   1794                                     &default_search_provider_id));
   1795     EXPECT_NE(default_search_provider_id, 0);
   1796     previous_default_search_provider_id = default_search_provider_id;
   1797 
   1798     EXPECT_NO_FATAL_FAILURE(CheckHasBackupData(&meta_table));
   1799     EXPECT_TRUE(connection.DoesTableExist("keywords_backup"));
   1800   }
   1801 
   1802   DoMigration();
   1803 
   1804   // Verify post-conditions.  These are expectations for current version of the
   1805   // database.
   1806   {
   1807     sql::Connection connection;
   1808     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1809     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1810 
   1811     // Check version.
   1812     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1813 
   1814     sql::MetaTable meta_table;
   1815     ASSERT_TRUE(meta_table.Init(
   1816         &connection,
   1817         kCurrentTestedVersionNumber,
   1818         kCurrentTestedVersionNumber));
   1819 
   1820     int64 default_search_provider_id = 0;
   1821     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   1822                                     &default_search_provider_id));
   1823     // Default search provider ID should not change.
   1824     EXPECT_EQ(previous_default_search_provider_id, default_search_provider_id);
   1825 
   1826     EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
   1827   }
   1828 }
   1829 
   1830 // Tests that the |autogenerate_keyword| and |logo_id| columns get removed from
   1831 // the keyword table schema for a version 45 database.
   1832 TEST_F(WebDatabaseMigrationTest, MigrateVersion44ToCurrent) {
   1833   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_44.sql")));
   1834 
   1835   // Verify pre-conditions.  These are expectations for version 44 of the
   1836   // database.
   1837   {
   1838     sql::Connection connection;
   1839     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1840     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1841 
   1842     sql::MetaTable meta_table;
   1843     ASSERT_TRUE(meta_table.Init(&connection, 44, 44));
   1844 
   1845     ASSERT_TRUE(connection.DoesColumnExist("keywords", "autogenerate_keyword"));
   1846     ASSERT_TRUE(connection.DoesColumnExist("keywords", "logo_id"));
   1847   }
   1848 
   1849   DoMigration();
   1850 
   1851   // Verify post-conditions.  These are expectations for current version of the
   1852   // database.
   1853   {
   1854     sql::Connection connection;
   1855     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1856     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1857 
   1858     // Check version.
   1859     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1860 
   1861     sql::MetaTable meta_table;
   1862     ASSERT_TRUE(meta_table.Init(&connection, kCurrentTestedVersionNumber,
   1863                                 kCurrentTestedVersionNumber));
   1864 
   1865     // We should have removed this obsolete key.
   1866     std::string default_search_provider_backup;
   1867     EXPECT_FALSE(meta_table.GetValue("Default Search Provider Backup",
   1868                                      &default_search_provider_backup));
   1869 
   1870     // Two columns should have been removed.
   1871     EXPECT_FALSE(connection.DoesColumnExist("keywords",
   1872                                             "autogenerate_keyword"));
   1873     EXPECT_FALSE(connection.DoesColumnExist("keywords", "logo_id"));
   1874 
   1875     // Backup data should have been removed.
   1876     EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
   1877   }
   1878 }
   1879 
   1880 // Tests that the web_intents and web_intents_defaults tables are
   1881 // modified to include "scheme" columns.
   1882 TEST_F(WebDatabaseMigrationTest, MigrateVersion45ToCurrent) {
   1883   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_45.sql")));
   1884 
   1885   // Verify pre-conditions.  These are expectations for version 45 of the
   1886   // database.
   1887   {
   1888     sql::Connection connection;
   1889     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1890     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1891 
   1892     sql::MetaTable meta_table;
   1893     ASSERT_TRUE(meta_table.Init(&connection, 45, 45));
   1894 
   1895     ASSERT_FALSE(connection.DoesColumnExist("scheme", "web_intents"));
   1896     ASSERT_FALSE(connection.DoesColumnExist(
   1897         "scheme", "web_intents_defaults"));
   1898   }
   1899 
   1900   DoMigration();
   1901 
   1902   // Verify post-conditions.  These are expectations for current version of the
   1903   // database.
   1904   {
   1905     sql::Connection connection;
   1906     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1907     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1908 
   1909     // Check version.
   1910     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1911 
   1912     sql::MetaTable meta_table;
   1913     ASSERT_TRUE(meta_table.Init(
   1914         &connection,
   1915         kCurrentTestedVersionNumber,
   1916         kCurrentTestedVersionNumber));
   1917 
   1918     // A new "scheme" column should have been added to each web_intents table.
   1919     EXPECT_TRUE(connection.DoesColumnExist("web_intents", "scheme"));
   1920     EXPECT_TRUE(connection.DoesColumnExist("web_intents_defaults", "scheme"));
   1921 
   1922     // Verify existing user data was copied.
   1923     sql::Statement s1(
   1924         connection.GetUniqueStatement("SELECT * FROM web_intents"));
   1925 
   1926     ASSERT_TRUE(s1.Step());
   1927     EXPECT_EQ("http://poodles.com/fuzzer", s1.ColumnString(0));
   1928     EXPECT_EQ(ASCIIToUTF16("fuzz"), s1.ColumnString16(1));
   1929     EXPECT_EQ(ASCIIToUTF16("poodle/*"), s1.ColumnString16(2));
   1930     EXPECT_EQ(ASCIIToUTF16("Poodle Fuzzer"), s1.ColumnString16(3));
   1931     EXPECT_EQ(ASCIIToUTF16("window"), s1.ColumnString16(4));
   1932     EXPECT_EQ(ASCIIToUTF16(""), s1.ColumnString16(5));
   1933     ASSERT_FALSE(s1.Step());
   1934 
   1935     // Now we want to verify existing user data was copied
   1936     sql::Statement s2(
   1937         connection.GetUniqueStatement("SELECT * FROM web_intents_defaults"));
   1938 
   1939     ASSERT_TRUE(s2.Step());
   1940     EXPECT_EQ("fuzz", s2.ColumnString(0));
   1941     EXPECT_EQ(ASCIIToUTF16("poodle/*"), s2.ColumnString16(1));
   1942     EXPECT_EQ(ASCIIToUTF16(""), s2.ColumnString16(2));
   1943     EXPECT_EQ(0, s2.ColumnInt(3));
   1944     EXPECT_EQ(0, s2.ColumnInt(4));
   1945     EXPECT_EQ(ASCIIToUTF16("http://poodles.com/fuzzer"), s2.ColumnString16(5));
   1946     EXPECT_EQ(ASCIIToUTF16(""), s2.ColumnString16(6));
   1947     ASSERT_FALSE(s2.Step());
   1948 
   1949     // finally ensure the migration code cleaned up after itself
   1950     EXPECT_FALSE(connection.DoesTableExist("old_web_intents"));
   1951     EXPECT_FALSE(connection.DoesTableExist("old_web_intents_defaults"));
   1952   }
   1953 }
   1954 
   1955 // Tests that the web_intents and web_intents_defaults tables are
   1956 // modified to include "scheme" columns.
   1957 TEST_F(WebDatabaseMigrationTest, MigrateVersion45InvalidToCurrent) {
   1958   ASSERT_NO_FATAL_FAILURE(
   1959       LoadDatabase(FILE_PATH_LITERAL("version_45_invalid.sql")));
   1960 
   1961   // Verify pre-conditions.  These are expectations for version 45 of the
   1962   // database.
   1963   {
   1964     sql::Connection connection;
   1965     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1966     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1967 
   1968     sql::MetaTable meta_table;
   1969     ASSERT_TRUE(meta_table.Init(&connection, 45, 45));
   1970 
   1971     ASSERT_FALSE(connection.DoesColumnExist("scheme", "web_intents"));
   1972     ASSERT_FALSE(connection.DoesColumnExist(
   1973         "scheme", "web_intents_defaults"));
   1974   }
   1975 
   1976   DoMigration();
   1977 
   1978   // Verify post-conditions.  These are expectations for current version of the
   1979   // database.
   1980   {
   1981     sql::Connection connection;
   1982     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   1983     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   1984 
   1985     // Check version.
   1986     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   1987 
   1988     sql::MetaTable meta_table;
   1989     ASSERT_TRUE(meta_table.Init(
   1990         &connection,
   1991         kCurrentTestedVersionNumber,
   1992         kCurrentTestedVersionNumber));
   1993 
   1994     // A new "scheme" column should have been added to each web_intents table.
   1995     EXPECT_TRUE(connection.DoesColumnExist("web_intents", "scheme"));
   1996     EXPECT_TRUE(connection.DoesColumnExist("web_intents_defaults", "scheme"));
   1997 
   1998     // Verify existing user data was copied.
   1999     sql::Statement s1(
   2000         connection.GetUniqueStatement("SELECT * FROM web_intents"));
   2001 
   2002     ASSERT_FALSE(s1.Step());  // Basically should be empty at this point.
   2003 
   2004     // Now we want to verify existing user data was copied
   2005     sql::Statement s2(
   2006         connection.GetUniqueStatement("SELECT * FROM web_intents_defaults"));
   2007 
   2008     // We were able to create the new tables, but unable to copy any data
   2009     // Given the initial bad state of the tables.
   2010     ASSERT_FALSE(s2.Step());
   2011 
   2012     // Finally ensure the migration code cleaned up after itself.
   2013     EXPECT_FALSE(connection.DoesTableExist("old_web_intents"));
   2014     EXPECT_FALSE(connection.DoesTableExist("old_web_intents_defaults"));
   2015   }
   2016 }
   2017 
   2018 // Check that current version is forced to compatible version before migration,
   2019 // if the former is smaller.
   2020 TEST_F(WebDatabaseMigrationTest, MigrateVersion45CompatibleToCurrent) {
   2021   ASSERT_NO_FATAL_FAILURE(
   2022       LoadDatabase(FILE_PATH_LITERAL("version_45_compatible.sql")));
   2023 
   2024   // Verify pre-conditions.  These are expectations for version 45 of the
   2025   // database.
   2026   {
   2027     sql::Connection connection;
   2028     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   2029     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   2030 
   2031     sql::MetaTable meta_table;
   2032     // Database is actually version 45 but the version field states 40.
   2033     ASSERT_TRUE(meta_table.Init(&connection, 40, 45));
   2034   }
   2035 
   2036   DoMigration();
   2037 
   2038   // Verify post-conditions.  These are expectations for current version of the
   2039   // database.
   2040   {
   2041     sql::Connection connection;
   2042     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   2043     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   2044 
   2045     // Check version.
   2046     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   2047     EXPECT_LE(45, VersionFromConnection(&connection));
   2048   }
   2049 }
   2050 
   2051 // Tests that the |alternate_urls| column is added to the keyword table schema
   2052 // for a version 47 database.
   2053 TEST_F(WebDatabaseMigrationTest, MigrateVersion46ToCurrent) {
   2054   ASSERT_NO_FATAL_FAILURE(
   2055       LoadDatabase(FILE_PATH_LITERAL("version_46.sql")));
   2056 
   2057   // Verify pre-conditions.  These are expectations for version 46 of the
   2058   // database.
   2059   {
   2060     sql::Connection connection;
   2061     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   2062     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   2063 
   2064     sql::MetaTable meta_table;
   2065     ASSERT_TRUE(meta_table.Init(&connection, 46, 46));
   2066 
   2067     ASSERT_FALSE(connection.DoesColumnExist("keywords", "alternate_urls"));
   2068     ASSERT_FALSE(connection.DoesColumnExist("keywords_backup",
   2069                                             "alternate_urls"));
   2070   }
   2071 
   2072   DoMigration();
   2073 
   2074   // Verify post-conditions.  These are expectations for current version of the
   2075   // database.
   2076   {
   2077     sql::Connection connection;
   2078     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   2079     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   2080 
   2081     // Check version.
   2082     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   2083 
   2084     // A new column should have been created.
   2085     EXPECT_TRUE(connection.DoesColumnExist("keywords", "alternate_urls"));
   2086   }
   2087 }
   2088 
   2089 // Tests that the backup data is removed from the database.
   2090 TEST_F(WebDatabaseMigrationTest, MigrateVersion47ToCurrent) {
   2091   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_47.sql")));
   2092 
   2093   // Verify pre-conditions.  These are expectations for version 47 of the
   2094   // database.
   2095   {
   2096     sql::Connection connection;
   2097     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   2098     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   2099 
   2100     sql::MetaTable meta_table;
   2101     ASSERT_TRUE(meta_table.Init(&connection, 47, 47));
   2102 
   2103     int64 default_search_provider_id = 0;
   2104     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   2105                                     &default_search_provider_id));
   2106     EXPECT_NE(0, default_search_provider_id);
   2107 
   2108     EXPECT_NO_FATAL_FAILURE(CheckHasBackupData(&meta_table));
   2109     EXPECT_TRUE(connection.DoesTableExist("keywords_backup"));
   2110   }
   2111 
   2112   DoMigration();
   2113 
   2114   // Verify post-conditions.  These are expectations for current version of the
   2115   // database.
   2116   {
   2117     sql::Connection connection;
   2118     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   2119     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   2120 
   2121     // Check version.
   2122     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   2123 
   2124     sql::MetaTable meta_table;
   2125     ASSERT_TRUE(meta_table.Init(&connection, kCurrentTestedVersionNumber,
   2126                                 kCurrentTestedVersionNumber));
   2127 
   2128     int64 default_search_provider_id = 0;
   2129     EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
   2130                                     &default_search_provider_id));
   2131     EXPECT_NE(0, default_search_provider_id);
   2132 
   2133     EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
   2134   }
   2135 }
   2136 
   2137 // Tests that the |search_terms_replacement_key| column is added to the keyword
   2138 // table schema for a version 49 database.
   2139 TEST_F(WebDatabaseMigrationTest, MigrateVersion48ToCurrent) {
   2140   ASSERT_NO_FATAL_FAILURE(
   2141       LoadDatabase(FILE_PATH_LITERAL("version_48.sql")));
   2142 
   2143   // Verify pre-conditions.  These are expectations for version 48 of the
   2144   // database.
   2145   {
   2146     sql::Connection connection;
   2147     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   2148     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   2149 
   2150     sql::MetaTable meta_table;
   2151     ASSERT_TRUE(meta_table.Init(&connection, 48, 48));
   2152 
   2153     ASSERT_FALSE(connection.DoesColumnExist("keywords",
   2154                                             "search_terms_replacement_key"));
   2155   }
   2156 
   2157   DoMigration();
   2158 
   2159   // Verify post-conditions.  These are expectations for current version of the
   2160   // database.
   2161   {
   2162     sql::Connection connection;
   2163     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   2164     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   2165 
   2166     // Check version.
   2167     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   2168 
   2169     // A new column should have been created.
   2170     EXPECT_TRUE(connection.DoesColumnExist("keywords",
   2171                                            "search_terms_replacement_key"));
   2172   }
   2173 }
   2174 
   2175 // Tests that the |origin| column is added to the autofill_profiles and
   2176 // credit_cards table schemas for a version 50 database.
   2177 TEST_F(WebDatabaseMigrationTest, MigrateVersion49ToCurrent) {
   2178   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_49.sql")));
   2179 
   2180   // Verify pre-conditions.  These are expectations for version 49 of the
   2181   // database.
   2182   {
   2183     sql::Connection connection;
   2184     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   2185 
   2186     ASSERT_FALSE(connection.DoesColumnExist("autofill_profiles", "origin"));
   2187     ASSERT_FALSE(connection.DoesColumnExist("credit_cards", "origin"));
   2188   }
   2189 
   2190   DoMigration();
   2191 
   2192   // Verify post-conditions.  These are expectations for current version of the
   2193   // database.
   2194   {
   2195     sql::Connection connection;
   2196     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   2197     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   2198 
   2199     // Check version.
   2200     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   2201 
   2202     // A new column should have been created in both tables.
   2203     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "origin"));
   2204     EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "origin"));
   2205   }
   2206 }
   2207 
   2208 // Tests that the columns |image_url|, |search_url_post_params|,
   2209 // |suggest_url_post_params|, |instant_url_post_params|, and
   2210 // |image_url_post_params| are added to the keyword table schema for a version
   2211 // 50 database.
   2212 TEST_F(WebDatabaseMigrationTest, MigrateVersion50ToCurrent) {
   2213   ASSERT_NO_FATAL_FAILURE(
   2214       LoadDatabase(FILE_PATH_LITERAL("version_50.sql")));
   2215 
   2216   // Verify pre-conditions.  These are expectations for version 50 of the
   2217   // database.
   2218   {
   2219     sql::Connection connection;
   2220     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   2221     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   2222 
   2223     sql::MetaTable meta_table;
   2224     ASSERT_TRUE(meta_table.Init(&connection, 50, 50));
   2225 
   2226     ASSERT_FALSE(connection.DoesColumnExist("keywords", "image_url"));
   2227     ASSERT_FALSE(connection.DoesColumnExist("keywords",
   2228                                             "search_url_post_params"));
   2229     ASSERT_FALSE(connection.DoesColumnExist("keywords",
   2230                                             "suggest_url_post_params"));
   2231     ASSERT_FALSE(connection.DoesColumnExist("keywords",
   2232                                             "instant_url_post_params"));
   2233     ASSERT_FALSE(connection.DoesColumnExist("keywords",
   2234                                             "image_url_post_params"));
   2235   }
   2236 
   2237   DoMigration();
   2238 
   2239   // Verify post-conditions.  These are expectations for current version of the
   2240   // database.
   2241   {
   2242     sql::Connection connection;
   2243     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   2244     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   2245 
   2246     // Check version.
   2247     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   2248 
   2249     // New columns should have been created.
   2250     EXPECT_TRUE(connection.DoesColumnExist("keywords", "image_url"));
   2251     EXPECT_TRUE(connection.DoesColumnExist("keywords",
   2252                                            "search_url_post_params"));
   2253     EXPECT_TRUE(connection.DoesColumnExist("keywords",
   2254                                            "suggest_url_post_params"));
   2255     EXPECT_TRUE(connection.DoesColumnExist("keywords",
   2256                                            "instant_url_post_params"));
   2257     EXPECT_TRUE(connection.DoesColumnExist("keywords",
   2258                                            "image_url_post_params"));
   2259   }
   2260 }
   2261 
   2262 // Tests that the column |new_tab_url| is added to the keyword table schema for
   2263 // a version 52 database.
   2264 TEST_F(WebDatabaseMigrationTest, MigrateVersion52ToCurrent) {
   2265   ASSERT_NO_FATAL_FAILURE(
   2266       LoadDatabase(FILE_PATH_LITERAL("version_52.sql")));
   2267 
   2268   // Verify pre-conditions.  These are expectations for version 52 of the
   2269   // database.
   2270   {
   2271     sql::Connection connection;
   2272     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   2273     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   2274 
   2275     sql::MetaTable meta_table;
   2276     ASSERT_TRUE(meta_table.Init(&connection, 52, 52));
   2277 
   2278     ASSERT_FALSE(connection.DoesColumnExist("keywords", "new_tab_url"));
   2279   }
   2280 
   2281   DoMigration();
   2282 
   2283   // Verify post-conditions.  These are expectations for current version of the
   2284   // database.
   2285   {
   2286     sql::Connection connection;
   2287     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   2288     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   2289 
   2290     // Check version.
   2291     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   2292 
   2293     // New columns should have been created.
   2294     EXPECT_TRUE(connection.DoesColumnExist("keywords", "new_tab_url"));
   2295   }
   2296 }
   2297 
   2298 // Tests that for a version 54 database,
   2299 //   (a) The street_address, dependent_locality, and sorting_code columns are
   2300 //       added to the autofill_profiles table schema.
   2301 //   (b) The address_line1, address_line2, and country columns are dropped from
   2302 //       the autofill_profiles table schema.
   2303 //   (c) The type column is dropped from the autofill_profile_phones schema.
   2304 TEST_F(WebDatabaseMigrationTest, MigrateVersion53ToCurrent) {
   2305   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_53.sql")));
   2306 
   2307   // Verify pre-conditions.  These are expectations for version 53 of the
   2308   // database.
   2309   {
   2310     sql::Connection connection;
   2311     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   2312 
   2313     EXPECT_TRUE(
   2314         connection.DoesColumnExist("autofill_profiles", "address_line_1"));
   2315     EXPECT_TRUE(
   2316         connection.DoesColumnExist("autofill_profiles", "address_line_2"));
   2317     EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "country"));
   2318     EXPECT_FALSE(
   2319         connection.DoesColumnExist("autofill_profiles", "street_address"));
   2320     EXPECT_FALSE(
   2321         connection.DoesColumnExist("autofill_profiles", "dependent_locality"));
   2322     EXPECT_FALSE(
   2323         connection.DoesColumnExist("autofill_profiles", "sorting_code"));
   2324     EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_phones", "type"));
   2325   }
   2326 
   2327   DoMigration();
   2328 
   2329   // Verify post-conditions.  These are expectations for current version of the
   2330   // database.
   2331   {
   2332     sql::Connection connection;
   2333     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   2334     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   2335 
   2336     // Check version.
   2337     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   2338 
   2339     // Columns should have been added and removed appropriately.
   2340     EXPECT_FALSE(
   2341         connection.DoesColumnExist("autofill_profiles", "address_line1"));
   2342     EXPECT_FALSE(
   2343         connection.DoesColumnExist("autofill_profiles", "address_line2"));
   2344     EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "country"));
   2345     EXPECT_TRUE(
   2346         connection.DoesColumnExist("autofill_profiles", "street_address"));
   2347     EXPECT_TRUE(
   2348         connection.DoesColumnExist("autofill_profiles", "dependent_locality"));
   2349     EXPECT_TRUE(
   2350         connection.DoesColumnExist("autofill_profiles", "sorting_code"));
   2351     EXPECT_FALSE(connection.DoesColumnExist("autofill_profile_phones", "type"));
   2352 
   2353     // Data should have been preserved.
   2354     sql::Statement s_profiles(
   2355         connection.GetUniqueStatement(
   2356             "SELECT guid, company_name, street_address, dependent_locality,"
   2357             " city, state, zipcode, sorting_code, country_code, date_modified,"
   2358             " origin "
   2359             "FROM autofill_profiles"));
   2360 
   2361     // Address lines 1 and 2.
   2362     ASSERT_TRUE(s_profiles.Step());
   2363     EXPECT_EQ("00000000-0000-0000-0000-000000000001",
   2364               s_profiles.ColumnString(0));
   2365     EXPECT_EQ(ASCIIToUTF16("Google, Inc."), s_profiles.ColumnString16(1));
   2366     EXPECT_EQ(ASCIIToUTF16("1950 Charleston Rd.\n"
   2367                            "(2nd floor)"),
   2368               s_profiles.ColumnString16(2));
   2369     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(3));
   2370     EXPECT_EQ(ASCIIToUTF16("Mountain View"), s_profiles.ColumnString16(4));
   2371     EXPECT_EQ(ASCIIToUTF16("CA"), s_profiles.ColumnString16(5));
   2372     EXPECT_EQ(ASCIIToUTF16("94043"), s_profiles.ColumnString16(6));
   2373     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(7));
   2374     EXPECT_EQ(ASCIIToUTF16("US"), s_profiles.ColumnString16(8));
   2375     EXPECT_EQ(1386046731, s_profiles.ColumnInt(9));
   2376     EXPECT_EQ(ASCIIToUTF16("Chrome settings"), s_profiles.ColumnString16(10));
   2377 
   2378     // Only address line 1.
   2379     ASSERT_TRUE(s_profiles.Step());
   2380     EXPECT_EQ("00000000-0000-0000-0000-000000000002",
   2381               s_profiles.ColumnString(0));
   2382     EXPECT_EQ(ASCIIToUTF16("Google!"), s_profiles.ColumnString16(1));
   2383     EXPECT_EQ(ASCIIToUTF16("1600 Amphitheatre Pkwy."),
   2384               s_profiles.ColumnString16(2));
   2385     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(3));
   2386     EXPECT_EQ(ASCIIToUTF16("Mtn. View"), s_profiles.ColumnString16(4));
   2387     EXPECT_EQ(ASCIIToUTF16("California"), s_profiles.ColumnString16(5));
   2388     EXPECT_EQ(ASCIIToUTF16("94043-1234"), s_profiles.ColumnString16(6));
   2389     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(7));
   2390     EXPECT_EQ(ASCIIToUTF16("US"), s_profiles.ColumnString16(8));
   2391     EXPECT_EQ(1386046800, s_profiles.ColumnInt(9));
   2392     EXPECT_EQ(ASCIIToUTF16("Chrome settings"), s_profiles.ColumnString16(10));
   2393 
   2394     // Only address line 2.
   2395     ASSERT_TRUE(s_profiles.Step());
   2396     EXPECT_EQ("00000000-0000-0000-0000-000000000003",
   2397               s_profiles.ColumnString(0));
   2398     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(1));
   2399     EXPECT_EQ(ASCIIToUTF16("\nOnly line 2???"), s_profiles.ColumnString16(2));
   2400     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(3));
   2401     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(4));
   2402     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(5));
   2403     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(6));
   2404     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(7));
   2405     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(8));
   2406     EXPECT_EQ(1386046834, s_profiles.ColumnInt(9));
   2407     EXPECT_EQ(ASCIIToUTF16("Chrome settings"), s_profiles.ColumnString16(10));
   2408 
   2409     // No address lines.
   2410     ASSERT_TRUE(s_profiles.Step());
   2411     EXPECT_EQ("00000000-0000-0000-0000-000000000004",
   2412               s_profiles.ColumnString(0));
   2413     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(1));
   2414     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(2));
   2415     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(3));
   2416     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(4));
   2417     EXPECT_EQ(ASCIIToUTF16("Texas"), s_profiles.ColumnString16(5));
   2418     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(6));
   2419     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(7));
   2420     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(8));
   2421     EXPECT_EQ(1386046847, s_profiles.ColumnInt(9));
   2422     EXPECT_EQ(ASCIIToUTF16("Chrome settings"), s_profiles.ColumnString16(10));
   2423 
   2424     // That should be it.
   2425     EXPECT_FALSE(s_profiles.Step());
   2426 
   2427     // Verify the phone number data as well.
   2428     sql::Statement s_phones(
   2429         connection.GetUniqueStatement(
   2430             "SELECT guid, number FROM autofill_profile_phones"));
   2431 
   2432     ASSERT_TRUE(s_phones.Step());
   2433     EXPECT_EQ("00000000-0000-0000-0000-000000000001", s_phones.ColumnString(0));
   2434     EXPECT_EQ(ASCIIToUTF16("1.800.555.1234"), s_phones.ColumnString16(1));
   2435 
   2436     ASSERT_TRUE(s_phones.Step());
   2437     EXPECT_EQ("00000000-0000-0000-0000-000000000001", s_phones.ColumnString(0));
   2438     EXPECT_EQ(ASCIIToUTF16("+1 (800) 555-4321"), s_phones.ColumnString16(1));
   2439 
   2440     ASSERT_TRUE(s_phones.Step());
   2441     EXPECT_EQ("00000000-0000-0000-0000-000000000002", s_phones.ColumnString(0));
   2442     EXPECT_EQ(base::string16(), s_phones.ColumnString16(1));
   2443 
   2444     ASSERT_TRUE(s_phones.Step());
   2445     EXPECT_EQ("00000000-0000-0000-0000-000000000003", s_phones.ColumnString(0));
   2446     EXPECT_EQ(ASCIIToUTF16("6505557890"), s_phones.ColumnString16(1));
   2447 
   2448     ASSERT_TRUE(s_phones.Step());
   2449     EXPECT_EQ("00000000-0000-0000-0000-000000000004", s_phones.ColumnString(0));
   2450     EXPECT_EQ(base::string16(), s_phones.ColumnString16(1));
   2451 
   2452     EXPECT_FALSE(s_phones.Step());
   2453   }
   2454 }
   2455 
   2456 // Tests that migrating from version 54 to version 55 drops the autofill_dates
   2457 // table, and merges the appropriate dates into the autofill table.
   2458 TEST_F(WebDatabaseMigrationTest, MigrateVersion54ToCurrent) {
   2459   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_54.sql")));
   2460 
   2461   // Verify pre-conditions.  These are expectations for version 54 of the
   2462   // database.
   2463   {
   2464     sql::Connection connection;
   2465     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   2466 
   2467     EXPECT_TRUE(connection.DoesTableExist("autofill_dates"));
   2468     EXPECT_FALSE(connection.DoesColumnExist("autofill", "date_created"));
   2469     EXPECT_FALSE(connection.DoesColumnExist("autofill", "date_last_used"));
   2470 
   2471     // Verify the incoming data.
   2472     sql::Statement s_autofill(connection.GetUniqueStatement(
   2473         "SELECT name, value, value_lower, pair_id, count FROM autofill"));
   2474     sql::Statement s_dates(connection.GetUniqueStatement(
   2475         "SELECT pair_id, date_created FROM autofill_dates"));
   2476 
   2477     // An entry with one timestamp.
   2478     ASSERT_TRUE(s_autofill.Step());
   2479     EXPECT_EQ(ASCIIToUTF16("Name"), s_autofill.ColumnString16(0));
   2480     EXPECT_EQ(ASCIIToUTF16("John Doe"), s_autofill.ColumnString16(1));
   2481     EXPECT_EQ(ASCIIToUTF16("john doe"), s_autofill.ColumnString16(2));
   2482     EXPECT_EQ(10, s_autofill.ColumnInt(3));
   2483     EXPECT_EQ(1, s_autofill.ColumnInt(4));
   2484     ASSERT_TRUE(s_dates.Step());
   2485     EXPECT_EQ(10, s_dates.ColumnInt(0));
   2486     EXPECT_EQ(1384299100, s_dates.ColumnInt64(1));
   2487 
   2488     // Another entry with one timestamp, differing from the previous one in case
   2489     // only.
   2490     ASSERT_TRUE(s_autofill.Step());
   2491     EXPECT_EQ(ASCIIToUTF16("Name"), s_autofill.ColumnString16(0));
   2492     EXPECT_EQ(ASCIIToUTF16("john doe"), s_autofill.ColumnString16(1));
   2493     EXPECT_EQ(ASCIIToUTF16("john doe"), s_autofill.ColumnString16(2));
   2494     EXPECT_EQ(11, s_autofill.ColumnInt(3));
   2495     EXPECT_EQ(1, s_autofill.ColumnInt(4));
   2496     ASSERT_TRUE(s_dates.Step());
   2497     EXPECT_EQ(11, s_dates.ColumnInt(0));
   2498     EXPECT_EQ(1384299200, s_dates.ColumnInt64(1));
   2499 
   2500     // An entry with two timestamps (with count > 2; this is realistic).
   2501     ASSERT_TRUE(s_autofill.Step());
   2502     EXPECT_EQ(ASCIIToUTF16("Email"), s_autofill.ColumnString16(0));
   2503     EXPECT_EQ(ASCIIToUTF16("jane (at) example.com"), s_autofill.ColumnString16(1));
   2504     EXPECT_EQ(ASCIIToUTF16("jane (at) example.com"), s_autofill.ColumnString16(2));
   2505     EXPECT_EQ(20, s_autofill.ColumnInt(3));
   2506     EXPECT_EQ(3, s_autofill.ColumnInt(4));
   2507     ASSERT_TRUE(s_dates.Step());
   2508     EXPECT_EQ(20, s_dates.ColumnInt(0));
   2509     EXPECT_EQ(1384299300, s_dates.ColumnInt64(1));
   2510     ASSERT_TRUE(s_dates.Step());
   2511     EXPECT_EQ(20, s_dates.ColumnInt(0));
   2512     EXPECT_EQ(1384299301, s_dates.ColumnInt64(1));
   2513 
   2514     // An entry with more than two timestamps, which are stored out of order.
   2515     ASSERT_TRUE(s_autofill.Step());
   2516     EXPECT_EQ(ASCIIToUTF16("Email"), s_autofill.ColumnString16(0));
   2517     EXPECT_EQ(ASCIIToUTF16("jane.doe (at) example.org"),
   2518               s_autofill.ColumnString16(1));
   2519     EXPECT_EQ(ASCIIToUTF16("jane.doe (at) example.org"),
   2520               s_autofill.ColumnString16(2));
   2521     EXPECT_EQ(21, s_autofill.ColumnInt(3));
   2522     EXPECT_EQ(4, s_autofill.ColumnInt(4));
   2523     ASSERT_TRUE(s_dates.Step());
   2524     EXPECT_EQ(21, s_dates.ColumnInt(0));
   2525     EXPECT_EQ(1384299401, s_dates.ColumnInt64(1));
   2526     ASSERT_TRUE(s_dates.Step());
   2527     EXPECT_EQ(21, s_dates.ColumnInt(0));
   2528     EXPECT_EQ(1384299400, s_dates.ColumnInt64(1));
   2529     ASSERT_TRUE(s_dates.Step());
   2530     EXPECT_EQ(21, s_dates.ColumnInt(0));
   2531     EXPECT_EQ(1384299403, s_dates.ColumnInt64(1));
   2532     ASSERT_TRUE(s_dates.Step());
   2533     EXPECT_EQ(21, s_dates.ColumnInt(0));
   2534     EXPECT_EQ(1384299402, s_dates.ColumnInt64(1));
   2535 
   2536     // No more entries expected.
   2537     ASSERT_FALSE(s_autofill.Step());
   2538     ASSERT_FALSE(s_dates.Step());
   2539   }
   2540 
   2541   DoMigration();
   2542 
   2543   // Verify post-conditions.  These are expectations for current version of the
   2544   // database.
   2545   {
   2546     sql::Connection connection;
   2547     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   2548     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   2549 
   2550     // Check version.
   2551     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   2552 
   2553     // The autofill_dates table should have been dropped, and its columns should
   2554     // have been migrated to the autofill table.
   2555     EXPECT_FALSE(connection.DoesTableExist("autofill_dates"));
   2556     EXPECT_TRUE(connection.DoesColumnExist("autofill", "date_created"));
   2557     EXPECT_TRUE(connection.DoesColumnExist("autofill", "date_last_used"));
   2558 
   2559     // Data should have been preserved.  Note that it appears out of order
   2560     // relative to the previous table, as it's been alphabetized.  That's ok.
   2561     sql::Statement s(
   2562         connection.GetUniqueStatement(
   2563             "SELECT name, value, value_lower, date_created, date_last_used,"
   2564             " count "
   2565             "FROM autofill "
   2566             "ORDER BY name, value ASC"));
   2567 
   2568     // "jane.doe (at) example.org": Timestamps should be parsed correctly, and only
   2569     // the first and last should be kept.
   2570     ASSERT_TRUE(s.Step());
   2571     EXPECT_EQ(ASCIIToUTF16("Email"), s.ColumnString16(0));
   2572     EXPECT_EQ(ASCIIToUTF16("jane.doe (at) example.org"), s.ColumnString16(1));
   2573     EXPECT_EQ(ASCIIToUTF16("jane.doe (at) example.org"), s.ColumnString16(2));
   2574     EXPECT_EQ(1384299400, s.ColumnInt64(3));
   2575     EXPECT_EQ(1384299403, s.ColumnInt64(4));
   2576     EXPECT_EQ(4, s.ColumnInt(5));
   2577 
   2578     // "jane (at) example.com": Timestamps should be parsed correctly.
   2579     ASSERT_TRUE(s.Step());
   2580     EXPECT_EQ(ASCIIToUTF16("Email"), s.ColumnString16(0));
   2581     EXPECT_EQ(ASCIIToUTF16("jane (at) example.com"), s.ColumnString16(1));
   2582     EXPECT_EQ(ASCIIToUTF16("jane (at) example.com"), s.ColumnString16(2));
   2583     EXPECT_EQ(1384299300, s.ColumnInt64(3));
   2584     EXPECT_EQ(1384299301, s.ColumnInt64(4));
   2585     EXPECT_EQ(3, s.ColumnInt(5));
   2586 
   2587     // "John Doe": The single timestamp should be assigned as both the creation
   2588     // and the last use timestamp.
   2589     ASSERT_TRUE(s.Step());
   2590     EXPECT_EQ(ASCIIToUTF16("Name"), s.ColumnString16(0));
   2591     EXPECT_EQ(ASCIIToUTF16("John Doe"), s.ColumnString16(1));
   2592     EXPECT_EQ(ASCIIToUTF16("john doe"), s.ColumnString16(2));
   2593     EXPECT_EQ(1384299100, s.ColumnInt64(3));
   2594     EXPECT_EQ(1384299100, s.ColumnInt64(4));
   2595     EXPECT_EQ(1, s.ColumnInt(5));
   2596 
   2597     // "john doe": Should not be merged with "John Doe" (case-sensitivity).
   2598     ASSERT_TRUE(s.Step());
   2599     EXPECT_EQ(ASCIIToUTF16("Name"), s.ColumnString16(0));
   2600     EXPECT_EQ(ASCIIToUTF16("john doe"), s.ColumnString16(1));
   2601     EXPECT_EQ(ASCIIToUTF16("john doe"), s.ColumnString16(2));
   2602     EXPECT_EQ(1384299200, s.ColumnInt64(3));
   2603     EXPECT_EQ(1384299200, s.ColumnInt64(4));
   2604     EXPECT_EQ(1, s.ColumnInt(5));
   2605 
   2606     // No more entries expected.
   2607     ASSERT_FALSE(s.Step());
   2608   }
   2609 }
   2610 
   2611 // Tests that migrating from version 55 to version 56 adds the language_code
   2612 // column to autofill_profiles table.
   2613 TEST_F(WebDatabaseMigrationTest, MigrateVersion55ToCurrent) {
   2614   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_55.sql")));
   2615 
   2616   // Verify pre-conditions. These are expectations for version 55 of the
   2617   // database.
   2618   {
   2619     sql::Connection connection;
   2620     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   2621     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   2622 
   2623     EXPECT_FALSE(
   2624         connection.DoesColumnExist("autofill_profiles", "language_code"));
   2625   }
   2626 
   2627   DoMigration();
   2628 
   2629   // Verify post-conditions. These are expectations for current version of the
   2630   // database.
   2631   {
   2632     sql::Connection connection;
   2633     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   2634     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   2635 
   2636     // Check version.
   2637     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   2638 
   2639     // The language_code column should have been added to autofill_profiles
   2640     // table.
   2641     EXPECT_TRUE(
   2642         connection.DoesColumnExist("autofill_profiles", "language_code"));
   2643 
   2644     // Data should have been preserved. Language code should have been set to
   2645     // empty string.
   2646     sql::Statement s_profiles(
   2647         connection.GetUniqueStatement(
   2648             "SELECT guid, company_name, street_address, dependent_locality,"
   2649             " city, state, zipcode, sorting_code, country_code, date_modified,"
   2650             " origin, language_code "
   2651             "FROM autofill_profiles"));
   2652 
   2653     ASSERT_TRUE(s_profiles.Step());
   2654     EXPECT_EQ("00000000-0000-0000-0000-000000000001",
   2655               s_profiles.ColumnString(0));
   2656     EXPECT_EQ(ASCIIToUTF16("Google Inc"), s_profiles.ColumnString16(1));
   2657     EXPECT_EQ(ASCIIToUTF16("340 Main St"),
   2658               s_profiles.ColumnString16(2));
   2659     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(3));
   2660     EXPECT_EQ(ASCIIToUTF16("Los Angeles"), s_profiles.ColumnString16(4));
   2661     EXPECT_EQ(ASCIIToUTF16("CA"), s_profiles.ColumnString16(5));
   2662     EXPECT_EQ(ASCIIToUTF16("90291"), s_profiles.ColumnString16(6));
   2663     EXPECT_EQ(base::string16(), s_profiles.ColumnString16(7));
   2664     EXPECT_EQ(ASCIIToUTF16("US"), s_profiles.ColumnString16(8));
   2665     EXPECT_EQ(1395948829, s_profiles.ColumnInt(9));
   2666     EXPECT_EQ(ASCIIToUTF16("Chrome settings"), s_profiles.ColumnString16(10));
   2667     EXPECT_EQ(std::string(), s_profiles.ColumnString(11));
   2668 
   2669     // No more entries expected.
   2670     ASSERT_FALSE(s_profiles.Step());
   2671   }
   2672 }
   2673 
   2674 // Tests that migrating from version 56 to version 57 adds the full_name
   2675 // column to autofill_profile_names table.
   2676 TEST_F(WebDatabaseMigrationTest, MigrateVersion56ToCurrent) {
   2677   ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_56.sql")));
   2678 
   2679   // Verify pre-conditions. These are expectations for version 56 of the
   2680   // database.
   2681   {
   2682     sql::Connection connection;
   2683     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   2684     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   2685 
   2686     EXPECT_FALSE(
   2687         connection.DoesColumnExist("autofill_profile_names", "full_name"));
   2688 
   2689     // Verify the starting data.
   2690     sql::Statement s_names(
   2691         connection.GetUniqueStatement(
   2692             "SELECT guid, first_name, middle_name, last_name "
   2693             "FROM autofill_profile_names"));
   2694     ASSERT_TRUE(s_names.Step());
   2695     EXPECT_EQ("B41FE6E0-B13E-2A2A-BF0B-29FCE2C3ADBD", s_names.ColumnString(0));
   2696     EXPECT_EQ(ASCIIToUTF16("Jon"), s_names.ColumnString16(1));
   2697     EXPECT_EQ(base::string16(), s_names.ColumnString16(2));
   2698     EXPECT_EQ(ASCIIToUTF16("Smith"), s_names.ColumnString16(3));
   2699   }
   2700 
   2701   DoMigration();
   2702 
   2703   // Verify post-conditions. These are expectations for current version of the
   2704   // database.
   2705   {
   2706     sql::Connection connection;
   2707     ASSERT_TRUE(connection.Open(GetDatabasePath()));
   2708     ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
   2709 
   2710     // Check version.
   2711     EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
   2712 
   2713     // The full_name column should have been added to autofill_profile_names
   2714     // table.
   2715     EXPECT_TRUE(
   2716         connection.DoesColumnExist("autofill_profile_names", "full_name"));
   2717 
   2718     // Data should have been preserved. Full name should have been set to the
   2719     // empty string.
   2720     sql::Statement s_names(
   2721         connection.GetUniqueStatement(
   2722             "SELECT guid, first_name, middle_name, last_name, full_name "
   2723             "FROM autofill_profile_names"));
   2724 
   2725     ASSERT_TRUE(s_names.Step());
   2726     EXPECT_EQ("B41FE6E0-B13E-2A2A-BF0B-29FCE2C3ADBD", s_names.ColumnString(0));
   2727     EXPECT_EQ(ASCIIToUTF16("Jon"), s_names.ColumnString16(1));
   2728     EXPECT_EQ(base::string16(), s_names.ColumnString16(2));
   2729     EXPECT_EQ(ASCIIToUTF16("Smith"), s_names.ColumnString16(3));
   2730     EXPECT_EQ(base::string16(), s_names.ColumnString16(4));
   2731 
   2732     // No more entries expected.
   2733     ASSERT_FALSE(s_names.Step());
   2734   }
   2735 }
   2736