Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(49)

Side by Side Diff: chrome/browser/webdata/web_database_migration_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698