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