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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/files/file_util.h" | 6 #include "base/files/file_util.h" |
7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/memory/scoped_vector.h" | 9 #include "base/memory/scoped_vector.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 13 #include "base/thread_task_runner_handle.h" |
13 #include "net/base/test_data_directory.h" | 14 #include "net/base/test_data_directory.h" |
14 #include "net/extras/sqlite/sqlite_channel_id_store.h" | 15 #include "net/extras/sqlite/sqlite_channel_id_store.h" |
15 #include "net/ssl/ssl_client_cert_type.h" | 16 #include "net/ssl/ssl_client_cert_type.h" |
16 #include "net/test/cert_test_util.h" | 17 #include "net/test/cert_test_util.h" |
17 #include "sql/statement.h" | 18 #include "sql/statement.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 | 20 |
20 namespace net { | 21 namespace net { |
21 | 22 |
22 const base::FilePath::CharType kTestChannelIDFilename[] = | 23 const base::FilePath::CharType kTestChannelIDFilename[] = |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 exploded_time.minute = 23; | 71 exploded_time.minute = 23; |
71 exploded_time.second = 45; | 72 exploded_time.second = 45; |
72 exploded_time.millisecond = 0; | 73 exploded_time.millisecond = 0; |
73 return base::Time::FromUTCExploded(exploded_time); | 74 return base::Time::FromUTCExploded(exploded_time); |
74 } | 75 } |
75 | 76 |
76 void SetUp() override { | 77 void SetUp() override { |
77 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 78 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
78 store_ = new SQLiteChannelIDStore( | 79 store_ = new SQLiteChannelIDStore( |
79 temp_dir_.path().Append(kTestChannelIDFilename), | 80 temp_dir_.path().Append(kTestChannelIDFilename), |
80 base::MessageLoopProxy::current()); | 81 base::ThreadTaskRunnerHandle::Get()); |
81 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids; | 82 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids; |
82 Load(&channel_ids); | 83 Load(&channel_ids); |
83 ASSERT_EQ(0u, channel_ids.size()); | 84 ASSERT_EQ(0u, channel_ids.size()); |
84 // Make sure the store gets written at least once. | 85 // Make sure the store gets written at least once. |
85 store_->AddChannelID( | 86 store_->AddChannelID( |
86 DefaultChannelIDStore::ChannelID("google.com", | 87 DefaultChannelIDStore::ChannelID("google.com", |
87 base::Time::FromInternalValue(1), | 88 base::Time::FromInternalValue(1), |
88 base::Time::FromInternalValue(2), | 89 base::Time::FromInternalValue(2), |
89 "a", | 90 "a", |
90 "b")); | 91 "b")); |
(...skipping 15 matching lines...) Expand all Loading... |
106 | 107 |
107 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids; | 108 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids; |
108 // Replace the store effectively destroying the current one and forcing it | 109 // Replace the store effectively destroying the current one and forcing it |
109 // to write its data to disk. Then we can see if after loading it again it | 110 // to write its data to disk. Then we can see if after loading it again it |
110 // is still there. | 111 // is still there. |
111 store_ = NULL; | 112 store_ = NULL; |
112 // Make sure we wait until the destructor has run. | 113 // Make sure we wait until the destructor has run. |
113 base::RunLoop().RunUntilIdle(); | 114 base::RunLoop().RunUntilIdle(); |
114 store_ = | 115 store_ = |
115 new SQLiteChannelIDStore(temp_dir_.path().Append(kTestChannelIDFilename), | 116 new SQLiteChannelIDStore(temp_dir_.path().Append(kTestChannelIDFilename), |
116 base::MessageLoopProxy::current()); | 117 base::ThreadTaskRunnerHandle::Get()); |
117 | 118 |
118 // Reload and test for persistence | 119 // Reload and test for persistence |
119 Load(&channel_ids); | 120 Load(&channel_ids); |
120 ASSERT_EQ(2U, channel_ids.size()); | 121 ASSERT_EQ(2U, channel_ids.size()); |
121 DefaultChannelIDStore::ChannelID* goog_channel_id; | 122 DefaultChannelIDStore::ChannelID* goog_channel_id; |
122 DefaultChannelIDStore::ChannelID* foo_channel_id; | 123 DefaultChannelIDStore::ChannelID* foo_channel_id; |
123 if (channel_ids[0]->server_identifier() == "google.com") { | 124 if (channel_ids[0]->server_identifier() == "google.com") { |
124 goog_channel_id = channel_ids[0]; | 125 goog_channel_id = channel_ids[0]; |
125 foo_channel_id = channel_ids[1]; | 126 foo_channel_id = channel_ids[1]; |
126 } else { | 127 } else { |
(...skipping 13 matching lines...) Expand all Loading... |
140 | 141 |
141 // Now delete the cert and check persistence again. | 142 // Now delete the cert and check persistence again. |
142 store_->DeleteChannelID(*channel_ids[0]); | 143 store_->DeleteChannelID(*channel_ids[0]); |
143 store_->DeleteChannelID(*channel_ids[1]); | 144 store_->DeleteChannelID(*channel_ids[1]); |
144 store_ = NULL; | 145 store_ = NULL; |
145 // Make sure we wait until the destructor has run. | 146 // Make sure we wait until the destructor has run. |
146 base::RunLoop().RunUntilIdle(); | 147 base::RunLoop().RunUntilIdle(); |
147 channel_ids.clear(); | 148 channel_ids.clear(); |
148 store_ = | 149 store_ = |
149 new SQLiteChannelIDStore(temp_dir_.path().Append(kTestChannelIDFilename), | 150 new SQLiteChannelIDStore(temp_dir_.path().Append(kTestChannelIDFilename), |
150 base::MessageLoopProxy::current()); | 151 base::ThreadTaskRunnerHandle::Get()); |
151 | 152 |
152 // Reload and check if the cert has been removed. | 153 // Reload and check if the cert has been removed. |
153 Load(&channel_ids); | 154 Load(&channel_ids); |
154 ASSERT_EQ(0U, channel_ids.size()); | 155 ASSERT_EQ(0U, channel_ids.size()); |
155 // Close the store. | 156 // Close the store. |
156 store_ = NULL; | 157 store_ = NULL; |
157 // Make sure we wait until the destructor has run. | 158 // Make sure we wait until the destructor has run. |
158 base::RunLoop().RunUntilIdle(); | 159 base::RunLoop().RunUntilIdle(); |
159 } | 160 } |
160 | 161 |
161 // Test if data is stored as expected in the SQLite database. | 162 // Test if data is stored as expected in the SQLite database. |
162 TEST_F(SQLiteChannelIDStoreTest, TestDeleteAll) { | 163 TEST_F(SQLiteChannelIDStoreTest, TestDeleteAll) { |
163 store_->AddChannelID( | 164 store_->AddChannelID( |
164 DefaultChannelIDStore::ChannelID("foo.com", | 165 DefaultChannelIDStore::ChannelID("foo.com", |
165 base::Time::FromInternalValue(3), | 166 base::Time::FromInternalValue(3), |
166 base::Time::FromInternalValue(4), | 167 base::Time::FromInternalValue(4), |
167 "c", | 168 "c", |
168 "d")); | 169 "d")); |
169 | 170 |
170 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids; | 171 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids; |
171 // Replace the store effectively destroying the current one and forcing it | 172 // Replace the store effectively destroying the current one and forcing it |
172 // to write its data to disk. Then we can see if after loading it again it | 173 // to write its data to disk. Then we can see if after loading it again it |
173 // is still there. | 174 // is still there. |
174 store_ = NULL; | 175 store_ = NULL; |
175 // Make sure we wait until the destructor has run. | 176 // Make sure we wait until the destructor has run. |
176 base::RunLoop().RunUntilIdle(); | 177 base::RunLoop().RunUntilIdle(); |
177 store_ = | 178 store_ = |
178 new SQLiteChannelIDStore(temp_dir_.path().Append(kTestChannelIDFilename), | 179 new SQLiteChannelIDStore(temp_dir_.path().Append(kTestChannelIDFilename), |
179 base::MessageLoopProxy::current()); | 180 base::ThreadTaskRunnerHandle::Get()); |
180 | 181 |
181 // Reload and test for persistence | 182 // Reload and test for persistence |
182 Load(&channel_ids); | 183 Load(&channel_ids); |
183 ASSERT_EQ(2U, channel_ids.size()); | 184 ASSERT_EQ(2U, channel_ids.size()); |
184 // DeleteAll except foo.com (shouldn't fail if one is missing either). | 185 // DeleteAll except foo.com (shouldn't fail if one is missing either). |
185 std::list<std::string> delete_server_identifiers; | 186 std::list<std::string> delete_server_identifiers; |
186 delete_server_identifiers.push_back("google.com"); | 187 delete_server_identifiers.push_back("google.com"); |
187 delete_server_identifiers.push_back("missing.com"); | 188 delete_server_identifiers.push_back("missing.com"); |
188 store_->DeleteAllInList(delete_server_identifiers); | 189 store_->DeleteAllInList(delete_server_identifiers); |
189 | 190 |
190 // Now check persistence again. | 191 // Now check persistence again. |
191 store_ = NULL; | 192 store_ = NULL; |
192 // Make sure we wait until the destructor has run. | 193 // Make sure we wait until the destructor has run. |
193 base::RunLoop().RunUntilIdle(); | 194 base::RunLoop().RunUntilIdle(); |
194 channel_ids.clear(); | 195 channel_ids.clear(); |
195 store_ = | 196 store_ = |
196 new SQLiteChannelIDStore(temp_dir_.path().Append(kTestChannelIDFilename), | 197 new SQLiteChannelIDStore(temp_dir_.path().Append(kTestChannelIDFilename), |
197 base::MessageLoopProxy::current()); | 198 base::ThreadTaskRunnerHandle::Get()); |
198 | 199 |
199 // Reload and check that only foo.com persisted in store. | 200 // Reload and check that only foo.com persisted in store. |
200 Load(&channel_ids); | 201 Load(&channel_ids); |
201 ASSERT_EQ(1U, channel_ids.size()); | 202 ASSERT_EQ(1U, channel_ids.size()); |
202 ASSERT_EQ("foo.com", channel_ids[0]->server_identifier()); | 203 ASSERT_EQ("foo.com", channel_ids[0]->server_identifier()); |
203 // Close the store. | 204 // Close the store. |
204 store_ = NULL; | 205 store_ = NULL; |
205 // Make sure we wait until the destructor has run. | 206 // Make sure we wait until the destructor has run. |
206 base::RunLoop().RunUntilIdle(); | 207 base::RunLoop().RunUntilIdle(); |
207 } | 208 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 "'foo.com',X'AA',X'BB');")); | 243 "'foo.com',X'AA',X'BB');")); |
243 } | 244 } |
244 | 245 |
245 // Load and test the DB contents twice. First time ensures that we can use | 246 // Load and test the DB contents twice. First time ensures that we can use |
246 // the updated values immediately. Second time ensures that the updated | 247 // the updated values immediately. Second time ensures that the updated |
247 // values are stored and read correctly on next load. | 248 // values are stored and read correctly on next load. |
248 for (int i = 0; i < 2; ++i) { | 249 for (int i = 0; i < 2; ++i) { |
249 SCOPED_TRACE(i); | 250 SCOPED_TRACE(i); |
250 | 251 |
251 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids; | 252 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids; |
252 store_ = | 253 store_ = new SQLiteChannelIDStore(v1_db_path, |
253 new SQLiteChannelIDStore(v1_db_path, base::MessageLoopProxy::current()); | 254 base::ThreadTaskRunnerHandle::Get()); |
254 | 255 |
255 // Load the database. Because the existing v1 certs are implicitly of type | 256 // Load the database. Because the existing v1 certs are implicitly of type |
256 // RSA, which is unsupported, they're discarded. | 257 // RSA, which is unsupported, they're discarded. |
257 Load(&channel_ids); | 258 Load(&channel_ids); |
258 ASSERT_EQ(0U, channel_ids.size()); | 259 ASSERT_EQ(0U, channel_ids.size()); |
259 | 260 |
260 store_ = NULL; | 261 store_ = NULL; |
261 base::RunLoop().RunUntilIdle(); | 262 base::RunLoop().RunUntilIdle(); |
262 | 263 |
263 // Verify the database version is updated. | 264 // Verify the database version is updated. |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 "'foo.com',X'AA',X'BB',64);")); | 313 "'foo.com',X'AA',X'BB',64);")); |
313 } | 314 } |
314 | 315 |
315 // Load and test the DB contents twice. First time ensures that we can use | 316 // Load and test the DB contents twice. First time ensures that we can use |
316 // the updated values immediately. Second time ensures that the updated | 317 // the updated values immediately. Second time ensures that the updated |
317 // values are saved and read correctly on next load. | 318 // values are saved and read correctly on next load. |
318 for (int i = 0; i < 2; ++i) { | 319 for (int i = 0; i < 2; ++i) { |
319 SCOPED_TRACE(i); | 320 SCOPED_TRACE(i); |
320 | 321 |
321 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids; | 322 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids; |
322 store_ = | 323 store_ = new SQLiteChannelIDStore(v2_db_path, |
323 new SQLiteChannelIDStore(v2_db_path, base::MessageLoopProxy::current()); | 324 base::ThreadTaskRunnerHandle::Get()); |
324 | 325 |
325 // Load the database and ensure the certs can be read. | 326 // Load the database and ensure the certs can be read. |
326 Load(&channel_ids); | 327 Load(&channel_ids); |
327 ASSERT_EQ(2U, channel_ids.size()); | 328 ASSERT_EQ(2U, channel_ids.size()); |
328 | 329 |
329 ASSERT_EQ("google.com", channel_ids[0]->server_identifier()); | 330 ASSERT_EQ("google.com", channel_ids[0]->server_identifier()); |
330 ASSERT_EQ(GetTestCertExpirationTime(), channel_ids[0]->expiration_time()); | 331 ASSERT_EQ(GetTestCertExpirationTime(), channel_ids[0]->expiration_time()); |
331 ASSERT_EQ(key_data, channel_ids[0]->private_key()); | 332 ASSERT_EQ(key_data, channel_ids[0]->private_key()); |
332 ASSERT_EQ(cert_data, channel_ids[0]->cert()); | 333 ASSERT_EQ(cert_data, channel_ids[0]->cert()); |
333 | 334 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 "'foo.com',X'AA',X'BB',64,2000);")); | 396 "'foo.com',X'AA',X'BB',64,2000);")); |
396 } | 397 } |
397 | 398 |
398 // Load and test the DB contents twice. First time ensures that we can use | 399 // Load and test the DB contents twice. First time ensures that we can use |
399 // the updated values immediately. Second time ensures that the updated | 400 // the updated values immediately. Second time ensures that the updated |
400 // values are saved and read correctly on next load. | 401 // values are saved and read correctly on next load. |
401 for (int i = 0; i < 2; ++i) { | 402 for (int i = 0; i < 2; ++i) { |
402 SCOPED_TRACE(i); | 403 SCOPED_TRACE(i); |
403 | 404 |
404 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids; | 405 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids; |
405 store_ = | 406 store_ = new SQLiteChannelIDStore(v3_db_path, |
406 new SQLiteChannelIDStore(v3_db_path, base::MessageLoopProxy::current()); | 407 base::ThreadTaskRunnerHandle::Get()); |
407 | 408 |
408 // Load the database and ensure the certs can be read. | 409 // Load the database and ensure the certs can be read. |
409 Load(&channel_ids); | 410 Load(&channel_ids); |
410 ASSERT_EQ(2U, channel_ids.size()); | 411 ASSERT_EQ(2U, channel_ids.size()); |
411 | 412 |
412 ASSERT_EQ("google.com", channel_ids[0]->server_identifier()); | 413 ASSERT_EQ("google.com", channel_ids[0]->server_identifier()); |
413 ASSERT_EQ(1000, channel_ids[0]->expiration_time().ToInternalValue()); | 414 ASSERT_EQ(1000, channel_ids[0]->expiration_time().ToInternalValue()); |
414 ASSERT_EQ(GetTestCertCreationTime(), channel_ids[0]->creation_time()); | 415 ASSERT_EQ(GetTestCertCreationTime(), channel_ids[0]->creation_time()); |
415 ASSERT_EQ(key_data, channel_ids[0]->private_key()); | 416 ASSERT_EQ(key_data, channel_ids[0]->private_key()); |
416 ASSERT_EQ(cert_data, channel_ids[0]->cert()); | 417 ASSERT_EQ(cert_data, channel_ids[0]->cert()); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
487 add_smt.BindBlob(1, key_data.data(), key_data.size()); | 488 add_smt.BindBlob(1, key_data.data(), key_data.size()); |
488 add_smt.BindBlob(2, cert_data.data(), cert_data.size()); | 489 add_smt.BindBlob(2, cert_data.data(), cert_data.size()); |
489 add_smt.BindInt64(3, 1); | 490 add_smt.BindInt64(3, 1); |
490 add_smt.BindInt64(4, GetTestCertExpirationTime().ToInternalValue()); | 491 add_smt.BindInt64(4, GetTestCertExpirationTime().ToInternalValue()); |
491 add_smt.BindInt64(5, base::Time::Now().ToInternalValue()); | 492 add_smt.BindInt64(5, base::Time::Now().ToInternalValue()); |
492 ASSERT_TRUE(add_smt.Run()); | 493 ASSERT_TRUE(add_smt.Run()); |
493 } | 494 } |
494 | 495 |
495 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids; | 496 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids; |
496 store_ = | 497 store_ = |
497 new SQLiteChannelIDStore(v4_db_path, base::MessageLoopProxy::current()); | 498 new SQLiteChannelIDStore(v4_db_path, base::ThreadTaskRunnerHandle::Get()); |
498 | 499 |
499 // Load the database and ensure the certs can be read. | 500 // Load the database and ensure the certs can be read. |
500 Load(&channel_ids); | 501 Load(&channel_ids); |
501 // Only the ECDSA cert (for google.com) is read, the RSA one is discarded. | 502 // Only the ECDSA cert (for google.com) is read, the RSA one is discarded. |
502 ASSERT_EQ(1U, channel_ids.size()); | 503 ASSERT_EQ(1U, channel_ids.size()); |
503 | 504 |
504 ASSERT_EQ("google.com", channel_ids[0]->server_identifier()); | 505 ASSERT_EQ("google.com", channel_ids[0]->server_identifier()); |
505 ASSERT_EQ(GetTestCertExpirationTime(), channel_ids[0]->expiration_time()); | 506 ASSERT_EQ(GetTestCertExpirationTime(), channel_ids[0]->expiration_time()); |
506 ASSERT_EQ(key_data, channel_ids[0]->private_key()); | 507 ASSERT_EQ(key_data, channel_ids[0]->private_key()); |
507 ASSERT_EQ(cert_data, channel_ids[0]->cert()); | 508 ASSERT_EQ(cert_data, channel_ids[0]->cert()); |
508 | 509 |
509 store_ = NULL; | 510 store_ = NULL; |
510 // Make sure we wait until the destructor has run. | 511 // Make sure we wait until the destructor has run. |
511 base::RunLoop().RunUntilIdle(); | 512 base::RunLoop().RunUntilIdle(); |
512 } | 513 } |
513 | 514 |
514 } // namespace net | 515 } // namespace net |
OLD | NEW |