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.
7 #include "base/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/path_service.h"
12 #include "base/stl_util.h"
13 #include "base/strings/string16.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/time/time.h"
17 #include "base/values.h"
18 #include "chrome/browser/webdata/keyword_table.h"
19 #include "chrome/browser/webdata/logins_table.h"
20 #include "chrome/browser/webdata/web_apps_table.h"
21 #include "chrome/browser/webdata/web_intents_table.h"
22 #include "components/autofill/core/browser/autofill_country.h"
23 #include "components/autofill/core/browser/autofill_profile.h"
24 #include "components/autofill/core/browser/autofill_type.h"
25 #include "components/autofill/core/browser/credit_card.h"
26 #include "components/autofill/core/browser/webdata/autofill_change.h"
27 #include "components/autofill/core/browser/webdata/autofill_entry.h"
28 #include "components/autofill/core/browser/webdata/autofill_table.h"
29 #include "components/signin/core/browser/webdata/token_service_table.h"
30 #include "components/webdata/common/web_database.h"
31 #include "sql/statement.h"
32 #include "testing/gtest/include/gtest/gtest.h"
34 using autofill::AutofillProfile;
35 using autofill::AutofillTable;
36 using autofill::CreditCard;
37 using base::ASCIIToUTF16;
42 void AutofillProfile31FromStatement(const sql::Statement& s,
43 AutofillProfile* profile,
44 base::string16* label,
46 int64* date_modified) {
50 DCHECK(date_modified);
51 *label = s.ColumnString16(0);
52 *unique_id = s.ColumnInt(1);
53 profile->SetRawInfo(autofill::NAME_FIRST, s.ColumnString16(2));
54 profile->SetRawInfo(autofill::NAME_MIDDLE, s.ColumnString16(3));
55 profile->SetRawInfo(autofill::NAME_LAST, s.ColumnString16(4));
56 profile->SetRawInfo(autofill::EMAIL_ADDRESS, s.ColumnString16(5));
57 profile->SetRawInfo(autofill::COMPANY_NAME, s.ColumnString16(6));
58 profile->SetRawInfo(autofill::ADDRESS_HOME_LINE1, s.ColumnString16(7));
59 profile->SetRawInfo(autofill::ADDRESS_HOME_LINE2, s.ColumnString16(8));
60 profile->SetRawInfo(autofill::ADDRESS_HOME_CITY, s.ColumnString16(9));
61 profile->SetRawInfo(autofill::ADDRESS_HOME_STATE, s.ColumnString16(10));
62 profile->SetRawInfo(autofill::ADDRESS_HOME_ZIP, s.ColumnString16(11));
64 autofill::AutofillType(autofill::ADDRESS_HOME_COUNTRY),
65 s.ColumnString16(12), "en-US");
66 profile->SetRawInfo(autofill::PHONE_HOME_WHOLE_NUMBER, s.ColumnString16(13));
67 *date_modified = s.ColumnInt64(15);
68 profile->set_guid(s.ColumnString(16));
69 EXPECT_TRUE(base::IsValidGUID(profile->guid()));
72 void AutofillProfile33FromStatement(const sql::Statement& s,
73 AutofillProfile* profile,
74 int64* date_modified) {
76 DCHECK(date_modified);
77 profile->set_guid(s.ColumnString(0));
78 EXPECT_TRUE(base::IsValidGUID(profile->guid()));
79 profile->SetRawInfo(autofill::COMPANY_NAME, s.ColumnString16(1));
80 profile->SetRawInfo(autofill::ADDRESS_HOME_STREET_ADDRESS,
82 profile->SetRawInfo(autofill::ADDRESS_HOME_CITY, s.ColumnString16(3));
83 profile->SetRawInfo(autofill::ADDRESS_HOME_STATE, s.ColumnString16(4));
84 profile->SetRawInfo(autofill::ADDRESS_HOME_ZIP, s.ColumnString16(5));
86 autofill::AutofillType(autofill::ADDRESS_HOME_COUNTRY),
87 s.ColumnString16(6), "en-US");
88 *date_modified = s.ColumnInt64(7);
91 void CreditCard31FromStatement(const sql::Statement& s,
92 CreditCard* credit_card,
93 base::string16* label,
95 std::string* encrypted_number,
96 int64* date_modified) {
100 DCHECK(encrypted_number);
101 DCHECK(date_modified);
102 *label = s.ColumnString16(0);
103 *unique_id = s.ColumnInt(1);
104 credit_card->SetRawInfo(autofill::CREDIT_CARD_NAME, s.ColumnString16(2));
105 credit_card->SetRawInfo(autofill::CREDIT_CARD_TYPE, s.ColumnString16(3));
106 credit_card->SetRawInfo(autofill::CREDIT_CARD_EXP_MONTH, s.ColumnString16(5));
107 credit_card->SetRawInfo(
108 autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR, s.ColumnString16(6));
109 int encrypted_number_len = s.ColumnByteLength(10);
110 if (encrypted_number_len) {
111 encrypted_number->resize(encrypted_number_len);
112 memcpy(&(*encrypted_number)[0], s.ColumnBlob(10), encrypted_number_len);
114 *date_modified = s.ColumnInt64(12);
115 credit_card->set_guid(s.ColumnString(13));
116 EXPECT_TRUE(base::IsValidGUID(credit_card->guid()));
119 void CreditCard32FromStatement(const sql::Statement& s,
120 CreditCard* credit_card,
121 std::string* encrypted_number,
122 int64* date_modified) {
124 DCHECK(encrypted_number);
125 DCHECK(date_modified);
126 credit_card->set_guid(s.ColumnString(0));
127 EXPECT_TRUE(base::IsValidGUID(credit_card->guid()));
128 credit_card->SetRawInfo(autofill::CREDIT_CARD_NAME, s.ColumnString16(1));
129 credit_card->SetRawInfo(autofill::CREDIT_CARD_EXP_MONTH, s.ColumnString16(2));
130 credit_card->SetRawInfo(
131 autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR, s.ColumnString16(3));
132 int encrypted_number_len = s.ColumnByteLength(4);
133 if (encrypted_number_len) {
134 encrypted_number->resize(encrypted_number_len);
135 memcpy(&(*encrypted_number)[0], s.ColumnBlob(4), encrypted_number_len);
137 *date_modified = s.ColumnInt64(5);
140 void CheckHasBackupData(sql::MetaTable* meta_table) {
142 EXPECT_TRUE(meta_table->GetValue(
143 "Default Search Provider ID Backup", &value));
144 EXPECT_TRUE(meta_table->GetValue(
145 "Default Search Provider ID Backup Signature", &value));
148 void CheckNoBackupData(const sql::Connection& connection,
149 sql::MetaTable* meta_table) {
151 EXPECT_FALSE(meta_table->GetValue(
152 "Default Search Provider ID Backup", &value));
153 EXPECT_FALSE(meta_table->GetValue(
154 "Default Search Provider ID Backup Signature", &value));
155 EXPECT_FALSE(connection.DoesTableExist("keywords_backup"));
158 } // anonymous namespace
160 // The WebDatabaseMigrationTest encapsulates testing of database migrations.
161 // Specifically, these tests are intended to exercise any schema changes in
162 // the WebDatabase and data migrations that occur in
163 // |WebDatabase::MigrateOldVersionsAsNeeded()|.
164 class WebDatabaseMigrationTest : public testing::Test {
166 WebDatabaseMigrationTest() {}
167 virtual ~WebDatabaseMigrationTest() {}
169 virtual void SetUp() {
170 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
173 // Load the database via the WebDatabase class and migrate the database to
174 // the current version.
176 // TODO(joi): This whole unit test file needs to stay in //chrome
177 // for now, as it needs to know about all the different table
178 // types. Once all webdata datatypes have been componentized, this
179 // could move to components_unittests.
180 AutofillTable autofill_table("en-US");
181 KeywordTable keyword_table;
182 LoginsTable logins_table;
183 TokenServiceTable token_service_table;
184 WebAppsTable web_apps_table;
185 WebIntentsTable web_intents_table;
188 db.AddTable(&autofill_table);
189 db.AddTable(&keyword_table);
190 db.AddTable(&logins_table);
191 db.AddTable(&token_service_table);
192 db.AddTable(&web_apps_table);
193 db.AddTable(&web_intents_table);
195 // This causes the migration to occur.
196 ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath()));
200 // Current tested version number. When adding a migration in
201 // |WebDatabase::MigrateOldVersionsAsNeeded()| and changing the version number
202 // |kCurrentVersionNumber| this value should change to reflect the new version
203 // number and a new migration test added below.
204 static const int kCurrentTestedVersionNumber;
206 base::FilePath GetDatabasePath() {
207 const base::FilePath::CharType kWebDatabaseFilename[] =
208 FILE_PATH_LITERAL("TestWebDatabase.sqlite3");
209 return temp_dir_.path().Append(base::FilePath(kWebDatabaseFilename));
212 // The textual contents of |file| are read from
213 // "components/test/data/web_database" and returned in the string |contents|.
214 // Returns true if the file exists and is read successfully, false otherwise.
215 bool GetWebDatabaseData(const base::FilePath& file, std::string* contents) {
216 base::FilePath source_path;
217 PathService::Get(base::DIR_SOURCE_ROOT, &source_path);
218 source_path = source_path.AppendASCII("components");
219 source_path = source_path.AppendASCII("test");
220 source_path = source_path.AppendASCII("data");
221 source_path = source_path.AppendASCII("web_database");
222 source_path = source_path.Append(file);
223 return base::PathExists(source_path) &&
224 base::ReadFileToString(source_path, contents);
227 static int VersionFromConnection(sql::Connection* connection) {
229 sql::Statement s(connection->GetUniqueStatement(
230 "SELECT value FROM meta WHERE key='version'"));
233 return s.ColumnInt(0);
236 // The sql files located in "chrome/test/data/web_database" were generated by
237 // launching the Chromium application prior to schema change, then using the
238 // sqlite3 command-line application to dump the contents of the "Web Data"
241 // > .output version_nn.sql
243 void LoadDatabase(const base::FilePath::StringType& file);
246 base::ScopedTempDir temp_dir_;
248 DISALLOW_COPY_AND_ASSIGN(WebDatabaseMigrationTest);
251 const int WebDatabaseMigrationTest::kCurrentTestedVersionNumber = 55;
253 void WebDatabaseMigrationTest::LoadDatabase(
254 const base::FilePath::StringType& file) {
255 std::string contents;
256 ASSERT_TRUE(GetWebDatabaseData(base::FilePath(file), &contents));
258 sql::Connection connection;
259 ASSERT_TRUE(connection.Open(GetDatabasePath()));
260 ASSERT_TRUE(connection.Execute(contents.data()));
263 // Tests that the all migrations from an empty database succeed.
264 TEST_F(WebDatabaseMigrationTest, MigrateEmptyToCurrent) {
267 // Verify post-conditions. These are expectations for current version of the
270 sql::Connection connection;
271 ASSERT_TRUE(connection.Open(GetDatabasePath()));
274 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
276 // Check that expected tables are present.
277 EXPECT_TRUE(connection.DoesTableExist("autofill"));
278 // The autofill_dates table is obsolete. (It's been merged into the autofill
280 EXPECT_FALSE(connection.DoesTableExist("autofill_dates"));
281 EXPECT_TRUE(connection.DoesTableExist("autofill_profiles"));
282 EXPECT_TRUE(connection.DoesTableExist("credit_cards"));
283 EXPECT_TRUE(connection.DoesTableExist("keywords"));
284 // The logins table is obsolete. (We used to store saved passwords here.)
285 EXPECT_FALSE(connection.DoesTableExist("logins"));
286 EXPECT_TRUE(connection.DoesTableExist("meta"));
287 EXPECT_TRUE(connection.DoesTableExist("token_service"));
288 EXPECT_TRUE(connection.DoesTableExist("web_app_icons"));
289 EXPECT_TRUE(connection.DoesTableExist("web_apps"));
290 EXPECT_TRUE(connection.DoesTableExist("web_intents"));
291 EXPECT_TRUE(connection.DoesTableExist("web_intents_defaults"));
295 // Tests that absent Autofill tables do not create any problems when migrating
296 // from a DB written by the earliest publicly released version of Chrome.
297 TEST_F(WebDatabaseMigrationTest, MigrateVersion20ToCurrent) {
298 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_20.sql")));
300 // Verify pre-conditions.
302 sql::Connection connection;
303 ASSERT_TRUE(connection.Open(GetDatabasePath()));
305 EXPECT_FALSE(connection.DoesTableExist("autofill"));
306 EXPECT_FALSE(connection.DoesTableExist("autofill_profiles"));
307 EXPECT_FALSE(connection.DoesTableExist("credit_cards"));
312 // Verify post-conditions. These are expectations for current version of the
315 sql::Connection connection;
316 ASSERT_TRUE(connection.Open(GetDatabasePath()));
319 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
321 // Mostly this test just verifies that no SQL errors occur during migration;
322 // but might as well verify that the tables were created as well.
323 EXPECT_TRUE(connection.DoesTableExist("autofill"));
324 EXPECT_TRUE(connection.DoesTableExist("autofill_profiles"));
325 EXPECT_TRUE(connection.DoesTableExist("credit_cards"));
329 // Tests that rows with empty values get removed from the autofill tables.
330 TEST_F(WebDatabaseMigrationTest, MigrateVersion21ToCurrent) {
331 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_21.sql")));
333 // Verify pre-conditions.
335 sql::Connection connection;
336 ASSERT_TRUE(connection.Open(GetDatabasePath()));
338 // Both empty and non-empty values are allowed in a version 21 database.
339 sql::Statement s_autofill(connection.GetUniqueStatement(
340 "SELECT name, value, value_lower, pair_id, count FROM autofill"));
341 sql::Statement s_dates(connection.GetUniqueStatement(
342 "SELECT pair_id, date_created FROM autofill_dates"));
344 // An entry with a non-empty value.
345 ASSERT_TRUE(s_autofill.Step());
346 EXPECT_EQ(ASCIIToUTF16("Name"), s_autofill.ColumnString16(0));
347 EXPECT_EQ(ASCIIToUTF16("John Doe"), s_autofill.ColumnString16(1));
348 EXPECT_EQ(ASCIIToUTF16("john doe"), s_autofill.ColumnString16(2));
349 EXPECT_EQ(10, s_autofill.ColumnInt(3));
350 EXPECT_EQ(1, s_autofill.ColumnInt(4));
351 ASSERT_TRUE(s_dates.Step());
352 EXPECT_EQ(10, s_dates.ColumnInt(0));
353 EXPECT_EQ(1384299100, s_dates.ColumnInt64(1));
355 // An entry with an empty value.
356 ASSERT_TRUE(s_autofill.Step());
357 EXPECT_EQ(ASCIIToUTF16("Name"), s_autofill.ColumnString16(0));
358 EXPECT_EQ(base::string16(), s_autofill.ColumnString16(1));
359 EXPECT_EQ(base::string16(), s_autofill.ColumnString16(2));
360 EXPECT_EQ(11, s_autofill.ColumnInt(3));
361 EXPECT_EQ(1, s_autofill.ColumnInt(4));
362 ASSERT_TRUE(s_dates.Step());
363 EXPECT_EQ(11, s_dates.ColumnInt(0));
364 EXPECT_EQ(1384299200, s_dates.ColumnInt64(1));
366 // Another entry with a non-empty value.
367 ASSERT_TRUE(s_autofill.Step());
368 EXPECT_EQ(ASCIIToUTF16("Email"), s_autofill.ColumnString16(0));
369 EXPECT_EQ(ASCIIToUTF16("jane@example.com"), s_autofill.ColumnString16(1));
370 EXPECT_EQ(ASCIIToUTF16("jane@example.com"), s_autofill.ColumnString16(2));
371 EXPECT_EQ(20, s_autofill.ColumnInt(3));
372 EXPECT_EQ(3, s_autofill.ColumnInt(4));
373 ASSERT_TRUE(s_dates.Step());
374 EXPECT_EQ(20, s_dates.ColumnInt(0));
375 EXPECT_EQ(1384299300, s_dates.ColumnInt64(1));
376 ASSERT_TRUE(s_dates.Step());
377 EXPECT_EQ(20, s_dates.ColumnInt(0));
378 EXPECT_EQ(1384299301, s_dates.ColumnInt64(1));
380 // Another entry with an empty value.
381 ASSERT_TRUE(s_autofill.Step());
382 EXPECT_EQ(ASCIIToUTF16("Email"), s_autofill.ColumnString16(0));
383 EXPECT_EQ(base::string16(), s_autofill.ColumnString16(1));
384 EXPECT_EQ(base::string16(), s_autofill.ColumnString16(2));
385 EXPECT_EQ(21, s_autofill.ColumnInt(3));
386 EXPECT_EQ(4, s_autofill.ColumnInt(4));
387 ASSERT_TRUE(s_dates.Step());
388 EXPECT_EQ(21, s_dates.ColumnInt(0));
389 EXPECT_EQ(1384299401, s_dates.ColumnInt64(1));
390 ASSERT_TRUE(s_dates.Step());
391 EXPECT_EQ(21, s_dates.ColumnInt(0));
392 EXPECT_EQ(1384299400, s_dates.ColumnInt64(1));
393 ASSERT_TRUE(s_dates.Step());
394 EXPECT_EQ(21, s_dates.ColumnInt(0));
395 EXPECT_EQ(1384299403, s_dates.ColumnInt64(1));
396 ASSERT_TRUE(s_dates.Step());
397 EXPECT_EQ(21, s_dates.ColumnInt(0));
398 EXPECT_EQ(1384299402, s_dates.ColumnInt64(1));
400 // No more entries expected.
401 ASSERT_FALSE(s_autofill.Step());
402 ASSERT_FALSE(s_dates.Step());
407 // Verify post-conditions. These are expectations for current version of the
410 sql::Connection connection;
411 ASSERT_TRUE(connection.Open(GetDatabasePath()));
414 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
416 // Entries with empty values should have been dropped. The remaining
417 // entries should have been preserved.
419 connection.GetUniqueStatement(
420 "SELECT name, value, value_lower, date_created, date_last_used,"
423 "ORDER BY name, value ASC"));
425 // "jane@example.com"
426 ASSERT_TRUE(s.Step());
427 EXPECT_EQ(ASCIIToUTF16("Email"), s.ColumnString16(0));
428 EXPECT_EQ(ASCIIToUTF16("jane@example.com"), s.ColumnString16(1));
429 EXPECT_EQ(ASCIIToUTF16("jane@example.com"), s.ColumnString16(2));
430 EXPECT_EQ(1384299300, s.ColumnInt64(3));
431 EXPECT_EQ(1384299301, s.ColumnInt64(4));
432 EXPECT_EQ(3, s.ColumnInt(5));
435 ASSERT_TRUE(s.Step());
436 EXPECT_EQ(ASCIIToUTF16("Name"), s.ColumnString16(0));
437 EXPECT_EQ(ASCIIToUTF16("John Doe"), s.ColumnString16(1));
438 EXPECT_EQ(ASCIIToUTF16("john doe"), s.ColumnString16(2));
439 EXPECT_EQ(1384299100, s.ColumnInt64(3));
440 EXPECT_EQ(1384299100, s.ColumnInt64(4));
441 EXPECT_EQ(1, s.ColumnInt(5));
443 // No more entries expected.
444 ASSERT_FALSE(s.Step());
448 // Tests that the |credit_card| table gets added to the schema for a version 22
450 TEST_F(WebDatabaseMigrationTest, MigrateVersion22ToCurrent) {
451 // This schema is taken from a build prior to the addition of the
452 // |credit_card| table. Version 22 of the schema. Contrast this with the
453 // corrupt version below.
454 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_22.sql")));
456 // Verify pre-conditions.
458 sql::Connection connection;
459 ASSERT_TRUE(connection.Open(GetDatabasePath()));
461 // No |credit_card| table prior to version 23.
462 ASSERT_FALSE(connection.DoesColumnExist("credit_cards", "guid"));
464 connection.DoesColumnExist("credit_cards", "card_number_encrypted"));
469 // Verify post-conditions. These are expectations for current version of the
472 sql::Connection connection;
473 ASSERT_TRUE(connection.Open(GetDatabasePath()));
476 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
478 // |credit_card| table now exists.
479 EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "guid"));
481 connection.DoesColumnExist("credit_cards", "card_number_encrypted"));
485 // Tests that the |credit_card| table gets added to the schema for a corrupt
486 // version 22 database. The corruption is that the |credit_cards| table exists
487 // but the schema version number was not set correctly to 23 or later. This
488 // test exercises code introduced to fix bug http://crbug.com/50699 that
489 // resulted from the corruption.
490 TEST_F(WebDatabaseMigrationTest, MigrateVersion22CorruptedToCurrent) {
491 // This schema is taken from a build after the addition of the |credit_card|
492 // table. Due to a bug in the migration logic the version is set incorrectly
493 // to 22 (it should have been updated to 23 at least).
494 ASSERT_NO_FATAL_FAILURE(
495 LoadDatabase(FILE_PATH_LITERAL("version_22_corrupt.sql")));
497 // Verify pre-conditions. These are expectations for corrupt version 22 of
500 sql::Connection connection;
501 ASSERT_TRUE(connection.Open(GetDatabasePath()));
503 // Columns existing and not existing before current version.
504 ASSERT_TRUE(connection.DoesColumnExist("credit_cards", "unique_id"));
506 connection.DoesColumnExist("credit_cards", "card_number_encrypted"));
507 ASSERT_TRUE(connection.DoesColumnExist("keywords", "id"));
512 // Verify post-conditions. These are expectations for current version of the
515 sql::Connection connection;
516 ASSERT_TRUE(connection.Open(GetDatabasePath()));
519 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
522 // Columns existing and not existing before version 25.
523 EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "unique_id"));
524 EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "guid"));
526 connection.DoesColumnExist("credit_cards", "card_number_encrypted"));
527 EXPECT_TRUE(connection.DoesColumnExist("keywords", "id"));
531 // Tests that the |keywords| |created_by_policy| column gets added to the schema
532 // for a version 25 database.
533 TEST_F(WebDatabaseMigrationTest, MigrateVersion25ToCurrent) {
534 // This schema is taken from a build prior to the addition of the |keywords|
535 // |created_by_policy| column.
536 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_25.sql")));
538 // Verify pre-conditions. These are expectations for version 25 of the
541 sql::Connection connection;
542 ASSERT_TRUE(connection.Open(GetDatabasePath()));
547 // Verify post-conditions. These are expectations for current version of the
550 sql::Connection connection;
551 ASSERT_TRUE(connection.Open(GetDatabasePath()));
554 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
556 // |keywords| |created_by_policy| column should have been added.
557 EXPECT_TRUE(connection.DoesColumnExist("keywords", "id"));
558 EXPECT_TRUE(connection.DoesColumnExist("keywords", "created_by_policy"));
562 // Tests that the credit_cards.billing_address column is changed from a string
563 // to an int whilst preserving the associated billing address. This version of
564 // the test makes sure a stored label is converted to an ID.
565 TEST_F(WebDatabaseMigrationTest, MigrateVersion26ToCurrentStringLabels) {
566 // This schema is taken from a build prior to the change of column type for
567 // credit_cards.billing_address from string to int.
568 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_26.sql")));
570 // Verify pre-conditions. These are expectations for version 26 of the
573 sql::Connection connection;
574 ASSERT_TRUE(connection.Open(GetDatabasePath()));
576 // Columns existing and not existing before current version.
577 EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "billing_address"));
579 std::string stmt = "INSERT INTO autofill_profiles"
580 "(label, unique_id, first_name, middle_name, last_name, email,"
581 " company_name, address_line_1, address_line_2, city, state, zipcode,"
582 " country, phone, fax)"
583 "VALUES ('Home',1,'','','','','','','','','','','','','')";
584 sql::Statement s(connection.GetUniqueStatement(stmt.c_str()));
585 ASSERT_TRUE(s.Run());
587 // Insert a CC linked to an existing address.
588 std::string stmt2 = "INSERT INTO credit_cards"
589 "(label, unique_id, name_on_card, type, card_number,"
590 " expiration_month, expiration_year, verification_code, billing_address,"
591 " shipping_address, card_number_encrypted, verification_code_encrypted)"
592 "VALUES ('label',2,'Jack','Visa','1234',2,2012,'','Home','','','')";
593 sql::Statement s2(connection.GetUniqueStatement(stmt2.c_str()));
594 ASSERT_TRUE(s2.Run());
596 // |billing_address| is a string.
597 std::string stmt3 = "SELECT billing_address FROM credit_cards";
598 sql::Statement s3(connection.GetUniqueStatement(stmt3.c_str()));
599 ASSERT_TRUE(s3.Step());
600 EXPECT_EQ(s3.ColumnType(0), sql::COLUMN_TYPE_TEXT);
605 // Verify post-conditions. These are expectations for current version of the
608 sql::Connection connection;
609 ASSERT_TRUE(connection.Open(GetDatabasePath()));
612 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
613 EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "billing_address"));
615 // Verify the credit card data is converted.
616 sql::Statement s(connection.GetUniqueStatement(
617 "SELECT guid, name_on_card, expiration_month, expiration_year, "
618 "card_number_encrypted, date_modified "
619 "FROM credit_cards"));
620 ASSERT_TRUE(s.Step());
621 EXPECT_EQ("Jack", s.ColumnString(1));
622 EXPECT_EQ(2, s.ColumnInt(2));
623 EXPECT_EQ(2012, s.ColumnInt(3));
624 // Column 5 is encrypted number blob.
625 // Column 6 is date_modified.
629 // Tests that the credit_cards.billing_address column is changed from a string
630 // to an int whilst preserving the associated billing address. This version of
631 // the test makes sure a stored string ID is converted to an integer ID.
632 TEST_F(WebDatabaseMigrationTest, MigrateVersion26ToCurrentStringIDs) {
633 // This schema is taken from a build prior to the change of column type for
634 // credit_cards.billing_address from string to int.
635 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_26.sql")));
637 // Verify pre-conditions. These are expectations for version 26 of the
640 sql::Connection connection;
641 ASSERT_TRUE(connection.Open(GetDatabasePath()));
642 EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "billing_address"));
644 std::string stmt = "INSERT INTO autofill_profiles"
645 "(label, unique_id, first_name, middle_name, last_name, email,"
646 " company_name, address_line_1, address_line_2, city, state, zipcode,"
647 " country, phone, fax)"
648 "VALUES ('Home',1,'','','','','','','','','','','','','')";
649 sql::Statement s(connection.GetUniqueStatement(stmt.c_str()));
650 ASSERT_TRUE(s.Run());
652 // Insert a CC linked to an existing address.
653 std::string stmt2 = "INSERT INTO credit_cards"
654 "(label, unique_id, name_on_card, type, card_number,"
655 " expiration_month, expiration_year, verification_code, billing_address,"
656 " shipping_address, card_number_encrypted, verification_code_encrypted)"
657 "VALUES ('label',2,'Jack','Visa','1234',2,2012,'','1','','','')";
658 sql::Statement s2(connection.GetUniqueStatement(stmt2.c_str()));
659 ASSERT_TRUE(s2.Run());
661 // |billing_address| is a string.
662 std::string stmt3 = "SELECT billing_address FROM credit_cards";
663 sql::Statement s3(connection.GetUniqueStatement(stmt3.c_str()));
664 ASSERT_TRUE(s3.Step());
665 EXPECT_EQ(s3.ColumnType(0), sql::COLUMN_TYPE_TEXT);
670 // Verify post-conditions. These are expectations for current version of the
673 sql::Connection connection;
674 ASSERT_TRUE(connection.Open(GetDatabasePath()));
677 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
679 // |keywords| |created_by_policy| column should have been added.
680 EXPECT_TRUE(connection.DoesColumnExist("keywords", "id"));
681 EXPECT_TRUE(connection.DoesColumnExist("keywords", "created_by_policy"));
682 EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "billing_address"));
684 // Verify the credit card data is converted.
685 sql::Statement s(connection.GetUniqueStatement(
686 "SELECT guid, name_on_card, expiration_month, expiration_year, "
687 "card_number_encrypted, date_modified "
688 "FROM credit_cards"));
689 ASSERT_TRUE(s.Step());
690 EXPECT_EQ("Jack", s.ColumnString(1));
691 EXPECT_EQ(2, s.ColumnInt(2));
692 EXPECT_EQ(2012, s.ColumnInt(3));
693 // Column 5 is encrypted credit card number blo b.
694 // Column 6 is date_modified.
698 // Makes sure instant_url is added correctly to keywords.
699 TEST_F(WebDatabaseMigrationTest, MigrateVersion27ToCurrent) {
700 // Initialize the database.
701 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_27.sql")));
703 // Verify pre-conditions. These are expectations for version 27 of the
706 sql::Connection connection;
707 ASSERT_TRUE(connection.Open(GetDatabasePath()));
709 ASSERT_FALSE(connection.DoesColumnExist("keywords", "instant_url"));
714 // Verify post-conditions. These are expectations for current version of the
717 sql::Connection connection;
718 ASSERT_TRUE(connection.Open(GetDatabasePath()));
721 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
723 // Make sure supports_instant (added in Version 28) was ultimately dropped
724 // again and instant_url was added.
725 EXPECT_FALSE(connection.DoesColumnExist("keywords", "supports_instant"));
726 EXPECT_TRUE(connection.DoesColumnExist("keywords", "instant_url"));
728 // Check that instant_url is empty.
729 std::string stmt = "SELECT instant_url FROM keywords";
730 sql::Statement s(connection.GetUniqueStatement(stmt.c_str()));
731 ASSERT_TRUE(s.Step());
732 EXPECT_EQ(std::string(), s.ColumnString(0));
734 // Verify the data made it over.
735 stmt = "SELECT " + KeywordTable::GetKeywordColumns() + " FROM keywords";
736 sql::Statement s2(connection.GetUniqueStatement(stmt.c_str()));
737 ASSERT_TRUE(s2.Step());
738 EXPECT_EQ(2, s2.ColumnInt(0));
739 EXPECT_EQ("Google", s2.ColumnString(1));
740 EXPECT_EQ("google.com", s2.ColumnString(2));
741 EXPECT_EQ("http://www.google.com/favicon.ico", s2.ColumnString(3));
742 EXPECT_EQ("{google:baseURL}search?{google:RLZ}{google:acceptedSuggestion}"\
743 "{google:originalQueryForSuggestion}sourceid=chrome&ie={inputEncoding}"\
746 EXPECT_TRUE(s2.ColumnBool(5));
747 EXPECT_EQ(std::string(), s2.ColumnString(6));
748 EXPECT_EQ(0, s2.ColumnInt(7));
749 EXPECT_EQ(0, s2.ColumnInt(8));
750 EXPECT_EQ(std::string("UTF-8"), s2.ColumnString(9));
751 EXPECT_TRUE(s2.ColumnBool(10));
752 EXPECT_EQ(std::string("{google:baseSuggestURL}search?client=chrome&hl="
753 "{language}&q={searchTerms}"), s2.ColumnString(11));
754 EXPECT_EQ(1, s2.ColumnInt(12));
755 EXPECT_FALSE(s2.ColumnBool(13));
756 EXPECT_EQ(std::string(), s2.ColumnString(14));
757 EXPECT_EQ(0, s2.ColumnInt(15));
758 EXPECT_EQ(std::string(), s2.ColumnString(16));
762 // Makes sure date_modified is added correctly to autofill_profiles and
764 TEST_F(WebDatabaseMigrationTest, MigrateVersion29ToCurrent) {
765 // Initialize the database.
766 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_29.sql")));
768 // Verify pre-conditions. These are expectations for version 29 of the
771 sql::Connection connection;
772 ASSERT_TRUE(connection.Open(GetDatabasePath()));
774 EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles",
776 EXPECT_FALSE(connection.DoesColumnExist("credit_cards",
780 Time pre_creation_time = Time::Now();
782 Time post_creation_time = Time::Now();
784 // Verify post-conditions. These are expectations for current version of the
787 sql::Connection connection;
788 ASSERT_TRUE(connection.Open(GetDatabasePath()));
791 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
793 // Check that the columns were created.
794 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
796 EXPECT_TRUE(connection.DoesColumnExist("credit_cards",
799 sql::Statement s_profiles(connection.GetUniqueStatement(
800 "SELECT date_modified FROM autofill_profiles "));
801 ASSERT_TRUE(s_profiles.is_valid());
802 while (s_profiles.Step()) {
803 EXPECT_GE(s_profiles.ColumnInt64(0),
804 pre_creation_time.ToTimeT());
805 EXPECT_LE(s_profiles.ColumnInt64(0),
806 post_creation_time.ToTimeT());
808 EXPECT_TRUE(s_profiles.Succeeded());
810 sql::Statement s_credit_cards(connection.GetUniqueStatement(
811 "SELECT date_modified FROM credit_cards "));
812 ASSERT_TRUE(s_credit_cards.is_valid());
813 while (s_credit_cards.Step()) {
814 EXPECT_GE(s_credit_cards.ColumnInt64(0),
815 pre_creation_time.ToTimeT());
816 EXPECT_LE(s_credit_cards.ColumnInt64(0),
817 post_creation_time.ToTimeT());
819 EXPECT_TRUE(s_credit_cards.Succeeded());
823 // Makes sure guids are added to autofill_profiles and credit_cards tables.
824 TEST_F(WebDatabaseMigrationTest, MigrateVersion30ToCurrent) {
825 // Initialize the database.
826 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_30.sql")));
828 // Verify pre-conditions. These are expectations for version 29 of the
831 sql::Connection connection;
832 ASSERT_TRUE(connection.Open(GetDatabasePath()));
834 EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "guid"));
835 EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "guid"));
840 // Verify post-conditions. These are expectations for current version of the
843 sql::Connection connection;
844 ASSERT_TRUE(connection.Open(GetDatabasePath()));
847 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
849 ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid"));
850 ASSERT_TRUE(connection.DoesColumnExist("credit_cards", "guid"));
852 // Check that guids are non-null, non-empty, conforms to guid format, and
855 connection.GetUniqueStatement("SELECT guid FROM autofill_profiles"));
857 ASSERT_TRUE(s.Step());
858 std::string guid1 = s.ColumnString(0);
859 EXPECT_TRUE(base::IsValidGUID(guid1));
861 ASSERT_TRUE(s.Step());
862 std::string guid2 = s.ColumnString(0);
863 EXPECT_TRUE(base::IsValidGUID(guid2));
865 EXPECT_NE(guid1, guid2);
869 // Removes unique IDs and make GUIDs the primary key. Also removes unused
871 TEST_F(WebDatabaseMigrationTest, MigrateVersion31ToCurrent) {
872 // Initialize the database.
873 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_31.sql")));
875 // Verify pre-conditions. These are expectations for version 30 of the
877 AutofillProfile profile;
878 base::string16 profile_label;
879 int profile_unique_id = 0;
880 int64 profile_date_modified = 0;
881 CreditCard credit_card;
882 base::string16 cc_label;
883 int cc_unique_id = 0;
884 std::string cc_number_encrypted;
885 int64 cc_date_modified = 0;
887 sql::Connection connection;
888 ASSERT_TRUE(connection.Open(GetDatabasePath()));
890 // Verify existence of columns we'll be changing.
891 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid"));
892 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "unique_id"));
893 EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "guid"));
894 EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "unique_id"));
895 EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "type"));
896 EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "card_number"));
897 EXPECT_TRUE(connection.DoesColumnExist("credit_cards",
898 "verification_code"));
899 EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "billing_address"));
900 EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "shipping_address"));
901 EXPECT_TRUE(connection.DoesColumnExist("credit_cards",
902 "verification_code_encrypted"));
904 // Fetch data in the database prior to migration.
906 connection.GetUniqueStatement(
907 "SELECT label, unique_id, first_name, middle_name, last_name, "
908 "email, company_name, address_line_1, address_line_2, city, state, "
909 "zipcode, country, phone, fax, date_modified, guid "
910 "FROM autofill_profiles"));
911 ASSERT_TRUE(s1.Step());
912 EXPECT_NO_FATAL_FAILURE(AutofillProfile31FromStatement(
913 s1, &profile, &profile_label, &profile_unique_id,
914 &profile_date_modified));
917 connection.GetUniqueStatement(
918 "SELECT label, unique_id, name_on_card, type, card_number, "
919 "expiration_month, expiration_year, verification_code, "
920 "billing_address, shipping_address, card_number_encrypted, "
921 "verification_code_encrypted, date_modified, guid "
922 "FROM credit_cards"));
923 ASSERT_TRUE(s2.Step());
924 EXPECT_NO_FATAL_FAILURE(CreditCard31FromStatement(s2,
928 &cc_number_encrypted,
931 EXPECT_NE(profile_unique_id, cc_unique_id);
932 EXPECT_NE(profile.guid(), credit_card.guid());
937 // Verify post-conditions. These are expectations for current version of the
940 sql::Connection connection;
941 ASSERT_TRUE(connection.Open(GetDatabasePath()));
944 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
946 // Verify existence of columns we'll be changing.
947 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid"));
948 EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "unique_id"));
949 EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "guid"));
950 EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "unique_id"));
951 EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "type"));
952 EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "card_number"));
953 EXPECT_FALSE(connection.DoesColumnExist("credit_cards",
954 "verification_code"));
955 EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "billing_address"));
956 EXPECT_FALSE(connection.DoesColumnExist("credit_cards",
957 "shipping_address"));
958 EXPECT_FALSE(connection.DoesColumnExist("credit_cards",
959 "verification_code_encrypted"));
961 // Verify data in the database after the migration.
963 connection.GetUniqueStatement(
964 "SELECT guid, company_name, street_address, city, state, zipcode,"
965 " country_code, date_modified "
966 "FROM autofill_profiles"));
967 ASSERT_TRUE(s1.Step());
969 AutofillProfile profile_a;
970 int64 profile_date_modified_a = 0;
971 EXPECT_NO_FATAL_FAILURE(AutofillProfile33FromStatement(
972 s1, &profile_a, &profile_date_modified_a));
973 EXPECT_EQ(profile.guid(), profile_a.guid());
974 EXPECT_EQ(profile.GetRawInfo(autofill::COMPANY_NAME),
975 profile_a.GetRawInfo(autofill::COMPANY_NAME));
976 EXPECT_EQ(profile.GetRawInfo(autofill::ADDRESS_HOME_LINE1),
977 profile_a.GetRawInfo(autofill::ADDRESS_HOME_LINE1));
978 EXPECT_EQ(profile.GetRawInfo(autofill::ADDRESS_HOME_LINE2),
979 profile_a.GetRawInfo(autofill::ADDRESS_HOME_LINE2));
980 EXPECT_EQ(profile.GetRawInfo(autofill::ADDRESS_HOME_CITY),
981 profile_a.GetRawInfo(autofill::ADDRESS_HOME_CITY));
982 EXPECT_EQ(profile.GetRawInfo(autofill::ADDRESS_HOME_STATE),
983 profile_a.GetRawInfo(autofill::ADDRESS_HOME_STATE));
984 EXPECT_EQ(profile.GetRawInfo(autofill::ADDRESS_HOME_ZIP),
985 profile_a.GetRawInfo(autofill::ADDRESS_HOME_ZIP));
986 EXPECT_EQ(profile.GetRawInfo(autofill::ADDRESS_HOME_COUNTRY),
987 profile_a.GetRawInfo(autofill::ADDRESS_HOME_COUNTRY));
988 EXPECT_EQ(profile_date_modified, profile_date_modified_a);
991 connection.GetUniqueStatement(
992 "SELECT guid, name_on_card, expiration_month, "
993 "expiration_year, card_number_encrypted, date_modified "
994 "FROM credit_cards"));
995 ASSERT_TRUE(s2.Step());
997 CreditCard credit_card_a;
998 base::string16 cc_label_a;
999 std::string cc_number_encrypted_a;
1000 int64 cc_date_modified_a = 0;
1001 EXPECT_NO_FATAL_FAILURE(CreditCard32FromStatement(s2,
1003 &cc_number_encrypted_a,
1004 &cc_date_modified_a));
1005 EXPECT_EQ(credit_card, credit_card_a);
1006 EXPECT_EQ(cc_label, cc_label_a);
1007 EXPECT_EQ(cc_number_encrypted, cc_number_encrypted_a);
1008 EXPECT_EQ(cc_date_modified, cc_date_modified_a);
1012 // Factor |autofill_profiles| address information separately from name, email,
1014 TEST_F(WebDatabaseMigrationTest, MigrateVersion32ToCurrent) {
1015 // Initialize the database.
1016 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_32.sql")));
1018 // Verify pre-conditions. These are expectations for version 32 of the
1021 sql::Connection connection;
1022 ASSERT_TRUE(connection.Open(GetDatabasePath()));
1024 // Verify existence of columns we'll be changing.
1025 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid"));
1026 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "label"));
1027 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "first_name"));
1028 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "middle_name"));
1029 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "last_name"));
1030 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "email"));
1031 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
1033 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
1035 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
1037 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "city"));
1038 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "state"));
1039 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "zipcode"));
1040 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "country"));
1041 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "phone"));
1042 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "fax"));
1043 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
1046 EXPECT_FALSE(connection.DoesTableExist("autofill_profile_names"));
1047 EXPECT_FALSE(connection.DoesTableExist("autofill_profile_emails"));
1048 EXPECT_FALSE(connection.DoesTableExist("autofill_profile_phones"));
1050 EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "label"));
1055 // Verify post-conditions. These are expectations for current version of the
1058 sql::Connection connection;
1059 ASSERT_TRUE(connection.Open(GetDatabasePath()));
1062 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
1064 // Verify changes to columns.
1065 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid"));
1066 EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "label"));
1067 EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "first_name"));
1068 EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles",
1070 EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "last_name"));
1071 EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "email"));
1072 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
1074 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
1076 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "city"));
1077 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "state"));
1078 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "zipcode"));
1079 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
1081 EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "phone"));
1082 EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "fax"));
1083 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
1086 // New "names" table.
1087 EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_names", "guid"));
1088 EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_names",
1090 EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_names",
1092 EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_names",
1095 // New "emails" table.
1096 EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_emails", "guid"));
1097 EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_emails", "email"));
1099 // New "phones" table.
1100 EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_phones", "guid"));
1101 EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_phones",
1104 EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "label"));
1106 // Verify data in the database after the migration.
1108 connection.GetUniqueStatement(
1109 "SELECT guid, company_name, street_address, city, state, zipcode, "
1110 " country_code, date_modified "
1111 "FROM autofill_profiles"));
1114 ASSERT_TRUE(s1.Step());
1115 EXPECT_EQ("00580526-FF81-EE2A-0546-1AC593A32E2F", s1.ColumnString(0));
1116 EXPECT_EQ(ASCIIToUTF16("Doe Enterprises"), s1.ColumnString16(1));
1117 EXPECT_EQ(ASCIIToUTF16("1 Main St\n"
1119 s1.ColumnString16(2));
1120 EXPECT_EQ(ASCIIToUTF16("Los Altos"), s1.ColumnString16(3));
1121 EXPECT_EQ(ASCIIToUTF16("CA"), s1.ColumnString16(4));
1122 EXPECT_EQ(ASCIIToUTF16("94022"), s1.ColumnString16(5));
1123 EXPECT_EQ(ASCIIToUTF16("US"), s1.ColumnString16(6));
1124 EXPECT_EQ(1297882100L, s1.ColumnInt64(7));
1127 // Gets merged during migration from 35 to 37 due to multi-valued fields.
1130 ASSERT_TRUE(s1.Step());
1131 EXPECT_EQ("4C74A9D8-7EEE-423E-F9C2-E7FA70ED1396", s1.ColumnString(0));
1132 EXPECT_EQ(base::string16(), s1.ColumnString16(1));
1133 EXPECT_EQ(ASCIIToUTF16("2 Main Street"), s1.ColumnString16(2));
1134 EXPECT_EQ(ASCIIToUTF16("Los Altos"), s1.ColumnString16(3));
1135 EXPECT_EQ(ASCIIToUTF16("CA"), s1.ColumnString16(4));
1136 EXPECT_EQ(ASCIIToUTF16("94022"), s1.ColumnString16(5));
1137 EXPECT_EQ(ASCIIToUTF16("US"), s1.ColumnString16(6));
1138 EXPECT_EQ(1297882100L, s1.ColumnInt64(7));
1140 // Dave Smith (Part 2).
1141 ASSERT_TRUE(s1.Step());
1142 EXPECT_EQ("722DF5C4-F74A-294A-46F0-31FFDED0D635", s1.ColumnString(0));
1143 EXPECT_EQ(base::string16(), s1.ColumnString16(1));
1144 EXPECT_EQ(ASCIIToUTF16("2 Main St"), s1.ColumnString16(2));
1145 EXPECT_EQ(ASCIIToUTF16("Los Altos"), s1.ColumnString16(3));
1146 EXPECT_EQ(ASCIIToUTF16("CA"), s1.ColumnString16(4));
1147 EXPECT_EQ(ASCIIToUTF16("94022"), s1.ColumnString16(5));
1148 EXPECT_EQ(ASCIIToUTF16("US"), s1.ColumnString16(6));
1149 EXPECT_EQ(1297882100L, s1.ColumnInt64(7));
1152 // Gets culled during migration from 35 to 36 due to incomplete address.
1155 ASSERT_TRUE(s1.Step());
1156 EXPECT_EQ("9E5FE298-62C7-83DF-6293-381BC589183F", s1.ColumnString(0));
1157 EXPECT_EQ(base::string16(), s1.ColumnString16(1));
1158 EXPECT_EQ(ASCIIToUTF16("3 Main St"), s1.ColumnString16(2));
1159 EXPECT_EQ(ASCIIToUTF16("Los Altos"), s1.ColumnString16(3));
1160 EXPECT_EQ(ASCIIToUTF16("CA"), s1.ColumnString16(4));
1161 EXPECT_EQ(ASCIIToUTF16("94022"), s1.ColumnString16(5));
1162 EXPECT_EQ(ASCIIToUTF16("US"), s1.ColumnString16(6));
1163 EXPECT_EQ(1297882100L, s1.ColumnInt64(7));
1165 // That should be all.
1166 EXPECT_FALSE(s1.Step());
1169 connection.GetUniqueStatement(
1170 "SELECT guid, first_name, middle_name, last_name "
1171 "FROM autofill_profile_names"));
1174 ASSERT_TRUE(s2.Step());
1175 EXPECT_EQ("00580526-FF81-EE2A-0546-1AC593A32E2F", s2.ColumnString(0));
1176 EXPECT_EQ(ASCIIToUTF16("John"), s2.ColumnString16(1));
1177 EXPECT_EQ(base::string16(), s2.ColumnString16(2));
1178 EXPECT_EQ(ASCIIToUTF16("Doe"), s2.ColumnString16(3));
1180 // John P. Doe. Note same guid as above due to merging of multi-valued
1182 ASSERT_TRUE(s2.Step());
1183 EXPECT_EQ("00580526-FF81-EE2A-0546-1AC593A32E2F", s2.ColumnString(0));
1184 EXPECT_EQ(ASCIIToUTF16("John"), s2.ColumnString16(1));
1185 EXPECT_EQ(ASCIIToUTF16("P."), s2.ColumnString16(2));
1186 EXPECT_EQ(ASCIIToUTF16("Doe"), s2.ColumnString16(3));
1189 ASSERT_TRUE(s2.Step());
1190 EXPECT_EQ("4C74A9D8-7EEE-423E-F9C2-E7FA70ED1396", s2.ColumnString(0));
1191 EXPECT_EQ(ASCIIToUTF16("Dave"), s2.ColumnString16(1));
1192 EXPECT_EQ(base::string16(), s2.ColumnString16(2));
1193 EXPECT_EQ(ASCIIToUTF16("Smith"), s2.ColumnString16(3));
1195 // Dave Smith (Part 2).
1196 ASSERT_TRUE(s2.Step());
1197 EXPECT_EQ("722DF5C4-F74A-294A-46F0-31FFDED0D635", s2.ColumnString(0));
1198 EXPECT_EQ(ASCIIToUTF16("Dave"), s2.ColumnString16(1));
1199 EXPECT_EQ(base::string16(), s2.ColumnString16(2));
1200 EXPECT_EQ(ASCIIToUTF16("Smith"), s2.ColumnString16(3));
1203 // Gets culled during migration from 35 to 36 due to incomplete address.
1206 ASSERT_TRUE(s2.Step());
1207 EXPECT_EQ("9E5FE298-62C7-83DF-6293-381BC589183F", s2.ColumnString(0));
1208 EXPECT_EQ(base::string16(), s2.ColumnString16(1));
1209 EXPECT_EQ(base::string16(), s2.ColumnString16(2));
1210 EXPECT_EQ(base::string16(), s2.ColumnString16(3));
1213 EXPECT_FALSE(s2.Step());
1216 connection.GetUniqueStatement(
1217 "SELECT guid, email "
1218 "FROM autofill_profile_emails"));
1221 ASSERT_TRUE(s3.Step());
1222 EXPECT_EQ("00580526-FF81-EE2A-0546-1AC593A32E2F", s3.ColumnString(0));
1223 EXPECT_EQ(ASCIIToUTF16("john@doe.com"), s3.ColumnString16(1));
1226 // Gets culled during migration from 35 to 37 due to merging of John Doe and
1227 // John P. Doe addresses.
1230 ASSERT_TRUE(s3.Step());
1231 EXPECT_EQ("4C74A9D8-7EEE-423E-F9C2-E7FA70ED1396", s3.ColumnString(0));
1232 EXPECT_EQ(base::string16(), s3.ColumnString16(1));
1235 ASSERT_TRUE(s3.Step());
1236 EXPECT_EQ("722DF5C4-F74A-294A-46F0-31FFDED0D635", s3.ColumnString(0));
1237 EXPECT_EQ(base::string16(), s3.ColumnString16(1));
1240 // Gets culled during migration from 35 to 36 due to incomplete address.
1243 ASSERT_TRUE(s3.Step());
1244 EXPECT_EQ("9E5FE298-62C7-83DF-6293-381BC589183F", s3.ColumnString(0));
1245 EXPECT_EQ(base::string16(), s3.ColumnString16(1));
1248 EXPECT_FALSE(s3.Step());
1251 connection.GetUniqueStatement(
1252 "SELECT guid, number "
1253 "FROM autofill_profile_phones"));
1256 ASSERT_TRUE(s4.Step());
1257 EXPECT_EQ("00580526-FF81-EE2A-0546-1AC593A32E2F", s4.ColumnString(0));
1258 EXPECT_EQ(ASCIIToUTF16("4151112222"), s4.ColumnString16(1));
1261 // Gets culled after fax type removed.
1263 // John P. Doe phone.
1264 // Gets culled during migration from 35 to 37 due to merging of John Doe and
1265 // John P. Doe addresses.
1268 // Gets culled during migration from 35 to 37 due to merging of John Doe and
1269 // John P. Doe addresses.
1271 // 2 Main Street phone.
1272 ASSERT_TRUE(s4.Step());
1273 EXPECT_EQ("4C74A9D8-7EEE-423E-F9C2-E7FA70ED1396", s4.ColumnString(0));
1274 EXPECT_EQ(base::string16(), s4.ColumnString16(1));
1276 // 2 Main Street fax.
1277 // Gets culled after fax type removed.
1280 ASSERT_TRUE(s4.Step());
1281 EXPECT_EQ("722DF5C4-F74A-294A-46F0-31FFDED0D635", s4.ColumnString(0));
1282 EXPECT_EQ(0, s4.ColumnInt(1)); // 0 means phone.
1283 EXPECT_EQ(base::string16(), s4.ColumnString16(2));
1286 // Gets culled after fax type removed.
1288 // Note no phone or fax for Alfred E Newman.
1291 ASSERT_TRUE(s4.Step());
1292 EXPECT_EQ("9E5FE298-62C7-83DF-6293-381BC589183F", s4.ColumnString(0));
1293 EXPECT_EQ(base::string16(), s4.ColumnString16(1));
1296 // Gets culled after fax type removed.
1299 EXPECT_FALSE(s4.Step());
1303 // Adds a column for the autofill profile's country code.
1304 TEST_F(WebDatabaseMigrationTest, MigrateVersion33ToCurrent) {
1305 // Initialize the database.
1306 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_33.sql")));
1308 // Verify pre-conditions. These are expectations for version 33 of the
1311 sql::Connection connection;
1312 ASSERT_TRUE(connection.Open(GetDatabasePath()));
1314 EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles",
1317 // Check that the country value is the one we expect.
1319 connection.GetUniqueStatement("SELECT country FROM autofill_profiles"));
1321 ASSERT_TRUE(s.Step());
1322 std::string country = s.ColumnString(0);
1323 EXPECT_EQ("United States", country);
1328 // Verify post-conditions. These are expectations for current version of the
1331 sql::Connection connection;
1332 ASSERT_TRUE(connection.Open(GetDatabasePath()));
1335 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
1337 ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles",
1340 // Check that the country code is properly converted.
1341 sql::Statement s(connection.GetUniqueStatement(
1342 "SELECT country_code FROM autofill_profiles"));
1344 ASSERT_TRUE(s.Step());
1345 std::string country_code = s.ColumnString(0);
1346 EXPECT_EQ("US", country_code);
1350 // Cleans up bad country code "UK" in favor of good country code "GB".
1351 TEST_F(WebDatabaseMigrationTest, MigrateVersion34ToCurrent) {
1352 // Initialize the database.
1353 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_34.sql")));
1355 // Verify pre-conditions. These are expectations for version 34 of the
1358 sql::Connection connection;
1359 ASSERT_TRUE(connection.Open(GetDatabasePath()));
1361 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles",
1364 // Check that the country_code value is the one we expect.
1366 connection.GetUniqueStatement("SELECT country_code "
1367 "FROM autofill_profiles"));
1369 ASSERT_TRUE(s.Step());
1370 std::string country_code = s.ColumnString(0);
1371 EXPECT_EQ("UK", country_code);
1373 // Should have only one.
1374 ASSERT_FALSE(s.Step());
1379 // Verify post-conditions. These are expectations for current version of the
1382 sql::Connection connection;
1383 ASSERT_TRUE(connection.Open(GetDatabasePath()));
1386 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
1388 ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles",
1391 // Check that the country_code code is properly converted.
1392 sql::Statement s(connection.GetUniqueStatement(
1393 "SELECT country_code FROM autofill_profiles"));
1395 ASSERT_TRUE(s.Step());
1396 std::string country_code = s.ColumnString(0);
1397 EXPECT_EQ("GB", country_code);
1399 // Should have only one.
1400 ASSERT_FALSE(s.Step());
1404 // Cleans up invalid profiles based on more agressive merging. Filters out
1405 // profiles that are subsets of other profiles, and profiles with invalid email,
1406 // state, and incomplete address.
1407 TEST_F(WebDatabaseMigrationTest, MigrateVersion35ToCurrent) {
1408 // Initialize the database.
1409 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_35.sql")));
1411 // Verify pre-conditions. These are expectations for version 34 of the
1414 sql::Connection connection;
1415 ASSERT_TRUE(connection.Open(GetDatabasePath()));
1417 EXPECT_FALSE(connection.DoesTableExist("autofill_profiles_trash"));
1418 ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid"));
1420 // Check that there are 6 profiles prior to merge.
1422 connection.GetUniqueStatement("SELECT guid FROM autofill_profiles"));
1431 // Verify post-conditions. These are expectations for current version of the
1434 sql::Connection connection;
1435 ASSERT_TRUE(connection.Open(GetDatabasePath()));
1438 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
1440 ASSERT_TRUE(connection.DoesTableExist("autofill_profiles_trash"));
1441 ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles_trash", "guid"));
1442 ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid"));
1444 // Verify data in the database after the migration.
1446 connection.GetUniqueStatement(
1447 "SELECT guid, company_name, street_address, city, state, zipcode,"
1448 " country_code, date_modified "
1449 "FROM autofill_profiles"));
1452 ASSERT_TRUE(s1.Step());
1453 EXPECT_EQ("00000000-0000-0000-0000-000000000001", s1.ColumnString(0));
1454 EXPECT_EQ(ASCIIToUTF16("Acme Inc."), s1.ColumnString16(1));
1455 EXPECT_EQ(ASCIIToUTF16("1 Main Street\n"
1457 s1.ColumnString16(2));
1458 EXPECT_EQ(ASCIIToUTF16("San Francisco"), s1.ColumnString16(3));
1459 EXPECT_EQ(ASCIIToUTF16("CA"), s1.ColumnString16(4));
1460 EXPECT_EQ(ASCIIToUTF16("94102"), s1.ColumnString16(5));
1461 EXPECT_EQ(ASCIIToUTF16("US"), s1.ColumnString16(6));
1462 EXPECT_EQ(1300131704, s1.ColumnInt64(7));
1464 // That should be it.
1465 ASSERT_FALSE(s1.Step());
1467 // Check that there 5 trashed profile after the merge.
1469 connection.GetUniqueStatement("SELECT guid "
1470 "FROM autofill_profiles_trash"));
1471 ASSERT_TRUE(s2.Step());
1472 EXPECT_EQ("00000000-0000-0000-0000-000000000002", s2.ColumnString(0));
1474 ASSERT_TRUE(s2.Step());
1475 EXPECT_EQ("00000000-0000-0000-0000-000000000003", s2.ColumnString(0));
1477 ASSERT_TRUE(s2.Step());
1478 EXPECT_EQ("00000000-0000-0000-0000-000000000004", s2.ColumnString(0));
1480 ASSERT_TRUE(s2.Step());
1481 EXPECT_EQ("00000000-0000-0000-0000-000000000005", s2.ColumnString(0));
1483 ASSERT_TRUE(s2.Step());
1484 EXPECT_EQ("00000000-0000-0000-0000-000000000006", s2.ColumnString(0));
1486 // That should be it.
1487 ASSERT_FALSE(s2.Step());
1491 // Tests that the |keywords| |last_modified| column gets added to the schema for
1492 // a version 37 database.
1493 TEST_F(WebDatabaseMigrationTest, MigrateVersion37ToCurrent) {
1494 // This schema is taken from a build prior to the addition of the |keywords|
1495 // |last_modified| column.
1496 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_37.sql")));
1498 // Verify pre-conditions. These are expectations for version 37 of the
1501 sql::Connection connection;
1502 ASSERT_TRUE(connection.Open(GetDatabasePath()));
1504 // Columns existing and not existing before current version.
1505 ASSERT_TRUE(connection.DoesColumnExist("keywords", "id"));
1506 ASSERT_FALSE(connection.DoesColumnExist("keywords", "last_modified"));
1511 // Verify post-conditions. These are expectations for current version of the
1514 sql::Connection connection;
1515 ASSERT_TRUE(connection.Open(GetDatabasePath()));
1518 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
1520 // |keywords| |last_modified| column should have been added.
1521 EXPECT_TRUE(connection.DoesColumnExist("keywords", "id"));
1522 EXPECT_TRUE(connection.DoesColumnExist("keywords", "last_modified"));
1526 // Tests that the |keywords| |sync_guid| column gets added to the schema for
1527 // a version 38 database.
1528 TEST_F(WebDatabaseMigrationTest, MigrateVersion38ToCurrent) {
1529 // This schema is taken from a build prior to the addition of the |keywords|
1530 // |sync_guid| column.
1531 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_38.sql")));
1533 // Verify pre-conditions. These are expectations for version 38 of the
1536 sql::Connection connection;
1537 ASSERT_TRUE(connection.Open(GetDatabasePath()));
1539 // Columns existing and not existing before current version.
1540 ASSERT_TRUE(connection.DoesColumnExist("keywords", "id"));
1541 ASSERT_FALSE(connection.DoesColumnExist("keywords", "sync_guid"));
1546 // Verify post-conditions. These are expectations for current version of the
1549 sql::Connection connection;
1550 ASSERT_TRUE(connection.Open(GetDatabasePath()));
1553 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
1555 // |keywords| |sync_guid| column should have been added.
1556 EXPECT_TRUE(connection.DoesColumnExist("keywords", "id"));
1557 EXPECT_TRUE(connection.DoesColumnExist("keywords", "sync_guid"));
1561 // Tests that no backup data is added to a version 39 database.
1562 TEST_F(WebDatabaseMigrationTest, MigrateVersion39ToCurrent) {
1563 // This schema is taken from a build prior to the addition of the default
1564 // search provider backup field to the meta table.
1565 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_39.sql")));
1567 // Verify pre-conditions. These are expectations for version 39 of the
1570 sql::Connection connection;
1571 ASSERT_TRUE(connection.Open(GetDatabasePath()));
1572 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1574 sql::MetaTable meta_table;
1575 ASSERT_TRUE(meta_table.Init(&connection, 39, 39));
1577 int64 default_search_provider_id = 0;
1578 EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
1579 &default_search_provider_id));
1581 EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
1586 // Verify post-conditions. These are expectations for current version of the
1589 sql::Connection connection;
1590 ASSERT_TRUE(connection.Open(GetDatabasePath()));
1591 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1594 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
1596 sql::MetaTable meta_table;
1597 ASSERT_TRUE(meta_table.Init(&connection, kCurrentTestedVersionNumber,
1598 kCurrentTestedVersionNumber));
1600 int64 default_search_provider_id = 0;
1601 EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
1602 &default_search_provider_id));
1603 EXPECT_NE(0, default_search_provider_id);
1605 EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
1609 // Tests that the backup data is removed from the database.
1610 TEST_F(WebDatabaseMigrationTest, MigrateVersion40ToCurrent) {
1611 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_40.sql")));
1613 // Verify pre-conditions. These are expectations for version 40 of the
1616 sql::Connection connection;
1617 ASSERT_TRUE(connection.Open(GetDatabasePath()));
1618 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1620 sql::MetaTable meta_table;
1621 ASSERT_TRUE(meta_table.Init(&connection, 40, 40));
1623 int64 default_search_provider_id = 0;
1624 EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
1625 &default_search_provider_id));
1627 EXPECT_NO_FATAL_FAILURE(CheckHasBackupData(&meta_table));
1632 // Verify post-conditions. These are expectations for current version of the
1635 sql::Connection connection;
1636 ASSERT_TRUE(connection.Open(GetDatabasePath()));
1637 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1640 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
1642 sql::MetaTable meta_table;
1643 ASSERT_TRUE(meta_table.Init(&connection, kCurrentTestedVersionNumber,
1644 kCurrentTestedVersionNumber));
1646 int64 default_search_provider_id = 0;
1647 EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
1648 &default_search_provider_id));
1649 EXPECT_NE(0, default_search_provider_id);
1651 EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
1655 // Tests that the backup data is removed from the database.
1656 TEST_F(WebDatabaseMigrationTest, MigrateVersion41ToCurrent) {
1657 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_41.sql")));
1659 // Verify pre-conditions. These are expectations for version 41 of the
1662 sql::Connection connection;
1663 ASSERT_TRUE(connection.Open(GetDatabasePath()));
1664 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1666 sql::MetaTable meta_table;
1667 ASSERT_TRUE(meta_table.Init(&connection, 41, 41));
1669 int64 default_search_provider_id = 0;
1670 EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
1671 &default_search_provider_id));
1673 EXPECT_NO_FATAL_FAILURE(CheckHasBackupData(&meta_table));
1678 // Verify post-conditions. These are expectations for current version of the
1681 sql::Connection connection;
1682 ASSERT_TRUE(connection.Open(GetDatabasePath()));
1683 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1686 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
1688 sql::MetaTable meta_table;
1689 ASSERT_TRUE(meta_table.Init(&connection, kCurrentTestedVersionNumber,
1690 kCurrentTestedVersionNumber));
1692 int64 default_search_provider_id = 0;
1693 EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
1694 &default_search_provider_id));
1695 EXPECT_NE(0, default_search_provider_id);
1697 EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
1701 // Tests that the backup data is removed from the database.
1702 TEST_F(WebDatabaseMigrationTest, MigrateVersion42ToCurrent) {
1703 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_42.sql")));
1705 // Verify pre-conditions. These are expectations for version 42 of the
1708 sql::Connection connection;
1709 ASSERT_TRUE(connection.Open(GetDatabasePath()));
1710 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1712 sql::MetaTable meta_table;
1713 ASSERT_TRUE(meta_table.Init(&connection, 42, 42));
1715 int64 default_search_provider_id = 0;
1716 EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
1717 &default_search_provider_id));
1719 EXPECT_NO_FATAL_FAILURE(CheckHasBackupData(&meta_table));
1721 EXPECT_FALSE(connection.DoesTableExist("keywords_backup"));
1726 // Verify post-conditions. These are expectations for current version of the
1729 sql::Connection connection;
1730 ASSERT_TRUE(connection.Open(GetDatabasePath()));
1731 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1734 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
1736 sql::MetaTable meta_table;
1737 ASSERT_TRUE(meta_table.Init(&connection, kCurrentTestedVersionNumber,
1738 kCurrentTestedVersionNumber));
1740 int64 default_search_provider_id = 0;
1741 EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
1742 &default_search_provider_id));
1743 EXPECT_NE(0, default_search_provider_id);
1745 EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
1749 // Tests that the backup data is removed from the database.
1750 TEST_F(WebDatabaseMigrationTest, MigrateVersion43ToCurrent) {
1751 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_43.sql")));
1753 int64 previous_default_search_provider_id;
1755 // Verify pre-conditions. These are expectations for version 43 of the
1758 sql::Connection connection;
1759 ASSERT_TRUE(connection.Open(GetDatabasePath()));
1760 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1762 sql::MetaTable meta_table;
1763 ASSERT_TRUE(meta_table.Init(&connection, 43, 43));
1765 int64 default_search_provider_id = 0;
1766 EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
1767 &default_search_provider_id));
1768 EXPECT_NE(default_search_provider_id, 0);
1769 previous_default_search_provider_id = default_search_provider_id;
1771 EXPECT_NO_FATAL_FAILURE(CheckHasBackupData(&meta_table));
1772 EXPECT_TRUE(connection.DoesTableExist("keywords_backup"));
1777 // Verify post-conditions. These are expectations for current version of the
1780 sql::Connection connection;
1781 ASSERT_TRUE(connection.Open(GetDatabasePath()));
1782 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1785 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
1787 sql::MetaTable meta_table;
1788 ASSERT_TRUE(meta_table.Init(
1790 kCurrentTestedVersionNumber,
1791 kCurrentTestedVersionNumber));
1793 int64 default_search_provider_id = 0;
1794 EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
1795 &default_search_provider_id));
1796 // Default search provider ID should not change.
1797 EXPECT_EQ(previous_default_search_provider_id, default_search_provider_id);
1799 EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
1803 // Tests that the |autogenerate_keyword| and |logo_id| columns get removed from
1804 // the keyword table schema for a version 45 database.
1805 TEST_F(WebDatabaseMigrationTest, MigrateVersion44ToCurrent) {
1806 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_44.sql")));
1808 // Verify pre-conditions. These are expectations for version 44 of the
1811 sql::Connection connection;
1812 ASSERT_TRUE(connection.Open(GetDatabasePath()));
1813 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1815 sql::MetaTable meta_table;
1816 ASSERT_TRUE(meta_table.Init(&connection, 44, 44));
1818 ASSERT_TRUE(connection.DoesColumnExist("keywords", "autogenerate_keyword"));
1819 ASSERT_TRUE(connection.DoesColumnExist("keywords", "logo_id"));
1824 // Verify post-conditions. These are expectations for current version of the
1827 sql::Connection connection;
1828 ASSERT_TRUE(connection.Open(GetDatabasePath()));
1829 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1832 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
1834 sql::MetaTable meta_table;
1835 ASSERT_TRUE(meta_table.Init(&connection, kCurrentTestedVersionNumber,
1836 kCurrentTestedVersionNumber));
1838 // We should have removed this obsolete key.
1839 std::string default_search_provider_backup;
1840 EXPECT_FALSE(meta_table.GetValue("Default Search Provider Backup",
1841 &default_search_provider_backup));
1843 // Two columns should have been removed.
1844 EXPECT_FALSE(connection.DoesColumnExist("keywords",
1845 "autogenerate_keyword"));
1846 EXPECT_FALSE(connection.DoesColumnExist("keywords", "logo_id"));
1848 // Backup data should have been removed.
1849 EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
1853 // Tests that the web_intents and web_intents_defaults tables are
1854 // modified to include "scheme" columns.
1855 TEST_F(WebDatabaseMigrationTest, MigrateVersion45ToCurrent) {
1856 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_45.sql")));
1858 // Verify pre-conditions. These are expectations for version 45 of the
1861 sql::Connection connection;
1862 ASSERT_TRUE(connection.Open(GetDatabasePath()));
1863 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1865 sql::MetaTable meta_table;
1866 ASSERT_TRUE(meta_table.Init(&connection, 45, 45));
1868 ASSERT_FALSE(connection.DoesColumnExist("scheme", "web_intents"));
1869 ASSERT_FALSE(connection.DoesColumnExist(
1870 "scheme", "web_intents_defaults"));
1875 // Verify post-conditions. These are expectations for current version of the
1878 sql::Connection connection;
1879 ASSERT_TRUE(connection.Open(GetDatabasePath()));
1880 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1883 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
1885 sql::MetaTable meta_table;
1886 ASSERT_TRUE(meta_table.Init(
1888 kCurrentTestedVersionNumber,
1889 kCurrentTestedVersionNumber));
1891 // A new "scheme" column should have been added to each web_intents table.
1892 EXPECT_TRUE(connection.DoesColumnExist("web_intents", "scheme"));
1893 EXPECT_TRUE(connection.DoesColumnExist("web_intents_defaults", "scheme"));
1895 // Verify existing user data was copied.
1897 connection.GetUniqueStatement("SELECT * FROM web_intents"));
1899 ASSERT_TRUE(s1.Step());
1900 EXPECT_EQ("http://poodles.com/fuzzer", s1.ColumnString(0));
1901 EXPECT_EQ(ASCIIToUTF16("fuzz"), s1.ColumnString16(1));
1902 EXPECT_EQ(ASCIIToUTF16("poodle/*"), s1.ColumnString16(2));
1903 EXPECT_EQ(ASCIIToUTF16("Poodle Fuzzer"), s1.ColumnString16(3));
1904 EXPECT_EQ(ASCIIToUTF16("window"), s1.ColumnString16(4));
1905 EXPECT_EQ(ASCIIToUTF16(""), s1.ColumnString16(5));
1906 ASSERT_FALSE(s1.Step());
1908 // Now we want to verify existing user data was copied
1910 connection.GetUniqueStatement("SELECT * FROM web_intents_defaults"));
1912 ASSERT_TRUE(s2.Step());
1913 EXPECT_EQ("fuzz", s2.ColumnString(0));
1914 EXPECT_EQ(ASCIIToUTF16("poodle/*"), s2.ColumnString16(1));
1915 EXPECT_EQ(ASCIIToUTF16(""), s2.ColumnString16(2));
1916 EXPECT_EQ(0, s2.ColumnInt(3));
1917 EXPECT_EQ(0, s2.ColumnInt(4));
1918 EXPECT_EQ(ASCIIToUTF16("http://poodles.com/fuzzer"), s2.ColumnString16(5));
1919 EXPECT_EQ(ASCIIToUTF16(""), s2.ColumnString16(6));
1920 ASSERT_FALSE(s2.Step());
1922 // finally ensure the migration code cleaned up after itself
1923 EXPECT_FALSE(connection.DoesTableExist("old_web_intents"));
1924 EXPECT_FALSE(connection.DoesTableExist("old_web_intents_defaults"));
1928 // Tests that the web_intents and web_intents_defaults tables are
1929 // modified to include "scheme" columns.
1930 TEST_F(WebDatabaseMigrationTest, MigrateVersion45InvalidToCurrent) {
1931 ASSERT_NO_FATAL_FAILURE(
1932 LoadDatabase(FILE_PATH_LITERAL("version_45_invalid.sql")));
1934 // Verify pre-conditions. These are expectations for version 45 of the
1937 sql::Connection connection;
1938 ASSERT_TRUE(connection.Open(GetDatabasePath()));
1939 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1941 sql::MetaTable meta_table;
1942 ASSERT_TRUE(meta_table.Init(&connection, 45, 45));
1944 ASSERT_FALSE(connection.DoesColumnExist("scheme", "web_intents"));
1945 ASSERT_FALSE(connection.DoesColumnExist(
1946 "scheme", "web_intents_defaults"));
1951 // Verify post-conditions. These are expectations for current version of the
1954 sql::Connection connection;
1955 ASSERT_TRUE(connection.Open(GetDatabasePath()));
1956 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
1959 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
1961 sql::MetaTable meta_table;
1962 ASSERT_TRUE(meta_table.Init(
1964 kCurrentTestedVersionNumber,
1965 kCurrentTestedVersionNumber));
1967 // A new "scheme" column should have been added to each web_intents table.
1968 EXPECT_TRUE(connection.DoesColumnExist("web_intents", "scheme"));
1969 EXPECT_TRUE(connection.DoesColumnExist("web_intents_defaults", "scheme"));
1971 // Verify existing user data was copied.
1973 connection.GetUniqueStatement("SELECT * FROM web_intents"));
1975 ASSERT_FALSE(s1.Step()); // Basically should be empty at this point.
1977 // Now we want to verify existing user data was copied
1979 connection.GetUniqueStatement("SELECT * FROM web_intents_defaults"));
1981 // We were able to create the new tables, but unable to copy any data
1982 // Given the initial bad state of the tables.
1983 ASSERT_FALSE(s2.Step());
1985 // Finally ensure the migration code cleaned up after itself.
1986 EXPECT_FALSE(connection.DoesTableExist("old_web_intents"));
1987 EXPECT_FALSE(connection.DoesTableExist("old_web_intents_defaults"));
1991 // Check that current version is forced to compatible version before migration,
1992 // if the former is smaller.
1993 TEST_F(WebDatabaseMigrationTest, MigrateVersion45CompatibleToCurrent) {
1994 ASSERT_NO_FATAL_FAILURE(
1995 LoadDatabase(FILE_PATH_LITERAL("version_45_compatible.sql")));
1997 // Verify pre-conditions. These are expectations for version 45 of the
2000 sql::Connection connection;
2001 ASSERT_TRUE(connection.Open(GetDatabasePath()));
2002 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
2004 sql::MetaTable meta_table;
2005 // Database is actually version 45 but the version field states 40.
2006 ASSERT_TRUE(meta_table.Init(&connection, 40, 45));
2011 // Verify post-conditions. These are expectations for current version of the
2014 sql::Connection connection;
2015 ASSERT_TRUE(connection.Open(GetDatabasePath()));
2016 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
2019 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
2020 EXPECT_LE(45, VersionFromConnection(&connection));
2024 // Tests that the |alternate_urls| column is added to the keyword table schema
2025 // for a version 47 database.
2026 TEST_F(WebDatabaseMigrationTest, MigrateVersion46ToCurrent) {
2027 ASSERT_NO_FATAL_FAILURE(
2028 LoadDatabase(FILE_PATH_LITERAL("version_46.sql")));
2030 // Verify pre-conditions. These are expectations for version 46 of the
2033 sql::Connection connection;
2034 ASSERT_TRUE(connection.Open(GetDatabasePath()));
2035 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
2037 sql::MetaTable meta_table;
2038 ASSERT_TRUE(meta_table.Init(&connection, 46, 46));
2040 ASSERT_FALSE(connection.DoesColumnExist("keywords", "alternate_urls"));
2041 ASSERT_FALSE(connection.DoesColumnExist("keywords_backup",
2047 // Verify post-conditions. These are expectations for current version of the
2050 sql::Connection connection;
2051 ASSERT_TRUE(connection.Open(GetDatabasePath()));
2052 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
2055 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
2057 // A new column should have been created.
2058 EXPECT_TRUE(connection.DoesColumnExist("keywords", "alternate_urls"));
2062 // Tests that the backup data is removed from the database.
2063 TEST_F(WebDatabaseMigrationTest, MigrateVersion47ToCurrent) {
2064 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_47.sql")));
2066 // Verify pre-conditions. These are expectations for version 47 of the
2069 sql::Connection connection;
2070 ASSERT_TRUE(connection.Open(GetDatabasePath()));
2071 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
2073 sql::MetaTable meta_table;
2074 ASSERT_TRUE(meta_table.Init(&connection, 47, 47));
2076 int64 default_search_provider_id = 0;
2077 EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
2078 &default_search_provider_id));
2079 EXPECT_NE(0, default_search_provider_id);
2081 EXPECT_NO_FATAL_FAILURE(CheckHasBackupData(&meta_table));
2082 EXPECT_TRUE(connection.DoesTableExist("keywords_backup"));
2087 // Verify post-conditions. These are expectations for current version of the
2090 sql::Connection connection;
2091 ASSERT_TRUE(connection.Open(GetDatabasePath()));
2092 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
2095 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
2097 sql::MetaTable meta_table;
2098 ASSERT_TRUE(meta_table.Init(&connection, kCurrentTestedVersionNumber,
2099 kCurrentTestedVersionNumber));
2101 int64 default_search_provider_id = 0;
2102 EXPECT_TRUE(meta_table.GetValue(KeywordTable::kDefaultSearchProviderKey,
2103 &default_search_provider_id));
2104 EXPECT_NE(0, default_search_provider_id);
2106 EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table));
2110 // Tests that the |search_terms_replacement_key| column is added to the keyword
2111 // table schema for a version 49 database.
2112 TEST_F(WebDatabaseMigrationTest, MigrateVersion48ToCurrent) {
2113 ASSERT_NO_FATAL_FAILURE(
2114 LoadDatabase(FILE_PATH_LITERAL("version_48.sql")));
2116 // Verify pre-conditions. These are expectations for version 48 of the
2119 sql::Connection connection;
2120 ASSERT_TRUE(connection.Open(GetDatabasePath()));
2121 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
2123 sql::MetaTable meta_table;
2124 ASSERT_TRUE(meta_table.Init(&connection, 48, 48));
2126 ASSERT_FALSE(connection.DoesColumnExist("keywords",
2127 "search_terms_replacement_key"));
2132 // Verify post-conditions. These are expectations for current version of the
2135 sql::Connection connection;
2136 ASSERT_TRUE(connection.Open(GetDatabasePath()));
2137 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
2140 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
2142 // A new column should have been created.
2143 EXPECT_TRUE(connection.DoesColumnExist("keywords",
2144 "search_terms_replacement_key"));
2148 // Tests that the |origin| column is added to the autofill_profiles and
2149 // credit_cards table schemas for a version 50 database.
2150 TEST_F(WebDatabaseMigrationTest, MigrateVersion49ToCurrent) {
2151 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_49.sql")));
2153 // Verify pre-conditions. These are expectations for version 49 of the
2156 sql::Connection connection;
2157 ASSERT_TRUE(connection.Open(GetDatabasePath()));
2159 ASSERT_FALSE(connection.DoesColumnExist("autofill_profiles", "origin"));
2160 ASSERT_FALSE(connection.DoesColumnExist("credit_cards", "origin"));
2165 // Verify post-conditions. These are expectations for current version of the
2168 sql::Connection connection;
2169 ASSERT_TRUE(connection.Open(GetDatabasePath()));
2170 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
2173 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
2175 // A new column should have been created in both tables.
2176 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "origin"));
2177 EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "origin"));
2181 // Tests that the columns |image_url|, |search_url_post_params|,
2182 // |suggest_url_post_params|, |instant_url_post_params|, and
2183 // |image_url_post_params| are added to the keyword table schema for a version
2185 TEST_F(WebDatabaseMigrationTest, MigrateVersion50ToCurrent) {
2186 ASSERT_NO_FATAL_FAILURE(
2187 LoadDatabase(FILE_PATH_LITERAL("version_50.sql")));
2189 // Verify pre-conditions. These are expectations for version 50 of the
2192 sql::Connection connection;
2193 ASSERT_TRUE(connection.Open(GetDatabasePath()));
2194 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
2196 sql::MetaTable meta_table;
2197 ASSERT_TRUE(meta_table.Init(&connection, 50, 50));
2199 ASSERT_FALSE(connection.DoesColumnExist("keywords", "image_url"));
2200 ASSERT_FALSE(connection.DoesColumnExist("keywords",
2201 "search_url_post_params"));
2202 ASSERT_FALSE(connection.DoesColumnExist("keywords",
2203 "suggest_url_post_params"));
2204 ASSERT_FALSE(connection.DoesColumnExist("keywords",
2205 "instant_url_post_params"));
2206 ASSERT_FALSE(connection.DoesColumnExist("keywords",
2207 "image_url_post_params"));
2212 // Verify post-conditions. These are expectations for current version of the
2215 sql::Connection connection;
2216 ASSERT_TRUE(connection.Open(GetDatabasePath()));
2217 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
2220 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
2222 // New columns should have been created.
2223 EXPECT_TRUE(connection.DoesColumnExist("keywords", "image_url"));
2224 EXPECT_TRUE(connection.DoesColumnExist("keywords",
2225 "search_url_post_params"));
2226 EXPECT_TRUE(connection.DoesColumnExist("keywords",
2227 "suggest_url_post_params"));
2228 EXPECT_TRUE(connection.DoesColumnExist("keywords",
2229 "instant_url_post_params"));
2230 EXPECT_TRUE(connection.DoesColumnExist("keywords",
2231 "image_url_post_params"));
2235 // Tests that the column |new_tab_url| is added to the keyword table schema for
2236 // a version 52 database.
2237 TEST_F(WebDatabaseMigrationTest, MigrateVersion52ToCurrent) {
2238 ASSERT_NO_FATAL_FAILURE(
2239 LoadDatabase(FILE_PATH_LITERAL("version_52.sql")));
2241 // Verify pre-conditions. These are expectations for version 52 of the
2244 sql::Connection connection;
2245 ASSERT_TRUE(connection.Open(GetDatabasePath()));
2246 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
2248 sql::MetaTable meta_table;
2249 ASSERT_TRUE(meta_table.Init(&connection, 52, 52));
2251 ASSERT_FALSE(connection.DoesColumnExist("keywords", "new_tab_url"));
2256 // Verify post-conditions. These are expectations for current version of the
2259 sql::Connection connection;
2260 ASSERT_TRUE(connection.Open(GetDatabasePath()));
2261 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
2264 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
2266 // New columns should have been created.
2267 EXPECT_TRUE(connection.DoesColumnExist("keywords", "new_tab_url"));
2271 // Tests that for a version 54 database,
2272 // (a) The street_address, dependent_locality, and sorting_code columns are
2273 // added to the autofill_profiles table schema.
2274 // (b) The address_line1, address_line2, and country columns are dropped from
2275 // the autofill_profiles table schema.
2276 // (c) The type column is dropped from the autofill_profile_phones schema.
2277 TEST_F(WebDatabaseMigrationTest, MigrateVersion53ToCurrent) {
2278 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_53.sql")));
2280 // Verify pre-conditions. These are expectations for version 53 of the
2283 sql::Connection connection;
2284 ASSERT_TRUE(connection.Open(GetDatabasePath()));
2287 connection.DoesColumnExist("autofill_profiles", "address_line_1"));
2289 connection.DoesColumnExist("autofill_profiles", "address_line_2"));
2290 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "country"));
2292 connection.DoesColumnExist("autofill_profiles", "street_address"));
2294 connection.DoesColumnExist("autofill_profiles", "dependent_locality"));
2296 connection.DoesColumnExist("autofill_profiles", "sorting_code"));
2297 EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_phones", "type"));
2302 // Verify post-conditions. These are expectations for current version of the
2305 sql::Connection connection;
2306 ASSERT_TRUE(connection.Open(GetDatabasePath()));
2307 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
2310 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
2312 // Columns should have been added and removed appropriately.
2314 connection.DoesColumnExist("autofill_profiles", "address_line1"));
2316 connection.DoesColumnExist("autofill_profiles", "address_line2"));
2317 EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "country"));
2319 connection.DoesColumnExist("autofill_profiles", "street_address"));
2321 connection.DoesColumnExist("autofill_profiles", "dependent_locality"));
2323 connection.DoesColumnExist("autofill_profiles", "sorting_code"));
2324 EXPECT_FALSE(connection.DoesColumnExist("autofill_profile_phones", "type"));
2326 // Data should have been preserved.
2327 sql::Statement s_profiles(
2328 connection.GetUniqueStatement(
2329 "SELECT guid, company_name, street_address, dependent_locality,"
2330 " city, state, zipcode, sorting_code, country_code, date_modified,"
2332 "FROM autofill_profiles"));
2334 // Address lines 1 and 2.
2335 ASSERT_TRUE(s_profiles.Step());
2336 EXPECT_EQ("00000000-0000-0000-0000-000000000001",
2337 s_profiles.ColumnString(0));
2338 EXPECT_EQ(ASCIIToUTF16("Google, Inc."), s_profiles.ColumnString16(1));
2339 EXPECT_EQ(ASCIIToUTF16("1950 Charleston Rd.\n"
2341 s_profiles.ColumnString16(2));
2342 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(3));
2343 EXPECT_EQ(ASCIIToUTF16("Mountain View"), s_profiles.ColumnString16(4));
2344 EXPECT_EQ(ASCIIToUTF16("CA"), s_profiles.ColumnString16(5));
2345 EXPECT_EQ(ASCIIToUTF16("94043"), s_profiles.ColumnString16(6));
2346 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(7));
2347 EXPECT_EQ(ASCIIToUTF16("US"), s_profiles.ColumnString16(8));
2348 EXPECT_EQ(1386046731, s_profiles.ColumnInt(9));
2349 EXPECT_EQ(ASCIIToUTF16("Chrome settings"), s_profiles.ColumnString16(10));
2351 // Only address line 1.
2352 ASSERT_TRUE(s_profiles.Step());
2353 EXPECT_EQ("00000000-0000-0000-0000-000000000002",
2354 s_profiles.ColumnString(0));
2355 EXPECT_EQ(ASCIIToUTF16("Google!"), s_profiles.ColumnString16(1));
2356 EXPECT_EQ(ASCIIToUTF16("1600 Amphitheatre Pkwy."),
2357 s_profiles.ColumnString16(2));
2358 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(3));
2359 EXPECT_EQ(ASCIIToUTF16("Mtn. View"), s_profiles.ColumnString16(4));
2360 EXPECT_EQ(ASCIIToUTF16("California"), s_profiles.ColumnString16(5));
2361 EXPECT_EQ(ASCIIToUTF16("94043-1234"), s_profiles.ColumnString16(6));
2362 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(7));
2363 EXPECT_EQ(ASCIIToUTF16("US"), s_profiles.ColumnString16(8));
2364 EXPECT_EQ(1386046800, s_profiles.ColumnInt(9));
2365 EXPECT_EQ(ASCIIToUTF16("Chrome settings"), s_profiles.ColumnString16(10));
2367 // Only address line 2.
2368 ASSERT_TRUE(s_profiles.Step());
2369 EXPECT_EQ("00000000-0000-0000-0000-000000000003",
2370 s_profiles.ColumnString(0));
2371 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(1));
2372 EXPECT_EQ(ASCIIToUTF16("\nOnly line 2???"), s_profiles.ColumnString16(2));
2373 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(3));
2374 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(4));
2375 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(5));
2376 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(6));
2377 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(7));
2378 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(8));
2379 EXPECT_EQ(1386046834, s_profiles.ColumnInt(9));
2380 EXPECT_EQ(ASCIIToUTF16("Chrome settings"), s_profiles.ColumnString16(10));
2382 // No address lines.
2383 ASSERT_TRUE(s_profiles.Step());
2384 EXPECT_EQ("00000000-0000-0000-0000-000000000004",
2385 s_profiles.ColumnString(0));
2386 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(1));
2387 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(2));
2388 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(3));
2389 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(4));
2390 EXPECT_EQ(ASCIIToUTF16("Texas"), s_profiles.ColumnString16(5));
2391 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(6));
2392 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(7));
2393 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(8));
2394 EXPECT_EQ(1386046847, s_profiles.ColumnInt(9));
2395 EXPECT_EQ(ASCIIToUTF16("Chrome settings"), s_profiles.ColumnString16(10));
2397 // That should be it.
2398 EXPECT_FALSE(s_profiles.Step());
2400 // Verify the phone number data as well.
2401 sql::Statement s_phones(
2402 connection.GetUniqueStatement(
2403 "SELECT guid, number FROM autofill_profile_phones"));
2405 ASSERT_TRUE(s_phones.Step());
2406 EXPECT_EQ("00000000-0000-0000-0000-000000000001", s_phones.ColumnString(0));
2407 EXPECT_EQ(ASCIIToUTF16("1.800.555.1234"), s_phones.ColumnString16(1));
2409 ASSERT_TRUE(s_phones.Step());
2410 EXPECT_EQ("00000000-0000-0000-0000-000000000001", s_phones.ColumnString(0));
2411 EXPECT_EQ(ASCIIToUTF16("+1 (800) 555-4321"), s_phones.ColumnString16(1));
2413 ASSERT_TRUE(s_phones.Step());
2414 EXPECT_EQ("00000000-0000-0000-0000-000000000002", s_phones.ColumnString(0));
2415 EXPECT_EQ(base::string16(), s_phones.ColumnString16(1));
2417 ASSERT_TRUE(s_phones.Step());
2418 EXPECT_EQ("00000000-0000-0000-0000-000000000003", s_phones.ColumnString(0));
2419 EXPECT_EQ(ASCIIToUTF16("6505557890"), s_phones.ColumnString16(1));
2421 ASSERT_TRUE(s_phones.Step());
2422 EXPECT_EQ("00000000-0000-0000-0000-000000000004", s_phones.ColumnString(0));
2423 EXPECT_EQ(base::string16(), s_phones.ColumnString16(1));
2425 EXPECT_FALSE(s_phones.Step());
2429 // Tests that migrating from version 54 to version 55 drops the autofill_dates
2430 // table, and merges the appropriate dates into the autofill table.
2431 TEST_F(WebDatabaseMigrationTest, MigrateVersion54ToCurrent) {
2432 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_54.sql")));
2434 // Verify pre-conditions. These are expectations for version 54 of the
2437 sql::Connection connection;
2438 ASSERT_TRUE(connection.Open(GetDatabasePath()));
2440 EXPECT_TRUE(connection.DoesTableExist("autofill_dates"));
2441 EXPECT_FALSE(connection.DoesColumnExist("autofill", "date_created"));
2442 EXPECT_FALSE(connection.DoesColumnExist("autofill", "date_last_used"));
2444 // Verify the incoming data.
2445 sql::Statement s_autofill(connection.GetUniqueStatement(
2446 "SELECT name, value, value_lower, pair_id, count FROM autofill"));
2447 sql::Statement s_dates(connection.GetUniqueStatement(
2448 "SELECT pair_id, date_created FROM autofill_dates"));
2450 // An entry with one timestamp.
2451 ASSERT_TRUE(s_autofill.Step());
2452 EXPECT_EQ(ASCIIToUTF16("Name"), s_autofill.ColumnString16(0));
2453 EXPECT_EQ(ASCIIToUTF16("John Doe"), s_autofill.ColumnString16(1));
2454 EXPECT_EQ(ASCIIToUTF16("john doe"), s_autofill.ColumnString16(2));
2455 EXPECT_EQ(10, s_autofill.ColumnInt(3));
2456 EXPECT_EQ(1, s_autofill.ColumnInt(4));
2457 ASSERT_TRUE(s_dates.Step());
2458 EXPECT_EQ(10, s_dates.ColumnInt(0));
2459 EXPECT_EQ(1384299100, s_dates.ColumnInt64(1));
2461 // Another entry with one timestamp, differing from the previous one in case
2463 ASSERT_TRUE(s_autofill.Step());
2464 EXPECT_EQ(ASCIIToUTF16("Name"), s_autofill.ColumnString16(0));
2465 EXPECT_EQ(ASCIIToUTF16("john doe"), s_autofill.ColumnString16(1));
2466 EXPECT_EQ(ASCIIToUTF16("john doe"), s_autofill.ColumnString16(2));
2467 EXPECT_EQ(11, s_autofill.ColumnInt(3));
2468 EXPECT_EQ(1, s_autofill.ColumnInt(4));
2469 ASSERT_TRUE(s_dates.Step());
2470 EXPECT_EQ(11, s_dates.ColumnInt(0));
2471 EXPECT_EQ(1384299200, s_dates.ColumnInt64(1));
2473 // An entry with two timestamps (with count > 2; this is realistic).
2474 ASSERT_TRUE(s_autofill.Step());
2475 EXPECT_EQ(ASCIIToUTF16("Email"), s_autofill.ColumnString16(0));
2476 EXPECT_EQ(ASCIIToUTF16("jane@example.com"), s_autofill.ColumnString16(1));
2477 EXPECT_EQ(ASCIIToUTF16("jane@example.com"), s_autofill.ColumnString16(2));
2478 EXPECT_EQ(20, s_autofill.ColumnInt(3));
2479 EXPECT_EQ(3, s_autofill.ColumnInt(4));
2480 ASSERT_TRUE(s_dates.Step());
2481 EXPECT_EQ(20, s_dates.ColumnInt(0));
2482 EXPECT_EQ(1384299300, s_dates.ColumnInt64(1));
2483 ASSERT_TRUE(s_dates.Step());
2484 EXPECT_EQ(20, s_dates.ColumnInt(0));
2485 EXPECT_EQ(1384299301, s_dates.ColumnInt64(1));
2487 // An entry with more than two timestamps, which are stored out of order.
2488 ASSERT_TRUE(s_autofill.Step());
2489 EXPECT_EQ(ASCIIToUTF16("Email"), s_autofill.ColumnString16(0));
2490 EXPECT_EQ(ASCIIToUTF16("jane.doe@example.org"),
2491 s_autofill.ColumnString16(1));
2492 EXPECT_EQ(ASCIIToUTF16("jane.doe@example.org"),
2493 s_autofill.ColumnString16(2));
2494 EXPECT_EQ(21, s_autofill.ColumnInt(3));
2495 EXPECT_EQ(4, s_autofill.ColumnInt(4));
2496 ASSERT_TRUE(s_dates.Step());
2497 EXPECT_EQ(21, s_dates.ColumnInt(0));
2498 EXPECT_EQ(1384299401, s_dates.ColumnInt64(1));
2499 ASSERT_TRUE(s_dates.Step());
2500 EXPECT_EQ(21, s_dates.ColumnInt(0));
2501 EXPECT_EQ(1384299400, s_dates.ColumnInt64(1));
2502 ASSERT_TRUE(s_dates.Step());
2503 EXPECT_EQ(21, s_dates.ColumnInt(0));
2504 EXPECT_EQ(1384299403, s_dates.ColumnInt64(1));
2505 ASSERT_TRUE(s_dates.Step());
2506 EXPECT_EQ(21, s_dates.ColumnInt(0));
2507 EXPECT_EQ(1384299402, s_dates.ColumnInt64(1));
2509 // No more entries expected.
2510 ASSERT_FALSE(s_autofill.Step());
2511 ASSERT_FALSE(s_dates.Step());
2516 // Verify post-conditions. These are expectations for current version of the
2519 sql::Connection connection;
2520 ASSERT_TRUE(connection.Open(GetDatabasePath()));
2521 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
2524 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
2526 // The autofill_dates table should have been dropped, and its columns should
2527 // have been migrated to the autofill table.
2528 EXPECT_FALSE(connection.DoesTableExist("autofill_dates"));
2529 EXPECT_TRUE(connection.DoesColumnExist("autofill", "date_created"));
2530 EXPECT_TRUE(connection.DoesColumnExist("autofill", "date_last_used"));
2532 // Data should have been preserved. Note that it appears out of order
2533 // relative to the previous table, as it's been alphabetized. That's ok.
2535 connection.GetUniqueStatement(
2536 "SELECT name, value, value_lower, date_created, date_last_used,"
2539 "ORDER BY name, value ASC"));
2541 // "jane.doe@example.org": Timestamps should be parsed correctly, and only
2542 // the first and last should be kept.
2543 ASSERT_TRUE(s.Step());
2544 EXPECT_EQ(ASCIIToUTF16("Email"), s.ColumnString16(0));
2545 EXPECT_EQ(ASCIIToUTF16("jane.doe@example.org"), s.ColumnString16(1));
2546 EXPECT_EQ(ASCIIToUTF16("jane.doe@example.org"), s.ColumnString16(2));
2547 EXPECT_EQ(1384299400, s.ColumnInt64(3));
2548 EXPECT_EQ(1384299403, s.ColumnInt64(4));
2549 EXPECT_EQ(4, s.ColumnInt(5));
2551 // "jane@example.com": Timestamps should be parsed correctly.
2552 ASSERT_TRUE(s.Step());
2553 EXPECT_EQ(ASCIIToUTF16("Email"), s.ColumnString16(0));
2554 EXPECT_EQ(ASCIIToUTF16("jane@example.com"), s.ColumnString16(1));
2555 EXPECT_EQ(ASCIIToUTF16("jane@example.com"), s.ColumnString16(2));
2556 EXPECT_EQ(1384299300, s.ColumnInt64(3));
2557 EXPECT_EQ(1384299301, s.ColumnInt64(4));
2558 EXPECT_EQ(3, s.ColumnInt(5));
2560 // "John Doe": The single timestamp should be assigned as both the creation
2561 // and the last use timestamp.
2562 ASSERT_TRUE(s.Step());
2563 EXPECT_EQ(ASCIIToUTF16("Name"), s.ColumnString16(0));
2564 EXPECT_EQ(ASCIIToUTF16("John Doe"), s.ColumnString16(1));
2565 EXPECT_EQ(ASCIIToUTF16("john doe"), s.ColumnString16(2));
2566 EXPECT_EQ(1384299100, s.ColumnInt64(3));
2567 EXPECT_EQ(1384299100, s.ColumnInt64(4));
2568 EXPECT_EQ(1, s.ColumnInt(5));
2570 // "john doe": Should not be merged with "John Doe" (case-sensitivity).
2571 ASSERT_TRUE(s.Step());
2572 EXPECT_EQ(ASCIIToUTF16("Name"), s.ColumnString16(0));
2573 EXPECT_EQ(ASCIIToUTF16("john doe"), s.ColumnString16(1));
2574 EXPECT_EQ(ASCIIToUTF16("john doe"), s.ColumnString16(2));
2575 EXPECT_EQ(1384299200, s.ColumnInt64(3));
2576 EXPECT_EQ(1384299200, s.ColumnInt64(4));
2577 EXPECT_EQ(1, s.ColumnInt(5));
2579 // No more entries expected.
2580 ASSERT_FALSE(s.Step());