web_database_migration_unittest.cc revision 868fa2fe829687343ffae624259930155e16dbd8
1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <string>
6
7#include "base/file_util.h"
8#include "base/files/scoped_temp_dir.h"
9#include "base/guid.h"
10#include "base/message_loop.h"
11#include "base/stl_util.h"
12#include "base/strings/string16.h"
13#include "base/strings/string_number_conversions.h"
14#include "base/strings/utf_string_conversions.h"
15#include "base/time.h"
16#include "base/values.h"
17#include "chrome/browser/webdata/keyword_table.h"
18#include "chrome/browser/webdata/logins_table.h"
19#include "chrome/browser/webdata/token_service_table.h"
20#include "chrome/browser/webdata/web_apps_table.h"
21#include "chrome/browser/webdata/web_intents_table.h"
22#include "chrome/test/base/ui_test_utils.h"
23#include "components/autofill/browser/autofill_country.h"
24#include "components/autofill/browser/autofill_profile.h"
25#include "components/autofill/browser/autofill_type.h"
26#include "components/autofill/browser/credit_card.h"
27#include "components/autofill/browser/webdata/autofill_change.h"
28#include "components/autofill/browser/webdata/autofill_entry.h"
29#include "components/autofill/browser/webdata/autofill_table.h"
30#include "components/webdata/common/web_database.h"
31#include "sql/statement.h"
32#include "testing/gtest/include/gtest/gtest.h"
33
34using autofill::AutofillProfile;
35using autofill::AutofillTable;
36using autofill::CreditCard;
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::ADDRESS_HOME_COUNTRY, s.ColumnString16(12), "en-US");
64  profile->SetRawInfo(autofill::PHONE_HOME_WHOLE_NUMBER, s.ColumnString16(13));
65  *date_modified = s.ColumnInt64(15);
66  profile->set_guid(s.ColumnString(16));
67  EXPECT_TRUE(base::IsValidGUID(profile->guid()));
68}
69
70void AutofillProfile33FromStatement(const sql::Statement& s,
71                                    AutofillProfile* profile,
72                                    int64* date_modified) {
73  DCHECK(profile);
74  DCHECK(date_modified);
75  profile->set_guid(s.ColumnString(0));
76  EXPECT_TRUE(base::IsValidGUID(profile->guid()));
77  profile->SetRawInfo(autofill::COMPANY_NAME, s.ColumnString16(1));
78  profile->SetRawInfo(autofill::ADDRESS_HOME_LINE1, s.ColumnString16(2));
79  profile->SetRawInfo(autofill::ADDRESS_HOME_LINE2, s.ColumnString16(3));
80  profile->SetRawInfo(autofill::ADDRESS_HOME_CITY, s.ColumnString16(4));
81  profile->SetRawInfo(autofill::ADDRESS_HOME_STATE, s.ColumnString16(5));
82  profile->SetRawInfo(autofill::ADDRESS_HOME_ZIP, s.ColumnString16(6));
83  profile->SetInfo(
84      autofill::ADDRESS_HOME_COUNTRY, s.ColumnString16(7), "en-US");
85  *date_modified = s.ColumnInt64(8);
86}
87
88void CreditCard31FromStatement(const sql::Statement& s,
89                              CreditCard* credit_card,
90                              base::string16* label,
91                              int* unique_id,
92                              std::string* encrypted_number,
93                              int64* date_modified) {
94  DCHECK(credit_card);
95  DCHECK(label);
96  DCHECK(unique_id);
97  DCHECK(encrypted_number);
98  DCHECK(date_modified);
99  *label = s.ColumnString16(0);
100  *unique_id = s.ColumnInt(1);
101  credit_card->SetRawInfo(autofill::CREDIT_CARD_NAME, s.ColumnString16(2));
102  credit_card->SetRawInfo(autofill::CREDIT_CARD_TYPE, s.ColumnString16(3));
103  credit_card->SetRawInfo(autofill::CREDIT_CARD_EXP_MONTH, s.ColumnString16(5));
104  credit_card->SetRawInfo(
105      autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR, s.ColumnString16(6));
106  int encrypted_number_len = s.ColumnByteLength(10);
107  if (encrypted_number_len) {
108    encrypted_number->resize(encrypted_number_len);
109    memcpy(&(*encrypted_number)[0], s.ColumnBlob(10), encrypted_number_len);
110  }
111  *date_modified = s.ColumnInt64(12);
112  credit_card->set_guid(s.ColumnString(13));
113  EXPECT_TRUE(base::IsValidGUID(credit_card->guid()));
114}
115
116void CreditCard32FromStatement(const sql::Statement& s,
117                               CreditCard* credit_card,
118                               std::string* encrypted_number,
119                               int64* date_modified) {
120  DCHECK(credit_card);
121  DCHECK(encrypted_number);
122  DCHECK(date_modified);
123  credit_card->set_guid(s.ColumnString(0));
124  EXPECT_TRUE(base::IsValidGUID(credit_card->guid()));
125  credit_card->SetRawInfo(autofill::CREDIT_CARD_NAME, s.ColumnString16(1));
126  credit_card->SetRawInfo(autofill::CREDIT_CARD_EXP_MONTH, s.ColumnString16(2));
127  credit_card->SetRawInfo(
128      autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR, s.ColumnString16(3));
129  int encrypted_number_len = s.ColumnByteLength(4);
130  if (encrypted_number_len) {
131    encrypted_number->resize(encrypted_number_len);
132    memcpy(&(*encrypted_number)[0], s.ColumnBlob(4), encrypted_number_len);
133  }
134  *date_modified = s.ColumnInt64(5);
135}
136
137void CheckHasBackupData(sql::MetaTable* meta_table) {
138  std::string value;
139  EXPECT_TRUE(meta_table->GetValue(
140      "Default Search Provider ID Backup", &value));
141  EXPECT_TRUE(meta_table->GetValue(
142      "Default Search Provider ID Backup Signature", &value));
143}
144
145void CheckNoBackupData(const sql::Connection& connection,
146                       sql::MetaTable* meta_table) {
147  std::string value;
148  EXPECT_FALSE(meta_table->GetValue(
149      "Default Search Provider ID Backup", &value));
150  EXPECT_FALSE(meta_table->GetValue(
151      "Default Search Provider ID Backup Signature", &value));
152  EXPECT_FALSE(connection.DoesTableExist("keywords_backup"));
153}
154
155}  // anonymous namespace
156
157// The WebDatabaseMigrationTest encapsulates testing of database migrations.
158// Specifically, these tests are intended to exercise any schema changes in
159// the WebDatabase and data migrations that occur in
160// |WebDatabase::MigrateOldVersionsAsNeeded()|.
161class WebDatabaseMigrationTest : public testing::Test {
162 public:
163  WebDatabaseMigrationTest() {}
164  virtual ~WebDatabaseMigrationTest() {}
165
166  virtual void SetUp() {
167    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
168  }
169
170  // Load the database via the WebDatabase class and migrate the database to
171  // the current version.
172  void DoMigration() {
173    // TODO(joi): This whole unit test file needs to stay in //chrome
174    // for now, as it needs to know about all the different table
175    // types. Once all webdata datatypes have been componentized, this
176    // could move to components_unittests.
177    AutofillTable autofill_table("en-US");
178    KeywordTable keyword_table;
179    LoginsTable logins_table;
180    TokenServiceTable token_service_table;
181    WebAppsTable web_apps_table;
182    WebIntentsTable web_intents_table;
183
184    WebDatabase db;
185    db.AddTable(&autofill_table);
186    db.AddTable(&keyword_table);
187    db.AddTable(&logins_table);
188    db.AddTable(&token_service_table);
189    db.AddTable(&web_apps_table);
190    db.AddTable(&web_intents_table);
191
192    // This causes the migration to occur.
193    ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath()));
194  }
195
196 protected:
197  // Current tested version number.  When adding a migration in
198  // |WebDatabase::MigrateOldVersionsAsNeeded()| and changing the version number
199  // |kCurrentVersionNumber| this value should change to reflect the new version
200  // number and a new migration test added below.
201  static const int kCurrentTestedVersionNumber;
202
203  base::FilePath GetDatabasePath() {
204    const base::FilePath::CharType kWebDatabaseFilename[] =
205        FILE_PATH_LITERAL("TestWebDatabase.sqlite3");
206    return temp_dir_.path().Append(base::FilePath(kWebDatabaseFilename));
207  }
208
209  // The textual contents of |file| are read from
210  // "chrome/test/data/web_database" and returned in the string |contents|.
211  // Returns true if the file exists and is read successfully, false otherwise.
212  bool GetWebDatabaseData(const base::FilePath& file, std::string* contents) {
213    base::FilePath path = ui_test_utils::GetTestFilePath(
214        base::FilePath(FILE_PATH_LITERAL("web_database")), file);
215    return file_util::PathExists(path) &&
216        file_util::ReadFileToString(path, contents);
217  }
218
219  static int VersionFromConnection(sql::Connection* connection) {
220    // Get version.
221    sql::Statement s(connection->GetUniqueStatement(
222        "SELECT value FROM meta WHERE key='version'"));
223    if (!s.Step())
224      return 0;
225    return s.ColumnInt(0);
226  }
227
228  // The sql files located in "chrome/test/data/web_database" were generated by
229  // launching the Chromium application prior to schema change, then using the
230  // sqlite3 command-line application to dump the contents of the "Web Data"
231  // database.
232  // Like this:
233  //   > .output version_nn.sql
234  //   > .dump
235  void LoadDatabase(const base::FilePath::StringType& file);
236
237 private:
238  base::ScopedTempDir temp_dir_;
239
240  DISALLOW_COPY_AND_ASSIGN(WebDatabaseMigrationTest);
241};
242
243const int WebDatabaseMigrationTest::kCurrentTestedVersionNumber = 51;
244
245void WebDatabaseMigrationTest::LoadDatabase(
246    const base::FilePath::StringType& file) {
247  std::string contents;
248  ASSERT_TRUE(GetWebDatabaseData(base::FilePath(file), &contents));
249
250  sql::Connection connection;
251  ASSERT_TRUE(connection.Open(GetDatabasePath()));
252  ASSERT_TRUE(connection.Execute(contents.data()));
253}
254
255// Tests that the all migrations from an empty database succeed.
256TEST_F(WebDatabaseMigrationTest, MigrateEmptyToCurrent) {
257  DoMigration();
258
259  // Verify post-conditions.  These are expectations for current version of the
260  // database.
261  {
262    sql::Connection connection;
263    ASSERT_TRUE(connection.Open(GetDatabasePath()));
264
265    // Check version.
266    EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
267
268    // Check that expected tables are present.
269    EXPECT_TRUE(connection.DoesTableExist("autofill"));
270    EXPECT_TRUE(connection.DoesTableExist("autofill_dates"));
271    EXPECT_TRUE(connection.DoesTableExist("autofill_profiles"));
272    EXPECT_TRUE(connection.DoesTableExist("credit_cards"));
273    EXPECT_TRUE(connection.DoesTableExist("keywords"));
274    // The logins table is obsolete. (We used to store saved passwords here.)
275    EXPECT_FALSE(connection.DoesTableExist("logins"));
276    EXPECT_TRUE(connection.DoesTableExist("meta"));
277    EXPECT_TRUE(connection.DoesTableExist("token_service"));
278    EXPECT_TRUE(connection.DoesTableExist("web_app_icons"));
279    EXPECT_TRUE(connection.DoesTableExist("web_apps"));
280    EXPECT_TRUE(connection.DoesTableExist("web_intents"));
281    EXPECT_TRUE(connection.DoesTableExist("web_intents_defaults"));
282  }
283}
284
285// Tests that the |credit_card| table gets added to the schema for a version 22
286// database.
287TEST_F(WebDatabaseMigrationTest, MigrateVersion22ToCurrent) {
288  // This schema is taken from a build prior to the addition of the
289  // |credit_card| table.  Version 22 of the schema.  Contrast this with the
290  // corrupt version below.
291  ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_22.sql")));
292
293  // Verify pre-conditions.
294  {
295    sql::Connection connection;
296    ASSERT_TRUE(connection.Open(GetDatabasePath()));
297
298    // No |credit_card| table prior to version 23.
299    ASSERT_FALSE(connection.DoesColumnExist("credit_cards", "guid"));
300    ASSERT_FALSE(
301        connection.DoesColumnExist("credit_cards", "card_number_encrypted"));
302  }
303
304  DoMigration();
305
306  // Verify post-conditions.  These are expectations for current version of the
307  // database.
308  {
309    sql::Connection connection;
310    ASSERT_TRUE(connection.Open(GetDatabasePath()));
311
312    // Check version.
313    EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
314
315    // |credit_card| table now exists.
316    EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "guid"));
317    EXPECT_TRUE(
318        connection.DoesColumnExist("credit_cards", "card_number_encrypted"));
319  }
320}
321
322// Tests that the |credit_card| table gets added to the schema for a corrupt
323// version 22 database.  The corruption is that the |credit_cards| table exists
324// but the schema version number was not set correctly to 23 or later.  This
325// test exercises code introduced to fix bug http://crbug.com/50699 that
326// resulted from the corruption.
327TEST_F(WebDatabaseMigrationTest, MigrateVersion22CorruptedToCurrent) {
328  // This schema is taken from a build after the addition of the |credit_card|
329  // table.  Due to a bug in the migration logic the version is set incorrectly
330  // to 22 (it should have been updated to 23 at least).
331  ASSERT_NO_FATAL_FAILURE(
332      LoadDatabase(FILE_PATH_LITERAL("version_22_corrupt.sql")));
333
334  // Verify pre-conditions.  These are expectations for corrupt version 22 of
335  // the database.
336  {
337    sql::Connection connection;
338    ASSERT_TRUE(connection.Open(GetDatabasePath()));
339
340    // Columns existing and not existing before current version.
341    ASSERT_TRUE(connection.DoesColumnExist("credit_cards", "unique_id"));
342    ASSERT_TRUE(
343        connection.DoesColumnExist("credit_cards", "card_number_encrypted"));
344    ASSERT_TRUE(connection.DoesColumnExist("keywords", "id"));
345  }
346
347  DoMigration();
348
349  // Verify post-conditions.  These are expectations for current version of the
350  // database.
351  {
352    sql::Connection connection;
353    ASSERT_TRUE(connection.Open(GetDatabasePath()));
354
355    // Check version.
356    EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
357
358
359    // Columns existing and not existing before version 25.
360    EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "unique_id"));
361    EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "guid"));
362    EXPECT_TRUE(
363        connection.DoesColumnExist("credit_cards", "card_number_encrypted"));
364    EXPECT_TRUE(connection.DoesColumnExist("keywords", "id"));
365  }
366}
367
368// Tests that the |keywords| |created_by_policy| column gets added to the schema
369// for a version 25 database.
370TEST_F(WebDatabaseMigrationTest, MigrateVersion25ToCurrent) {
371  // This schema is taken from a build prior to the addition of the |keywords|
372  // |created_by_policy| column.
373  ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_25.sql")));
374
375  // Verify pre-conditions.  These are expectations for version 25 of the
376  // database.
377  {
378    sql::Connection connection;
379    ASSERT_TRUE(connection.Open(GetDatabasePath()));
380  }
381
382  DoMigration();
383
384  // Verify post-conditions.  These are expectations for current version of the
385  // database.
386  {
387    sql::Connection connection;
388    ASSERT_TRUE(connection.Open(GetDatabasePath()));
389
390    // Check version.
391    EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
392
393    // |keywords| |created_by_policy| column should have been added.
394    EXPECT_TRUE(connection.DoesColumnExist("keywords", "id"));
395    EXPECT_TRUE(connection.DoesColumnExist("keywords", "created_by_policy"));
396  }
397}
398
399// Tests that the credit_cards.billing_address column is changed from a string
400// to an int whilst preserving the associated billing address. This version of
401// the test makes sure a stored label is converted to an ID.
402TEST_F(WebDatabaseMigrationTest, MigrateVersion26ToCurrentStringLabels) {
403  // This schema is taken from a build prior to the change of column type for
404  // credit_cards.billing_address from string to int.
405  ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_26.sql")));
406
407  // Verify pre-conditions. These are expectations for version 26 of the
408  // database.
409  {
410    sql::Connection connection;
411    ASSERT_TRUE(connection.Open(GetDatabasePath()));
412
413    // Columns existing and not existing before current version.
414    EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "billing_address"));
415
416    std::string stmt = "INSERT INTO autofill_profiles"
417      "(label, unique_id, first_name, middle_name, last_name, email,"
418      " company_name, address_line_1, address_line_2, city, state, zipcode,"
419      " country, phone, fax)"
420      "VALUES ('Home',1,'','','','','','','','','','','','','')";
421    sql::Statement s(connection.GetUniqueStatement(stmt.c_str()));
422    ASSERT_TRUE(s.Run());
423
424    // Insert a CC linked to an existing address.
425    std::string stmt2 = "INSERT INTO credit_cards"
426      "(label, unique_id, name_on_card, type, card_number,"
427      " expiration_month, expiration_year, verification_code, billing_address,"
428      " shipping_address, card_number_encrypted, verification_code_encrypted)"
429      "VALUES ('label',2,'Jack','Visa','1234',2,2012,'','Home','','','')";
430    sql::Statement s2(connection.GetUniqueStatement(stmt2.c_str()));
431    ASSERT_TRUE(s2.Run());
432
433    // |billing_address| is a string.
434    std::string stmt3 = "SELECT billing_address FROM credit_cards";
435    sql::Statement s3(connection.GetUniqueStatement(stmt3.c_str()));
436    ASSERT_TRUE(s3.Step());
437    EXPECT_EQ(s3.ColumnType(0), sql::COLUMN_TYPE_TEXT);
438  }
439
440  DoMigration();
441
442  // Verify post-conditions.  These are expectations for current version of the
443  // database.
444  {
445    sql::Connection connection;
446    ASSERT_TRUE(connection.Open(GetDatabasePath()));
447
448    // Check version.
449    EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
450    EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "billing_address"));
451
452    // Verify the credit card data is converted.
453    sql::Statement s(connection.GetUniqueStatement(
454        "SELECT guid, name_on_card, expiration_month, expiration_year, "
455        "card_number_encrypted, date_modified "
456        "FROM credit_cards"));
457    ASSERT_TRUE(s.Step());
458    EXPECT_EQ("Jack", s.ColumnString(1));
459    EXPECT_EQ(2, s.ColumnInt(2));
460    EXPECT_EQ(2012, s.ColumnInt(3));
461    // Column 5 is encrypted number blob.
462    // Column 6 is date_modified.
463  }
464}
465
466// Tests that the credit_cards.billing_address column is changed from a string
467// to an int whilst preserving the associated billing address. This version of
468// the test makes sure a stored string ID is converted to an integer ID.
469TEST_F(WebDatabaseMigrationTest, MigrateVersion26ToCurrentStringIDs) {
470  // This schema is taken from a build prior to the change of column type for
471  // credit_cards.billing_address from string to int.
472  ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_26.sql")));
473
474  // Verify pre-conditions. These are expectations for version 26 of the
475  // database.
476  {
477    sql::Connection connection;
478    ASSERT_TRUE(connection.Open(GetDatabasePath()));
479    EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "billing_address"));
480
481    std::string stmt = "INSERT INTO autofill_profiles"
482      "(label, unique_id, first_name, middle_name, last_name, email,"
483      " company_name, address_line_1, address_line_2, city, state, zipcode,"
484      " country, phone, fax)"
485      "VALUES ('Home',1,'','','','','','','','','','','','','')";
486    sql::Statement s(connection.GetUniqueStatement(stmt.c_str()));
487    ASSERT_TRUE(s.Run());
488
489    // Insert a CC linked to an existing address.
490    std::string stmt2 = "INSERT INTO credit_cards"
491      "(label, unique_id, name_on_card, type, card_number,"
492      " expiration_month, expiration_year, verification_code, billing_address,"
493      " shipping_address, card_number_encrypted, verification_code_encrypted)"
494      "VALUES ('label',2,'Jack','Visa','1234',2,2012,'','1','','','')";
495    sql::Statement s2(connection.GetUniqueStatement(stmt2.c_str()));
496    ASSERT_TRUE(s2.Run());
497
498    // |billing_address| is a string.
499    std::string stmt3 = "SELECT billing_address FROM credit_cards";
500    sql::Statement s3(connection.GetUniqueStatement(stmt3.c_str()));
501    ASSERT_TRUE(s3.Step());
502    EXPECT_EQ(s3.ColumnType(0), sql::COLUMN_TYPE_TEXT);
503  }
504
505  DoMigration();
506
507  // Verify post-conditions.  These are expectations for current version of the
508  // database.
509  {
510    sql::Connection connection;
511    ASSERT_TRUE(connection.Open(GetDatabasePath()));
512
513    // Check version.
514    EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
515
516    // |keywords| |created_by_policy| column should have been added.
517    EXPECT_TRUE(connection.DoesColumnExist("keywords", "id"));
518    EXPECT_TRUE(connection.DoesColumnExist("keywords", "created_by_policy"));
519    EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "billing_address"));
520
521    // Verify the credit card data is converted.
522    sql::Statement s(connection.GetUniqueStatement(
523        "SELECT guid, name_on_card, expiration_month, expiration_year, "
524        "card_number_encrypted, date_modified "
525        "FROM credit_cards"));
526    ASSERT_TRUE(s.Step());
527    EXPECT_EQ("Jack", s.ColumnString(1));
528    EXPECT_EQ(2, s.ColumnInt(2));
529    EXPECT_EQ(2012, s.ColumnInt(3));
530    // Column 5 is encrypted credit card number blo b.
531    // Column 6 is date_modified.
532  }
533}
534
535// Makes sure instant_url is added correctly to keywords.
536TEST_F(WebDatabaseMigrationTest, MigrateVersion27ToCurrent) {
537  // Initialize the database.
538  ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_27.sql")));
539
540  // Verify pre-conditions. These are expectations for version 27 of the
541  // database.
542  {
543    sql::Connection connection;
544    ASSERT_TRUE(connection.Open(GetDatabasePath()));
545
546    ASSERT_FALSE(connection.DoesColumnExist("keywords", "instant_url"));
547  }
548
549  DoMigration();
550
551  // Verify post-conditions.  These are expectations for current version of the
552  // database.
553  {
554    sql::Connection connection;
555    ASSERT_TRUE(connection.Open(GetDatabasePath()));
556
557    // Check version.
558    EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
559
560    // Make sure supports_instant (added in Version 28) was ultimately dropped
561    // again and instant_url was added.
562    EXPECT_FALSE(connection.DoesColumnExist("keywords", "supports_instant"));
563    EXPECT_TRUE(connection.DoesColumnExist("keywords", "instant_url"));
564
565    // Check that instant_url is empty.
566    std::string stmt = "SELECT instant_url FROM keywords";
567    sql::Statement s(connection.GetUniqueStatement(stmt.c_str()));
568    ASSERT_TRUE(s.Step());
569    EXPECT_EQ(std::string(), s.ColumnString(0));
570
571    // Verify the data made it over.
572    stmt = "SELECT " + KeywordTable::GetKeywordColumns() + " FROM keywords";
573    sql::Statement s2(connection.GetUniqueStatement(stmt.c_str()));
574    ASSERT_TRUE(s2.Step());
575    EXPECT_EQ(2, s2.ColumnInt(0));
576    EXPECT_EQ("Google", s2.ColumnString(1));
577    EXPECT_EQ("google.com", s2.ColumnString(2));
578    EXPECT_EQ("http://www.google.com/favicon.ico", s2.ColumnString(3));
579    EXPECT_EQ("{google:baseURL}search?{google:RLZ}{google:acceptedSuggestion}"\
580        "{google:originalQueryForSuggestion}sourceid=chrome&ie={inputEncoding}"\
581        "&q={searchTerms}",
582        s2.ColumnString(4));
583    EXPECT_TRUE(s2.ColumnBool(5));
584    EXPECT_EQ(std::string(), s2.ColumnString(6));
585    EXPECT_EQ(0, s2.ColumnInt(7));
586    EXPECT_EQ(0, s2.ColumnInt(8));
587    EXPECT_EQ(std::string("UTF-8"), s2.ColumnString(9));
588    EXPECT_TRUE(s2.ColumnBool(10));
589    EXPECT_EQ(std::string("{google:baseSuggestURL}search?client=chrome&hl="
590                          "{language}&q={searchTerms}"), s2.ColumnString(11));
591    EXPECT_EQ(1, s2.ColumnInt(12));
592    //EXPECT_EQ(false, s2.ColumnBool(13));
593    EXPECT_EQ(std::string(), s2.ColumnString(14));
594    EXPECT_EQ(0, s2.ColumnInt(15));
595    EXPECT_EQ(std::string(), s2.ColumnString(16));
596  }
597}
598
599// Makes sure date_modified is added correctly to autofill_profiles and
600// credit_cards.
601TEST_F(WebDatabaseMigrationTest, MigrateVersion29ToCurrent) {
602  // Initialize the database.
603  ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_29.sql")));
604
605  // Verify pre-conditions.  These are expectations for version 29 of the
606  // database.
607  {
608    sql::Connection connection;
609    ASSERT_TRUE(connection.Open(GetDatabasePath()));
610
611    EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles",
612                                            "date_modified"));
613    EXPECT_FALSE(connection.DoesColumnExist("credit_cards",
614                                            "date_modified"));
615  }
616
617  Time pre_creation_time = Time::Now();
618  DoMigration();
619  Time post_creation_time = Time::Now();
620
621  // Verify post-conditions.  These are expectations for current version of the
622  // database.
623  {
624    sql::Connection connection;
625    ASSERT_TRUE(connection.Open(GetDatabasePath()));
626
627    // Check version.
628    EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
629
630    // Check that the columns were created.
631    EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
632                                           "date_modified"));
633    EXPECT_TRUE(connection.DoesColumnExist("credit_cards",
634                                           "date_modified"));
635
636    sql::Statement s_profiles(connection.GetUniqueStatement(
637        "SELECT date_modified FROM autofill_profiles "));
638    ASSERT_TRUE(s_profiles.is_valid());
639    while (s_profiles.Step()) {
640      EXPECT_GE(s_profiles.ColumnInt64(0),
641                pre_creation_time.ToTimeT());
642      EXPECT_LE(s_profiles.ColumnInt64(0),
643                post_creation_time.ToTimeT());
644    }
645    EXPECT_TRUE(s_profiles.Succeeded());
646
647    sql::Statement s_credit_cards(connection.GetUniqueStatement(
648        "SELECT date_modified FROM credit_cards "));
649    ASSERT_TRUE(s_credit_cards.is_valid());
650    while (s_credit_cards.Step()) {
651      EXPECT_GE(s_credit_cards.ColumnInt64(0),
652                pre_creation_time.ToTimeT());
653      EXPECT_LE(s_credit_cards.ColumnInt64(0),
654                post_creation_time.ToTimeT());
655    }
656    EXPECT_TRUE(s_credit_cards.Succeeded());
657  }
658}
659
660// Makes sure guids are added to autofill_profiles and credit_cards tables.
661TEST_F(WebDatabaseMigrationTest, MigrateVersion30ToCurrent) {
662  // Initialize the database.
663  ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_30.sql")));
664
665  // Verify pre-conditions. These are expectations for version 29 of the
666  // database.
667  {
668    sql::Connection connection;
669    ASSERT_TRUE(connection.Open(GetDatabasePath()));
670
671    EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "guid"));
672    EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "guid"));
673  }
674
675  DoMigration();
676
677  // Verify post-conditions.  These are expectations for current version of the
678  // database.
679  {
680    sql::Connection connection;
681    ASSERT_TRUE(connection.Open(GetDatabasePath()));
682
683    // Check version.
684    EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
685
686    ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid"));
687    ASSERT_TRUE(connection.DoesColumnExist("credit_cards", "guid"));
688
689    // Check that guids are non-null, non-empty, conforms to guid format, and
690    // are different.
691    sql::Statement s(
692        connection.GetUniqueStatement("SELECT guid FROM autofill_profiles"));
693
694    ASSERT_TRUE(s.Step());
695    std::string guid1 = s.ColumnString(0);
696    EXPECT_TRUE(base::IsValidGUID(guid1));
697
698    ASSERT_TRUE(s.Step());
699    std::string guid2 = s.ColumnString(0);
700    EXPECT_TRUE(base::IsValidGUID(guid2));
701
702    EXPECT_NE(guid1, guid2);
703  }
704}
705
706// Removes unique IDs and make GUIDs the primary key.  Also removes unused
707// columns.
708TEST_F(WebDatabaseMigrationTest, MigrateVersion31ToCurrent) {
709  // Initialize the database.
710  ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_31.sql")));
711
712  // Verify pre-conditions. These are expectations for version 30 of the
713  // database.
714  AutofillProfile profile;
715  base::string16 profile_label;
716  int profile_unique_id = 0;
717  int64 profile_date_modified = 0;
718  CreditCard credit_card;
719  base::string16 cc_label;
720  int cc_unique_id = 0;
721  std::string cc_number_encrypted;
722  int64 cc_date_modified = 0;
723  {
724    sql::Connection connection;
725    ASSERT_TRUE(connection.Open(GetDatabasePath()));
726
727    // Verify existence of columns we'll be changing.
728    EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid"));
729    EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "unique_id"));
730    EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "guid"));
731    EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "unique_id"));
732    EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "type"));
733    EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "card_number"));
734    EXPECT_TRUE(connection.DoesColumnExist("credit_cards",
735                                           "verification_code"));
736    EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "billing_address"));
737    EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "shipping_address"));
738    EXPECT_TRUE(connection.DoesColumnExist("credit_cards",
739                                           "verification_code_encrypted"));
740
741    // Fetch data in the database prior to migration.
742    sql::Statement s1(
743        connection.GetUniqueStatement(
744            "SELECT label, unique_id, first_name, middle_name, last_name, "
745            "email, company_name, address_line_1, address_line_2, city, state, "
746            "zipcode, country, phone, fax, date_modified, guid "
747            "FROM autofill_profiles"));
748    ASSERT_TRUE(s1.Step());
749    EXPECT_NO_FATAL_FAILURE(AutofillProfile31FromStatement(
750        s1, &profile, &profile_label, &profile_unique_id,
751        &profile_date_modified));
752
753    sql::Statement s2(
754        connection.GetUniqueStatement(
755            "SELECT label, unique_id, name_on_card, type, card_number, "
756            "expiration_month, expiration_year, verification_code, "
757            "billing_address, shipping_address, card_number_encrypted, "
758            "verification_code_encrypted, date_modified, guid "
759            "FROM credit_cards"));
760    ASSERT_TRUE(s2.Step());
761    EXPECT_NO_FATAL_FAILURE(CreditCard31FromStatement(s2,
762                                                      &credit_card,
763                                                      &cc_label,
764                                                      &cc_unique_id,
765                                                      &cc_number_encrypted,
766                                                      &cc_date_modified));
767
768    EXPECT_NE(profile_unique_id, cc_unique_id);
769    EXPECT_NE(profile.guid(), credit_card.guid());
770  }
771
772  DoMigration();
773
774  // Verify post-conditions.  These are expectations for current version of the
775  // database.
776  {
777    sql::Connection connection;
778    ASSERT_TRUE(connection.Open(GetDatabasePath()));
779
780    // Check version.
781    EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
782
783    // Verify existence of columns we'll be changing.
784    EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid"));
785    EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "unique_id"));
786    EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "guid"));
787    EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "unique_id"));
788    EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "type"));
789    EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "card_number"));
790    EXPECT_FALSE(connection.DoesColumnExist("credit_cards",
791                                            "verification_code"));
792    EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "billing_address"));
793    EXPECT_FALSE(connection.DoesColumnExist("credit_cards",
794                                            "shipping_address"));
795    EXPECT_FALSE(connection.DoesColumnExist("credit_cards",
796                                            "verification_code_encrypted"));
797
798    // Verify data in the database after the migration.
799    sql::Statement s1(
800        connection.GetUniqueStatement(
801            "SELECT guid, company_name, address_line_1, address_line_2, "
802            "city, state, zipcode, country, date_modified "
803            "FROM autofill_profiles"));
804    ASSERT_TRUE(s1.Step());
805
806    AutofillProfile profile_a;
807    int64 profile_date_modified_a = 0;
808    EXPECT_NO_FATAL_FAILURE(AutofillProfile33FromStatement(
809        s1, &profile_a, &profile_date_modified_a));
810    EXPECT_EQ(profile.guid(), profile_a.guid());
811    EXPECT_EQ(profile.GetRawInfo(autofill::COMPANY_NAME),
812              profile_a.GetRawInfo(autofill::COMPANY_NAME));
813    EXPECT_EQ(profile.GetRawInfo(autofill::ADDRESS_HOME_LINE1),
814              profile_a.GetRawInfo(autofill::ADDRESS_HOME_LINE1));
815    EXPECT_EQ(profile.GetRawInfo(autofill::ADDRESS_HOME_LINE2),
816              profile_a.GetRawInfo(autofill::ADDRESS_HOME_LINE2));
817    EXPECT_EQ(profile.GetRawInfo(autofill::ADDRESS_HOME_CITY),
818              profile_a.GetRawInfo(autofill::ADDRESS_HOME_CITY));
819    EXPECT_EQ(profile.GetRawInfo(autofill::ADDRESS_HOME_STATE),
820              profile_a.GetRawInfo(autofill::ADDRESS_HOME_STATE));
821    EXPECT_EQ(profile.GetRawInfo(autofill::ADDRESS_HOME_ZIP),
822              profile_a.GetRawInfo(autofill::ADDRESS_HOME_ZIP));
823    EXPECT_EQ(profile.GetRawInfo(autofill::ADDRESS_HOME_COUNTRY),
824              profile_a.GetRawInfo(autofill::ADDRESS_HOME_COUNTRY));
825    EXPECT_EQ(profile_date_modified, profile_date_modified_a);
826
827    sql::Statement s2(
828        connection.GetUniqueStatement(
829            "SELECT guid, name_on_card, expiration_month, "
830            "expiration_year, card_number_encrypted, date_modified "
831            "FROM credit_cards"));
832    ASSERT_TRUE(s2.Step());
833
834    CreditCard credit_card_a;
835    base::string16 cc_label_a;
836    std::string cc_number_encrypted_a;
837    int64 cc_date_modified_a = 0;
838    EXPECT_NO_FATAL_FAILURE(CreditCard32FromStatement(s2,
839                                                      &credit_card_a,
840                                                      &cc_number_encrypted_a,
841                                                      &cc_date_modified_a));
842    EXPECT_EQ(credit_card, credit_card_a);
843    EXPECT_EQ(cc_label, cc_label_a);
844    EXPECT_EQ(cc_number_encrypted, cc_number_encrypted_a);
845    EXPECT_EQ(cc_date_modified, cc_date_modified_a);
846  }
847}
848
849// Factor |autofill_profiles| address information separately from name, email,
850// and phone.
851TEST_F(WebDatabaseMigrationTest, MigrateVersion32ToCurrent) {
852  // Initialize the database.
853  ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_32.sql")));
854
855  // Verify pre-conditions. These are expectations for version 32 of the
856  // database.
857  {
858    sql::Connection connection;
859    ASSERT_TRUE(connection.Open(GetDatabasePath()));
860
861    // Verify existence of columns we'll be changing.
862    EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid"));
863    EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "label"));
864    EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "first_name"));
865    EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "middle_name"));
866    EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "last_name"));
867    EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "email"));
868    EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
869                                           "company_name"));
870    EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
871                                           "address_line_1"));
872    EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
873                                           "address_line_2"));
874    EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "city"));
875    EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "state"));
876    EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "zipcode"));
877    EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "country"));
878    EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "phone"));
879    EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "fax"));
880    EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
881                                           "date_modified"));
882
883    EXPECT_FALSE(connection.DoesTableExist("autofill_profile_names"));
884    EXPECT_FALSE(connection.DoesTableExist("autofill_profile_emails"));
885    EXPECT_FALSE(connection.DoesTableExist("autofill_profile_phones"));
886
887    EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "label"));
888  }
889
890  DoMigration();
891
892  // Verify post-conditions.  These are expectations for current version of the
893  // database.
894  {
895    sql::Connection connection;
896    ASSERT_TRUE(connection.Open(GetDatabasePath()));
897
898    // Check version.
899    EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
900
901    // Verify changes to columns.
902    EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid"));
903    EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "label"));
904    EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "first_name"));
905    EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles",
906                                            "middle_name"));
907    EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "last_name"));
908    EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "email"));
909    EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
910                                           "company_name"));
911    EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
912                                           "address_line_1"));
913    EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
914                                           "address_line_2"));
915    EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "city"));
916    EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "state"));
917    EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "zipcode"));
918    EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "country"));
919    EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "phone"));
920    EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "fax"));
921    EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
922                                           "date_modified"));
923
924    // New "names" table.
925    EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_names", "guid"));
926    EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_names",
927                                           "first_name"));
928    EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_names",
929                                           "middle_name"));
930    EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_names",
931                                           "last_name"));
932
933    // New "emails" table.
934    EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_emails", "guid"));
935    EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_emails", "email"));
936
937    // New "phones" table.
938    EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_phones", "guid"));
939    EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_phones", "type"));
940    EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_phones",
941                                           "number"));
942
943    EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "label"));
944
945    // Verify data in the database after the migration.
946    sql::Statement s1(
947        connection.GetUniqueStatement(
948            "SELECT guid, company_name, address_line_1, address_line_2, "
949            "city, state, zipcode, country, date_modified "
950            "FROM autofill_profiles"));
951
952    // John Doe.
953    ASSERT_TRUE(s1.Step());
954    EXPECT_EQ("00580526-FF81-EE2A-0546-1AC593A32E2F", s1.ColumnString(0));
955    EXPECT_EQ(ASCIIToUTF16("Doe Enterprises"), s1.ColumnString16(1));
956    EXPECT_EQ(ASCIIToUTF16("1 Main St"), s1.ColumnString16(2));
957    EXPECT_EQ(ASCIIToUTF16("Apt 1"), s1.ColumnString16(3));
958    EXPECT_EQ(ASCIIToUTF16("Los Altos"), s1.ColumnString16(4));
959    EXPECT_EQ(ASCIIToUTF16("CA"), s1.ColumnString16(5));
960    EXPECT_EQ(ASCIIToUTF16("94022"), s1.ColumnString16(6));
961    EXPECT_EQ(ASCIIToUTF16("United States"), s1.ColumnString16(7));
962    EXPECT_EQ(1297882100L, s1.ColumnInt64(8));
963
964    // John P. Doe.
965    // Gets merged during migration from 35 to 37 due to multi-valued fields.
966
967    // Dave Smith.
968    ASSERT_TRUE(s1.Step());
969    EXPECT_EQ("4C74A9D8-7EEE-423E-F9C2-E7FA70ED1396", s1.ColumnString(0));
970    EXPECT_EQ(base::string16(), s1.ColumnString16(1));
971    EXPECT_EQ(ASCIIToUTF16("2 Main Street"), s1.ColumnString16(2));
972    EXPECT_EQ(base::string16(), s1.ColumnString16(3));
973    EXPECT_EQ(ASCIIToUTF16("Los Altos"), s1.ColumnString16(4));
974    EXPECT_EQ(ASCIIToUTF16("CA"), s1.ColumnString16(5));
975    EXPECT_EQ(ASCIIToUTF16("94022"), s1.ColumnString16(6));
976    EXPECT_EQ(ASCIIToUTF16("United States"), s1.ColumnString16(7));
977    EXPECT_EQ(1297882100L, s1.ColumnInt64(8));
978
979    // Dave Smith (Part 2).
980    ASSERT_TRUE(s1.Step());
981    EXPECT_EQ("722DF5C4-F74A-294A-46F0-31FFDED0D635", s1.ColumnString(0));
982    EXPECT_EQ(base::string16(), s1.ColumnString16(1));
983    EXPECT_EQ(ASCIIToUTF16("2 Main St"), s1.ColumnString16(2));
984    EXPECT_EQ(base::string16(), s1.ColumnString16(3));
985    EXPECT_EQ(ASCIIToUTF16("Los Altos"), s1.ColumnString16(4));
986    EXPECT_EQ(ASCIIToUTF16("CA"), s1.ColumnString16(5));
987    EXPECT_EQ(ASCIIToUTF16("94022"), s1.ColumnString16(6));
988    EXPECT_EQ(ASCIIToUTF16("United States"), s1.ColumnString16(7));
989    EXPECT_EQ(1297882100L, s1.ColumnInt64(8));
990
991    // Alfred E Newman.
992    // Gets culled during migration from 35 to 36 due to incomplete address.
993
994    // 3 Main St.
995    ASSERT_TRUE(s1.Step());
996    EXPECT_EQ("9E5FE298-62C7-83DF-6293-381BC589183F", s1.ColumnString(0));
997    EXPECT_EQ(base::string16(), s1.ColumnString16(1));
998    EXPECT_EQ(ASCIIToUTF16("3 Main St"), s1.ColumnString16(2));
999    EXPECT_EQ(base::string16(), s1.ColumnString16(3));
1000    EXPECT_EQ(ASCIIToUTF16("Los Altos"), s1.ColumnString16(4));
1001    EXPECT_EQ(ASCIIToUTF16("CA"), s1.ColumnString16(5));
1002    EXPECT_EQ(ASCIIToUTF16("94022"), s1.ColumnString16(6));
1003    EXPECT_EQ(ASCIIToUTF16("United States"), s1.ColumnString16(7));
1004    EXPECT_EQ(1297882100L, s1.ColumnInt64(8));
1005
1006    // That should be all.
1007    EXPECT_FALSE(s1.Step());
1008
1009    sql::Statement s2(
1010        connection.GetUniqueStatement(
1011            "SELECT guid, first_name, middle_name, last_name "
1012            "FROM autofill_profile_names"));
1013
1014    // John Doe.
1015    ASSERT_TRUE(s2.Step());
1016    EXPECT_EQ("00580526-FF81-EE2A-0546-1AC593A32E2F", s2.ColumnString(0));
1017    EXPECT_EQ(ASCIIToUTF16("John"), s2.ColumnString16(1));
1018    EXPECT_EQ(base::string16(), s2.ColumnString16(2));
1019    EXPECT_EQ(ASCIIToUTF16("Doe"), s2.ColumnString16(3));
1020
1021    // John P. Doe.  Note same guid as above due to merging of multi-valued
1022    // fields.
1023    ASSERT_TRUE(s2.Step());
1024    EXPECT_EQ("00580526-FF81-EE2A-0546-1AC593A32E2F", s2.ColumnString(0));
1025    EXPECT_EQ(ASCIIToUTF16("John"), s2.ColumnString16(1));
1026    EXPECT_EQ(ASCIIToUTF16("P."), s2.ColumnString16(2));
1027    EXPECT_EQ(ASCIIToUTF16("Doe"), s2.ColumnString16(3));
1028
1029    // Dave Smith.
1030    ASSERT_TRUE(s2.Step());
1031    EXPECT_EQ("4C74A9D8-7EEE-423E-F9C2-E7FA70ED1396", s2.ColumnString(0));
1032    EXPECT_EQ(ASCIIToUTF16("Dave"), s2.ColumnString16(1));
1033    EXPECT_EQ(base::string16(), s2.ColumnString16(2));
1034    EXPECT_EQ(ASCIIToUTF16("Smith"), s2.ColumnString16(3));
1035
1036    // Dave Smith (Part 2).
1037    ASSERT_TRUE(s2.Step());
1038    EXPECT_EQ("722DF5C4-F74A-294A-46F0-31FFDED0D635", s2.ColumnString(0));
1039    EXPECT_EQ(ASCIIToUTF16("Dave"), s2.ColumnString16(1));
1040    EXPECT_EQ(base::string16(), s2.ColumnString16(2));
1041    EXPECT_EQ(ASCIIToUTF16("Smith"), s2.ColumnString16(3));
1042
1043    // Alfred E Newman.
1044    // Gets culled during migration from 35 to 36 due to incomplete address.
1045
1046    // 3 Main St.
1047    ASSERT_TRUE(s2.Step());
1048    EXPECT_EQ("9E5FE298-62C7-83DF-6293-381BC589183F", s2.ColumnString(0));
1049    EXPECT_EQ(base::string16(), s2.ColumnString16(1));
1050    EXPECT_EQ(base::string16(), s2.ColumnString16(2));
1051    EXPECT_EQ(base::string16(), s2.ColumnString16(3));
1052
1053    // Should be all.
1054    EXPECT_FALSE(s2.Step());
1055
1056    sql::Statement s3(
1057        connection.GetUniqueStatement(
1058            "SELECT guid, email "
1059            "FROM autofill_profile_emails"));
1060
1061    // John Doe.
1062    ASSERT_TRUE(s3.Step());
1063    EXPECT_EQ("00580526-FF81-EE2A-0546-1AC593A32E2F", s3.ColumnString(0));
1064    EXPECT_EQ(ASCIIToUTF16("john@doe.com"), s3.ColumnString16(1));
1065
1066    // John P. Doe.
1067    // Gets culled during migration from 35 to 37 due to merging of John Doe and
1068    // John P. Doe addresses.
1069
1070    // 2 Main Street.
1071    ASSERT_TRUE(s3.Step());
1072    EXPECT_EQ("4C74A9D8-7EEE-423E-F9C2-E7FA70ED1396", s3.ColumnString(0));
1073    EXPECT_EQ(base::string16(), s3.ColumnString16(1));
1074
1075    // 2 Main St.
1076    ASSERT_TRUE(s3.Step());
1077    EXPECT_EQ("722DF5C4-F74A-294A-46F0-31FFDED0D635", s3.ColumnString(0));
1078    EXPECT_EQ(base::string16(), s3.ColumnString16(1));
1079
1080    // Alfred E Newman.
1081    // Gets culled during migration from 35 to 36 due to incomplete address.
1082
1083    // 3 Main St.
1084    ASSERT_TRUE(s3.Step());
1085    EXPECT_EQ("9E5FE298-62C7-83DF-6293-381BC589183F", s3.ColumnString(0));
1086    EXPECT_EQ(base::string16(), s3.ColumnString16(1));
1087
1088    // Should be all.
1089    EXPECT_FALSE(s3.Step());
1090
1091    sql::Statement s4(
1092        connection.GetUniqueStatement(
1093            "SELECT guid, type, number "
1094            "FROM autofill_profile_phones"));
1095
1096    // John Doe phone.
1097    ASSERT_TRUE(s4.Step());
1098    EXPECT_EQ("00580526-FF81-EE2A-0546-1AC593A32E2F", s4.ColumnString(0));
1099    EXPECT_EQ(0, s4.ColumnInt(1));  // 0 means phone.
1100    EXPECT_EQ(ASCIIToUTF16("4151112222"), s4.ColumnString16(2));
1101
1102    // John Doe fax.
1103    // Gets culled after fax type removed.
1104
1105    // John P. Doe phone.
1106    // Gets culled during migration from 35 to 37 due to merging of John Doe and
1107    // John P. Doe addresses.
1108
1109    // John P. Doe fax.
1110    // Gets culled during migration from 35 to 37 due to merging of John Doe and
1111    // John P. Doe addresses.
1112
1113    // 2 Main Street phone.
1114    ASSERT_TRUE(s4.Step());
1115    EXPECT_EQ("4C74A9D8-7EEE-423E-F9C2-E7FA70ED1396", s4.ColumnString(0));
1116    EXPECT_EQ(0, s4.ColumnInt(1));  // 0 means phone.
1117    EXPECT_EQ(base::string16(), s4.ColumnString16(2));
1118
1119    // 2 Main Street fax.
1120    // Gets culled after fax type removed.
1121
1122    // 2 Main St phone.
1123    ASSERT_TRUE(s4.Step());
1124    EXPECT_EQ("722DF5C4-F74A-294A-46F0-31FFDED0D635", s4.ColumnString(0));
1125    EXPECT_EQ(0, s4.ColumnInt(1));  // 0 means phone.
1126    EXPECT_EQ(base::string16(), s4.ColumnString16(2));
1127
1128    // 2 Main St fax.
1129    // Gets culled after fax type removed.
1130
1131    // Note no phone or fax for Alfred E Newman.
1132
1133    // 3 Main St phone.
1134    ASSERT_TRUE(s4.Step());
1135    EXPECT_EQ("9E5FE298-62C7-83DF-6293-381BC589183F", s4.ColumnString(0));
1136    EXPECT_EQ(0, s4.ColumnInt(1));  // 0 means phone.
1137    EXPECT_EQ(base::string16(), s4.ColumnString16(2));
1138
1139    // 2 Main St fax.
1140    // Gets culled after fax type removed.
1141
1142    // Should be all.
1143    EXPECT_FALSE(s4.Step());
1144  }
1145}
1146
1147// Adds a column for the autofill profile's country code.
1148TEST_F(WebDatabaseMigrationTest, MigrateVersion33ToCurrent) {
1149  // Initialize the database.
1150  ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_33.sql")));
1151
1152  // Verify pre-conditions. These are expectations for version 33 of the
1153  // database.
1154  {
1155    sql::Connection connection;
1156    ASSERT_TRUE(connection.Open(GetDatabasePath()));
1157
1158    EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles",
1159                                            "country_code"));
1160
1161    // Check that the country value is the one we expect.
1162    sql::Statement s(
1163        connection.GetUniqueStatement("SELECT country FROM autofill_profiles"));
1164
1165    ASSERT_TRUE(s.Step());
1166    std::string country = s.ColumnString(0);
1167    EXPECT_EQ("United States", country);
1168  }
1169
1170  DoMigration();
1171
1172  // Verify post-conditions.  These are expectations for current version of the
1173  // database.
1174  {
1175    sql::Connection connection;
1176    ASSERT_TRUE(connection.Open(GetDatabasePath()));
1177
1178    // Check version.
1179    EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
1180
1181    ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles",
1182                                           "country_code"));
1183
1184    // Check that the country code is properly converted.
1185    sql::Statement s(connection.GetUniqueStatement(
1186        "SELECT country_code FROM autofill_profiles"));
1187
1188    ASSERT_TRUE(s.Step());
1189    std::string country_code = s.ColumnString(0);
1190    EXPECT_EQ("US", country_code);
1191  }
1192}
1193
1194// Cleans up bad country code "UK" in favor of good country code "GB".
1195TEST_F(WebDatabaseMigrationTest, MigrateVersion34ToCurrent) {
1196  // Initialize the database.
1197  ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_34.sql")));
1198
1199  // Verify pre-conditions. These are expectations for version 34 of the
1200  // database.
1201  {
1202    sql::Connection connection;
1203    ASSERT_TRUE(connection.Open(GetDatabasePath()));
1204
1205    EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
1206                                           "country_code"));
1207
1208    // Check that the country_code value is the one we expect.
1209    sql::Statement s(
1210        connection.GetUniqueStatement("SELECT country_code "
1211                                      "FROM autofill_profiles"));
1212
1213    ASSERT_TRUE(s.Step());
1214    std::string country_code = s.ColumnString(0);
1215    EXPECT_EQ("UK", country_code);
1216
1217    // Should have only one.
1218    ASSERT_FALSE(s.Step());
1219  }
1220
1221  DoMigration();
1222
1223  // Verify post-conditions.  These are expectations for current version of the
1224  // database.
1225  {
1226    sql::Connection connection;
1227    ASSERT_TRUE(connection.Open(GetDatabasePath()));
1228
1229    // Check version.
1230    EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
1231
1232    ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles",
1233                                           "country_code"));
1234
1235    // Check that the country_code code is properly converted.
1236    sql::Statement s(connection.GetUniqueStatement(
1237        "SELECT country_code FROM autofill_profiles"));
1238
1239    ASSERT_TRUE(s.Step());
1240    std::string country_code = s.ColumnString(0);
1241    EXPECT_EQ("GB", country_code);
1242
1243    // Should have only one.
1244    ASSERT_FALSE(s.Step());
1245  }
1246}
1247
1248// Cleans up invalid profiles based on more agressive merging.  Filters out
1249// profiles that are subsets of other profiles, and profiles with invalid email,
1250// state, and incomplete address.
1251TEST_F(WebDatabaseMigrationTest, MigrateVersion35ToCurrent) {
1252  // Initialize the database.
1253  ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_35.sql")));
1254
1255  // Verify pre-conditions. These are expectations for version 34 of the
1256  // database.
1257  {
1258    sql::Connection connection;
1259    ASSERT_TRUE(connection.Open(GetDatabasePath()));
1260
1261    EXPECT_FALSE(connection.DoesTableExist("autofill_profiles_trash"));
1262    ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid"));
1263
1264    // Check that there are 6 profiles prior to merge.
1265    sql::Statement s(
1266        connection.GetUniqueStatement("SELECT guid FROM autofill_profiles"));
1267    int i = 0;
1268    while (s.Step())
1269      ++i;
1270    EXPECT_EQ(6, i);
1271  }
1272
1273  DoMigration();
1274
1275  // Verify post-conditions.  These are expectations for current version of the
1276  // database.
1277  {
1278    sql::Connection connection;
1279    ASSERT_TRUE(connection.Open(GetDatabasePath()));
1280
1281    // Check version.
1282    EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
1283
1284    ASSERT_TRUE(connection.DoesTableExist("autofill_profiles_trash"));
1285    ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles_trash", "guid"));
1286    ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid"));
1287
1288    // Verify data in the database after the migration.
1289    sql::Statement s1(
1290        connection.GetUniqueStatement(
1291            "SELECT guid, company_name, address_line_1, address_line_2, "
1292            "city, state, zipcode, country, date_modified "
1293            "FROM autofill_profiles"));
1294
1295    // John Doe.
1296    ASSERT_TRUE(s1.Step());
1297    EXPECT_EQ("00000000-0000-0000-0000-000000000001", s1.ColumnString(0));
1298    EXPECT_EQ(ASCIIToUTF16("Acme Inc."), s1.ColumnString16(1));
1299    EXPECT_EQ(ASCIIToUTF16("1 Main Street"), s1.ColumnString16(2));
1300    EXPECT_EQ(ASCIIToUTF16("Apt 2"), s1.ColumnString16(3));
1301    EXPECT_EQ(ASCIIToUTF16("San Francisco"), s1.ColumnString16(4));
1302    EXPECT_EQ(ASCIIToUTF16("CA"), s1.ColumnString16(5));
1303    EXPECT_EQ(ASCIIToUTF16("94102"), s1.ColumnString16(6));
1304    EXPECT_EQ(ASCIIToUTF16("United States"), s1.ColumnString16(7));
1305    EXPECT_EQ(1300131704, s1.ColumnInt64(8));
1306
1307    // That should be it.
1308    ASSERT_FALSE(s1.Step());
1309
1310    // Check that there 5 trashed profile after the merge.
1311    sql::Statement s2(
1312        connection.GetUniqueStatement("SELECT guid "
1313                                      "FROM autofill_profiles_trash"));
1314    ASSERT_TRUE(s2.Step());
1315    EXPECT_EQ("00000000-0000-0000-0000-000000000002", s2.ColumnString(0));
1316
1317    ASSERT_TRUE(s2.Step());
1318    EXPECT_EQ("00000000-0000-0000-0000-000000000003", s2.ColumnString(0));
1319
1320    ASSERT_TRUE(s2.Step());
1321    EXPECT_EQ("00000000-0000-0000-0000-000000000004", s2.ColumnString(0));
1322
1323    ASSERT_TRUE(s2.Step());
1324    EXPECT_EQ("00000000-0000-0000-0000-000000000005", s2.ColumnString(0));
1325
1326    ASSERT_TRUE(s2.Step());
1327    EXPECT_EQ("00000000-0000-0000-0000-000000000006", s2.ColumnString(0));
1328
1329    // That should be it.
1330    ASSERT_FALSE(s2.Step());
1331  }
1332}
1333
1334// Tests that the |keywords| |last_modified| column gets added to the schema for
1335// a version 37 database.
1336TEST_F(WebDatabaseMigrationTest, MigrateVersion37ToCurrent) {
1337  // This schema is taken from a build prior to the addition of the |keywords|
1338  // |last_modified| column.
1339  ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_37.sql")));
1340
1341  // Verify pre-conditions.  These are expectations for version 37 of the
1342  // database.
1343  {
1344    sql::Connection connection;
1345    ASSERT_TRUE(connection.Open(GetDatabasePath()));
1346
1347    // Columns existing and not existing before current version.
1348    ASSERT_TRUE(connection.DoesColumnExist("keywords", "id"));
1349    ASSERT_FALSE(connection.DoesColumnExist("keywords", "last_modified"));
1350  }
1351
1352  DoMigration();
1353
1354  // Verify post-conditions.  These are expectations for current version of the
1355  // database.
1356  {
1357    sql::Connection connection;
1358    ASSERT_TRUE(connection.Open(GetDatabasePath()));
1359
1360    // Check version.
1361    EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
1362
1363    // |keywords| |last_modified| column should have been added.
1364    EXPECT_TRUE(connection.DoesColumnExist("keywords", "id"));
1365    EXPECT_TRUE(connection.DoesColumnExist("keywords", "last_modified"));
1366  }
1367}
1368
1369// Tests that the |keywords| |sync_guid| column gets added to the schema for
1370// a version 38 database.
1371TEST_F(WebDatabaseMigrationTest, MigrateVersion38ToCurrent) {
1372  // This schema is taken from a build prior to the addition of the |keywords|
1373  // |sync_guid| column.
1374  ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_38.sql")));
1375
1376  // Verify pre-conditions.  These are expectations for version 38 of the
1377  // database.
1378  {
1379    sql::Connection connection;
1380    ASSERT_TRUE(connection.Open(GetDatabasePath()));
1381
1382    // Columns existing and not existing before current version.
1383    ASSERT_TRUE(connection.DoesColumnExist("keywords", "id"));
1384    ASSERT_FALSE(connection.DoesColumnExist("keywords", "sync_guid"));
1385  }
1386
1387  DoMigration();
1388
1389  // Verify post-conditions.  These are expectations for current version of the
1390  // database.
1391  {
1392    sql::Connection connection;
1393    ASSERT_TRUE(connection.Open(GetDatabasePath()));
1394
1395    // Check version.
1396    EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
1397
1398    // |keywords| |sync_guid| column should have been added.
1399    EXPECT_TRUE(connection.DoesColumnExist("keywords", "id"));
1400    EXPECT_TRUE(connection.DoesColumnExist("keywords", "sync_guid"));
1401  }
1402}
1403
1404// Tests that no backup data is added to a version 39 database.
1405TEST_F(WebDatabaseMigrationTest, MigrateVersion39ToCurrent) {
1406  // This schema is taken from a build prior to the addition of the default
1407  // search provider backup field to the meta table.
1408  ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_39.sql")));
1409
1410  // Verify pre-conditions.  These are expectations for version 39 of the
1411  // database.
1412  {
1413    sql::Connection connection;
1414    ASSERT_TRUE(connection.Open(GetDatabasePath()));
1415    ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1416
1417    sql::MetaTable meta_table;
1418    ASSERT_TRUE(meta_table.Init(&connection, 39, 39));
1419
1420    int64 default_search_provider_id = 0;
1421    EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
1422                                    &default_search_provider_id));
1423
1424    EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
1425  }
1426
1427  DoMigration();
1428
1429  // Verify post-conditions.  These are expectations for current version of the
1430  // database.
1431  {
1432    sql::Connection connection;
1433    ASSERT_TRUE(connection.Open(GetDatabasePath()));
1434    ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1435
1436    // Check version.
1437    EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
1438
1439    sql::MetaTable meta_table;
1440    ASSERT_TRUE(meta_table.Init(&connection, kCurrentTestedVersionNumber,
1441                                kCurrentTestedVersionNumber));
1442
1443    int64 default_search_provider_id = 0;
1444    EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
1445                                    &default_search_provider_id));
1446    EXPECT_NE(0, default_search_provider_id);
1447
1448    EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
1449  }
1450}
1451
1452// Tests that the backup data is removed from the database.
1453TEST_F(WebDatabaseMigrationTest, MigrateVersion40ToCurrent) {
1454  ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_40.sql")));
1455
1456  // Verify pre-conditions.  These are expectations for version 40 of the
1457  // database.
1458  {
1459    sql::Connection connection;
1460    ASSERT_TRUE(connection.Open(GetDatabasePath()));
1461    ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1462
1463    sql::MetaTable meta_table;
1464    ASSERT_TRUE(meta_table.Init(&connection, 40, 40));
1465
1466    int64 default_search_provider_id = 0;
1467    EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
1468                                    &default_search_provider_id));
1469
1470    EXPECT_NO_FATAL_FAILURE(CheckHasBackupData(&meta_table));
1471  }
1472
1473  DoMigration();
1474
1475  // Verify post-conditions.  These are expectations for current version of the
1476  // database.
1477  {
1478    sql::Connection connection;
1479    ASSERT_TRUE(connection.Open(GetDatabasePath()));
1480    ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1481
1482    // Check version.
1483    EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
1484
1485    sql::MetaTable meta_table;
1486    ASSERT_TRUE(meta_table.Init(&connection, kCurrentTestedVersionNumber,
1487                                kCurrentTestedVersionNumber));
1488
1489    int64 default_search_provider_id = 0;
1490    EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
1491                                    &default_search_provider_id));
1492    EXPECT_NE(0, default_search_provider_id);
1493
1494    EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
1495  }
1496}
1497
1498// Tests that the backup data is removed from the database.
1499TEST_F(WebDatabaseMigrationTest, MigrateVersion41ToCurrent) {
1500  ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_41.sql")));
1501
1502  // Verify pre-conditions.  These are expectations for version 41 of the
1503  // database.
1504  {
1505    sql::Connection connection;
1506    ASSERT_TRUE(connection.Open(GetDatabasePath()));
1507    ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1508
1509    sql::MetaTable meta_table;
1510    ASSERT_TRUE(meta_table.Init(&connection, 41, 41));
1511
1512    int64 default_search_provider_id = 0;
1513    EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
1514                                    &default_search_provider_id));
1515
1516    EXPECT_NO_FATAL_FAILURE(CheckHasBackupData(&meta_table));
1517  }
1518
1519  DoMigration();
1520
1521  // Verify post-conditions.  These are expectations for current version of the
1522  // database.
1523  {
1524    sql::Connection connection;
1525    ASSERT_TRUE(connection.Open(GetDatabasePath()));
1526    ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1527
1528    // Check version.
1529    EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
1530
1531    sql::MetaTable meta_table;
1532    ASSERT_TRUE(meta_table.Init(&connection, kCurrentTestedVersionNumber,
1533                                kCurrentTestedVersionNumber));
1534
1535    int64 default_search_provider_id = 0;
1536    EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
1537                                    &default_search_provider_id));
1538    EXPECT_NE(0, default_search_provider_id);
1539
1540    EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
1541  }
1542}
1543
1544// Tests that the backup data is removed from the database.
1545TEST_F(WebDatabaseMigrationTest, MigrateVersion42ToCurrent) {
1546  ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_42.sql")));
1547
1548  // Verify pre-conditions.  These are expectations for version 42 of the
1549  // database.
1550  {
1551    sql::Connection connection;
1552    ASSERT_TRUE(connection.Open(GetDatabasePath()));
1553    ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1554
1555    sql::MetaTable meta_table;
1556    ASSERT_TRUE(meta_table.Init(&connection, 42, 42));
1557
1558    int64 default_search_provider_id = 0;
1559    EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
1560                                    &default_search_provider_id));
1561
1562    EXPECT_NO_FATAL_FAILURE(CheckHasBackupData(&meta_table));
1563
1564    EXPECT_FALSE(connection.DoesTableExist("keywords_backup"));
1565  }
1566
1567  DoMigration();
1568
1569  // Verify post-conditions.  These are expectations for current version of the
1570  // database.
1571  {
1572    sql::Connection connection;
1573    ASSERT_TRUE(connection.Open(GetDatabasePath()));
1574    ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1575
1576    // Check version.
1577    EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
1578
1579    sql::MetaTable meta_table;
1580    ASSERT_TRUE(meta_table.Init(&connection, kCurrentTestedVersionNumber,
1581                                kCurrentTestedVersionNumber));
1582
1583    int64 default_search_provider_id = 0;
1584    EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
1585                                    &default_search_provider_id));
1586    EXPECT_NE(0, default_search_provider_id);
1587
1588    EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
1589  }
1590}
1591
1592// Tests that the backup data is removed from the database.
1593TEST_F(WebDatabaseMigrationTest, MigrateVersion43ToCurrent) {
1594  ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_43.sql")));
1595
1596  int64 previous_default_search_provider_id;
1597
1598  // Verify pre-conditions.  These are expectations for version 43 of the
1599  // database.
1600  {
1601    sql::Connection connection;
1602    ASSERT_TRUE(connection.Open(GetDatabasePath()));
1603    ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1604
1605    sql::MetaTable meta_table;
1606    ASSERT_TRUE(meta_table.Init(&connection, 43, 43));
1607
1608    int64 default_search_provider_id = 0;
1609    EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
1610                                    &default_search_provider_id));
1611    EXPECT_NE(default_search_provider_id, 0);
1612    previous_default_search_provider_id = default_search_provider_id;
1613
1614    EXPECT_NO_FATAL_FAILURE(CheckHasBackupData(&meta_table));
1615    EXPECT_TRUE(connection.DoesTableExist("keywords_backup"));
1616  }
1617
1618  DoMigration();
1619
1620  // Verify post-conditions.  These are expectations for current version of the
1621  // database.
1622  {
1623    sql::Connection connection;
1624    ASSERT_TRUE(connection.Open(GetDatabasePath()));
1625    ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1626
1627    // Check version.
1628    EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
1629
1630    sql::MetaTable meta_table;
1631    ASSERT_TRUE(meta_table.Init(
1632        &connection,
1633        kCurrentTestedVersionNumber,
1634        kCurrentTestedVersionNumber));
1635
1636    int64 default_search_provider_id = 0;
1637    EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
1638                                    &default_search_provider_id));
1639    // Default search provider ID should not change.
1640    EXPECT_EQ(previous_default_search_provider_id, default_search_provider_id);
1641
1642    EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
1643  }
1644}
1645
1646// Tests that the |autogenerate_keyword| and |logo_id| columns get removed from
1647// the keyword table schema for a version 45 database.
1648TEST_F(WebDatabaseMigrationTest, MigrateVersion44ToCurrent) {
1649  ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_44.sql")));
1650
1651  // Verify pre-conditions.  These are expectations for version 44 of the
1652  // database.
1653  {
1654    sql::Connection connection;
1655    ASSERT_TRUE(connection.Open(GetDatabasePath()));
1656    ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1657
1658    sql::MetaTable meta_table;
1659    ASSERT_TRUE(meta_table.Init(&connection, 44, 44));
1660
1661    ASSERT_TRUE(connection.DoesColumnExist("keywords", "autogenerate_keyword"));
1662    ASSERT_TRUE(connection.DoesColumnExist("keywords", "logo_id"));
1663  }
1664
1665  DoMigration();
1666
1667  // Verify post-conditions.  These are expectations for current version of the
1668  // database.
1669  {
1670    sql::Connection connection;
1671    ASSERT_TRUE(connection.Open(GetDatabasePath()));
1672    ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1673
1674    // Check version.
1675    EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
1676
1677    sql::MetaTable meta_table;
1678    ASSERT_TRUE(meta_table.Init(&connection, kCurrentTestedVersionNumber,
1679                                kCurrentTestedVersionNumber));
1680
1681    // We should have removed this obsolete key.
1682    std::string default_search_provider_backup;
1683    EXPECT_FALSE(meta_table.GetValue("Default Search Provider Backup",
1684                                     &default_search_provider_backup));
1685
1686    // Two columns should have been removed.
1687    EXPECT_FALSE(connection.DoesColumnExist("keywords",
1688                                            "autogenerate_keyword"));
1689    EXPECT_FALSE(connection.DoesColumnExist("keywords", "logo_id"));
1690
1691    // Backup data should have been removed.
1692    EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
1693  }
1694}
1695
1696// Tests that the web_intents and web_intents_defaults tables are
1697// modified to include "scheme" columns.
1698TEST_F(WebDatabaseMigrationTest, MigrateVersion45ToCurrent) {
1699  ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_45.sql")));
1700
1701  // Verify pre-conditions.  These are expectations for version 45 of the
1702  // database.
1703  {
1704    sql::Connection connection;
1705    ASSERT_TRUE(connection.Open(GetDatabasePath()));
1706    ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1707
1708    sql::MetaTable meta_table;
1709    ASSERT_TRUE(meta_table.Init(&connection, 45, 45));
1710
1711    ASSERT_FALSE(connection.DoesColumnExist("scheme", "web_intents"));
1712    ASSERT_FALSE(connection.DoesColumnExist(
1713        "scheme", "web_intents_defaults"));
1714  }
1715
1716  DoMigration();
1717
1718  // Verify post-conditions.  These are expectations for current version of the
1719  // database.
1720  {
1721    sql::Connection connection;
1722    ASSERT_TRUE(connection.Open(GetDatabasePath()));
1723    ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1724
1725    // Check version.
1726    EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
1727
1728    sql::MetaTable meta_table;
1729    ASSERT_TRUE(meta_table.Init(
1730        &connection,
1731        kCurrentTestedVersionNumber,
1732        kCurrentTestedVersionNumber));
1733
1734    // A new "scheme" column should have been added to each web_intents table.
1735    EXPECT_TRUE(connection.DoesColumnExist("web_intents", "scheme"));
1736    EXPECT_TRUE(connection.DoesColumnExist("web_intents_defaults", "scheme"));
1737
1738    // Verify existing user data was copied.
1739    sql::Statement s1(
1740        connection.GetUniqueStatement("SELECT * FROM web_intents"));
1741
1742    ASSERT_TRUE(s1.Step());
1743    EXPECT_EQ("http://poodles.com/fuzzer", s1.ColumnString(0));
1744    EXPECT_EQ(ASCIIToUTF16("fuzz"), s1.ColumnString16(1));
1745    EXPECT_EQ(ASCIIToUTF16("poodle/*"), s1.ColumnString16(2));
1746    EXPECT_EQ(ASCIIToUTF16("Poodle Fuzzer"), s1.ColumnString16(3));
1747    EXPECT_EQ(ASCIIToUTF16("window"), s1.ColumnString16(4));
1748    EXPECT_EQ(ASCIIToUTF16(""), s1.ColumnString16(5));
1749    ASSERT_FALSE(s1.Step());
1750
1751    // Now we want to verify existing user data was copied
1752    sql::Statement s2(
1753        connection.GetUniqueStatement("SELECT * FROM web_intents_defaults"));
1754
1755    ASSERT_TRUE(s2.Step());
1756    EXPECT_EQ("fuzz", s2.ColumnString(0));
1757    EXPECT_EQ(ASCIIToUTF16("poodle/*"), s2.ColumnString16(1));
1758    EXPECT_EQ(ASCIIToUTF16(""), s2.ColumnString16(2));
1759    EXPECT_EQ(0, s2.ColumnInt(3));
1760    EXPECT_EQ(0, s2.ColumnInt(4));
1761    EXPECT_EQ(ASCIIToUTF16("http://poodles.com/fuzzer"), s2.ColumnString16(5));
1762    EXPECT_EQ(ASCIIToUTF16(""), s2.ColumnString16(6));
1763    ASSERT_FALSE(s2.Step());
1764
1765    // finally ensure the migration code cleaned up after itself
1766    EXPECT_FALSE(connection.DoesTableExist("old_web_intents"));
1767    EXPECT_FALSE(connection.DoesTableExist("old_web_intents_defaults"));
1768  }
1769}
1770
1771// Tests that the web_intents and web_intents_defaults tables are
1772// modified to include "scheme" columns.
1773TEST_F(WebDatabaseMigrationTest, MigrateVersion45InvalidToCurrent) {
1774  ASSERT_NO_FATAL_FAILURE(
1775      LoadDatabase(FILE_PATH_LITERAL("version_45_invalid.sql")));
1776
1777  // Verify pre-conditions.  These are expectations for version 45 of the
1778  // database.
1779  {
1780    sql::Connection connection;
1781    ASSERT_TRUE(connection.Open(GetDatabasePath()));
1782    ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1783
1784    sql::MetaTable meta_table;
1785    ASSERT_TRUE(meta_table.Init(&connection, 45, 45));
1786
1787    ASSERT_FALSE(connection.DoesColumnExist("scheme", "web_intents"));
1788    ASSERT_FALSE(connection.DoesColumnExist(
1789        "scheme", "web_intents_defaults"));
1790  }
1791
1792  DoMigration();
1793
1794  // Verify post-conditions.  These are expectations for current version of the
1795  // database.
1796  {
1797    sql::Connection connection;
1798    ASSERT_TRUE(connection.Open(GetDatabasePath()));
1799    ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1800
1801    // Check version.
1802    EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
1803
1804    sql::MetaTable meta_table;
1805    ASSERT_TRUE(meta_table.Init(
1806        &connection,
1807        kCurrentTestedVersionNumber,
1808        kCurrentTestedVersionNumber));
1809
1810    // A new "scheme" column should have been added to each web_intents table.
1811    EXPECT_TRUE(connection.DoesColumnExist("web_intents", "scheme"));
1812    EXPECT_TRUE(connection.DoesColumnExist("web_intents_defaults", "scheme"));
1813
1814    // Verify existing user data was copied.
1815    sql::Statement s1(
1816        connection.GetUniqueStatement("SELECT * FROM web_intents"));
1817
1818    ASSERT_FALSE(s1.Step());  // Basically should be empty at this point.
1819
1820    // Now we want to verify existing user data was copied
1821    sql::Statement s2(
1822        connection.GetUniqueStatement("SELECT * FROM web_intents_defaults"));
1823
1824    // We were able to create the new tables, but unable to copy any data
1825    // Given the initial bad state of the tables.
1826    ASSERT_FALSE(s2.Step());
1827
1828    // Finally ensure the migration code cleaned up after itself.
1829    EXPECT_FALSE(connection.DoesTableExist("old_web_intents"));
1830    EXPECT_FALSE(connection.DoesTableExist("old_web_intents_defaults"));
1831  }
1832}
1833
1834// Check that current version is forced to compatible version before migration,
1835// if the former is smaller.
1836TEST_F(WebDatabaseMigrationTest, MigrateVersion45CompatibleToCurrent) {
1837  ASSERT_NO_FATAL_FAILURE(
1838      LoadDatabase(FILE_PATH_LITERAL("version_45_compatible.sql")));
1839
1840  // Verify pre-conditions.  These are expectations for version 45 of the
1841  // database.
1842  {
1843    sql::Connection connection;
1844    ASSERT_TRUE(connection.Open(GetDatabasePath()));
1845    ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1846
1847    sql::MetaTable meta_table;
1848    // Database is actually version 45 but the version field states 40.
1849    ASSERT_TRUE(meta_table.Init(&connection, 40, 45));
1850  }
1851
1852  DoMigration();
1853
1854  // Verify post-conditions.  These are expectations for current version of the
1855  // database.
1856  {
1857    sql::Connection connection;
1858    ASSERT_TRUE(connection.Open(GetDatabasePath()));
1859    ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1860
1861    // Check version.
1862    EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
1863    EXPECT_LE(45, VersionFromConnection(&connection));
1864  }
1865}
1866
1867// Tests that the |alternate_urls| column is added to the keyword table schema
1868// for a version 47 database.
1869TEST_F(WebDatabaseMigrationTest, MigrateVersion46ToCurrent) {
1870  ASSERT_NO_FATAL_FAILURE(
1871      LoadDatabase(FILE_PATH_LITERAL("version_46.sql")));
1872
1873  // Verify pre-conditions.  These are expectations for version 46 of the
1874  // database.
1875  {
1876    sql::Connection connection;
1877    ASSERT_TRUE(connection.Open(GetDatabasePath()));
1878    ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1879
1880    sql::MetaTable meta_table;
1881    ASSERT_TRUE(meta_table.Init(&connection, 46, 46));
1882
1883    ASSERT_FALSE(connection.DoesColumnExist("keywords", "alternate_urls"));
1884    ASSERT_FALSE(connection.DoesColumnExist("keywords_backup",
1885                                            "alternate_urls"));
1886  }
1887
1888  DoMigration();
1889
1890  // Verify post-conditions.  These are expectations for current version of the
1891  // database.
1892  {
1893    sql::Connection connection;
1894    ASSERT_TRUE(connection.Open(GetDatabasePath()));
1895    ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1896
1897    // Check version.
1898    EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
1899
1900    // A new column should have been created.
1901    EXPECT_TRUE(connection.DoesColumnExist("keywords", "alternate_urls"));
1902  }
1903}
1904
1905// Tests that the backup data is removed from the database.
1906TEST_F(WebDatabaseMigrationTest, MigrateVersion47ToCurrent) {
1907  ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_47.sql")));
1908
1909  // Verify pre-conditions.  These are expectations for version 47 of the
1910  // database.
1911  {
1912    sql::Connection connection;
1913    ASSERT_TRUE(connection.Open(GetDatabasePath()));
1914    ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1915
1916    sql::MetaTable meta_table;
1917    ASSERT_TRUE(meta_table.Init(&connection, 47, 47));
1918
1919    int64 default_search_provider_id = 0;
1920    EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
1921                                    &default_search_provider_id));
1922    EXPECT_NE(0, default_search_provider_id);
1923
1924    EXPECT_NO_FATAL_FAILURE(CheckHasBackupData(&meta_table));
1925    EXPECT_TRUE(connection.DoesTableExist("keywords_backup"));
1926  }
1927
1928  DoMigration();
1929
1930  // Verify post-conditions.  These are expectations for current version of the
1931  // database.
1932  {
1933    sql::Connection connection;
1934    ASSERT_TRUE(connection.Open(GetDatabasePath()));
1935    ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1936
1937    // Check version.
1938    EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
1939
1940    sql::MetaTable meta_table;
1941    ASSERT_TRUE(meta_table.Init(&connection, kCurrentTestedVersionNumber,
1942                                kCurrentTestedVersionNumber));
1943
1944    int64 default_search_provider_id = 0;
1945    EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
1946                                    &default_search_provider_id));
1947    EXPECT_NE(0, default_search_provider_id);
1948
1949    EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
1950  }
1951}
1952
1953// Tests that the |search_terms_replacement_key| column is added to the keyword
1954// table schema for a version 49 database.
1955TEST_F(WebDatabaseMigrationTest, MigrateVersion48ToCurrent) {
1956  ASSERT_NO_FATAL_FAILURE(
1957      LoadDatabase(FILE_PATH_LITERAL("version_48.sql")));
1958
1959  // Verify pre-conditions.  These are expectations for version 48 of the
1960  // database.
1961  {
1962    sql::Connection connection;
1963    ASSERT_TRUE(connection.Open(GetDatabasePath()));
1964    ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1965
1966    sql::MetaTable meta_table;
1967    ASSERT_TRUE(meta_table.Init(&connection, 48, 48));
1968
1969    ASSERT_FALSE(connection.DoesColumnExist("keywords",
1970                                            "search_terms_replacement_key"));
1971  }
1972
1973  DoMigration();
1974
1975  // Verify post-conditions.  These are expectations for current version of the
1976  // database.
1977  {
1978    sql::Connection connection;
1979    ASSERT_TRUE(connection.Open(GetDatabasePath()));
1980    ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1981
1982    // Check version.
1983    EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
1984
1985    // A new column should have been created.
1986    EXPECT_TRUE(connection.DoesColumnExist("keywords",
1987                                           "search_terms_replacement_key"));
1988  }
1989}
1990
1991// Tests that the |origin| column is added to the autofill_profiles and
1992// credit_cards table schemas for a version 50 database.
1993TEST_F(WebDatabaseMigrationTest, MigrateVersion49ToCurrent) {
1994  ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_49.sql")));
1995
1996  // Verify pre-conditions.  These are expectations for version 49 of the
1997  // database.
1998  {
1999    sql::Connection connection;
2000    ASSERT_TRUE(connection.Open(GetDatabasePath()));
2001
2002    ASSERT_FALSE(connection.DoesColumnExist("autofill_profiles", "origin"));
2003    ASSERT_FALSE(connection.DoesColumnExist("credit_cards", "origin"));
2004  }
2005
2006  DoMigration();
2007
2008  // Verify post-conditions.  These are expectations for current version of the
2009  // database.
2010  {
2011    sql::Connection connection;
2012    ASSERT_TRUE(connection.Open(GetDatabasePath()));
2013    ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
2014
2015    // Check version.
2016    EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
2017
2018    // A new column should have been created in both tables.
2019    EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "origin"));
2020    EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "origin"));
2021  }
2022}
2023