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 <vector> |
| 6 |
5 #include "base/bind.h" | 7 #include "base/bind.h" |
6 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
7 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
8 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
9 #include "base/memory/scoped_vector.h" | 11 #include "base/memory/scoped_ptr.h" |
10 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
11 #include "base/thread_task_runner_handle.h" | 13 #include "base/thread_task_runner_handle.h" |
12 #include "crypto/ec_private_key.h" | 14 #include "crypto/ec_private_key.h" |
13 #include "net/base/test_data_directory.h" | 15 #include "net/base/test_data_directory.h" |
14 #include "net/cert/asn1_util.h" | 16 #include "net/cert/asn1_util.h" |
15 #include "net/extras/sqlite/sqlite_channel_id_store.h" | 17 #include "net/extras/sqlite/sqlite_channel_id_store.h" |
16 #include "net/ssl/channel_id_service.h" | 18 #include "net/ssl/channel_id_service.h" |
17 #include "net/ssl/ssl_client_cert_type.h" | 19 #include "net/ssl/ssl_client_cert_type.h" |
18 #include "net/test/cert_test_util.h" | 20 #include "net/test/cert_test_util.h" |
19 #include "net/test/channel_id_test_util.h" | 21 #include "net/test/channel_id_test_util.h" |
20 #include "sql/statement.h" | 22 #include "sql/statement.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
22 | 24 |
23 namespace net { | 25 namespace net { |
24 | 26 |
25 const base::FilePath::CharType kTestChannelIDFilename[] = | 27 const base::FilePath::CharType kTestChannelIDFilename[] = |
26 FILE_PATH_LITERAL("ChannelID"); | 28 FILE_PATH_LITERAL("ChannelID"); |
27 | 29 |
28 class SQLiteChannelIDStoreTest : public testing::Test { | 30 class SQLiteChannelIDStoreTest : public testing::Test { |
29 public: | 31 public: |
30 void Load(ScopedVector<DefaultChannelIDStore::ChannelID>* channel_ids) { | 32 void Load( |
| 33 std::vector<scoped_ptr<DefaultChannelIDStore::ChannelID>>* channel_ids) { |
31 base::RunLoop run_loop; | 34 base::RunLoop run_loop; |
32 store_->Load(base::Bind(&SQLiteChannelIDStoreTest::OnLoaded, | 35 store_->Load(base::Bind(&SQLiteChannelIDStoreTest::OnLoaded, |
33 base::Unretained(this), | 36 base::Unretained(this), |
34 &run_loop)); | 37 &run_loop)); |
35 run_loop.Run(); | 38 run_loop.Run(); |
36 channel_ids->swap(channel_ids_); | 39 channel_ids->swap(channel_ids_); |
37 channel_ids_.clear(); | 40 channel_ids_.clear(); |
38 } | 41 } |
39 | 42 |
40 void OnLoaded( | 43 void OnLoaded( |
41 base::RunLoop* run_loop, | 44 base::RunLoop* run_loop, |
42 scoped_ptr<ScopedVector<DefaultChannelIDStore::ChannelID> > channel_ids) { | 45 scoped_ptr<std::vector<scoped_ptr<DefaultChannelIDStore::ChannelID>>> |
| 46 channel_ids) { |
43 channel_ids_.swap(*channel_ids); | 47 channel_ids_.swap(*channel_ids); |
44 run_loop->Quit(); | 48 run_loop->Quit(); |
45 } | 49 } |
46 | 50 |
47 protected: | 51 protected: |
48 static void ReadTestKeyAndCert(std::string* key_data, | 52 static void ReadTestKeyAndCert(std::string* key_data, |
49 std::string* cert_data, | 53 std::string* cert_data, |
50 scoped_ptr<crypto::ECPrivateKey>* key) { | 54 scoped_ptr<crypto::ECPrivateKey>* key) { |
51 base::FilePath key_path = | 55 base::FilePath key_path = |
52 GetTestCertsDirectory().AppendASCII("unittest.originbound.key.der"); | 56 GetTestCertsDirectory().AppendASCII("unittest.originbound.key.der"); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 exploded_time.second = 39; | 98 exploded_time.second = 39; |
95 exploded_time.millisecond = 0; | 99 exploded_time.millisecond = 0; |
96 return base::Time::FromUTCExploded(exploded_time); | 100 return base::Time::FromUTCExploded(exploded_time); |
97 } | 101 } |
98 | 102 |
99 void SetUp() override { | 103 void SetUp() override { |
100 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 104 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
101 store_ = new SQLiteChannelIDStore( | 105 store_ = new SQLiteChannelIDStore( |
102 temp_dir_.path().Append(kTestChannelIDFilename), | 106 temp_dir_.path().Append(kTestChannelIDFilename), |
103 base::ThreadTaskRunnerHandle::Get()); | 107 base::ThreadTaskRunnerHandle::Get()); |
104 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids; | 108 std::vector<scoped_ptr<DefaultChannelIDStore::ChannelID>> channel_ids; |
105 Load(&channel_ids); | 109 Load(&channel_ids); |
106 ASSERT_EQ(0u, channel_ids.size()); | 110 ASSERT_EQ(0u, channel_ids.size()); |
107 // Make sure the store gets written at least once. | 111 // Make sure the store gets written at least once. |
108 google_key_.reset(crypto::ECPrivateKey::Create()); | 112 google_key_.reset(crypto::ECPrivateKey::Create()); |
109 store_->AddChannelID(DefaultChannelIDStore::ChannelID( | 113 store_->AddChannelID(DefaultChannelIDStore::ChannelID( |
110 "google.com", base::Time::FromInternalValue(1), | 114 "google.com", base::Time::FromInternalValue(1), |
111 make_scoped_ptr(google_key_->Copy()))); | 115 make_scoped_ptr(google_key_->Copy()))); |
112 } | 116 } |
113 | 117 |
114 base::ScopedTempDir temp_dir_; | 118 base::ScopedTempDir temp_dir_; |
115 scoped_refptr<SQLiteChannelIDStore> store_; | 119 scoped_refptr<SQLiteChannelIDStore> store_; |
116 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids_; | 120 std::vector<scoped_ptr<DefaultChannelIDStore::ChannelID>> channel_ids_; |
117 scoped_ptr<crypto::ECPrivateKey> google_key_; | 121 scoped_ptr<crypto::ECPrivateKey> google_key_; |
118 }; | 122 }; |
119 | 123 |
120 // Test if data is stored as expected in the SQLite database. | 124 // Test if data is stored as expected in the SQLite database. |
121 TEST_F(SQLiteChannelIDStoreTest, TestPersistence) { | 125 TEST_F(SQLiteChannelIDStoreTest, TestPersistence) { |
122 scoped_ptr<crypto::ECPrivateKey> foo_key(crypto::ECPrivateKey::Create()); | 126 scoped_ptr<crypto::ECPrivateKey> foo_key(crypto::ECPrivateKey::Create()); |
123 store_->AddChannelID(DefaultChannelIDStore::ChannelID( | 127 store_->AddChannelID(DefaultChannelIDStore::ChannelID( |
124 "foo.com", base::Time::FromInternalValue(3), | 128 "foo.com", base::Time::FromInternalValue(3), |
125 make_scoped_ptr(foo_key->Copy()))); | 129 make_scoped_ptr(foo_key->Copy()))); |
126 | 130 |
127 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids; | 131 std::vector<scoped_ptr<DefaultChannelIDStore::ChannelID>> channel_ids; |
128 // Replace the store effectively destroying the current one and forcing it | 132 // Replace the store effectively destroying the current one and forcing it |
129 // to write its data to disk. Then we can see if after loading it again it | 133 // to write its data to disk. Then we can see if after loading it again it |
130 // is still there. | 134 // is still there. |
131 store_ = NULL; | 135 store_ = NULL; |
132 // Make sure we wait until the destructor has run. | 136 // Make sure we wait until the destructor has run. |
133 base::RunLoop().RunUntilIdle(); | 137 base::RunLoop().RunUntilIdle(); |
134 store_ = | 138 store_ = |
135 new SQLiteChannelIDStore(temp_dir_.path().Append(kTestChannelIDFilename), | 139 new SQLiteChannelIDStore(temp_dir_.path().Append(kTestChannelIDFilename), |
136 base::ThreadTaskRunnerHandle::Get()); | 140 base::ThreadTaskRunnerHandle::Get()); |
137 | 141 |
138 // Reload and test for persistence | 142 // Reload and test for persistence |
139 Load(&channel_ids); | 143 Load(&channel_ids); |
140 ASSERT_EQ(2U, channel_ids.size()); | 144 ASSERT_EQ(2U, channel_ids.size()); |
141 DefaultChannelIDStore::ChannelID* goog_channel_id; | 145 DefaultChannelIDStore::ChannelID* goog_channel_id; |
142 DefaultChannelIDStore::ChannelID* foo_channel_id; | 146 DefaultChannelIDStore::ChannelID* foo_channel_id; |
143 if (channel_ids[0]->server_identifier() == "google.com") { | 147 if (channel_ids[0]->server_identifier() == "google.com") { |
144 goog_channel_id = channel_ids[0]; | 148 goog_channel_id = channel_ids[0].get(); |
145 foo_channel_id = channel_ids[1]; | 149 foo_channel_id = channel_ids[1].get(); |
146 } else { | 150 } else { |
147 goog_channel_id = channel_ids[1]; | 151 goog_channel_id = channel_ids[1].get(); |
148 foo_channel_id = channel_ids[0]; | 152 foo_channel_id = channel_ids[0].get(); |
149 } | 153 } |
150 ASSERT_EQ("google.com", goog_channel_id->server_identifier()); | 154 ASSERT_EQ("google.com", goog_channel_id->server_identifier()); |
151 EXPECT_TRUE(KeysEqual(google_key_.get(), goog_channel_id->key())); | 155 EXPECT_TRUE(KeysEqual(google_key_.get(), goog_channel_id->key())); |
152 ASSERT_EQ(1, goog_channel_id->creation_time().ToInternalValue()); | 156 ASSERT_EQ(1, goog_channel_id->creation_time().ToInternalValue()); |
153 ASSERT_EQ("foo.com", foo_channel_id->server_identifier()); | 157 ASSERT_EQ("foo.com", foo_channel_id->server_identifier()); |
154 EXPECT_TRUE(KeysEqual(foo_key.get(), foo_channel_id->key())); | 158 EXPECT_TRUE(KeysEqual(foo_key.get(), foo_channel_id->key())); |
155 ASSERT_EQ(3, foo_channel_id->creation_time().ToInternalValue()); | 159 ASSERT_EQ(3, foo_channel_id->creation_time().ToInternalValue()); |
156 | 160 |
157 // Now delete the keypair and check persistence again. | 161 // Now delete the keypair and check persistence again. |
158 store_->DeleteChannelID(*channel_ids[0]); | 162 store_->DeleteChannelID(*channel_ids[0]); |
(...skipping 14 matching lines...) Expand all Loading... |
173 // Make sure we wait until the destructor has run. | 177 // Make sure we wait until the destructor has run. |
174 base::RunLoop().RunUntilIdle(); | 178 base::RunLoop().RunUntilIdle(); |
175 } | 179 } |
176 | 180 |
177 // Test if data is stored as expected in the SQLite database. | 181 // Test if data is stored as expected in the SQLite database. |
178 TEST_F(SQLiteChannelIDStoreTest, TestDeleteAll) { | 182 TEST_F(SQLiteChannelIDStoreTest, TestDeleteAll) { |
179 store_->AddChannelID(DefaultChannelIDStore::ChannelID( | 183 store_->AddChannelID(DefaultChannelIDStore::ChannelID( |
180 "foo.com", base::Time::FromInternalValue(3), | 184 "foo.com", base::Time::FromInternalValue(3), |
181 make_scoped_ptr(crypto::ECPrivateKey::Create()))); | 185 make_scoped_ptr(crypto::ECPrivateKey::Create()))); |
182 | 186 |
183 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids; | 187 std::vector<scoped_ptr<DefaultChannelIDStore::ChannelID>> channel_ids; |
184 // Replace the store effectively destroying the current one and forcing it | 188 // Replace the store effectively destroying the current one and forcing it |
185 // to write its data to disk. Then we can see if after loading it again it | 189 // to write its data to disk. Then we can see if after loading it again it |
186 // is still there. | 190 // is still there. |
187 store_ = NULL; | 191 store_ = NULL; |
188 // Make sure we wait until the destructor has run. | 192 // Make sure we wait until the destructor has run. |
189 base::RunLoop().RunUntilIdle(); | 193 base::RunLoop().RunUntilIdle(); |
190 store_ = | 194 store_ = |
191 new SQLiteChannelIDStore(temp_dir_.path().Append(kTestChannelIDFilename), | 195 new SQLiteChannelIDStore(temp_dir_.path().Append(kTestChannelIDFilename), |
192 base::ThreadTaskRunnerHandle::Get()); | 196 base::ThreadTaskRunnerHandle::Get()); |
193 | 197 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 "INSERT INTO \"origin_bound_certs\" VALUES(" | 259 "INSERT INTO \"origin_bound_certs\" VALUES(" |
256 "'foo.com',X'AA',X'BB');")); | 260 "'foo.com',X'AA',X'BB');")); |
257 } | 261 } |
258 | 262 |
259 // Load and test the DB contents twice. First time ensures that we can use | 263 // Load and test the DB contents twice. First time ensures that we can use |
260 // the updated values immediately. Second time ensures that the updated | 264 // the updated values immediately. Second time ensures that the updated |
261 // values are stored and read correctly on next load. | 265 // values are stored and read correctly on next load. |
262 for (int i = 0; i < 2; ++i) { | 266 for (int i = 0; i < 2; ++i) { |
263 SCOPED_TRACE(i); | 267 SCOPED_TRACE(i); |
264 | 268 |
265 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids; | 269 std::vector<scoped_ptr<DefaultChannelIDStore::ChannelID>> channel_ids; |
266 store_ = new SQLiteChannelIDStore(v1_db_path, | 270 store_ = new SQLiteChannelIDStore(v1_db_path, |
267 base::ThreadTaskRunnerHandle::Get()); | 271 base::ThreadTaskRunnerHandle::Get()); |
268 | 272 |
269 // Load the database. Because the existing v1 certs are implicitly of type | 273 // Load the database. Because the existing v1 certs are implicitly of type |
270 // RSA, which is unsupported, they're discarded. | 274 // RSA, which is unsupported, they're discarded. |
271 Load(&channel_ids); | 275 Load(&channel_ids); |
272 ASSERT_EQ(0U, channel_ids.size()); | 276 ASSERT_EQ(0U, channel_ids.size()); |
273 | 277 |
274 store_ = NULL; | 278 store_ = NULL; |
275 base::RunLoop().RunUntilIdle(); | 279 base::RunLoop().RunUntilIdle(); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 "INSERT INTO \"origin_bound_certs\" VALUES(" | 331 "INSERT INTO \"origin_bound_certs\" VALUES(" |
328 "'foo.com',X'AA',X'BB',64);")); | 332 "'foo.com',X'AA',X'BB',64);")); |
329 } | 333 } |
330 | 334 |
331 // Load and test the DB contents twice. First time ensures that we can use | 335 // Load and test the DB contents twice. First time ensures that we can use |
332 // the updated values immediately. Second time ensures that the updated | 336 // the updated values immediately. Second time ensures that the updated |
333 // values are saved and read correctly on next load. | 337 // values are saved and read correctly on next load. |
334 for (int i = 0; i < 2; ++i) { | 338 for (int i = 0; i < 2; ++i) { |
335 SCOPED_TRACE(i); | 339 SCOPED_TRACE(i); |
336 | 340 |
337 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids; | 341 std::vector<scoped_ptr<DefaultChannelIDStore::ChannelID>> channel_ids; |
338 store_ = new SQLiteChannelIDStore(v2_db_path, | 342 store_ = new SQLiteChannelIDStore(v2_db_path, |
339 base::ThreadTaskRunnerHandle::Get()); | 343 base::ThreadTaskRunnerHandle::Get()); |
340 | 344 |
341 // Load the database and ensure the certs can be read. | 345 // Load the database and ensure the certs can be read. |
342 Load(&channel_ids); | 346 Load(&channel_ids); |
343 ASSERT_EQ(1U, channel_ids.size()); | 347 ASSERT_EQ(1U, channel_ids.size()); |
344 | 348 |
345 ASSERT_EQ("google.com", channel_ids[0]->server_identifier()); | 349 ASSERT_EQ("google.com", channel_ids[0]->server_identifier()); |
346 ASSERT_EQ(GetTestCertCreationTime(), channel_ids[0]->creation_time()); | 350 ASSERT_EQ(GetTestCertCreationTime(), channel_ids[0]->creation_time()); |
347 EXPECT_TRUE(KeysEqual(key.get(), channel_ids[0]->key())); | 351 EXPECT_TRUE(KeysEqual(key.get(), channel_ids[0]->key())); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 "INSERT INTO \"origin_bound_certs\" VALUES(" | 409 "INSERT INTO \"origin_bound_certs\" VALUES(" |
406 "'foo.com',X'AA',X'BB',64,2000);")); | 410 "'foo.com',X'AA',X'BB',64,2000);")); |
407 } | 411 } |
408 | 412 |
409 // Load and test the DB contents twice. First time ensures that we can use | 413 // Load and test the DB contents twice. First time ensures that we can use |
410 // the updated values immediately. Second time ensures that the updated | 414 // the updated values immediately. Second time ensures that the updated |
411 // values are saved and read correctly on next load. | 415 // values are saved and read correctly on next load. |
412 for (int i = 0; i < 2; ++i) { | 416 for (int i = 0; i < 2; ++i) { |
413 SCOPED_TRACE(i); | 417 SCOPED_TRACE(i); |
414 | 418 |
415 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids; | 419 std::vector<scoped_ptr<DefaultChannelIDStore::ChannelID>> channel_ids; |
416 store_ = new SQLiteChannelIDStore(v3_db_path, | 420 store_ = new SQLiteChannelIDStore(v3_db_path, |
417 base::ThreadTaskRunnerHandle::Get()); | 421 base::ThreadTaskRunnerHandle::Get()); |
418 | 422 |
419 // Load the database and ensure the certs can be read. | 423 // Load the database and ensure the certs can be read. |
420 Load(&channel_ids); | 424 Load(&channel_ids); |
421 ASSERT_EQ(1U, channel_ids.size()); | 425 ASSERT_EQ(1U, channel_ids.size()); |
422 | 426 |
423 ASSERT_EQ("google.com", channel_ids[0]->server_identifier()); | 427 ASSERT_EQ("google.com", channel_ids[0]->server_identifier()); |
424 ASSERT_EQ(GetTestCertCreationTime(), channel_ids[0]->creation_time()); | 428 ASSERT_EQ(GetTestCertCreationTime(), channel_ids[0]->creation_time()); |
425 EXPECT_TRUE(KeysEqual(key.get(), channel_ids[0]->key())); | 429 EXPECT_TRUE(KeysEqual(key.get(), channel_ids[0]->key())); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
499 "INSERT INTO \"origin_bound_certs\" VALUES(" | 503 "INSERT INTO \"origin_bound_certs\" VALUES(" |
500 "'bar.com',X'AA',X'BB',64,2000,3000);")); | 504 "'bar.com',X'AA',X'BB',64,2000,3000);")); |
501 } | 505 } |
502 | 506 |
503 // Load and test the DB contents twice. First time ensures that we can use | 507 // Load and test the DB contents twice. First time ensures that we can use |
504 // the updated values immediately. Second time ensures that the updated | 508 // the updated values immediately. Second time ensures that the updated |
505 // values are saved and read correctly on next load. | 509 // values are saved and read correctly on next load. |
506 for (int i = 0; i < 2; ++i) { | 510 for (int i = 0; i < 2; ++i) { |
507 SCOPED_TRACE(i); | 511 SCOPED_TRACE(i); |
508 | 512 |
509 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids; | 513 std::vector<scoped_ptr<DefaultChannelIDStore::ChannelID>> channel_ids; |
510 store_ = new SQLiteChannelIDStore(v4_db_path, | 514 store_ = new SQLiteChannelIDStore(v4_db_path, |
511 base::ThreadTaskRunnerHandle::Get()); | 515 base::ThreadTaskRunnerHandle::Get()); |
512 | 516 |
513 // Load the database and ensure the certs can be read. | 517 // Load the database and ensure the certs can be read. |
514 Load(&channel_ids); | 518 Load(&channel_ids); |
515 ASSERT_EQ(1U, channel_ids.size()); | 519 ASSERT_EQ(1U, channel_ids.size()); |
516 | 520 |
517 ASSERT_EQ("google.com", channel_ids[0]->server_identifier()); | 521 ASSERT_EQ("google.com", channel_ids[0]->server_identifier()); |
518 ASSERT_EQ(GetTestCertCreationTime(), channel_ids[0]->creation_time()); | 522 ASSERT_EQ(GetTestCertCreationTime(), channel_ids[0]->creation_time()); |
519 EXPECT_TRUE(KeysEqual(key.get(), channel_ids[0]->key())); | 523 EXPECT_TRUE(KeysEqual(key.get(), channel_ids[0]->key())); |
520 | 524 |
521 store_ = NULL; | 525 store_ = NULL; |
522 // Make sure we wait until the destructor has run. | 526 // Make sure we wait until the destructor has run. |
523 base::RunLoop().RunUntilIdle(); | 527 base::RunLoop().RunUntilIdle(); |
524 | 528 |
525 // Verify the database version is updated. | 529 // Verify the database version is updated. |
526 { | 530 { |
527 sql::Connection db; | 531 sql::Connection db; |
528 ASSERT_TRUE(db.Open(v4_db_path)); | 532 ASSERT_TRUE(db.Open(v4_db_path)); |
529 sql::Statement smt(db.GetUniqueStatement( | 533 sql::Statement smt(db.GetUniqueStatement( |
530 "SELECT value FROM meta WHERE key = \"version\"")); | 534 "SELECT value FROM meta WHERE key = \"version\"")); |
531 ASSERT_TRUE(smt.Step()); | 535 ASSERT_TRUE(smt.Step()); |
532 EXPECT_EQ(5, smt.ColumnInt(0)); | 536 EXPECT_EQ(5, smt.ColumnInt(0)); |
533 EXPECT_FALSE(smt.Step()); | 537 EXPECT_FALSE(smt.Step()); |
534 } | 538 } |
535 } | 539 } |
536 } | 540 } |
537 | 541 |
538 } // namespace net | 542 } // namespace net |
OLD | NEW |