OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/extras/sqlite/sqlite_channel_id_store.h" | 5 #include "net/extras/sqlite/sqlite_channel_id_store.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
281 store_ = NULL; | 281 store_ = NULL; |
282 base::RunLoop().RunUntilIdle(); | 282 base::RunLoop().RunUntilIdle(); |
283 | 283 |
284 // Verify the database version is updated. | 284 // Verify the database version is updated. |
285 { | 285 { |
286 sql::Connection db; | 286 sql::Connection db; |
287 ASSERT_TRUE(db.Open(v1_db_path)); | 287 ASSERT_TRUE(db.Open(v1_db_path)); |
288 sql::Statement smt(db.GetUniqueStatement( | 288 sql::Statement smt(db.GetUniqueStatement( |
289 "SELECT value FROM meta WHERE key = \"version\"")); | 289 "SELECT value FROM meta WHERE key = \"version\"")); |
290 ASSERT_TRUE(smt.Step()); | 290 ASSERT_TRUE(smt.Step()); |
291 EXPECT_EQ(5, smt.ColumnInt(0)); | 291 EXPECT_EQ(6, smt.ColumnInt(0)); |
292 EXPECT_FALSE(smt.Step()); | 292 EXPECT_FALSE(smt.Step()); |
293 } | 293 } |
294 } | 294 } |
295 } | 295 } |
296 | 296 |
297 TEST_F(SQLiteChannelIDStoreTest, TestUpgradeV2) { | 297 TEST_F(SQLiteChannelIDStoreTest, TestUpgradeV2) { |
298 // Reset the store. We'll be using a different database for this test. | 298 // Reset the store. We'll be using a different database for this test. |
299 store_ = NULL; | 299 store_ = NULL; |
300 | 300 |
301 base::FilePath v2_db_path(temp_dir_.GetPath().AppendASCII("v2db")); | 301 base::FilePath v2_db_path(temp_dir_.GetPath().AppendASCII("v2db")); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
357 // Make sure we wait until the destructor has run. | 357 // Make sure we wait until the destructor has run. |
358 base::RunLoop().RunUntilIdle(); | 358 base::RunLoop().RunUntilIdle(); |
359 | 359 |
360 // Verify the database version is updated. | 360 // Verify the database version is updated. |
361 { | 361 { |
362 sql::Connection db; | 362 sql::Connection db; |
363 ASSERT_TRUE(db.Open(v2_db_path)); | 363 ASSERT_TRUE(db.Open(v2_db_path)); |
364 sql::Statement smt(db.GetUniqueStatement( | 364 sql::Statement smt(db.GetUniqueStatement( |
365 "SELECT value FROM meta WHERE key = \"version\"")); | 365 "SELECT value FROM meta WHERE key = \"version\"")); |
366 ASSERT_TRUE(smt.Step()); | 366 ASSERT_TRUE(smt.Step()); |
367 EXPECT_EQ(5, smt.ColumnInt(0)); | 367 EXPECT_EQ(6, smt.ColumnInt(0)); |
368 EXPECT_FALSE(smt.Step()); | 368 EXPECT_FALSE(smt.Step()); |
369 } | 369 } |
370 } | 370 } |
371 } | 371 } |
372 | 372 |
373 TEST_F(SQLiteChannelIDStoreTest, TestUpgradeV3) { | 373 TEST_F(SQLiteChannelIDStoreTest, TestUpgradeV3) { |
374 // Reset the store. We'll be using a different database for this test. | 374 // Reset the store. We'll be using a different database for this test. |
375 store_ = NULL; | 375 store_ = NULL; |
376 | 376 |
377 base::FilePath v3_db_path(temp_dir_.GetPath().AppendASCII("v3db")); | 377 base::FilePath v3_db_path(temp_dir_.GetPath().AppendASCII("v3db")); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
435 // Make sure we wait until the destructor has run. | 435 // Make sure we wait until the destructor has run. |
436 base::RunLoop().RunUntilIdle(); | 436 base::RunLoop().RunUntilIdle(); |
437 | 437 |
438 // Verify the database version is updated. | 438 // Verify the database version is updated. |
439 { | 439 { |
440 sql::Connection db; | 440 sql::Connection db; |
441 ASSERT_TRUE(db.Open(v3_db_path)); | 441 ASSERT_TRUE(db.Open(v3_db_path)); |
442 sql::Statement smt(db.GetUniqueStatement( | 442 sql::Statement smt(db.GetUniqueStatement( |
443 "SELECT value FROM meta WHERE key = \"version\"")); | 443 "SELECT value FROM meta WHERE key = \"version\"")); |
444 ASSERT_TRUE(smt.Step()); | 444 ASSERT_TRUE(smt.Step()); |
445 EXPECT_EQ(5, smt.ColumnInt(0)); | 445 EXPECT_EQ(6, smt.ColumnInt(0)); |
446 EXPECT_FALSE(smt.Step()); | 446 EXPECT_FALSE(smt.Step()); |
447 } | 447 } |
448 } | 448 } |
449 } | 449 } |
450 | 450 |
451 TEST_F(SQLiteChannelIDStoreTest, TestUpgradeV4) { | 451 TEST_F(SQLiteChannelIDStoreTest, TestUpgradeV4) { |
452 // Reset the store. We'll be using a different database for this test. | 452 // Reset the store. We'll be using a different database for this test. |
453 store_ = NULL; | 453 store_ = NULL; |
454 | 454 |
455 base::FilePath v4_db_path(temp_dir_.GetPath().AppendASCII("v4db")); | 455 base::FilePath v4_db_path(temp_dir_.GetPath().AppendASCII("v4db")); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
529 // Make sure we wait until the destructor has run. | 529 // Make sure we wait until the destructor has run. |
530 base::RunLoop().RunUntilIdle(); | 530 base::RunLoop().RunUntilIdle(); |
531 | 531 |
532 // Verify the database version is updated. | 532 // Verify the database version is updated. |
533 { | 533 { |
534 sql::Connection db; | 534 sql::Connection db; |
535 ASSERT_TRUE(db.Open(v4_db_path)); | 535 ASSERT_TRUE(db.Open(v4_db_path)); |
536 sql::Statement smt(db.GetUniqueStatement( | 536 sql::Statement smt(db.GetUniqueStatement( |
537 "SELECT value FROM meta WHERE key = \"version\"")); | 537 "SELECT value FROM meta WHERE key = \"version\"")); |
538 ASSERT_TRUE(smt.Step()); | 538 ASSERT_TRUE(smt.Step()); |
539 EXPECT_EQ(5, smt.ColumnInt(0)); | 539 EXPECT_EQ(6, smt.ColumnInt(0)); |
540 EXPECT_FALSE(smt.Step()); | 540 EXPECT_FALSE(smt.Step()); |
541 } | 541 } |
542 } | 542 } |
543 } | |
544 | |
545 TEST_F(SQLiteChannelIDStoreTest, TestUpgradeV5) { | |
546 // Reset the store. We'll be using a different database for this test. | |
547 store_ = NULL; | |
548 | |
549 base::FilePath v5_db_path(temp_dir_.GetPath().AppendASCII("v5db")); | |
550 | |
551 std::string key_data; | |
552 std::string cert_data; | |
553 std::unique_ptr<crypto::ECPrivateKey> key; | |
554 ASSERT_NO_FATAL_FAILURE(ReadTestKeyAndCert(&key_data, &cert_data, &key)); | |
555 | |
556 // Create a version 4 database. | |
mattm
2017/04/04 22:44:24
5
nharper
2017/04/04 23:01:31
Done.
| |
557 { | |
558 sql::Connection db; | |
559 ASSERT_TRUE(db.Open(v5_db_path)); | |
560 ASSERT_TRUE(db.Execute( | |
561 "CREATE TABLE meta(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY," | |
562 "value LONGVARCHAR);" | |
563 "INSERT INTO \"meta\" VALUES('version','5');" | |
564 "INSERT INTO \"meta\" VALUES('last_compatible_version','5');" | |
565 "CREATE TABLE channel_id (" | |
566 "host TEXT NOT NULL UNIQUE PRIMARY KEY," | |
567 "private_key BLOB NOT NULL," | |
568 "public_key BLOB NOT NULL," | |
569 "creation_time INTEGER);")); | |
570 | |
571 sql::Statement add_smt(db.GetUniqueStatement( | |
572 "INSERT INTO channel_id (host, private_key, public_key, creation_time) " | |
573 "VALUES (?,?,?,?)")); | |
574 add_smt.BindString(0, "google.com"); | |
575 add_smt.BindBlob(1, key_data.data(), key_data.size()); | |
576 add_smt.BindBlob(2, "", 0); | |
577 add_smt.BindInt64(3, GetTestCertCreationTime().ToInternalValue()); | |
578 ASSERT_TRUE(add_smt.Run()); | |
579 | |
580 // Malformed keys will be ignored and not migrated. | |
581 ASSERT_TRUE( | |
582 db.Execute("INSERT INTO \"channel_id\" VALUES(" | |
583 "'bar.com',X'AA',X'BB',3000);")); | |
584 } | |
585 | |
586 // Load and test the DB contents twice. First time ensures that we can use | |
587 // the updated values immediately. Second time ensures that the updated | |
588 // values are saved and read correctly on next load. | |
589 for (int i = 0; i < 2; ++i) { | |
590 SCOPED_TRACE(i); | |
591 | |
592 std::vector<std::unique_ptr<DefaultChannelIDStore::ChannelID>> channel_ids; | |
593 store_ = new SQLiteChannelIDStore(v5_db_path, | |
594 base::ThreadTaskRunnerHandle::Get()); | |
595 | |
596 // Load the database and ensure the certs can be read. | |
597 Load(&channel_ids); | |
598 ASSERT_EQ(1U, channel_ids.size()); | |
599 | |
600 ASSERT_EQ("google.com", channel_ids[0]->server_identifier()); | |
601 ASSERT_EQ(GetTestCertCreationTime(), channel_ids[0]->creation_time()); | |
602 EXPECT_TRUE(KeysEqual(key.get(), channel_ids[0]->key())); | |
603 | |
604 store_ = NULL; | |
605 // Make sure we wait until the destructor has run. | |
606 base::RunLoop().RunUntilIdle(); | |
607 | |
608 // Verify the database version is updated. | |
609 { | |
610 sql::Connection db; | |
611 ASSERT_TRUE(db.Open(v5_db_path)); | |
612 sql::Statement smt(db.GetUniqueStatement( | |
613 "SELECT value FROM meta WHERE key = \"version\"")); | |
614 ASSERT_TRUE(smt.Step()); | |
615 EXPECT_EQ(6, smt.ColumnInt(0)); | |
616 EXPECT_FALSE(smt.Step()); | |
617 } | |
618 } | |
543 } | 619 } |
544 | 620 |
545 } // namespace net | 621 } // namespace net |
OLD | NEW |