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