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