1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include <string> 6 7 #include "app/sql/statement.h" 8 #include "base/file_util.h" 9 #include "base/memory/scoped_temp_dir.h" 10 #include "base/stl_util-inl.h" 11 #include "base/string16.h" 12 #include "base/string_number_conversions.h" 13 #include "base/time.h" 14 #include "base/utf_string_conversions.h" 15 #include "base/values.h" 16 #include "chrome/browser/autofill/autofill_profile.h" 17 #include "chrome/browser/autofill/autofill_type.h" 18 #include "chrome/browser/autofill/credit_card.h" 19 #include "chrome/browser/webdata/autofill_change.h" 20 #include "chrome/browser/webdata/autofill_entry.h" 21 #include "chrome/browser/webdata/web_database.h" 22 #include "chrome/common/chrome_paths.h" 23 #include "chrome/common/guid.h" 24 #include "chrome/test/ui_test_utils.h" 25 #include "testing/gtest/include/gtest/gtest.h" 26 27 using base::Time; 28 29 namespace { 30 31 void AutofillProfile31FromStatement(const sql::Statement& s, 32 AutofillProfile* profile, 33 string16* label, 34 int* unique_id, 35 int64* date_modified) { 36 DCHECK(profile); 37 DCHECK(label); 38 DCHECK(unique_id); 39 DCHECK(date_modified); 40 *label = s.ColumnString16(0); 41 *unique_id = s.ColumnInt(1); 42 profile->SetInfo(NAME_FIRST, s.ColumnString16(2)); 43 profile->SetInfo(NAME_MIDDLE, s.ColumnString16(3)); 44 profile->SetInfo(NAME_LAST,s.ColumnString16(4)); 45 profile->SetInfo(EMAIL_ADDRESS, s.ColumnString16(5)); 46 profile->SetInfo(COMPANY_NAME, s.ColumnString16(6)); 47 profile->SetInfo(ADDRESS_HOME_LINE1, s.ColumnString16(7)); 48 profile->SetInfo(ADDRESS_HOME_LINE2, s.ColumnString16(8)); 49 profile->SetInfo(ADDRESS_HOME_CITY, s.ColumnString16(9)); 50 profile->SetInfo(ADDRESS_HOME_STATE, s.ColumnString16(10)); 51 profile->SetInfo(ADDRESS_HOME_ZIP, s.ColumnString16(11)); 52 profile->SetInfo(ADDRESS_HOME_COUNTRY, s.ColumnString16(12)); 53 profile->SetInfo(PHONE_HOME_WHOLE_NUMBER, s.ColumnString16(13)); 54 profile->SetInfo(PHONE_FAX_WHOLE_NUMBER, s.ColumnString16(14)); 55 *date_modified = s.ColumnInt64(15); 56 profile->set_guid(s.ColumnString(16)); 57 EXPECT_TRUE(guid::IsValidGUID(profile->guid())); 58 } 59 60 void AutofillProfile32FromStatement(const sql::Statement& s, 61 AutofillProfile* profile, 62 string16* label, 63 int64* date_modified) { 64 DCHECK(profile); 65 DCHECK(label); 66 DCHECK(date_modified); 67 profile->set_guid(s.ColumnString(0)); 68 EXPECT_TRUE(guid::IsValidGUID(profile->guid())); 69 *label = s.ColumnString16(1); 70 profile->SetInfo(NAME_FIRST, s.ColumnString16(2)); 71 profile->SetInfo(NAME_MIDDLE, s.ColumnString16(3)); 72 profile->SetInfo(NAME_LAST,s.ColumnString16(4)); 73 profile->SetInfo(EMAIL_ADDRESS, s.ColumnString16(5)); 74 profile->SetInfo(COMPANY_NAME, s.ColumnString16(6)); 75 profile->SetInfo(ADDRESS_HOME_LINE1, s.ColumnString16(7)); 76 profile->SetInfo(ADDRESS_HOME_LINE2, s.ColumnString16(8)); 77 profile->SetInfo(ADDRESS_HOME_CITY, s.ColumnString16(9)); 78 profile->SetInfo(ADDRESS_HOME_STATE, s.ColumnString16(10)); 79 profile->SetInfo(ADDRESS_HOME_ZIP, s.ColumnString16(11)); 80 profile->SetInfo(ADDRESS_HOME_COUNTRY, s.ColumnString16(12)); 81 profile->SetInfo(PHONE_HOME_WHOLE_NUMBER, s.ColumnString16(13)); 82 profile->SetInfo(PHONE_FAX_WHOLE_NUMBER, s.ColumnString16(14)); 83 *date_modified = s.ColumnInt64(15); 84 } 85 86 void AutofillProfile33FromStatement(const sql::Statement& s, 87 AutofillProfile* profile, 88 int64* date_modified) { 89 DCHECK(profile); 90 DCHECK(date_modified); 91 profile->set_guid(s.ColumnString(0)); 92 EXPECT_TRUE(guid::IsValidGUID(profile->guid())); 93 profile->SetInfo(COMPANY_NAME, s.ColumnString16(1)); 94 profile->SetInfo(ADDRESS_HOME_LINE1, s.ColumnString16(2)); 95 profile->SetInfo(ADDRESS_HOME_LINE2, s.ColumnString16(3)); 96 profile->SetInfo(ADDRESS_HOME_CITY, s.ColumnString16(4)); 97 profile->SetInfo(ADDRESS_HOME_STATE, s.ColumnString16(5)); 98 profile->SetInfo(ADDRESS_HOME_ZIP, s.ColumnString16(6)); 99 profile->SetInfo(ADDRESS_HOME_COUNTRY, s.ColumnString16(7)); 100 *date_modified = s.ColumnInt64(8); 101 } 102 103 void CreditCard31FromStatement(const sql::Statement& s, 104 CreditCard* credit_card, 105 string16* label, 106 int* unique_id, 107 std::string* encrypted_number, 108 int64* date_modified) { 109 DCHECK(credit_card); 110 DCHECK(label); 111 DCHECK(unique_id); 112 DCHECK(encrypted_number); 113 DCHECK(date_modified); 114 *label = s.ColumnString16(0); 115 *unique_id = s.ColumnInt(1); 116 credit_card->SetInfo(CREDIT_CARD_NAME, s.ColumnString16(2)); 117 credit_card->SetInfo(CREDIT_CARD_TYPE, s.ColumnString16(3)); 118 credit_card->SetInfo(CREDIT_CARD_EXP_MONTH, s.ColumnString16(5)); 119 credit_card->SetInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, s.ColumnString16(6)); 120 int encrypted_number_len = s.ColumnByteLength(10); 121 if (encrypted_number_len) { 122 encrypted_number->resize(encrypted_number_len); 123 memcpy(&(*encrypted_number)[0], s.ColumnBlob(10), encrypted_number_len); 124 } 125 *date_modified = s.ColumnInt64(12); 126 credit_card->set_guid(s.ColumnString(13)); 127 EXPECT_TRUE(guid::IsValidGUID(credit_card->guid())); 128 } 129 130 void CreditCard32FromStatement(const sql::Statement& s, 131 CreditCard* credit_card, 132 std::string* encrypted_number, 133 int64* date_modified) { 134 DCHECK(credit_card); 135 DCHECK(encrypted_number); 136 DCHECK(date_modified); 137 credit_card->set_guid(s.ColumnString(0)); 138 EXPECT_TRUE(guid::IsValidGUID(credit_card->guid())); 139 credit_card->SetInfo(CREDIT_CARD_NAME, s.ColumnString16(1)); 140 credit_card->SetInfo(CREDIT_CARD_EXP_MONTH, s.ColumnString16(2)); 141 credit_card->SetInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, s.ColumnString16(3)); 142 int encrypted_number_len = s.ColumnByteLength(4); 143 if (encrypted_number_len) { 144 encrypted_number->resize(encrypted_number_len); 145 memcpy(&(*encrypted_number)[0], s.ColumnBlob(4), encrypted_number_len); 146 } 147 *date_modified = s.ColumnInt64(5); 148 } 149 150 } // anonymous namespace 151 152 // The WebDatabaseMigrationTest encapsulates testing of database migrations. 153 // Specifically, these tests are intended to exercise any schema changes in 154 // the WebDatabase and data migrations that occur in 155 // |WebDatabase::MigrateOldVersionsAsNeeded()|. 156 class WebDatabaseMigrationTest : public testing::Test { 157 public: 158 WebDatabaseMigrationTest() {} 159 virtual ~WebDatabaseMigrationTest() {} 160 161 virtual void SetUp() { 162 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 163 } 164 165 protected: 166 // Current tested version number. When adding a migration in 167 // |WebDatabase::MigrateOldVersionsAsNeeded()| and changing the version number 168 // |kCurrentVersionNumber| this value should change to reflect the new version 169 // number and a new migration test added below. 170 static const int kCurrentTestedVersionNumber; 171 172 FilePath GetDatabasePath() { 173 const FilePath::CharType kWebDatabaseFilename[] = 174 FILE_PATH_LITERAL("TestWebDatabase.sqlite3"); 175 return temp_dir_.path().Append(FilePath(kWebDatabaseFilename)); 176 } 177 178 // The textual contents of |file| are read from 179 // "chrome/test/data/web_database" and returned in the string |contents|. 180 // Returns true if the file exists and is read successfully, false otherwise. 181 bool GetWebDatabaseData(const FilePath& file, std::string* contents) { 182 FilePath path = ui_test_utils::GetTestFilePath( 183 FilePath(FILE_PATH_LITERAL("web_database")), file); 184 return file_util::PathExists(path) && 185 file_util::ReadFileToString(path, contents); 186 } 187 188 static int VersionFromConnection(sql::Connection* connection) { 189 // Get version. 190 sql::Statement s(connection->GetUniqueStatement( 191 "SELECT value FROM meta WHERE key='version'")); 192 if (!s.Step()) 193 return 0; 194 return s.ColumnInt(0); 195 } 196 197 // The sql files located in "chrome/test/data/web_database" were generated by 198 // launching the Chromium application prior to schema change, then using the 199 // sqlite3 command-line application to dump the contents of the "Web Data" 200 // database. 201 // Like this: 202 // > .output version_nn.sql 203 // > .dump 204 void LoadDatabase(const FilePath::StringType& file); 205 206 // Assertion testing for migrating from version 27 and 28. 207 void MigrateVersion28Assertions(); 208 209 private: 210 ScopedTempDir temp_dir_; 211 212 DISALLOW_COPY_AND_ASSIGN(WebDatabaseMigrationTest); 213 }; 214 215 const int WebDatabaseMigrationTest::kCurrentTestedVersionNumber = 37; 216 217 void WebDatabaseMigrationTest::LoadDatabase(const FilePath::StringType& file) { 218 std::string contents; 219 ASSERT_TRUE(GetWebDatabaseData(FilePath(file), &contents)); 220 221 sql::Connection connection; 222 ASSERT_TRUE(connection.Open(GetDatabasePath())); 223 ASSERT_TRUE(connection.Execute(contents.data())); 224 } 225 226 void WebDatabaseMigrationTest::MigrateVersion28Assertions() { 227 // Load the database via the WebDatabase class and migrate the database to 228 // the current version. 229 { 230 WebDatabase db; 231 ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath())); 232 } 233 234 // Verify post-conditions. These are expectations for current version of the 235 // database. 236 { 237 sql::Connection connection; 238 ASSERT_TRUE(connection.Open(GetDatabasePath())); 239 240 // Check version. 241 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection)); 242 243 // Make sure supports_instant was dropped and instant_url was added. 244 EXPECT_FALSE(connection.DoesColumnExist("keywords", "supports_instant")); 245 EXPECT_TRUE(connection.DoesColumnExist("keywords", "instant_url")); 246 247 // Check that instant_url is empty. 248 std::string stmt = "SELECT instant_url FROM keywords"; 249 sql::Statement s(connection.GetUniqueStatement(stmt.c_str())); 250 ASSERT_TRUE(s.Step()); 251 EXPECT_EQ(std::string(), s.ColumnString(0)); 252 253 // Verify the data made it over. 254 stmt = "SELECT id, short_name, keyword, favicon_url, url, " 255 "show_in_default_list, safe_for_autoreplace, originating_url, " 256 "date_created, usage_count, input_encodings, suggest_url, " 257 "prepopulate_id, autogenerate_keyword, logo_id, created_by_policy, " 258 "instant_url FROM keywords"; 259 sql::Statement s2(connection.GetUniqueStatement(stmt.c_str())); 260 ASSERT_TRUE(s2.Step()); 261 EXPECT_EQ(2, s2.ColumnInt(0)); 262 EXPECT_EQ("Google", s2.ColumnString(1)); 263 EXPECT_EQ("google.com", s2.ColumnString(2)); 264 EXPECT_EQ("http://www.google.com/favicon.ico", s2.ColumnString(3)); 265 EXPECT_EQ("{google:baseURL}search?{google:RLZ}{google:acceptedSuggestion}"\ 266 "{google:originalQueryForSuggestion}sourceid=chrome&ie={inputEncoding}"\ 267 "&q={searchTerms}", 268 s2.ColumnString(4)); 269 EXPECT_EQ(1, s2.ColumnInt(5)); 270 EXPECT_EQ(1, s2.ColumnInt(6)); 271 EXPECT_EQ(std::string(), s2.ColumnString(7)); 272 EXPECT_EQ(0, s2.ColumnInt(8)); 273 EXPECT_EQ(0, s2.ColumnInt(9)); 274 EXPECT_EQ(std::string("UTF-8"), s2.ColumnString(10)); 275 EXPECT_EQ(std::string("{google:baseSuggestURL}search?client=chrome&hl=" 276 "{language}&q={searchTerms}"), s2.ColumnString(11)); 277 EXPECT_EQ(1, s2.ColumnInt(12)); 278 EXPECT_EQ(1, s2.ColumnInt(13)); 279 EXPECT_EQ(6245, s2.ColumnInt(14)); 280 EXPECT_EQ(0, s2.ColumnInt(15)); 281 EXPECT_EQ(0, s2.ColumnInt(16)); 282 EXPECT_EQ(std::string(), s2.ColumnString(17)); 283 } 284 } 285 286 // Tests that the all migrations from an empty database succeed. 287 TEST_F(WebDatabaseMigrationTest, MigrateEmptyToCurrent) { 288 // Load the database via the WebDatabase class and migrate the database to 289 // the current version. 290 { 291 WebDatabase db; 292 ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath())); 293 } 294 295 // Verify post-conditions. These are expectations for current version of the 296 // database. 297 { 298 sql::Connection connection; 299 ASSERT_TRUE(connection.Open(GetDatabasePath())); 300 301 // Check version. 302 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection)); 303 304 // Check that expected tables are present. 305 EXPECT_TRUE(connection.DoesTableExist("meta")); 306 EXPECT_TRUE(connection.DoesTableExist("keywords")); 307 EXPECT_TRUE(connection.DoesTableExist("logins")); 308 EXPECT_TRUE(connection.DoesTableExist("web_app_icons")); 309 EXPECT_TRUE(connection.DoesTableExist("web_apps")); 310 EXPECT_TRUE(connection.DoesTableExist("autofill")); 311 EXPECT_TRUE(connection.DoesTableExist("autofill_dates")); 312 EXPECT_TRUE(connection.DoesTableExist("autofill_profiles")); 313 EXPECT_TRUE(connection.DoesTableExist("credit_cards")); 314 EXPECT_TRUE(connection.DoesTableExist("token_service")); 315 } 316 } 317 318 // Tests that the |credit_card| table gets added to the schema for a version 22 319 // database. 320 TEST_F(WebDatabaseMigrationTest, MigrateVersion22ToCurrent) { 321 // This schema is taken from a build prior to the addition of the 322 // |credit_card| table. Version 22 of the schema. Contrast this with the 323 // corrupt version below. 324 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_22.sql"))); 325 326 // Verify pre-conditions. 327 { 328 sql::Connection connection; 329 ASSERT_TRUE(connection.Open(GetDatabasePath())); 330 331 // No |credit_card| table prior to version 23. 332 ASSERT_FALSE(connection.DoesColumnExist("credit_cards", "guid")); 333 ASSERT_FALSE( 334 connection.DoesColumnExist("credit_cards", "card_number_encrypted")); 335 } 336 337 // Load the database via the WebDatabase class and migrate the database to 338 // the current version. 339 { 340 WebDatabase db; 341 ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath())); 342 } 343 344 // Verify post-conditions. These are expectations for current version of the 345 // database. 346 { 347 sql::Connection connection; 348 ASSERT_TRUE(connection.Open(GetDatabasePath())); 349 350 // Check version. 351 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection)); 352 353 // |credit_card| table now exists. 354 EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "guid")); 355 EXPECT_TRUE( 356 connection.DoesColumnExist("credit_cards", "card_number_encrypted")); 357 } 358 } 359 360 // Tests that the |credit_card| table gets added to the schema for a corrupt 361 // version 22 database. The corruption is that the |credit_cards| table exists 362 // but the schema version number was not set correctly to 23 or later. This 363 // test exercises code introduced to fix bug http://crbug.com/50699 that 364 // resulted from the corruption. 365 TEST_F(WebDatabaseMigrationTest, MigrateVersion22CorruptedToCurrent) { 366 // This schema is taken from a build after the addition of the |credit_card| 367 // table. Due to a bug in the migration logic the version is set incorrectly 368 // to 22 (it should have been updated to 23 at least). 369 ASSERT_NO_FATAL_FAILURE( 370 LoadDatabase(FILE_PATH_LITERAL("version_22_corrupt.sql"))); 371 372 // Verify pre-conditions. These are expectations for corrupt version 22 of 373 // the database. 374 { 375 sql::Connection connection; 376 ASSERT_TRUE(connection.Open(GetDatabasePath())); 377 378 // Columns existing and not existing before current version. 379 ASSERT_TRUE(connection.DoesColumnExist("credit_cards", "unique_id")); 380 ASSERT_TRUE( 381 connection.DoesColumnExist("credit_cards", "card_number_encrypted")); 382 ASSERT_TRUE(connection.DoesColumnExist("keywords", "id")); 383 ASSERT_FALSE(connection.DoesColumnExist("keywords", "logo_id")); 384 } 385 386 // Load the database via the WebDatabase class and migrate the database to 387 // the current version. 388 { 389 WebDatabase db; 390 ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath())); 391 } 392 393 // Verify post-conditions. These are expectations for current version of the 394 // database. 395 { 396 sql::Connection connection; 397 ASSERT_TRUE(connection.Open(GetDatabasePath())); 398 399 // Check version. 400 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection)); 401 402 403 // Columns existing and not existing before version 25. 404 EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "unique_id")); 405 EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "guid")); 406 EXPECT_TRUE( 407 connection.DoesColumnExist("credit_cards", "card_number_encrypted")); 408 EXPECT_TRUE(connection.DoesColumnExist("keywords", "id")); 409 EXPECT_TRUE(connection.DoesColumnExist("keywords", "logo_id")); 410 } 411 } 412 413 // Tests that the |keywords| |logo_id| column gets added to the schema for a 414 // version 24 database. 415 TEST_F(WebDatabaseMigrationTest, MigrateVersion24ToCurrent) { 416 // This schema is taken from a build prior to the addition of the |keywords| 417 // |logo_id| column. 418 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_24.sql"))); 419 420 // Verify pre-conditions. These are expectations for version 24 of the 421 // database. 422 { 423 sql::Connection connection; 424 ASSERT_TRUE(connection.Open(GetDatabasePath())); 425 426 // Columns existing and not existing before current version. 427 ASSERT_TRUE(connection.DoesColumnExist("keywords", "id")); 428 ASSERT_FALSE(connection.DoesColumnExist("keywords", "logo_id")); 429 } 430 431 // Load the database via the WebDatabase class and migrate the database to 432 // the current version. 433 { 434 WebDatabase db; 435 ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath())); 436 } 437 438 // Verify post-conditions. These are expectations for current version of the 439 // database. 440 { 441 sql::Connection connection; 442 ASSERT_TRUE(connection.Open(GetDatabasePath())); 443 444 // Check version. 445 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection)); 446 447 // |keywords| |logo_id| column should have been added. 448 EXPECT_TRUE(connection.DoesColumnExist("keywords", "id")); 449 EXPECT_TRUE(connection.DoesColumnExist("keywords", "logo_id")); 450 } 451 } 452 453 // Tests that the |keywords| |created_by_policy| column gets added to the schema 454 // for a version 25 database. 455 TEST_F(WebDatabaseMigrationTest, MigrateVersion25ToCurrent) { 456 // This schema is taken from a build prior to the addition of the |keywords| 457 // |created_by_policy| column. 458 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_25.sql"))); 459 460 // Verify pre-conditions. These are expectations for version 25 of the 461 // database. 462 { 463 sql::Connection connection; 464 ASSERT_TRUE(connection.Open(GetDatabasePath())); 465 } 466 467 // Load the database via the WebDatabase class and migrate the database to 468 // the current version. 469 { 470 WebDatabase db; 471 ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath())); 472 } 473 474 // Verify post-conditions. These are expectations for current version of the 475 // database. 476 { 477 sql::Connection connection; 478 ASSERT_TRUE(connection.Open(GetDatabasePath())); 479 480 // Check version. 481 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection)); 482 483 // |keywords| |logo_id| column should have been added. 484 EXPECT_TRUE(connection.DoesColumnExist("keywords", "id")); 485 EXPECT_TRUE(connection.DoesColumnExist("keywords", "created_by_policy")); 486 } 487 } 488 489 // Tests that the credit_cards.billing_address column is changed from a string 490 // to an int whilst preserving the associated billing address. This version of 491 // the test makes sure a stored label is converted to an ID. 492 TEST_F(WebDatabaseMigrationTest, MigrateVersion26ToCurrentStringLabels) { 493 // This schema is taken from a build prior to the change of column type for 494 // credit_cards.billing_address from string to int. 495 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_26.sql"))); 496 497 // Verify pre-conditions. These are expectations for version 26 of the 498 // database. 499 { 500 sql::Connection connection; 501 ASSERT_TRUE(connection.Open(GetDatabasePath())); 502 503 // Columns existing and not existing before current version. 504 EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "billing_address")); 505 506 std::string stmt = "INSERT INTO autofill_profiles" 507 "(label, unique_id, first_name, middle_name, last_name, email," 508 " company_name, address_line_1, address_line_2, city, state, zipcode," 509 " country, phone, fax)" 510 "VALUES ('Home',1,'','','','','','','','','','','','','')"; 511 sql::Statement s(connection.GetUniqueStatement(stmt.c_str())); 512 ASSERT_TRUE(s.Run()); 513 514 // Insert a CC linked to an existing address. 515 std::string stmt2 = "INSERT INTO credit_cards" 516 "(label, unique_id, name_on_card, type, card_number," 517 " expiration_month, expiration_year, verification_code, billing_address," 518 " shipping_address, card_number_encrypted, verification_code_encrypted)" 519 "VALUES ('label',2,'Jack','Visa','1234',2,2012,'','Home','','','')"; 520 sql::Statement s2(connection.GetUniqueStatement(stmt2.c_str())); 521 ASSERT_TRUE(s2.Run()); 522 523 // |billing_address| is a string. 524 std::string stmt3 = "SELECT billing_address FROM credit_cards"; 525 sql::Statement s3(connection.GetUniqueStatement(stmt3.c_str())); 526 ASSERT_TRUE(s3.Step()); 527 EXPECT_EQ(s3.ColumnType(0), sql::COLUMN_TYPE_TEXT); 528 } 529 530 // Load the database via the WebDatabase class and migrate the database to 531 // the current version. 532 { 533 WebDatabase db; 534 ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath())); 535 } 536 537 // Verify post-conditions. These are expectations for current version of the 538 // database. 539 { 540 sql::Connection connection; 541 ASSERT_TRUE(connection.Open(GetDatabasePath())); 542 543 // Check version. 544 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection)); 545 EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "billing_address")); 546 547 // Verify the credit card data is converted. 548 sql::Statement s(connection.GetUniqueStatement( 549 "SELECT guid, name_on_card, expiration_month, expiration_year, " 550 "card_number_encrypted, date_modified " 551 "FROM credit_cards")); 552 ASSERT_TRUE(s.Step()); 553 EXPECT_EQ("Jack", s.ColumnString(1)); 554 EXPECT_EQ(2, s.ColumnInt(2)); 555 EXPECT_EQ(2012, s.ColumnInt(3)); 556 // Column 5 is encrypted number blob. 557 // Column 6 is date_modified. 558 } 559 } 560 561 // Tests that the credit_cards.billing_address column is changed from a string 562 // to an int whilst preserving the associated billing address. This version of 563 // the test makes sure a stored string ID is converted to an integer ID. 564 TEST_F(WebDatabaseMigrationTest, MigrateVersion26ToCurrentStringIDs) { 565 // This schema is taken from a build prior to the change of column type for 566 // credit_cards.billing_address from string to int. 567 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_26.sql"))); 568 569 // Verify pre-conditions. These are expectations for version 26 of the 570 // database. 571 { 572 sql::Connection connection; 573 ASSERT_TRUE(connection.Open(GetDatabasePath())); 574 EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "billing_address")); 575 576 std::string stmt = "INSERT INTO autofill_profiles" 577 "(label, unique_id, first_name, middle_name, last_name, email," 578 " company_name, address_line_1, address_line_2, city, state, zipcode," 579 " country, phone, fax)" 580 "VALUES ('Home',1,'','','','','','','','','','','','','')"; 581 sql::Statement s(connection.GetUniqueStatement(stmt.c_str())); 582 ASSERT_TRUE(s.Run()); 583 584 // Insert a CC linked to an existing address. 585 std::string stmt2 = "INSERT INTO credit_cards" 586 "(label, unique_id, name_on_card, type, card_number," 587 " expiration_month, expiration_year, verification_code, billing_address," 588 " shipping_address, card_number_encrypted, verification_code_encrypted)" 589 "VALUES ('label',2,'Jack','Visa','1234',2,2012,'','1','','','')"; 590 sql::Statement s2(connection.GetUniqueStatement(stmt2.c_str())); 591 ASSERT_TRUE(s2.Run()); 592 593 // |billing_address| is a string. 594 std::string stmt3 = "SELECT billing_address FROM credit_cards"; 595 sql::Statement s3(connection.GetUniqueStatement(stmt3.c_str())); 596 ASSERT_TRUE(s3.Step()); 597 EXPECT_EQ(s3.ColumnType(0), sql::COLUMN_TYPE_TEXT); 598 } 599 600 // Load the database via the WebDatabase class and migrate the database to 601 // the current version. 602 { 603 WebDatabase db; 604 ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath())); 605 } 606 607 // Verify post-conditions. These are expectations for current version of the 608 // database. 609 { 610 sql::Connection connection; 611 ASSERT_TRUE(connection.Open(GetDatabasePath())); 612 613 // Check version. 614 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection)); 615 616 // |keywords| |logo_id| column should have been added. 617 EXPECT_TRUE(connection.DoesColumnExist("keywords", "id")); 618 EXPECT_TRUE(connection.DoesColumnExist("keywords", "created_by_policy")); 619 EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "billing_address")); 620 621 // Verify the credit card data is converted. 622 sql::Statement s(connection.GetUniqueStatement( 623 "SELECT guid, name_on_card, expiration_month, expiration_year, " 624 "card_number_encrypted, date_modified " 625 "FROM credit_cards")); 626 ASSERT_TRUE(s.Step()); 627 EXPECT_EQ("Jack", s.ColumnString(1)); 628 EXPECT_EQ(2, s.ColumnInt(2)); 629 EXPECT_EQ(2012, s.ColumnInt(3)); 630 // Column 5 is encrypted credit card number blob. 631 // Column 6 is date_modified. 632 } 633 } 634 635 // Tests migration from 27->current. This test is now the same as 28->current 636 // as the column added in 28 was nuked in 29. 637 TEST_F(WebDatabaseMigrationTest, MigrateVersion27ToCurrent) { 638 // Initialize the database. 639 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_27.sql"))); 640 641 // Verify pre-conditions. These are expectations for version 28 of the 642 // database. 643 { 644 sql::Connection connection; 645 ASSERT_TRUE(connection.Open(GetDatabasePath())); 646 647 ASSERT_FALSE(connection.DoesColumnExist("keywords", "supports_instant")); 648 ASSERT_FALSE(connection.DoesColumnExist("keywords", "instant_url")); 649 } 650 651 MigrateVersion28Assertions(); 652 } 653 654 // Makes sure instant_url is added correctly to keywords. 655 TEST_F(WebDatabaseMigrationTest, MigrateVersion28ToCurrent) { 656 // Initialize the database. 657 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_28.sql"))); 658 659 // Verify pre-conditions. These are expectations for version 28 of the 660 // database. 661 { 662 sql::Connection connection; 663 ASSERT_TRUE(connection.Open(GetDatabasePath())); 664 665 ASSERT_TRUE(connection.DoesColumnExist("keywords", "supports_instant")); 666 ASSERT_FALSE(connection.DoesColumnExist("keywords", "instant_url")); 667 } 668 669 MigrateVersion28Assertions(); 670 } 671 672 // Makes sure date_modified is added correctly to autofill_profiles and 673 // credit_cards. 674 TEST_F(WebDatabaseMigrationTest, MigrateVersion29ToCurrent) { 675 // Initialize the database. 676 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_29.sql"))); 677 678 // Verify pre-conditions. These are expectations for version 29 of the 679 // database. 680 { 681 sql::Connection connection; 682 ASSERT_TRUE(connection.Open(GetDatabasePath())); 683 684 EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", 685 "date_modified")); 686 EXPECT_FALSE(connection.DoesColumnExist("credit_cards", 687 "date_modified")); 688 } 689 690 // Load the database via the WebDatabase class and migrate the database to 691 // the current version. 692 Time pre_creation_time = Time::Now(); 693 { 694 WebDatabase db; 695 ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath())); 696 } 697 Time post_creation_time = Time::Now(); 698 699 // Verify post-conditions. These are expectations for current version of the 700 // database. 701 { 702 sql::Connection connection; 703 ASSERT_TRUE(connection.Open(GetDatabasePath())); 704 705 // Check version. 706 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection)); 707 708 // Check that the columns were created. 709 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", 710 "date_modified")); 711 EXPECT_TRUE(connection.DoesColumnExist("credit_cards", 712 "date_modified")); 713 714 sql::Statement s_profiles(connection.GetUniqueStatement( 715 "SELECT date_modified FROM autofill_profiles ")); 716 ASSERT_TRUE(s_profiles); 717 while (s_profiles.Step()) { 718 EXPECT_GE(s_profiles.ColumnInt64(0), 719 pre_creation_time.ToTimeT()); 720 EXPECT_LE(s_profiles.ColumnInt64(0), 721 post_creation_time.ToTimeT()); 722 } 723 EXPECT_TRUE(s_profiles.Succeeded()); 724 725 sql::Statement s_credit_cards(connection.GetUniqueStatement( 726 "SELECT date_modified FROM credit_cards ")); 727 ASSERT_TRUE(s_credit_cards); 728 while (s_credit_cards.Step()) { 729 EXPECT_GE(s_credit_cards.ColumnInt64(0), 730 pre_creation_time.ToTimeT()); 731 EXPECT_LE(s_credit_cards.ColumnInt64(0), 732 post_creation_time.ToTimeT()); 733 } 734 EXPECT_TRUE(s_credit_cards.Succeeded()); 735 } 736 } 737 738 // Makes sure guids are added to autofill_profiles and credit_cards tables. 739 TEST_F(WebDatabaseMigrationTest, MigrateVersion30ToCurrent) { 740 // Initialize the database. 741 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_30.sql"))); 742 743 // Verify pre-conditions. These are expectations for version 29 of the 744 // database. 745 { 746 sql::Connection connection; 747 ASSERT_TRUE(connection.Open(GetDatabasePath())); 748 749 EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "guid")); 750 EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "guid")); 751 } 752 753 // Load the database via the WebDatabase class and migrate the database to 754 // the current version. 755 { 756 WebDatabase db; 757 ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath())); 758 } 759 760 // Verify post-conditions. These are expectations for current version of the 761 // database. 762 { 763 sql::Connection connection; 764 ASSERT_TRUE(connection.Open(GetDatabasePath())); 765 766 // Check version. 767 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection)); 768 769 ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid")); 770 ASSERT_TRUE(connection.DoesColumnExist("credit_cards", "guid")); 771 772 // Check that guids are non-null, non-empty, conforms to guid format, and 773 // are different. 774 sql::Statement s( 775 connection.GetUniqueStatement("SELECT guid FROM autofill_profiles")); 776 777 ASSERT_TRUE(s.Step()); 778 std::string guid1 = s.ColumnString(0); 779 EXPECT_TRUE(guid::IsValidGUID(guid1)); 780 781 ASSERT_TRUE(s.Step()); 782 std::string guid2 = s.ColumnString(0); 783 EXPECT_TRUE(guid::IsValidGUID(guid2)); 784 785 EXPECT_NE(guid1, guid2); 786 } 787 } 788 789 // Removes unique IDs and make GUIDs the primary key. Also removes unused 790 // columns. 791 TEST_F(WebDatabaseMigrationTest, MigrateVersion31ToCurrent) { 792 // Initialize the database. 793 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_31.sql"))); 794 795 // Verify pre-conditions. These are expectations for version 30 of the 796 // database. 797 AutofillProfile profile; 798 string16 profile_label; 799 int profile_unique_id = 0; 800 int64 profile_date_modified = 0; 801 CreditCard credit_card; 802 string16 cc_label; 803 int cc_unique_id = 0; 804 std::string cc_number_encrypted; 805 int64 cc_date_modified = 0; 806 { 807 sql::Connection connection; 808 ASSERT_TRUE(connection.Open(GetDatabasePath())); 809 810 // Verify existence of columns we'll be changing. 811 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid")); 812 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "unique_id")); 813 EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "guid")); 814 EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "unique_id")); 815 EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "type")); 816 EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "card_number")); 817 EXPECT_TRUE(connection.DoesColumnExist("credit_cards", 818 "verification_code")); 819 EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "billing_address")); 820 EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "shipping_address")); 821 EXPECT_TRUE(connection.DoesColumnExist("credit_cards", 822 "verification_code_encrypted")); 823 824 // Fetch data in the database prior to migration. 825 sql::Statement s1( 826 connection.GetUniqueStatement( 827 "SELECT label, unique_id, first_name, middle_name, last_name, " 828 "email, company_name, address_line_1, address_line_2, city, state, " 829 "zipcode, country, phone, fax, date_modified, guid " 830 "FROM autofill_profiles")); 831 ASSERT_TRUE(s1.Step()); 832 EXPECT_NO_FATAL_FAILURE(AutofillProfile31FromStatement( 833 s1, &profile, &profile_label, &profile_unique_id, 834 &profile_date_modified)); 835 836 sql::Statement s2( 837 connection.GetUniqueStatement( 838 "SELECT label, unique_id, name_on_card, type, card_number, " 839 "expiration_month, expiration_year, verification_code, " 840 "billing_address, shipping_address, card_number_encrypted, " 841 "verification_code_encrypted, date_modified, guid " 842 "FROM credit_cards")); 843 ASSERT_TRUE(s2.Step()); 844 EXPECT_NO_FATAL_FAILURE(CreditCard31FromStatement(s2, 845 &credit_card, 846 &cc_label, 847 &cc_unique_id, 848 &cc_number_encrypted, 849 &cc_date_modified)); 850 851 EXPECT_NE(profile_unique_id, cc_unique_id); 852 EXPECT_NE(profile.guid(), credit_card.guid()); 853 } 854 855 // Load the database via the WebDatabase class and migrate the database to 856 // the current version. 857 { 858 WebDatabase db; 859 ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath())); 860 } 861 862 // Verify post-conditions. These are expectations for current version of the 863 // database. 864 { 865 sql::Connection connection; 866 ASSERT_TRUE(connection.Open(GetDatabasePath())); 867 868 // Check version. 869 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection)); 870 871 // Verify existence of columns we'll be changing. 872 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid")); 873 EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "unique_id")); 874 EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "guid")); 875 EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "unique_id")); 876 EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "type")); 877 EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "card_number")); 878 EXPECT_FALSE(connection.DoesColumnExist("credit_cards", 879 "verification_code")); 880 EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "billing_address")); 881 EXPECT_FALSE(connection.DoesColumnExist("credit_cards", 882 "shipping_address")); 883 EXPECT_FALSE(connection.DoesColumnExist("credit_cards", 884 "verification_code_encrypted")); 885 886 // Verify data in the database after the migration. 887 sql::Statement s1( 888 connection.GetUniqueStatement( 889 "SELECT guid, company_name, address_line_1, address_line_2, " 890 "city, state, zipcode, country, date_modified " 891 "FROM autofill_profiles")); 892 ASSERT_TRUE(s1.Step()); 893 894 AutofillProfile profile_a; 895 int64 profile_date_modified_a = 0; 896 EXPECT_NO_FATAL_FAILURE(AutofillProfile33FromStatement( 897 s1, &profile_a, &profile_date_modified_a)); 898 EXPECT_EQ(profile.guid(), profile_a.guid()); 899 EXPECT_EQ(profile.GetInfo(COMPANY_NAME), 900 profile_a.GetInfo(COMPANY_NAME)); 901 EXPECT_EQ(profile.GetInfo(ADDRESS_HOME_LINE1), 902 profile_a.GetInfo(ADDRESS_HOME_LINE1)); 903 EXPECT_EQ(profile.GetInfo(ADDRESS_HOME_LINE2), 904 profile_a.GetInfo(ADDRESS_HOME_LINE2)); 905 EXPECT_EQ(profile.GetInfo(ADDRESS_HOME_CITY), 906 profile_a.GetInfo(ADDRESS_HOME_CITY)); 907 EXPECT_EQ(profile.GetInfo(ADDRESS_HOME_STATE), 908 profile_a.GetInfo(ADDRESS_HOME_STATE)); 909 EXPECT_EQ(profile.GetInfo(ADDRESS_HOME_ZIP), 910 profile_a.GetInfo(ADDRESS_HOME_ZIP)); 911 EXPECT_EQ(profile.GetInfo(ADDRESS_HOME_COUNTRY), 912 profile_a.GetInfo(ADDRESS_HOME_COUNTRY)); 913 EXPECT_EQ(profile_date_modified, profile_date_modified_a); 914 915 sql::Statement s2( 916 connection.GetUniqueStatement( 917 "SELECT guid, name_on_card, expiration_month, " 918 "expiration_year, card_number_encrypted, date_modified " 919 "FROM credit_cards")); 920 ASSERT_TRUE(s2.Step()); 921 922 CreditCard credit_card_a; 923 string16 cc_label_a; 924 std::string cc_number_encrypted_a; 925 int64 cc_date_modified_a = 0; 926 EXPECT_NO_FATAL_FAILURE(CreditCard32FromStatement(s2, 927 &credit_card_a, 928 &cc_number_encrypted_a, 929 &cc_date_modified_a)); 930 EXPECT_EQ(credit_card, credit_card_a); 931 EXPECT_EQ(cc_label, cc_label_a); 932 EXPECT_EQ(cc_number_encrypted, cc_number_encrypted_a); 933 EXPECT_EQ(cc_date_modified, cc_date_modified_a); 934 } 935 } 936 937 // Factor |autofill_profiles| address information separately from name, email, 938 // and phone. 939 TEST_F(WebDatabaseMigrationTest, MigrateVersion32ToCurrent) { 940 // Initialize the database. 941 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_32.sql"))); 942 943 // Verify pre-conditions. These are expectations for version 32 of the 944 // database. 945 { 946 sql::Connection connection; 947 ASSERT_TRUE(connection.Open(GetDatabasePath())); 948 949 // Verify existence of columns we'll be changing. 950 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid")); 951 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "label")); 952 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "first_name")); 953 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "middle_name")); 954 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "last_name")); 955 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "email")); 956 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", 957 "company_name")); 958 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", 959 "address_line_1")); 960 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", 961 "address_line_2")); 962 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "city")); 963 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "state")); 964 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "zipcode")); 965 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "country")); 966 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "phone")); 967 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "fax")); 968 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", 969 "date_modified")); 970 971 EXPECT_FALSE(connection.DoesTableExist("autofill_profile_names")); 972 EXPECT_FALSE(connection.DoesTableExist("autofill_profile_emails")); 973 EXPECT_FALSE(connection.DoesTableExist("autofill_profile_phones")); 974 975 EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "label")); 976 } 977 978 // Load the database via the WebDatabase class and migrate the database to 979 // the current version. 980 { 981 WebDatabase db; 982 ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath())); 983 } 984 985 // Verify post-conditions. These are expectations for current version of the 986 // database. 987 { 988 sql::Connection connection; 989 ASSERT_TRUE(connection.Open(GetDatabasePath())); 990 991 // Check version. 992 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection)); 993 994 // Verify changes to columns. 995 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid")); 996 EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "label")); 997 EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "first_name")); 998 EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", 999 "middle_name")); 1000 EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "last_name")); 1001 EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "email")); 1002 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", 1003 "company_name")); 1004 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", 1005 "address_line_1")); 1006 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", 1007 "address_line_2")); 1008 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "city")); 1009 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "state")); 1010 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "zipcode")); 1011 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "country")); 1012 EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "phone")); 1013 EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "fax")); 1014 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", 1015 "date_modified")); 1016 1017 // New "names" table. 1018 EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_names", "guid")); 1019 EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_names", 1020 "first_name")); 1021 EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_names", 1022 "middle_name")); 1023 EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_names", 1024 "last_name")); 1025 1026 // New "emails" table. 1027 EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_emails", "guid")); 1028 EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_emails", "email")); 1029 1030 // New "phones" table. 1031 EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_phones", "guid")); 1032 EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_phones", "type")); 1033 EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_phones", 1034 "number")); 1035 1036 EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "label")); 1037 1038 // Verify data in the database after the migration. 1039 sql::Statement s1( 1040 connection.GetUniqueStatement( 1041 "SELECT guid, company_name, address_line_1, address_line_2, " 1042 "city, state, zipcode, country, date_modified " 1043 "FROM autofill_profiles")); 1044 1045 // John Doe. 1046 ASSERT_TRUE(s1.Step()); 1047 EXPECT_EQ("00580526-FF81-EE2A-0546-1AC593A32E2F", s1.ColumnString(0)); 1048 EXPECT_EQ(ASCIIToUTF16("Doe Enterprises"), s1.ColumnString16(1)); 1049 EXPECT_EQ(ASCIIToUTF16("1 Main St"), s1.ColumnString16(2)); 1050 EXPECT_EQ(ASCIIToUTF16("Apt 1"), s1.ColumnString16(3)); 1051 EXPECT_EQ(ASCIIToUTF16("Los Altos"), s1.ColumnString16(4)); 1052 EXPECT_EQ(ASCIIToUTF16("CA"), s1.ColumnString16(5)); 1053 EXPECT_EQ(ASCIIToUTF16("94022"), s1.ColumnString16(6)); 1054 EXPECT_EQ(ASCIIToUTF16("United States"), s1.ColumnString16(7)); 1055 EXPECT_EQ(1297882100L, s1.ColumnInt64(8)); 1056 1057 // John P. Doe. 1058 // Gets merged during migration from 35 to 37 due to multi-valued fields. 1059 1060 // Dave Smith. 1061 ASSERT_TRUE(s1.Step()); 1062 EXPECT_EQ("4C74A9D8-7EEE-423E-F9C2-E7FA70ED1396", s1.ColumnString(0)); 1063 EXPECT_EQ(ASCIIToUTF16(""), s1.ColumnString16(1)); 1064 EXPECT_EQ(ASCIIToUTF16("2 Main Street"), s1.ColumnString16(2)); 1065 EXPECT_EQ(ASCIIToUTF16(""), s1.ColumnString16(3)); 1066 EXPECT_EQ(ASCIIToUTF16("Los Altos"), s1.ColumnString16(4)); 1067 EXPECT_EQ(ASCIIToUTF16("CA"), s1.ColumnString16(5)); 1068 EXPECT_EQ(ASCIIToUTF16("94022"), s1.ColumnString16(6)); 1069 EXPECT_EQ(ASCIIToUTF16("United States"), s1.ColumnString16(7)); 1070 EXPECT_EQ(1297882100L, s1.ColumnInt64(8)); 1071 1072 // Dave Smith (Part 2). 1073 ASSERT_TRUE(s1.Step()); 1074 EXPECT_EQ("722DF5C4-F74A-294A-46F0-31FFDED0D635", s1.ColumnString(0)); 1075 EXPECT_EQ(ASCIIToUTF16(""), s1.ColumnString16(1)); 1076 EXPECT_EQ(ASCIIToUTF16("2 Main St"), s1.ColumnString16(2)); 1077 EXPECT_EQ(ASCIIToUTF16(""), s1.ColumnString16(3)); 1078 EXPECT_EQ(ASCIIToUTF16("Los Altos"), s1.ColumnString16(4)); 1079 EXPECT_EQ(ASCIIToUTF16("CA"), s1.ColumnString16(5)); 1080 EXPECT_EQ(ASCIIToUTF16("94022"), s1.ColumnString16(6)); 1081 EXPECT_EQ(ASCIIToUTF16("United States"), s1.ColumnString16(7)); 1082 EXPECT_EQ(1297882100L, s1.ColumnInt64(8)); 1083 1084 // Alfred E Newman. 1085 // Gets culled during migration from 35 to 36 due to incomplete address. 1086 1087 // 3 Main St. 1088 ASSERT_TRUE(s1.Step()); 1089 EXPECT_EQ("9E5FE298-62C7-83DF-6293-381BC589183F", s1.ColumnString(0)); 1090 EXPECT_EQ(ASCIIToUTF16(""), s1.ColumnString16(1)); 1091 EXPECT_EQ(ASCIIToUTF16("3 Main St"), s1.ColumnString16(2)); 1092 EXPECT_EQ(ASCIIToUTF16(""), s1.ColumnString16(3)); 1093 EXPECT_EQ(ASCIIToUTF16("Los Altos"), s1.ColumnString16(4)); 1094 EXPECT_EQ(ASCIIToUTF16("CA"), s1.ColumnString16(5)); 1095 EXPECT_EQ(ASCIIToUTF16("94022"), s1.ColumnString16(6)); 1096 EXPECT_EQ(ASCIIToUTF16("United States"), s1.ColumnString16(7)); 1097 EXPECT_EQ(1297882100L, s1.ColumnInt64(8)); 1098 1099 // That should be all. 1100 EXPECT_FALSE(s1.Step()); 1101 1102 sql::Statement s2( 1103 connection.GetUniqueStatement( 1104 "SELECT guid, first_name, middle_name, last_name " 1105 "FROM autofill_profile_names")); 1106 1107 // John Doe. 1108 ASSERT_TRUE(s2.Step()); 1109 EXPECT_EQ("00580526-FF81-EE2A-0546-1AC593A32E2F", s2.ColumnString(0)); 1110 EXPECT_EQ(ASCIIToUTF16("John"), s2.ColumnString16(1)); 1111 EXPECT_EQ(ASCIIToUTF16(""), s2.ColumnString16(2)); 1112 EXPECT_EQ(ASCIIToUTF16("Doe"), s2.ColumnString16(3)); 1113 1114 // John P. Doe. Note same guid as above due to merging of multi-valued 1115 // fields. 1116 ASSERT_TRUE(s2.Step()); 1117 EXPECT_EQ("00580526-FF81-EE2A-0546-1AC593A32E2F", s2.ColumnString(0)); 1118 EXPECT_EQ(ASCIIToUTF16("John"), s2.ColumnString16(1)); 1119 EXPECT_EQ(ASCIIToUTF16("P."), s2.ColumnString16(2)); 1120 EXPECT_EQ(ASCIIToUTF16("Doe"), s2.ColumnString16(3)); 1121 1122 // Dave Smith. 1123 ASSERT_TRUE(s2.Step()); 1124 EXPECT_EQ("4C74A9D8-7EEE-423E-F9C2-E7FA70ED1396", s2.ColumnString(0)); 1125 EXPECT_EQ(ASCIIToUTF16("Dave"), s2.ColumnString16(1)); 1126 EXPECT_EQ(ASCIIToUTF16(""), s2.ColumnString16(2)); 1127 EXPECT_EQ(ASCIIToUTF16("Smith"), s2.ColumnString16(3)); 1128 1129 // Dave Smith (Part 2). 1130 ASSERT_TRUE(s2.Step()); 1131 EXPECT_EQ("722DF5C4-F74A-294A-46F0-31FFDED0D635", s2.ColumnString(0)); 1132 EXPECT_EQ(ASCIIToUTF16("Dave"), s2.ColumnString16(1)); 1133 EXPECT_EQ(ASCIIToUTF16(""), s2.ColumnString16(2)); 1134 EXPECT_EQ(ASCIIToUTF16("Smith"), s2.ColumnString16(3)); 1135 1136 // Alfred E Newman. 1137 // Gets culled during migration from 35 to 36 due to incomplete address. 1138 1139 // 3 Main St. 1140 ASSERT_TRUE(s2.Step()); 1141 EXPECT_EQ("9E5FE298-62C7-83DF-6293-381BC589183F", s2.ColumnString(0)); 1142 EXPECT_EQ(ASCIIToUTF16(""), s2.ColumnString16(1)); 1143 EXPECT_EQ(ASCIIToUTF16(""), s2.ColumnString16(2)); 1144 EXPECT_EQ(ASCIIToUTF16(""), s2.ColumnString16(3)); 1145 1146 // Should be all. 1147 EXPECT_FALSE(s2.Step()); 1148 1149 sql::Statement s3( 1150 connection.GetUniqueStatement( 1151 "SELECT guid, email " 1152 "FROM autofill_profile_emails")); 1153 1154 // John Doe. 1155 ASSERT_TRUE(s3.Step()); 1156 EXPECT_EQ("00580526-FF81-EE2A-0546-1AC593A32E2F", s3.ColumnString(0)); 1157 EXPECT_EQ(ASCIIToUTF16("john (at) doe.com"), s3.ColumnString16(1)); 1158 1159 // John P. Doe. 1160 // Gets culled during migration from 35 to 37 due to merging of John Doe and 1161 // John P. Doe addresses. 1162 1163 // 2 Main Street. 1164 ASSERT_TRUE(s3.Step()); 1165 EXPECT_EQ("4C74A9D8-7EEE-423E-F9C2-E7FA70ED1396", s3.ColumnString(0)); 1166 EXPECT_EQ(ASCIIToUTF16(""), s3.ColumnString16(1)); 1167 1168 // 2 Main St. 1169 ASSERT_TRUE(s3.Step()); 1170 EXPECT_EQ("722DF5C4-F74A-294A-46F0-31FFDED0D635", s3.ColumnString(0)); 1171 EXPECT_EQ(ASCIIToUTF16(""), s3.ColumnString16(1)); 1172 1173 // Alfred E Newman. 1174 // Gets culled during migration from 35 to 36 due to incomplete address. 1175 1176 // 3 Main St. 1177 ASSERT_TRUE(s3.Step()); 1178 EXPECT_EQ("9E5FE298-62C7-83DF-6293-381BC589183F", s3.ColumnString(0)); 1179 EXPECT_EQ(ASCIIToUTF16(""), s3.ColumnString16(1)); 1180 1181 // Should be all. 1182 EXPECT_FALSE(s3.Step()); 1183 1184 sql::Statement s4( 1185 connection.GetUniqueStatement( 1186 "SELECT guid, type, number " 1187 "FROM autofill_profile_phones")); 1188 1189 // John Doe phone. 1190 ASSERT_TRUE(s4.Step()); 1191 EXPECT_EQ("00580526-FF81-EE2A-0546-1AC593A32E2F", s4.ColumnString(0)); 1192 EXPECT_EQ(0, s4.ColumnInt(1)); // 0 means phone. 1193 EXPECT_EQ(ASCIIToUTF16("4151112222"), s4.ColumnString16(2)); 1194 1195 // John Doe fax. 1196 ASSERT_TRUE(s4.Step()); 1197 EXPECT_EQ("00580526-FF81-EE2A-0546-1AC593A32E2F", s4.ColumnString(0)); 1198 EXPECT_EQ(1, s4.ColumnInt(1)); // 1 means phone. 1199 EXPECT_EQ(ASCIIToUTF16("4153334444"), s4.ColumnString16(2)); 1200 1201 // John P. Doe phone. 1202 // Gets culled during migration from 35 to 37 due to merging of John Doe and 1203 // John P. Doe addresses. 1204 1205 // John P. Doe fax. 1206 // Gets culled during migration from 35 to 37 due to merging of John Doe and 1207 // John P. Doe addresses. 1208 1209 // 2 Main Street phone. 1210 ASSERT_TRUE(s4.Step()); 1211 EXPECT_EQ("4C74A9D8-7EEE-423E-F9C2-E7FA70ED1396", s4.ColumnString(0)); 1212 EXPECT_EQ(0, s4.ColumnInt(1)); // 0 means phone. 1213 EXPECT_EQ(ASCIIToUTF16(""), s4.ColumnString16(2)); 1214 1215 // 2 Main Street fax. 1216 ASSERT_TRUE(s4.Step()); 1217 EXPECT_EQ("4C74A9D8-7EEE-423E-F9C2-E7FA70ED1396", s4.ColumnString(0)); 1218 EXPECT_EQ(1, s4.ColumnInt(1)); // 1 means fax. 1219 EXPECT_EQ(ASCIIToUTF16(""), s4.ColumnString16(2)); 1220 1221 // 2 Main St phone. 1222 ASSERT_TRUE(s4.Step()); 1223 EXPECT_EQ("722DF5C4-F74A-294A-46F0-31FFDED0D635", s4.ColumnString(0)); 1224 EXPECT_EQ(0, s4.ColumnInt(1)); // 0 means phone. 1225 EXPECT_EQ(ASCIIToUTF16(""), s4.ColumnString16(2)); 1226 1227 // 2 Main St fax. 1228 ASSERT_TRUE(s4.Step()); 1229 EXPECT_EQ("722DF5C4-F74A-294A-46F0-31FFDED0D635", s4.ColumnString(0)); 1230 EXPECT_EQ(1, s4.ColumnInt(1)); // 1 means fax. 1231 EXPECT_EQ(ASCIIToUTF16(""), s4.ColumnString16(2)); 1232 1233 // Note no phone or fax for Alfred E Newman. 1234 1235 // 3 Main St phone. 1236 ASSERT_TRUE(s4.Step()); 1237 EXPECT_EQ("9E5FE298-62C7-83DF-6293-381BC589183F", s4.ColumnString(0)); 1238 EXPECT_EQ(0, s4.ColumnInt(1)); // 0 means phone. 1239 EXPECT_EQ(ASCIIToUTF16(""), s4.ColumnString16(2)); 1240 1241 // 2 Main St fax. 1242 ASSERT_TRUE(s4.Step()); 1243 EXPECT_EQ("9E5FE298-62C7-83DF-6293-381BC589183F", s4.ColumnString(0)); 1244 EXPECT_EQ(1, s4.ColumnInt(1)); // 1 means fax. 1245 EXPECT_EQ(ASCIIToUTF16(""), s4.ColumnString16(2)); 1246 1247 // Should be all. 1248 EXPECT_FALSE(s4.Step()); 1249 } 1250 } 1251 1252 // Adds a column for the autofill profile's country code. 1253 TEST_F(WebDatabaseMigrationTest, MigrateVersion33ToCurrent) { 1254 // Initialize the database. 1255 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_33.sql"))); 1256 1257 // Verify pre-conditions. These are expectations for version 33 of the 1258 // database. 1259 { 1260 sql::Connection connection; 1261 ASSERT_TRUE(connection.Open(GetDatabasePath())); 1262 1263 EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", 1264 "country_code")); 1265 1266 // Check that the country value is the one we expect. 1267 sql::Statement s( 1268 connection.GetUniqueStatement("SELECT country FROM autofill_profiles")); 1269 1270 ASSERT_TRUE(s.Step()); 1271 std::string country = s.ColumnString(0); 1272 EXPECT_EQ("United States", country); 1273 } 1274 1275 // Load the database via the WebDatabase class and migrate the database to 1276 // the current version. 1277 { 1278 WebDatabase db; 1279 ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath())); 1280 } 1281 1282 // Verify post-conditions. These are expectations for current version of the 1283 // database. 1284 { 1285 sql::Connection connection; 1286 ASSERT_TRUE(connection.Open(GetDatabasePath())); 1287 1288 // Check version. 1289 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection)); 1290 1291 ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles", 1292 "country_code")); 1293 1294 // Check that the country code is properly converted. 1295 sql::Statement s(connection.GetUniqueStatement( 1296 "SELECT country_code FROM autofill_profiles")); 1297 1298 ASSERT_TRUE(s.Step()); 1299 std::string country_code = s.ColumnString(0); 1300 EXPECT_EQ("US", country_code); 1301 } 1302 } 1303 1304 // Cleans up bad country code "UK" in favor of good country code "GB". 1305 TEST_F(WebDatabaseMigrationTest, MigrateVersion34ToCurrent) { 1306 // Initialize the database. 1307 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_34.sql"))); 1308 1309 // Verify pre-conditions. These are expectations for version 34 of the 1310 // database. 1311 { 1312 sql::Connection connection; 1313 ASSERT_TRUE(connection.Open(GetDatabasePath())); 1314 1315 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", 1316 "country_code")); 1317 1318 // Check that the country_code value is the one we expect. 1319 sql::Statement s( 1320 connection.GetUniqueStatement("SELECT country_code " 1321 "FROM autofill_profiles")); 1322 1323 ASSERT_TRUE(s.Step()); 1324 std::string country_code = s.ColumnString(0); 1325 EXPECT_EQ("UK", country_code); 1326 1327 // Should have only one. 1328 ASSERT_FALSE(s.Step()); 1329 } 1330 1331 // Load the database via the WebDatabase class and migrate the database to 1332 // the current version. 1333 { 1334 WebDatabase db; 1335 ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath())); 1336 } 1337 1338 // Verify post-conditions. These are expectations for current version of the 1339 // database. 1340 { 1341 sql::Connection connection; 1342 ASSERT_TRUE(connection.Open(GetDatabasePath())); 1343 1344 // Check version. 1345 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection)); 1346 1347 ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles", 1348 "country_code")); 1349 1350 // Check that the country_code code is properly converted. 1351 sql::Statement s(connection.GetUniqueStatement( 1352 "SELECT country_code FROM autofill_profiles")); 1353 1354 ASSERT_TRUE(s.Step()); 1355 std::string country_code = s.ColumnString(0); 1356 EXPECT_EQ("GB", country_code); 1357 1358 // Should have only one. 1359 ASSERT_FALSE(s.Step()); 1360 } 1361 } 1362 1363 // Cleans up invalid profiles based on more agressive merging. Filters out 1364 // profiles that are subsets of other profiles, and profiles with invalid email, 1365 // state, and incomplete address. 1366 TEST_F(WebDatabaseMigrationTest, MigrateVersion35ToCurrent) { 1367 // Initialize the database. 1368 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_35.sql"))); 1369 1370 // Verify pre-conditions. These are expectations for version 34 of the 1371 // database. 1372 { 1373 sql::Connection connection; 1374 ASSERT_TRUE(connection.Open(GetDatabasePath())); 1375 1376 EXPECT_FALSE(connection.DoesTableExist("autofill_profiles_trash")); 1377 ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid")); 1378 1379 // Check that there are 6 profiles prior to merge. 1380 sql::Statement s( 1381 connection.GetUniqueStatement("SELECT guid FROM autofill_profiles")); 1382 int i = 0; 1383 while (s.Step()) 1384 ++i; 1385 EXPECT_EQ(6, i); 1386 } 1387 1388 // Load the database via the WebDatabase class and migrate the database to 1389 // the current version. 1390 { 1391 WebDatabase db; 1392 ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath())); 1393 } 1394 1395 // Verify post-conditions. These are expectations for current version of the 1396 // database. 1397 { 1398 sql::Connection connection; 1399 ASSERT_TRUE(connection.Open(GetDatabasePath())); 1400 1401 // Check version. 1402 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection)); 1403 1404 ASSERT_TRUE(connection.DoesTableExist("autofill_profiles_trash")); 1405 ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles_trash", "guid")); 1406 ASSERT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid")); 1407 1408 // Verify data in the database after the migration. 1409 sql::Statement s1( 1410 connection.GetUniqueStatement( 1411 "SELECT guid, company_name, address_line_1, address_line_2, " 1412 "city, state, zipcode, country, date_modified " 1413 "FROM autofill_profiles")); 1414 1415 // John Doe. 1416 ASSERT_TRUE(s1.Step()); 1417 EXPECT_EQ("00000000-0000-0000-0000-000000000001", s1.ColumnString(0)); 1418 EXPECT_EQ(ASCIIToUTF16("Acme Inc."), s1.ColumnString16(1)); 1419 EXPECT_EQ(ASCIIToUTF16("1 Main Street"), s1.ColumnString16(2)); 1420 EXPECT_EQ(ASCIIToUTF16("Apt 2"), s1.ColumnString16(3)); 1421 EXPECT_EQ(ASCIIToUTF16("San Francisco"), s1.ColumnString16(4)); 1422 EXPECT_EQ(ASCIIToUTF16("CA"), s1.ColumnString16(5)); 1423 EXPECT_EQ(ASCIIToUTF16("94102"), s1.ColumnString16(6)); 1424 EXPECT_EQ(ASCIIToUTF16("United States"), s1.ColumnString16(7)); 1425 EXPECT_EQ(1300131704, s1.ColumnInt64(8)); 1426 1427 // That should be it. 1428 ASSERT_FALSE(s1.Step()); 1429 1430 // Check that there 5 trashed profile after the merge. 1431 sql::Statement s2( 1432 connection.GetUniqueStatement("SELECT guid " 1433 "FROM autofill_profiles_trash")); 1434 ASSERT_TRUE(s2.Step()); 1435 EXPECT_EQ("00000000-0000-0000-0000-000000000002", s2.ColumnString(0)); 1436 1437 ASSERT_TRUE(s2.Step()); 1438 EXPECT_EQ("00000000-0000-0000-0000-000000000003", s2.ColumnString(0)); 1439 1440 ASSERT_TRUE(s2.Step()); 1441 EXPECT_EQ("00000000-0000-0000-0000-000000000004", s2.ColumnString(0)); 1442 1443 ASSERT_TRUE(s2.Step()); 1444 EXPECT_EQ("00000000-0000-0000-0000-000000000005", s2.ColumnString(0)); 1445 1446 ASSERT_TRUE(s2.Step()); 1447 EXPECT_EQ("00000000-0000-0000-0000-000000000006", s2.ColumnString(0)); 1448 1449 // That should be it. 1450 ASSERT_FALSE(s2.Step()); 1451 } 1452 } 1453