Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / webdata / common / web_database_migration_unittest.cc
bloba5f371bcb65f6b126d7139ea5ace57ed9d5c3c9f
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.
5 #include <string>
7 #include "base/files/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/guid.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/path_service.h"
12 #include "base/stl_util.h"
13 #include "base/strings/string16.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/time/time.h"
18 #include "base/values.h"
19 #include "components/autofill/core/browser/autofill_country.h"
20 #include "components/autofill/core/browser/autofill_profile.h"
21 #include "components/autofill/core/browser/autofill_type.h"
22 #include "components/autofill/core/browser/credit_card.h"
23 #include "components/autofill/core/browser/webdata/autofill_change.h"
24 #include "components/autofill/core/browser/webdata/autofill_entry.h"
25 #include "components/autofill/core/browser/webdata/autofill_table.h"
26 #include "components/password_manager/core/browser/webdata/logins_table.h"
27 #include "components/search_engines/keyword_table.h"
28 #include "components/signin/core/browser/webdata/token_service_table.h"
29 #include "components/webdata/common/web_database.h"
30 #include "sql/statement.h"
31 #include "testing/gtest/include/gtest/gtest.h"
33 using autofill::AutofillProfile;
34 using autofill::AutofillTable;
35 using autofill::CreditCard;
36 using base::ASCIIToUTF16;
37 using base::Time;
39 namespace {
41 std::string RemoveQuotes(const std::string& has_quotes) {
42 std::string no_quotes;
43 // SQLite quotes: http://www.sqlite.org/lang_keywords.html
44 base::RemoveChars(has_quotes, "\"[]`", &no_quotes);
45 return no_quotes;
48 } // anonymous namespace
50 // The WebDatabaseMigrationTest encapsulates testing of database migrations.
51 // Specifically, these tests are intended to exercise any schema changes in
52 // the WebDatabase and data migrations that occur in
53 // |WebDatabase::MigrateOldVersionsAsNeeded()|.
54 class WebDatabaseMigrationTest : public testing::Test {
55 public:
56 WebDatabaseMigrationTest() {}
57 ~WebDatabaseMigrationTest() override {}
59 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); }
61 // Load the database via the WebDatabase class and migrate the database to
62 // the current version.
63 void DoMigration() {
64 AutofillTable autofill_table;
65 KeywordTable keyword_table;
66 LoginsTable logins_table;
67 TokenServiceTable token_service_table;
69 WebDatabase db;
70 db.AddTable(&autofill_table);
71 db.AddTable(&keyword_table);
72 db.AddTable(&logins_table);
73 db.AddTable(&token_service_table);
75 // This causes the migration to occur.
76 ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath()));
79 protected:
80 // Current tested version number. When adding a migration in
81 // |WebDatabase::MigrateOldVersionsAsNeeded()| and changing the version number
82 // |kCurrentVersionNumber| this value should change to reflect the new version
83 // number and a new migration test added below.
84 static const int kCurrentTestedVersionNumber;
86 base::FilePath GetDatabasePath() {
87 const base::FilePath::CharType kWebDatabaseFilename[] =
88 FILE_PATH_LITERAL("TestWebDatabase.sqlite3");
89 return temp_dir_.path().Append(base::FilePath(kWebDatabaseFilename));
92 // The textual contents of |file| are read from
93 // "components/test/data/web_database" and returned in the string |contents|.
94 // Returns true if the file exists and is read successfully, false otherwise.
95 bool GetWebDatabaseData(const base::FilePath& file, std::string* contents) {
96 base::FilePath source_path;
97 PathService::Get(base::DIR_SOURCE_ROOT, &source_path);
98 source_path = source_path.AppendASCII("components");
99 source_path = source_path.AppendASCII("test");
100 source_path = source_path.AppendASCII("data");
101 source_path = source_path.AppendASCII("web_database");
102 source_path = source_path.Append(file);
103 return base::PathExists(source_path) &&
104 base::ReadFileToString(source_path, contents);
107 static int VersionFromConnection(sql::Connection* connection) {
108 // Get version.
109 sql::Statement s(connection->GetUniqueStatement(
110 "SELECT value FROM meta WHERE key='version'"));
111 if (!s.Step())
112 return 0;
113 return s.ColumnInt(0);
116 // The sql files located in "components/test/data/web_database" were generated
117 // by launching the Chromium application prior to schema change, then using
118 // the sqlite3 command-line application to dump the contents of the "Web Data"
119 // database.
120 // Like this:
121 // > .output version_nn.sql
122 // > .dump
123 void LoadDatabase(const base::FilePath::StringType& file);
125 private:
126 base::ScopedTempDir temp_dir_;
128 DISALLOW_COPY_AND_ASSIGN(WebDatabaseMigrationTest);
131 const int WebDatabaseMigrationTest::kCurrentTestedVersionNumber = 65;
133 void WebDatabaseMigrationTest::LoadDatabase(
134 const base::FilePath::StringType& file) {
135 std::string contents;
136 ASSERT_TRUE(GetWebDatabaseData(base::FilePath(file), &contents));
138 sql::Connection connection;
139 ASSERT_TRUE(connection.Open(GetDatabasePath()));
140 ASSERT_TRUE(connection.Execute(contents.data()));
143 // Tests that migrating from the golden files version_XX.sql results in the same
144 // schema as migrating from an empty database.
145 TEST_F(WebDatabaseMigrationTest, VersionXxSqlFilesAreGolden) {
146 DoMigration();
147 sql::Connection connection;
148 ASSERT_TRUE(connection.Open(GetDatabasePath()));
149 const std::string& expected_schema = RemoveQuotes(connection.GetSchema());
150 for (int i = WebDatabase::kDeprecatedVersionNumber + 1;
151 i < kCurrentTestedVersionNumber; ++i) {
152 // We don't test version 52 because there's a slight discrepancy in the
153 // initialization code and the migration code (relating to schema
154 // formatting). Fixing the bug is possible, but would require updating every
155 // version_nn.sql file.
156 if (i == 52)
157 continue;
159 connection.Raze();
160 const base::FilePath& file_name = base::FilePath::FromUTF8Unsafe(
161 "version_" + base::IntToString(i) + ".sql");
162 ASSERT_NO_FATAL_FAILURE(LoadDatabase(file_name.value()))
163 << "Failed to load " << file_name.MaybeAsASCII();
164 DoMigration();
165 EXPECT_EQ(expected_schema, RemoveQuotes(connection.GetSchema()))
166 << "For version " << i;
170 // Tests that the all migrations from an empty database succeed.
171 TEST_F(WebDatabaseMigrationTest, MigrateEmptyToCurrent) {
172 DoMigration();
174 // Verify post-conditions. These are expectations for current version of the
175 // database.
177 sql::Connection connection;
178 ASSERT_TRUE(connection.Open(GetDatabasePath()));
180 // Check version.
181 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
183 // Check that expected tables are present.
184 EXPECT_TRUE(connection.DoesTableExist("autofill"));
185 // The autofill_dates table is obsolete. (It's been merged into the autofill
186 // table.)
187 EXPECT_FALSE(connection.DoesTableExist("autofill_dates"));
188 EXPECT_TRUE(connection.DoesTableExist("autofill_profiles"));
189 EXPECT_TRUE(connection.DoesTableExist("credit_cards"));
190 EXPECT_TRUE(connection.DoesTableExist("keywords"));
191 // The logins table is obsolete. (We used to store saved passwords here.)
192 EXPECT_FALSE(connection.DoesTableExist("logins"));
193 EXPECT_TRUE(connection.DoesTableExist("meta"));
194 EXPECT_TRUE(connection.DoesTableExist("token_service"));
195 // The web_apps and web_apps_icons tables are obsolete as of version 58.
196 EXPECT_FALSE(connection.DoesTableExist("web_apps"));
197 EXPECT_FALSE(connection.DoesTableExist("web_app_icons"));
198 // The web_intents and web_intents_defaults tables are obsolete as of
199 // version 58.
200 EXPECT_FALSE(connection.DoesTableExist("web_intents"));
201 EXPECT_FALSE(connection.DoesTableExist("web_intents_defaults"));
205 // Versions below 52 are deprecated. This verifies that old databases are razed.
206 TEST_F(WebDatabaseMigrationTest, RazeDeprecatedVersionAndReinit) {
207 ASSERT_NO_FATAL_FAILURE(
208 LoadDatabase(FILE_PATH_LITERAL("version_50.sql")));
210 // Verify pre-conditions. These are expectations for version 50 of the
211 // database.
213 sql::Connection connection;
214 ASSERT_TRUE(connection.Open(GetDatabasePath()));
215 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
217 sql::MetaTable meta_table;
218 ASSERT_TRUE(meta_table.Init(&connection, 50, 50));
220 ASSERT_FALSE(connection.DoesColumnExist("keywords", "image_url"));
221 ASSERT_FALSE(connection.DoesColumnExist("keywords",
222 "search_url_post_params"));
223 ASSERT_FALSE(connection.DoesColumnExist("keywords",
224 "suggest_url_post_params"));
225 ASSERT_FALSE(connection.DoesColumnExist("keywords",
226 "instant_url_post_params"));
227 ASSERT_FALSE(connection.DoesColumnExist("keywords",
228 "image_url_post_params"));
231 DoMigration();
233 // Verify post-conditions. These are expectations for current version of the
234 // database.
236 sql::Connection connection;
237 ASSERT_TRUE(connection.Open(GetDatabasePath()));
238 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
240 // Check version.
241 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
243 // New columns should have been created.
244 EXPECT_TRUE(connection.DoesColumnExist("keywords", "image_url"));
245 EXPECT_TRUE(connection.DoesColumnExist("keywords",
246 "search_url_post_params"));
247 EXPECT_TRUE(connection.DoesColumnExist("keywords",
248 "suggest_url_post_params"));
249 EXPECT_TRUE(connection.DoesColumnExist("keywords",
250 "instant_url_post_params"));
251 EXPECT_TRUE(connection.DoesColumnExist("keywords",
252 "image_url_post_params"));
256 // Tests that the column |new_tab_url| is added to the keyword table schema for
257 // a version 52 database.
258 TEST_F(WebDatabaseMigrationTest, MigrateVersion52ToCurrent) {
259 ASSERT_NO_FATAL_FAILURE(
260 LoadDatabase(FILE_PATH_LITERAL("version_52.sql")));
262 // Verify pre-conditions. These are expectations for version 52 of the
263 // database.
265 sql::Connection connection;
266 ASSERT_TRUE(connection.Open(GetDatabasePath()));
267 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
269 sql::MetaTable meta_table;
270 ASSERT_TRUE(meta_table.Init(&connection, 52, 52));
272 ASSERT_FALSE(connection.DoesColumnExist("keywords", "new_tab_url"));
275 DoMigration();
277 // Verify post-conditions. These are expectations for current version of the
278 // database.
280 sql::Connection connection;
281 ASSERT_TRUE(connection.Open(GetDatabasePath()));
282 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
284 // Check version.
285 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
287 // New columns should have been created.
288 EXPECT_TRUE(connection.DoesColumnExist("keywords", "new_tab_url"));
292 // Tests that for a version 54 database,
293 // (a) The street_address, dependent_locality, and sorting_code columns are
294 // added to the autofill_profiles table schema.
295 // (b) The address_line1, address_line2, and country columns are dropped from
296 // the autofill_profiles table schema.
297 // (c) The type column is dropped from the autofill_profile_phones schema.
298 TEST_F(WebDatabaseMigrationTest, MigrateVersion53ToCurrent) {
299 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_53.sql")));
301 // Verify pre-conditions. These are expectations for version 53 of the
302 // database.
304 sql::Connection connection;
305 ASSERT_TRUE(connection.Open(GetDatabasePath()));
307 EXPECT_TRUE(
308 connection.DoesColumnExist("autofill_profiles", "address_line_1"));
309 EXPECT_TRUE(
310 connection.DoesColumnExist("autofill_profiles", "address_line_2"));
311 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "country"));
312 EXPECT_FALSE(
313 connection.DoesColumnExist("autofill_profiles", "street_address"));
314 EXPECT_FALSE(
315 connection.DoesColumnExist("autofill_profiles", "dependent_locality"));
316 EXPECT_FALSE(
317 connection.DoesColumnExist("autofill_profiles", "sorting_code"));
318 EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_phones", "type"));
321 DoMigration();
323 // Verify post-conditions. These are expectations for current version of the
324 // database.
326 sql::Connection connection;
327 ASSERT_TRUE(connection.Open(GetDatabasePath()));
328 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
330 // Check version.
331 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
333 // Columns should have been added and removed appropriately.
334 EXPECT_FALSE(
335 connection.DoesColumnExist("autofill_profiles", "address_line1"));
336 EXPECT_FALSE(
337 connection.DoesColumnExist("autofill_profiles", "address_line2"));
338 EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "country"));
339 EXPECT_TRUE(
340 connection.DoesColumnExist("autofill_profiles", "street_address"));
341 EXPECT_TRUE(
342 connection.DoesColumnExist("autofill_profiles", "dependent_locality"));
343 EXPECT_TRUE(
344 connection.DoesColumnExist("autofill_profiles", "sorting_code"));
345 EXPECT_FALSE(connection.DoesColumnExist("autofill_profile_phones", "type"));
347 // Data should have been preserved.
348 sql::Statement s_profiles(
349 connection.GetUniqueStatement(
350 "SELECT guid, company_name, street_address, dependent_locality,"
351 " city, state, zipcode, sorting_code, country_code, date_modified,"
352 " origin "
353 "FROM autofill_profiles"));
355 // Address lines 1 and 2.
356 ASSERT_TRUE(s_profiles.Step());
357 EXPECT_EQ("00000000-0000-0000-0000-000000000001",
358 s_profiles.ColumnString(0));
359 EXPECT_EQ(ASCIIToUTF16("Google, Inc."), s_profiles.ColumnString16(1));
360 EXPECT_EQ(ASCIIToUTF16("1950 Charleston Rd.\n"
361 "(2nd floor)"),
362 s_profiles.ColumnString16(2));
363 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(3));
364 EXPECT_EQ(ASCIIToUTF16("Mountain View"), s_profiles.ColumnString16(4));
365 EXPECT_EQ(ASCIIToUTF16("CA"), s_profiles.ColumnString16(5));
366 EXPECT_EQ(ASCIIToUTF16("94043"), s_profiles.ColumnString16(6));
367 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(7));
368 EXPECT_EQ(ASCIIToUTF16("US"), s_profiles.ColumnString16(8));
369 EXPECT_EQ(1386046731, s_profiles.ColumnInt(9));
370 EXPECT_EQ(ASCIIToUTF16("Chrome settings"), s_profiles.ColumnString16(10));
372 // Only address line 1.
373 ASSERT_TRUE(s_profiles.Step());
374 EXPECT_EQ("00000000-0000-0000-0000-000000000002",
375 s_profiles.ColumnString(0));
376 EXPECT_EQ(ASCIIToUTF16("Google!"), s_profiles.ColumnString16(1));
377 EXPECT_EQ(ASCIIToUTF16("1600 Amphitheatre Pkwy."),
378 s_profiles.ColumnString16(2));
379 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(3));
380 EXPECT_EQ(ASCIIToUTF16("Mtn. View"), s_profiles.ColumnString16(4));
381 EXPECT_EQ(ASCIIToUTF16("California"), s_profiles.ColumnString16(5));
382 EXPECT_EQ(ASCIIToUTF16("94043-1234"), s_profiles.ColumnString16(6));
383 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(7));
384 EXPECT_EQ(ASCIIToUTF16("US"), s_profiles.ColumnString16(8));
385 EXPECT_EQ(1386046800, s_profiles.ColumnInt(9));
386 EXPECT_EQ(ASCIIToUTF16("Chrome settings"), s_profiles.ColumnString16(10));
388 // Only address line 2.
389 ASSERT_TRUE(s_profiles.Step());
390 EXPECT_EQ("00000000-0000-0000-0000-000000000003",
391 s_profiles.ColumnString(0));
392 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(1));
393 EXPECT_EQ(ASCIIToUTF16("\nOnly line 2???"), s_profiles.ColumnString16(2));
394 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(3));
395 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(4));
396 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(5));
397 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(6));
398 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(7));
399 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(8));
400 EXPECT_EQ(1386046834, s_profiles.ColumnInt(9));
401 EXPECT_EQ(ASCIIToUTF16("Chrome settings"), s_profiles.ColumnString16(10));
403 // No address lines.
404 ASSERT_TRUE(s_profiles.Step());
405 EXPECT_EQ("00000000-0000-0000-0000-000000000004",
406 s_profiles.ColumnString(0));
407 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(1));
408 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(2));
409 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(3));
410 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(4));
411 EXPECT_EQ(ASCIIToUTF16("Texas"), s_profiles.ColumnString16(5));
412 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(6));
413 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(7));
414 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(8));
415 EXPECT_EQ(1386046847, s_profiles.ColumnInt(9));
416 EXPECT_EQ(ASCIIToUTF16("Chrome settings"), s_profiles.ColumnString16(10));
418 // That should be it.
419 EXPECT_FALSE(s_profiles.Step());
421 // Verify the phone number data as well.
422 sql::Statement s_phones(
423 connection.GetUniqueStatement(
424 "SELECT guid, number FROM autofill_profile_phones"));
426 ASSERT_TRUE(s_phones.Step());
427 EXPECT_EQ("00000000-0000-0000-0000-000000000001", s_phones.ColumnString(0));
428 EXPECT_EQ(ASCIIToUTF16("1.800.555.1234"), s_phones.ColumnString16(1));
430 ASSERT_TRUE(s_phones.Step());
431 EXPECT_EQ("00000000-0000-0000-0000-000000000001", s_phones.ColumnString(0));
432 EXPECT_EQ(ASCIIToUTF16("+1 (800) 555-4321"), s_phones.ColumnString16(1));
434 ASSERT_TRUE(s_phones.Step());
435 EXPECT_EQ("00000000-0000-0000-0000-000000000002", s_phones.ColumnString(0));
436 EXPECT_EQ(base::string16(), s_phones.ColumnString16(1));
438 ASSERT_TRUE(s_phones.Step());
439 EXPECT_EQ("00000000-0000-0000-0000-000000000003", s_phones.ColumnString(0));
440 EXPECT_EQ(ASCIIToUTF16("6505557890"), s_phones.ColumnString16(1));
442 ASSERT_TRUE(s_phones.Step());
443 EXPECT_EQ("00000000-0000-0000-0000-000000000004", s_phones.ColumnString(0));
444 EXPECT_EQ(base::string16(), s_phones.ColumnString16(1));
446 EXPECT_FALSE(s_phones.Step());
450 // Tests that migrating from version 54 to version 55 drops the autofill_dates
451 // table, and merges the appropriate dates into the autofill table.
452 TEST_F(WebDatabaseMigrationTest, MigrateVersion54ToCurrent) {
453 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_54.sql")));
455 // Verify pre-conditions. These are expectations for version 54 of the
456 // database.
458 sql::Connection connection;
459 ASSERT_TRUE(connection.Open(GetDatabasePath()));
461 EXPECT_TRUE(connection.DoesTableExist("autofill_dates"));
462 EXPECT_FALSE(connection.DoesColumnExist("autofill", "date_created"));
463 EXPECT_FALSE(connection.DoesColumnExist("autofill", "date_last_used"));
465 // Verify the incoming data.
466 sql::Statement s_autofill(connection.GetUniqueStatement(
467 "SELECT name, value, value_lower, pair_id, count FROM autofill"));
468 sql::Statement s_dates(connection.GetUniqueStatement(
469 "SELECT pair_id, date_created FROM autofill_dates"));
471 // An entry with one timestamp.
472 ASSERT_TRUE(s_autofill.Step());
473 EXPECT_EQ(ASCIIToUTF16("Name"), s_autofill.ColumnString16(0));
474 EXPECT_EQ(ASCIIToUTF16("John Doe"), s_autofill.ColumnString16(1));
475 EXPECT_EQ(ASCIIToUTF16("john doe"), s_autofill.ColumnString16(2));
476 EXPECT_EQ(10, s_autofill.ColumnInt(3));
477 EXPECT_EQ(1, s_autofill.ColumnInt(4));
478 ASSERT_TRUE(s_dates.Step());
479 EXPECT_EQ(10, s_dates.ColumnInt(0));
480 EXPECT_EQ(1384299100, s_dates.ColumnInt64(1));
482 // Another entry with one timestamp, differing from the previous one in case
483 // only.
484 ASSERT_TRUE(s_autofill.Step());
485 EXPECT_EQ(ASCIIToUTF16("Name"), s_autofill.ColumnString16(0));
486 EXPECT_EQ(ASCIIToUTF16("john doe"), s_autofill.ColumnString16(1));
487 EXPECT_EQ(ASCIIToUTF16("john doe"), s_autofill.ColumnString16(2));
488 EXPECT_EQ(11, s_autofill.ColumnInt(3));
489 EXPECT_EQ(1, s_autofill.ColumnInt(4));
490 ASSERT_TRUE(s_dates.Step());
491 EXPECT_EQ(11, s_dates.ColumnInt(0));
492 EXPECT_EQ(1384299200, s_dates.ColumnInt64(1));
494 // An entry with two timestamps (with count > 2; this is realistic).
495 ASSERT_TRUE(s_autofill.Step());
496 EXPECT_EQ(ASCIIToUTF16("Email"), s_autofill.ColumnString16(0));
497 EXPECT_EQ(ASCIIToUTF16("jane@example.com"), s_autofill.ColumnString16(1));
498 EXPECT_EQ(ASCIIToUTF16("jane@example.com"), s_autofill.ColumnString16(2));
499 EXPECT_EQ(20, s_autofill.ColumnInt(3));
500 EXPECT_EQ(3, s_autofill.ColumnInt(4));
501 ASSERT_TRUE(s_dates.Step());
502 EXPECT_EQ(20, s_dates.ColumnInt(0));
503 EXPECT_EQ(1384299300, s_dates.ColumnInt64(1));
504 ASSERT_TRUE(s_dates.Step());
505 EXPECT_EQ(20, s_dates.ColumnInt(0));
506 EXPECT_EQ(1384299301, s_dates.ColumnInt64(1));
508 // An entry with more than two timestamps, which are stored out of order.
509 ASSERT_TRUE(s_autofill.Step());
510 EXPECT_EQ(ASCIIToUTF16("Email"), s_autofill.ColumnString16(0));
511 EXPECT_EQ(ASCIIToUTF16("jane.doe@example.org"),
512 s_autofill.ColumnString16(1));
513 EXPECT_EQ(ASCIIToUTF16("jane.doe@example.org"),
514 s_autofill.ColumnString16(2));
515 EXPECT_EQ(21, s_autofill.ColumnInt(3));
516 EXPECT_EQ(4, s_autofill.ColumnInt(4));
517 ASSERT_TRUE(s_dates.Step());
518 EXPECT_EQ(21, s_dates.ColumnInt(0));
519 EXPECT_EQ(1384299401, s_dates.ColumnInt64(1));
520 ASSERT_TRUE(s_dates.Step());
521 EXPECT_EQ(21, s_dates.ColumnInt(0));
522 EXPECT_EQ(1384299400, s_dates.ColumnInt64(1));
523 ASSERT_TRUE(s_dates.Step());
524 EXPECT_EQ(21, s_dates.ColumnInt(0));
525 EXPECT_EQ(1384299403, s_dates.ColumnInt64(1));
526 ASSERT_TRUE(s_dates.Step());
527 EXPECT_EQ(21, s_dates.ColumnInt(0));
528 EXPECT_EQ(1384299402, s_dates.ColumnInt64(1));
530 // No more entries expected.
531 ASSERT_FALSE(s_autofill.Step());
532 ASSERT_FALSE(s_dates.Step());
535 DoMigration();
537 // Verify post-conditions. These are expectations for current version of the
538 // database.
540 sql::Connection connection;
541 ASSERT_TRUE(connection.Open(GetDatabasePath()));
542 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
544 // Check version.
545 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
547 // The autofill_dates table should have been dropped, and its columns should
548 // have been migrated to the autofill table.
549 EXPECT_FALSE(connection.DoesTableExist("autofill_dates"));
550 EXPECT_TRUE(connection.DoesColumnExist("autofill", "date_created"));
551 EXPECT_TRUE(connection.DoesColumnExist("autofill", "date_last_used"));
553 // Data should have been preserved. Note that it appears out of order
554 // relative to the previous table, as it's been alphabetized. That's ok.
555 sql::Statement s(
556 connection.GetUniqueStatement(
557 "SELECT name, value, value_lower, date_created, date_last_used,"
558 " count "
559 "FROM autofill "
560 "ORDER BY name, value ASC"));
562 // "jane.doe@example.org": Timestamps should be parsed correctly, and only
563 // the first and last should be kept.
564 ASSERT_TRUE(s.Step());
565 EXPECT_EQ(ASCIIToUTF16("Email"), s.ColumnString16(0));
566 EXPECT_EQ(ASCIIToUTF16("jane.doe@example.org"), s.ColumnString16(1));
567 EXPECT_EQ(ASCIIToUTF16("jane.doe@example.org"), s.ColumnString16(2));
568 EXPECT_EQ(1384299400, s.ColumnInt64(3));
569 EXPECT_EQ(1384299403, s.ColumnInt64(4));
570 EXPECT_EQ(4, s.ColumnInt(5));
572 // "jane@example.com": Timestamps should be parsed correctly.
573 ASSERT_TRUE(s.Step());
574 EXPECT_EQ(ASCIIToUTF16("Email"), s.ColumnString16(0));
575 EXPECT_EQ(ASCIIToUTF16("jane@example.com"), s.ColumnString16(1));
576 EXPECT_EQ(ASCIIToUTF16("jane@example.com"), s.ColumnString16(2));
577 EXPECT_EQ(1384299300, s.ColumnInt64(3));
578 EXPECT_EQ(1384299301, s.ColumnInt64(4));
579 EXPECT_EQ(3, s.ColumnInt(5));
581 // "John Doe": The single timestamp should be assigned as both the creation
582 // and the last use timestamp.
583 ASSERT_TRUE(s.Step());
584 EXPECT_EQ(ASCIIToUTF16("Name"), s.ColumnString16(0));
585 EXPECT_EQ(ASCIIToUTF16("John Doe"), s.ColumnString16(1));
586 EXPECT_EQ(ASCIIToUTF16("john doe"), s.ColumnString16(2));
587 EXPECT_EQ(1384299100, s.ColumnInt64(3));
588 EXPECT_EQ(1384299100, s.ColumnInt64(4));
589 EXPECT_EQ(1, s.ColumnInt(5));
591 // "john doe": Should not be merged with "John Doe" (case-sensitivity).
592 ASSERT_TRUE(s.Step());
593 EXPECT_EQ(ASCIIToUTF16("Name"), s.ColumnString16(0));
594 EXPECT_EQ(ASCIIToUTF16("john doe"), s.ColumnString16(1));
595 EXPECT_EQ(ASCIIToUTF16("john doe"), s.ColumnString16(2));
596 EXPECT_EQ(1384299200, s.ColumnInt64(3));
597 EXPECT_EQ(1384299200, s.ColumnInt64(4));
598 EXPECT_EQ(1, s.ColumnInt(5));
600 // No more entries expected.
601 ASSERT_FALSE(s.Step());
605 // Tests that migrating from version 55 to version 56 adds the language_code
606 // column to autofill_profiles table.
607 TEST_F(WebDatabaseMigrationTest, MigrateVersion55ToCurrent) {
608 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_55.sql")));
610 // Verify pre-conditions. These are expectations for version 55 of the
611 // database.
613 sql::Connection connection;
614 ASSERT_TRUE(connection.Open(GetDatabasePath()));
615 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
617 EXPECT_FALSE(
618 connection.DoesColumnExist("autofill_profiles", "language_code"));
621 DoMigration();
623 // Verify post-conditions. These are expectations for current version of the
624 // database.
626 sql::Connection connection;
627 ASSERT_TRUE(connection.Open(GetDatabasePath()));
628 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
630 // Check version.
631 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
633 // The language_code column should have been added to autofill_profiles
634 // table.
635 EXPECT_TRUE(
636 connection.DoesColumnExist("autofill_profiles", "language_code"));
638 // Data should have been preserved. Language code should have been set to
639 // empty string.
640 sql::Statement s_profiles(
641 connection.GetUniqueStatement(
642 "SELECT guid, company_name, street_address, dependent_locality,"
643 " city, state, zipcode, sorting_code, country_code, date_modified,"
644 " origin, language_code "
645 "FROM autofill_profiles"));
647 ASSERT_TRUE(s_profiles.Step());
648 EXPECT_EQ("00000000-0000-0000-0000-000000000001",
649 s_profiles.ColumnString(0));
650 EXPECT_EQ(ASCIIToUTF16("Google Inc"), s_profiles.ColumnString16(1));
651 EXPECT_EQ(ASCIIToUTF16("340 Main St"),
652 s_profiles.ColumnString16(2));
653 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(3));
654 EXPECT_EQ(ASCIIToUTF16("Los Angeles"), s_profiles.ColumnString16(4));
655 EXPECT_EQ(ASCIIToUTF16("CA"), s_profiles.ColumnString16(5));
656 EXPECT_EQ(ASCIIToUTF16("90291"), s_profiles.ColumnString16(6));
657 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(7));
658 EXPECT_EQ(ASCIIToUTF16("US"), s_profiles.ColumnString16(8));
659 EXPECT_EQ(1395948829, s_profiles.ColumnInt(9));
660 EXPECT_EQ(ASCIIToUTF16("Chrome settings"), s_profiles.ColumnString16(10));
661 EXPECT_EQ(std::string(), s_profiles.ColumnString(11));
663 // No more entries expected.
664 ASSERT_FALSE(s_profiles.Step());
668 // Tests that migrating from version 56 to version 57 adds the full_name
669 // column to autofill_profile_names table.
670 TEST_F(WebDatabaseMigrationTest, MigrateVersion56ToCurrent) {
671 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_56.sql")));
673 // Verify pre-conditions. These are expectations for version 56 of the
674 // database.
676 sql::Connection connection;
677 ASSERT_TRUE(connection.Open(GetDatabasePath()));
678 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
680 EXPECT_FALSE(
681 connection.DoesColumnExist("autofill_profile_names", "full_name"));
683 // Verify the starting data.
684 sql::Statement s_names(
685 connection.GetUniqueStatement(
686 "SELECT guid, first_name, middle_name, last_name "
687 "FROM autofill_profile_names"));
688 ASSERT_TRUE(s_names.Step());
689 EXPECT_EQ("B41FE6E0-B13E-2A2A-BF0B-29FCE2C3ADBD", s_names.ColumnString(0));
690 EXPECT_EQ(ASCIIToUTF16("Jon"), s_names.ColumnString16(1));
691 EXPECT_EQ(base::string16(), s_names.ColumnString16(2));
692 EXPECT_EQ(ASCIIToUTF16("Smith"), s_names.ColumnString16(3));
695 DoMigration();
697 // Verify post-conditions. These are expectations for current version of the
698 // database.
700 sql::Connection connection;
701 ASSERT_TRUE(connection.Open(GetDatabasePath()));
702 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
704 // Check version.
705 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
707 // The full_name column should have been added to autofill_profile_names
708 // table.
709 EXPECT_TRUE(
710 connection.DoesColumnExist("autofill_profile_names", "full_name"));
712 // Data should have been preserved. Full name should have been set to the
713 // empty string.
714 sql::Statement s_names(
715 connection.GetUniqueStatement(
716 "SELECT guid, first_name, middle_name, last_name, full_name "
717 "FROM autofill_profile_names"));
719 ASSERT_TRUE(s_names.Step());
720 EXPECT_EQ("B41FE6E0-B13E-2A2A-BF0B-29FCE2C3ADBD", s_names.ColumnString(0));
721 EXPECT_EQ(ASCIIToUTF16("Jon"), s_names.ColumnString16(1));
722 EXPECT_EQ(base::string16(), s_names.ColumnString16(2));
723 EXPECT_EQ(ASCIIToUTF16("Smith"), s_names.ColumnString16(3));
724 EXPECT_EQ(base::string16(), s_names.ColumnString16(4));
726 // No more entries expected.
727 ASSERT_FALSE(s_names.Step());
731 // Tests that migrating from version 57 to version 58 drops the web_intents and
732 // web_apps tables.
733 TEST_F(WebDatabaseMigrationTest, MigrateVersion57ToCurrent) {
734 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_57.sql")));
736 // Verify pre-conditions. These are expectations for version 57 of the
737 // database.
739 sql::Connection connection;
740 ASSERT_TRUE(connection.Open(GetDatabasePath()));
741 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
743 EXPECT_TRUE(connection.DoesTableExist("web_apps"));
744 EXPECT_TRUE(connection.DoesTableExist("web_app_icons"));
745 EXPECT_TRUE(connection.DoesTableExist("web_intents"));
746 EXPECT_TRUE(connection.DoesTableExist("web_intents_defaults"));
749 DoMigration();
751 // Verify post-conditions. These are expectations for current version of the
752 // database.
754 sql::Connection connection;
755 ASSERT_TRUE(connection.Open(GetDatabasePath()));
756 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
758 // Check version.
759 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
761 EXPECT_FALSE(connection.DoesTableExist("web_apps"));
762 EXPECT_FALSE(connection.DoesTableExist("web_app_icons"));
763 EXPECT_FALSE(connection.DoesTableExist("web_intents"));
764 EXPECT_FALSE(connection.DoesTableExist("web_intents_defaults"));
768 // Tests that migrating from version 58 to version 59 drops the omnibox
769 // extension keywords.
770 TEST_F(WebDatabaseMigrationTest, MigrateVersion58ToCurrent) {
771 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_58.sql")));
773 const char query_extensions[] = "SELECT * FROM keywords "
774 "WHERE url='chrome-extension://iphchnegaodmijmkdlbhbanjhfphhikp/"
775 "?q={searchTerms}'";
776 // Verify pre-conditions.
778 sql::Connection connection;
779 ASSERT_TRUE(connection.Open(GetDatabasePath()));
780 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
782 sql::MetaTable meta_table;
783 ASSERT_TRUE(meta_table.Init(&connection, 58, 58));
785 sql::Statement s(connection.GetUniqueStatement(query_extensions));
786 ASSERT_TRUE(s.is_valid());
787 int count = 0;
788 while (s.Step()) {
789 ++count;
791 EXPECT_EQ(1, count);
794 DoMigration();
796 // Verify post-conditions.
798 sql::Connection connection;
799 ASSERT_TRUE(connection.Open(GetDatabasePath()));
800 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
802 // Check version.
803 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
805 sql::Statement s(connection.GetUniqueStatement(query_extensions));
806 ASSERT_TRUE(s.is_valid());
807 int count = 0;
808 while (s.Step()) {
809 ++count;
811 EXPECT_EQ(0, count);
813 s.Assign(connection.GetUniqueStatement("SELECT * FROM keywords "
814 "WHERE short_name='Google'"));
815 ASSERT_TRUE(s.is_valid());
816 count = 0;
817 while (s.Step()) {
818 ++count;
820 EXPECT_EQ(1, count);
824 // Tests creation of the server_credit_cards table.
825 TEST_F(WebDatabaseMigrationTest, MigrateVersion59ToCurrent) {
826 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_59.sql")));
828 // Verify pre-conditions.
830 sql::Connection connection;
831 ASSERT_TRUE(connection.Open(GetDatabasePath()));
832 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
834 sql::MetaTable meta_table;
835 ASSERT_TRUE(meta_table.Init(&connection, 59, 59));
837 ASSERT_FALSE(connection.DoesTableExist("masked_credit_cards"));
838 ASSERT_FALSE(connection.DoesTableExist("unmasked_credit_cards"));
839 ASSERT_FALSE(connection.DoesTableExist("server_addresses"));
842 DoMigration();
844 // Verify post-conditions.
846 sql::Connection connection;
847 ASSERT_TRUE(connection.Open(GetDatabasePath()));
848 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
850 // Check version.
851 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
853 ASSERT_TRUE(connection.DoesTableExist("masked_credit_cards"));
854 ASSERT_TRUE(connection.DoesTableExist("unmasked_credit_cards"));
855 ASSERT_TRUE(connection.DoesTableExist("server_addresses"));
859 // Tests addition of use_count and use_date fields to autofill profiles and
860 // credit cards.
861 TEST_F(WebDatabaseMigrationTest, MigrateVersion60ToCurrent) {
862 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_60.sql")));
864 // Verify pre-conditions.
866 sql::Connection connection;
867 ASSERT_TRUE(connection.Open(GetDatabasePath()));
868 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
870 sql::MetaTable meta_table;
871 ASSERT_TRUE(meta_table.Init(&connection, 60, 60));
873 EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "use_count"));
874 EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "use_date"));
875 EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "use_count"));
876 EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "use_date"));
879 DoMigration();
881 // Verify post-conditions.
883 sql::Connection connection;
884 ASSERT_TRUE(connection.Open(GetDatabasePath()));
885 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
887 // Check version.
888 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
890 EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "use_count"));
891 EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "use_date"));
892 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "use_count"));
893 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "use_date"));
897 // Tests addition of use_count and use_date fields to unmasked server cards.
898 TEST_F(WebDatabaseMigrationTest, MigrateVersion61ToCurrent) {
899 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_61.sql")));
901 // Verify pre-conditions.
903 sql::Connection connection;
904 ASSERT_TRUE(connection.Open(GetDatabasePath()));
905 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
907 sql::MetaTable meta_table;
908 ASSERT_TRUE(meta_table.Init(&connection, 61, 61));
910 EXPECT_FALSE(connection.DoesColumnExist("unmasked_credit_cards",
911 "use_count"));
912 EXPECT_FALSE(connection.DoesColumnExist("unmasked_credit_cards",
913 "use_date"));
916 DoMigration();
918 // Verify post-conditions.
920 sql::Connection connection;
921 ASSERT_TRUE(connection.Open(GetDatabasePath()));
922 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
924 // Check version.
925 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
927 EXPECT_TRUE(connection.DoesColumnExist("unmasked_credit_cards",
928 "use_count"));
929 EXPECT_TRUE(connection.DoesColumnExist("unmasked_credit_cards",
930 "use_date"));
934 // Tests addition of server metadata tables.
935 TEST_F(WebDatabaseMigrationTest, MigrateVersion64ToCurrent) {
936 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_64.sql")));
938 // Verify pre-conditions.
940 sql::Connection connection;
941 ASSERT_TRUE(connection.Open(GetDatabasePath()));
942 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
944 sql::MetaTable meta_table;
945 ASSERT_TRUE(meta_table.Init(&connection, 64, 64));
947 EXPECT_FALSE(connection.DoesTableExist("server_card_metadata"));
948 EXPECT_FALSE(connection.DoesTableExist("server_address_metadata"));
950 // Add a server address --- make sure it gets an ID.
951 sql::Statement insert_profiles(
952 connection.GetUniqueStatement(
953 "INSERT INTO server_addresses(id, postal_code) "
954 "VALUES ('', 90210)"));
955 insert_profiles.Run();
958 DoMigration();
960 // Verify post-conditions.
962 sql::Connection connection;
963 ASSERT_TRUE(connection.Open(GetDatabasePath()));
964 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
966 // Check version.
967 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
969 EXPECT_TRUE(connection.DoesTableExist("server_card_metadata"));
970 EXPECT_TRUE(connection.DoesTableExist("server_address_metadata"));
972 sql::Statement read_profiles(
973 connection.GetUniqueStatement(
974 "SELECT id, postal_code FROM server_addresses"));
975 ASSERT_TRUE(read_profiles.Step());
976 EXPECT_FALSE(read_profiles.ColumnString(0).empty());
977 EXPECT_EQ("90210", read_profiles.ColumnString(1));